]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/test-hash.c
testsuite: Fix macro missing format string
[thirdparty/kmod.git] / testsuite / test-hash.c
CommitLineData
5963c36d
LDM
1/*
2 * Copyright (C) 2014 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <errno.h>
24#include <unistd.h>
25
26#include <libkmod-hash.h>
27#include "testsuite.h"
28
29static int freecount;
30
31static void countfreecalls(void *v)
32{
33 freecount++;
34}
35
36static int test_hash_new(const struct test *t)
37{
38 struct hash *h = hash_new(8, NULL);
39 assert_return(h != NULL, EXIT_FAILURE);
40 hash_free(h);
41 return 0;
42}
43static DEFINE_TEST(test_hash_new,
44 .description = "test hash_new");
45
46
47static int test_hash_get_count(const struct test *t)
48{
49 struct hash *h = hash_new(8, NULL);
50 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
51 const char *v1 = "v1", *v2 = "v2", *v3 = "v3";
52
53 hash_add(h, k1, v1);
54 hash_add(h, k2, v2);
55 hash_add(h, k3, v3);
56
57 assert_return(hash_get_count(h) == 3, EXIT_FAILURE);
58
59 hash_free(h);
60 return 0;
61}
62static DEFINE_TEST(test_hash_get_count,
63 .description = "test hash_add / hash_get_count");
64
65
66static int test_hash_replace(const struct test *t)
67{
68 struct hash *h = hash_new(8, countfreecalls);
69 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
70 const char *v1 = "v1", *v2 = "v2", *v3 = "v3", *v4 = "v4";
71 const char *v;
72 int r = 0;
73
74 r |= hash_add(h, k1, v1);
75 r |= hash_add(h, k2, v2);
76 r |= hash_add(h, k3, v3);
77
78 /* replace v1 */
79 r |= hash_add(h, k1, v4);
80
81 assert_return(r == 0, EXIT_FAILURE);
82 assert_return(hash_get_count(h) == 3, EXIT_FAILURE);
83
84 v = hash_find(h, "k1");
85 assert_return(strcmp(v, v4) == 0, EXIT_FAILURE);
86
87 assert_return(freecount == 1, EXIT_FAILURE);
88
89 hash_free(h);
90 return 0;
91}
92static DEFINE_TEST(test_hash_replace,
93 .description = "test hash_add replacing existing value");
94
95
96static int test_hash_replace_failing(const struct test *t)
97{
98 struct hash *h = hash_new(8, countfreecalls);
99 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
100 const char *v1 = "v1", *v2 = "v2", *v3 = "v3", *v4 = "v4";
101 const char *v;
102 int r = 0;
103
104 r |= hash_add(h, k1, v1);
105 r |= hash_add(h, k2, v2);
106 r |= hash_add(h, k3, v3);
107
108 assert_return(r == 0, EXIT_FAILURE);
109
110 /* replace v1 */
111 r = hash_add_unique(h, k1, v4);
112 assert_return(r != 0, EXIT_FAILURE);
113 assert_return(hash_get_count(h) == 3, EXIT_FAILURE);
114
115 v = hash_find(h, "k1");
116 assert_return(strcmp(v, v1) == 0, EXIT_FAILURE);
117
118 assert_return(freecount == 0, EXIT_FAILURE);
119
120 hash_free(h);
121 return 0;
122}
123static DEFINE_TEST(test_hash_replace_failing,
124 .description = "test hash_add_unique failing to replace existing value");
125
126
127static int test_hash_iter(const struct test *t)
128{
129 struct hash *h = hash_new(8, NULL);
130 struct hash *h2 = hash_new(8, NULL);
131 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
132 const char *v1 = "v1", *v2 = "v2", *v3 = "v3";
133 struct hash_iter iter;
134 const char *k, *v;
135
136 hash_add(h, k1, v1);
137 hash_add(h2, k1, v1);
138 hash_add(h, k2, v2);
139 hash_add(h2, k2, v2);
140 hash_add(h, k3, v3);
141 hash_add(h2, k3, v3);
142
143 for (hash_iter_init(h, &iter);
144 hash_iter_next(&iter, &k, (const void **) &v);) {
145 v2 = hash_find(h2, k);
146 assert_return(v2 != NULL, EXIT_FAILURE);
147 hash_del(h2, k);
148 }
149
150 assert_return(hash_get_count(h) == 3, EXIT_FAILURE);
151 assert_return(hash_get_count(h2) == 0, EXIT_FAILURE);
152
153 hash_free(h);
154 hash_free(h2);
155 return 0;
156}
157static DEFINE_TEST(test_hash_iter,
158 .description = "test hash_iter");
159
160
161static int test_hash_iter_after_del(const struct test *t)
162{
163 struct hash *h = hash_new(8, NULL);
164 struct hash *h2 = hash_new(8, NULL);
165 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
166 const char *v1 = "v1", *v2 = "v2", *v3 = "v3";
167 struct hash_iter iter;
168 const char *k, *v;
169
170 hash_add(h, k1, v1);
171 hash_add(h2, k1, v1);
172 hash_add(h, k2, v2);
173 hash_add(h2, k2, v2);
174 hash_add(h, k3, v3);
175 hash_add(h2, k3, v3);
176
177 hash_del(h, k1);
178
179 for (hash_iter_init(h, &iter);
180 hash_iter_next(&iter, &k, (const void **) &v);) {
181 v2 = hash_find(h2, k);
182 assert_return(v2 != NULL, EXIT_FAILURE);
183 hash_del(h2, k);
184 }
185
186 assert_return(hash_get_count(h) == 2, EXIT_FAILURE);
187 assert_return(hash_get_count(h2) == 1, EXIT_FAILURE);
188
189 hash_free(h);
190 hash_free(h2);
191 return 0;
192}
193static DEFINE_TEST(test_hash_iter_after_del,
194 .description = "test hash_iter, after deleting element");
195
196
197static int test_hash_free(const struct test *t)
198{
199 struct hash *h = hash_new(8, countfreecalls);
200 const char *k1 = "k1", *k2 = "k2", *k3 = "k3";
201 const char *v1 = "v1", *v2 = "v2", *v3 = "v3";
202
203 hash_add(h, k1, v1);
204 hash_add(h, k2, v2);
205 hash_add(h, k3, v3);
206
207 hash_del(h, k1);
208
209 assert_return(freecount == 1, EXIT_FAILURE);
210
211 assert_return(hash_get_count(h) == 2, EXIT_FAILURE);
212
213 hash_free(h);
214
215 assert_return(freecount == 3, EXIT_FAILURE);
216
217 return 0;
218}
219static DEFINE_TEST(test_hash_free,
220 .description = "test hash_free calling free function for all values");
221static const struct test *tests[] = {
222 &stest_hash_new,
223 &stest_hash_get_count,
224 &stest_hash_replace,
225 &stest_hash_replace_failing,
226 &stest_hash_iter,
227 &stest_hash_iter_after_del,
228 &stest_hash_free,
229 NULL,
230};
231
232
233TESTSUITE_MAIN(tests);