EmbeddedUnit
AssertImpl.c
Go to the documentation of this file.
1 
34 #include "config.h"
35 #include "stdImpl.h"
36 #include "AssertImpl.h"
37 
47 void assertImplementationInt(long expected,long actual,unsigned char base, unsigned long line, const char *file)
48 {
49  char buffer[42]; /*"expected: -2147483647 actual: -2147483647" when base=10*/
50  /*"expected: 0x12345678 actual: 0x12345678" when base=16*/
51  char numbuf[12]; /*32bit int decimal maximum column is 11 (-2147483647~2147483647)*/
52 
53  stdimpl_strcpy(buffer, "expected: ");
54  stdimpl_strncat(buffer, (base==16)?"0x":"", 2);
55 
56  { stdimpl_itoa(expected, numbuf, base);
57  stdimpl_strncat(buffer, numbuf, 11); }
58 
59  stdimpl_strcat(buffer, " actual: ");
60  stdimpl_strncat(buffer, (base==16)?"0x":"", 2);
61 
62  { stdimpl_itoa(actual, numbuf, base);
63  stdimpl_strncat(buffer, numbuf, 11); }
64 
65  addFailure(buffer, line, file);
66 }
67 
77 void assertImplementationUInt(unsigned long expected,unsigned long actual,unsigned char base, unsigned long line, const char *file)
78 {
79  char buffer[42]; /*"expected: -4294967295 actual: -4294967295" when base=10*/
80  /*"expected: 0x12345678 actual: 0x12345678" when base=16*/
81  char numbuf[12]; /*32bit unsigned int decimal maximum column is 11 (-4294967295~4294967296)*/
82 
83  stdimpl_strcpy(buffer, "expected: ");
84  stdimpl_strncat(buffer, (base==16)?"0x":"", 2);
85 
86  { stdimpl_utoa(expected, numbuf, base);
87  stdimpl_strncat(buffer, numbuf, 11); }
88 
89  stdimpl_strcat(buffer, " actual: ");
90  stdimpl_strncat(buffer, (base==16)?"0x":"", 2);
91 
92  { stdimpl_utoa(actual, numbuf, base);
93  stdimpl_strncat(buffer, numbuf, 11); }
94 
95  addFailure(buffer, line, file);
96 }
97 
106 void assertImplementationCStr(const char *expected,const char *actual, unsigned long line, const char *file)
107 {
108  char buffer[ASSERT_STRING_BUFFER_MAX];
109  #define exp_act_limit_cstr ((ASSERT_STRING_BUFFER_MAX-11-1)/2)/* "exp'' was''" = 11 byte */
110  int el;
111  int al;
112 
113  if (expected) {
114  el = stdimpl_strlen(expected);
115  } else {
116  el = 4;
117  expected = "null";
118  }
119 
120  if (actual) {
121  al = stdimpl_strlen(actual);
122  } else {
123  al = 4;
124  actual = "null";
125  }
126  if (el > exp_act_limit_cstr) {
127  if (al > exp_act_limit_cstr) {
128  al = exp_act_limit_cstr;
129  el = exp_act_limit_cstr;
130  } else {
131  int w = exp_act_limit_cstr + (exp_act_limit_cstr - al);
132  if (el > w) {
133  el = w;
134  }
135  }
136  } else {
137  int w = exp_act_limit_cstr + (exp_act_limit_cstr - el);
138  if (al > w) {
139  al = w;
140  }
141  }
142  stdimpl_strcpy(buffer, "exp \"");
143  stdimpl_strncat(buffer, expected, el);
144  stdimpl_strcat(buffer, "\" was \"");
145  stdimpl_strncat(buffer, actual, al);
146  stdimpl_strcat(buffer, "\"");
147 
148  addFailure(buffer, line, file);
149 }
150 
160 void assertImplementationMem(const void *exp,const void *act, int size, unsigned long line, const char *file)
161 {
162  const char* expected = (char*)exp;
163  const char* actual = (char*)act;
164  char buffer[ASSERT_MEMORY_BUFFER_MAX];
165  char numbuf[4];
166  #define exp_act_limit_mem ((ASSERT_MEMORY_BUFFER_MAX-11-1)/2)/* "exp'' was''" = 11 byte */
167  int el;
168  int al;
169  int it;
170 
171  if (expected) {
172  el = 3 * size; /* " xx" = 3 byte */
173  } else {
174  el = 4;
175  expected = "null";
176  }
177 
178  if (actual) {
179  al = 3 * size; /* " xx" = 3 byte */
180  } else {
181  al = 4;
182  actual = "null";
183  }
184  if (el > exp_act_limit_mem) {
185  if (al > exp_act_limit_mem) {
186  al = exp_act_limit_mem;
187  el = exp_act_limit_mem;
188  } else {
189  int w = exp_act_limit_mem + (exp_act_limit_mem - al);
190  if (el > w) {
191  el = w;
192  }
193  }
194  } else {
195  int w = exp_act_limit_mem + (exp_act_limit_mem - el);
196  if (al > w) {
197  al = w;
198  }
199  }
200  stdimpl_strcpy(buffer, "exp \"");
201  it = 0;
202  while (el >= 3) {
203  stdimpl_itoa(expected[it], numbuf, 16);
204  stdimpl_strncat(buffer, numbuf, 2);
205  stdimpl_strncat(buffer, " ", 1);
206  it++;
207  el -= 3;
208  }
209  stdimpl_strcat(buffer, "\" was \"");
210  it = 0;
211  while (al >= 3) {
212  stdimpl_itoa(actual[it], numbuf, 16);
213  stdimpl_strncat(buffer, numbuf, 2);
214  stdimpl_strncat(buffer, " ", 1);
215  it++;
216  al -= 3;
217  }
218  stdimpl_strcat(buffer, "\"");
219 
220  addFailure(buffer, line, file);
221 }
222 
void assertImplementationUInt(unsigned long expected, unsigned long actual, unsigned char base, unsigned long line, const char *file)
Definition: AssertImpl.c:77
void assertImplementationMem(const void *exp, const void *act, int size, unsigned long line, const char *file)
Definition: AssertImpl.c:160
void assertImplementationCStr(const char *expected, const char *actual, unsigned long line, const char *file)
Definition: AssertImpl.c:106
void assertImplementationInt(long expected, long actual, unsigned char base, unsigned long line, const char *file)
Definition: AssertImpl.c:47