blob: 5242c4cc3edb7fb4105ec5787065f90cdec940bc [file] [log] [blame]
Roman Kapl07cb4e12019-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>
9#include <search.h>
10#include <stdio.h>
11#include <test/env.h>
12#include <test/ut.h>
13
14#define SIZE 32
15#define ITERATIONS 10000
16
17static int htab_fill(struct unit_test_state *uts,
18 struct hsearch_data *htab, size_t size)
19{
20 size_t i;
Simon Glass1a236862019-08-02 09:44:18 -060021 struct env_entry item;
22 struct env_entry *ritem;
Roman Kapl07cb4e12019-01-30 11:39:54 +010023 char key[20];
24
25 for (i = 0; i < size; i++) {
26 sprintf(key, "%d", (int)i);
27 item.callback = NULL;
28 item.data = key;
29 item.flags = 0;
30 item.key = key;
Simon Glass63a2f572019-08-01 09:47:09 -060031 ut_asserteq(1, hsearch_r(item, ENV_ENTER, &ritem, htab, 0));
Roman Kapl07cb4e12019-01-30 11:39:54 +010032 }
33
34 return 0;
35}
36
37static int htab_check_fill(struct unit_test_state *uts,
38 struct hsearch_data *htab, size_t size)
39{
40 size_t i;
Simon Glass1a236862019-08-02 09:44:18 -060041 struct env_entry item;
42 struct env_entry *ritem;
Roman Kapl07cb4e12019-01-30 11:39:54 +010043 char key[20];
44
45 for (i = 0; i < size; i++) {
46 sprintf(key, "%d", (int)i);
47 item.callback = NULL;
48 item.flags = 0;
49 item.data = key;
50 item.key = key;
Simon Glass63a2f572019-08-01 09:47:09 -060051 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
Roman Kapl07cb4e12019-01-30 11:39:54 +010052 ut_assert(ritem);
53 ut_asserteq_str(key, ritem->key);
54 ut_asserteq_str(key, ritem->data);
55 }
56
57 return 0;
58}
59
60static int htab_create_delete(struct unit_test_state *uts,
61 struct hsearch_data *htab, size_t iterations)
62{
63 size_t i;
Simon Glass1a236862019-08-02 09:44:18 -060064 struct env_entry item;
65 struct env_entry *ritem;
Roman Kapl07cb4e12019-01-30 11:39:54 +010066 char key[20];
67
68 for (i = 0; i < iterations; i++) {
69 sprintf(key, "cd-%d", (int)i);
70 item.callback = NULL;
71 item.flags = 0;
72 item.data = key;
73 item.key = key;
Simon Glass63a2f572019-08-01 09:47:09 -060074 hsearch_r(item, ENV_ENTER, &ritem, htab, 0);
Roman Kapl07cb4e12019-01-30 11:39:54 +010075 ritem = NULL;
76
Simon Glass63a2f572019-08-01 09:47:09 -060077 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
Roman Kapl07cb4e12019-01-30 11:39:54 +010078 ut_assert(ritem);
79 ut_asserteq_str(key, ritem->key);
80 ut_asserteq_str(key, ritem->data);
81
82 ut_asserteq(1, hdelete_r(key, htab, 0));
83 }
84
85 return 0;
86}
87
88/* Completely fill up the hash table */
89static int env_test_htab_fill(struct unit_test_state *uts)
90{
91 struct hsearch_data htab;
92
93 memset(&htab, 0, sizeof(htab));
94 ut_asserteq(1, hcreate_r(SIZE, &htab));
95
96 ut_assertok(htab_fill(uts, &htab, SIZE));
97 ut_assertok(htab_check_fill(uts, &htab, SIZE));
98 ut_asserteq(SIZE, htab.filled);
99
100 hdestroy_r(&htab);
101 return 0;
102}
103
104ENV_TEST(env_test_htab_fill, 0);
105
106/* Fill the hashtable up halfway an repeateadly delete/create elements
107 * and check for corruption
108 */
109static int env_test_htab_deletes(struct unit_test_state *uts)
110{
111 struct hsearch_data htab;
112
113 memset(&htab, 0, sizeof(htab));
114 ut_asserteq(1, hcreate_r(SIZE, &htab));
115
116 ut_assertok(htab_fill(uts, &htab, SIZE / 2));
117 ut_assertok(htab_create_delete(uts, &htab, ITERATIONS));
118 ut_assertok(htab_check_fill(uts, &htab, SIZE / 2));
119 ut_asserteq(SIZE / 2, htab.filled);
120
121 hdestroy_r(&htab);
122 return 0;
123}
124
125ENV_TEST(env_test_htab_deletes, 0);