pci: cleanly backout of pci_qdev_init()
[qemu/mdroth.git] / check-qdict.c
blobf2b4826560a6fa017f421913ac006ba58c581fd3
1 /*
2 * QDict unit-tests.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 */
9 #include <check.h>
11 #include "qint.h"
12 #include "qdict.h"
13 #include "qstring.h"
14 #include "qemu-common.h"
17 * Public Interface test-cases
19 * (with some violations to access 'private' data)
22 START_TEST(qdict_new_test)
24 QDict *qdict;
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);
32 // destroy doesn't exit yet
33 free(qdict);
35 END_TEST
37 START_TEST(qdict_put_obj_test)
39 QInt *qi;
40 QDict *qdict;
41 QDictEntry *ent;
42 const int num = 42;
44 qdict = qdict_new();
46 // key "" will have tdb hash 12345
47 qdict_put_obj(qdict, "", QOBJECT(qint_from_int(num)));
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);
54 // destroy doesn't exit yet
55 QDECREF(qi);
56 qemu_free(ent->key);
57 qemu_free(ent);
58 qemu_free(qdict);
60 END_TEST
62 START_TEST(qdict_destroy_simple_test)
64 QDict *qdict;
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")));
70 QDECREF(qdict);
72 END_TEST
74 static QDict *tests_dict = NULL;
76 static void qdict_setup(void)
78 tests_dict = qdict_new();
79 fail_unless(tests_dict != NULL);
82 static void qdict_teardown(void)
84 QDECREF(tests_dict);
85 tests_dict = NULL;
88 START_TEST(qdict_get_test)
90 QInt *qi;
91 QObject *obj;
92 const int value = -42;
93 const char *key = "test";
95 qdict_put(tests_dict, key, qint_from_int(value));
97 obj = qdict_get(tests_dict, key);
98 fail_unless(obj != NULL);
100 qi = qobject_to_qint(obj);
101 fail_unless(qint_get_int(qi) == value);
103 END_TEST
105 START_TEST(qdict_get_int_test)
107 int ret;
108 const int value = 100;
109 const char *key = "int";
111 qdict_put(tests_dict, key, qint_from_int(value));
113 ret = qdict_get_int(tests_dict, key);
114 fail_unless(ret == value);
116 END_TEST
118 START_TEST(qdict_get_try_int_test)
120 int ret;
121 const int value = 100;
122 const char *key = "int";
124 qdict_put(tests_dict, key, qint_from_int(value));
126 ret = qdict_get_try_int(tests_dict, key, 0);
127 fail_unless(ret == value);
129 END_TEST
131 START_TEST(qdict_get_str_test)
133 const char *p;
134 const char *key = "key";
135 const char *str = "string";
137 qdict_put(tests_dict, key, qstring_from_str(str));
139 p = qdict_get_str(tests_dict, key);
140 fail_unless(p != NULL);
141 fail_unless(strcmp(p, str) == 0);
143 END_TEST
145 START_TEST(qdict_get_try_str_test)
147 const char *p;
148 const char *key = "key";
149 const char *str = "string";
151 qdict_put(tests_dict, key, qstring_from_str(str));
153 p = qdict_get_try_str(tests_dict, key);
154 fail_unless(p != NULL);
155 fail_unless(strcmp(p, str) == 0);
157 END_TEST
159 START_TEST(qdict_haskey_not_test)
161 fail_unless(qdict_haskey(tests_dict, "test") == 0);
163 END_TEST
165 START_TEST(qdict_haskey_test)
167 const char *key = "test";
169 qdict_put(tests_dict, key, qint_from_int(0));
170 fail_unless(qdict_haskey(tests_dict, key) == 1);
172 END_TEST
174 START_TEST(qdict_del_test)
176 const char *key = "key test";
178 qdict_put(tests_dict, key, qstring_from_str("foo"));
179 fail_unless(qdict_size(tests_dict) == 1);
181 qdict_del(tests_dict, key);
183 fail_unless(qdict_size(tests_dict) == 0);
184 fail_unless(qdict_haskey(tests_dict, key) == 0);
186 END_TEST
188 START_TEST(qobject_to_qdict_test)
190 fail_unless(qobject_to_qdict(QOBJECT(tests_dict)) == tests_dict);
192 END_TEST
195 * Errors test-cases
198 START_TEST(qdict_put_exists_test)
200 int value;
201 const char *key = "exists";
203 qdict_put(tests_dict, key, qint_from_int(1));
204 qdict_put(tests_dict, key, qint_from_int(2));
206 value = qdict_get_int(tests_dict, key);
207 fail_unless(value == 2);
209 fail_unless(qdict_size(tests_dict) == 1);
211 END_TEST
213 START_TEST(qdict_get_not_exists_test)
215 fail_unless(qdict_get(tests_dict, "foo") == NULL);
217 END_TEST
220 * Stress test-case
222 * This is a lot big for a unit-test, but there is no other place
223 * to have it.
226 static void remove_dots(char *string)
228 char *p = strchr(string, ':');
229 if (p)
230 *p = '\0';
233 static QString *read_line(FILE *file, char *key)
235 char value[128];
237 if (fscanf(file, "%s%s", key, value) == EOF)
238 return NULL;
239 remove_dots(key);
240 return qstring_from_str(value);
243 #define reset_file(file) fseek(file, 0L, SEEK_SET)
245 START_TEST(qdict_stress_test)
247 size_t lines;
248 char key[128];
249 FILE *test_file;
250 QDict *qdict;
251 QString *value;
252 const char *test_file_path = "qdict-test-data.txt";
254 test_file = fopen(test_file_path, "r");
255 fail_unless(test_file != NULL);
257 // Create the dict
258 qdict = qdict_new();
259 fail_unless(qdict != NULL);
261 // Add everything from the test file
262 for (lines = 0;; lines++) {
263 value = read_line(test_file, key);
264 if (!value)
265 break;
267 qdict_put(qdict, key, value);
269 fail_unless(qdict_size(qdict) == lines);
271 // Check if everything is really in there
272 reset_file(test_file);
273 for (;;) {
274 const char *str1, *str2;
276 value = read_line(test_file, key);
277 if (!value)
278 break;
280 str1 = qstring_get_str(value);
282 str2 = qdict_get_str(qdict, key);
283 fail_unless(str2 != NULL);
285 fail_unless(strcmp(str1, str2) == 0);
287 QDECREF(value);
290 // Delete everything
291 reset_file(test_file);
292 for (;;) {
293 value = read_line(test_file, key);
294 if (!value)
295 break;
297 qdict_del(qdict, key);
298 QDECREF(value);
300 fail_unless(qdict_haskey(qdict, key) == 0);
302 fclose(test_file);
304 fail_unless(qdict_size(qdict) == 0);
305 QDECREF(qdict);
307 END_TEST
309 static Suite *qdict_suite(void)
311 Suite *s;
312 TCase *qdict_public_tcase;
313 TCase *qdict_public2_tcase;
314 TCase *qdict_stress_tcase;
315 TCase *qdict_errors_tcase;
317 s = suite_create("QDict test-suite");
319 qdict_public_tcase = tcase_create("Public Interface");
320 suite_add_tcase(s, qdict_public_tcase);
321 tcase_add_test(qdict_public_tcase, qdict_new_test);
322 tcase_add_test(qdict_public_tcase, qdict_put_obj_test);
323 tcase_add_test(qdict_public_tcase, qdict_destroy_simple_test);
325 /* Continue, but now with fixtures */
326 qdict_public2_tcase = tcase_create("Public Interface (2)");
327 suite_add_tcase(s, qdict_public2_tcase);
328 tcase_add_checked_fixture(qdict_public2_tcase, qdict_setup, qdict_teardown);
329 tcase_add_test(qdict_public2_tcase, qdict_get_test);
330 tcase_add_test(qdict_public2_tcase, qdict_get_int_test);
331 tcase_add_test(qdict_public2_tcase, qdict_get_try_int_test);
332 tcase_add_test(qdict_public2_tcase, qdict_get_str_test);
333 tcase_add_test(qdict_public2_tcase, qdict_get_try_str_test);
334 tcase_add_test(qdict_public2_tcase, qdict_haskey_not_test);
335 tcase_add_test(qdict_public2_tcase, qdict_haskey_test);
336 tcase_add_test(qdict_public2_tcase, qdict_del_test);
337 tcase_add_test(qdict_public2_tcase, qobject_to_qdict_test);
339 qdict_errors_tcase = tcase_create("Errors");
340 suite_add_tcase(s, qdict_errors_tcase);
341 tcase_add_checked_fixture(qdict_errors_tcase, qdict_setup, qdict_teardown);
342 tcase_add_test(qdict_errors_tcase, qdict_put_exists_test);
343 tcase_add_test(qdict_errors_tcase, qdict_get_not_exists_test);
345 /* The Big one */
346 qdict_stress_tcase = tcase_create("Stress Test");
347 suite_add_tcase(s, qdict_stress_tcase);
348 tcase_add_test(qdict_stress_tcase, qdict_stress_test);
350 return s;
353 int main(void)
355 int nf;
356 Suite *s;
357 SRunner *sr;
359 s = qdict_suite();
360 sr = srunner_create(s);
362 srunner_run_all(sr, CK_NORMAL);
363 nf = srunner_ntests_failed(sr);
364 srunner_free(sr);
366 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;