tests/test_hash_map.c

Sun, 22 Dec 2024 11:34:05 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 22 Dec 2024 11:34:05 +0100
changeset 1043
256ea5a36b5a
parent 994
3603bdf4a78b
permissions
-rw-r--r--

add function prototypes and macros for string conversion function

issue #532

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 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
45 CX_TEST_ASSERT(map->collection.elem_size == 1);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
46 CX_TEST_ASSERT(map->collection.size == 0);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
47 CX_TEST_ASSERT(map->collection.allocator == allocator);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
48 CX_TEST_ASSERT(!map->collection.store_pointer);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
49 CX_TEST_ASSERT(map->collection.cmpfunc == NULL);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
50 CX_TEST_ASSERT(map->collection.simple_destructor == NULL);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
51 CX_TEST_ASSERT(map->collection.advanced_destructor == NULL);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
52 CX_TEST_ASSERT(map->collection.destructor_data == NULL);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 cxMapStorePointers(map);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
54 CX_TEST_ASSERT(map->collection.store_pointer);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
55 CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 cxMapStoreObjects(map);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
57 CX_TEST_ASSERT(!map->collection.store_pointer);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
59 cxMapFree(map);
785
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 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
76 CX_TEST_ASSERT(map->collection.size == 0);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
77 CX_TEST_ASSERT(map->collection.allocator == allocator);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
78 CX_TEST_ASSERT(map->collection.store_pointer);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
79 CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
81 cxMapFree(map);
785
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);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
109 CX_TEST_ASSERT(map->collection.size == 10);
785
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
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
122 cxMapFree(map);
785
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
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
147 cxMapFree(map);
785
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
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
164 CX_TEST_ASSERT(map->collection.size == 3);
785
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
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
168 CX_TEST_ASSERT(map->collection.size == 0);
785
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
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
173 cxMapFree(map);
785
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
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
210 cxstring ret = {0};
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
211 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "s2", &ret));
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
212 CX_TEST_ASSERT(map->collection.size == 3);
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
213 CX_TEST_ASSERT(0 == cx_strcmp(ret, cx_str("is")));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 // iterate
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 bool s3found = false, s4found = false, s5found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 CxIterator iter = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 cx_foreach(cxstring*, s, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 s3found |= s3.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 s4found |= s4.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 s5found |= s5.ptr == s->ptr;
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 CX_TEST_ASSERT(s3found && s4found && s5found);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
225 cxMapFree(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 CX_TEST_ASSERT(cx_testing_allocator_verify(&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 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 CX_TEST(test_hash_map_remove_via_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 cxMapPut(map, "key 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
245 CxIterator iter = cxMapMutIterator(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 cx_foreach(CxMapEntry*, entry, iter) {
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
247 if (((const char *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
249 CX_TEST_ASSERT(map->collection.size == 3);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
250 CX_TEST_ASSERT(iter.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 CX_TEST_ASSERT(cxMapGet(map, "key 4") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 CX_TEST_ASSERT(cxMapGet(map, "key 5") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 CX_TEST_ASSERT(cxMapGet(map, "key 6") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
259 cxMapFree(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 CX_TEST_ASSERT(cx_testing_allocator_verify(&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 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 static void test_simple_destructor(void *data) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 static void test_advanced_destructor(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 926
diff changeset
270 cx_attr_unused void *unused,
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 void *data
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 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 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
277 CxHashKey k1 = cx_hash_key_str("key 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 CxHashKey k2 = cx_hash_key_str("key 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 CxHashKey k3 = cx_hash_key_str("key 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 CxHashKey k4 = cx_hash_key_str("key 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 CxHashKey k5 = cx_hash_key_str("key 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 char v1[] = "val 1";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 char v2[] = "val 2";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 char v3[] = "val 3";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286 char v4[] = "val 4";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 char v5[] = "val 5";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 cxMapPut(map, k1, v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 cxMapPut(map, k2, v2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 cxMapPut(map, k3, v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 cxMapPut(map, k4, v4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
294 CX_TEST_ASSERT(0 == cxMapRemove(map, k2));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
295 char *r;
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
296 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, k3, &r));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 CX_TEST_ASSERT(0 == strcmp(v4, "val 4"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 CX_TEST_ASSERT(r == v3);
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 cxMapClear(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 cxMapPut(map, k1, (void *) v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314 cxMapPut(map, k3, (void *) v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315 cxMapPut(map, k5, (void *) v5);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
316
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
318 CxIterator iter = cxMapMutIteratorKeys(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319 cx_foreach(CxHashKey*, key, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter);
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 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
324 CxIterator iter = cxMapMutIteratorValues(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325 cx_foreach(char*, v, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
326 if (v[4] == '5') cxIteratorFlagRemoval(iter);
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 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330 CX_TEST_ASSERT(0 == strcmp(v1, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 CX_TEST_ASSERT(0 == strcmp(v5, "OK"));
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 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
337
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
338 cxMapFree(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
339
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340 CX_TEST_ASSERT(0 == strcmp(v1, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
341 CX_TEST_ASSERT(0 == strcmp(v2, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 CX_TEST_ASSERT(0 == strcmp(v3, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 CX_TEST_ASSERT(0 == strcmp(v4, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344 CX_TEST_ASSERT(0 == strcmp(v5, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
346
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 CX_TEST(test_hash_map_simple_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
348 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
351 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
352 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
353 map->collection.simple_destructor = test_simple_destructor;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
355 CX_TEST_ASSERT(cx_testing_allocator_verify(&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 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 CX_TEST(test_hash_map_advanced_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
366 map->collection.advanced_destructor = test_advanced_destructor;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 CX_TEST_ASSERT(cx_testing_allocator_verify(&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 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373 CX_TEST(test_empty_map_size) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374 CX_TEST_DO {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
375 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
376 CX_TEST_ASSERT(cxMapSize(cxEmptyMap) == 0);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378 }
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 CX_TEST(test_empty_map_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381 CxMap *map = cxEmptyMap;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
382
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 CxIterator it1 = cxMapIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
384 CxIterator it2 = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
385 CxIterator it3 = cxMapIteratorKeys(map);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
386 CxIterator it4 = cxMapMutIterator(map);
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
387 CxIterator it5 = cxMapMutIteratorValues(map);
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
388 CxIterator it6 = cxMapMutIteratorKeys(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 CX_TEST_ASSERT(!cxIteratorValid(it1));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
392 CX_TEST_ASSERT(!cxIteratorValid(it2));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 CX_TEST_ASSERT(!cxIteratorValid(it3));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394 CX_TEST_ASSERT(!cxIteratorValid(it4));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395 CX_TEST_ASSERT(!cxIteratorValid(it5));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
396 CX_TEST_ASSERT(!cxIteratorValid(it6));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 int c = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 cx_foreach(void*, data, it1) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400 cx_foreach(void*, data, it2) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 cx_foreach(void*, data, it3) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 cx_foreach(void*, data, it4) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 cx_foreach(void*, data, it5) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 cx_foreach(void*, data, it6) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 CX_TEST_ASSERT(c == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 CX_TEST(test_empty_map_no_ops) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
411 // assertion not possible
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412 // 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
413 cxMapClear(cxEmptyMap);
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
414 cxMapFree(cxEmptyMap);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415 CX_TEST_ASSERT(true);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
418
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
419 CX_TEST(test_empty_map_get) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
420 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
421 CxHashKey key = cx_hash_key_str("test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
422 CX_TEST_ASSERT(cxMapGet(cxEmptyMap, key) == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
423 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
424 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
426 CX_TEST(test_hash_map_generics) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
427 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430 CX_TEST_DO {
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
431 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
432 cxMapPut(map, "test", "test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
433 cxMapPut(map, cx_mutstr("foo"), "bar");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
434 cxMapPut(map, cx_str("hallo"), "welt");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
435
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
436 CX_TEST_ASSERT(map->collection.size == 3);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
437 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
438 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
439 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
440
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
441 CX_TEST_ASSERT(0 == cxMapRemove(map, cx_str("test")));
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
442 const char *hallo = "hallo";
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
443 CX_TEST_ASSERT(0 == cxMapRemove(map, hallo));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
444 cxMapPut(map, cx_hash_key_str("key"), "value");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
445
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
446 CX_TEST_ASSERT(map->collection.size == 2);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
447 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
448 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
449
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
450 const char *r1, *r2;
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
451 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "key", &r1));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
452 CX_TEST_ASSERT(0 == strcmp(r1, "value"));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
453 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, cx_str("foo"), &r2));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
454 CX_TEST_ASSERT(0 == strcmp(r2, "bar"));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
455 r2 = "nope";
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
456 CX_TEST_ASSERT(0 != cxMapRemoveAndGet(map, cx_hash_key("notfound",9), &r2));
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
457 CX_TEST_ASSERT(0 == strcmp(r2, "nope"));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
458
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
459 CX_TEST_ASSERT(map->collection.size == 0);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
460
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
461 cxMapFree(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
462 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
463 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
464 cx_testing_allocator_destroy(&talloc);
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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
467 struct test_map_kv {
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
468 const char *key;
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
469 const char *value;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
470 };
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
471
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
472 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
473 {"key 1", "test"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
474 {"key 2", "blub"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
475 {"key 3", "hallo"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
476 {"key 2", "foobar"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
477 {"key 4", "value 4"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
478 {"key 5", "value 5"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
479 {"key 6", "value 6"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
480 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481 {"key 7", "value 7"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
482 {"key 8", "value 8"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
483 {"does not exist", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
484 {"key 9", "value 9"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
485 {"key 6", "other value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
486 {"key 7", "something else"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
487 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
488 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
489 {"key 8", "new value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
490 };
926
8fdd8d78c14b fix several survivors of east-const and some missing consts
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
491 static const size_t test_map_operations_len =
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
492 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
493 static struct test_map_kv test_map_reference[] = {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
494 {"key 1", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
495 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
496 {"key 3", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
497 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
498 {"key 5", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
499 {"key 6", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
500 {"key 7", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
501 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
502 {"key 9", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
503 };
926
8fdd8d78c14b fix several survivors of east-const and some missing consts
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
504 static const size_t test_map_reference_len =
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
505 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
506
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
507 static void test_map_reference_put(const char *key, const char *value) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
508 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
509 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
510 test_map_reference[i].value = value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
511 return;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
512 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
513 }
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
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
516 static const char *test_map_reference_get(const char *key) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
517 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
518 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
519 return test_map_reference[i].value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
520 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
521 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
522 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
523 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
524
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
525 static const char *test_map_reference_remove(const char *key) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
526 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
527 if (0 == strcmp(key, test_map_reference[i].key)) {
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
528 const char *ret = test_map_reference[i].value;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
529 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
530 return ret;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
531 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
532 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
533 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
534 }
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 static size_t test_map_reference_size(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
537 size_t size = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
538 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
539 if (test_map_reference[i].value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
540 size++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
541 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
542 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
543 return size;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
544 }
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 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
547 // verify that the reference map has same size (i.e. no other keys are mapped)
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
548 CX_TEST_ASSERT(map->collection.size == test_map_reference_size());
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
549
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
550 // verify key iterator
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 // collect the keys from the map iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
553 CxIterator keyiter = cxMapIteratorKeys(map);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
554 CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey));
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
555 CX_TEST_ASSERT(keyiter.elem_count == map->collection.size);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
556 CxHashKey *keys = calloc(map->collection.size, sizeof(CxHashKey));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
557 cx_foreach(CxHashKey*, elem, keyiter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
558 keys[keyiter.index] = *elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
559 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
560 CX_TEST_ASSERT(keyiter.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
561 // verify that all keys are mapped to values in reference map
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
562 for (size_t i = 0 ; i < map->collection.size ; i++) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
563 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
564 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
565 cx_strfree(&ksz);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
566 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
567 free(keys);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
568 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
569
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
570 // verify value iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
571 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
572 // 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
573 // 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
574 CxIterator valiter = cxMapIteratorValues(map);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
575 CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
576 CX_TEST_ASSERT(valiter.elem_count == map->collection.size);
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
577 const char ** values = calloc(map->collection.size, sizeof(const char *));
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
578 cx_foreach(const char *, elem, valiter) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
579 values[valiter.index] = elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
580 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
581 CX_TEST_ASSERT(valiter.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
582 // verify that all values are present in the reference map
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
583 for (size_t i = 0 ; i < map->collection.size ; i++) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
584 bool found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
585 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
586 if (test_map_reference[j].value == values[i]) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
587 found = true;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
588 break;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
589 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
590 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
591 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
592 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
593 free(values);
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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
596 // verify pair iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
597 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
598 CxIterator pairiter = cxMapIterator(map);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
599 CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry));
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
600 CX_TEST_ASSERT(pairiter.elem_count == map->collection.size);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
601 struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv));
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
602 cx_foreach(CxMapEntry*, entry, pairiter) {
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
603 const CxHashKey *key = entry->key;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
604 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
605 pairs[pairiter.index].value = entry->value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
606 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
607 CX_TEST_ASSERT(pairiter.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
608 // verify that all pairs are present in the reference map
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
609 for (size_t i = 0 ; i < map->collection.size ; i++) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
610 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
611 // this was strdup'ed
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
612 free((void*)pairs[i].key);
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 free(pairs);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
615 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
616 }
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 CX_TEST(test_hash_map_basic_operations) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
619 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
620 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
621 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
622 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
623 // create the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
624 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
625
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
626 // clear the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
627 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
628 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
629 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
630
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
631 // verify iterators for empty map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
632 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
633
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
634 // execute operations and verify results
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
635 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
636 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
637 CxHashKey key = cx_hash_key_str(kv.key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
638 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
639 if (kv.value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
640 // 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
641 test_map_reference_put(kv.key, kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
642 int result = cxMapPut(map, key, (void *) kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
643 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
644 void *added = cxMapGet(map, key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
645 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
646 } else {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
647 // execute a remove and verify that the removed element was returned (or NULL)
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
648 const char *found = test_map_reference_remove(kv.key);
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
649 void *removed = (void*) 0x1337;
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
650 int result = cxMapRemoveAndGet(map, key, &removed);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
651 if (found == NULL) {
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
652 CX_TEST_ASSERT(0 != result);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
653 CX_TEST_ASSERT(removed == (void*) 0x1337);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
654 } else {
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
655 CX_TEST_ASSERT(0 == result);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
656 CX_TEST_ASSERT(removed == found);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
657 }
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
658
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
659 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
660 // 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
661 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
662 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
663
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
664 // destroy the map and verify the memory (de)allocations
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
665 cxMapFree(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
666 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
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 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
669 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
670
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
671 CxTestSuite *cx_test_suite_hash_map(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
672 CxTestSuite *suite = cx_test_suite_new("map");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
673
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
674 cx_test_register(suite, test_hash_map_create);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
675 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
676 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
677 cx_test_register(suite, test_hash_map_rehash);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
678 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
679 cx_test_register(suite, test_hash_map_clear);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
680 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
681 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
682 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
683 cx_test_register(suite, test_empty_map_size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
684 cx_test_register(suite, test_empty_map_get);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
685 cx_test_register(suite, test_empty_map_iterator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
686 cx_test_register(suite, test_hash_map_generics);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
687
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
688 return suite;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
689 }

mercurial