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:
Wed Jan 29 23:01:16 2014 +0000
Revision:
3:5480dece8fa5
Parent:
2:e00ea5f2e80c
Add tests for remove() method

Who changed what in which revision?

UserRevisionLine numberNew 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 3:5480dece8fa5 7 @copyright Copyright 2014 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 2:e00ea5f2e80c 51 static void check_iterators( void );
johnb 0:12569365e1cf 52
johnb 0:12569365e1cf 53 int main() {
johnb 0:12569365e1cf 54 int i = 0;
johnb 0:12569365e1cf 55 PRINTF("FixedLengthList test\n");
johnb 0:12569365e1cf 56
johnb 0:12569365e1cf 57 /* Test operations on an empty list */
johnb 0:12569365e1cf 58 CHECK( list.used() == 0, "Initial used()" );
johnb 0:12569365e1cf 59 CHECK( list.inList(1) == false, "inList() on empty list" );
johnb 0:12569365e1cf 60 CHECK( list.available() == LIST_LEN, "Initial available()" );
johnb 0:12569365e1cf 61 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 62 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 63
johnb 0:12569365e1cf 64 /* Push a little data into the list, few tests, return to empty list */
johnb 0:12569365e1cf 65 CHECK( list.push( 1 ), "Initial push()" );
johnb 0:12569365e1cf 66 CHECK( list.inList(1) == true, "inList() for item which is in list" );
johnb 0:12569365e1cf 67 CHECK( list.inList(2) == false, "inList() for item which is not in list" );
johnb 0:12569365e1cf 68 CHECK( list.used() == 1, "used() after initial push()" );
johnb 0:12569365e1cf 69 CHECK( list.pop( &i ), "Check pop() returned OK" );
johnb 0:12569365e1cf 70 CHECK( i == 1, "pop() yielded correct value" );
johnb 0:12569365e1cf 71 CHECK( list.used() == 0, "used() after fully depleting pop" );
johnb 0:12569365e1cf 72 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 73 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 74
johnb 0:12569365e1cf 75 /* Push data onto an empty list and then dequeue & pop it */
johnb 0:12569365e1cf 76 CHECK( list.push( 22 ), "push() 22 (getting some data in)" );
johnb 0:12569365e1cf 77 CHECK( list.push( 33 ), "push() 33 (getting some data in)" );
johnb 0:12569365e1cf 78 CHECK( list.push( 44 ), "push() 33 (getting some data in)" );
johnb 0:12569365e1cf 79 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 80 CHECK( i == 22, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 81 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 82 CHECK( i == 33, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 83 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 84 CHECK( i == 44, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 85 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 86 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 87 CHECK( list.push( 55 ), "push() 55 (getting some data in)" );
johnb 0:12569365e1cf 88 CHECK( list.push( 66 ), "push() 66 (getting some data in)" );
johnb 0:12569365e1cf 89 CHECK( list.push( 77 ), "push() 77 (getting some data in)" );
johnb 0:12569365e1cf 90 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 91 CHECK( i == 77, "pop() yielded correct value" );
johnb 0:12569365e1cf 92 CHECK( list.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 93 CHECK( i == 55, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 94 CHECK( list.queue( 88 ), "push() 88 (getting some data in)" );
johnb 0:12569365e1cf 95 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 96 CHECK( i == 66, "pop() yielded correct value" );
johnb 0:12569365e1cf 97 CHECK( list.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 98 CHECK( i == 88, "pop() yielded correct value" );
johnb 0:12569365e1cf 99 CHECK( list.available() == LIST_LEN, "available()" );
johnb 0:12569365e1cf 100 CHECK( list.pop(&i) == false, "pop() on empty list" );
johnb 0:12569365e1cf 101 CHECK( list.dequeue(&i) == false, "dequeue() on empty list" );
johnb 0:12569365e1cf 102
johnb 0:12569365e1cf 103 /* Push data onto list until it's full & test operations on full list */
johnb 0:12569365e1cf 104 CHECK( list.push( 100 ), "push() 100 (getting some data in)" );
johnb 0:12569365e1cf 105 CHECK( list.push( 101 ), "push() 101 (getting some data in)" );
johnb 0:12569365e1cf 106 CHECK( list.push( 102 ), "push() 102 (getting some data in)" );
johnb 0:12569365e1cf 107 CHECK( list.push( 103 ), "push() 103 (getting some data in)" );
johnb 0:12569365e1cf 108 CHECK( list.push( 104 ), "push() 104 (getting some data in)" );
johnb 0:12569365e1cf 109 CHECK( list.push( 105 ), "push() 105 (getting some data in)" );
johnb 0:12569365e1cf 110 CHECK( list.push( 106 ), "push() 106 (getting some data in)" );
johnb 0:12569365e1cf 111 CHECK( list.push( 107 ), "push() 107 (getting some data in)" );
johnb 0:12569365e1cf 112 CHECK( list.push( 108 ), "push() 108 (getting some data in)" );
johnb 0:12569365e1cf 113 CHECK( list.push( 109 ), "push() 109 (getting some data in)" );
johnb 0:12569365e1cf 114 CHECK( list.push( 110 ), "push() 110 (getting some data in)" );
johnb 0:12569365e1cf 115 CHECK( list.queue( 111 ), "queue() 111 (getting some data in)" );
johnb 0:12569365e1cf 116 CHECK( list.queue( 112 ), "queue() 112 (getting some data in)" );
johnb 0:12569365e1cf 117 CHECK( list.queue( 113 ), "queue() 113 (getting some data in)" );
johnb 0:12569365e1cf 118 CHECK( list.queue( 114 ), "queue() 114 (getting some data in)" );
johnb 0:12569365e1cf 119 CHECK( list.queue( 115 ), "queue() 115 (getting some data in)" );
johnb 0:12569365e1cf 120 CHECK( list.queue( 116 ), "queue() 116 (getting some data in)" );
johnb 0:12569365e1cf 121 CHECK( list.queue( 117 ), "queue() 117 (getting some data in)" );
johnb 0:12569365e1cf 122 CHECK( list.queue( 118 ), "queue() 118 (getting some data in)" );
johnb 0:12569365e1cf 123 CHECK( list.queue( 119 ), "queue() 119 (getting some data in)" );
johnb 0:12569365e1cf 124 CHECK( list.queue( 120 ) == false, "queue() on a full list" );
johnb 0:12569365e1cf 125 CHECK( list.push( 120 ) == false, "push() on a full list" );
johnb 0:12569365e1cf 126 CHECK( list.available() == 0, "available()" );
johnb 0:12569365e1cf 127 CHECK( list.used() == LIST_LEN, "used() on full list" );
johnb 0:12569365e1cf 128
johnb 0:12569365e1cf 129 /* Some tests on the list using the initialising constructor - focused
johnb 0:12569365e1cf 130 around checking that the m_usedHead, m_usedTail, etc have been set
johnb 0:12569365e1cf 131 up correctly */
johnb 0:12569365e1cf 132 CHECK( list2.available() == (LIST_LEN-LIST2_INI), "list 2 available()" );
johnb 0:12569365e1cf 133 CHECK( list2.used() == LIST2_INI, "list 2 used()" );
johnb 0:12569365e1cf 134 CHECK( list2.pop(&i) == true, "pop() returned OK" );
johnb 0:12569365e1cf 135 CHECK( i == 12, "pop() yielded correct value" );
johnb 0:12569365e1cf 136 CHECK( list2.dequeue(&i) == true, "dequeue() returned OK" );
johnb 0:12569365e1cf 137 CHECK( i == 188, "dequeue() yielded correct value" );
johnb 0:12569365e1cf 138 CHECK( list2.queue( 199 ), "queue() 199 (getting some data in)" );
johnb 0:12569365e1cf 139 CHECK( list2.queue( 210 ), "queue() 210 (getting some data in)" );
johnb 0:12569365e1cf 140 CHECK( list2.queue( 221 ), "queue() 221 (getting some data in)" );
johnb 0:12569365e1cf 141 CHECK( list2.queue( 232 ), "queue() 232 (getting some data in)" );
johnb 0:12569365e1cf 142 CHECK( list2.queue( 243 ), "queue() 243 (getting some data in)" );
johnb 0:12569365e1cf 143 CHECK( list2.queue( 254 ) == false, "queue() on a full list" );
johnb 2:e00ea5f2e80c 144
johnb 2:e00ea5f2e80c 145 check_iterators();
johnb 3:5480dece8fa5 146
johnb 3:5480dece8fa5 147 CHECK( list2.remove( 255 ) == false, "remove() a non-existant item" );
johnb 3:5480dece8fa5 148 CHECK( list2.available() == 0, "available() having tried to remove non-existent item from full list" );
johnb 3:5480dece8fa5 149 CHECK( list2.remove( 243 ) == true, "remove() a valid item" );
johnb 3:5480dece8fa5 150 CHECK( list2.available() == 1, "available() having removed item from full list" );
johnb 3:5480dece8fa5 151 CHECK( list2.inList( 243 ) == false, "inList() for item which was just remove()d" );
johnb 2:e00ea5f2e80c 152
johnb 0:12569365e1cf 153 PRINTF("FixedLengthList test - Done\n");
johnb 2:e00ea5f2e80c 154
johnb 0:12569365e1cf 155 return 0;
johnb 0:12569365e1cf 156 }
johnb 2:e00ea5f2e80c 157
johnb 2:e00ea5f2e80c 158 static void check_iterators( void )
johnb 2:e00ea5f2e80c 159 {
johnb 2:e00ea5f2e80c 160 FixedLengthList<int, LIST_LEN > ilist( init_list, LIST2_INI );
johnb 2:e00ea5f2e80c 161 FixedLengthList<int, LIST_LEN >::iterator it;
johnb 2:e00ea5f2e80c 162
johnb 2:e00ea5f2e80c 163 it = ilist.begin();
johnb 2:e00ea5f2e80c 164 CHECK( *it == init_list[0], "iterators: begin()" );
johnb 2:e00ea5f2e80c 165 CHECK( it == ilist.begin(), "iterators: operator==" );
johnb 2:e00ea5f2e80c 166 CHECK( it != ilist.end(), "iterators: operator!=" );
johnb 2:e00ea5f2e80c 167 CHECK( *(it++) == init_list[0], "iterators: operator++ (post-increment)" );
johnb 2:e00ea5f2e80c 168 CHECK( *it == init_list[1], "iterators: operator++ (post-increment)" );
johnb 2:e00ea5f2e80c 169 CHECK( *(++it) == init_list[2], "iterators: operator++ (pre-increment)" );
johnb 2:e00ea5f2e80c 170 CHECK( *it == init_list[2], "iterators: operator++ (pre-increment)" );
johnb 2:e00ea5f2e80c 171 it+=15;
johnb 2:e00ea5f2e80c 172 CHECK( it != ilist.begin(), "iterators: operator!=" );
johnb 2:e00ea5f2e80c 173 CHECK( it == ilist.end(), "iterators: operator==" );
johnb 2:e00ea5f2e80c 174
johnb 3:5480dece8fa5 175 }