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