Debian Codegraph @

dhash/dhash_ut_check.c

raw symbols SCIP
1 /*
2 Authors:
3 Michal Zidek <[email protected]>
4
5 Copyright (C) 2016 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <check.h>
27
28 /* #define TRACE_LEVEL 7 */
29 #define TRACE_HOME
30 #include "dhash.h"
31
32 #define HTABLE_SIZE 128
33
34 int verbose = 0;
35
36 /* There must be no warnings generated during this test
37 * without having to cast the key value. */
38 START_TEST(test_key_const_string)
39 {
40 hash_table_t *htable;
41 int ret;
42 hash_value_t ret_val;
43 hash_value_t enter_val1;
44 hash_value_t enter_val2;
45 hash_key_t key;
46
47 enter_val1.type = HASH_VALUE_INT;
48 enter_val1.i = 1;
49 enter_val2.type = HASH_VALUE_INT;
50 enter_val2.i = 2;
51 key.type = HASH_KEY_CONST_STRING;
52 key.c_str = "constant";
53
54 ret = hash_create(HTABLE_SIZE, &htable, NULL, NULL);
55 fail_unless(ret == 0);
56
57 /* The table is empty, lookup should return error */
58 ret = hash_lookup(htable, &key, &ret_val);
59 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
60
61 /* Deleting with non-existing key should return error */
62 ret = hash_delete(htable, &key);
63 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
64
65 ret = hash_enter(htable, &key, &enter_val1);
66 fail_unless(ret == 0);
67
68 hash_lookup(htable, &key, &ret_val);
69 fail_unless(ret == 0);
70 fail_unless(ret_val.i == 1);
71
72 /* Overwrite the entry */
73 ret = hash_enter(htable, &key, &enter_val2);
74 fail_unless(ret == 0);
75
76 hash_lookup(htable, &key, &ret_val);
77 fail_unless(ret == 0);
78 fail_unless(ret_val.i == 2);
79
80 ret = hash_delete(htable, &key);
81 fail_unless(ret == 0);
82
83 /* Delete again with the same key */
84 ret = hash_delete(htable, &key);
85 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
86
87 ret = hash_destroy(htable);
88 fail_unless(ret == 0);
89 }
90 END_TEST
91
92 START_TEST(test_key_string)
93 {
94 hash_table_t *htable;
95 int ret;
96 hash_value_t ret_val;
97 hash_value_t enter_val1;
98 hash_value_t enter_val2;
99 hash_key_t key;
100 char str[] = "non_constant";
101
102 enter_val1.type = HASH_VALUE_INT;
103 enter_val1.i = 1;
104 enter_val2.type = HASH_VALUE_INT;
105 enter_val2.i = 2;
106 key.type = HASH_KEY_STRING;
107 key.str = str;
108
109 ret = hash_create(HTABLE_SIZE, &htable, NULL, NULL);
110 fail_unless(ret == 0);
111
112 /* The table is empty, lookup should return error */
113 ret = hash_lookup(htable, &key, &ret_val);
114 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
115
116 /* Deleting with non-existing key should return error */
117 ret = hash_delete(htable, &key);
118 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
119
120 ret = hash_enter(htable, &key, &enter_val1);
121 fail_unless(ret == 0);
122
123 hash_lookup(htable, &key, &ret_val);
124 fail_unless(ret == 0);
125 fail_unless(ret_val.i == 1);
126
127 /* Overwrite the entry */
128 ret = hash_enter(htable, &key, &enter_val2);
129 fail_unless(ret == 0);
130
131 hash_lookup(htable, &key, &ret_val);
132 fail_unless(ret == 0);
133 fail_unless(ret_val.i == 2);
134
135 ret = hash_delete(htable, &key);
136 fail_unless(ret == 0);
137
138 /* Delete again with the same key */
139 ret = hash_delete(htable, &key);
140 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
141
142
143 ret = hash_destroy(htable);
144 fail_unless(ret == 0);
145 }
146 END_TEST
147
148 START_TEST(test_key_ulong)
149 {
150 hash_table_t *htable;
151 int ret;
152 hash_value_t ret_val;
153 hash_value_t enter_val1;
154 hash_value_t enter_val2;
155 hash_key_t key;
156
157 enter_val1.type = HASH_VALUE_INT;
158 enter_val1.i = 1;
159 enter_val2.type = HASH_VALUE_INT;
160 enter_val2.i = 2;
161 key.type = HASH_KEY_ULONG;
162 key.ul = 68ul;
163
164 ret = hash_create(HTABLE_SIZE, &htable, NULL, NULL);
165 fail_unless(ret == 0);
166
167 /* The table is empty, lookup should return error */
168 ret = hash_lookup(htable, &key, &ret_val);
169 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
170
171 /* Deleting with non-existing key should return error */
172 ret = hash_delete(htable, &key);
173 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
174
175 ret = hash_enter(htable, &key, &enter_val1);
176 fail_unless(ret == 0);
177
178 hash_lookup(htable, &key, &ret_val);
179 fail_unless(ret == 0);
180 fail_unless(ret_val.i == 1);
181
182 /* Overwrite the entry */
183 ret = hash_enter(htable, &key, &enter_val2);
184 fail_unless(ret == 0);
185
186 hash_lookup(htable, &key, &ret_val);
187 fail_unless(ret == 0);
188 fail_unless(ret_val.i == 2);
189
190 ret = hash_delete(htable, &key);
191 fail_unless(ret == 0);
192
193 /* Delete again with the same key */
194 ret = hash_delete(htable, &key);
195 fail_unless(ret == HASH_ERROR_KEY_NOT_FOUND);
196
197 ret = hash_destroy(htable);
198 fail_unless(ret == 0);
199 }
200 END_TEST
201
202 static Suite *dhash_suite(void)
203 {
204 Suite *s = suite_create("");
205
206 TCase *tc_basic = tcase_create("dhash API tests");
207 tcase_add_test(tc_basic, test_key_const_string);
208 tcase_add_test(tc_basic, test_key_string);
209 tcase_add_test(tc_basic, test_key_ulong);
210 suite_add_tcase(s, tc_basic);
211
212 return s;
213 }
214
215 int main(void)
216 {
217 int number_failed;
218
219 Suite *s = dhash_suite();
220 SRunner *sr = srunner_create(s);
221 /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
222 srunner_run_all(sr, CK_ENV);
223 number_failed = srunner_ntests_failed(sr);
224 srunner_free(sr);
225 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
226 }