tests/test_hash_map.c

Tue, 22 Oct 2024 12:23:43 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 22 Oct 2024 12:23:43 +0200
changeset 943
9f1eb30dbc84
parent 926
8fdd8d78c14b
permissions
-rw-r--r--

fix memory leak in case of json parser errors

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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 CX_TEST(test_hash_map_create_store_pointers) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 CX_TEST_ASSERT(hmap->bucket_count > 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 for (size_t i = 0; i < hmap->bucket_count; i++) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_TEST_ASSERT(hmap->buckets[i] == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 }
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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 CX_TEST(test_hash_map_rehash) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 7);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 cxMapPut(map, "foo 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 cxMapPut(map, "bar 7", (void *) "val 7");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 cxMapPut(map, "key 8", (void *) "val 8");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 cxMapPut(map, "key 9", (void *) "val 9");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 cxMapPut(map, "key 10", (void *) "val 10");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 7);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 int result = cxMapRehash(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 25);
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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CX_TEST(test_hash_map_rehash_not_required) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 cxMapPut(map, "key 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 // 6/8 does not exceed 0.75, therefore the function should not rehash
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 int result = cxMapRehash(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 CX_TEST(test_hash_map_clear) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163
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
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 CX_TEST(test_hash_map_store_ucx_strings) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 // create the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 // define some strings
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 cxstring s1 = CX_STR("this");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 cxstring s2 = CX_STR("is");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 cxstring s3 = CX_STR("a");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 cxstring s4 = CX_STR("test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 cxstring s5 = CX_STR("setup");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 // put them into the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 cxMapPut(map, "s1", &s1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 cxMapPut(map, "s2", &s2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 cxMapPut(map, "s3", &s3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 cxMapPut(map, "s4", &s4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 // overwrite a value
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 cxMapPut(map, "s1", &s5);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 // look up a string
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 cxstring * s3p = cxMapGet(map, "s3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 CX_TEST_ASSERT(s3p->length == s3.length);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 CX_TEST_ASSERT(s3p->ptr == s3.ptr);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 CX_TEST_ASSERT(s3p != &s3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 // remove a string
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 cxMapRemove(map, "s2");
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
211 CX_TEST_ASSERT(map->collection.size == 3);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 // iterate
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 bool s3found = false, s4found = false, s5found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 CxIterator iter = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 cx_foreach(cxstring*, s, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 s3found |= s3.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 s4found |= s4.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 s5found |= s5.ptr == s->ptr;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 CX_TEST_ASSERT(s3found && s4found && s5found);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 CX_TEST(test_hash_map_remove_via_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 cxMapPut(map, "key 1", (void *) "val 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 cxMapPut(map, "key 2", (void *) "val 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 cxMapPut(map, "key 3", (void *) "val 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 cxMapPut(map, "key 4", (void *) "val 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 cxMapPut(map, "key 5", (void *) "val 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 cxMapPut(map, "key 6", (void *) "val 6");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
243 CxIterator iter = cxMapMutIterator(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 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
245 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
246 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
247 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
248 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
249
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 CX_TEST_ASSERT(cxMapGet(map, "key 4") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 CX_TEST_ASSERT(cxMapGet(map, "key 5") == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 CX_TEST_ASSERT(cxMapGet(map, "key 6") != NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 static void test_simple_destructor(void *data) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 static void test_advanced_destructor(
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 __attribute__((__unused__)) void *unused,
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 void *data
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 ) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 strcpy(data, "OK");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 static CX_TEST_SUBROUTINE(verify_any_destructor, CxMap *map) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 CxHashKey k1 = cx_hash_key_str("key 1");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 CxHashKey k2 = cx_hash_key_str("key 2");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 CxHashKey k3 = cx_hash_key_str("key 3");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 CxHashKey k4 = cx_hash_key_str("key 4");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 CxHashKey k5 = cx_hash_key_str("key 5");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 char v1[] = "val 1";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 char v2[] = "val 2";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 char v3[] = "val 3";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 char v4[] = "val 4";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 char v5[] = "val 5";
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 cxMapPut(map, k1, v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 cxMapPut(map, k2, v2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 cxMapPut(map, k3, v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 cxMapPut(map, k4, v4);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 cxMapRemove(map, k2);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 char *r = cxMapRemoveAndGet(map, k3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 cxMapDetach(map, k1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 CX_TEST_ASSERT(0 == strcmp(v4, "val 4"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 CX_TEST_ASSERT(r == v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 cxMapClear(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
304
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 CX_TEST_ASSERT(0 == strcmp(v5, "val 5"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 cxMapPut(map, k1, (void *) v1);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312 cxMapPut(map, k3, (void *) v3);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 cxMapPut(map, k5, (void *) v5);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315 {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
316 CxIterator iter = cxMapMutIteratorKeys(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 cx_foreach(CxHashKey*, key, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
318 if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321 {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
322 CxIterator iter = cxMapMutIteratorValues(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 cx_foreach(char*, v, iter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324 if (v[4] == '5') cxIteratorFlagRemoval(iter);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
326 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328 CX_TEST_ASSERT(0 == strcmp(v1, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 CX_TEST_ASSERT(0 == strcmp(v4, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332 CX_TEST_ASSERT(0 == strcmp(v5, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 v1[0] = v2[0] = v4[0] = v5[0] = 'c';
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
336 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
337
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
338 CX_TEST_ASSERT(0 == strcmp(v1, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
339 CX_TEST_ASSERT(0 == strcmp(v2, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340 CX_TEST_ASSERT(0 == strcmp(v3, "OK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
341 CX_TEST_ASSERT(0 == strcmp(v4, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 CX_TEST_ASSERT(0 == strcmp(v5, "cK"));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345 CX_TEST(test_hash_map_simple_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
346 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
348 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
351 map->collection.simple_destructor = test_simple_destructor;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
352 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
353 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
355 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
356 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
357
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 CX_TEST(test_hash_map_advanced_destructor) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
364 map->collection.advanced_destructor = test_advanced_destructor;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
370
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 CX_TEST(test_empty_map_size) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372 CX_TEST_DO {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
373 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
374 CX_TEST_ASSERT(cxMapSize(cxEmptyMap) == 0);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
376 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378 CX_TEST(test_empty_map_iterator) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
379 CxMap *map = cxEmptyMap;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381 CxIterator it1 = cxMapIterator(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
382 CxIterator it2 = cxMapIteratorValues(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 CxIterator it3 = cxMapIteratorKeys(map);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
384 CxIterator it4 = cxMapMutIterator(map);
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
385 CxIterator it5 = cxMapMutIteratorValues(map);
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
386 CxIterator it6 = cxMapMutIteratorKeys(map);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
387
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
388 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389 CX_TEST_ASSERT(!cxIteratorValid(it1));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 CX_TEST_ASSERT(!cxIteratorValid(it2));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 CX_TEST_ASSERT(!cxIteratorValid(it3));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
392 CX_TEST_ASSERT(!cxIteratorValid(it4));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 CX_TEST_ASSERT(!cxIteratorValid(it5));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394 CX_TEST_ASSERT(!cxIteratorValid(it6));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
396 int c = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397 cx_foreach(void*, data, it1) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 cx_foreach(void*, data, it2) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 cx_foreach(void*, data, it3) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400 cx_foreach(void*, data, it4) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 cx_foreach(void*, data, it5) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 cx_foreach(void*, data, it6) c++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 CX_TEST_ASSERT(c == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 CX_TEST(test_empty_map_no_ops) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 // assertion not possible
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410 // 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
411 cxMapClear(cxEmptyMap);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412 cxMapDestroy(cxEmptyMap);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
413 CX_TEST_ASSERT(true);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
414 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417 CX_TEST(test_empty_map_get) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
418 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
419 CxHashKey key = cx_hash_key_str("test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
420 CX_TEST_ASSERT(cxMapGet(cxEmptyMap, key) == NULL);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
421 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
422 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
423
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
424 CX_TEST(test_hash_map_generics) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
426 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
427 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429 CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430 cxMapPut(map, "test", "test");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
431 cxMapPut(map, cx_mutstr("foo"), "bar");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
432 cxMapPut(map, cx_str("hallo"), "welt");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
433
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
434 CX_TEST_ASSERT(map->collection.size == 3);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
435 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
436 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
437 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
438
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
439 // note: we don't have a destructor here, so remove and detach are the same
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
440 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
441 const char *hallo = "hallo";
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
442 cxMapDetach(map, hallo);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
443 cxMapPut(map, cx_hash_key_str("key"), "value");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
444
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
445 CX_TEST_ASSERT(map->collection.size == 2);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
446 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
447 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
448
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
449 void *r;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
450 r = cxMapRemoveAndGet(map, "key");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451 r = cxMapRemoveAndGet(map, cx_str("foo"));
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
452 if (r != NULL) map->collection.size = 47;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
453
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
454 CX_TEST_ASSERT(map->collection.size == 0);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
455
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
456 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
457 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
458 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
459 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
460 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
461
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
462 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
463 const char *key;
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
464 const char *value;
785
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 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
468 {"key 1", "test"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
469 {"key 2", "blub"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
470 {"key 3", "hallo"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
471 {"key 2", "foobar"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
472 {"key 4", "value 4"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
473 {"key 5", "value 5"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
474 {"key 6", "value 6"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
475 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
476 {"key 7", "value 7"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
477 {"key 8", "value 8"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
478 {"does not exist", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
479 {"key 9", "value 9"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
480 {"key 6", "other value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481 {"key 7", "something else"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
482 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
483 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
484 {"key 8", "new value"},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
485 };
926
8fdd8d78c14b fix several survivors of east-const and some missing consts
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
486 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
487 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
488 static struct test_map_kv test_map_reference[] = {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
489 {"key 1", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
490 {"key 2", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
491 {"key 3", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
492 {"key 4", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
493 {"key 5", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
494 {"key 6", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
495 {"key 7", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
496 {"key 8", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
497 {"key 9", NULL},
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
498 };
926
8fdd8d78c14b fix several survivors of east-const and some missing consts
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
499 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
500 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
501
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
502 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
503 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
504 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
505 test_map_reference[i].value = value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
506 return;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
507 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
508 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
509 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
510
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
511 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
512 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
513 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
514 return test_map_reference[i].value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
515 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
516 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
517 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
518 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
519
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
520 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
521 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
522 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
523 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
524 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
525 return ret;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
526 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
527 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
528 return NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
529 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
530
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
531 static size_t test_map_reference_size(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
532 size_t size = 0;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
533 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
534 if (test_map_reference[i].value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
535 size++;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
536 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
537 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
538 return size;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
539 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
540
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
541 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
542 // 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
543 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
544
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
545 // verify key iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
546 {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
547 // collect the keys from the map iterator
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
548 CxIterator keyiter = cxMapIteratorKeys(map);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
549 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
550 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
551 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
552 cx_foreach(CxHashKey*, elem, keyiter) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
553 keys[keyiter.index] = *elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
554 }
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.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
556 // 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
557 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
558 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
559 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
560 cx_strfree(&ksz);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
561 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
562 free(keys);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
563 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
564
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
565 // verify value iterator
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 // 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
568 // 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
569 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
570 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
571 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
572 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
573 cx_foreach(const char *, elem, valiter) {
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
574 values[valiter.index] = elem;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
575 }
856
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.index == map->collection.size);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
577 // 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
578 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
579 bool found = false;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
580 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
581 if (test_map_reference[j].value == values[i]) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
582 found = true;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
583 break;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
584 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
585 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
586 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
587 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
588 free(values);
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 // verify pair iterator
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 CxIterator pairiter = cxMapIterator(map);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
594 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
595 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
596 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
597 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
598 const CxHashKey *key = entry->key;
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
599 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
600 pairs[pairiter.index].value = entry->value;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
601 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
602 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
603 // 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
604 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
605 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
606 // this was strdup'ed
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
607 free((void*)pairs[i].key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
608 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
609 free(pairs);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
610 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
611 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
612
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
613 CX_TEST(test_hash_map_basic_operations) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
614 CxTestingAllocator talloc;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
615 cx_testing_allocator_init(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
616 CxAllocator *allocator = &talloc.base;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
617 CX_TEST_DO {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
618 // create the map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
619 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
620
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
621 // clear the reference map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
622 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
623 test_map_reference[i].value = NULL;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
624 }
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 // verify iterators for empty map
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
627 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
628
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
629 // execute operations and verify results
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
630 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
631 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
632 CxHashKey key = cx_hash_key_str(kv.key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
633 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
634 if (kv.value != NULL) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
635 // 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
636 test_map_reference_put(kv.key, kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
637 int result = cxMapPut(map, key, (void *) kv.value);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
638 CX_TEST_ASSERT(result == 0);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
639 void *added = cxMapGet(map, key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
640 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
641 } else {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
642 // 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
643 const char *found = test_map_reference_remove(kv.key);
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
644 void *removed = cxMapRemoveAndGet(map, key);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
645 CX_TEST_ASSERT(found == removed);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
646 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
647 // 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
648 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
649 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
650
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
651 // destroy the map and verify the memory (de)allocations
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
652 cxMapDestroy(map);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
653 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
654 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
655 cx_testing_allocator_destroy(&talloc);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
656 }
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
657
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
658 CxTestSuite *cx_test_suite_hash_map(void) {
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
659 CxTestSuite *suite = cx_test_suite_new("map");
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
660
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
661 cx_test_register(suite, test_hash_map_create);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
662 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
663 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
664 cx_test_register(suite, test_hash_map_rehash);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
665 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
666 cx_test_register(suite, test_hash_map_clear);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
667 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
668 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
669 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
670 cx_test_register(suite, test_empty_map_size);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
671 cx_test_register(suite, test_empty_map_get);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
672 cx_test_register(suite, test_empty_map_iterator);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
673 cx_test_register(suite, test_hash_map_generics);
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
674
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
675 return suite;
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
676 }

mercurial