pelib  2.0.0
src/test_map.c
Go to the documentation of this file.
00001 /*
00002  * test_map.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 #include <string.h>
00027 #include <math.h>
00028 
00029 #include <pelib/unit.h>
00030 #include <pelib/integer.h>
00031 #include <pelib/string_integer.h>
00032 
00033 #define LENGTH  8
00034 #define FILENAME_BINARY "map_binary.bin"
00035 #define VALUE   10
00036 #define SKIP    6
00037 
00038 #if 10
00039 #define debug(var) printf("[%s:%s:%d] %s = \"%s\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00040 #define debug_addr(var) printf("[%s:%s:%d] %s = \"%p\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00041 #define debug_int(var) printf("[%s:%s:%d] %s = \"%d\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00042 #define debug_size_t(var) printf("[%s:%s:%d] %s = \"%zu\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00043 #else
00044 #define debug(var)
00045 #define debug_addr(var)
00046 #define debug_int(var)
00047 #define debug_size_t(var)
00048 #endif
00049 
00050 map_t(string, int) map;
00051 int value;
00052 
00053 int insertion[] = {3, 6, 7, 5, 3, 5, 6, 2};
00054 
00055 void
00056 init()
00057 {
00058         unsigned int i;
00059         pelib_init(map_t(string, int))(&map);
00060         value = VALUE;
00061 
00062         for (i = 0; i < LENGTH; i++)
00063         {
00064                 string key;
00065                 pelib_alloc_buffer(string)(&key, (size_t)(log10(LENGTH) + 0.5 + 1));
00066                 sprintf(key, "%i", insertion[i]);
00067                 pair_t(string, int) pair;
00068                 pelib_alloc_buffer(string)(&pair.key, (strlen(key) + 1) * sizeof(char));
00069                 pelib_copy(string)(key, &pair.key);
00070                 pair.value = i;
00071                 pelib_map_insert(string, int)(&map, pair);
00072         }
00073 }
00074 
00075 void
00076 cleanup()
00077 {
00078         pelib_destroy(map_t(string, int))(map);
00079 }
00080 
00081 void setup() {};
00082 void teardown() {};
00083 
00084 static void
00085 test_string()
00086 {
00087         char *ref = "[(\"2\",7):(\"3\",0):(\"5\",3):(\"6\",1):(\"7\",2)]";
00088         char *str = pelib_string(map_t(string, int))(map);
00089         assert_equals_str(str, ref);
00090         free(str);
00091 }
00092 
00093 static void
00094 test_find()
00095 {
00096         int res = pelib_map_read(string, int)(pelib_map_find(string, int)(&map, "5")).value;
00097         assert_equals_int(res, 3);
00098 }
00099 
00100 void
00101 run()
00102 {
00103         test(test_string);
00104         test(test_find);
00105 }
00106