pelib
2.0.0
|
00001 /* 00002 * fifo_test.c 00003 * 00004 * Created on: 26 Jan 2012 00005 * Copyright 2012 Nicolas Melot 00006 * 00007 * This file is part of pelib. 00008 * 00009 * pelib is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * pelib is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with pelib. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 00024 #include <stdlib.h> 00025 #include <stdio.h> 00026 #include <string.h> 00027 00028 #include <pelib/unit.h> 00029 #include <pelib/integer.h> 00030 00031 #define CAPACITY 10 00032 #define VALUE 10 00033 #define TO_TAIL 7 00034 #define FROM_HEAD 4 00035 00036 cfifo_t(int) *cfifo; 00037 int value; 00038 int reverse[CAPACITY], normal[CAPACITY], different[CAPACITY]; 00039 00040 void 00041 init() 00042 { 00043 unsigned int i; 00044 00045 cfifo = pelib_alloc_collection(cfifo_t(int))(CAPACITY); 00046 pelib_init(cfifo_t(int))(cfifo); 00047 value = VALUE; 00048 00049 for (i = 0; i < CAPACITY; i++) 00050 { 00051 pelib_cfifo_push(int)(cfifo, i + VALUE); 00052 normal[i] = VALUE + TO_TAIL + i; 00053 reverse[i] = VALUE + CAPACITY + FROM_HEAD + i; 00054 different[i] = -1; 00055 } 00056 } 00057 00058 void 00059 cleanup() 00060 { 00061 pelib_free(cfifo_t(int))(cfifo); 00062 } 00063 00064 void setup() {} 00065 void teardown() {} 00066 00067 static void 00068 cfifo_from_array() 00069 { 00070 int i; 00071 array_t(int) *array; 00072 cfifo_t(int) *fifo; 00073 array = pelib_alloc_collection(array_t(int))(10); 00074 for (i = 0; i < 10; i++) 00075 { 00076 pelib_array_append(int)(array, i); 00077 } 00078 00079 fifo = pelib_cfifo_from_array(int)(array); 00080 00081 assert(fifo != NULL); 00082 assert(fifo->write == fifo->read); 00083 00084 for (i = 0; i < 10; i++) 00085 { 00086 assert(pelib_cfifo_pop(int)(fifo) == i); 00087 } 00088 } 00089 00090 static void 00091 array_from_cfifo() 00092 { 00093 int i; 00094 array_t(int) *array; 00095 cfifo_t(int) *fifo; 00096 00097 fifo = pelib_alloc_collection(cfifo_t(int))(10); 00098 pelib_init(cfifo_t(int))(fifo); 00099 for (i = 0; i < 10; i++) 00100 { 00101 pelib_cfifo_push(int)(fifo, i); 00102 } 00103 00104 array = pelib_array_from_cfifo(int)(fifo); 00105 00106 assert(array != NULL); 00107 assert(pelib_array_length(int)(array) == pelib_array_capacity(int)(array)); 00108 assert(pelib_array_length(int)(array) == 10); 00109 00110 for (i = 0; i < 10; i++) 00111 { 00112 assert(pelib_array_read(int)(array, i) == i); 00113 } 00114 } 00115 00116 void 00117 run() 00118 { 00119 test(cfifo_from_array); 00120 test(array_from_cfifo); 00121 }