Debian Codegraph @

vendor/c_exception/test/TestException.c

raw
1 #include "unity.h"
2 #include "CException.h"
3
4 volatile int TestingTheFallback;
5 volatile int TestingTheFallbackId;
6
7 void setUp(void)
8 {
9 CExceptionFrames[0].pFrame = NULL;
10 TestingTheFallback = 0;
11 }
12
13 void tearDown(void)
14 {
15 }
16
17 void test_BasicTryDoesNothingIfNoThrow(void)
18 {
19 int i = 0;
20 CEXCEPTION_T e = 0x5a;
21
22 Try
23 {
24 i += 1;
25 }
26 Catch(e)
27 {
28 TEST_FAIL_MESSAGE("Should Not Enter Catch If Not Thrown");
29 }
30
31 //verify that e was untouched
32 TEST_ASSERT_EQUAL(0x5a, e);
33
34 // verify that i was incremented once
35 TEST_ASSERT_EQUAL(1, i);
36 }
37
38 void test_BasicThrowAndCatch(void)
39 {
40 CEXCEPTION_T e;
41
42 Try
43 {
44 Throw(0xBE);
45 TEST_FAIL_MESSAGE("Should Have Thrown An Error");
46 }
47 Catch(e)
48 {
49 //verify that e has the right data
50 TEST_ASSERT_EQUAL(0xBE, e);
51 }
52
53 //verify that e STILL has the right data
54 TEST_ASSERT_EQUAL(0xBE, e);
55 }
56
57 void test_BasicThrowAndCatch_WithMiniSyntax(void)
58 {
59 CEXCEPTION_T e;
60
61 //Mini Throw and Catch
62 Try
63 Throw(0xEF);
64 Catch(e)
65 TEST_ASSERT_EQUAL(0xEF, e);
66 TEST_ASSERT_EQUAL(0xEF, e);
67
68 //Mini Passthrough
69 Try
70 e = 0;
71 Catch(e)
72 TEST_FAIL_MESSAGE("I shouldn't be caught because there was no throw");
73
74 TEST_ASSERT_EQUAL(0, e);
75 }
76
77 void test_VerifyVolatilesSurviveThrowAndCatch(void)
78 {
79 volatile unsigned int VolVal = 0;
80 CEXCEPTION_T e;
81
82 Try
83 {
84 VolVal = 2;
85 Throw(0xBF);
86 TEST_FAIL_MESSAGE("Should Have Thrown An Error");
87 }
88 Catch(e)
89 {
90 VolVal += 2;
91 TEST_ASSERT_EQUAL(0xBF, e);
92 }
93
94 TEST_ASSERT_EQUAL(4, VolVal);
95 TEST_ASSERT_EQUAL(0xBF, e);
96 }
97
98 void HappyExceptionThrower(unsigned int ID)
99 {
100 if (ID != 0)
101 {
102 Throw(ID);
103 }
104 }
105
106 void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
107 {
108 volatile unsigned int ID = 0;
109 CEXCEPTION_T e;
110
111 Try
112 {
113
114 HappyExceptionThrower(0xBA);
115 TEST_FAIL_MESSAGE("Should Have Thrown An Exception");
116 }
117 Catch(e)
118 {
119 ID = e;
120 }
121
122 //verify that I can pass that value to something else
123 TEST_ASSERT_EQUAL(0xBA, e);
124 //verify that ID and e have the same value
125 TEST_ASSERT_EQUAL(ID, e);
126 }
127
128 void HappyExceptionRethrower(unsigned int ID)
129 {
130 CEXCEPTION_T e;
131
132 Try
133 {
134 Throw(ID);
135 }
136 Catch(e)
137 {
138 switch (e)
139 {
140 case 0xBD:
141 Throw(0xBF);
142 break;
143 default:
144 break;
145 }
146 }
147 }
148
149 void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void)
150 {
151 volatile unsigned int ID = 0;
152 CEXCEPTION_T e;
153
154 Try
155 {
156 HappyExceptionRethrower(0xBD);
157 TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
158 }
159 Catch(e)
160 {
161 ID = 1;
162 }
163
164 TEST_ASSERT_EQUAL(0xBF, e);
165 TEST_ASSERT_EQUAL(1, ID);
166 }
167
168 void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void)
169 {
170 CEXCEPTION_T e = 3;
171
172 Try
173 {
174 HappyExceptionRethrower(0xBF);
175 }
176 Catch(e)
177 {
178 TEST_FAIL_MESSAGE("Should Not Have Re-thrown Error (it should have already been caught)");
179 }
180
181 //verify that THIS e is still untouched, even though subfunction was touched
182 TEST_ASSERT_EQUAL(3, e);
183 }
184
185 void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId(void)
186 {
187 CEXCEPTION_T e;
188
189 Try
190 {
191 HappyExceptionThrower(0xBF);
192 TEST_FAIL_MESSAGE("Should Have Thrown Exception");
193 }
194 Catch(e)
195 {
196 TEST_ASSERT_EQUAL(0xBF, e);
197 HappyExceptionRethrower(0x12);
198 TEST_ASSERT_EQUAL(0xBF, e);
199 }
200 TEST_ASSERT_EQUAL(0xBF, e);
201 }
202
203 void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent(void)
204 {
205 CEXCEPTION_T e1, e2;
206
207 Try
208 {
209 HappyExceptionThrower(0xBF);
210 TEST_FAIL_MESSAGE("Should Have Thrown Exception");
211 }
212 Catch(e1)
213 {
214 TEST_ASSERT_EQUAL(0xBF, e1);
215 Try
216 {
217 HappyExceptionThrower(0x12);
218 }
219 Catch(e2)
220 {
221 TEST_ASSERT_EQUAL(0x12, e2);
222 }
223 TEST_ASSERT_EQUAL(0x12, e2);
224 TEST_ASSERT_EQUAL(0xBF, e1);
225 }
226 TEST_ASSERT_EQUAL(0x12, e2);
227 TEST_ASSERT_EQUAL(0xBF, e1);
228 }
229
230 void test_CanHaveMultipleTryBlocksInASingleFunction(void)
231 {
232 CEXCEPTION_T e;
233
234 Try
235 {
236 HappyExceptionThrower(0x01);
237 TEST_FAIL_MESSAGE("Should Have Thrown Exception");
238 }
239 Catch(e)
240 {
241 TEST_ASSERT_EQUAL(0x01, e);
242 }
243
244 Try
245 {
246 HappyExceptionThrower(0xF0);
247 TEST_FAIL_MESSAGE("Should Have Thrown Exception");
248 }
249 Catch(e)
250 {
251 TEST_ASSERT_EQUAL(0xF0, e);
252 }
253 }
254
255 void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
256 {
257 int i = 0;
258 CEXCEPTION_T e;
259
260 Try
261 {
262 Try
263 {
264 HappyExceptionThrower(0x01);
265 i = 1;
266 TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
267 }
268 Catch(e)
269 {
270 TEST_ASSERT_EQUAL(0x01, e);
271 }
272 }
273 Catch(e)
274 {
275 TEST_FAIL_MESSAGE("Should Have Been Caught By Inside Catch");
276 }
277
278 // verify that i is still zero
279 TEST_ASSERT_EQUAL(0, i);
280 }
281
282 void test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside(void)
283 {
284 int i = 0;
285 CEXCEPTION_T e;
286
287 Try
288 {
289 Try
290 {
291 i = 2;
292 }
293 Catch(e)
294 {
295 TEST_FAIL_MESSAGE("Should Not Be Caught Here");
296 }
297 HappyExceptionThrower(0x01);
298 TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
299 }
300 Catch(e)
301 {
302 TEST_ASSERT_EQUAL(0x01, e);
303 }
304
305 // verify that i is 2
306 TEST_ASSERT_EQUAL(2, i);
307 }
308
309 void test_AThrowWithoutATryCatchWillUseDefaultHandlerIfSpecified(void)
310 {
311 //Let the fallback handler know we're expecting it to get called this time, so don't fail
312 TestingTheFallback = 1;
313
314 Throw(0xBE);
315
316 //We know the fallback was run because it decrements the counter above
317 TEST_ASSERT_FALSE(TestingTheFallback);
318 TEST_ASSERT_EQUAL(0xBE, TestingTheFallbackId);
319 }
320
321 void test_AThrowWithoutOutsideATryCatchWillUseDefaultHandlerEvenAfterTryCatch(void)
322 {
323 CEXCEPTION_T e;
324
325 Try
326 {
327 //It's not really important that we do anything here.
328 }
329 Catch(e)
330 {
331 //The entire purpose here is just to make sure things get set back to using the default handler when done
332 }
333
334 //Let the fallback handler know we're expecting it to get called this time, so don't fail
335 TestingTheFallback = 1;
336
337 Throw(0xBE);
338
339 //We know the fallback was run because it decrements the counter above
340 TEST_ASSERT_FALSE(TestingTheFallback);
341 TEST_ASSERT_EQUAL(0xBE, TestingTheFallbackId);
342 }
343
344 void test_AbilityToExitTryWithoutThrowingAnError(void)
345 {
346 int i=0;
347 CEXCEPTION_T e;
348
349 Try
350 {
351 ExitTry();
352 i = 1;
353 TEST_FAIL_MESSAGE("Should Have Exited Try Before This");
354 }
355 Catch(e)
356 {
357 i = 2;
358 TEST_FAIL_MESSAGE("Should Not Have Been Caught");
359 }
360
361 // verify that i is still zero
362 TEST_ASSERT_EQUAL(0, i);
363 }
364
365 void test_AbilityToExitTryWillOnlyExitOneLevel(void)
366 {
367 int i=0;
368 CEXCEPTION_T e;
369 CEXCEPTION_T e2;
370
371 Try
372 {
373 Try
374 {
375 ExitTry();
376 TEST_FAIL_MESSAGE("Should Have Exited Try Before This");
377 }
378 Catch(e)
379 {
380 TEST_FAIL_MESSAGE("Should Not Have Been Caught By Inside");
381 }
382 i = 1;
383 }
384 Catch(e2)
385 {
386 TEST_FAIL_MESSAGE("Should Not Have Been Caught By Outside");
387 }
388
389 // verify that we picked up and ran after first Try
390 TEST_ASSERT_EQUAL(1, i);
391 }