pelib  2.0.0
src/test_array.c
Go to the documentation of this file.
00001 /*
00002  * test_array.c
00003  *
00004  *  Created on: 13 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 
00027 #include <pelib/unit.h>
00028 #include <pelib/integer.h>
00029 
00030 #define LENGTH 8
00031 #define FILENAME_BINARY   "array_binary.bin"
00032 #define VALUE   10
00033 #define SKIP    6
00034 
00035 array_t(int) *array;
00036 int value;
00037 
00038 void
00039 init()
00040 {
00041   unsigned int i;
00042   array = pelib_alloc_collection(array_t(int))(LENGTH);
00043   value = VALUE;
00044 
00045   for (i = 0; i < LENGTH; i++)
00046     {
00047       pelib_array_append(int)(array, value);
00048     }
00049 }
00050 
00051 void
00052 cleanup()
00053 {
00054   pelib_free(array_t(int))(array);
00055 }
00056 
00057 void setup() {};
00058 void teardown() {};
00059 
00060 static void
00061 test_compare()
00062 {
00063   unsigned int i;
00064   array_t(int)* another_array;
00065   another_array = pelib_alloc_collection(array_t(int))(LENGTH);
00066 
00067   for (i = 0; i < LENGTH; i++)
00068     {
00069       pelib_array_append(int)(another_array, value);
00070     }
00071 
00072   assert(pelib_array_compare(int)(array, another_array) == 0);
00073 }
00074 
00075 static void
00076 test_store_load_binary()
00077 {
00078   array_t(int)* another_array;
00079   pelib_array_storefilenamebinary(int)(array, FILENAME_BINARY);
00080   another_array = pelib_array_loadfilenamebinary(int)(FILENAME_BINARY);
00081 
00082   assert(pelib_array_compare(int)(array, another_array) == 0);
00083 
00084   pelib_free(array_t(int))(another_array);
00085 }
00086 
00087 static void
00088 test_store_load_binary_skip()
00089 {
00090   array_t(int)* another_array;
00091   pelib_array_storefilenamebinary(int)(array, FILENAME_BINARY);
00092   another_array = pelib_array_loadfilenamewindowbinary(int)(FILENAME_BINARY, SKIP,
00093       0);
00094 
00095   assert(pelib_array_length(int)(another_array) == LENGTH - SKIP);
00096   assert(pelib_array_capacity(int)(another_array) == LENGTH - SKIP);
00097 
00098   pelib_free(array_t(int))(another_array);
00099 }
00100 
00101 static void
00102 test_store_load_binary_skip_too_much()
00103 {
00104   array_t(int)* another_array;
00105   pelib_array_storefilenamebinary(int)(array, FILENAME_BINARY);
00106   another_array = pelib_array_loadfilenamewindowbinary(int)(FILENAME_BINARY, LENGTH
00107       + SKIP, 0);
00108 
00109   assert(another_array == NULL);
00110 }
00111 
00112 static void
00113 test_store_load_binary_too_much()
00114 {
00115   array_t(int)* another_array;
00116   pelib_array_storefilenamebinary(int)(array, FILENAME_BINARY);
00117   another_array = pelib_array_loadfilenamewindowbinary(int)(FILENAME_BINARY, LENGTH
00118       / 2, LENGTH);
00119 
00120   assert(pelib_array_length(int)(another_array) == LENGTH / 2);
00121   assert(pelib_array_capacity(int)(another_array) == LENGTH / 2);
00122 
00123   pelib_free(array_t(int))(another_array);
00124 }
00125 
00126 static void
00127 test_store_load_binary_all_but_skipped_too_much()
00128 {
00129   array_t(int)* another_array;
00130   pelib_array_storefilenamebinary(int)(array, FILENAME_BINARY);
00131   another_array = pelib_array_loadfilenamewindowbinary(int)(FILENAME_BINARY, LENGTH
00132       / 2, 0);
00133 
00134   assert(pelib_array_length(int)(another_array) == LENGTH / 2);
00135   assert(pelib_array_capacity(int)(another_array) == LENGTH / 2);
00136 
00137   pelib_free(array_t(int))(another_array);
00138 }
00139 
00140 static void
00141 test_preload()
00142 {
00143   array_t(int)* another_array;
00144   another_array = pelib_array_preloadfilenamebinary(int)(FILENAME_BINARY);
00145 
00146   assert(pelib_array_length(int)(another_array) == LENGTH);
00147   assert(pelib_array_capacity(int)(another_array) == 0);
00148 
00149   pelib_free(array_t(int))(another_array);
00150 }
00151 
00152 void
00153 run()
00154 {
00155   test(test_compare);
00156   test(test_store_load_binary);
00157   test(test_store_load_binary_skip);
00158   test(test_store_load_binary_skip_too_much);
00159   test(test_store_load_binary_too_much);
00160   test(test_store_load_binary_all_but_skipped_too_much);
00161   test(test_preload);
00162 }