Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / check-qdict.c
blobc37d4482279bf6132b295534ccfaaadd30b7b084
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 END_TEST
211 START_TEST(qdict_get_not_exists_test)
213 fail_unless(qdict_get(tests_dict, "foo") == NULL);
215 END_TEST
218 * Stress test-case
220 * This is a lot big for a unit-test, but there is no other place
221 * to have it.
224 static void remove_dots(char *string)
226 char *p = strchr(string, ':');
227 if (p)
228 *p = '\0';
231 static QString *read_line(FILE *file, char *key)
233 char value[128];
235 if (fscanf(file, "%s%s", key, value) == EOF)
236 return NULL;
237 remove_dots(key);
238 return qstring_from_str(value);
241 #define reset_file(file) fseek(file, 0L, SEEK_SET)
243 START_TEST(qdict_stress_test)
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";
252 test_file = fopen(test_file_path, "r");
253 fail_unless(test_file != NULL);
255 // Create the dict
256 qdict = qdict_new();
257 fail_unless(qdict != NULL);
259 // Add everything from the test file
260 for (lines = 0;; lines++) {
261 value = read_line(test_file, key);
262 if (!value)
263 break;
265 qdict_put(qdict, key, value);
267 fail_unless(qdict_size(qdict) == lines);
269 // Check if everything is really in there
270 reset_file(test_file);
271 for (;;) {
272 const char *str1, *str2;
274 value = read_line(test_file, key);
275 if (!value)
276 break;
278 str1 = qstring_get_str(value);
280 str2 = qdict_get_str(qdict, key);
281 fail_unless(str2 != NULL);
283 fail_unless(strcmp(str1, str2) == 0);
285 QDECREF(value);
288 // Delete everything
289 reset_file(test_file);
290 for (;;) {
291 value = read_line(test_file, key);
292 if (!value)
293 break;
295 qdict_del(qdict, key);
296 QDECREF(value);
298 fail_unless(qdict_haskey(qdict, key) == 0);
300 fclose(test_file);
302 fail_unless(qdict_size(qdict) == 0);
303 QDECREF(qdict);
305 END_TEST
307 static Suite *qdict_suite(void)
309 Suite *s;
310 TCase *qdict_public_tcase;
311 TCase *qdict_public2_tcase;
312 TCase *qdict_stress_tcase;
313 TCase *qdict_errors_tcase;
315 s = suite_create("QDict test-suite");
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);
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);
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);
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);
348 return s;
351 int main(void)
353 int nf;
354 Suite *s;
355 SRunner *sr;
357 s = qdict_suite();
358 sr = srunner_create(s);
360 srunner_run_all(sr, CK_NORMAL);
361 nf = srunner_ntests_failed(sr);
362 srunner_free(sr);
364 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;