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 
foreach_callback(uint64_t key,uint64_t value,void * cookie)12 static void foreach_callback(uint64_t key, uint64_t value, void *cookie)
13 {
14 	bool *called = (bool *)cookie;
15 
16 	zassert_true(key < 10);
17 	called[key] = true;
18 }
19 
ZTEST(hash_map,test_foreach)20 ZTEST(hash_map, test_foreach)
21 {
22 	bool called[10] = {0};
23 
24 	zassert_true(sys_hashmap_is_empty(&map));
25 
26 	for (size_t i = 0; i < ARRAY_SIZE(called); ++i) {
27 		zassert_equal(1, sys_hashmap_insert(&map, i, i, NULL));
28 	}
29 
30 	zassert_equal(ARRAY_SIZE(called), sys_hashmap_size(&map));
31 
32 	sys_hashmap_foreach(&map, foreach_callback, called);
33 	for (size_t i = 0; i < ARRAY_SIZE(called); ++i) {
34 		zassert_true(called[i], "entry %zu was not called", i + 1);
35 	}
36 }
37