unity_output_Spy.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (c) 2010 James Grenning and Contributed to Unity Project
  2. * ==========================================
  3. * Unity Project - A Test Framework for C
  4. * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
  5. * [Released under MIT License. Please refer to license.txt for details]
  6. * ========================================== */
  7. #include "unity_output_Spy.h"
  8. #include "unity_fixture.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. static int size;
  12. static int count;
  13. static char* buffer;
  14. static int spy_enable;
  15. void UnityOutputCharSpy_Create(int s)
  16. {
  17. size = (s > 0) ? s : 0;
  18. count = 0;
  19. spy_enable = 0;
  20. buffer = malloc((size_t)size);
  21. TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "Internal malloc failed in Spy Create():" __FILE__);
  22. memset(buffer, 0, (size_t)size);
  23. }
  24. void UnityOutputCharSpy_Destroy(void)
  25. {
  26. size = 0;
  27. free(buffer);
  28. }
  29. void UnityOutputCharSpy_OutputChar(int c)
  30. {
  31. if (spy_enable)
  32. {
  33. if (count < (size-1))
  34. buffer[count++] = (char)c;
  35. }
  36. else
  37. {
  38. putchar(c);
  39. }
  40. }
  41. const char * UnityOutputCharSpy_Get(void)
  42. {
  43. return buffer;
  44. }
  45. void UnityOutputCharSpy_Enable(int enable)
  46. {
  47. spy_enable = enable;
  48. }