|
| 1 | +#include <ArduinoUnitTests.h> |
| 2 | +#include "../src/cppQueue.h" |
| 3 | + |
| 4 | +#define IMPLEMENTATION FIFO |
| 5 | +#define OVERWRITE true |
| 6 | + |
| 7 | +#define NB_PUSH 14 |
| 8 | +#define NB_PULL 11 |
| 9 | +#define Q_SIZE 10 |
| 10 | + |
| 11 | +typedef struct strRec { |
| 12 | + uint16_t entry1; |
| 13 | + uint16_t entry2; |
| 14 | +} Rec; |
| 15 | + |
| 16 | +unittest(LibTst) |
| 17 | +{ |
| 18 | + Rec tab[6] = { |
| 19 | + { 0x1234, 0x3456 }, |
| 20 | + { 0x5678, 0x7890 }, |
| 21 | + { 0x90AB, 0xABCD }, |
| 22 | + { 0xCDEF, 0xEFDC }, |
| 23 | + { 0xDCBA, 0xBA09 }, |
| 24 | + { 0x0987, 0x8765 } |
| 25 | + }; |
| 26 | + |
| 27 | + Queue q(sizeof(Rec), Q_SIZE, IMPLEMENTATION, OVERWRITE); // Instantiate queue |
| 28 | + assertEqual(40, q.sizeOf()); |
| 29 | + assertEqual(0, q.getCount()); |
| 30 | + |
| 31 | + int i; |
| 32 | + for (i = 0 ; i < NB_PUSH ; i++) |
| 33 | + { |
| 34 | + Rec rec = tab[i % (sizeof(tab)/sizeof(Rec))]; |
| 35 | + q.push(&rec); |
| 36 | + assertEqual(min(Q_SIZE, i + 1), q.getCount()); |
| 37 | + assertEqual(max(0, (Q_SIZE - 1) - i), q.getRemainingCount()); |
| 38 | + assertEqual((i + 1) >= Q_SIZE, q.isFull()); |
| 39 | + } |
| 40 | + |
| 41 | + assertFalse(q.isEmpty()); |
| 42 | + assertEqual(10, q.getCount()); |
| 43 | + |
| 44 | + for (i = 0 ; i < NB_PULL+1 ; i++) |
| 45 | + { |
| 46 | + // account for the behavior of the test in the example, |
| 47 | + // where at an odd spot we peek instead of pop. |
| 48 | + bool canPop = i <= Q_SIZE; // note allowance for the peek -- 'i' can be 10 |
| 49 | + bool didPeek = i <= NB_PULL / 2; // matches logic of conditional |
| 50 | + int offset = didPeek ? 4 : 3; // adjust offset in tab |
| 51 | + int idx = (i + offset) % (sizeof(tab)/sizeof(Rec)); // index of tab |
| 52 | + Rec rec = {0xffff,0xffff}; |
| 53 | + if (i != NB_PULL / 2) |
| 54 | + { |
| 55 | + assertEqual(canPop, q.pop(&rec)); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + assertTrue(q.peek(&rec)); |
| 60 | + } |
| 61 | + |
| 62 | + assertEqual(canPop ? tab[idx].entry1 : 0xffff, rec.entry1); |
| 63 | + assertEqual(canPop ? tab[idx].entry2 : 0xffff, rec.entry2); |
| 64 | + } |
| 65 | + |
| 66 | + assertTrue(q.isEmpty()); |
| 67 | + assertEqual(0, q.getCount()); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +unittest_main() |
0 commit comments