Test program to check various functionality in FixedLengthList (see http://mbed.org/users/johnb/code/FixedLengthList/ )

Dependencies:   FixedLengthList mbed

Test code for FixedLengthList class

Committer:
johnb
Date:
Sat Jan 18 16:07:14 2014 +0000
Revision:
0:12569365e1cf
Child:
1:ae1cd5670fec
Project/program to test FixedLengthList

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:12569365e1cf 1 #if defined __CC_ARM
johnb 0:12569365e1cf 2 #include "mbed.h"
johnb 0:12569365e1cf 3 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:12569365e1cf 4 #define PRINTF( ... ) pc.printf(__VA_ARGS__)
johnb 0:12569365e1cf 5 #else
johnb 0:12569365e1cf 6 #include <stdio.h>
johnb 0:12569365e1cf 7 #define PRINTF( ... ) printf(__VA_ARGS__)
johnb 0:12569365e1cf 8 #endif
johnb 0:12569365e1cf 9
johnb 0:12569365e1cf 10 #include "FixedLengthList.hpp"
johnb 0:12569365e1cf 11
johnb 0:12569365e1cf 12 #define LIST_LEN (20U)
johnb 0:12569365e1cf 13 #define LIST2_INI (17U)
johnb 0:12569365e1cf 14 #define CHECK( _x, ... ) do { PRINTF( __VA_ARGS__ ); if( _x ) { PRINTF(" OK\r\n"); } else { PRINTF(" FAILED!\r\n"); } } while( 0 )
johnb 0:12569365e1cf 15
johnb 0:12569365e1cf 16 /* Check that we can have multiple lists co-existing */
johnb 0:12569365e1cf 17
johnb 0:12569365e1cf 18 int init_list[ LIST2_INI ] = { 12, 23, 34, 45, 56, 67, 78, 89, 100, 111,
johnb 0:12569365e1cf 19 122, 133, 144, 155, 166, 177, 188, };
johnb 0:12569365e1cf 20
johnb 0:12569365e1cf 21 FixedLengthList<int, LIST_LEN > list;
johnb 0:12569365e1cf 22 FixedLengthList<int, LIST_LEN > list2( init_list, LIST2_INI );
johnb 0:12569365e1cf 23 #if 0
johnb 0:12569365e1cf 24 FixedLengthList<char, LIST_LEN > list3;
johnb 0:12569365e1cf 25 #endif
johnb 0:12569365e1cf 26
johnb 0:12569365e1cf 27
johnb 0:12569365e1cf 28 int main() {
johnb 0:12569365e1cf 29 int i = 0;
johnb 0:12569365e1cf 30 PRINTF("FixedLengthList test\n");
johnb 0:12569365e1cf 31
johnb 0:12569365e1cf 32 /* Test operations on an empty list */
johnb 0:12569365e1cf 33 CHECK( list.used() == 0, "Initial used()" );
johnb 0:12569365e1cf 34 CHECK( list.inList(1) == false, "inList() on empty list" );
johnb 0:12569365e1cf 35 CHECK( list.available() == LIST_LEN, "Initial available()" );
johnb 0:12569365e1cf 36 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 37 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 38
johnb 0:12569365e1cf 39 /* Push a little data into the list, few tests, return to empty list */
johnb 0:12569365e1cf 40 CHECK( list.push( 1 ), "Initial push()" );
johnb 0:12569365e1cf 41 CHECK( list.inList(1) == true, "inList() for item which is in list" );
johnb 0:12569365e1cf 42 CHECK( list.inList(2) == false, "inList() for item which is not in list" );
johnb 0:12569365e1cf 43 CHECK( list.used() == 1, "used() after initial push()" );
johnb 0:12569365e1cf 44 CHECK( list.pop( &i ), "Check pop() returned OK" );
johnb 0:12569365e1cf 45 CHECK( i == 1, "pop() yielded correct value" );
johnb 0:12569365e1cf 46 CHECK( list.used() == 0, "used() after fully depleting pop" );
johnb 0:12569365e1cf 47 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 48 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 49
johnb 0:12569365e1cf 50 /* Push data onto an empty list and then dequeue & pop it */
johnb 0:12569365e1cf 51 CHECK( list.push( 22 ), "push() 22 (getting some data in)" );
johnb 0:12569365e1cf 52 CHECK( list.push( 33 ), "push() 33 (getting some data in)" );
johnb 0:12569365e1cf 53 CHECK( list.push( 44 ), "push() 33 (getting some data in)" );
johnb 0:12569365e1cf 54 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 55 CHECK( i == 22, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 56 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 57 CHECK( i == 33, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 58 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 59 CHECK( i == 44, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 60 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 61 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 62 CHECK( list.push( 55 ), "push() 55 (getting some data in)" );
johnb 0:12569365e1cf 63 CHECK( list.push( 66 ), "push() 66 (getting some data in)" );
johnb 0:12569365e1cf 64 CHECK( list.push( 77 ), "push() 77 (getting some data in)" );
johnb 0:12569365e1cf 65 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 66 CHECK( i == 77, "pop() yielded correct value" );
johnb 0:12569365e1cf 67 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 68 CHECK( i == 55, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 69 CHECK( list.queue( 88 ), "push() 88 (getting some data in)" );
johnb 0:12569365e1cf 70 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 71 CHECK( i == 66, "pop() yielded correct value" );
johnb 0:12569365e1cf 72 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 73 CHECK( i == 88, "pop() yielded correct value" );
johnb 0:12569365e1cf 74 CHECK( list.available() == LIST_LEN, "available()" );
johnb 0:12569365e1cf 75 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 76 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 77
johnb 0:12569365e1cf 78 /* Push data onto list until it's full & test operations on full list */
johnb 0:12569365e1cf 79 CHECK( list.push( 100 ), "push() 100 (getting some data in)" );
johnb 0:12569365e1cf 80 CHECK( list.push( 101 ), "push() 101 (getting some data in)" );
johnb 0:12569365e1cf 81 CHECK( list.push( 102 ), "push() 102 (getting some data in)" );
johnb 0:12569365e1cf 82 CHECK( list.push( 103 ), "push() 103 (getting some data in)" );
johnb 0:12569365e1cf 83 CHECK( list.push( 104 ), "push() 104 (getting some data in)" );
johnb 0:12569365e1cf 84 CHECK( list.push( 105 ), "push() 105 (getting some data in)" );
johnb 0:12569365e1cf 85 CHECK( list.push( 106 ), "push() 106 (getting some data in)" );
johnb 0:12569365e1cf 86 CHECK( list.push( 107 ), "push() 107 (getting some data in)" );
johnb 0:12569365e1cf 87 CHECK( list.push( 108 ), "push() 108 (getting some data in)" );
johnb 0:12569365e1cf 88 CHECK( list.push( 109 ), "push() 109 (getting some data in)" );
johnb 0:12569365e1cf 89 CHECK( list.push( 110 ), "push() 110 (getting some data in)" );
johnb 0:12569365e1cf 90 CHECK( list.queue( 111 ), "queue() 111 (getting some data in)" );
johnb 0:12569365e1cf 91 CHECK( list.queue( 112 ), "queue() 112 (getting some data in)" );
johnb 0:12569365e1cf 92 CHECK( list.queue( 113 ), "queue() 113 (getting some data in)" );
johnb 0:12569365e1cf 93 CHECK( list.queue( 114 ), "queue() 114 (getting some data in)" );
johnb 0:12569365e1cf 94 CHECK( list.queue( 115 ), "queue() 115 (getting some data in)" );
johnb 0:12569365e1cf 95 CHECK( list.queue( 116 ), "queue() 116 (getting some data in)" );
johnb 0:12569365e1cf 96 CHECK( list.queue( 117 ), "queue() 117 (getting some data in)" );
johnb 0:12569365e1cf 97 CHECK( list.queue( 118 ), "queue() 118 (getting some data in)" );
johnb 0:12569365e1cf 98 CHECK( list.queue( 119 ), "queue() 119 (getting some data in)" );
johnb 0:12569365e1cf 99 CHECK( list.queue( 120 ) == false, "queue() on a full list" );
johnb 0:12569365e1cf 100 CHECK( list.push( 120 ) == false, "push() on a full list" );
johnb 0:12569365e1cf 101 CHECK( list.available() == 0, "available()" );
johnb 0:12569365e1cf 102 CHECK( list.used() == LIST_LEN, "used() on full list" );
johnb 0:12569365e1cf 103
johnb 0:12569365e1cf 104 /* Some tests on the list using the initialising constructor - focused
johnb 0:12569365e1cf 105 around checking that the m_usedHead, m_usedTail, etc have been set
johnb 0:12569365e1cf 106 up correctly */
johnb 0:12569365e1cf 107 CHECK( list2.available() == (LIST_LEN-LIST2_INI), "list 2 available()" );
johnb 0:12569365e1cf 108 CHECK( list2.used() == LIST2_INI, "list 2 used()" );
johnb 0:12569365e1cf 109 CHECK( list2.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 110 CHECK( i == 12, "pop() yielded correct value" );
johnb 0:12569365e1cf 111 CHECK( list2.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 112 CHECK( i == 188, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 113 CHECK( list2.queue( 199 ), "queue() 199 (getting some data in)" );
johnb 0:12569365e1cf 114 CHECK( list2.queue( 210 ), "queue() 210 (getting some data in)" );
johnb 0:12569365e1cf 115 CHECK( list2.queue( 221 ), "queue() 221 (getting some data in)" );
johnb 0:12569365e1cf 116 CHECK( list2.queue( 232 ), "queue() 232 (getting some data in)" );
johnb 0:12569365e1cf 117 CHECK( list2.queue( 243 ), "queue() 243 (getting some data in)" );
johnb 0:12569365e1cf 118 CHECK( list2.queue( 254 ) == false, "queue() on a full list" );
johnb 0:12569365e1cf 119 PRINTF("FixedLengthList test - Done\n");
johnb 0:12569365e1cf 120 return 0;
johnb 0:12569365e1cf 121 }