summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Test/CMock/vendor/c_exception/test/TestException.c
blob: e248cb0f28ad0e3b87cd899a69b330dbcfeb9e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "unity.h"
#include "CException.h"

volatile int TestingTheFallback;
volatile int TestingTheFallbackId;

void setUp(void)
{
    CExceptionFrames[0].pFrame = NULL;
    TestingTheFallback = 0;
}

void tearDown(void)
{
}

void test_BasicTryDoesNothingIfNoThrow(void)
{
  int i = 0;
  CEXCEPTION_T e = 0x5a;

  Try
  {
    i += 1;
  }
  Catch(e)
  {
    TEST_FAIL_MESSAGE("Should Not Enter Catch If Not Thrown");
  }

  //verify that e was untouched
  TEST_ASSERT_EQUAL(0x5a, e);

  // verify that i was incremented once
  TEST_ASSERT_EQUAL(1, i);
}

void test_BasicThrowAndCatch(void)
{
  CEXCEPTION_T e;

  Try
  {
    Throw(0xBE);
    TEST_FAIL_MESSAGE("Should Have Thrown An Error");
  }
  Catch(e)
  {
    //verify that e has the right data
    TEST_ASSERT_EQUAL(0xBE, e);
  }

  //verify that e STILL has the right data
  TEST_ASSERT_EQUAL(0xBE, e);
}

void test_BasicThrowAndCatch_WithMiniSyntax(void)
{
  CEXCEPTION_T e;

  //Mini Throw and Catch
  Try
    Throw(0xEF);
  Catch(e)
    TEST_ASSERT_EQUAL(0xEF, e);
  TEST_ASSERT_EQUAL(0xEF, e);

  //Mini Passthrough
  Try
    e = 0;
  Catch(e)
    TEST_FAIL_MESSAGE("I shouldn't be caught because there was no throw");

  TEST_ASSERT_EQUAL(0, e);
}

void test_VerifyVolatilesSurviveThrowAndCatch(void)
{
  volatile unsigned int VolVal = 0;
  CEXCEPTION_T e;

  Try
  {
    VolVal = 2;
    Throw(0xBF);
    TEST_FAIL_MESSAGE("Should Have Thrown An Error");
  }
  Catch(e)
  {
    VolVal += 2;
    TEST_ASSERT_EQUAL(0xBF, e);
  }

  TEST_ASSERT_EQUAL(4, VolVal);
  TEST_ASSERT_EQUAL(0xBF, e);
}

void HappyExceptionThrower(unsigned int ID)
{
  if (ID != 0)
  {
    Throw(ID);
  }
}

void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
{
  volatile  unsigned int ID = 0;
  CEXCEPTION_T e;

  Try
  {

    HappyExceptionThrower(0xBA);
    TEST_FAIL_MESSAGE("Should Have Thrown An Exception");
  }
  Catch(e)
  {
    ID = e;
  }

  //verify that I can pass that value to something else
  TEST_ASSERT_EQUAL(0xBA, e);
  //verify that ID and e have the same value
  TEST_ASSERT_EQUAL(ID, e);
}

void HappyExceptionRethrower(unsigned int ID)
{
  CEXCEPTION_T e;

  Try
  {
    Throw(ID);
  }
  Catch(e)
  {
    switch (e)
    {
    case 0xBD:
      Throw(0xBF);
      break;
    default:
      break;
    }
  }
}

void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void)
{
  volatile  unsigned int ID = 0;
  CEXCEPTION_T e;

  Try
  {
    HappyExceptionRethrower(0xBD);
    TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
  }
  Catch(e)
  {
    ID = 1;
  }

  TEST_ASSERT_EQUAL(0xBF, e);
  TEST_ASSERT_EQUAL(1, ID);
}

void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void)
{
  CEXCEPTION_T e = 3;

  Try
  {
    HappyExceptionRethrower(0xBF);
  }
  Catch(e)
  {
    TEST_FAIL_MESSAGE("Should Not Have Re-thrown Error (it should have already been caught)");
  }

  //verify that THIS e is still untouched, even though subfunction was touched
  TEST_ASSERT_EQUAL(3, e);
}

void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId(void)
{
  CEXCEPTION_T e;

  Try
  {
    HappyExceptionThrower(0xBF);
    TEST_FAIL_MESSAGE("Should Have Thrown Exception");
  }
  Catch(e)
  {
    TEST_ASSERT_EQUAL(0xBF, e);
    HappyExceptionRethrower(0x12);
    TEST_ASSERT_EQUAL(0xBF, e);
  }
  TEST_ASSERT_EQUAL(0xBF, e);
}

