1 /*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/sys/hash_map.h>
9
10 #include "_main.h"
11
ZTEST(hash_map,test_clear_no_callback)12 ZTEST(hash_map, test_clear_no_callback)
13 {
14 const size_t N = 10;
15
16 zassert_true(sys_hashmap_is_empty(&map));
17 for (size_t i = 0; i < N; ++i) {
18 zassert_equal(1, sys_hashmap_insert(&map, i, i, NULL));
19 }
20
21 zassert_equal(N, sys_hashmap_size(&map));
22
23 sys_hashmap_clear(&map, NULL, NULL);
24 zassert_true(sys_hashmap_is_empty(&map));
25 }
26
clear_callback(uint64_t key,uint64_t value,void * cookie)27 static void clear_callback(uint64_t key, uint64_t value, void *cookie)
28 {
29 bool *cleared = (bool *)cookie;
30
31 zassert_true(key < 10);
32 cleared[key] = true;
33 }
34
ZTEST(hash_map,test_clear_callback)35 ZTEST(hash_map, test_clear_callback)
36 {
37 bool cleared[10] = {0};
38
39 zassert_true(sys_hashmap_is_empty(&map));
40 for (size_t i = 0; i < ARRAY_SIZE(cleared); ++i) {
41 zassert_equal(1, sys_hashmap_insert(&map, i, i, NULL));
42 }
43
44 zassert_equal(ARRAY_SIZE(cleared), sys_hashmap_size(&map));
45
46 sys_hashmap_clear(&map, clear_callback, cleared);
47 zassert_true(sys_hashmap_is_empty(&map));
48
49 for (size_t i = 0; i < ARRAY_SIZE(cleared); ++i) {
50 zassert_true(cleared[i], "entry %zu was not cleared", i + 1);
51 }
52 }
53