test/map_tests.c

Thu, 04 Oct 2012 18:23:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 18:23:32 +0200
changeset 43
02f38adea013
parent 42
ff3dd1ee7dee
child 44
46356d74e873
permissions
-rw-r--r--

fixed crash fails by completing the implementation of the tested function....

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@20 4
olaf@20 5 #include "map_tests.h"
olaf@20 6
universe@33 7 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
olaf@20 8 UcxMap *map = ucx_map_new(16);
universe@33 9 UCX_TEST_BEGIN
universe@40 10 UCX_TEST_ASSERT(map->size == 16, "wrong size");
universe@40 11 UCX_TEST_ASSERT(map->map != NULL, "failed");
universe@29 12
universe@33 13 UCX_TEST_END
universe@29 14 ucx_map_free(map);
universe@29 15 }
olaf@20 16
universe@33 17 UCX_TEST_IMPLEMENT(test_ucx_key) {
universe@29 18 UcxKey key = ucx_key("This is a text.", 15);
universe@33 19 UCX_TEST_BEGIN
universe@40 20 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
universe@40 21 UCX_TEST_ASSERT(key.len == 15, "failed");
universe@40 22 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
universe@29 23
universe@29 24 UCX_TEST_END
universe@29 25 }
olaf@20 26
universe@33 27 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
universe@29 28
universe@29 29 UcxMap *map = ucx_map_new(4);
universe@29 30
universe@29 31 int td[5];
universe@29 32 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
olaf@20 33
universe@33 34 UCX_TEST_BEGIN
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@40 41 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
universe@40 42 UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
universe@40 43 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
universe@40 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@40 50 UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
universe@29 51
universe@40 52 UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
universe@40 53 UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
universe@29 54
universe@40 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@40 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@40 64 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
universe@29 65
universe@33 66 UCX_TEST_END
universe@29 67 ucx_map_free(map);
universe@33 68 }
universe@33 69
universe@33 70 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
universe@34 71 UcxMap *map = ucx_map_new(4);
universe@34 72
universe@34 73 int td[5];
universe@34 74 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
universe@34 75
universe@34 76 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
universe@34 77 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
universe@34 78 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
universe@34 79 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
universe@34 80 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
universe@33 81 UCX_TEST_BEGIN
universe@34 82
universe@34 83 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
universe@34 84 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
universe@34 85 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
universe@34 86 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
universe@34 87 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
universe@40 88 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
universe@40 89 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
universe@40 90 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
universe@40 91 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
universe@40 92 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
universe@34 93
universe@29 94 UCX_TEST_END
universe@34 95 ucx_map_free(map);
olaf@20 96 }
universe@29 97
universe@33 98 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
universe@33 99 UcxMap *map = (UcxMap*) mapptr;
olaf@31 100 int v1 = 10;
olaf@31 101 int v2 = 15;
olaf@31 102 int v3 = 7;
olaf@31 103 int v4 = 9;
universe@32 104
olaf@31 105 ucx_map_cstr_put(map, "v1", &v1);
olaf@31 106 ucx_map_cstr_put(map, "v2", &v2);
olaf@31 107 ucx_map_cstr_put(map, "v3", &v3);
olaf@31 108 ucx_map_cstr_put(map, "v4", &v4);
universe@32 109
olaf@31 110 UcxMapIterator i = ucx_map_iterator(map);
olaf@31 111 int check = 0;
olaf@31 112 int hit = 0;
universe@32 113
universe@41 114 int* v;
universe@41 115 UCX_MAP_FOREACH(v, i) {
olaf@31 116 check += *v;
olaf@31 117 hit++;
olaf@31 118 }
universe@32 119
olaf@31 120 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
olaf@31 121 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
universe@33 122 }
universe@32 123
universe@33 124 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
universe@33 125 UcxMap *map = ucx_map_new(16);
universe@33 126 UCX_TEST_BEGIN
universe@33 127 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 128 UCX_TEST_END
olaf@31 129 ucx_map_free(map);
universe@33 130 }
universe@33 131
universe@33 132 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
universe@33 133 UcxMap *map = ucx_map_new(1);
universe@33 134 UCX_TEST_BEGIN
universe@33 135 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 136 UCX_TEST_END
olaf@31 137 ucx_map_free(map);
olaf@31 138 }
universe@42 139
universe@42 140 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
universe@42 141 UcxMap *map = ucx_map_new(4);
universe@42 142
universe@42 143 ucx_map_cstr_put(map, "test", "test");
universe@42 144 ucx_map_cstr_put(map, "key", "value");
universe@42 145 ucx_map_cstr_put(map, "other.very.long.key", "value");
universe@42 146 ucx_map_cstr_put(map, "testkey", "testvalue");
universe@42 147 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
universe@42 148 "to test if the buffer extension works as designed");
universe@42 149
universe@42 150 FILE *f = fopen("test_ucx_map_store", "w");
universe@42 151 int r;
universe@42 152
universe@43 153 fwrite(" # comment test\n", 1, 16, f);
universe@42 154 r = ucx_map_store(map, f);
universe@43 155 fwrite("!discard this", 1, 13, f);
universe@42 156
universe@42 157 fclose(f);
universe@42 158 ucx_map_free(map);
universe@42 159 map = ucx_map_new(1);
universe@42 160 f = fopen("test_ucx_map_store", "r");
universe@42 161 r += ucx_map_load(map, f);
universe@42 162
universe@42 163 UCX_TEST_BEGIN
universe@42 164 char *value;
universe@42 165 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@42 166
universe@42 167 value = ucx_map_cstr_get(map, "test");
universe@43 168 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
universe@42 169 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
universe@42 170
universe@42 171 value = ucx_map_cstr_get(map, "key");
universe@43 172 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
universe@42 173 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
universe@42 174
universe@42 175 value = ucx_map_cstr_get(map, "other.very.long.key");
universe@43 176 UCX_TEST_ASSERT(value != NULL,
universe@43 177 "value not found for key: other.very.long.key");
universe@42 178 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
universe@42 179 "value error for key: other.very.long.key");
universe@42 180
universe@42 181 value = ucx_map_cstr_get(map, "testkey");
universe@43 182 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
universe@42 183 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
universe@42 184 "value error for key: testkey");
universe@42 185
universe@42 186 value = ucx_map_cstr_get(map, "simple");
universe@43 187 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
universe@42 188 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
universe@42 189 "to test if the buffer extension works as designed") == 0,
universe@42 190 "value error for key: simple");
universe@42 191
universe@42 192 UCX_TEST_END
universe@42 193 fclose(f);
universe@42 194
universe@42 195 unlink("test_ucx_map_store");
universe@42 196 }

mercurial