Mon, 15 Jul 2013 14:25:50 +0200
new map foreach macro
olaf@20 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@20 | 3 | * |
universe@103 | 4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@20 | 27 | */ |
olaf@20 | 28 | |
olaf@20 | 29 | #include "map_tests.h" |
olaf@20 | 30 | |
universe@33 | 31 | UCX_TEST_IMPLEMENT(test_ucx_map_new) { |
olaf@20 | 32 | UcxMap *map = ucx_map_new(16); |
universe@33 | 33 | UCX_TEST_BEGIN |
universe@40 | 34 | UCX_TEST_ASSERT(map->size == 16, "wrong size"); |
universe@40 | 35 | UCX_TEST_ASSERT(map->map != NULL, "failed"); |
universe@29 | 36 | |
universe@33 | 37 | UCX_TEST_END |
universe@29 | 38 | ucx_map_free(map); |
universe@29 | 39 | } |
olaf@20 | 40 | |
universe@33 | 41 | UCX_TEST_IMPLEMENT(test_ucx_key) { |
universe@71 | 42 | UcxKey key = ucx_key((void*)"This is a text.", 15); |
universe@33 | 43 | UCX_TEST_BEGIN |
universe@69 | 44 | UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0, |
universe@69 | 45 | "failed"); |
universe@40 | 46 | UCX_TEST_ASSERT(key.len == 15, "failed"); |
universe@40 | 47 | UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed"); |
universe@29 | 48 | |
universe@29 | 49 | UCX_TEST_END |
universe@29 | 50 | } |
olaf@20 | 51 | |
universe@33 | 52 | UCX_TEST_IMPLEMENT(test_ucx_map_put) { |
universe@29 | 53 | |
universe@29 | 54 | UcxMap *map = ucx_map_new(4); |
universe@29 | 55 | |
universe@29 | 56 | int td[5]; |
universe@29 | 57 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
olaf@20 | 58 | |
universe@33 | 59 | UCX_TEST_BEGIN |
universe@80 | 60 | ucx_map_cstr_put(map, "Key2", &td[2]); /* 3.2 */ |
universe@80 | 61 | ucx_map_cstr_put(map, "Key0", &td[0]); /* 0.0 */ |
universe@80 | 62 | ucx_map_cstr_put(map, "Key1", &td[1]); /* 3.0 */ |
universe@80 | 63 | ucx_map_cstr_put(map, "KeY3", &td[3]); /* 3.1 */ |
universe@80 | 64 | ucx_map_cstr_put(map, "KEY4", &td[4]); /* 1.0 */ |
universe@29 | 65 | |
universe@40 | 66 | UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0"); |
universe@80 | 67 | UCX_TEST_ASSERT(*((int*)map->map[1]->data) == td[4], "failed KEY4"); |
universe@80 | 68 | UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1"); |
universe@80 | 69 | |
universe@80 | 70 | UCX_TEST_ASSERT(map->map[3]->next != NULL, "no list at slot 3"); |
universe@80 | 71 | UCX_TEST_ASSERT(map->map[3]->next->next != NULL, "list corrupt at slot 3"); |
universe@80 | 72 | UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[3], |
universe@80 | 73 | "failed KeY3") |
universe@80 | 74 | UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2], |
universe@80 | 75 | "failed KeY2"); |
universe@29 | 76 | |
universe@80 | 77 | UCX_TEST_ASSERT(map->map[0]->next == NULL, "slot 0 not terminated"); |
universe@80 | 78 | UCX_TEST_ASSERT(map->map[1]->next == NULL, "slot 1 not terminated"); |
universe@80 | 79 | UCX_TEST_ASSERT(map->map[2] == NULL, "slot 2 not empty"); |
universe@80 | 80 | UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, |
universe@80 | 81 | "slot 3 not terminated") |
universe@29 | 82 | |
universe@80 | 83 | ucx_map_cstr_put(map, "KeY3", &td[4]); /* replace 3.1 */ |
universe@29 | 84 | |
universe@80 | 85 | UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], |
universe@29 | 86 | "overwrite failed") |
universe@80 | 87 | UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[4], |
universe@80 | 88 | "overwrite failed"); |
universe@80 | 89 | UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2], |
universe@29 | 90 | "overwrite failed") |
universe@80 | 91 | UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, "overwrite failed"); |
universe@29 | 92 | |
universe@33 | 93 | UCX_TEST_END |
universe@29 | 94 | ucx_map_free(map); |
universe@33 | 95 | } |
universe@33 | 96 | |
universe@33 | 97 | UCX_TEST_IMPLEMENT(test_ucx_map_get) { |
universe@34 | 98 | UcxMap *map = ucx_map_new(4); |
universe@34 | 99 | |
universe@34 | 100 | int td[5]; |
universe@34 | 101 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
universe@34 | 102 | |
universe@80 | 103 | ucx_map_cstr_put(map, "Key2", &td[2]); |
universe@80 | 104 | ucx_map_cstr_put(map, "Key0", &td[0]); |
universe@80 | 105 | ucx_map_cstr_put(map, "Key1", &td[1]); |
universe@80 | 106 | ucx_map_cstr_put(map, "KeY3", &td[3]); |
universe@80 | 107 | ucx_map_cstr_put(map, "KEY4", &td[4]); |
universe@33 | 108 | UCX_TEST_BEGIN |
universe@34 | 109 | |
universe@34 | 110 | td[0] = *((int*)ucx_map_cstr_get(map, "Key0")); |
universe@34 | 111 | td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); |
universe@34 | 112 | td[2] = *((int*)ucx_map_cstr_get(map, "Key2")); |
universe@34 | 113 | td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); |
universe@34 | 114 | td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); |
universe@40 | 115 | UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); |
universe@40 | 116 | UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); |
universe@40 | 117 | UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); |
universe@40 | 118 | UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); |
universe@40 | 119 | UCX_TEST_ASSERT(td[4] == 80000, "failed key 4"); |
universe@34 | 120 | |
universe@53 | 121 | UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values"); |
universe@53 | 122 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed"); |
universe@53 | 123 | |
universe@53 | 124 | UCX_TEST_END |
universe@53 | 125 | ucx_map_free(map); |
universe@53 | 126 | } |
universe@53 | 127 | |
universe@53 | 128 | UCX_TEST_IMPLEMENT(test_ucx_map_remove) { |
universe@53 | 129 | UcxMap *map = ucx_map_new(4); |
universe@53 | 130 | |
universe@53 | 131 | int td[5]; |
universe@53 | 132 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
universe@53 | 133 | |
universe@53 | 134 | ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ |
universe@53 | 135 | ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ |
universe@53 | 136 | ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ |
universe@53 | 137 | ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ |
universe@53 | 138 | ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ |
universe@53 | 139 | UCX_TEST_BEGIN |
universe@53 | 140 | |
universe@53 | 141 | td[0] = *((int*)ucx_map_cstr_remove(map, "Key0")); |
universe@53 | 142 | td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); |
universe@53 | 143 | td[2] = *((int*)ucx_map_cstr_remove(map, "Key2")); |
universe@53 | 144 | td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); |
universe@53 | 145 | td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); |
universe@53 | 146 | UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); |
universe@53 | 147 | UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); |
universe@53 | 148 | UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); |
universe@53 | 149 | UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); |
universe@53 | 150 | UCX_TEST_ASSERT(td[4] == 80000, "failed key 4"); |
universe@53 | 151 | |
universe@53 | 152 | UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values"); |
universe@53 | 153 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed"); |
universe@53 | 154 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed"); |
universe@53 | 155 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed"); |
universe@53 | 156 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed"); |
universe@53 | 157 | UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed"); |
universe@53 | 158 | |
universe@53 | 159 | UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL, |
universe@53 | 160 | "subsequent remove call shall return NULL"); |
universe@53 | 161 | |
universe@29 | 162 | UCX_TEST_END |
universe@34 | 163 | ucx_map_free(map); |
olaf@20 | 164 | } |
universe@29 | 165 | |
universe@88 | 166 | UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) { |
olaf@31 | 167 | int v1 = 10; |
olaf@31 | 168 | int v2 = 15; |
olaf@31 | 169 | int v3 = 7; |
olaf@31 | 170 | int v4 = 9; |
universe@32 | 171 | |
olaf@31 | 172 | ucx_map_cstr_put(map, "v1", &v1); |
olaf@31 | 173 | ucx_map_cstr_put(map, "v2", &v2); |
olaf@31 | 174 | ucx_map_cstr_put(map, "v3", &v3); |
olaf@31 | 175 | ucx_map_cstr_put(map, "v4", &v4); |
universe@32 | 176 | |
olaf@31 | 177 | UcxMapIterator i = ucx_map_iterator(map); |
olaf@31 | 178 | int check = 0; |
olaf@31 | 179 | int hit = 0; |
universe@32 | 180 | |
universe@41 | 181 | int* v; |
olaf@111 | 182 | UCX_MAP_FOREACH(key, v, i) { |
olaf@31 | 183 | check += *v; |
olaf@31 | 184 | hit++; |
olaf@31 | 185 | } |
universe@32 | 186 | |
olaf@31 | 187 | UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); |
olaf@31 | 188 | UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); |
universe@33 | 189 | } |
universe@32 | 190 | |
universe@33 | 191 | UCX_TEST_IMPLEMENT(test_ucx_map_iterator) { |
universe@33 | 192 | UcxMap *map = ucx_map_new(16); |
universe@33 | 193 | UCX_TEST_BEGIN |
universe@33 | 194 | UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
universe@33 | 195 | UCX_TEST_END |
olaf@31 | 196 | ucx_map_free(map); |
universe@33 | 197 | } |
universe@33 | 198 | |
universe@33 | 199 | UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) { |
universe@33 | 200 | UcxMap *map = ucx_map_new(1); |
universe@33 | 201 | UCX_TEST_BEGIN |
universe@33 | 202 | UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
universe@33 | 203 | UCX_TEST_END |
olaf@31 | 204 | ucx_map_free(map); |
olaf@31 | 205 | } |
universe@42 | 206 | |
universe@48 | 207 | void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { |
universe@69 | 208 | const char *string = (const char*) value; |
universe@46 | 209 | size_t n = strlen(string); |
universe@69 | 210 | char *encoded = (char*) malloc(n+1); |
universe@95 | 211 | for (size_t i = 0 ; i < n ; i++) { |
universe@46 | 212 | encoded[i] = string[n-1-i]; |
universe@46 | 213 | } |
universe@46 | 214 | encoded[n] = 0; |
universe@48 | 215 | *size = n+1; |
universe@46 | 216 | return encoded; |
universe@46 | 217 | } |
universe@46 | 218 | |
universe@42 | 219 | UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { |
universe@42 | 220 | UcxMap *map = ucx_map_new(4); |
universe@42 | 221 | |
universe@71 | 222 | ucx_map_cstr_put(map, "test", (void*)"test"); |
universe@71 | 223 | ucx_map_cstr_put(map, "key", (void*)"value"); |
universe@71 | 224 | ucx_map_cstr_put(map, "other.very.long.key", (void*)"value"); |
universe@71 | 225 | ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); |
universe@71 | 226 | ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long " |
universe@71 | 227 | "value to test if the buffer extension works as designed"); |
universe@42 | 228 | |
universe@55 | 229 | UCX_TEST_BEGIN |
universe@55 | 230 | FILE *f = tmpfile(); |
universe@55 | 231 | UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted") |
universe@42 | 232 | int r; |
universe@42 | 233 | |
universe@43 | 234 | fwrite(" # comment test\n", 1, 16, f); |
universe@46 | 235 | r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); |
universe@43 | 236 | fwrite("!discard this", 1, 13, f); |
universe@55 | 237 | fflush(f); |
universe@42 | 238 | |
universe@42 | 239 | ucx_map_free(map); |
universe@42 | 240 | map = ucx_map_new(1); |
universe@55 | 241 | fseek(f, 0, SEEK_SET); |
universe@48 | 242 | UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; |
universe@48 | 243 | r += ucx_map_load_enc(map, f, allocator, |
universe@48 | 244 | test_ucx_map_store_load_encdec, NULL); |
universe@48 | 245 | fclose(f); |
universe@42 | 246 | |
universe@69 | 247 | const char *value; |
universe@42 | 248 | UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
universe@42 | 249 | |
universe@69 | 250 | value = (const char *) ucx_map_cstr_get(map, "test"); |
universe@43 | 251 | UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); |
universe@42 | 252 | UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); |
universe@42 | 253 | |
universe@69 | 254 | value = (const char *) ucx_map_cstr_get(map, "key"); |
universe@43 | 255 | UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); |
universe@42 | 256 | UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); |
universe@42 | 257 | |
universe@69 | 258 | value = (const char *) ucx_map_cstr_get(map, "other.very.long.key"); |
universe@43 | 259 | UCX_TEST_ASSERT(value != NULL, |
universe@43 | 260 | "value not found for key: other.very.long.key"); |
universe@42 | 261 | UCX_TEST_ASSERT(strcmp(value, "value") == 0, |
universe@42 | 262 | "value error for key: other.very.long.key"); |
universe@42 | 263 | |
universe@69 | 264 | value = (const char *) ucx_map_cstr_get(map, "testkey"); |
universe@43 | 265 | UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); |
universe@42 | 266 | UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, |
universe@42 | 267 | "value error for key: testkey"); |
universe@42 | 268 | |
universe@69 | 269 | value = (const char *) ucx_map_cstr_get(map, "simple"); |
universe@43 | 270 | UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); |
universe@42 | 271 | UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " |
universe@42 | 272 | "to test if the buffer extension works as designed") == 0, |
universe@42 | 273 | "value error for key: simple"); |
universe@42 | 274 | |
universe@55 | 275 | void *d; |
universe@55 | 276 | UcxMapIterator iter = ucx_map_iterator(map); |
olaf@111 | 277 | UCX_MAP_FOREACH(key, d, iter) { |
universe@55 | 278 | free(d); |
universe@55 | 279 | } |
universe@55 | 280 | ucx_map_free(map); |
universe@42 | 281 | UCX_TEST_END |
universe@48 | 282 | } |
universe@48 | 283 | |
universe@48 | 284 | UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { |
universe@48 | 285 | UcxMap *map = ucx_map_new(4); |
universe@48 | 286 | |
universe@71 | 287 | ucx_map_cstr_put(map, "test", (void*)"test"); |
universe@71 | 288 | ucx_map_cstr_put(map, "key", (void*)"value"); |
universe@71 | 289 | ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); |
universe@71 | 290 | ucx_map_cstr_put(map, "simple", (void*)"a simple value"); |
universe@48 | 291 | |
universe@55 | 292 | UCX_TEST_BEGIN |
universe@55 | 293 | FILE *f = tmpfile(); |
olaf@76 | 294 | UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted"); |
universe@48 | 295 | int r; |
universe@48 | 296 | r = ucx_map_store_enc(map, f, NULL, NULL); |
universe@48 | 297 | ucx_map_free(map); |
universe@55 | 298 | fflush(f); |
universe@42 | 299 | |
universe@48 | 300 | UcxMempool *pool = ucx_mempool_new(4); |
universe@48 | 301 | map = ucx_map_new(4); |
universe@55 | 302 | fseek(f, 0, SEEK_SET); |
universe@48 | 303 | UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); |
universe@48 | 304 | r += ucx_map_load_enc(map, f, allocator, |
universe@48 | 305 | test_ucx_map_store_load_encdec, NULL); |
universe@48 | 306 | fclose(f); |
universe@48 | 307 | |
universe@48 | 308 | UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
universe@48 | 309 | UcxMapIterator iter = ucx_map_iterator(map); |
universe@69 | 310 | const char *value; size_t n; |
olaf@111 | 311 | UCX_MAP_FOREACH(key, value, iter) { |
universe@48 | 312 | n = strlen(value); |
universe@69 | 313 | UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n), |
universe@48 | 314 | "values of map does not match pooled values"); |
universe@48 | 315 | } |
universe@48 | 316 | |
universe@48 | 317 | ucx_mempool_free(pool); |
universe@55 | 318 | ucx_map_free(map); |
universe@55 | 319 | UCX_TEST_END |
universe@42 | 320 | } |
olaf@44 | 321 | |
olaf@44 | 322 | UCX_TEST_IMPLEMENT(test_ucx_map_clone) { |
olaf@44 | 323 | UcxMap *map = ucx_map_new(4); |
olaf@44 | 324 | |
universe@71 | 325 | ucx_map_cstr_put(map, "key1", (void*)"value1"); |
universe@71 | 326 | ucx_map_cstr_put(map, "key2", (void*)"value2"); |
universe@71 | 327 | ucx_map_cstr_put(map, "key3", (void*)"value3"); |
olaf@44 | 328 | |
olaf@44 | 329 | UcxMap *clone = ucx_map_clone(map, NULL, NULL); |
olaf@44 | 330 | |
universe@69 | 331 | const char *v1 = (const char *) ucx_map_cstr_get(map, "key1"); |
universe@69 | 332 | const char *v2 = (const char *) ucx_map_cstr_get(map, "key2"); |
universe@69 | 333 | const char *v3 = (const char *) ucx_map_cstr_get(map, "key3"); |
olaf@44 | 334 | |
olaf@44 | 335 | UCX_TEST_BEGIN |
olaf@44 | 336 | |
olaf@44 | 337 | UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); |
olaf@44 | 338 | UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); |
olaf@44 | 339 | UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); |
olaf@44 | 340 | |
universe@69 | 341 | const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1"); |
universe@69 | 342 | const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2"); |
universe@69 | 343 | const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3"); |
olaf@44 | 344 | |
olaf@44 | 345 | UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); |
olaf@44 | 346 | UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); |
olaf@44 | 347 | UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); |
olaf@44 | 348 | |
olaf@44 | 349 | UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1"); |
olaf@44 | 350 | UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2"); |
olaf@44 | 351 | UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3"); |
olaf@44 | 352 | |
olaf@44 | 353 | UCX_TEST_END |
olaf@44 | 354 | |
olaf@44 | 355 | ucx_map_free(map); |
olaf@44 | 356 | ucx_map_free(clone); |
olaf@44 | 357 | } |
universe@51 | 358 | |
universe@51 | 359 | UCX_TEST_IMPLEMENT(test_ucx_map_rehash) { |
universe@51 | 360 | UcxMap *map = ucx_map_new(4); |
universe@51 | 361 | |
universe@51 | 362 | char keys[10][5]; |
universe@51 | 363 | char values[10][7]; |
universe@51 | 364 | for (int i = 0 ; i < 10 ; i++) { |
universe@51 | 365 | strcpy(keys[i], "key"); |
universe@51 | 366 | keys[i][3] = 48+i; keys[i][4] = 0; |
universe@51 | 367 | strcpy(values[i], "value"); |
universe@51 | 368 | values[i][5] = 48+i; values[i][6] = 0; |
universe@51 | 369 | |
universe@51 | 370 | ucx_map_cstr_put(map, keys[i], values[i]); |
universe@51 | 371 | } |
universe@51 | 372 | |
olaf@52 | 373 | ucx_map_rehash(map); |
universe@51 | 374 | |
universe@51 | 375 | UCX_TEST_BEGIN |
universe@51 | 376 | UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count"); |
universe@51 | 377 | UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect"); |
universe@51 | 378 | for (int i = 0 ; i < 10 ; i++) { |
universe@69 | 379 | const char *value = (const char *) ucx_map_cstr_get(map, keys[i]); |
universe@51 | 380 | UCX_TEST_ASSERT(value != NULL, "new map is missing old keys"); |
universe@51 | 381 | UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0, |
universe@51 | 382 | "new map contains incorrect values"); |
universe@51 | 383 | } |
olaf@52 | 384 | ucx_map_rehash(map); |
olaf@52 | 385 | UCX_TEST_ASSERT(map->size == 25, |
universe@51 | 386 | "subsequent rehashing call shall not change size"); |
universe@51 | 387 | UCX_TEST_END |
universe@51 | 388 | |
universe@51 | 389 | ucx_map_free(map); |
universe@51 | 390 | } |