MOS Source Code
Loading...
Searching...
No Matches
test_hashmap.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "test_engine_impl.h"
4
7#include <mos/mos_global.h>
8
9#define HASHMAP_MAGIC MOS_FOURCC('H', 'M', 'a', 'p')
10
12{
13 hashmap_t map = { 0 };
14 hashmap_common_type_init(&map, 64, string);
16 MOS_TEST_CHECK(map.capacity, 64);
17 MOS_TEST_CHECK(map.size, 0);
20 MOS_TEST_CHECK(map.entries != NULL, true);
21 hashmap_deinit(&map);
22}
23
25{
26 hashmap_t map = { 0 };
27 hashmap_common_type_init(&map, 135, string);
29
30 MOS_TEST_CHECK(map.capacity, 135);
31 MOS_TEST_CHECK(map.size, 0);
32 void *old = hashmap_put(&map, (ptr_t) "foo", "bar");
33 MOS_TEST_CHECK(old, NULL);
34 MOS_TEST_CHECK(map.capacity, 135);
35 MOS_TEST_CHECK(map.size, 1);
36
37 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "bar");
38 hashmap_deinit(&map);
39}
40
42{
43 hashmap_t map = { 0 };
44 hashmap_common_type_init(&map, 1, string);
47 MOS_TEST_CHECK(map.size, 0);
48
49 hashmap_put(&map, (ptr_t) "foo", "foo1");
51 MOS_TEST_CHECK(map.size, 1);
52 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
53
54 hashmap_put(&map, (ptr_t) "bar", "bar1");
56 MOS_TEST_CHECK(map.size, 2);
57 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar1");
58
59 hashmap_put(&map, (ptr_t) "bar", "bar2");
61 MOS_TEST_CHECK(map.size, 2);
62 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar2");
63
64 hashmap_deinit(&map);
65}
66
68{
69 hashmap_t map = { 0 };
70 const char *old;
71 hashmap_common_type_init(&map, 135, string);
73 MOS_TEST_CHECK(map.capacity, 135);
74 MOS_TEST_CHECK(map.size, 0);
75
76 old = hashmap_put(&map, (ptr_t) "foo", "foo1");
77 MOS_TEST_CHECK(old, NULL);
78 MOS_TEST_CHECK(map.capacity, 135);
79 MOS_TEST_CHECK(map.size, 1);
80 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
81
82 old = hashmap_put(&map, (ptr_t) "foo", "foo2");
83 MOS_TEST_CHECK(map.capacity, 135);
84 MOS_TEST_CHECK(map.size, 1);
85 MOS_TEST_CHECK_STRING(old, "foo1");
86 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo2");
87
88 old = hashmap_put(&map, (ptr_t) "bar", "bar1");
89 MOS_TEST_CHECK(old, NULL);
90 MOS_TEST_CHECK(map.capacity, 135);
91 MOS_TEST_CHECK(map.size, 2);
92 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar1");
93 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo2");
94
95 old = hashmap_put(&map, (ptr_t) "bar", "bar2");
96 MOS_TEST_CHECK(map.capacity, 135);
97 MOS_TEST_CHECK(map.size, 2);
98 MOS_TEST_CHECK_STRING(old, "bar1");
99 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar2");
100 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo2");
101 hashmap_deinit(&map);
102}
103
105{
106 hashmap_t map = { 0 };
107 const char *old;
108 hashmap_common_type_init(&map, 1, string);
110 MOS_TEST_CHECK(map.capacity, 1);
111 MOS_TEST_CHECK(map.size, 0);
112
113 old = hashmap_put(&map, (ptr_t) "foo", "foo1");
114 MOS_TEST_CHECK(old, NULL);
115 MOS_TEST_CHECK(map.capacity, 1);
116 MOS_TEST_CHECK(map.size, 1);
117 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
118
119 old = hashmap_put(&map, (ptr_t) "bar", "bar1");
120 MOS_TEST_CHECK(old, NULL);
121 MOS_TEST_CHECK(map.capacity, 1);
122 MOS_TEST_CHECK(map.size, 2);
123 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar1");
124 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
125
126 old = hashmap_put(&map, (ptr_t) "bar", "bar2");
127 MOS_TEST_CHECK_STRING(old, "bar1");
128 MOS_TEST_CHECK(map.capacity, 1);
129 MOS_TEST_CHECK(map.size, 2);
130 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "bar"), "bar2");
131 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
132
133 hashmap_deinit(&map);
134}
135
137{
138 const char *old;
139 hashmap_t map = { 0 };
140 hashmap_common_type_init(&map, 10, string);
142 MOS_TEST_CHECK(map.capacity, 10);
143 MOS_TEST_CHECK(map.size, 0);
144
145 old = hashmap_put(&map, (ptr_t) "foo", "foo1");
146 MOS_TEST_CHECK(map.capacity, 10);
147 MOS_TEST_CHECK(map.size, 1);
148 MOS_TEST_CHECK(old, NULL);
149 MOS_TEST_CHECK_STRING((const char *) hashmap_get(&map, (ptr_t) "foo"), "foo1");
150
151 old = hashmap_remove(&map, (ptr_t) "foo");
152 MOS_TEST_CHECK(map.capacity, 10);
153 MOS_TEST_CHECK(map.size, 0);
154 MOS_TEST_CHECK_STRING(old, "foo1");
155 const char *nothing = hashmap_get(&map, (ptr_t) "foo");
156 MOS_TEST_CHECK(nothing, NULL);
157
158 old = hashmap_remove(&map, (ptr_t) "foo");
159 MOS_TEST_CHECK(old, NULL);
160 MOS_TEST_CHECK(map.capacity, 10);
161 MOS_TEST_CHECK(map.size, 0);
162
163 MOS_TEST_CHECK(hashmap_get(&map, (ptr_t) "foo"), NULL);
164 hashmap_deinit(&map);
165}
166
168
169bool test_foreach_function(uintn key, void *value, void *data)
170{
171 MOS_UNUSED(key);
172 MOS_UNUSED(value);
173 MOS_UNUSED(data);
175 return true;
176}
177
178bool test_foreach_stop_at_quux(uintn key, void *value, void *data)
179{
180 MOS_UNUSED(data);
181 MOS_UNUSED(value);
183 if (strcmp((void *) key, "quux") == 0)
184 return false;
185 return true;
186}
187
189{
190 hashmap_t map = { 0 };
191 hashmap_common_type_init(&map, 10, string);
193 MOS_TEST_CHECK(map.capacity, 10);
194 MOS_TEST_CHECK(map.size, 0);
195 hashmap_put(&map, (ptr_t) "foo", "foo1");
196 hashmap_put(&map, (ptr_t) "bar", "bar1");
197 hashmap_put(&map, (ptr_t) "baz", "baz1");
198 hashmap_put(&map, (ptr_t) "qux", "qux1");
199 hashmap_put(&map, (ptr_t) "quux", "quux1");
200 hashmap_put(&map, (ptr_t) "corge", "corge1");
201 hashmap_put(&map, (ptr_t) "grault", "grault1");
202 hashmap_put(&map, (ptr_t) "garply", "garply1");
203 hashmap_put(&map, (ptr_t) "waldo", "waldo1");
204 hashmap_put(&map, (ptr_t) "fred", "fred1");
205 hashmap_put(&map, (ptr_t) "plugh", "plugh1");
206 hashmap_put(&map, (ptr_t) "xyzzy", "xyzzy1");
207
211
215 hashmap_deinit(&map);
216}
MOSAPI hash_t __pure hashmap_hash_string(uintn key)
MOSAPI int __pure hashmap_compare_string(uintn key1, uintn key2)
#define hashmap_common_type_init(map, cap, type)
MOSAPI void * hashmap_get(hashmap_t *map, uintn key)
Definition hashmap.c:96
MOSAPI void * hashmap_put(hashmap_t *map, uintn key, void *value)
Definition hashmap.c:68
MOSAPI void hashmap_deinit(hashmap_t *map)
Deinitialize a hashmap.
Definition hashmap.c:49
MOSAPI void * hashmap_remove(hashmap_t *map, uintn key)
Definition hashmap.c:117
MOSAPI void hashmap_foreach(hashmap_t *map, hashmap_foreach_func_t func, void *data)
Definition hashmap.c:146
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
#define MOS_UNUSED(x)
Definition mos_global.h:64
#define NULL
Definition pb_syshdr.h:46
s32 magic
Definition hashmap.h:24
size_t size
Definition hashmap.h:27
hashmap_hash_t hash_func
Definition hashmap.h:28
hashmap_key_compare_t key_compare_func
Definition hashmap.h:29
hashmap_entry_t ** entries
Definition hashmap.h:25
size_t capacity
Definition hashmap.h:26
#define MOS_TEST_CHECK(actual, expected)
#define MOS_TEST_CHECK_STRING(actual, expected)
#define MOS_TEST_CASE(_TestName)
static size_t test_hashmap_foreach_count
bool test_foreach_stop_at_quux(uintn key, void *value, void *data)
static void hashmap_foreach_function(mos_test_result_t *, bool *, bool *)
bool test_foreach_function(uintn key, void *value, void *data)
static void hashmap_put_multiple(mos_test_result_t *, bool *, bool *)
#define HASHMAP_MAGIC
Definition test_hashmap.c:9
static void hashmap_init_simple_macro(mos_test_result_t *, bool *, bool *)
static void hashmap_put_single(mos_test_result_t *, bool *, bool *)
static void hashmap_put_overflow(mos_test_result_t *, bool *, bool *)
static void hashmap_remove_function(mos_test_result_t *, bool *, bool *)
static void hashmap_get_function(mos_test_result_t *, bool *, bool *)
unsigned long uintn
Definition types.h:30
unsigned long ptr_t
Definition types.h:25