root / check-qdict.c @ 1bb62099
History | View | Annotate | Download (7.9 kB)
1 |
/*
|
---|---|
2 |
* QDict unit-tests.
|
3 |
*
|
4 |
* Copyright (C) 2009 Red Hat Inc.
|
5 |
*
|
6 |
* Authors:
|
7 |
* Luiz Capitulino <lcapitulino@redhat.com>
|
8 |
*/
|
9 |
#include <check.h> |
10 |
|
11 |
#include "qint.h" |
12 |
#include "qdict.h" |
13 |
#include "qstring.h" |
14 |
#include "qemu-common.h" |
15 |
|
16 |
/*
|
17 |
* Public Interface test-cases
|
18 |
*
|
19 |
* (with some violations to access 'private' data)
|
20 |
*/
|
21 |
|
22 |
START_TEST(qdict_new_test) |
23 |
{ |
24 |
QDict *qdict; |
25 |
|
26 |
qdict = qdict_new(); |
27 |
fail_unless(qdict != NULL);
|
28 |
fail_unless(qdict_size(qdict) == 0);
|
29 |
fail_unless(qdict->base.refcnt == 1);
|
30 |
fail_unless(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT); |
31 |
|
32 |
// destroy doesn't exit yet
|
33 |
free(qdict); |
34 |
} |
35 |
END_TEST |
36 |
|
37 |
START_TEST(qdict_put_obj_test) |
38 |
{ |
39 |
QInt *qi; |
40 |
QDict *qdict; |
41 |
QDictEntry *ent; |
42 |
const int num = 42; |
43 |
|
44 |
qdict = qdict_new(); |
45 |
|
46 |
// key "" will have tdb hash 12345
|
47 |
qdict_put_obj(qdict, "", QOBJECT(qint_from_int(num)));
|
48 |
|
49 |
fail_unless(qdict_size(qdict) == 1);
|
50 |
ent = QLIST_FIRST(&qdict->table[12345 % QDICT_HASH_SIZE]);
|
51 |
qi = qobject_to_qint(ent->value); |
52 |
fail_unless(qint_get_int(qi) == num); |
53 |
|
54 |
// destroy doesn't exit yet
|
55 |
QDECREF(qi); |
56 |
qemu_free(ent->key); |
57 |
qemu_free(ent); |
58 |
qemu_free(qdict); |
59 |
} |
60 |
END_TEST |
61 |
|
62 |
START_TEST(qdict_destroy_simple_test) |
63 |
{ |
64 |
QDict *qdict; |
65 |
|
66 |
qdict = qdict_new(); |
67 |
qdict_put_obj(qdict, "num", QOBJECT(qint_from_int(0))); |
68 |
qdict_put_obj(qdict, "str", QOBJECT(qstring_from_str("foo"))); |
69 |
|
70 |
QDECREF(qdict); |
71 |
} |
72 |
END_TEST |
73 |
|
74 |
static QDict *tests_dict = NULL; |
75 |
|
76 |
static void qdict_setup(void) |
77 |
{ |
78 |
tests_dict = qdict_new(); |
79 |
fail_unless(tests_dict != NULL);
|
80 |
} |
81 |
|
82 |
static void qdict_teardown(void) |
83 |
{ |
84 |
QDECREF(tests_dict); |
85 |
tests_dict = NULL;
|
86 |
} |
87 |
|
88 |
START_TEST(qdict_get_test) |
89 |
{ |
90 |
QInt *qi; |
91 |
QObject *obj; |
92 |
const int value = -42; |
93 |
const char *key = "test"; |
94 |
|
95 |
qdict_put(tests_dict, key, qint_from_int(value)); |
96 |
|
97 |
obj = qdict_get(tests_dict, key); |
98 |
fail_unless(obj != NULL);
|
99 |
|
100 |
qi = qobject_to_qint(obj); |
101 |
fail_unless(qint_get_int(qi) == value); |
102 |
} |
103 |
END_TEST |
104 |
|
105 |
START_TEST(qdict_get_int_test) |
106 |
{ |
107 |
int ret;
|
108 |
const int value = 100; |
109 |
const char *key = "int"; |
110 |
|
111 |
qdict_put(tests_dict, key, qint_from_int(value)); |
112 |
|
113 |
ret = qdict_get_int(tests_dict, key); |
114 |
fail_unless(ret == value); |
115 |
} |
116 |
END_TEST |
117 |
|
118 |
START_TEST(qdict_get_try_int_test) |
119 |
{ |
120 |
int ret;
|
121 |
const int value = 100; |
122 |
const char *key = "int"; |
123 |
|
124 |
qdict_put(tests_dict, key, qint_from_int(value)); |
125 |
|
126 |
ret = qdict_get_try_int(tests_dict, key, 0);
|
127 |
fail_unless(ret == value); |
128 |
} |
129 |
END_TEST |
130 |
|
131 |
START_TEST(qdict_get_str_test) |
132 |
{ |
133 |
const char *p; |
134 |
const char *key = "key"; |
135 |
const char *str = "string"; |
136 |
|
137 |
qdict_put(tests_dict, key, qstring_from_str(str)); |
138 |
|
139 |
p = qdict_get_str(tests_dict, key); |
140 |
fail_unless(p != NULL);
|
141 |
fail_unless(strcmp(p, str) == 0);
|
142 |
} |
143 |
END_TEST |
144 |
|
145 |
START_TEST(qdict_get_try_str_test) |
146 |
{ |
147 |
const char *p; |
148 |
const char *key = "key"; |
149 |
const char *str = "string"; |
150 |
|
151 |
qdict_put(tests_dict, key, qstring_from_str(str)); |
152 |
|
153 |
p = qdict_get_try_str(tests_dict, key); |
154 |
fail_unless(p != NULL);
|
155 |
fail_unless(strcmp(p, str) == 0);
|
156 |
} |
157 |
END_TEST |
158 |
|
159 |
START_TEST(qdict_haskey_not_test) |
160 |
{ |
161 |
fail_unless(qdict_haskey(tests_dict, "test") == 0); |
162 |
} |
163 |
END_TEST |
164 |
|
165 |
START_TEST(qdict_haskey_test) |
166 |
{ |
167 |
const char *key = "test"; |
168 |
|
169 |
qdict_put(tests_dict, key, qint_from_int(0));
|
170 |
fail_unless(qdict_haskey(tests_dict, key) == 1);
|
171 |
} |
172 |
END_TEST |
173 |
|
174 |
START_TEST(qdict_del_test) |
175 |
{ |
176 |
const char *key = "key test"; |
177 |
|
178 |
qdict_put(tests_dict, key, qstring_from_str("foo"));
|
179 |
fail_unless(qdict_size(tests_dict) == 1);
|
180 |
|
181 |
qdict_del(tests_dict, key); |
182 |
|
183 |
fail_unless(qdict_size(tests_dict) == 0);
|
184 |
fail_unless(qdict_haskey(tests_dict, key) == 0);
|
185 |
} |
186 |
END_TEST |
187 |
|
188 |
START_TEST(qobject_to_qdict_test) |
189 |
{ |
190 |
fail_unless(qobject_to_qdict(QOBJECT(tests_dict)) == tests_dict); |
191 |
} |
192 |
END_TEST |
193 |
|
194 |
/*
|
195 |
* Errors test-cases
|
196 |
*/
|
197 |
|
198 |
START_TEST(qdict_put_exists_test) |
199 |
{ |
200 |
int value;
|
201 |
const char *key = "exists"; |
202 |
|
203 |
qdict_put(tests_dict, key, qint_from_int(1));
|
204 |
qdict_put(tests_dict, key, qint_from_int(2));
|
205 |
|
206 |
value = qdict_get_int(tests_dict, key); |
207 |
fail_unless(value == 2);
|
208 |
} |
209 |
END_TEST |
210 |
|
211 |
START_TEST(qdict_get_not_exists_test) |
212 |
{ |
213 |
fail_unless(qdict_get(tests_dict, "foo") == NULL); |
214 |
} |
215 |
END_TEST |
216 |
|
217 |
/*
|
218 |
* Stress test-case
|
219 |
*
|
220 |
* This is a lot big for a unit-test, but there is no other place
|
221 |
* to have it.
|
222 |
*/
|
223 |
|
224 |
static void remove_dots(char *string) |
225 |
{ |
226 |
char *p = strchr(string, ':'); |
227 |
if (p)
|
228 |
*p = '\0';
|
229 |
} |
230 |
|
231 |
static QString *read_line(FILE *file, char *key) |
232 |
{ |
233 |
char value[128]; |
234 |
|
235 |
if (fscanf(file, "%s%s", key, value) == EOF) |
236 |
return NULL; |
237 |
remove_dots(key); |
238 |
return qstring_from_str(value);
|
239 |
} |
240 |
|
241 |
#define reset_file(file) fseek(file, 0L, SEEK_SET) |
242 |
|
243 |
START_TEST(qdict_stress_test) |
244 |
{ |
245 |
size_t lines; |
246 |
char key[128]; |
247 |
FILE *test_file; |
248 |
QDict *qdict; |
249 |
QString *value; |
250 |
const char *test_file_path = "qdict-test-data.txt"; |
251 |
|
252 |
test_file = fopen(test_file_path, "r");
|
253 |
fail_unless(test_file != NULL);
|
254 |
|
255 |
// Create the dict
|
256 |
qdict = qdict_new(); |
257 |
fail_unless(qdict != NULL);
|
258 |
|
259 |
// Add everything from the test file
|
260 |
for (lines = 0;; lines++) { |
261 |
value = read_line(test_file, key); |
262 |
if (!value)
|
263 |
break;
|
264 |
|
265 |
qdict_put(qdict, key, value); |
266 |
} |
267 |
fail_unless(qdict_size(qdict) == lines); |
268 |
|
269 |
// Check if everything is really in there
|
270 |
reset_file(test_file); |
271 |
for (;;) {
|
272 |
const char *str1, *str2; |
273 |
|
274 |
value = read_line(test_file, key); |
275 |
if (!value)
|
276 |
break;
|
277 |
|
278 |
str1 = qstring_get_str(value); |
279 |
|
280 |
str2 = qdict_get_str(qdict, key); |
281 |
fail_unless(str2 != NULL);
|
282 |
|
283 |
fail_unless(strcmp(str1, str2) == 0);
|
284 |
|
285 |
QDECREF(value); |
286 |
} |
287 |
|
288 |
// Delete everything
|
289 |
reset_file(test_file); |
290 |
for (;;) {
|
291 |
value = read_line(test_file, key); |
292 |
if (!value)
|
293 |
break;
|
294 |
|
295 |
qdict_del(qdict, key); |
296 |
QDECREF(value); |
297 |
|
298 |
fail_unless(qdict_haskey(qdict, key) == 0);
|
299 |
} |
300 |
fclose(test_file); |
301 |
|
302 |
fail_unless(qdict_size(qdict) == 0);
|
303 |
QDECREF(qdict); |
304 |
} |
305 |
END_TEST |
306 |
|
307 |
static Suite *qdict_suite(void) |
308 |
{ |
309 |
Suite *s; |
310 |
TCase *qdict_public_tcase; |
311 |
TCase *qdict_public2_tcase; |
312 |
TCase *qdict_stress_tcase; |
313 |
TCase *qdict_errors_tcase; |
314 |
|
315 |
s = suite_create("QDict test-suite");
|
316 |
|
317 |
qdict_public_tcase = tcase_create("Public Interface");
|
318 |
suite_add_tcase(s, qdict_public_tcase); |
319 |
tcase_add_test(qdict_public_tcase, qdict_new_test); |
320 |
tcase_add_test(qdict_public_tcase, qdict_put_obj_test); |
321 |
tcase_add_test(qdict_public_tcase, qdict_destroy_simple_test); |
322 |
|
323 |
/* Continue, but now with fixtures */
|
324 |
qdict_public2_tcase = tcase_create("Public Interface (2)");
|
325 |
suite_add_tcase(s, qdict_public2_tcase); |
326 |
tcase_add_checked_fixture(qdict_public2_tcase, qdict_setup, qdict_teardown); |
327 |
tcase_add_test(qdict_public2_tcase, qdict_get_test); |
328 |
tcase_add_test(qdict_public2_tcase, qdict_get_int_test); |
329 |
tcase_add_test(qdict_public2_tcase, qdict_get_try_int_test); |
330 |
tcase_add_test(qdict_public2_tcase, qdict_get_str_test); |
331 |
tcase_add_test(qdict_public2_tcase, qdict_get_try_str_test); |
332 |
tcase_add_test(qdict_public2_tcase, qdict_haskey_not_test); |
333 |
tcase_add_test(qdict_public2_tcase, qdict_haskey_test); |
334 |
tcase_add_test(qdict_public2_tcase, qdict_del_test); |
335 |
tcase_add_test(qdict_public2_tcase, qobject_to_qdict_test); |
336 |
|
337 |
qdict_errors_tcase = tcase_create("Errors");
|
338 |
suite_add_tcase(s, qdict_errors_tcase); |
339 |
tcase_add_checked_fixture(qdict_errors_tcase, qdict_setup, qdict_teardown); |
340 |
tcase_add_test(qdict_errors_tcase, qdict_put_exists_test); |
341 |
tcase_add_test(qdict_errors_tcase, qdict_get_not_exists_test); |
342 |
|
343 |
/* The Big one */
|
344 |
qdict_stress_tcase = tcase_create("Stress Test");
|
345 |
suite_add_tcase(s, qdict_stress_tcase); |
346 |
tcase_add_test(qdict_stress_tcase, qdict_stress_test); |
347 |
|
348 |
return s;
|
349 |
} |
350 |
|
351 |
int main(void) |
352 |
{ |
353 |
int nf;
|
354 |
Suite *s; |
355 |
SRunner *sr; |
356 |
|
357 |
s = qdict_suite(); |
358 |
sr = srunner_create(s); |
359 |
|
360 |
srunner_run_all(sr, CK_NORMAL); |
361 |
nf = srunner_ntests_failed(sr); |
362 |
srunner_free(sr); |
363 |
|
364 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
365 |
} |