Mon, 20 Feb 2017 16:04:14 +0100
adds new test case for sstrsplit: string ends with delimiter but empty string exceeds list bound
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "map_tests.h"
31 UCX_TEST(test_ucx_map_new) {
32 UcxMap *map = ucx_map_new(16);
33 UCX_TEST_BEGIN
34 UCX_TEST_ASSERT(map->size == 16, "wrong size");
35 UCX_TEST_ASSERT(map->map != NULL, "failed");
37 UCX_TEST_END
38 ucx_map_free(map);
39 }
41 UCX_TEST(test_ucx_key) {
42 UcxKey key = ucx_key((void*)"This is a text.", 15);
43 UCX_TEST_BEGIN
44 UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0,
45 "failed");
46 UCX_TEST_ASSERT(key.len == 15, "failed");
47 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
49 UCX_TEST_END
50 }
52 UCX_TEST(test_ucx_map_put) {
54 UcxMap *map = ucx_map_new(4);
56 int td[5];
57 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
59 UCX_TEST_BEGIN
60 ucx_map_cstr_put(map, "Key2", &td[2]); /* 3.2 */
61 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0.0 */
62 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3.0 */
63 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 3.1 */
64 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 1.0 */
66 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
67 UCX_TEST_ASSERT(*((int*)map->map[1]->data) == td[4], "failed KEY4");
68 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
70 UCX_TEST_ASSERT(map->map[3]->next != NULL, "no list at slot 3");
71 UCX_TEST_ASSERT(map->map[3]->next->next != NULL, "list corrupt at slot 3");
72 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[3],
73 "failed KeY3")
74 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
75 "failed KeY2");
77 UCX_TEST_ASSERT(map->map[0]->next == NULL, "slot 0 not terminated");
78 UCX_TEST_ASSERT(map->map[1]->next == NULL, "slot 1 not terminated");
79 UCX_TEST_ASSERT(map->map[2] == NULL, "slot 2 not empty");
80 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL,
81 "slot 3 not terminated")
83 ucx_map_cstr_put(map, "KeY3", &td[4]); /* replace 3.1 */
85 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1],
86 "overwrite failed")
87 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[4],
88 "overwrite failed");
89 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
90 "overwrite failed")
91 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, "overwrite failed");
93 UCX_TEST_END
94 ucx_map_free(map);
95 }
97 UCX_TEST(test_ucx_map_get) {
98 UcxMap *map = ucx_map_new(4);
100 int td[5];
101 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
103 ucx_map_cstr_put(map, "Key2", &td[2]);
104 ucx_map_cstr_put(map, "Key0", &td[0]);
105 ucx_map_cstr_put(map, "Key1", &td[1]);
106 ucx_map_cstr_put(map, "KeY3", &td[3]);
107 ucx_map_cstr_put(map, "KEY4", &td[4]);
108 UCX_TEST_BEGIN
110 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
111 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
112 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
113 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
114 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
115 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
116 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
117 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
118 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
119 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
121 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
122 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
124 UCX_TEST_END
125 ucx_map_free(map);
126 }
128 UCX_TEST(test_ucx_map_remove) {
129 UcxMap *map = ucx_map_new(4);
131 int td[5];
132 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
134 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
135 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
136 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
137 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
138 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
139 UCX_TEST_BEGIN
141 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
142 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
143 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
144 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
145 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
146 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
147 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
148 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
149 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
150 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
152 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
153 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
154 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
155 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
156 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
157 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
159 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
160 "subsequent remove call shall return NULL");
162 UCX_TEST_END
163 ucx_map_free(map);
164 }
166 UCX_TEST(test_ucx_map_clear) {
167 UcxMap *map = ucx_map_new(4);
169 int value = 42;
171 ucx_map_cstr_put(map, "Key0", &value);
172 ucx_map_cstr_put(map, "Key1", &value);
173 ucx_map_cstr_put(map, "Key2", &value);
174 ucx_map_cstr_put(map, "Key3", &value);
175 ucx_map_cstr_put(map, "Key4", &value);
176 ucx_map_cstr_put(map, "Key5", &value);
177 ucx_map_cstr_put(map, "Key6", &value);
178 UCX_TEST_BEGIN
180 ucx_map_clear(map);
182 UCX_TEST_ASSERT(map->count == 0, "map has not been cleared");
183 UCX_TEST_ASSERT(map->size == 4, "map size has changed unexpectedly");
185 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
186 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")==NULL, "element not removed");
187 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
188 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key3")==NULL, "element not removed");
189 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key4")==NULL, "element not removed");
190 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key5")==NULL, "element not removed");
191 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key6")==NULL, "element not removed");
193 UCX_TEST_END
194 ucx_map_free(map);
195 }
197 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) {
198 int v1 = 10;
199 int v2 = 15;
200 int v3 = 7;
201 int v4 = 9;
203 ucx_map_cstr_put(map, "v1", &v1);
204 ucx_map_cstr_put(map, "v2", &v2);
205 ucx_map_cstr_put(map, "v3", &v3);
206 ucx_map_cstr_put(map, "v4", &v4);
208 UcxMapIterator i = ucx_map_iterator(map);
209 int check = 0;
210 int hit = 0;
212 void* v;
213 UCX_MAP_FOREACH(key, v, i) {
214 check += *((int*)v);
215 hit++;
216 }
218 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
219 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
220 }
222 UCX_TEST(test_ucx_map_iterator) {
223 UcxMap *map = ucx_map_new(16);
224 UCX_TEST_BEGIN
225 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
226 UCX_TEST_END
227 ucx_map_free(map);
228 }
230 UCX_TEST(test_ucx_map_iterator_chain) {
231 UcxMap *map = ucx_map_new(1);
232 UCX_TEST_BEGIN
233 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
234 UCX_TEST_END
235 ucx_map_free(map);
236 }
238 UCX_TEST(test_ucx_map_clone) {
239 UcxMap *map = ucx_map_new(4);
241 ucx_map_cstr_put(map, "key1", (void*)"value1");
242 ucx_map_cstr_put(map, "key2", (void*)"value2");
243 ucx_map_cstr_put(map, "key3", (void*)"value3");
245 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
247 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
248 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
249 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
251 UCX_TEST_BEGIN
253 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
254 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
255 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
257 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
258 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
259 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
261 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
262 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
263 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
265 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
266 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
267 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
269 UCX_TEST_END
271 ucx_map_free(map);
272 ucx_map_free(clone);
273 }
275 UCX_TEST(test_ucx_map_rehash) {
276 UcxMap *map = ucx_map_new(4);
278 char keys[10][5];
279 char values[10][7];
280 for (int i = 0 ; i < 10 ; i++) {
281 strcpy(keys[i], "key");
282 keys[i][3] = 48+i; keys[i][4] = 0;
283 strcpy(values[i], "value");
284 values[i][5] = 48+i; values[i][6] = 0;
286 ucx_map_cstr_put(map, keys[i], values[i]);
287 }
289 ucx_map_rehash(map);
291 UCX_TEST_BEGIN
292 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
293 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
294 for (int i = 0 ; i < 10 ; i++) {
295 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
296 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
297 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
298 "new map contains incorrect values");
299 }
300 ucx_map_rehash(map);
301 UCX_TEST_ASSERT(map->size == 25,
302 "subsequent rehashing call shall not change size");
303 UCX_TEST_END
305 ucx_map_free(map);
306 }