olaf@20: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@20: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@20: */ olaf@20: olaf@20: #include "map_tests.h" olaf@20: universe@134: UCX_TEST(test_ucx_map_new) { olaf@20: UcxMap *map = ucx_map_new(16); universe@33: UCX_TEST_BEGIN universe@40: UCX_TEST_ASSERT(map->size == 16, "wrong size"); universe@40: UCX_TEST_ASSERT(map->map != NULL, "failed"); universe@29: universe@33: UCX_TEST_END universe@29: ucx_map_free(map); universe@29: } olaf@20: universe@134: UCX_TEST(test_ucx_key) { universe@71: UcxKey key = ucx_key((void*)"This is a text.", 15); universe@33: UCX_TEST_BEGIN universe@69: UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0, universe@69: "failed"); universe@40: UCX_TEST_ASSERT(key.len == 15, "failed"); universe@40: UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed"); universe@29: universe@29: UCX_TEST_END universe@29: } olaf@20: universe@134: UCX_TEST(test_ucx_map_put) { universe@29: universe@29: UcxMap *map = ucx_map_new(4); universe@29: universe@29: int td[5]; universe@29: td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; olaf@20: universe@33: UCX_TEST_BEGIN universe@80: ucx_map_cstr_put(map, "Key2", &td[2]); /* 3.2 */ universe@80: ucx_map_cstr_put(map, "Key0", &td[0]); /* 0.0 */ universe@80: ucx_map_cstr_put(map, "Key1", &td[1]); /* 3.0 */ universe@80: ucx_map_cstr_put(map, "KeY3", &td[3]); /* 3.1 */ universe@80: ucx_map_cstr_put(map, "KEY4", &td[4]); /* 1.0 */ universe@29: universe@40: UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0"); universe@80: UCX_TEST_ASSERT(*((int*)map->map[1]->data) == td[4], "failed KEY4"); universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1"); universe@80: universe@80: UCX_TEST_ASSERT(map->map[3]->next != NULL, "no list at slot 3"); universe@80: UCX_TEST_ASSERT(map->map[3]->next->next != NULL, "list corrupt at slot 3"); universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[3], universe@80: "failed KeY3") universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2], universe@80: "failed KeY2"); universe@29: universe@80: UCX_TEST_ASSERT(map->map[0]->next == NULL, "slot 0 not terminated"); universe@80: UCX_TEST_ASSERT(map->map[1]->next == NULL, "slot 1 not terminated"); universe@80: UCX_TEST_ASSERT(map->map[2] == NULL, "slot 2 not empty"); universe@80: UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, universe@80: "slot 3 not terminated") universe@29: universe@80: ucx_map_cstr_put(map, "KeY3", &td[4]); /* replace 3.1 */ universe@29: universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], universe@29: "overwrite failed") universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[4], universe@80: "overwrite failed"); universe@80: UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2], universe@29: "overwrite failed") universe@80: UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, "overwrite failed"); universe@29: universe@33: UCX_TEST_END universe@29: ucx_map_free(map); universe@33: } universe@33: universe@134: UCX_TEST(test_ucx_map_get) { universe@34: UcxMap *map = ucx_map_new(4); universe@34: universe@34: int td[5]; universe@34: td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; universe@34: universe@80: ucx_map_cstr_put(map, "Key2", &td[2]); universe@80: ucx_map_cstr_put(map, "Key0", &td[0]); universe@80: ucx_map_cstr_put(map, "Key1", &td[1]); universe@80: ucx_map_cstr_put(map, "KeY3", &td[3]); universe@80: ucx_map_cstr_put(map, "KEY4", &td[4]); universe@33: UCX_TEST_BEGIN universe@34: universe@34: td[0] = *((int*)ucx_map_cstr_get(map, "Key0")); universe@34: td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); universe@34: td[2] = *((int*)ucx_map_cstr_get(map, "Key2")); universe@34: td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); universe@34: td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); universe@40: UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); universe@40: UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); universe@40: UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); universe@40: UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); universe@40: UCX_TEST_ASSERT(td[4] == 80000, "failed key 4"); universe@34: universe@53: UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed"); universe@53: universe@53: UCX_TEST_END universe@53: ucx_map_free(map); universe@53: } universe@53: universe@134: UCX_TEST(test_ucx_map_remove) { universe@53: UcxMap *map = ucx_map_new(4); universe@53: universe@53: int td[5]; universe@53: td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; universe@53: universe@53: ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ universe@53: ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ universe@53: ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ universe@53: ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ universe@53: ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ universe@53: UCX_TEST_BEGIN universe@53: universe@53: td[0] = *((int*)ucx_map_cstr_remove(map, "Key0")); universe@53: td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); universe@53: td[2] = *((int*)ucx_map_cstr_remove(map, "Key2")); universe@53: td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); universe@53: td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); universe@53: UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); universe@53: UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); universe@53: UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); universe@53: UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); universe@53: UCX_TEST_ASSERT(td[4] == 80000, "failed key 4"); universe@53: universe@53: UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed"); universe@53: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed"); universe@53: universe@53: UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL, universe@53: "subsequent remove call shall return NULL"); universe@53: universe@29: UCX_TEST_END universe@34: ucx_map_free(map); olaf@20: } universe@29: universe@206: UCX_TEST(test_ucx_map_clear) { universe@206: UcxMap *map = ucx_map_new(4); universe@206: universe@206: int value = 42; universe@206: universe@206: ucx_map_cstr_put(map, "Key0", &value); universe@206: ucx_map_cstr_put(map, "Key1", &value); universe@206: ucx_map_cstr_put(map, "Key2", &value); universe@206: ucx_map_cstr_put(map, "Key3", &value); universe@206: ucx_map_cstr_put(map, "Key4", &value); universe@206: ucx_map_cstr_put(map, "Key5", &value); universe@206: ucx_map_cstr_put(map, "Key6", &value); universe@206: UCX_TEST_BEGIN universe@206: universe@206: ucx_map_clear(map); universe@206: universe@206: UCX_TEST_ASSERT(map->count == 0, "map has not been cleared"); universe@206: UCX_TEST_ASSERT(map->size == 4, "map size has changed unexpectedly"); universe@206: universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key3")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key4")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key5")==NULL, "element not removed"); universe@206: UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key6")==NULL, "element not removed"); universe@206: universe@206: UCX_TEST_END universe@206: ucx_map_free(map); universe@206: } universe@206: universe@88: UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) { olaf@31: int v1 = 10; olaf@31: int v2 = 15; olaf@31: int v3 = 7; olaf@31: int v4 = 9; universe@32: olaf@31: ucx_map_cstr_put(map, "v1", &v1); olaf@31: ucx_map_cstr_put(map, "v2", &v2); olaf@31: ucx_map_cstr_put(map, "v3", &v3); olaf@31: ucx_map_cstr_put(map, "v4", &v4); universe@32: olaf@31: UcxMapIterator i = ucx_map_iterator(map); olaf@31: int check = 0; olaf@31: int hit = 0; universe@32: universe@152: void* v; olaf@111: UCX_MAP_FOREACH(key, v, i) { universe@152: check += *((int*)v); olaf@31: hit++; olaf@31: } universe@32: olaf@31: UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); olaf@31: UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); universe@33: } universe@32: universe@134: UCX_TEST(test_ucx_map_iterator) { universe@33: UcxMap *map = ucx_map_new(16); universe@33: UCX_TEST_BEGIN universe@33: UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) universe@33: UCX_TEST_END olaf@31: ucx_map_free(map); universe@33: } universe@33: universe@134: UCX_TEST(test_ucx_map_iterator_chain) { universe@33: UcxMap *map = ucx_map_new(1); universe@33: UCX_TEST_BEGIN universe@33: UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) universe@33: UCX_TEST_END olaf@31: ucx_map_free(map); olaf@31: } universe@42: universe@134: UCX_TEST(test_ucx_map_clone) { olaf@44: UcxMap *map = ucx_map_new(4); olaf@44: universe@71: ucx_map_cstr_put(map, "key1", (void*)"value1"); universe@71: ucx_map_cstr_put(map, "key2", (void*)"value2"); universe@71: ucx_map_cstr_put(map, "key3", (void*)"value3"); olaf@44: olaf@44: UcxMap *clone = ucx_map_clone(map, NULL, NULL); olaf@44: universe@69: const char *v1 = (const char *) ucx_map_cstr_get(map, "key1"); universe@69: const char *v2 = (const char *) ucx_map_cstr_get(map, "key2"); universe@69: const char *v3 = (const char *) ucx_map_cstr_get(map, "key3"); olaf@44: olaf@44: UCX_TEST_BEGIN olaf@44: olaf@44: UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); olaf@44: UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); olaf@44: UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); olaf@44: universe@69: const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1"); universe@69: const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2"); universe@69: const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3"); olaf@44: olaf@44: UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); olaf@44: UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); olaf@44: UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); olaf@44: olaf@44: UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1"); olaf@44: UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2"); olaf@44: UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3"); olaf@44: olaf@44: UCX_TEST_END olaf@44: olaf@44: ucx_map_free(map); olaf@44: ucx_map_free(clone); olaf@44: } universe@51: universe@134: UCX_TEST(test_ucx_map_rehash) { universe@51: UcxMap *map = ucx_map_new(4); universe@51: universe@51: char keys[10][5]; universe@51: char values[10][7]; universe@51: for (int i = 0 ; i < 10 ; i++) { universe@51: strcpy(keys[i], "key"); universe@51: keys[i][3] = 48+i; keys[i][4] = 0; universe@51: strcpy(values[i], "value"); universe@51: values[i][5] = 48+i; values[i][6] = 0; universe@51: universe@51: ucx_map_cstr_put(map, keys[i], values[i]); universe@51: } universe@51: olaf@52: ucx_map_rehash(map); universe@51: universe@51: UCX_TEST_BEGIN universe@51: UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count"); universe@51: UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect"); universe@51: for (int i = 0 ; i < 10 ; i++) { universe@69: const char *value = (const char *) ucx_map_cstr_get(map, keys[i]); universe@51: UCX_TEST_ASSERT(value != NULL, "new map is missing old keys"); universe@51: UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0, universe@51: "new map contains incorrect values"); universe@51: } olaf@52: ucx_map_rehash(map); olaf@52: UCX_TEST_ASSERT(map->size == 25, universe@51: "subsequent rehashing call shall not change size"); universe@51: UCX_TEST_END universe@51: universe@51: ucx_map_free(map); universe@51: }