test/map_tests.c

Thu, 19 Dec 2019 19:58:41 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 19 Dec 2019 19:58:41 +0100
changeset 374
be77fb2da242
parent 327
fbc33813265b
permissions
-rw-r--r--

adds set operations for UcxMap

olaf@20 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@20 3 *
universe@259 4 * Copyright 2017 Mike Becker, 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"
universe@374 30 #include <ucx/utils.h>
olaf@20 31
universe@134 32 UCX_TEST(test_ucx_map_new) {
olaf@20 33 UcxMap *map = ucx_map_new(16);
universe@33 34 UCX_TEST_BEGIN
universe@40 35 UCX_TEST_ASSERT(map->size == 16, "wrong size");
universe@40 36 UCX_TEST_ASSERT(map->map != NULL, "failed");
universe@29 37
universe@33 38 UCX_TEST_END
universe@29 39 ucx_map_free(map);
universe@29 40 }
olaf@20 41
universe@134 42 UCX_TEST(test_ucx_key) {
universe@327 43 UcxKey key = ucx_key("This is a text.", 15);
universe@33 44 UCX_TEST_BEGIN
universe@69 45 UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0,
universe@69 46 "failed");
universe@40 47 UCX_TEST_ASSERT(key.len == 15, "failed");
universe@40 48 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
universe@29 49
universe@29 50 UCX_TEST_END
universe@29 51 }
olaf@20 52
universe@134 53 UCX_TEST(test_ucx_map_put) {
universe@29 54
universe@29 55 UcxMap *map = ucx_map_new(4);
universe@29 56
universe@29 57 int td[5];
universe@29 58 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
olaf@20 59
universe@33 60 UCX_TEST_BEGIN
universe@80 61 ucx_map_cstr_put(map, "Key2", &td[2]); /* 3.2 */
universe@80 62 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0.0 */
universe@80 63 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3.0 */
universe@80 64 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 3.1 */
universe@80 65 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 1.0 */
universe@29 66
universe@40 67 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
universe@80 68 UCX_TEST_ASSERT(*((int*)map->map[1]->data) == td[4], "failed KEY4");
universe@80 69 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
universe@80 70
universe@80 71 UCX_TEST_ASSERT(map->map[3]->next != NULL, "no list at slot 3");
universe@80 72 UCX_TEST_ASSERT(map->map[3]->next->next != NULL, "list corrupt at slot 3");
universe@80 73 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[3],
universe@80 74 "failed KeY3")
universe@80 75 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
universe@80 76 "failed KeY2");
universe@29 77
universe@80 78 UCX_TEST_ASSERT(map->map[0]->next == NULL, "slot 0 not terminated");
universe@80 79 UCX_TEST_ASSERT(map->map[1]->next == NULL, "slot 1 not terminated");
universe@80 80 UCX_TEST_ASSERT(map->map[2] == NULL, "slot 2 not empty");
universe@80 81 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL,
universe@80 82 "slot 3 not terminated")
universe@29 83
universe@80 84 ucx_map_cstr_put(map, "KeY3", &td[4]); /* replace 3.1 */
universe@29 85
universe@80 86 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1],
universe@29 87 "overwrite failed")
universe@80 88 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[4],
universe@80 89 "overwrite failed");
universe@80 90 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
universe@29 91 "overwrite failed")
universe@80 92 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, "overwrite failed");
universe@29 93
universe@33 94 UCX_TEST_END
universe@29 95 ucx_map_free(map);
universe@33 96 }
universe@33 97
universe@134 98 UCX_TEST(test_ucx_map_get) {
universe@34 99 UcxMap *map = ucx_map_new(4);
universe@34 100
universe@34 101 int td[5];
universe@34 102 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
universe@34 103
universe@80 104 ucx_map_cstr_put(map, "Key2", &td[2]);
universe@80 105 ucx_map_cstr_put(map, "Key0", &td[0]);
universe@80 106 ucx_map_cstr_put(map, "Key1", &td[1]);
universe@80 107 ucx_map_cstr_put(map, "KeY3", &td[3]);
universe@80 108 ucx_map_cstr_put(map, "KEY4", &td[4]);
universe@33 109 UCX_TEST_BEGIN
universe@34 110
universe@34 111 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
universe@34 112 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
universe@34 113 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
universe@34 114 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
universe@34 115 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
universe@40 116 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
universe@40 117 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
universe@40 118 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
universe@40 119 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
universe@40 120 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
universe@34 121
universe@53 122 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
universe@53 123 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
universe@53 124
universe@53 125 UCX_TEST_END
universe@53 126 ucx_map_free(map);
universe@53 127 }
universe@53 128
universe@134 129 UCX_TEST(test_ucx_map_remove) {
universe@53 130 UcxMap *map = ucx_map_new(4);
universe@53 131
universe@53 132 int td[5];
universe@53 133 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
universe@53 134
universe@53 135 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
universe@53 136 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
universe@53 137 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
universe@53 138 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
universe@53 139 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
universe@53 140 UCX_TEST_BEGIN
universe@53 141
universe@53 142 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
universe@53 143 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
universe@53 144 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
universe@53 145 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
universe@53 146 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
universe@53 147 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
universe@53 148 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
universe@53 149 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
universe@53 150 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
universe@53 151 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
universe@53 152
universe@53 153 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
universe@53 154 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
universe@53 155 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
universe@53 156 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
universe@53 157 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
universe@53 158 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
universe@53 159
universe@53 160 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
universe@53 161 "subsequent remove call shall return NULL");
universe@53 162
universe@29 163 UCX_TEST_END
universe@34 164 ucx_map_free(map);
olaf@20 165 }
universe@29 166
universe@206 167 UCX_TEST(test_ucx_map_clear) {
universe@206 168 UcxMap *map = ucx_map_new(4);
universe@206 169
universe@206 170 int value = 42;
universe@206 171
universe@206 172 ucx_map_cstr_put(map, "Key0", &value);
universe@206 173 ucx_map_cstr_put(map, "Key1", &value);
universe@206 174 ucx_map_cstr_put(map, "Key2", &value);
universe@206 175 ucx_map_cstr_put(map, "Key3", &value);
universe@206 176 ucx_map_cstr_put(map, "Key4", &value);
universe@206 177 ucx_map_cstr_put(map, "Key5", &value);
universe@206 178 ucx_map_cstr_put(map, "Key6", &value);
universe@206 179 UCX_TEST_BEGIN
universe@206 180
universe@206 181 ucx_map_clear(map);
universe@206 182
universe@206 183 UCX_TEST_ASSERT(map->count == 0, "map has not been cleared");
universe@206 184 UCX_TEST_ASSERT(map->size == 4, "map size has changed unexpectedly");
universe@206 185
universe@206 186 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
universe@206 187 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")==NULL, "element not removed");
universe@206 188 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
universe@206 189 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key3")==NULL, "element not removed");
universe@206 190 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key4")==NULL, "element not removed");
universe@206 191 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key5")==NULL, "element not removed");
universe@206 192 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key6")==NULL, "element not removed");
universe@206 193
universe@206 194 UCX_TEST_END
universe@206 195 ucx_map_free(map);
universe@206 196 }
universe@206 197
universe@88 198 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) {
olaf@31 199 int v1 = 10;
olaf@31 200 int v2 = 15;
olaf@31 201 int v3 = 7;
olaf@31 202 int v4 = 9;
universe@32 203
olaf@31 204 ucx_map_cstr_put(map, "v1", &v1);
olaf@31 205 ucx_map_cstr_put(map, "v2", &v2);
olaf@31 206 ucx_map_cstr_put(map, "v3", &v3);
olaf@31 207 ucx_map_cstr_put(map, "v4", &v4);
universe@32 208
olaf@31 209 UcxMapIterator i = ucx_map_iterator(map);
olaf@31 210 int check = 0;
olaf@31 211 int hit = 0;
universe@32 212
universe@152 213 void* v;
olaf@111 214 UCX_MAP_FOREACH(key, v, i) {
universe@152 215 check += *((int*)v);
olaf@31 216 hit++;
olaf@31 217 }
universe@32 218
olaf@31 219 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
olaf@31 220 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
universe@33 221 }
universe@32 222
universe@134 223 UCX_TEST(test_ucx_map_iterator) {
universe@33 224 UcxMap *map = ucx_map_new(16);
universe@33 225 UCX_TEST_BEGIN
universe@33 226 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 227 UCX_TEST_END
olaf@31 228 ucx_map_free(map);
universe@33 229 }
universe@33 230
universe@134 231 UCX_TEST(test_ucx_map_iterator_chain) {
universe@33 232 UcxMap *map = ucx_map_new(1);
universe@33 233 UCX_TEST_BEGIN
universe@33 234 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 235 UCX_TEST_END
olaf@31 236 ucx_map_free(map);
olaf@31 237 }
universe@42 238
universe@134 239 UCX_TEST(test_ucx_map_clone) {
olaf@44 240 UcxMap *map = ucx_map_new(4);
olaf@44 241
universe@71 242 ucx_map_cstr_put(map, "key1", (void*)"value1");
universe@71 243 ucx_map_cstr_put(map, "key2", (void*)"value2");
universe@71 244 ucx_map_cstr_put(map, "key3", (void*)"value3");
olaf@44 245
olaf@44 246 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
olaf@44 247
universe@69 248 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
universe@69 249 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
universe@69 250 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
olaf@44 251
olaf@44 252 UCX_TEST_BEGIN
olaf@44 253
olaf@44 254 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
olaf@44 255 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
olaf@44 256 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
olaf@44 257
universe@69 258 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
universe@69 259 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
universe@69 260 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
olaf@44 261
olaf@44 262 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
olaf@44 263 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
olaf@44 264 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
olaf@44 265
olaf@44 266 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
olaf@44 267 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
olaf@44 268 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
olaf@44 269
olaf@44 270 UCX_TEST_END
olaf@44 271
olaf@44 272 ucx_map_free(map);
olaf@44 273 ucx_map_free(clone);
olaf@44 274 }
universe@51 275
universe@134 276 UCX_TEST(test_ucx_map_rehash) {
universe@51 277 UcxMap *map = ucx_map_new(4);
universe@51 278
universe@51 279 char keys[10][5];
universe@51 280 char values[10][7];
universe@51 281 for (int i = 0 ; i < 10 ; i++) {
universe@51 282 strcpy(keys[i], "key");
universe@51 283 keys[i][3] = 48+i; keys[i][4] = 0;
universe@51 284 strcpy(values[i], "value");
universe@51 285 values[i][5] = 48+i; values[i][6] = 0;
universe@51 286
universe@51 287 ucx_map_cstr_put(map, keys[i], values[i]);
universe@51 288 }
universe@51 289
olaf@52 290 ucx_map_rehash(map);
universe@51 291
universe@51 292 UCX_TEST_BEGIN
universe@51 293 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
universe@51 294 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
universe@51 295 for (int i = 0 ; i < 10 ; i++) {
universe@69 296 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
universe@51 297 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
universe@51 298 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
universe@51 299 "new map contains incorrect values");
universe@51 300 }
olaf@52 301 ucx_map_rehash(map);
olaf@52 302 UCX_TEST_ASSERT(map->size == 25,
universe@51 303 "subsequent rehashing call shall not change size");
universe@51 304 UCX_TEST_END
universe@51 305
universe@51 306 ucx_map_free(map);
universe@51 307 }
universe@374 308
universe@374 309 UCX_TEST(test_ucx_map_union) {
universe@374 310 int td[5];
universe@374 311 size_t intlen = sizeof(int);
universe@374 312 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
universe@374 313
universe@374 314 UcxMap *first = ucx_map_new(4);
universe@374 315 UcxMap *second = ucx_map_new(4);
universe@374 316
universe@374 317 ucx_map_cstr_put(first, "key0", &td[0]);
universe@374 318 ucx_map_cstr_put(first, "key1", &td[1]);
universe@374 319 ucx_map_cstr_put(second, "key2", &td[2]);
universe@374 320 ucx_map_cstr_put(second, "key0", &td[3]);
universe@374 321 ucx_map_cstr_put(second, "key3", &td[4]);
universe@374 322
universe@374 323 UcxMap *result = ucx_map_union(first, second, ucx_memcpy, &intlen);
universe@374 324
universe@374 325 UCX_TEST_BEGIN
universe@374 326
universe@374 327 int* r;
universe@374 328 UCX_TEST_ASSERT(result->count == 4,
universe@374 329 "result has incorrect number of elements");
universe@374 330
universe@374 331 r = (int*)ucx_map_cstr_get(result, "key0");
universe@374 332 UCX_TEST_ASSERT(!!r, "key0 is not present");
universe@374 333 UCX_TEST_ASSERT(*r == td[3], "key0 has not been overwritten");
universe@374 334 r = (int*)ucx_map_cstr_get(result, "key1");
universe@374 335 UCX_TEST_ASSERT(!!r, "key1 is not present");
universe@374 336 UCX_TEST_ASSERT(*r == td[1], "key1 contains wrong data");
universe@374 337 r = (int*)ucx_map_cstr_get(result, "key2");
universe@374 338 UCX_TEST_ASSERT(!!r, "key2 is not present");
universe@374 339 UCX_TEST_ASSERT(*r == td[2], "key2 contains wrong data");
universe@374 340 r = (int*)ucx_map_cstr_get(result, "key3");
universe@374 341 UCX_TEST_ASSERT(!!r, "key3 is not present");
universe@374 342 UCX_TEST_ASSERT(*r == td[4], "key3 contains wrong data");
universe@374 343
universe@374 344 UCX_TEST_END
universe@374 345
universe@374 346 ucx_map_free_content(result, NULL);
universe@374 347 ucx_map_free(result);
universe@374 348 ucx_map_free(second);
universe@374 349 ucx_map_free(first);
universe@374 350 }
universe@374 351
universe@374 352 UCX_TEST(test_ucx_map_intersection) {
universe@374 353 int td[5];
universe@374 354 size_t intlen = sizeof(int);
universe@374 355 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
universe@374 356
universe@374 357 UcxMap *first = ucx_map_new(4);
universe@374 358 UcxMap *second = ucx_map_new(4);
universe@374 359
universe@374 360 ucx_map_cstr_put(first, "key0", &td[0]);
universe@374 361 ucx_map_cstr_put(first, "key1", &td[1]);
universe@374 362 ucx_map_cstr_put(first, "key4", &td[3]);
universe@374 363 ucx_map_cstr_put(second, "key2", &td[2]);
universe@374 364 ucx_map_cstr_put(second, "key0", &td[3]);
universe@374 365 ucx_map_cstr_put(second, "key3", &td[4]);
universe@374 366 ucx_map_cstr_put(second, "key4", &td[4]);
universe@374 367
universe@374 368 UcxMap *result = ucx_map_intersection(first, second,
universe@374 369 ucx_memcpy, &intlen);
universe@374 370
universe@374 371 UCX_TEST_BEGIN
universe@374 372
universe@374 373 int* r;
universe@374 374 UCX_TEST_ASSERT(result->count == 2,
universe@374 375 "result has incorrect number of elements");
universe@374 376
universe@374 377 r = (int*)ucx_map_cstr_get(result, "key0");
universe@374 378 UCX_TEST_ASSERT(!!r, "key0 is not present");
universe@374 379 UCX_TEST_ASSERT(*r == td[0], "key0 has not original data");
universe@374 380 r = (int*)ucx_map_cstr_get(result, "key4");
universe@374 381 UCX_TEST_ASSERT(!!r, "key4 is not present");
universe@374 382 UCX_TEST_ASSERT(*r == td[3], "key4 has not original data");
universe@374 383
universe@374 384 UCX_TEST_END
universe@374 385
universe@374 386 ucx_map_free_content(result, NULL);
universe@374 387 ucx_map_free(result);
universe@374 388 ucx_map_free(second);
universe@374 389 ucx_map_free(first);
universe@374 390 }
universe@374 391
universe@374 392
universe@374 393 UCX_TEST(test_ucx_map_difference) {
universe@374 394 int td[5];
universe@374 395 size_t intlen = sizeof(int);
universe@374 396 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
universe@374 397
universe@374 398 UcxMap *first = ucx_map_new(4);
universe@374 399 UcxMap *second = ucx_map_new(4);
universe@374 400
universe@374 401 ucx_map_cstr_put(first, "key0", &td[0]);
universe@374 402 ucx_map_cstr_put(first, "key1", &td[1]);
universe@374 403 ucx_map_cstr_put(first, "key2", &td[2]);
universe@374 404 ucx_map_cstr_put(first, "key4", &td[3]);
universe@374 405 ucx_map_cstr_put(second, "key0", &td[3]);
universe@374 406 ucx_map_cstr_put(second, "key3", &td[4]);
universe@374 407 ucx_map_cstr_put(second, "key4", &td[4]);
universe@374 408
universe@374 409 UcxMap *result = ucx_map_difference(first, second, ucx_memcpy, &intlen);
universe@374 410
universe@374 411 UCX_TEST_BEGIN
universe@374 412
universe@374 413 int* r;
universe@374 414 UCX_TEST_ASSERT(result->count == 2,
universe@374 415 "result has incorrect number of elements");
universe@374 416
universe@374 417 r = (int*)ucx_map_cstr_get(result, "key1");
universe@374 418 UCX_TEST_ASSERT(!!r, "key1 is not present");
universe@374 419 UCX_TEST_ASSERT(*r == td[1], "key1 has incorrect data");
universe@374 420 r = (int*)ucx_map_cstr_get(result, "key2");
universe@374 421 UCX_TEST_ASSERT(!!r, "key2 is not present");
universe@374 422 UCX_TEST_ASSERT(*r == td[2], "key2 has incorrect data");
universe@374 423
universe@374 424 UCX_TEST_END
universe@374 425
universe@374 426 ucx_map_free_content(result, NULL);
universe@374 427 ucx_map_free(result);
universe@374 428 ucx_map_free(second);
universe@374 429 ucx_map_free(first);
universe@374 430 }
universe@374 431

mercurial