void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent(void)
{
  CEXCEPTION_T e1, e2;

  Try
  {
    HappyExceptionThrower(0xBF);
    TEST_FAIL_MESSAGE("Should Have Thrown Exception");
  }
  Catch(e1)
  {
    TEST_ASSERT_EQUAL(0xBF, e1);
    Try
    {
      HappyExceptionThrower(0x12);
    }
    Catch(e2)
    {
      TEST_ASSERT_EQUAL(0x12, e2);
    }
    TEST_ASSERT_EQUAL(0x12, e2);
    TEST_ASSERT_EQUAL(0xBF, e1);
  }
  TEST_ASSERT_EQUAL(0x12, e2);
  TEST_ASSERT_EQUAL(0xBF, e1);
}

void test_CanHaveMultipleTryBlocksInASingleFunction(void)
{
  CEXCEPTION_T e;

  Try
  {
    HappyExceptionThrower(0x01);
    TEST_FAIL_MESSAGE("Should Have Thrown Exception");
  }
  Catch(e)
  {
    TEST_ASSERT_EQUAL(0x01, e);
  }

  Try
  {
    HappyExceptionThrower(0xF0);
    TEST_FAIL_MESSAGE("Should Have Thrown Exception");
  }
  Catch(e)
  {
    TEST_ASSERT_EQUAL(0xF0, e);
  }
}

void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
{
  int i = 0;
  CEXCEPTION_T e;

  Try
  {
    Try
    {
      HappyExceptionThrower(0x01);
      i = 1;
      TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
    }
    Catch(e)
    {
      TEST_ASSERT_EQUAL(0x01, e);
    }
  }
  Catch(e)
  {
    TEST_FAIL_MESSAGE("Should Have Been Caught By Inside Catch");
  }

  // verify that i is still zero
  TEST_ASSERT_EQUAL(0, i);
}

void test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside(void)
{
  int i = 0;
  CEXCEPTION_T e;

  Try
  {
    Try
    {
      i = 2;
    }
    Catch(e)
    {
      TEST_FAIL_MESSAGE("Should Not Be Caught Here");
    }
    HappyExceptionThrower(0x01);
    TEST_FAIL_MESSAGE("Should Have Rethrown Exception");
  }
  Catch(e)
  {
    TEST_ASSERT_EQUAL(0x01, e);
  }

  // verify that i is 2
  TEST_ASSERT_EQUAL(2, i);
}

void test_AThrowWithoutATryCatchWillUseDefaultHandlerIfSpecified(void)
{
    //Let the fallback handler know we're expecting it to get called this time, so don't fail
    TestingTheFallback = 1;

    Throw(0xBE);

    //We know the fallback was run because it decrements the counter above
    TEST_ASSERT_FALSE(TestingTheFallback);
    TEST_ASSERT_EQUAL(0xBE, TestingTheFallbackId);
}

void test_AThrowWithoutOutsideATryCatchWillUseDefaultHandlerEvenAfterTryCatch(void)
{
    CEXCEPTION_T e;

    Try
    {
        //It's not really important that we do anything here.
    }
    Catch(e)
    {
        //The entire purpose here is just to make sure things get set back to using the default handler when done
    }

    //Let the fallback handler know we're expecting it to get called this time, so don't fail
    TestingTheFallback = 1;

    Throw(0xBE);

    //We know the fallback was run because it decrements the counter above
    TEST_ASSERT_FALSE(TestingTheFallback);
    TEST_ASSERT_EQUAL(0xBE, TestingTheFallbackId);
}

void test_AbilityToExitTryWithoutThrowingAnError(void)
{
    int i=0;
    CEXCEPTION_T e;

    Try
    {
        ExitTry();
        i = 1;
        TEST_FAIL_MESSAGE("Should Have Exited Try Before This");
    }
    Catch(e)
    {
        i = 2;
        TEST_FAIL_MESSAGE("Should Not Have Been Caught");
    }

    // verify that i is still zero
    TEST_ASSERT_EQUAL(0, i);
}

void test_AbilityToExitTryWillOnlyExitOneLevel(void)
{
    int i=0;
    CEXCEPTION_T e;
    CEXCEPTION_T e2;

    Try
    {
        Try
        {
            ExitTry();
            TEST_FAIL_MESSAGE("Should Have Exited Try Before This");
        }
        Catch(e)
        {
            TEST_FAIL_MESSAGE("Should Not Have Been Caught By Inside");
        }
        i = 1;
    }
    Catch(e2)
    {
        TEST_FAIL_MESSAGE("Should Not Have Been Caught By Outside");
    }

    // verify that we picked up and ran after first Try
    TEST_ASSERT_EQUAL(1, i);
}