Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z by
SocketModem/tests/test_MTS_Circular_Buffer.h@2:2f9019c5a9fc, 2014-07-02 (annotated)
- Committer:
- AxedaCorp
- Date:
- Wed Jul 02 19:57:37 2014 +0000
- Revision:
- 2:2f9019c5a9fc
- Parent:
- 0:65004368569c
ip switch
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | /* Universal Socket Modem Interface Library |
AxedaCorp | 0:65004368569c | 2 | * Copyright (c) 2013 Multi-Tech Systems |
AxedaCorp | 0:65004368569c | 3 | * |
AxedaCorp | 0:65004368569c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AxedaCorp | 0:65004368569c | 5 | * you may not use this file except in compliance with the License. |
AxedaCorp | 0:65004368569c | 6 | * You may obtain a copy of the License at |
AxedaCorp | 0:65004368569c | 7 | * |
AxedaCorp | 0:65004368569c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AxedaCorp | 0:65004368569c | 9 | * |
AxedaCorp | 0:65004368569c | 10 | * Unless required by applicable law or agreed to in writing, software |
AxedaCorp | 0:65004368569c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AxedaCorp | 0:65004368569c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AxedaCorp | 0:65004368569c | 13 | * See the License for the specific language governing permissions and |
AxedaCorp | 0:65004368569c | 14 | * limitations under the License. |
AxedaCorp | 0:65004368569c | 15 | */ |
AxedaCorp | 0:65004368569c | 16 | |
AxedaCorp | 0:65004368569c | 17 | #ifndef TESTMTSCIRCULARBUFFER_H |
AxedaCorp | 0:65004368569c | 18 | #define TESTMTSCIRCULARBUFFER_H |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | #include "MTSCircularBuffer.h" |
AxedaCorp | 0:65004368569c | 21 | #include "Vars.h" |
AxedaCorp | 0:65004368569c | 22 | |
AxedaCorp | 0:65004368569c | 23 | /* unit tests for the circular buffer class */ |
AxedaCorp | 0:65004368569c | 24 | |
AxedaCorp | 0:65004368569c | 25 | using namespace mts; |
AxedaCorp | 0:65004368569c | 26 | |
AxedaCorp | 0:65004368569c | 27 | int capacity = 0; |
AxedaCorp | 0:65004368569c | 28 | MTSCircularBuffer* buffer = new MTSCircularBuffer(5); |
AxedaCorp | 0:65004368569c | 29 | |
AxedaCorp | 0:65004368569c | 30 | void callback() |
AxedaCorp | 0:65004368569c | 31 | { |
AxedaCorp | 0:65004368569c | 32 | capacity = buffer->remaining(); |
AxedaCorp | 0:65004368569c | 33 | } |
AxedaCorp | 0:65004368569c | 34 | |
AxedaCorp | 0:65004368569c | 35 | int testMTSCircularBuffer() |
AxedaCorp | 0:65004368569c | 36 | { |
AxedaCorp | 0:65004368569c | 37 | printf("Testing: MTSCircularBuffer\r\n"); |
AxedaCorp | 0:65004368569c | 38 | int failed = 0; |
AxedaCorp | 0:65004368569c | 39 | char byte; |
AxedaCorp | 0:65004368569c | 40 | |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | //Test getSize method |
AxedaCorp | 0:65004368569c | 43 | if (buffer->capacity() != 5) { |
AxedaCorp | 0:65004368569c | 44 | printf("Failed: capacity()\r\n"); |
AxedaCorp | 0:65004368569c | 45 | failed++; |
AxedaCorp | 0:65004368569c | 46 | } |
AxedaCorp | 0:65004368569c | 47 | |
AxedaCorp | 0:65004368569c | 48 | //Test clear function |
AxedaCorp | 0:65004368569c | 49 | buffer->write("AT", 2); |
AxedaCorp | 0:65004368569c | 50 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 51 | if (buffer->size() != 0) { |
AxedaCorp | 0:65004368569c | 52 | printf("Failed: clear()\r\n"); |
AxedaCorp | 0:65004368569c | 53 | failed++; |
AxedaCorp | 0:65004368569c | 54 | } |
AxedaCorp | 0:65004368569c | 55 | |
AxedaCorp | 0:65004368569c | 56 | /* The next set of test all rely on an empty buffer!!! */ |
AxedaCorp | 0:65004368569c | 57 | |
AxedaCorp | 0:65004368569c | 58 | //Test isEmpty method - empty buffer |
AxedaCorp | 0:65004368569c | 59 | if (buffer->isEmpty() != true) { |
AxedaCorp | 0:65004368569c | 60 | printf("Failed: isEmpty() - empty\r\n"); |
AxedaCorp | 0:65004368569c | 61 | failed++; |
AxedaCorp | 0:65004368569c | 62 | } |
AxedaCorp | 0:65004368569c | 63 | |
AxedaCorp | 0:65004368569c | 64 | //Test isFull method - empty buffer |
AxedaCorp | 0:65004368569c | 65 | if (buffer->isFull() == true) { |
AxedaCorp | 0:65004368569c | 66 | printf("Failed: isFull() - empty\r\n"); |
AxedaCorp | 0:65004368569c | 67 | failed++; |
AxedaCorp | 0:65004368569c | 68 | } |
AxedaCorp | 0:65004368569c | 69 | |
AxedaCorp | 0:65004368569c | 70 | //Test capacity method - empty buffer |
AxedaCorp | 0:65004368569c | 71 | if (buffer->remaining() != 5) { |
AxedaCorp | 0:65004368569c | 72 | printf("Failed: remaining() - empty\r\n"); |
AxedaCorp | 0:65004368569c | 73 | failed++; |
AxedaCorp | 0:65004368569c | 74 | } |
AxedaCorp | 0:65004368569c | 75 | |
AxedaCorp | 0:65004368569c | 76 | //Test available method - empty buffer |
AxedaCorp | 0:65004368569c | 77 | if (buffer->size() != 0) { |
AxedaCorp | 0:65004368569c | 78 | printf("Failed: size() - empty\r\n"); |
AxedaCorp | 0:65004368569c | 79 | failed++; |
AxedaCorp | 0:65004368569c | 80 | } |
AxedaCorp | 0:65004368569c | 81 | |
AxedaCorp | 0:65004368569c | 82 | /* The next set of tests all rely on a full buffer */ |
AxedaCorp | 0:65004368569c | 83 | |
AxedaCorp | 0:65004368569c | 84 | //Test bulk write method |
AxedaCorp | 0:65004368569c | 85 | int tmp = buffer->write("Test", 5); |
AxedaCorp | 0:65004368569c | 86 | if (tmp != 5) { |
AxedaCorp | 0:65004368569c | 87 | printf("Failed: bulk write()\r\n"); |
AxedaCorp | 0:65004368569c | 88 | failed++; |
AxedaCorp | 0:65004368569c | 89 | } |
AxedaCorp | 0:65004368569c | 90 | |
AxedaCorp | 0:65004368569c | 91 | //Test isEmpty method - full buffer |
AxedaCorp | 0:65004368569c | 92 | if (buffer->isEmpty() == true) { |
AxedaCorp | 0:65004368569c | 93 | printf("Failed: isEmpty() - full\r\n"); |
AxedaCorp | 0:65004368569c | 94 | failed++; |
AxedaCorp | 0:65004368569c | 95 | } |
AxedaCorp | 0:65004368569c | 96 | |
AxedaCorp | 0:65004368569c | 97 | //Test isFull method - full buffer |
AxedaCorp | 0:65004368569c | 98 | if (buffer->isFull() == false) { |
AxedaCorp | 0:65004368569c | 99 | printf("Failed: isFull() - full\r\n"); |
AxedaCorp | 0:65004368569c | 100 | failed++; |
AxedaCorp | 0:65004368569c | 101 | } |
AxedaCorp | 0:65004368569c | 102 | |
AxedaCorp | 0:65004368569c | 103 | //Test capacity method - full buffer |
AxedaCorp | 0:65004368569c | 104 | if (buffer->remaining() != 0) { |
AxedaCorp | 0:65004368569c | 105 | printf("Failed: remaining() - full\r\n"); |
AxedaCorp | 0:65004368569c | 106 | failed++; |
AxedaCorp | 0:65004368569c | 107 | } |
AxedaCorp | 0:65004368569c | 108 | |
AxedaCorp | 0:65004368569c | 109 | //Test available method - full buffer |
AxedaCorp | 0:65004368569c | 110 | if (buffer->size() != 5) { |
AxedaCorp | 0:65004368569c | 111 | printf("Failed: size() - full\r\n"); |
AxedaCorp | 0:65004368569c | 112 | failed++; |
AxedaCorp | 0:65004368569c | 113 | } |
AxedaCorp | 0:65004368569c | 114 | |
AxedaCorp | 0:65004368569c | 115 | //Test single overwrite method |
AxedaCorp | 0:65004368569c | 116 | if (buffer->write('A') != 0) { |
AxedaCorp | 0:65004368569c | 117 | printf("Failed: write() - overwrite\r\n"); |
AxedaCorp | 0:65004368569c | 118 | failed++; |
AxedaCorp | 0:65004368569c | 119 | } |
AxedaCorp | 0:65004368569c | 120 | |
AxedaCorp | 0:65004368569c | 121 | //Test bulk overwrite method |
AxedaCorp | 0:65004368569c | 122 | if (buffer->write("Test", 5) != 0) { |
AxedaCorp | 0:65004368569c | 123 | printf("Failed: bulk write() - overflow\r\n"); |
AxedaCorp | 0:65004368569c | 124 | failed++; |
AxedaCorp | 0:65004368569c | 125 | } |
AxedaCorp | 0:65004368569c | 126 | |
AxedaCorp | 0:65004368569c | 127 | //Test single read method |
AxedaCorp | 0:65004368569c | 128 | if ((buffer->read(byte) < 1 && byte != 'T') || buffer->remaining() != 1) { |
AxedaCorp | 0:65004368569c | 129 | printf("Failed: single read()\r\n"); |
AxedaCorp | 0:65004368569c | 130 | failed++; |
AxedaCorp | 0:65004368569c | 131 | } |
AxedaCorp | 0:65004368569c | 132 | |
AxedaCorp | 0:65004368569c | 133 | //Test bulk read method |
AxedaCorp | 0:65004368569c | 134 | char data[5]; |
AxedaCorp | 0:65004368569c | 135 | if (buffer->read(data, 4) != 4 || data[0] != 'e' || data[1] != 's' || data[2] != 't' || data[3] != '\0') { |
AxedaCorp | 0:65004368569c | 136 | printf("Failed: bulk read()\r\n"); |
AxedaCorp | 0:65004368569c | 137 | failed++; |
AxedaCorp | 0:65004368569c | 138 | } |
AxedaCorp | 0:65004368569c | 139 | |
AxedaCorp | 0:65004368569c | 140 | //Test wrap write/read methods |
AxedaCorp | 0:65004368569c | 141 | tmp = buffer->write("AT", 3); |
AxedaCorp | 0:65004368569c | 142 | if (tmp != 3 || (buffer->read(byte) < 1 && byte != 'A') || (buffer->read(byte) < 1 && byte != 'T')) { |
AxedaCorp | 0:65004368569c | 143 | printf("Failed: wrap write()/read()\r\n"); |
AxedaCorp | 0:65004368569c | 144 | failed++; |
AxedaCorp | 0:65004368569c | 145 | } |
AxedaCorp | 0:65004368569c | 146 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 147 | |
AxedaCorp | 0:65004368569c | 148 | /* The next set of test are focused all on the attach methods */ |
AxedaCorp | 0:65004368569c | 149 | |
AxedaCorp | 0:65004368569c | 150 | //Test attach with greater than below level |
AxedaCorp | 0:65004368569c | 151 | buffer->attach(&callback, 3, Vars::GREATER); |
AxedaCorp | 0:65004368569c | 152 | buffer->write("ABC", 3); |
AxedaCorp | 0:65004368569c | 153 | if (capacity != 0) { |
AxedaCorp | 0:65004368569c | 154 | printf("Failed: attach() - greater/below\r\n"); |
AxedaCorp | 0:65004368569c | 155 | failed++; |
AxedaCorp | 0:65004368569c | 156 | } |
AxedaCorp | 0:65004368569c | 157 | |
AxedaCorp | 0:65004368569c | 158 | //Test attach with greater than above level |
AxedaCorp | 0:65004368569c | 159 | buffer->write('T'); |
AxedaCorp | 0:65004368569c | 160 | if (capacity != 1) { |
AxedaCorp | 0:65004368569c | 161 | printf("Failed: attach() - greater/above\r\n"); |
AxedaCorp | 0:65004368569c | 162 | failed++; |
AxedaCorp | 0:65004368569c | 163 | } |
AxedaCorp | 0:65004368569c | 164 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 165 | capacity = 0; |
AxedaCorp | 0:65004368569c | 166 | |
AxedaCorp | 0:65004368569c | 167 | //Test attach with greater equal than below level |
AxedaCorp | 0:65004368569c | 168 | buffer->attach(&callback, 3, Vars::GREATER_EQUAL); |
AxedaCorp | 0:65004368569c | 169 | buffer->write("AB", 2); |
AxedaCorp | 0:65004368569c | 170 | if (capacity != 0) { |
AxedaCorp | 0:65004368569c | 171 | printf("Failed: attach() - greater equal/below\r\n"); |
AxedaCorp | 0:65004368569c | 172 | failed++; |
AxedaCorp | 0:65004368569c | 173 | } |
AxedaCorp | 0:65004368569c | 174 | |
AxedaCorp | 0:65004368569c | 175 | //Test attach with greater equal than above level |
AxedaCorp | 0:65004368569c | 176 | buffer->write('T'); |
AxedaCorp | 0:65004368569c | 177 | if (capacity != 2) { |
AxedaCorp | 0:65004368569c | 178 | printf("Failed: attach() - greater equal/above\r\n"); |
AxedaCorp | 0:65004368569c | 179 | failed++; |
AxedaCorp | 0:65004368569c | 180 | } |
AxedaCorp | 0:65004368569c | 181 | |
AxedaCorp | 0:65004368569c | 182 | //Test attach with less than above level |
AxedaCorp | 0:65004368569c | 183 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 184 | buffer->write("ABC", 3); |
AxedaCorp | 0:65004368569c | 185 | capacity = 0; |
AxedaCorp | 0:65004368569c | 186 | buffer->attach(&callback, 2, Vars::LESS); |
AxedaCorp | 0:65004368569c | 187 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 188 | if (capacity != 0) { |
AxedaCorp | 0:65004368569c | 189 | printf("Failed: attach() - less_equal/above\r\n"); |
AxedaCorp | 0:65004368569c | 190 | failed++; |
AxedaCorp | 0:65004368569c | 191 | } |
AxedaCorp | 0:65004368569c | 192 | |
AxedaCorp | 0:65004368569c | 193 | //Test attach with less than below level |
AxedaCorp | 0:65004368569c | 194 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 195 | if (capacity != 4) { |
AxedaCorp | 0:65004368569c | 196 | printf("Failed: attach() - less_equal/below%d\r\n", capacity); |
AxedaCorp | 0:65004368569c | 197 | failed++; |
AxedaCorp | 0:65004368569c | 198 | } |
AxedaCorp | 0:65004368569c | 199 | |
AxedaCorp | 0:65004368569c | 200 | //Test attach with less equal than above level |
AxedaCorp | 0:65004368569c | 201 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 202 | buffer->write("Test", 4); |
AxedaCorp | 0:65004368569c | 203 | capacity = 0; |
AxedaCorp | 0:65004368569c | 204 | buffer->attach(&callback, 2, Vars::LESS_EQUAL); |
AxedaCorp | 0:65004368569c | 205 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 206 | if (capacity != 0) { |
AxedaCorp | 0:65004368569c | 207 | printf("Failed: attach() - less equal/above\r\n"); |
AxedaCorp | 0:65004368569c | 208 | failed++; |
AxedaCorp | 0:65004368569c | 209 | } |
AxedaCorp | 0:65004368569c | 210 | |
AxedaCorp | 0:65004368569c | 211 | //Test attach with less equal than below level |
AxedaCorp | 0:65004368569c | 212 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 213 | if (capacity != 3) { |
AxedaCorp | 0:65004368569c | 214 | printf("Failed: attach() - less equal/below%d\r\n", capacity); |
AxedaCorp | 0:65004368569c | 215 | failed++; |
AxedaCorp | 0:65004368569c | 216 | } |
AxedaCorp | 0:65004368569c | 217 | |
AxedaCorp | 0:65004368569c | 218 | //Test attach with less equal than above level |
AxedaCorp | 0:65004368569c | 219 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 220 | buffer->write("Test", 4); |
AxedaCorp | 0:65004368569c | 221 | capacity = 0; |
AxedaCorp | 0:65004368569c | 222 | buffer->attach(&callback, 2, Vars::EQUAL); |
AxedaCorp | 0:65004368569c | 223 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 224 | if (capacity != 0) { |
AxedaCorp | 0:65004368569c | 225 | printf("Failed: attach() - equal/above\r\n"); |
AxedaCorp | 0:65004368569c | 226 | failed++; |
AxedaCorp | 0:65004368569c | 227 | } |
AxedaCorp | 0:65004368569c | 228 | |
AxedaCorp | 0:65004368569c | 229 | //Test attach with less equal than below level |
AxedaCorp | 0:65004368569c | 230 | buffer->read(byte); |
AxedaCorp | 0:65004368569c | 231 | if (capacity != 3) { |
AxedaCorp | 0:65004368569c | 232 | printf("Failed: attach() - equal/below%d\r\n", capacity); |
AxedaCorp | 0:65004368569c | 233 | failed++; |
AxedaCorp | 0:65004368569c | 234 | } |
AxedaCorp | 0:65004368569c | 235 | |
AxedaCorp | 0:65004368569c | 236 | //Test Ins and Outs |
AxedaCorp | 0:65004368569c | 237 | { |
AxedaCorp | 0:65004368569c | 238 | const char inData[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; |
AxedaCorp | 0:65004368569c | 239 | const int size = sizeof(inData); |
AxedaCorp | 0:65004368569c | 240 | char outData[size]; |
AxedaCorp | 0:65004368569c | 241 | |
AxedaCorp | 0:65004368569c | 242 | int bytesWritten = 0; |
AxedaCorp | 0:65004368569c | 243 | int bytesRead = 0; |
AxedaCorp | 0:65004368569c | 244 | buffer->clear(); |
AxedaCorp | 0:65004368569c | 245 | |
AxedaCorp | 0:65004368569c | 246 | Timer tmr; |
AxedaCorp | 0:65004368569c | 247 | tmr.start(); |
AxedaCorp | 0:65004368569c | 248 | do { |
AxedaCorp | 0:65004368569c | 249 | int remaining = size - bytesRead; |
AxedaCorp | 0:65004368569c | 250 | int readable = buffer->size(); |
AxedaCorp | 0:65004368569c | 251 | if(remaining) { |
AxedaCorp | 0:65004368569c | 252 | if(readable) { |
AxedaCorp | 0:65004368569c | 253 | //printf("READABLE [%d]\r\n", readable); |
AxedaCorp | 0:65004368569c | 254 | int received = buffer->read(&outData[bytesRead], remaining); |
AxedaCorp | 0:65004368569c | 255 | bytesRead += received; |
AxedaCorp | 0:65004368569c | 256 | //printf("READ [%d] TOTAL[%d] REMAINING[%d]\r\n", received, bytesRead, size - bytesRead); |
AxedaCorp | 0:65004368569c | 257 | } |
AxedaCorp | 0:65004368569c | 258 | } |
AxedaCorp | 0:65004368569c | 259 | |
AxedaCorp | 0:65004368569c | 260 | remaining = size - bytesWritten; |
AxedaCorp | 0:65004368569c | 261 | int writeable = buffer->remaining(); |
AxedaCorp | 0:65004368569c | 262 | if(remaining) { |
AxedaCorp | 0:65004368569c | 263 | if(writeable) { |
AxedaCorp | 0:65004368569c | 264 | //printf("WRITEABLE [%d]\r\n", writeable); |
AxedaCorp | 0:65004368569c | 265 | int written = buffer->write(&inData[bytesWritten], remaining); |
AxedaCorp | 0:65004368569c | 266 | bytesWritten += written; |
AxedaCorp | 0:65004368569c | 267 | remaining = size - bytesWritten; |
AxedaCorp | 0:65004368569c | 268 | //printf("WROTE [%d] TOTAL[%d] REMAINING[%d]\r\n", written, bytesWritten, size - bytesWritten); |
AxedaCorp | 0:65004368569c | 269 | } |
AxedaCorp | 0:65004368569c | 270 | } |
AxedaCorp | 0:65004368569c | 271 | |
AxedaCorp | 0:65004368569c | 272 | } while (tmr.read_ms() <= 5000 && bytesRead < size); |
AxedaCorp | 0:65004368569c | 273 | |
AxedaCorp | 0:65004368569c | 274 | printf("INPUT [%d]: [%s]\r\n", size, inData); |
AxedaCorp | 0:65004368569c | 275 | printf("OUTPUT [%d]: [%s]\r\n", bytesRead, outData); |
AxedaCorp | 0:65004368569c | 276 | for(int i = 0; i < size - 1; i++) { |
AxedaCorp | 0:65004368569c | 277 | if(inData[i] != outData[i]) { |
AxedaCorp | 0:65004368569c | 278 | printf("Failed: Buffers do not match at index %d\r\n", i); |
AxedaCorp | 0:65004368569c | 279 | failed++; |
AxedaCorp | 0:65004368569c | 280 | break; |
AxedaCorp | 0:65004368569c | 281 | } |
AxedaCorp | 0:65004368569c | 282 | } |
AxedaCorp | 0:65004368569c | 283 | } |
AxedaCorp | 0:65004368569c | 284 | |
AxedaCorp | 0:65004368569c | 285 | printf("Finished Testing: MTSCircularBuffer\r\n"); |
AxedaCorp | 0:65004368569c | 286 | return failed; |
AxedaCorp | 0:65004368569c | 287 | } |
AxedaCorp | 0:65004368569c | 288 | |
AxedaCorp | 0:65004368569c | 289 | #endif /* TESTMTSCIRCULARBUFFER_H */ |