test/map_tests.c

Thu, 04 Oct 2012 18:46:57 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Oct 2012 18:46:57 +0200
changeset 44
46356d74e873
parent 43
02f38adea013
child 46
48ca036d7d9c
permissions
-rw-r--r--

added map clone

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@20 4
olaf@20 5 #include "map_tests.h"
olaf@20 6
olaf@44 7 #ifndef _WIN32
olaf@44 8 #include <unistd.h>
olaf@44 9 #endif /* not _WIN32 */
olaf@44 10
universe@33 11 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
olaf@20 12 UcxMap *map = ucx_map_new(16);
universe@33 13 UCX_TEST_BEGIN
universe@40 14 UCX_TEST_ASSERT(map->size == 16, "wrong size");
universe@40 15 UCX_TEST_ASSERT(map->map != NULL, "failed");
universe@29 16
universe@33 17 UCX_TEST_END
universe@29 18 ucx_map_free(map);
universe@29 19 }
olaf@20 20
universe@33 21 UCX_TEST_IMPLEMENT(test_ucx_key) {
universe@29 22 UcxKey key = ucx_key("This is a text.", 15);
universe@33 23 UCX_TEST_BEGIN
universe@40 24 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
universe@40 25 UCX_TEST_ASSERT(key.len == 15, "failed");
universe@40 26 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
universe@29 27
universe@29 28 UCX_TEST_END
universe@29 29 }
olaf@20 30
universe@33 31 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
universe@29 32
universe@29 33 UcxMap *map = ucx_map_new(4);
universe@29 34
universe@29 35 int td[5];
universe@29 36 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
olaf@20 37
universe@33 38 UCX_TEST_BEGIN
universe@29 39 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
universe@29 40 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
universe@29 41 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
universe@29 42 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
universe@29 43 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
universe@29 44
universe@40 45 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
universe@40 46 UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
universe@40 47 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
universe@40 48 UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
universe@29 49 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
universe@29 50 "failed Key4")
universe@29 51 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
universe@29 52 "slot 0 not terminated")
universe@29 53
universe@40 54 UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
universe@29 55
universe@40 56 UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
universe@40 57 UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
universe@29 58
universe@40 59 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
universe@29 60
universe@29 61 ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
universe@29 62
universe@40 63 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
universe@29 64 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
universe@29 65 "overwrite failed")
universe@29 66 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
universe@29 67 "overwrite failed")
universe@40 68 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
universe@29 69
universe@33 70 UCX_TEST_END
universe@29 71 ucx_map_free(map);
universe@33 72 }
universe@33 73
universe@33 74 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
universe@34 75 UcxMap *map = ucx_map_new(4);
universe@34 76
universe@34 77 int td[5];
universe@34 78 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
universe@34 79
universe@34 80 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
universe@34 81 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
universe@34 82 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
universe@34 83 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
universe@34 84 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
universe@33 85 UCX_TEST_BEGIN
universe@34 86
universe@34 87 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
universe@34 88 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
universe@34 89 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
universe@34 90 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
universe@34 91 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
universe@40 92 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
universe@40 93 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
universe@40 94 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
universe@40 95 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
universe@40 96 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
universe@34 97
universe@29 98 UCX_TEST_END
universe@34 99 ucx_map_free(map);
olaf@20 100 }
universe@29 101
universe@33 102 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
universe@33 103 UcxMap *map = (UcxMap*) mapptr;
olaf@31 104 int v1 = 10;
olaf@31 105 int v2 = 15;
olaf@31 106 int v3 = 7;
olaf@31 107 int v4 = 9;
universe@32 108
olaf@31 109 ucx_map_cstr_put(map, "v1", &v1);
olaf@31 110 ucx_map_cstr_put(map, "v2", &v2);
olaf@31 111 ucx_map_cstr_put(map, "v3", &v3);
olaf@31 112 ucx_map_cstr_put(map, "v4", &v4);
universe@32 113
olaf@31 114 UcxMapIterator i = ucx_map_iterator(map);
olaf@31 115 int check = 0;
olaf@31 116 int hit = 0;
universe@32 117
universe@41 118 int* v;
universe@41 119 UCX_MAP_FOREACH(v, i) {
olaf@31 120 check += *v;
olaf@31 121 hit++;
olaf@31 122 }
universe@32 123
olaf@31 124 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
olaf@31 125 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
universe@33 126 }
universe@32 127
universe@33 128 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
universe@33 129 UcxMap *map = ucx_map_new(16);
universe@33 130 UCX_TEST_BEGIN
universe@33 131 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 132 UCX_TEST_END
olaf@31 133 ucx_map_free(map);
universe@33 134 }
universe@33 135
universe@33 136 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
universe@33 137 UcxMap *map = ucx_map_new(1);
universe@33 138 UCX_TEST_BEGIN
universe@33 139 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 140 UCX_TEST_END
olaf@31 141 ucx_map_free(map);
olaf@31 142 }
universe@42 143
universe@42 144 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
universe@42 145 UcxMap *map = ucx_map_new(4);
universe@42 146
universe@42 147 ucx_map_cstr_put(map, "test", "test");
universe@42 148 ucx_map_cstr_put(map, "key", "value");
universe@42 149 ucx_map_cstr_put(map, "other.very.long.key", "value");
universe@42 150 ucx_map_cstr_put(map, "testkey", "testvalue");
universe@42 151 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
universe@42 152 "to test if the buffer extension works as designed");
universe@42 153
universe@42 154 FILE *f = fopen("test_ucx_map_store", "w");
universe@42 155 int r;
universe@42 156
universe@43 157 fwrite(" # comment test\n", 1, 16, f);
universe@42 158 r = ucx_map_store(map, f);
universe@43 159 fwrite("!discard this", 1, 13, f);
universe@42 160
universe@42 161 fclose(f);
universe@42 162 ucx_map_free(map);
universe@42 163 map = ucx_map_new(1);
universe@42 164 f = fopen("test_ucx_map_store", "r");
universe@42 165 r += ucx_map_load(map, f);
universe@42 166
universe@42 167 UCX_TEST_BEGIN
universe@42 168 char *value;
universe@42 169 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@42 170
universe@42 171 value = ucx_map_cstr_get(map, "test");
universe@43 172 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
universe@42 173 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
universe@42 174
universe@42 175 value = ucx_map_cstr_get(map, "key");
universe@43 176 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
universe@42 177 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
universe@42 178
universe@42 179 value = ucx_map_cstr_get(map, "other.very.long.key");
universe@43 180 UCX_TEST_ASSERT(value != NULL,
universe@43 181 "value not found for key: other.very.long.key");
universe@42 182 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
universe@42 183 "value error for key: other.very.long.key");
universe@42 184
universe@42 185 value = ucx_map_cstr_get(map, "testkey");
universe@43 186 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
universe@42 187 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
universe@42 188 "value error for key: testkey");
universe@42 189
universe@42 190 value = ucx_map_cstr_get(map, "simple");
universe@43 191 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
universe@42 192 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
universe@42 193 "to test if the buffer extension works as designed") == 0,
universe@42 194 "value error for key: simple");
universe@42 195
universe@42 196 UCX_TEST_END
universe@42 197 fclose(f);
universe@42 198
universe@42 199 unlink("test_ucx_map_store");
universe@42 200 }
olaf@44 201
olaf@44 202 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
olaf@44 203 UcxMap *map = ucx_map_new(4);
olaf@44 204
olaf@44 205 ucx_map_cstr_put(map, "key1", "value1");
olaf@44 206 ucx_map_cstr_put(map, "key2", "value2");
olaf@44 207 ucx_map_cstr_put(map, "key3", "value3");
olaf@44 208
olaf@44 209 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
olaf@44 210
olaf@44 211 char *v1 = ucx_map_cstr_get(map, "key1");
olaf@44 212 char *v2 = ucx_map_cstr_get(map, "key2");
olaf@44 213 char *v3 = ucx_map_cstr_get(map, "key3");
olaf@44 214
olaf@44 215 UCX_TEST_BEGIN
olaf@44 216
olaf@44 217 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
olaf@44 218 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
olaf@44 219 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
olaf@44 220
olaf@44 221 char *c1 = ucx_map_cstr_get(clone, "key1");
olaf@44 222 char *c2 = ucx_map_cstr_get(clone, "key2");
olaf@44 223 char *c3 = ucx_map_cstr_get(clone, "key3");
olaf@44 224
olaf@44 225 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
olaf@44 226 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
olaf@44 227 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
olaf@44 228
olaf@44 229 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
olaf@44 230 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
olaf@44 231 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
olaf@44 232
olaf@44 233 UCX_TEST_END
olaf@44 234
olaf@44 235 ucx_map_free(map);
olaf@44 236 ucx_map_free(clone);
olaf@44 237 }

mercurial