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_load_factor_default)12 ZTEST(hash_map, test_load_factor_default)
13 {
14 int ret;
15 uint32_t load_factor;
16
17 zassert_true(sys_hashmap_is_empty(&map));
18 zassert_equal(0, sys_hashmap_load_factor(&map));
19
20 for (size_t i = 0; i < MANY; ++i) {
21 ret = sys_hashmap_insert(&map, i, i, NULL);
22 zassert_equal(1, ret, "failed to insert (%zu, %zu): %d", i, i, ret);
23 load_factor = sys_hashmap_load_factor(&map);
24 zassert_true(load_factor > 0 && load_factor <= SYS_HASHMAP_DEFAULT_LOAD_FACTOR);
25 }
26
27 for (size_t i = MANY; i > 0; --i) {
28 zassert_equal(true, sys_hashmap_remove(&map, i - 1, NULL));
29 load_factor = sys_hashmap_load_factor(&map);
30 zassert_true(load_factor <= SYS_HASHMAP_DEFAULT_LOAD_FACTOR);
31 }
32 }
33
ZTEST(hash_map,test_load_factor_custom)34 ZTEST(hash_map, test_load_factor_custom)
35 {
36 int ret;
37 uint32_t load_factor;
38 struct sys_hashmap *const hmap = &custom_load_factor_map;
39
40 zassert_equal(hmap->config->load_factor, CUSTOM_LOAD_FACTOR);
41
42 zassert_true(sys_hashmap_is_empty(hmap));
43 zassert_equal(0, sys_hashmap_load_factor(hmap));
44
45 for (size_t i = 0; i < MANY; ++i) {
46 ret = sys_hashmap_insert(hmap, i, i, NULL);
47 zassert_equal(1, ret, "failed to insert (%zu, %zu): %d", i, i, ret);
48 load_factor = sys_hashmap_load_factor(hmap);
49 zassert_true(load_factor > 0 && load_factor <= CUSTOM_LOAD_FACTOR);
50 }
51
52 for (size_t i = MANY; i > 0; --i) {
53 zassert_equal(true, sys_hashmap_remove(hmap, i - 1, NULL));
54 load_factor = sys_hashmap_load_factor(hmap);
55 zassert_true(load_factor <= CUSTOM_LOAD_FACTOR);
56 }
57 }
58