libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
gcc.test.h
Go to the documentation of this file.
1 // Copyright 2013 SRLM and Red9
2 #include <propeller.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 
7 #include "unity.h"
8 
9 class UnityTests{
10 public:
11  static void setUp(void){
12 
13  }
14 
15  static void tearDown(void){
16  for(int i = 1; i < 8; ++i){
17  cogstop(i);
18  }
19  }
20 
21  // -----------------------------------------------------------------------------
22  // Test Shift operations. Right shift is compiler dependent: it can be either
23  // arithemetic right shift or logical right shift. It usually (and the tests
24  // confirm this) depends on the variable:
25  // unsigned -> use logical right shift
26  // signed -> use arithmetic right shift
27  // -----------------------------------------------------------------------------
28 
29  static void test_ShiftLeft(void){
30  volatile int x = 1;
31  x = x << 2;
32  TEST_ASSERT_EQUAL_INT(4, x);
33  }
34 
35  static void test_ShiftRightNegative(void){
36  volatile int x = -8;
37  x = x >> 1;
38  TEST_ASSERT_EQUAL_INT(-4, x);
39  }
40 
41  static void test_ShiftRightUnsignedNumber(void){
42  volatile unsigned int x = 0xFFFFFFFF;
43  x = x >> 4;
44  TEST_ASSERT_EQUAL_HEX32(0x0FFFFFFF, x);
45  }
46 
47 
48  static void test_ShiftRightSignedPositiveHighBitSet(void){
49  volatile signed int x = 0xFfffFfff;
50  x = x >> 16;
51  TEST_ASSERT_EQUAL_HEX32(0xFfffFfff, x);
52  }
53 
54  static void test_ShiftRightSignedPositiveHighBitNotSet(void){
55  volatile signed int x = 0xFfff;
56  x = x >> 8;
57  TEST_ASSERT_EQUAL_HEX32(0xFf, x);
58  }
59 
60  static void test_LeftShiftUnsigned(void){
61  volatile unsigned int x = 0xF;
62  x = x << 16;
63  TEST_ASSERT_EQUAL_HEX32(0xF0000, x);
64  }
65 
66  // -----------------------------------------------------------------------------
67  // Propeller.h lock tests
68  // Suggested rules for locks: Propeller Manual v1.2 pg 123
69  // -----------------------------------------------------------------------------
70 
71  // @WARNING for all locks: Must lockclr the lock after setting it!
72 
73 
74 
75  static void test_lockretReturnsLock(void){
76  int num_first = locknew();
77  lockret(num_first);
78  int num_second = locknew();
79  TEST_ASSERT_EQUAL_INT(num_first, num_second);
80  lockret(num_second);
81  }
82 
83  static void test_locknewFirstLockIs1(void){
84  int num = locknew();
85 
86  TEST_ASSERT_EQUAL_INT(1, num);
87 
88  lockret(num);
89  }
90 
91  static void test_locknewSevenAvailableLocks(void){
92  for(int i = 1; i < 8; i++){
93  TEST_ASSERT_EQUAL_INT(i, locknew());
94  }
95 
96  TEST_ASSERT_EQUAL_INT(-1, locknew());
97 
98  for(int i = 1; i < 8; i++){
99  lockret(i);
100  }
101  }
102 
103  static void test_locksetAndlockclr(void){
104  int lock = locknew();
105  TEST_ASSERT_TRUE(lock != -1);
106 
107  TEST_ASSERT_FALSE(lockset(lock));
108  TEST_ASSERT_TRUE(lockset(lock));
109 
110  lockclr(lock);
111  lockret(lock);
112  }
113 
114  static void test_ReturnSetLock(void){
115  int lock = locknew();
116  TEST_ASSERT_TRUE(lock != -1);
117 
118  lockset(lock);
119  lockclr(lock);
120  lockret(lock);
121  int lock2 = locknew();
122  TEST_ASSERT_EQUAL_INT(lock, lock2);
123 
124  lockret(lock2);
125 
126  }
127 
128  static void test_locksetIsEqualToCTrueFalseConstant(void){
129  int lock = locknew();
130  TEST_ASSERT_TRUE(lock != -1);
131 
132  //lockclr(lock);
133 
134  TEST_ASSERT_TRUE(lockset(lock) == false);
135  TEST_ASSERT_TRUE(lockset(lock) != false);
136 
137  lockclr(lock);
138  lockret(lock);
139 
140  }
141 
142  static void test_lockretReturnOrderDoesntMatter(void){
143  int lockA = locknew();
144  int lockB = locknew();
145 
146  // Sanity Check
147  TEST_ASSERT_EQUAL_INT(lockB-1, lockA);
148 
149  lockret(lockA);
150  int lockC = locknew();
151 
152  TEST_ASSERT_EQUAL_INT(lockB-1, lockC);
153 
154  lockret(lockA);
155  lockret(lockC);
156  }
157 
158  // -----------------------------------------------------------------------------
159  // Type sizes
160  // -----------------------------------------------------------------------------
161  static void test_SizeofInt(void){
162  TEST_ASSERT_EQUAL_INT(4, sizeof(int));
163  }
164 
165  static void test_SizeofUnsignedInt(void){
166  TEST_ASSERT_EQUAL_INT(4, sizeof(unsigned int));
167  }
168 
169  static void test_SizeofChar(void){
170  TEST_ASSERT_EQUAL_INT(1, sizeof(char));
171  }
172 
173  static void test_SizeofUnsignedChar(void){
174  TEST_ASSERT_EQUAL_INT(1, sizeof(unsigned char));
175  }
176 
177  static void test_SizeofBool(void){
178  TEST_ASSERT_EQUAL_INT(1, sizeof(bool));
179  }
180 
181  static void test_SizeofShort(void){
182  TEST_ASSERT_EQUAL_INT(2, sizeof(short));
183  }
184 
185  static void test_SizeofUnsignedShort(void){
186  TEST_ASSERT_EQUAL_INT(2, sizeof(unsigned short));
187  }
188 
189  static void test_SizeofIntPointer(void){
190  TEST_ASSERT_EQUAL_INT(4, sizeof(int *));
191  }
192 
193  static void test_SizeofShortPointer(void){
194  TEST_ASSERT_EQUAL_INT(4, sizeof(short *));
195  }
196 
197  static void test_SizeofCharPointer(void){
198  TEST_ASSERT_EQUAL_INT(4, sizeof(char *));
199  }
200 
201  // -----------------------------------------------------------------------------
202 
203  static void test_WhatIsTrue(void){
204  TEST_ASSERT_EQUAL_INT(1, true);
205  }
206 
207  static void test_WhatIsFalse(void){
208  TEST_ASSERT_EQUAL_INT(0, false);
209  }
210 
211  static void test_OnlyTrueEqualsTrue(void){
212  TEST_ASSERT_TRUE(2 != true);
213  }
214 
215  static void test_AnyNonZeroNumberIsTrue(void){
216  TEST_ASSERT_TRUE(1);
217  TEST_ASSERT_TRUE(2);
218  TEST_ASSERT_TRUE(200);
219  TEST_ASSERT_TRUE(-1);
220  TEST_ASSERT_TRUE(-200);
221  }
222 
223  static void test_ZeroIsFalse(void){
224  TEST_ASSERT_FALSE(0);
225  }
226 
227  static void test_BooleanAndIsSameAsBitwiseAnd(void){
228  TEST_ASSERT_TRUE((true && false) == (true & false));
229  TEST_ASSERT_TRUE((false && true ) == (false & true ));
230  TEST_ASSERT_TRUE((false && false) == (false & false));
231  TEST_ASSERT_TRUE((true && true ) == (true & true ));
232  }
233 
234 
235  // -----------------------------------------------------------------------------
236 
237  static void test_WritingAnIntToACharWillTruncate(void){
238  char data []= {0,0,0,0,0,0,0,0};
239  data[4] = 0xFFFFFFFF;
240  TEST_ASSERT_EQUAL_HEX8(0, data[0]);
241  TEST_ASSERT_EQUAL_HEX8(0, data[1]);
242  TEST_ASSERT_EQUAL_HEX8(0, data[2]);
243  TEST_ASSERT_EQUAL_HEX8(0, data[3]);
244  TEST_ASSERT_EQUAL_HEX8(0xFF, data[4]);
245  TEST_ASSERT_EQUAL_HEX8(0, data[5]);
246  TEST_ASSERT_EQUAL_HEX8(0, data[6]);
247  TEST_ASSERT_EQUAL_HEX8(0, data[7]);
248  }
249 
250  static void test_InitializingACharWith16BitsWillTruncate(void){
251  char data = 0xABCD;
252  TEST_ASSERT_EQUAL_INT(0xCD, data);
253  }
254 
255  // -----------------------------------------------------------------------------
256 
257 
258  static void FunctionThatEnds(void){
259  waitcnt(CLKFREQ/10 + CNT);
260  }
261 
262  static void FunctionThatEndsWithCogstop(void){
263  waitcnt(CLKFREQ/10 + CNT);
264  cogstop(cogid());
265  }
266 
267  static void test_WhatHappensWhenACogReachesTheEnd(void){
268  int stacksize = sizeof(_thread_state_t)+sizeof(int)*3 + sizeof(int)*100;
269 
270  int * stackA = (int*) malloc(stacksize);
271  int cogA = cogstart(FunctionThatEnds, NULL, stackA, stacksize);
272 
273  int * stackB = (int*) malloc(stacksize);
274  int cogB = cogstart(FunctionThatEnds, NULL, stackB, stacksize);
275 
276  waitcnt(CLKFREQ/10 + CNT);
277 
278  int * stackC = (int*) malloc(stacksize);
279  int cogC = cogstart(FunctionThatEnds, NULL, stackC, stacksize);
280 
281  TEST_ASSERT_EQUAL_INT(1, cogA);
282  TEST_ASSERT_EQUAL_INT(2, cogB);
283  TEST_ASSERT_EQUAL_INT(3, cogC);
284 
285  //cleanUp
286  for(int i = 1; i < 8; ++i){
287  cogstop(i);
288  }
289 
290  free(stackA);
291  free(stackB);
292  free(stackC);
293  }
294 
295  static void test_WhatHappensWhenACogReachesTheEndWithCogstop(void){
296  int stacksize = sizeof(_thread_state_t)+sizeof(int)*3 + sizeof(int)*100;
297 
298  int * stackA = (int*) malloc(stacksize);
299  int cogA = cogstart(FunctionThatEndsWithCogstop, NULL, stackA, stacksize);
300 
301  int * stackB = (int*) malloc(stacksize);
302  int cogB = cogstart(FunctionThatEnds, NULL, stackB, stacksize);
303 
304  waitcnt(CLKFREQ/5 + CNT);
305 
306  int * stackC = (int*) malloc(stacksize);
307  int cogC = cogstart(FunctionThatEndsWithCogstop, NULL, stackC, stacksize);
308 
309  TEST_ASSERT_EQUAL_INT(1, cogA);
310  TEST_ASSERT_EQUAL_INT(2, cogB);
311  TEST_ASSERT_EQUAL_INT(1, cogC);
312 
313  //cleanUp
314  for(int i = 1; i < 8; ++i){
315  cogstop(i);
316  }
317  free(stackA);
318  free(stackB);
319  free(stackC);
320  }
321 
322  // -----------------------------------------------------------------------------
323 
324  static void test_64bitIntegerBasic(void){
325  volatile int64_t a = 0x1;
326 
327  a = a << 32;
328  a = a >> 32;
329  TEST_ASSERT_EQUAL_INT(0x1, a);
330  }
331 
332  static void test_64bitIntegerAdd(void){
333  volatile int64_t a = 0x100000000;
334  a = a + a; //== 0x200000000;
335 
336  #ifndef UNITY_SUPPORT_64
337  a = a >> 32; //== 0x2;
338  TEST_ASSERT_EQUAL_INT(0x2, a);
339  #else
340  TEST_ASSERT_EQUAL_HEX64(0x200000000, a);
341  #endif
342  }
343 
344  static void test_64bitIntegerSubtract(void){
345  volatile int64_t a = 0x500000000;
346  a = a - 0x100000000;
347 
348  #ifndef UNITY_SUPPORT_64
349  a = a >> 32;
350  TEST_ASSERT_EQUAL_INT(0x4, a);
351  #else
352  TEST_ASSERT_EQUAL_HEX64(0x400000000, a);
353  #endif
354  }
355 
356  static void test_64bitIntegerMultiply(void){
357  volatile int64_t a = 0x3;
358  volatile int64_t b = 0x300000000;
359  a = a * b;
360 
361  #ifndef UNITY_SUPPORT_64
362  a = a >> 32;
363  TEST_ASSERT_EQUAL_INT(0x9, a);
364  #else
365  TEST_ASSERT_EQUAL_HEX64(0x900000000, a);
366  #endif
367  }
368 
369  static void test_64bitIntegerDivide(void){
370  volatile int64_t a = 0x3;
371  volatile int64_t b = 0x900000000;
372  a = b / a;
373 
374  #ifndef UNITY_SUPPORT_64
375  a = a >> 32;
376  TEST_ASSERT_EQUAL_INT(0x3, a);
377  #else
378  TEST_ASSERT_EQUAL_HEX64(0x300000000, a);
379  #endif
380  }
381 
382  static void test_64bitIntegerAddSpeed(void){
383 
384  //Nothing
385  int startCNT = CNT;
386  int endCNT = CNT;
387  int nothingDelta = endCNT - startCNT;
388 
389  //64 bit
390  volatile int64_t a64 = 0x500000000;
391  startCNT = CNT;
392  a64 = a64 + a64;
393  endCNT = CNT;
394  UnityPrint("64bit add deltaCNT == ");
395  UnityPrintNumber(endCNT - startCNT - nothingDelta);
396  UNITY_OUTPUT_CHAR('\n');
397 
398  //32 bit
399  volatile int32_t a32 = 0x50000;
400  startCNT = CNT;
401  a32 = a32 + a32;
402  endCNT = CNT;
403  UnityPrint("32bit add deltaCNT == ");
404  UnityPrintNumber(endCNT - startCNT - nothingDelta);
405  UNITY_OUTPUT_CHAR('\n');
406 
407 
408  TEST_ASSERT_EQUAL_INT(0xA0000, a32);
409  #ifndef UNITY_SUPPORT_64
410  a64 = a64 >> 32;
411  TEST_ASSERT_EQUAL_INT(0xA, a64);
412  #else
413  TEST_ASSERT_EQUAL_HEX64(0xA00000000, a64);
414  #endif
415 
416 
417  }
418 
419 
420  static void test_64bitIntegerDivideSpeed(void){
421  //Nothing
422  int startCNT = CNT;
423  int endCNT = CNT;
424  int nothingDelta = endCNT - startCNT;
425 
426 
427  //64 bit
428  volatile int64_t a64 = 0x600000000;
429  startCNT = CNT;
430  a64 = a64 / 3;
431  endCNT = CNT;
432  UnityPrint("64bit divide deltaCNT == ");
433  UnityPrintNumber(endCNT - startCNT - nothingDelta);
434  UNITY_OUTPUT_CHAR('\n');
435 
436  //32 bit
437  volatile int32_t a32 = 0x60000;
438  startCNT = CNT;
439  a32 = a32 / 3;
440  endCNT = CNT;
441  UnityPrint("32bit divide deltaCNT == ");
442  UnityPrintNumber(endCNT - startCNT - nothingDelta);
443  UNITY_OUTPUT_CHAR('\n');
444 
445  TEST_ASSERT_EQUAL_INT(0x20000, a32);
446  #ifndef UNITY_SUPPORT_64
447  a64 = a64 >> 32;
448  TEST_ASSERT_EQUAL_INT(0x2, a64);
449  #else
450  TEST_ASSERT_EQUAL_HEX64(0x200000000, a64);
451  #endif
452 
453 
454  }
455 
456 
457  static void test_64bitIntegerMultiplySpeed(void){
458 
459  //Nothing
460  int startCNT = CNT;
461  int endCNT = CNT;
462  int nothingDelta = endCNT - startCNT;
463 
464  //64 bit
465  volatile int64_t a64 = 0x600000000;
466  startCNT = CNT;
467  a64 = a64 * 0x30;
468  endCNT = CNT;
469  UnityPrint("64bit multiply deltaCNT == ");
470  UnityPrintNumber(endCNT - startCNT - nothingDelta);
471  UNITY_OUTPUT_CHAR('\n');
472 
473 
474  //32 bit
475  volatile int32_t a32 = 0x60000;
476  startCNT = CNT;
477  a32 = a32 * 0x30;
478  endCNT = CNT;
479  UnityPrint("32bit multiply deltaCNT == ");
480  UnityPrintNumber(endCNT - startCNT - nothingDelta);
481  UNITY_OUTPUT_CHAR('\n');
482 
483  TEST_ASSERT_EQUAL_HEX32(0x1200000, a32);
484  #ifndef UNITY_SUPPORT_64
485  a64 = a64 >> 32;
486  TEST_ASSERT_EQUAL_HEX32(0x120, a64);
487  #else
488  TEST_ASSERT_EQUAL_HEX64(0x12000000000, a64);
489  #endif
490 
491 
492  }
493 
494  // -----------------------------------------------------------------------------
495 
496  static void test_FloatVariableToInt(void){
497  // Get the bits of a float number into an int
498  const float floatNum = 0.01f;
499  int number = *(int *)&floatNum;
500  TEST_ASSERT_EQUAL_HEX32(0x3C23D70A, number);
501  }
502 
503  // -----------------------------------------------------------------------------
504 
505  static void test_SignedVsUnsignedComparison(void){
506  int sA = 0xFfffFfff;
507  int sB = 0x0fffFfff;
508 
509  TEST_ASSERT_TRUE(sB > sA);
510 
511  unsigned int uA = 0xFfffFfff;
512  unsigned int uB = 0x0fffFfff;
513 
514  TEST_ASSERT_TRUE(uA > uB);
515  }
516 
517  static void test_UnsignedReverseRolloverSubtraction(void){
518  unsigned int A = 0xF;
519  unsigned int B = 0x10;
520  unsigned int result = A - B;
521  TEST_ASSERT_EQUAL_HEX32(0xFfffFfff, result);
522 
523 
524  }
525 
526  // -----------------------------------------------------------------------------
527 
528 
529  static void test_nullptrIsTheSameAsNULL(void){
530  TEST_ASSERT_TRUE(nullptr == NULL);
531  }
532 
533 
534 };
535 
536 
537 
538 
539 
540 
541 
542 
543 
544 
545 
546 
547 
548 
549