hw/net: fix vmxnet3 live migration
[qemu/ar7.git] / tests / check-qdict.c
blobb5efa859b0b11b3f9e6ede532ae32804e3245a1b
1 /*
2 * QDict unit-tests.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qnum.h"
16 #include "qapi/qmp/qstring.h"
19 * Public Interface test-cases
21 * (with some violations to access 'private' data)
24 static void qdict_new_test(void)
26 QDict *qdict;
28 qdict = qdict_new();
29 g_assert(qdict != NULL);
30 g_assert(qdict_size(qdict) == 0);
31 g_assert(qdict->base.refcnt == 1);
32 g_assert(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT);
34 qobject_unref(qdict);
37 static void qdict_put_obj_test(void)
39 QNum *qn;
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_int(qdict, "", num);
49 g_assert(qdict_size(qdict) == 1);
50 ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]);
51 qn = qobject_to(QNum, ent->value);
52 g_assert_cmpint(qnum_get_int(qn), ==, num);
54 qobject_unref(qdict);
57 static void qdict_destroy_simple_test(void)
59 QDict *qdict;
61 qdict = qdict_new();
62 qdict_put_int(qdict, "num", 0);
63 qdict_put_str(qdict, "str", "foo");
65 qobject_unref(qdict);
68 static void qdict_get_test(void)
70 QNum *qn;
71 QObject *obj;
72 const int value = -42;
73 const char *key = "test";
74 QDict *tests_dict = qdict_new();
76 qdict_put_int(tests_dict, key, value);
78 obj = qdict_get(tests_dict, key);
79 g_assert(obj != NULL);
81 qn = qobject_to(QNum, obj);
82 g_assert_cmpint(qnum_get_int(qn), ==, value);
84 qobject_unref(tests_dict);
87 static void qdict_get_int_test(void)
89 int ret;
90 const int value = 100;
91 const char *key = "int";
92 QDict *tests_dict = qdict_new();
94 qdict_put_int(tests_dict, key, value);
96 ret = qdict_get_int(tests_dict, key);
97 g_assert(ret == value);
99 qobject_unref(tests_dict);
102 static void qdict_get_try_int_test(void)
104 int ret;
105 const int value = 100;
106 const char *key = "int";
107 QDict *tests_dict = qdict_new();
109 qdict_put_int(tests_dict, key, value);
110 qdict_put_str(tests_dict, "string", "test");
112 ret = qdict_get_try_int(tests_dict, key, 0);
113 g_assert(ret == value);
115 ret = qdict_get_try_int(tests_dict, "missing", -42);
116 g_assert_cmpuint(ret, ==, -42);
118 ret = qdict_get_try_int(tests_dict, "string", -42);
119 g_assert_cmpuint(ret, ==, -42);
121 qobject_unref(tests_dict);
124 static void qdict_get_str_test(void)
126 const char *p;
127 const char *key = "key";
128 const char *str = "string";
129 QDict *tests_dict = qdict_new();
131 qdict_put_str(tests_dict, key, str);
133 p = qdict_get_str(tests_dict, key);
134 g_assert(p != NULL);
135 g_assert(strcmp(p, str) == 0);
137 qobject_unref(tests_dict);
140 static void qdict_get_try_str_test(void)
142 const char *p;
143 const char *key = "key";
144 const char *str = "string";
145 QDict *tests_dict = qdict_new();
147 qdict_put_str(tests_dict, key, str);
149 p = qdict_get_try_str(tests_dict, key);
150 g_assert(p != NULL);
151 g_assert(strcmp(p, str) == 0);
153 qobject_unref(tests_dict);
156 static void qdict_haskey_not_test(void)
158 QDict *tests_dict = qdict_new();
159 g_assert(qdict_haskey(tests_dict, "test") == 0);
161 qobject_unref(tests_dict);
164 static void qdict_haskey_test(void)
166 const char *key = "test";
167 QDict *tests_dict = qdict_new();
169 qdict_put_int(tests_dict, key, 0);
170 g_assert(qdict_haskey(tests_dict, key) == 1);
172 qobject_unref(tests_dict);
175 static void qdict_del_test(void)
177 const char *key = "key test";
178 QDict *tests_dict = qdict_new();
180 qdict_put_str(tests_dict, key, "foo");
181 g_assert(qdict_size(tests_dict) == 1);
183 qdict_del(tests_dict, key);
185 g_assert(qdict_size(tests_dict) == 0);
186 g_assert(qdict_haskey(tests_dict, key) == 0);
188 qobject_unref(tests_dict);
191 static void qobject_to_qdict_test(void)
193 QDict *tests_dict = qdict_new();
194 g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict);
196 qobject_unref(tests_dict);
199 static void qdict_iterapi_test(void)
201 int count;
202 const QDictEntry *ent;
203 QDict *tests_dict = qdict_new();
205 g_assert(qdict_first(tests_dict) == NULL);
207 qdict_put_int(tests_dict, "key1", 1);
208 qdict_put_int(tests_dict, "key2", 2);
209 qdict_put_int(tests_dict, "key3", 3);
211 count = 0;
212 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
213 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
214 count++;
217 g_assert(count == qdict_size(tests_dict));
219 /* Do it again to test restarting */
220 count = 0;
221 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
222 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
223 count++;
226 g_assert(count == qdict_size(tests_dict));
228 qobject_unref(tests_dict);
232 * Errors test-cases
235 static void qdict_put_exists_test(void)
237 int value;
238 const char *key = "exists";
239 QDict *tests_dict = qdict_new();
241 qdict_put_int(tests_dict, key, 1);
242 qdict_put_int(tests_dict, key, 2);
244 value = qdict_get_int(tests_dict, key);
245 g_assert(value == 2);
247 g_assert(qdict_size(tests_dict) == 1);
249 qobject_unref(tests_dict);
252 static void qdict_get_not_exists_test(void)
254 QDict *tests_dict = qdict_new();
255 g_assert(qdict_get(tests_dict, "foo") == NULL);
257 qobject_unref(tests_dict);
261 * Stress test-case
263 * This is a lot big for a unit-test, but there is no other place
264 * to have it.
267 static void remove_dots(char *string)
269 char *p = strchr(string, ':');
270 if (p)
271 *p = '\0';
274 static QString *read_line(FILE *file, char *key)
276 char value[128];
278 if (fscanf(file, "%127s%127s", key, value) == EOF) {
279 return NULL;
281 remove_dots(key);
282 return qstring_from_str(value);
285 #define reset_file(file) fseek(file, 0L, SEEK_SET)
287 static void qdict_stress_test(void)
289 size_t lines;
290 char key[128];
291 FILE *test_file;
292 QDict *qdict;
293 QString *value;
294 const char *test_file_path = "tests/data/qobject/qdict.txt";
296 test_file = fopen(test_file_path, "r");
297 g_assert(test_file != NULL);
299 // Create the dict
300 qdict = qdict_new();
301 g_assert(qdict != NULL);
303 // Add everything from the test file
304 for (lines = 0;; lines++) {
305 value = read_line(test_file, key);
306 if (!value)
307 break;
309 qdict_put(qdict, key, value);
311 g_assert(qdict_size(qdict) == lines);
313 // Check if everything is really in there
314 reset_file(test_file);
315 for (;;) {
316 const char *str1, *str2;
318 value = read_line(test_file, key);
319 if (!value)
320 break;
322 str1 = qstring_get_str(value);
324 str2 = qdict_get_str(qdict, key);
325 g_assert(str2 != NULL);
327 g_assert(strcmp(str1, str2) == 0);
329 qobject_unref(value);
332 // Delete everything
333 reset_file(test_file);
334 for (;;) {
335 value = read_line(test_file, key);
336 if (!value)
337 break;
339 qdict_del(qdict, key);
340 qobject_unref(value);
342 g_assert(qdict_haskey(qdict, key) == 0);
344 fclose(test_file);
346 g_assert(qdict_size(qdict) == 0);
347 qobject_unref(qdict);
350 int main(int argc, char **argv)
352 g_test_init(&argc, &argv, NULL);
354 g_test_add_func("/public/new", qdict_new_test);
355 g_test_add_func("/public/put_obj", qdict_put_obj_test);
356 g_test_add_func("/public/destroy_simple", qdict_destroy_simple_test);
358 /* Continue, but now with fixtures */
359 g_test_add_func("/public/get", qdict_get_test);
360 g_test_add_func("/public/get_int", qdict_get_int_test);
361 g_test_add_func("/public/get_try_int", qdict_get_try_int_test);
362 g_test_add_func("/public/get_str", qdict_get_str_test);
363 g_test_add_func("/public/get_try_str", qdict_get_try_str_test);
364 g_test_add_func("/public/haskey_not", qdict_haskey_not_test);
365 g_test_add_func("/public/haskey", qdict_haskey_test);
366 g_test_add_func("/public/del", qdict_del_test);
367 g_test_add_func("/public/to_qdict", qobject_to_qdict_test);
368 g_test_add_func("/public/iterapi", qdict_iterapi_test);
370 g_test_add_func("/errors/put_exists", qdict_put_exists_test);
371 g_test_add_func("/errors/get_not_exists", qdict_get_not_exists_test);
373 /* The Big one */
374 if (g_test_slow()) {
375 g_test_add_func("/stress/test", qdict_stress_test);
378 return g_test_run();