-
Notifications
You must be signed in to change notification settings - Fork 1
/
FabOS_test.c
582 lines (485 loc) · 12.1 KB
/
FabOS_test.c
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
FabOS for ATMEL AVR
Test suite
tested on WinAVR 20090913 and Mega32 / ATXMEGA32A4
(c) 2008-2010 Fabian Huslik
This is free software according GNU GENERAL PUBLIC LICENSE Version 3.
This file implememts automated software testing of the OS modules.
This file should only be altered if really necessary.
It does not contain any code, which is useful for production use of FabOS.
remember:
avr-gcc -mmcu=atmega32 -E FabOS_test.c >bla.c invoke preprocessor only
*/
#include "OS/FabOS.h"
#if OS_DO_TESTSUITE == 1 // compile only if tests are activated (OS development use only)
OS_DeclareTask(TestTask0,200);
OS_DeclareTask(TestTask1,200);
OS_DeclareTask(TestTask2,200);
OS_DeclareTask(TestTaskhi,100);
OS_DeclareTask(TestTasklo,100);
volatile uint16_t r,s,t,u;
volatile uint8_t testvar=0;
OS_DeclareQueue(TestQ,64,1);
OS_DeclareQueue(TestQLong,10,4);
//OS_Queue_t TestQ ={0,0,{}};
uint32_t longQtest[10] = {0x11223344, 0x22334455, 0x33445566, 0x44556677, 0x55667788, 0x66778899, 0x778899aa, 0x8899aabb, 0x99aabbcc, 0xaabbccdd};
#define MAXTESTCASES 12 // one more than the max "case" ID.
uint8_t testcase = 0; // test case number
uint8_t TestResults[MAXTESTCASES]; // test result array (0= OK)
uint8_t TestProcessed[MAXTESTCASES]; // test passed array (number of processed assertions
#define assert(X) do{ \
OS_DISABLEALLINTERRUPTS; \
if(!(X)) \
{ \
TestResults[testcase]++;\
} \
TestProcessed[testcase]++; \
OS_ENABLEALLINTERRUPTS; \
}while(0)
void WasteOfTime(uint32_t waittime);
#define OSALMTest0 0
#define OSALMTest1 1
#define OSALMTest2 2
#define OSALMTest3 3
#define OSALMTesthi 4
#define OSALMTestlo 5
#define OSTestTskID0 1
#define OSTestTskID1 2
#define OSTestTskID2 3
#define OSTestTskIDlo 4
#define OSTestTskIDhi 0
void OS_TestSuite(void)
{
uint8_t test0pass=0;
OS_CreateTask(TestTaskhi, OSTestTskIDhi);
OS_CreateTask(TestTask0, OSTestTskID0);
OS_CreateTask(TestTask1, OSTestTskID1);
OS_CreateTask(TestTask2, OSTestTskID2);
OS_CreateTask(TestTasklo, OSTestTskIDlo);
OS_CreateAlarm(OSALMTest0,OSTestTskID0);
OS_CreateAlarm(OSALMTest1,OSTestTskID1);
OS_CreateAlarm(OSALMTest2,OSTestTskID2);
OS_CreateAlarm(OSALMTest3,OSTestTskID0); // testcase 10
OS_CreateAlarm(OSALMTestlo,OSTestTskIDlo);
OS_CreateAlarm(OSALMTesthi,OSTestTskIDhi);
OS_StartExecution() ;
while(1)
{
// THIS IS the idle task which will be preemted by all other tasks.
// NO OS-wait-API allowed here!!!
#if OS_USEMEMCHECKS ==1
r = OS_GetUnusedStack(0);
s = OS_GetUnusedStack(1);
t = OS_GetUnusedStack(2);
u = OS_GetUnusedStack(3); // idle
#endif
switch(testcase)
{
case 0:
// Task Priority
if(!test0pass)
{
assert(testvar ==3);
test0pass = 1;
}
break;
default:
break;
}
}
}
volatile uint32_t testtime=0;
void TestTask1(void)
{
uint8_t ret,i;
uint32_t ts,te;
uint8_t v;
uint16_t t=25;
uint32_t ti,tj;
while(1)
{
switch(testcase)
{
case 0:
// Task Priority
assert(testvar ==1);
testvar++;
break;
case 1:
OS_WaitTicks(OSALMTest1, 1);
// Timer / Wait
OS_GetTicks(&ti); // get time
OS_WaitTicks(OSALMTest1, t);
OS_GetTicks(&tj); // get time
assert(tj-ti == t); // waited time was correct
break;
case 2:
// WaitEvent
// SetEvent
// wait for A, then A occurs
testvar=1;
OS_WaitEvent(1<<0);
testvar=2;
// A occurs, then wait for A
WasteOfTime(50);
assert(testvar==3);
OS_WaitEvent(1<<0);
testvar=4;
// wait for more events, one occurs, then the other
ret = OS_WaitEvent(1<<1 | 1<<2);
testvar=5;
assert(ret == (1<<2));
testvar=6;
ret = OS_WaitEvent(1<<1 | 1<<2);
testvar=7;
assert(ret == (1<<1));
testvar=8;
// wait for less events
break;
case 3:
// MutexGet
// MutexRelease
// two cases:
// 1:mutex is free
OS_MutexGet(0);
WasteOfTime(50);
testvar = 1;
OS_WaitTicks(OSALMTest1, 50);
assert(testvar == 1);
OS_MutexRelease(0);
OS_WaitEvent(1<<1);
// 2:mutex is occupied by lower prio
OS_WaitTicks(OSALMTest1, 50);
OS_MutexGet(0);
testvar = 3;
WasteOfTime(50);
assert(testvar == 3);
OS_MutexRelease(0);
break;
case 4:
// QueueIn
// QueueOut
TestQ.read = 13; // tweak the queue to a "used" state
TestQ.write =13;
ret = OS_QueueOut(&TestQ,&v); // q empty at start
assert(ret == 1);
OS_WaitEvent(1<<6);
for (i=0;i<20;i++) // forward
{
ret = OS_QueueOut(&TestQ,&v);
assert(v==i);
assert(ret ==0);
}
// Q empty
ret = OS_QueueOut(&TestQ,&v);
//assert(v==i);
assert(ret ==1);
OS_WaitEvent(1<<6);
for (i=20;i>0;i--) // backward
{
ret = OS_QueueOut(&TestQ,&v);
assert(v==i);
assert(ret ==0);
}
// Q empty
ret = OS_QueueOut(&TestQ,&v);
assert(ret ==1);
//OS_WaitEvent(1<<6);
break;
case 5:
// SetAlarm
// WaitAlarm
OS_GetTicks(&ts);
OS_WaitAlarm(OSALMTest1);
OS_GetTicks(&te);
assert(te-ts == 53);
break;
case 6:
// Event with timeout
ret = OS_WaitEventTimeout(1<<3,OSALMTest1, 30);
assert(ret == 1<<3); // no timeout, it was the event.
OS_GetTicks(&ti); // get time
ret = OS_WaitEventTimeout(1<<3,OSALMTest1, t); // wait for the event, which never comes...
assert(ret == 0); // timeout recoginzed
OS_GetTicks(&tj); // get time
assert(tj-ti == t); // waited time was correct
break;
case 7:
// long queue
break;
default:
break;
}
OS_WaitEvent(1<<7);
}// while(1)
}
void TestTask2(void)
{
uint32_t ts,te;
uint32_t i,j;
uint16_t t;
while(1)
{
switch(testcase)
{
case 0:
// Task Priority
assert(testvar ==2);
testvar++;
break;
case 1:
// Timer / Wait
OS_WaitTicks(OSALMTest2, 2);
t=50;
OS_GetTicks(&i); // get time
OS_WaitTicks(OSALMTest2, t);
OS_GetTicks(&j); // get time
assert(j-i == t); // waited time was correct
break;
case 2:
// WaitEvent
// SetEvent
break;
case 3:
// MutexGet
// MutexRelease
// two cases:
// 1:mutex is occupied by higher prio
WasteOfTime(10);
OS_MutexGet(0);
testvar = 7;
WasteOfTime(50);
assert(testvar == 7);
OS_MutexRelease(0);
OS_SetEvent(OSTestTskID1,1<<1);
// 2:mutex is occupied
OS_MutexGet(0);
testvar = 4;
WasteOfTime(50);
assert(testvar == 4);
OS_MutexRelease(0);
break;
case 4:
// QueueIn
// QueueOut
break;
case 5:
// SetAlarm
// WaitAlarm
OS_GetTicks(&ts);
OS_WaitAlarm(OSALMTest2);
OS_GetTicks(&te);
assert(te-ts == 44);
break;
case 6:
// Event with timeout
break;
case 7:
// long queue
break;
default:
break;
}
OS_WaitEvent(1<<7);
}// while(1)
}
// This is the coordinator task, the main beat for the tests comes from here.
void TestTask0(void)
{
uint8_t ret,i;
uint32_t ti,tj;
uint16_t t;
while(1)
{
switch(testcase)
{
case 0:
// Task Priority
assert(testvar ==0);
testvar++;
break;
case 1:
// Timer / Wait
OS_WaitTicks(OSALMTest0, 3);
t=10;
OS_GetTicks(&ti); // get time
OS_WaitTicks(OSALMTest0, t);
OS_GetTicks(&tj); // get time
assert(tj-ti == t); // waited time was correct
break;
case 2:
// WaitEvent
// SetEvent
// wait for A, then A occurs
testvar =0;
OS_WaitTicks(OSALMTest0, 10);
assert(testvar==1);
OS_SetEvent(OSTestTskID1,1<<0);
OS_WaitTicks(OSALMTest0, 2);
assert(testvar==2);
// A occurs, then wait for A
testvar =3;
OS_SetEvent(OSTestTskID1,1<<0);
assert(testvar==3);
OS_WaitTicks(OSALMTest0, 100); // other task wastes time...
assert(testvar==4);
// wait for more events, one occurs, then the other
OS_SetEvent(OSTestTskID1,1<<2);
assert(testvar==4);
OS_WaitTicks(OSALMTest0, 10);
assert(testvar==6);
OS_SetEvent(OSTestTskID1,1<<1);
assert(testvar==6);
OS_WaitTicks(OSALMTest0, 10);
assert(testvar==8);
break;
case 3:
// MutexGet
// MutexRelease
break;
case 4:
// QueueIn
// QueueOut
OS_WaitTicks(OSALMTest0, 50);
for (i=0;i<20;i++) // forward
{
ret = OS_QueueIn(&TestQ,&i);
assert(ret==0);
}
OS_SetEvent(OSTestTskID1,1<<6);
OS_WaitTicks(OSALMTest0, 50);
for (i=20;i>0;i--) // backward
{
ret = OS_QueueIn(&TestQ,&i);
assert(ret==0);
}
OS_SetEvent(OSTestTskID1,1<<6);
OS_WaitTicks(OSALMTest0, 50);
// check, if q is empty
t = OS_GetQueueSpace(&TestQ);
assert(t==64);
for (i=0;i<64;i++) // overload the Q
{
ret = OS_QueueIn(&TestQ,&i);
assert(ret==0);
}
// check, if none left
t = OS_GetQueueSpace(&TestQ);
assert(t==0);
// now one too much:
ret = OS_QueueIn(&TestQ,&i);
assert(ret==1);
break;
case 5:
// SetAlarm
// WaitAlarm
OS_SetAlarm(OSALMTest1,53);
OS_SetAlarm(OSALMTest2,44);
break;
case 6:
// Event with timeout
OS_SetEvent(OSTestTskID1,1<<3);
OS_WaitTicks(OSALMTest0, 50);
// do not set the second one...
break;
case 7:
//queue long test
ret = OS_QueueOut(&TestQLong,(uint8_t*)&ti); // Q empty
assert(ret == 1);
for (i=0;i<10;i++)
{
ret = OS_QueueIn(&TestQLong,(uint8_t*)&longQtest[i]);
assert (ret==0);
}
ret = OS_QueueIn(&TestQLong,(uint8_t*)&longQtest[9]);
assert (ret==1); // Q full
for (i=0;i<10;i++)
{
ret = OS_QueueOut(&TestQLong,(uint8_t*)&ti);
assert(ret==0);
assert(ti == longQtest[i]);
}
ret = OS_QueueOut(&TestQLong,(uint8_t*)&ti);
assert(ret==1); // q empty
break;
case 8:
// Nested Mutexes
OS_MutexGet(0);
WasteOfTime(1);
OS_MutexGet(1);
WasteOfTime(1);
OS_MutexRelease(0);
WasteOfTime(1);
OS_MutexRelease(1);
assert(1==1); // if it doesnt work, we do not come to here,
break;
case 9:
// test of OS_GetUnusedStack (uint8_t TaskID)
t=OS_GetUnusedStack(OSTestTskID0); // task0
assert(t>80 && t<150);
t=OS_GetUnusedStack(OSTestTskID1); // task1
assert(t>80 && t<150);
t=OS_GetUnusedStack(OSTestTskID2); // task2
assert(t>80 && t<150);
t=OS_GetUnusedStack(5); // idle
assert(t>2000 && t<2600);
break;
case 10:
OS_GetTicks(&ti);
OS_SetAlarm(OSALMTest0,39);
OS_SetAlarm(OSALMTest3,64);
OS_WaitAlarm(OSALMTest0);
OS_GetTicks(&tj);
assert(tj-ti == 39);
OS_WaitAlarm(OSALMTest3);
OS_GetTicks(&tj);
assert(tj-ti == 64);
break;
case 11:
assert(0); // just for proofing that it works!
break;
default:
// The END
while(1)
{
// sit here and wait for someone picking up the results out of "TestResults". 0 = OK.
// "TestProcessed" shows the number of processed assertions.
asm("break");
}
break;
}
OS_WaitTicks(OSALMTest0, 500); // wait for tests to be processed...
testcase++;
OS_SetEvent(OSTestTskID1,1<<7); // tick other tasks in sync.
OS_SetEvent(OSTestTskID2,1<<7);
}//while(1)
}
void WasteOfTime(uint32_t waittime)
{
uint32_t oldTime,newTime;
OS_GetTicks(&oldTime); // get time
do
{
OS_GetTicks(&newTime); // get time
}
while (newTime - oldTime < waittime);
}
void TestTaskhi(void)
{
while(1)
{
OS_SetAlarm(OSALMTesthi,3);
OS_WaitAlarm(OSALMTesthi);
}
}
extern void clutterRegs(void);
void TestTasklo(void)
{
while(1)
{
OS_SetAlarm(OSALMTestlo,3);
OS_WaitAlarm(OSALMTestlo);
clutterRegs();
}
}
#endif // OS_DO_TESTSUITE == 1