![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program to check various functionality in FixedLengthList (see http://mbed.org/users/johnb/code/FixedLengthList/ )
Dependencies: FixedLengthList mbed
Test code for FixedLengthList class
main.cpp@1:ae1cd5670fec, 2014-01-18 (annotated)
- Committer:
- johnb
- Date:
- Sat Jan 18 16:09:51 2014 +0000
- Revision:
- 1:ae1cd5670fec
- Parent:
- 0:12569365e1cf
- Child:
- 2:e00ea5f2e80c
Add license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 1:ae1cd5670fec | 1 | /** |
johnb | 1:ae1cd5670fec | 2 | @file |
johnb | 1:ae1cd5670fec | 3 | @brief Tests for the FixedLengthList class |
johnb | 1:ae1cd5670fec | 4 | |
johnb | 1:ae1cd5670fec | 5 | @author John Bailey |
johnb | 1:ae1cd5670fec | 6 | |
johnb | 1:ae1cd5670fec | 7 | @copyright Copyright 2013 John Bailey |
johnb | 1:ae1cd5670fec | 8 | |
johnb | 1:ae1cd5670fec | 9 | @section LICENSE |
johnb | 1:ae1cd5670fec | 10 | |
johnb | 1:ae1cd5670fec | 11 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 1:ae1cd5670fec | 12 | you may not use this file except in compliance with the License. |
johnb | 1:ae1cd5670fec | 13 | You may obtain a copy of the License at |
johnb | 1:ae1cd5670fec | 14 | |
johnb | 1:ae1cd5670fec | 15 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 1:ae1cd5670fec | 16 | |
johnb | 1:ae1cd5670fec | 17 | Unless required by applicable law or agreed to in writing, software |
johnb | 1:ae1cd5670fec | 18 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 1:ae1cd5670fec | 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 1:ae1cd5670fec | 20 | See the License for the specific language governing permissions and |
johnb | 1:ae1cd5670fec | 21 | limitations under the License. |
johnb | 1:ae1cd5670fec | 22 | |
johnb | 1:ae1cd5670fec | 23 | */ |
johnb | 1:ae1cd5670fec | 24 | |
johnb | 0:12569365e1cf | 25 | #if defined __CC_ARM |
johnb | 0:12569365e1cf | 26 | #include "mbed.h" |
johnb | 0:12569365e1cf | 27 | Serial pc(USBTX, USBRX); // tx, rx |
johnb | 0:12569365e1cf | 28 | #define PRINTF( ... ) pc.printf(__VA_ARGS__) |
johnb | 0:12569365e1cf | 29 | #else |
johnb | 0:12569365e1cf | 30 | #include <stdio.h> |
johnb | 0:12569365e1cf | 31 | #define PRINTF( ... ) printf(__VA_ARGS__) |
johnb | 0:12569365e1cf | 32 | #endif |
johnb | 0:12569365e1cf | 33 | |
johnb | 0:12569365e1cf | 34 | #include "FixedLengthList.hpp" |
johnb | 0:12569365e1cf | 35 | |
johnb | 0:12569365e1cf | 36 | #define LIST_LEN (20U) |
johnb | 0:12569365e1cf | 37 | #define LIST2_INI (17U) |
johnb | 0:12569365e1cf | 38 | #define CHECK( _x, ... ) do { PRINTF( __VA_ARGS__ ); if( _x ) { PRINTF(" OK\r\n"); } else { PRINTF(" FAILED!\r\n"); } } while( 0 ) |
johnb | 0:12569365e1cf | 39 | |
johnb | 0:12569365e1cf | 40 | /* Check that we can have multiple lists co-existing */ |
johnb | 0:12569365e1cf | 41 | |
johnb | 0:12569365e1cf | 42 | int init_list[ LIST2_INI ] = { 12, 23, 34, 45, 56, 67, 78, 89, 100, 111, |
johnb | 0:12569365e1cf | 43 | 122, 133, 144, 155, 166, 177, 188, }; |
johnb | 0:12569365e1cf | 44 | |
johnb | 0:12569365e1cf | 45 | FixedLengthList<int, LIST_LEN > list; |
johnb | 0:12569365e1cf | 46 | FixedLengthList<int, LIST_LEN > list2( init_list, LIST2_INI ); |
johnb | 0:12569365e1cf | 47 | #if 0 |
johnb | 0:12569365e1cf | 48 | FixedLengthList<char, LIST_LEN > list3; |
johnb | 0:12569365e1cf | 49 | #endif |
johnb | 0:12569365e1cf | 50 | |
johnb | 0:12569365e1cf | 51 | |
johnb | 0:12569365e1cf | 52 | int main() { |
johnb | 0:12569365e1cf | 53 | int i = 0; |
johnb | 0:12569365e1cf | 54 | PRINTF("FixedLengthList test\n"); |
johnb | 0:12569365e1cf | 55 | |
johnb | 0:12569365e1cf | 56 | /* Test operations on an empty list */ |
johnb | 0:12569365e1cf | 57 | CHECK( list.used() == 0, "Initial used()" ); |
johnb | 0:12569365e1cf | 58 | CHECK( list.inList(1) == false, "inList() on empty list" ); |
johnb | 0:12569365e1cf | 59 | CHECK( list.available() == LIST_LEN, "Initial available()" ); |
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 | |
johnb | 0:12569365e1cf | 63 | /* Push a little data into the list, few tests, return to empty list */ |
johnb | 0:12569365e1cf | 64 | CHECK( list.push( 1 ), "Initial push()" ); |
johnb | 0:12569365e1cf | 65 | CHECK( list.inList(1) == true, "inList() for item which is in list" ); |
johnb | 0:12569365e1cf | 66 | CHECK( list.inList(2) == false, "inList() for item which is not in list" ); |
johnb | 0:12569365e1cf | 67 | CHECK( list.used() == 1, "used() after initial push()" ); |
johnb | 0:12569365e1cf | 68 | CHECK( list.pop( &i ), "Check pop() returned OK" ); |
johnb | 0:12569365e1cf | 69 | CHECK( i == 1, "pop() yielded correct value" ); |
johnb | 0:12569365e1cf | 70 | CHECK( list.used() == 0, "used() after fully depleting pop" ); |
johnb | 0:12569365e1cf | 71 | CHECK( list.pop(&i) == false, "pop() on empty list" ); |
johnb | 0:12569365e1cf | 72 | CHECK( list.dequeue(&i) == false, "dequeue() on empty list" ); |
johnb | 0:12569365e1cf | 73 | |
johnb | 0:12569365e1cf | 74 | /* Push data onto an empty list and then dequeue & pop it */ |
johnb | 0:12569365e1cf | 75 | CHECK( list.push( 22 ), "push() 22 (getting some data in)" ); |
johnb | 0:12569365e1cf | 76 | CHECK( list.push( 33 ), "push() 33 (getting some data in)" ); |
johnb | 0:12569365e1cf | 77 | CHECK( list.push( 44 ), "push() 33 (getting some data in)" ); |
johnb | 0:12569365e1cf | 78 | CHECK( list.dequeue(&i) == true, "dequeue() returned OK" ); |
johnb | 0:12569365e1cf | 79 | CHECK( i == 22, "dequeue() yielded correct value" ); |
johnb | 0:12569365e1cf | 80 | CHECK( list.dequeue(&i) == true, "dequeue() returned OK" ); |
johnb | 0:12569365e1cf | 81 | CHECK( i == 33, "dequeue() yielded correct value" ); |
johnb | 0:12569365e1cf | 82 | CHECK( list.dequeue(&i) == true, "dequeue() returned OK" ); |
johnb | 0:12569365e1cf | 83 | CHECK( i == 44, "dequeue() yielded correct value" ); |
johnb | 0:12569365e1cf | 84 | CHECK( list.pop(&i) == false, "pop() on empty list" ); |
johnb | 0:12569365e1cf | 85 | CHECK( list.dequeue(&i) == false, "dequeue() on empty list" ); |
johnb | 0:12569365e1cf | 86 | CHECK( list.push( 55 ), "push() 55 (getting some data in)" ); |
johnb | 0:12569365e1cf | 87 | CHECK( list.push( 66 ), "push() 66 (getting some data in)" ); |
johnb | 0:12569365e1cf | 88 | CHECK( list.push( 77 ), "push() 77 (getting some data in)" ); |
johnb | 0:12569365e1cf | 89 | CHECK( list.pop(&i) == true, "pop() returned OK" ); |
johnb | 0:12569365e1cf | 90 | CHECK( i == 77, "pop() yielded correct value" ); |
johnb | 0:12569365e1cf | 91 | CHECK( list.dequeue(&i) == true, "dequeue() returned OK" ); |
johnb | 0:12569365e1cf | 92 | CHECK( i == 55, "dequeue() yielded correct value" ); |
johnb | 0:12569365e1cf | 93 | CHECK( list.queue( 88 ), "push() 88 (getting some data in)" ); |
johnb | 0:12569365e1cf | 94 | CHECK( list.pop(&i) == true, "pop() returned OK" ); |
johnb | 0:12569365e1cf | 95 | CHECK( i == 66, "pop() yielded correct value" ); |
johnb | 0:12569365e1cf | 96 | CHECK( list.pop(&i) == true, "pop() returned OK" ); |
johnb | 0:12569365e1cf | 97 | CHECK( i == 88, "pop() yielded correct value" ); |
johnb | 0:12569365e1cf | 98 | CHECK( list.available() == LIST_LEN, "available()" ); |
johnb | 0:12569365e1cf | 99 | CHECK( list.pop(&i) == false, "pop() on empty list" ); |
johnb | 0:12569365e1cf | 100 | CHECK( list.dequeue(&i) == false, "dequeue() on empty list" ); |
johnb | 0:12569365e1cf | 101 | |
johnb | 0:12569365e1cf | 102 | /* Push data onto list until it's full & test operations on full list */ |
johnb | 0:12569365e1cf | 103 | CHECK( list.push( 100 ), "push() 100 (getting some data in)" ); |
johnb | 0:12569365e1cf | 104 | CHECK( list.push( 101 ), "push() 101 (getting some data in)" ); |
johnb | 0:12569365e1cf | 105 | CHECK( list.push( 102 ), "push() 102 (getting some data in)" ); |
johnb | 0:12569365e1cf | 106 | CHECK( list.push( 103 ), "push() 103 (getting some data in)" ); |
johnb | 0:12569365e1cf | 107 | CHECK( list.push( 104 ), "push() 104 (getting some data in)" ); |
johnb | 0:12569365e1cf | 108 | CHECK( list.push( 105 ), "push() 105 (getting some data in)" ); |
johnb | 0:12569365e1cf | 109 | CHECK( list.push( 106 ), "push() 106 (getting some data in)" ); |
johnb | 0:12569365e1cf | 110 | CHECK( list.push( 107 ), "push() 107 (getting some data in)" ); |
johnb | 0:12569365e1cf | 111 | CHECK( list.push( 108 ), "push() 108 (getting some data in)" ); |
johnb | 0:12569365e1cf | 112 | CHECK( list.push( 109 ), "push() 109 (getting some data in)" ); |
johnb | 0:12569365e1cf | 113 | CHECK( list.push( 110 ), "push() 110 (getting some data in)" ); |
johnb | 0:12569365e1cf | 114 | CHECK( list.queue( 111 ), "queue() 111 (getting some data in)" ); |
johnb | 0:12569365e1cf | 115 | CHECK( list.queue( 112 ), "queue() 112 (getting some data in)" ); |
johnb | 0:12569365e1cf | 116 | CHECK( list.queue( 113 ), "queue() 113 (getting some data in)" ); |
johnb | 0:12569365e1cf | 117 | CHECK( list.queue( 114 ), "queue() 114 (getting some data in)" ); |
johnb | 0:12569365e1cf | 118 | CHECK( list.queue( 115 ), "queue() 115 (getting some data in)" ); |
johnb | 0:12569365e1cf | 119 | CHECK( list.queue( 116 ), "queue() 116 (getting some data in)" ); |
johnb | 0:12569365e1cf | 120 | CHECK( list.queue( 117 ), "queue() 117 (getting some data in)" ); |
johnb | 0:12569365e1cf | 121 | CHECK( list.queue( 118 ), "queue() 118 (getting some data in)" ); |
johnb | 0:12569365e1cf | 122 | CHECK( list.queue( 119 ), "queue() 119 (getting some data in)" ); |
johnb | 0:12569365e1cf | 123 | CHECK( list.queue( 120 ) == false, "queue() on a full list" ); |
johnb | 0:12569365e1cf | 124 | CHECK( list.push( 120 ) == false, "push() on a full list" ); |
johnb | 0:12569365e1cf | 125 | CHECK( list.available() == 0, "available()" ); |
johnb | 0:12569365e1cf | 126 | CHECK( list.used() == LIST_LEN, "used() on full list" ); |
johnb | 0:12569365e1cf | 127 | |
johnb | 0:12569365e1cf | 128 | /* Some tests on the list using the initialising constructor - focused |
johnb | 0:12569365e1cf | 129 | around checking that the m_usedHead, m_usedTail, etc have been set |
johnb | 0:12569365e1cf | 130 | up correctly */ |
johnb | 0:12569365e1cf | 131 | CHECK( list2.available() == (LIST_LEN-LIST2_INI), "list 2 available()" ); |
johnb | 0:12569365e1cf | 132 | CHECK( list2.used() == LIST2_INI, "list 2 used()" ); |
johnb | 0:12569365e1cf | 133 | CHECK( list2.pop(&i) == true, "pop() returned OK" ); |
johnb | 0:12569365e1cf | 134 | CHECK( i == 12, "pop() yielded correct value" ); |
johnb | 0:12569365e1cf | 135 | CHECK( list2.dequeue(&i) == true, "dequeue() returned OK" ); |
johnb | 0:12569365e1cf | 136 | CHECK( i == 188, "dequeue() yielded correct value" ); |
johnb | 0:12569365e1cf | 137 | CHECK( list2.queue( 199 ), "queue() 199 (getting some data in)" ); |
johnb | 0:12569365e1cf | 138 | CHECK( list2.queue( 210 ), "queue() 210 (getting some data in)" ); |
johnb | 0:12569365e1cf | 139 | CHECK( list2.queue( 221 ), "queue() 221 (getting some data in)" ); |
johnb | 0:12569365e1cf | 140 | CHECK( list2.queue( 232 ), "queue() 232 (getting some data in)" ); |
johnb | 0:12569365e1cf | 141 | CHECK( list2.queue( 243 ), "queue() 243 (getting some data in)" ); |
johnb | 0:12569365e1cf | 142 | CHECK( list2.queue( 254 ) == false, "queue() on a full list" ); |
johnb | 0:12569365e1cf | 143 | PRINTF("FixedLengthList test - Done\n"); |
johnb | 0:12569365e1cf | 144 | return 0; |
johnb | 0:12569365e1cf | 145 | } |