blob: 70102f9121c271d00b851ed074ac9f4df11472f9 [file] [log] [blame]
Roman Kapl9dfdbd92019-01-30 11:39:54 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2019
4 * Roman Kapl, SYSGO, rka@sysgo.com
5 */
6
7#include <common.h>
8#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Roman Kapl9dfdbd92019-01-30 11:39:54 +010010#include <search.h>
11#include <stdio.h>
12#include <test/env.h>
13#include <test/ut.h>
14
15#define SIZE 32
16#define ITERATIONS 10000
17
18static int htab_fill(struct unit_test_state *uts,
19 struct hsearch_data *htab, size_t size)
20{
21 size_t i;
Simon Glassdd2408c2019-08-02 09:44:18 -060022 struct env_entry item;
23 struct env_entry *ritem;
Roman Kapl9dfdbd92019-01-30 11:39:54 +010024 char key[20];
25
26 for (i = 0; i < size; i++) {
27 sprintf(key, "%d", (int)i);
28 item.callback = NULL;
29 item.data = key;
30 item.flags = 0;
31 item.key = key;
Simon Glass3f0d6802019-08-01 09:47:09 -060032 ut_asserteq(1, hsearch_r(item, ENV_ENTER, &ritem, htab, 0));
Roman Kapl9dfdbd92019-01-30 11:39:54 +010033 }
34
35 return 0;
36}
37
38static int htab_check_fill(struct unit_test_state *uts,
39 struct hsearch_data *htab, size_t size)
40{
41 size_t i;
Simon Glassdd2408c2019-08-02 09:44:18 -060042 struct env_entry item;
43 struct env_entry *ritem;
Roman Kapl9dfdbd92019-01-30 11:39:54 +010044 char key[20];
45
46 for (i = 0; i < size; i++) {
47 sprintf(key, "%d", (int)i);
48 item.callback = NULL;
49 item.flags = 0;
50 item.data = key;
51 item.key = key;
Simon Glass3f0d6802019-08-01 09:47:09 -060052 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
Roman Kapl9dfdbd92019-01-30 11:39:54 +010053 ut_assert(ritem);
54 ut_asserteq_str(key, ritem->key);
55 ut_asserteq_str(key, ritem->data);
56 }
57
58 return 0;
59}
60
61static int htab_create_delete(struct unit_test_state *uts,
62 struct hsearch_data *htab, size_t iterations)
63{
64 size_t i;
Simon Glassdd2408c2019-08-02 09:44:18 -060065 struct env_entry item;
66 struct env_entry *ritem;
Roman Kapl9dfdbd92019-01-30 11:39:54 +010067 char key[20];
68
69 for (i = 0; i < iterations; i++) {
70 sprintf(key, "cd-%d", (int)i);
71 item.callback = NULL;
72 item.flags = 0;
73 item.data = key;
74 item.key = key;
Simon Glass3f0d6802019-08-01 09:47:09 -060075 hsearch_r(item, ENV_ENTER, &ritem, htab, 0);
Roman Kapl9dfdbd92019-01-30 11:39:54 +010076 ritem = NULL;
77
Simon Glass3f0d6802019-08-01 09:47:09 -060078 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
Roman Kapl9dfdbd92019-01-30 11:39:54 +010079 ut_assert(ritem);
80 ut_asserteq_str(key, ritem->key);
81 ut_asserteq_str(key, ritem->data);
82
Simon Glass96434a72020-11-05 10:33:37 -070083 ut_asserteq(0, hdelete_r(key, htab, 0));
Roman Kapl9dfdbd92019-01-30 11:39:54 +010084 }
85
86 return 0;
87}
88
89/* Completely fill up the hash table */
90static int env_test_htab_fill(struct unit_test_state *uts)
91{
92 struct hsearch_data htab;
93
94 memset(&htab, 0, sizeof(htab));
95 ut_asserteq(1, hcreate_r(SIZE, &htab));
96
97 ut_assertok(htab_fill(uts, &htab, SIZE));
98 ut_assertok(htab_check_fill(uts, &htab, SIZE));
99 ut_asserteq(SIZE, htab.filled);
100
101 hdestroy_r(&htab);
102 return 0;
103}
104
105ENV_TEST(env_test_htab_fill, 0);
106
107/* Fill the hashtable up halfway an repeateadly delete/create elements
108 * and check for corruption
109 */
110static int env_test_htab_deletes(struct unit_test_state *uts)
111{
112 struct hsearch_data htab;
113
114 memset(&htab, 0, sizeof(htab));
115 ut_asserteq(1, hcreate_r(SIZE, &htab));
116
117 ut_assertok(htab_fill(uts, &htab, SIZE / 2));
118 ut_assertok(htab_create_delete(uts, &htab, ITERATIONS));
119 ut_assertok(htab_check_fill(uts, &htab, SIZE / 2));
120 ut_asserteq(SIZE / 2, htab.filled);
121
122 hdestroy_r(&htab);
123 return 0;
124}
125
126ENV_TEST(env_test_htab_deletes, 0);