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