Fri, 25 May 2012 17:39:27 +0200
added map iterator
olaf@20 | 1 | /* |
olaf@20 | 2 | * |
olaf@20 | 3 | */ |
olaf@20 | 4 | |
olaf@20 | 5 | #include "map_tests.h" |
olaf@20 | 6 | |
universe@29 | 7 | UCX_TEST_BEGIN(test_ucx_map_new) { |
olaf@20 | 8 | UcxMap *map = ucx_map_new(16); |
universe@29 | 9 | |
universe@29 | 10 | UCX_TEST_ASSERT(map->size == 16, "wrong size") |
universe@29 | 11 | UCX_TEST_ASSERT(map->map != NULL, "failed") |
universe@29 | 12 | |
universe@29 | 13 | ucx_map_free(map); |
universe@29 | 14 | |
universe@29 | 15 | UCX_TEST_END |
universe@29 | 16 | } |
olaf@20 | 17 | |
universe@29 | 18 | UCX_TEST_BEGIN(test_ucx_key) { |
universe@29 | 19 | |
universe@29 | 20 | UcxKey key = ucx_key("This is a text.", 15); |
universe@29 | 21 | UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") |
universe@29 | 22 | UCX_TEST_ASSERT(key.len == 15, "failed") |
universe@29 | 23 | UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") |
universe@29 | 24 | |
universe@29 | 25 | UCX_TEST_END |
universe@29 | 26 | } |
olaf@20 | 27 | |
universe@29 | 28 | UCX_TEST_BEGIN(test_ucx_map_put) { |
universe@29 | 29 | |
universe@29 | 30 | UcxMap *map = ucx_map_new(4); |
universe@29 | 31 | |
universe@29 | 32 | int td[5]; |
universe@29 | 33 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
olaf@20 | 34 | |
universe@29 | 35 | ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ |
universe@29 | 36 | ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ |
universe@29 | 37 | ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ |
universe@29 | 38 | ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ |
universe@29 | 39 | ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ |
universe@29 | 40 | |
universe@29 | 41 | UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0") |
universe@29 | 42 | UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0") |
universe@29 | 43 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2") |
universe@29 | 44 | UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0") |
universe@29 | 45 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], |
universe@29 | 46 | "failed Key4") |
universe@29 | 47 | UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, |
universe@29 | 48 | "slot 0 not terminated") |
universe@29 | 49 | |
universe@29 | 50 | UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated") |
universe@29 | 51 | |
universe@29 | 52 | UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3") |
universe@29 | 53 | UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated") |
universe@29 | 54 | |
universe@29 | 55 | UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1") |
universe@29 | 56 | |
universe@29 | 57 | ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */ |
universe@29 | 58 | |
universe@29 | 59 | UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed") |
universe@29 | 60 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], |
universe@29 | 61 | "overwrite failed") |
universe@29 | 62 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], |
universe@29 | 63 | "overwrite failed") |
universe@29 | 64 | UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed") |
universe@29 | 65 | |
universe@29 | 66 | ucx_map_free(map); |
universe@29 | 67 | |
universe@29 | 68 | UCX_TEST_END |
olaf@20 | 69 | } |
universe@29 | 70 | |
universe@29 | 71 | UCX_TEST_BEGIN(test_ucx_map_get) { |
universe@29 | 72 | UCX_TEST_END |
universe@29 | 73 | } |
olaf@31 | 74 | |
olaf@31 | 75 | UCX_TEST_BEGIN(test_ucx_map_iterator) { |
olaf@31 | 76 | UcxMap *map = ucx_map_new(16); |
olaf@31 | 77 | |
olaf@31 | 78 | int v1 = 10; |
olaf@31 | 79 | int v2 = 15; |
olaf@31 | 80 | int v3 = 7; |
olaf@31 | 81 | int v4 = 9; |
olaf@31 | 82 | |
olaf@31 | 83 | ucx_map_cstr_put(map, "v1", &v1); |
olaf@31 | 84 | ucx_map_cstr_put(map, "v2", &v2); |
olaf@31 | 85 | ucx_map_cstr_put(map, "v3", &v3); |
olaf@31 | 86 | ucx_map_cstr_put(map, "v4", &v4); |
olaf@31 | 87 | |
olaf@31 | 88 | UcxMapIterator i = ucx_map_iterator(map); |
olaf@31 | 89 | int check = 0; |
olaf@31 | 90 | int hit = 0; |
olaf@31 | 91 | |
olaf@31 | 92 | UCX_MAP_FOREACH(int*, v, map, i) { |
olaf@31 | 93 | check += *v; |
olaf@31 | 94 | hit++; |
olaf@31 | 95 | } |
olaf@31 | 96 | |
olaf@31 | 97 | UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); |
olaf@31 | 98 | UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); |
olaf@31 | 99 | |
olaf@31 | 100 | ucx_map_free(map); |
olaf@31 | 101 | |
olaf@31 | 102 | map = ucx_map_new(1); |
olaf@31 | 103 | ucx_map_cstr_put(map, "v1", &v1); |
olaf@31 | 104 | ucx_map_cstr_put(map, "v2", &v2); |
olaf@31 | 105 | ucx_map_cstr_put(map, "v3", &v3); |
olaf@31 | 106 | ucx_map_cstr_put(map, "v4", &v4); |
olaf@31 | 107 | |
olaf@31 | 108 | i = ucx_map_iterator(map); |
olaf@31 | 109 | check = 0; |
olaf@31 | 110 | hit = 0; |
olaf@31 | 111 | |
olaf@31 | 112 | UCX_MAP_FOREACH(int*, v, map, i) { |
olaf@31 | 113 | check += *v; |
olaf@31 | 114 | hit++; |
olaf@31 | 115 | } |
olaf@31 | 116 | |
olaf@31 | 117 | UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits"); |
olaf@31 | 118 | UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result"); |
olaf@31 | 119 | |
olaf@31 | 120 | |
olaf@31 | 121 | ucx_map_free(map); |
olaf@31 | 122 | |
olaf@31 | 123 | UCX_TEST_END |
olaf@31 | 124 | } |