tests/test_hash_map.c

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 785
bb18daa62d5f
child 850
b2bc48c2b251
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/test.h"
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "util_allocator.h"
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "cx/hash_map.h"
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 CX_TEST(test_hash_map_create) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 CxMap *map = cxHashMapCreate(allocator, 1, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 CX_TEST_ASSERT(hmap->bucket_count > 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 for(size_t i = 0 ; i < hmap->bucket_count ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 CX_TEST_ASSERT(hmap->buckets[i] == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 CX_TEST_ASSERT(map->item_size == 1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 CX_TEST_ASSERT(map->size == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 CX_TEST_ASSERT(map->allocator == allocator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 CX_TEST_ASSERT(!map->store_pointer);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CX_TEST_ASSERT(map->cmpfunc == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 CX_TEST_ASSERT(map->simple_destructor == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 CX_TEST_ASSERT(map->advanced_destructor == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_TEST_ASSERT(map->destructor_data == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 cxMapStorePointers(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_TEST_ASSERT(map->store_pointer);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 CX_TEST_ASSERT(map->item_size == sizeof(void *));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 cxMapStoreObjects(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_TEST_ASSERT(!map->store_pointer);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 CX_TEST(test_hash_map_create_store_pointers) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 CX_TEST_ASSERT(hmap->bucket_count > 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 for (size_t i = 0; i < hmap->bucket_count; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_TEST_ASSERT(hmap->buckets[i] == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 CX_TEST_ASSERT(map->size == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CX_TEST_ASSERT(map->allocator == allocator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 CX_TEST_ASSERT(map->store_pointer);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 CX_TEST_ASSERT(map->item_size == sizeof(void *));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 CX_TEST(test_hash_map_rehash) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 7);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 cxMapPut(map, "foo 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 cxMapPut(map, "bar 7", (void *) "val 7");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 cxMapPut(map, "key 8", (void *) "val 8");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 cxMapPut(map, "key 9", (void *) "val 9");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 cxMapPut(map, "key 10", (void *) "val 10");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 7);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 int result = cxMapRehash(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 25);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 CX_TEST_ASSERT(map->size == 10);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 1"), "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 2"), "val 2"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 3"), "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo 4"), "val 4"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 5"), "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 6"), "val 6"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "bar 7"), "val 7"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 8"), "val 8"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 9"), "val 9"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 10"), "val 10"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CX_TEST(test_hash_map_rehash_not_required) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 cxMapPut(map, "key 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 // 6/8 does not exceed 0.75, therefore the function should not rehash
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 int result = cxMapRehash(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 CX_TEST(test_hash_map_clear) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 CX_TEST_ASSERT(map->size == 3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 cxMapClear(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 CX_TEST_ASSERT(map->size == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 CX_TEST_ASSERT(cxMapGet(map, "key 2") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 CX_TEST(test_hash_map_store_ucx_strings) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 // create the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 // define some strings
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 cxstring s1 = CX_STR("this");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 cxstring s2 = CX_STR("is");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 cxstring s3 = CX_STR("a");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 cxstring s4 = CX_STR("test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 cxstring s5 = CX_STR("setup");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 // put them into the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 cxMapPut(map, "s1", &s1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 cxMapPut(map, "s2", &s2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 cxMapPut(map, "s3", &s3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 cxMapPut(map, "s4", &s4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 // overwrite a value
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 cxMapPut(map, "s1", &s5);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 // look up a string
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 cxstring * s3p = cxMapGet(map, "s3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 CX_TEST_ASSERT(s3p->length == s3.length);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 CX_TEST_ASSERT(s3p->ptr == s3.ptr);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 CX_TEST_ASSERT(s3p != &s3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 // remove a string
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 cxMapRemove(map, "s2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 CX_TEST_ASSERT(map->size == 3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 // iterate
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 bool s3found = false, s4found = false, s5found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 CxIterator iter = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 cx_foreach(cxstring*, s, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 s3found |= s3.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 s4found |= s4.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 s5found |= s5.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 CX_TEST_ASSERT(s3found && s4found && s5found);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 CX_TEST(test_hash_map_remove_via_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 cxMapPut(map, "key 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 CxMutIterator iter = cxMapMutIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 cx_foreach(CxMapEntry*, entry, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 if (((char const *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 CX_TEST_ASSERT(map->size == 3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 CX_TEST_ASSERT(iter.index == map->size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 CX_TEST_ASSERT(cxMapGet(map, "key 4") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 CX_TEST_ASSERT(cxMapGet(map, "key 5") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 CX_TEST_ASSERT(cxMapGet(map, "key 6") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 static void test_simple_destructor(void *data) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 static void test_advanced_destructor(
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 __attribute__((__unused__)) void *unused,
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 void *data
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 ) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 static CX_TEST_SUBROUTINE(verify_any_destructor, CxMap *map) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 CxHashKey k1 = cx_hash_key_str("key 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 CxHashKey k2 = cx_hash_key_str("key 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 CxHashKey k3 = cx_hash_key_str("key 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 CxHashKey k4 = cx_hash_key_str("key 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 CxHashKey k5 = cx_hash_key_str("key 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 char v1[] = "val 1";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 char v2[] = "val 2";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 char v3[] = "val 3";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 char v4[] = "val 4";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 char v5[] = "val 5";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 cxMapPut(map, k1, v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 cxMapPut(map, k2, v2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 cxMapPut(map, k3, v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 cxMapPut(map, k4, v4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 cxMapRemove(map, k2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 char *r = cxMapRemoveAndGet(map, k3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 cxMapDetach(map, k1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 CX_TEST_ASSERT(0 == strcmp(v4, "val 4"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 CX_TEST_ASSERT(r == v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 cxMapClear(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
304
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 cxMapPut(map, k1, (void *) v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312 cxMapPut(map, k3, (void *) v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 cxMapPut(map, k5, (void *) v5);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
316 CxMutIterator iter = cxMapMutIteratorKeys(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 cx_foreach(CxHashKey*, key, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
318 if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
322 CxMutIterator iter = cxMapMutIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 cx_foreach(char*, v, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324 if (v[4] == '5') cxIteratorFlagRemoval(iter);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
326 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328 CX_TEST_ASSERT(0 == strcmp(v1, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332 CX_TEST_ASSERT(0 == strcmp(v5, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 v1[0] = v2[0] = v4[0] = v5[0] = 'c';
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
336 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
337
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
338 CX_TEST_ASSERT(0 == strcmp(v1, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
339 CX_TEST_ASSERT(0 == strcmp(v2, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340 CX_TEST_ASSERT(0 == strcmp(v3, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
341 CX_TEST_ASSERT(0 == strcmp(v4, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 CX_TEST_ASSERT(0 == strcmp(v5, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345 CX_TEST(test_hash_map_simple_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
346 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
348 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
351 map->simple_destructor = test_simple_destructor;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
352 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
353 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
355 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
356 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
357
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 CX_TEST(test_hash_map_advanced_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 map->advanced_destructor = test_advanced_destructor;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
370
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 CX_TEST(test_empty_map_size) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373 CX_TEST_ASSERT(cxEmptyMap->size == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
376
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 CX_TEST(test_empty_map_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378 CxMap *map = cxEmptyMap;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
379
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380 CxIterator it1 = cxMapIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381 CxIterator it2 = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
382 CxIterator it3 = cxMapIteratorKeys(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 CxMutIterator it4 = cxMapMutIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
384 CxMutIterator it5 = cxMapMutIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
385 CxMutIterator it6 = cxMapMutIteratorKeys(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
386
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
387 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
388 CX_TEST_ASSERT(!cxIteratorValid(it1));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389 CX_TEST_ASSERT(!cxIteratorValid(it2));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 CX_TEST_ASSERT(!cxIteratorValid(it3));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 CX_TEST_ASSERT(!cxIteratorValid(it4));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
392 CX_TEST_ASSERT(!cxIteratorValid(it5));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 CX_TEST_ASSERT(!cxIteratorValid(it6));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395 int c = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
396 cx_foreach(void*, data, it1) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397 cx_foreach(void*, data, it2) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 cx_foreach(void*, data, it3) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 cx_foreach(void*, data, it4) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400 cx_foreach(void*, data, it5) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 cx_foreach(void*, data, it6) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 CX_TEST_ASSERT(c == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406 CX_TEST(test_empty_map_no_ops) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408 // assertion not possible
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 // test that no segfault happens and valgrind is happy
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410 cxMapClear(cxEmptyMap);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
411 cxMapDestroy(cxEmptyMap);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412 CX_TEST_ASSERT(true);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
413 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
414 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416 CX_TEST(test_empty_map_get) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
418 CxHashKey key = cx_hash_key_str("test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
419 CX_TEST_ASSERT(cxMapGet(cxEmptyMap, key) == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
420 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
421 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
422
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
423 CX_TEST(test_hash_map_generics) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
424 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
426 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
427 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429 cxMapPut(map, "test", "test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430 cxMapPut(map, cx_mutstr("foo"), "bar");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
431 cxMapPut(map, cx_str("hallo"), "welt");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
432
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
433 CX_TEST_ASSERT(map->size == 3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
434 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
435 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
436 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
437
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
438 // note: we don't have a destructor here, so remove and detach are the same
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
439 cxMapRemove(map, cx_str("test"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
440 char const *hallo = "hallo";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
441 cxMapDetach(map, hallo);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
442 cxMapPut(map, cx_hash_key_str("key"), "value");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
443
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
444 CX_TEST_ASSERT(map->size == 2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
445 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
446 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
447
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
448 void *r;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
449 r = cxMapRemoveAndGet(map, "key");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
450 r = cxMapRemoveAndGet(map, cx_str("foo"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451 if (r != NULL) map->size = 47;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
452
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
453 CX_TEST_ASSERT(map->size == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
454
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
455 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
456 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
457 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
458 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
459 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
460
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
461 struct test_map_kv {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
462 char const *key;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
463 char const *value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
464 };
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
465
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
466 static struct test_map_kv const test_map_operations[] = {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
467 {"key 1", "test"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
468 {"key 2", "blub"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
469 {"key 3", "hallo"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
470 {"key 2", "foobar"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
471 {"key 4", "value 4"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
472 {"key 5", "value 5"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
473 {"key 6", "value 6"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
474 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
475 {"key 7", "value 7"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
476 {"key 8", "value 8"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
477 {"does not exist", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
478 {"key 9", "value 9"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
479 {"key 6", "other value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
480 {"key 7", "something else"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
482 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
483 {"key 8", "new value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
484 };
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
485 static size_t const test_map_operations_len =
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
486 sizeof(test_map_operations) / sizeof(struct test_map_kv);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
487 static struct test_map_kv test_map_reference[] = {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
488 {"key 1", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
489 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
490 {"key 3", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
491 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
492 {"key 5", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
493 {"key 6", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
494 {"key 7", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
495 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
496 {"key 9", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
497 };
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
498 static size_t const test_map_reference_len =
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
499 sizeof(test_map_reference) / sizeof(struct test_map_kv);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
500
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
501 static void test_map_reference_put(char const *key, char const* value) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
502 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
503 if (0 == strcmp(key, test_map_reference[i].key)) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
504 test_map_reference[i].value = value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
505 return;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
506 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
507 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
508 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
509
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
510 static char const *test_map_reference_get(char const *key) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
511 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
512 if (0 == strcmp(key, test_map_reference[i].key)) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
513 return test_map_reference[i].value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
514 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
515 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
516 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
517 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
518
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
519 static char const *test_map_reference_remove(char const *key) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
520 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
521 if (0 == strcmp(key, test_map_reference[i].key)) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
522 char const *ret = test_map_reference[i].value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
523 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
524 return ret;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
525 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
526 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
527 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
528 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
529
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
530 static size_t test_map_reference_size(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
531 size_t size = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
532 for (size_t i = 0; i < test_map_reference_len; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
533 if (test_map_reference[i].value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
534 size++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
535 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
536 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
537 return size;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
538 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
539
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
540 static CX_TEST_SUBROUTINE(verify_map_contents, CxMap *map) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
541 // verify that the reference map has same size (i.e. no other keys are mapped)
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
542 CX_TEST_ASSERT(map->size == test_map_reference_size());
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
543
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
544 // verify key iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
545 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
546 // collect the keys from the map iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
547 CxIterator keyiter = cxMapIteratorKeys(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
548 CxHashKey *keys = calloc(map->size, sizeof(CxHashKey));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
549 cx_foreach(CxHashKey*, elem, keyiter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
550 keys[keyiter.index] = *elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
551 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
552 CX_TEST_ASSERT(keyiter.index == map->size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
553 // verify that all keys are mapped to values in reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
554 for (size_t i = 0 ; i < map->size ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
555 cxmutstr ksz = cx_strdup(cx_strn(keys[i].data, keys[i].len));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
556 CX_TEST_ASSERT(test_map_reference_get(ksz.ptr) != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
557 cx_strfree(&ksz);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
558 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
559 free(keys);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
560 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
561
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
562 // verify value iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
563 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
564 // by using that the values in our test data are unique strings
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
565 // we can re-use a similar approach as above
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
566 CxIterator valiter = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
567 char const** values = calloc(map->size, sizeof(char const*));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
568 cx_foreach(char const*, elem, valiter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
569 values[valiter.index] = elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
570 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
571 CX_TEST_ASSERT(valiter.index == map->size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
572 // verify that all values are present in the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
573 for (size_t i = 0 ; i < map->size ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
574 bool found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
575 for (size_t j = 0; j < test_map_reference_len ; j++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
576 if (test_map_reference[j].value == values[i]) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
577 found = true;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
578 break;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
579 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
580 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
581 CX_TEST_ASSERTM(found, "A value was not found in the reference map");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
582 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
583 free(values);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
584 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
585
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
586 // verify pair iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
587 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
588 CxIterator pairiter = cxMapIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
589 struct test_map_kv *pairs = calloc(map->size, sizeof(struct test_map_kv));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
590 cx_foreach(CxMapEntry*, entry, pairiter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
591 CxHashKey const *key = entry->key;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
592 pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
593 pairs[pairiter.index].value = entry->value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
594 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
595 CX_TEST_ASSERT(pairiter.index == map->size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
596 // verify that all pairs are present in the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
597 for (size_t i = 0 ; i < map->size ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
598 CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
599 // this was strdup'ed
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
600 free((void*)pairs[i].key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
601 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
602 free(pairs);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
603 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
604 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
605
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
606 CX_TEST(test_hash_map_basic_operations) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
607 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
608 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
609 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
610 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
611 // create the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
612 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
613
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
614 // clear the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
615 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
616 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
617 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
618
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
619 // verify iterators for empty map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
620 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
621
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
622 // execute operations and verify results
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
623 for (size_t i = 0 ; i < test_map_operations_len ; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
624 struct test_map_kv kv = test_map_operations[i];
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
625 CxHashKey key = cx_hash_key_str(kv.key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
626 key.hash = 0; // force the hash map to compute the hash
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
627 if (kv.value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
628 // execute a put operation and verify that the exact value can be read back
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
629 test_map_reference_put(kv.key, kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
630 int result = cxMapPut(map, key, (void *) kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
631 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
632 void *added = cxMapGet(map, key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
633 CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value)));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
634 } else {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
635 // execute a remove and verify that the removed element was returned (or NULL)
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
636 char const *found = test_map_reference_remove(kv.key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
637 void *removed = cxMapRemoveAndGet(map, key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
638 CX_TEST_ASSERT(found == removed);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
639 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
640 // compare the current map state with the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
641 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
642 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
643
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
644 // destroy the map and verify the memory (de)allocations
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
645 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
646 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
647 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
648 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
649 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
650
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
651 CxTestSuite *cx_test_suite_hash_map(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
652 CxTestSuite *suite = cx_test_suite_new("map");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
653
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
654 cx_test_register(suite, test_hash_map_create);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
655 cx_test_register(suite, test_hash_map_create_store_pointers);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
656 cx_test_register(suite, test_hash_map_basic_operations);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
657 cx_test_register(suite, test_hash_map_rehash);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
658 cx_test_register(suite, test_hash_map_rehash_not_required);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
659 cx_test_register(suite, test_hash_map_clear);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
660 cx_test_register(suite, test_hash_map_store_ucx_strings);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
661 cx_test_register(suite, test_hash_map_remove_via_iterator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
662 cx_test_register(suite, test_empty_map_no_ops);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
663 cx_test_register(suite, test_empty_map_size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
664 cx_test_register(suite, test_empty_map_get);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
665 cx_test_register(suite, test_empty_map_iterator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
666 cx_test_register(suite, test_hash_map_generics);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
667
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
668 return suite;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
669 }

mercurial