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 <setjmp.h>
8 #include <stdio.h>
9 #include "unity.h"
10 #include "types_for_test.h"
11
12 /* Include Passthroughs for Linking Tests */
putcharSpy(int c)13 void putcharSpy(int c) { (void)putchar(c);}
flushSpy(void)14 void flushSpy(void) {}
15
16 #define EXPECT_ABORT_BEGIN \
17 if (TEST_PROTECT()) \
18 {
19
20 #define VERIFY_FAILS_END \
21 } \
22 Unity.CurrentTestFailed = (Unity.CurrentTestFailed != 0) ? 0 : 1; \
23 if (Unity.CurrentTestFailed == 1) { \
24 SetToOneMeanWeAlreadyCheckedThisGuy = 1; \
25 UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \
26 UNITY_OUTPUT_CHAR(':'); \
27 UnityPrint(Unity.CurrentTestName); \
28 UnityPrint(":FAIL: [[[[ Test Should Have Failed But Did Not ]]]]"); \
29 UNITY_OUTPUT_CHAR('\n'); \
30 }
31
32 #define VERIFY_IGNORES_END \
33 } \
34 Unity.CurrentTestFailed = (Unity.CurrentTestIgnored != 0) ? 0 : 1; \
35 Unity.CurrentTestIgnored = 0; \
36 if (Unity.CurrentTestFailed == 1) { \
37 SetToOneMeanWeAlreadyCheckedThisGuy = 1; \
38 UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \
39 UNITY_OUTPUT_CHAR(':'); \
40 UnityPrint(Unity.CurrentTestName); \
41 UnityPrint(":FAIL: [[[[ Test Should Have Ignored But Did Not ]]]]"); \
42 UNITY_OUTPUT_CHAR('\n'); \
43 }
44
45 static int SetToOneToFailInTearDown;
46 static int SetToOneMeanWeAlreadyCheckedThisGuy;
47 static unsigned NextExpectedStringIndex;
48 static unsigned NextExpectedCharIndex;
49 static unsigned NextExpectedSpaceIndex;
50
suiteSetUp(void)51 void suiteSetUp(void)
52 {
53 NextExpectedStringIndex = 0;
54 NextExpectedCharIndex = 0;
55 NextExpectedSpaceIndex = 0;
56 }
57
setUp(void)58 void setUp(void)
59 {
60 SetToOneToFailInTearDown = 0;
61 SetToOneMeanWeAlreadyCheckedThisGuy = 0;
62 }
63
tearDown(void)64 void tearDown(void)
65 {
66 if (SetToOneToFailInTearDown == 1)
67 TEST_FAIL_MESSAGE("<= Failed in tearDown");
68 if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0))
69 {
70 UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]");
71 UNITY_OUTPUT_CHAR('\n');
72 }
73 }
74
75 TEST_CASE(0)
76 TEST_CASE(44)
77 TEST_CASE((90)+9)
test_TheseShouldAllPass(int Num)78 void test_TheseShouldAllPass(int Num)
79 {
80 TEST_ASSERT_TRUE(Num < 100);
81 }
82
83 TEST_CASE(3)
84 TEST_CASE(77)
85 TEST_CASE( (99) + 1 - (1))
test_TheseShouldAllFail(int Num)86 void test_TheseShouldAllFail(int Num)
87 {
88 EXPECT_ABORT_BEGIN
89 TEST_ASSERT_TRUE(Num > 100);
90 VERIFY_FAILS_END
91 }
92
93 TEST_CASE(1)
94 TEST_CASE(44)
95 TEST_CASE(99)
96 TEST_CASE(98)
test_TheseAreEveryOther(int Num)97 void test_TheseAreEveryOther(int Num)
98 {
99 if (Num & 1)
100 {
101 EXPECT_ABORT_BEGIN
102 TEST_ASSERT_TRUE(Num > 100);
103 VERIFY_FAILS_END
104 }
105 else
106 {
107 TEST_ASSERT_TRUE(Num < 100);
108 }
109 }
110
test_NormalPassesStillWork(void)111 void test_NormalPassesStillWork(void)
112 {
113 TEST_ASSERT_TRUE(1);
114 }
115
test_NormalFailsStillWork(void)116 void test_NormalFailsStillWork(void)
117 {
118 EXPECT_ABORT_BEGIN
119 TEST_ASSERT_TRUE(0);
120 VERIFY_FAILS_END
121 }
122
123 TEST_CASE(0, "abc")
124 TEST_CASE(1, "{")
125 TEST_CASE(2, "}")
126 TEST_CASE(3, ";")
127 TEST_CASE(4, "\"quoted\"")
test_StringsArePreserved(unsigned index,const char * str)128 void test_StringsArePreserved(unsigned index, const char * str)
129 {
130 static const char * const expected[] =
131 {
132 "abc",
133 "{",
134 "}",
135 ";",
136 "\"quoted\""
137 };
138
139 /* Ensure that no test cases are skipped by tracking the next expected index */
140 TEST_ASSERT_EQUAL_UINT32(NextExpectedStringIndex, index);
141 TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index);
142 TEST_ASSERT_EQUAL_STRING(expected[index], str);
143
144 NextExpectedStringIndex++;
145 }
146
147 TEST_CASE(0, 'x')
148 TEST_CASE(1, '{')
149 TEST_CASE(2, '}')
150 TEST_CASE(3, ';')
151 TEST_CASE(4, '\'')
152 TEST_CASE(5, '"')
test_CharsArePreserved(unsigned index,char c)153 void test_CharsArePreserved(unsigned index, char c)
154 {
155 static const char expected[] =
156 {
157 'x',
158 '{',
159 '}',
160 ';',
161 '\'',
162 '"'
163 };
164
165 /* Ensure that no test cases are skipped by tracking the next expected index */
166 TEST_ASSERT_EQUAL_UINT32(NextExpectedCharIndex, index);
167 TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index);
168 TEST_ASSERT_EQUAL(expected[index], c);
169
170 NextExpectedCharIndex++;
171 }
172
173 TEST_RANGE([0, 10, 2])
test_SingleRange(unsigned value)174 void test_SingleRange(unsigned value)
175 {
176 TEST_ASSERT_EQUAL(0, value % 2);
177 TEST_ASSERT_LESS_OR_EQUAL(10, value);
178 }
179
180 TEST_RANGE([1, 2, 1], [2, 1, -1])
test_TwoRanges(unsigned first,unsigned second)181 void test_TwoRanges(unsigned first, unsigned second)
182 {
183 TEST_ASSERT_LESS_OR_EQUAL(4, first * second);
184 }
185
186 TEST_RANGE(<0, 10, 2>)
test_SingleExclusiveRange(unsigned value)187 void test_SingleExclusiveRange(unsigned value)
188 {
189 TEST_ASSERT_EQUAL(0, value % 2);
190 TEST_ASSERT_LESS_THAN(10, value);
191 }
192
193 TEST_RANGE([2, 4, 1], <1, 2, 1>)
test_BothInclusiveAndExclusiveRange(unsigned first,unsigned second)194 void test_BothInclusiveAndExclusiveRange(unsigned first, unsigned second)
195 {
196 TEST_ASSERT_LESS_THAN(first, second);
197 }
198
199 TEST_CASE(0,
200
201 1)
202 TEST_CASE(1,
203
204 2
205
206 )
207 TEST_RANGE([2,
208 5 ,
209 1], [6, 6, 1])
210 TEST_CASE(
211
212 6 , 7)
213 TEST_MATRIX([7,
214 8 ,
215
216 9, 10],
217 [
218 11]
219
220 )
test_SpaceInTestCase(unsigned index,unsigned bigger)221 void test_SpaceInTestCase(unsigned index, unsigned bigger)
222 {
223 TEST_ASSERT_EQUAL_UINT32(NextExpectedSpaceIndex, index);
224 TEST_ASSERT_LESS_THAN(bigger, index);
225
226 NextExpectedSpaceIndex++;
227 }
228
229 TEST_MATRIX([1, 5, (2*2)+1, 4])
test_SingleMatix(unsigned value)230 void test_SingleMatix(unsigned value)
231 {
232 TEST_ASSERT_LESS_OR_EQUAL(10, value);
233 }
234
235 TEST_MATRIX([2, 5l, 4u+3, 4ul], [-2, 3])
test_TwoMatrices(unsigned first,signed second)236 void test_TwoMatrices(unsigned first, signed second)
237 {
238 static unsigned idx = 0;
239 static const unsigned expected[] =
240 {
241 // -2 3
242 -4, 6, // 2
243 -10, 15, // 5
244 -14, 21, // 7
245 -8, 12, // 4
246 };
247 TEST_ASSERT_EQUAL_INT(expected[idx++], first * second);
248 }
249
250 TEST_MATRIX(["String1", "String,2", "Stri" "ng3", "String[4]", "String\"5\""], [-5, 12.5f])
test_StringsAndNumbersMatrices(const char * str,float number)251 void test_StringsAndNumbersMatrices(const char* str, float number)
252 {
253 static unsigned idx = 0;
254 static const char* expected[] =
255 {
256 "String1_-05.00",
257 "String1_+12.50",
258 "String,2_-05.00",
259 "String,2_+12.50",
260 "String3_-05.00",
261 "String3_+12.50",
262 "String[4]_-05.00",
263 "String[4]_+12.50",
264 "String\"5\"_-05.00",
265 "String\"5\"_+12.50",
266 };
267 char buf[200] = {0};
268 snprintf(buf, sizeof(buf), "%s_%+06.2f", str, number);
269 TEST_ASSERT_EQUAL_STRING(expected[idx++], buf);
270 }
271
272 TEST_MATRIX(
273 [ENUM_A, ENUM_4, ENUM_C],
274 [test_arr[0], 7.8f, test_arr[2]],
275 ['a', 'f', '[', ']', '\'', '"'],
276 )
test_EnumCharAndArrayMatrices(test_enum_t e,float n,char c)277 void test_EnumCharAndArrayMatrices(test_enum_t e, float n, char c)
278 {
279 static unsigned enum_idx = 0;
280 static const test_enum_t exp_enum[3] = {
281 ENUM_A, ENUM_4, ENUM_C,
282 };
283
284 static unsigned float_idx = 0;
285 float exp_float[3] = {0};
286 exp_float[0] = test_arr[0];
287 exp_float[1] = 7.8f;
288 exp_float[2] = test_arr[2];
289
290 static unsigned char_idx = 0;
291 static const test_enum_t exp_char[] = {
292 'a', 'f', '[', ']', '\'', '"'
293 };
294
295 TEST_ASSERT_EQUAL_INT(exp_enum[enum_idx], e);
296 TEST_ASSERT_EQUAL_FLOAT(exp_float[float_idx], n);
297 TEST_ASSERT_EQUAL_CHAR(exp_char[char_idx], c);
298
299 char_idx = (char_idx + 1) % 6;
300 if (char_idx == 0.0f)
301 {
302 float_idx = (float_idx + 1) % 3;
303 if (float_idx == 0.0f)
304 {
305 enum_idx = (enum_idx + 1) % 3;
306 }
307 }
308 }
309