A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_MTS_Circular_Buffer.h Source File

test_MTS_Circular_Buffer.h

00001 /* Universal Socket Modem Interface Library
00002 * Copyright (c) 2013 Multi-Tech Systems
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #ifndef TESTMTSCIRCULARBUFFER_H
00018 #define TESTMTSCIRCULARBUFFER_H
00019 
00020 #include "MTSCircularBuffer.h"
00021 #include "Vars.h"
00022 
00023 /* unit tests for the circular buffer class */
00024 
00025 using namespace mts;
00026 
00027 int capacity = 0;
00028 MTSCircularBuffer* buffer = new MTSCircularBuffer(5);
00029 
00030 void callback()
00031 {
00032     capacity = buffer->remaining();
00033 }
00034 
00035 int testMTSCircularBuffer()
00036 {
00037     printf("Testing: MTSCircularBuffer\r\n");
00038     int failed = 0;
00039     char byte;
00040 
00041 
00042     //Test getSize method
00043     if (buffer->capacity() != 5) {
00044         printf("Failed: capacity()\r\n");
00045         failed++;
00046     }
00047 
00048     //Test clear function
00049     buffer->write("AT", 2);
00050     buffer->clear();
00051     if (buffer->size() != 0) {
00052         printf("Failed: clear()\r\n");
00053         failed++;
00054     }
00055 
00056     /* The next set of test all rely on an empty buffer!!! */
00057 
00058     //Test isEmpty method - empty buffer
00059     if (buffer->isEmpty() != true) {
00060         printf("Failed: isEmpty() - empty\r\n");
00061         failed++;
00062     }
00063 
00064     //Test isFull method - empty buffer
00065     if (buffer->isFull() == true) {
00066         printf("Failed: isFull() - empty\r\n");
00067         failed++;
00068     }
00069 
00070     //Test capacity method - empty buffer
00071     if (buffer->remaining() != 5) {
00072         printf("Failed: remaining() - empty\r\n");
00073         failed++;
00074     }
00075 
00076     //Test available method - empty buffer
00077     if (buffer->size() != 0) {
00078         printf("Failed: size() - empty\r\n");
00079         failed++;
00080     }
00081 
00082     /* The next set of tests all rely on a full buffer */
00083 
00084     //Test bulk write method
00085     int tmp = buffer->write("Test", 5);
00086     if (tmp != 5) {
00087         printf("Failed: bulk write()\r\n");
00088         failed++;
00089     }
00090 
00091     //Test isEmpty method - full buffer
00092     if (buffer->isEmpty() == true) {
00093         printf("Failed: isEmpty() - full\r\n");
00094         failed++;
00095     }
00096 
00097     //Test isFull method - full buffer
00098     if (buffer->isFull() == false) {
00099         printf("Failed: isFull() - full\r\n");
00100         failed++;
00101     }
00102 
00103     //Test capacity method - full buffer
00104     if (buffer->remaining() != 0) {
00105         printf("Failed: remaining() - full\r\n");
00106         failed++;
00107     }
00108 
00109     //Test available method - full buffer
00110     if (buffer->size() != 5) {
00111         printf("Failed: size() - full\r\n");
00112         failed++;
00113     }
00114 
00115     //Test single overwrite method
00116     if (buffer->write('A') != 0) {
00117         printf("Failed: write() - overwrite\r\n");
00118         failed++;
00119     }
00120 
00121     //Test bulk overwrite method
00122     if (buffer->write("Test", 5) != 0) {
00123         printf("Failed: bulk write() - overflow\r\n");
00124         failed++;
00125     }
00126 
00127     //Test single read method
00128     if ((buffer->read(byte) < 1 && byte != 'T') || buffer->remaining() != 1) {
00129         printf("Failed: single read()\r\n");
00130         failed++;
00131     }
00132 
00133     //Test bulk read method
00134     char data[5];
00135     if (buffer->read(data, 4) != 4 || data[0] != 'e' || data[1] != 's' || data[2] != 't' || data[3] != '\0') {
00136         printf("Failed: bulk read()\r\n");
00137         failed++;
00138     }
00139 
00140     //Test wrap write/read methods
00141     tmp = buffer->write("AT", 3);
00142     if (tmp != 3 || (buffer->read(byte) < 1 && byte != 'A') || (buffer->read(byte) < 1 && byte != 'T')) {
00143         printf("Failed: wrap write()/read()\r\n");
00144         failed++;
00145     }
00146     buffer->clear();
00147 
00148     /* The next set of test are focused all on the attach methods */
00149 
00150     //Test attach with greater than below level
00151     buffer->attach(&callback, 3, Vars::GREATER);
00152     buffer->write("ABC", 3);
00153     if (capacity != 0) {
00154         printf("Failed: attach() - greater/below\r\n");
00155         failed++;
00156     }
00157 
00158     //Test attach with greater than above level
00159     buffer->write('T');
00160     if (capacity != 1) {
00161         printf("Failed: attach() - greater/above\r\n");
00162         failed++;
00163     }
00164     buffer->clear();
00165     capacity = 0;
00166 
00167     //Test attach with greater equal than below level
00168     buffer->attach(&callback, 3, Vars::GREATER_EQUAL);
00169     buffer->write("AB", 2);
00170     if (capacity != 0) {
00171         printf("Failed: attach() - greater equal/below\r\n");
00172         failed++;
00173     }
00174 
00175     //Test attach with greater equal than above level
00176     buffer->write('T');
00177     if (capacity != 2) {
00178         printf("Failed: attach() - greater equal/above\r\n");
00179         failed++;
00180     }
00181 
00182     //Test attach with less than above level
00183     buffer->clear();
00184     buffer->write("ABC", 3);
00185     capacity = 0;
00186     buffer->attach(&callback, 2, Vars::LESS);
00187     buffer->read(byte);
00188     if (capacity != 0) {
00189         printf("Failed: attach() - less_equal/above\r\n");
00190         failed++;
00191     }
00192 
00193     //Test attach with less than below level
00194     buffer->read(byte);
00195     if (capacity != 4) {
00196         printf("Failed: attach() - less_equal/below%d\r\n", capacity);
00197         failed++;
00198     }
00199 
00200     //Test attach with less equal than above level
00201     buffer->clear();
00202     buffer->write("Test", 4);
00203     capacity = 0;
00204     buffer->attach(&callback, 2, Vars::LESS_EQUAL);
00205     buffer->read(byte);
00206     if (capacity != 0) {
00207         printf("Failed: attach() - less equal/above\r\n");
00208         failed++;
00209     }
00210 
00211     //Test attach with less equal than below level
00212     buffer->read(byte);
00213     if (capacity != 3) {
00214         printf("Failed: attach() - less equal/below%d\r\n", capacity);
00215         failed++;
00216     }
00217 
00218     //Test attach with less equal than above level
00219     buffer->clear();
00220     buffer->write("Test", 4);
00221     capacity = 0;
00222     buffer->attach(&callback, 2, Vars::EQUAL);
00223     buffer->read(byte);
00224     if (capacity != 0) {
00225         printf("Failed: attach() - equal/above\r\n");
00226         failed++;
00227     }
00228 
00229     //Test attach with less equal than below level
00230     buffer->read(byte);
00231     if (capacity != 3) {
00232         printf("Failed: attach() - equal/below%d\r\n", capacity);
00233         failed++;
00234     }
00235 
00236     //Test Ins and Outs
00237     {
00238         const char inData[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
00239         const int size = sizeof(inData); 
00240         char outData[size];
00241         
00242         int bytesWritten = 0;
00243         int bytesRead = 0;
00244         buffer->clear();
00245         
00246         Timer tmr;
00247         tmr.start();
00248         do {       
00249             int remaining = size - bytesRead;
00250             int readable = buffer->size();
00251             if(remaining) {
00252                 if(readable) {
00253                     //printf("READABLE [%d]\r\n", readable);
00254                     int received = buffer->read(&outData[bytesRead], remaining);
00255                     bytesRead += received;
00256                     //printf("READ [%d]  TOTAL[%d]  REMAINING[%d]\r\n", received, bytesRead, size - bytesRead);
00257                 }
00258             }
00259             
00260             remaining = size - bytesWritten;
00261             int writeable = buffer->remaining();
00262             if(remaining) {
00263                 if(writeable) {
00264                     //printf("WRITEABLE [%d]\r\n", writeable);
00265                     int written = buffer->write(&inData[bytesWritten], remaining);   
00266                     bytesWritten += written;
00267                     remaining = size - bytesWritten;
00268                     //printf("WROTE [%d]  TOTAL[%d]  REMAINING[%d]\r\n", written, bytesWritten, size - bytesWritten);
00269                 }
00270             }
00271             
00272         } while (tmr.read_ms() <= 5000 && bytesRead < size);
00273         
00274         printf("INPUT  [%d]: [%s]\r\n", size, inData);
00275         printf("OUTPUT [%d]: [%s]\r\n", bytesRead, outData);
00276         for(int i = 0; i < size - 1; i++) {
00277             if(inData[i] != outData[i]) {
00278                 printf("Failed: Buffers do not match at index %d\r\n", i);
00279                 failed++;
00280                 break;   
00281             }   
00282         }
00283     }
00284 
00285     printf("Finished Testing: MTSCircularBuffer\r\n");
00286     return failed;
00287 }
00288 
00289 #endif /* TESTMTSCIRCULARBUFFER_H */