1 /*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8
9 #include <zephyr/ztest.h>
10 #include <zephyr/sys/hash_map.h>
11
12 #include "_main.h"
13
ZTEST(hash_map,test_insert_no_replacement)14 ZTEST(hash_map, test_insert_no_replacement)
15 {
16 zassert_true(sys_hashmap_is_empty(&map));
17
18 zassert_equal(1, sys_hashmap_insert(&map, 1, 1, NULL));
19 zassert_equal(1, sys_hashmap_size(&map));
20 zassert_true(sys_hashmap_contains_key(&map, 1));
21
22 zassert_equal(1, sys_hashmap_insert(&map, 2, 2, NULL));
23 zassert_equal(2, sys_hashmap_size(&map));
24 zassert_true(sys_hashmap_contains_key(&map, 2));
25 }
26
ZTEST(hash_map,test_insert_replacement)27 ZTEST(hash_map, test_insert_replacement)
28 {
29 uint64_t old_value;
30
31 zassert_true(sys_hashmap_is_empty(&map));
32
33 zassert_equal(1, sys_hashmap_insert(&map, 1, 1, NULL));
34 zassert_equal(1, sys_hashmap_size(&map));
35 zassert_true(sys_hashmap_contains_key(&map, 1));
36
37 old_value = 0x42;
38 zassert_equal(0, sys_hashmap_insert(&map, 1, 2, &old_value));
39 zassert_equal(1, old_value);
40 zassert_equal(1, sys_hashmap_size(&map));
41 zassert_true(sys_hashmap_contains_key(&map, 1));
42 }
43
ZTEST(hash_map,test_insert_many)44 ZTEST(hash_map, test_insert_many)
45 {
46 int ret;
47
48 zassert_true(sys_hashmap_is_empty(&map));
49
50 for (size_t i = 0; i < MANY; ++i) {
51 ret = sys_hashmap_insert(&map, i, i, NULL);
52 zassert_equal(1, ret, "failed to insert (%zu, %zu): %d", i, i, ret);
53 zassert_equal(i + 1, sys_hashmap_size(&map));
54 }
55
56 for (size_t i = 0; i < MANY; ++i) {
57 zassert_true(sys_hashmap_contains_key(&map, i));
58 }
59 }
60