monitor: Fix tracepoint crash on JSON syntax error
[qemu/armbru.git] / tests / check-qdict.c
blob86e9fe7dc488ade189a4131ccd1e484ae25bcafc
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"
17 * Public Interface test-cases
19 * (with some violations to access 'private' data)
22 static void qdict_new_test(void)
24 QDict *qdict;
26 qdict = qdict_new();
27 g_assert(qdict != NULL);
28 g_assert(qdict_size(qdict) == 0);
29 g_assert(qdict->base.refcnt == 1);
30 g_assert(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT);
32 qobject_unref(qdict);
35 static void qdict_put_obj_test(void)
37 QNum *qn;
38 QDict *qdict;
39 QDictEntry *ent;
40 const int num = 42;
42 qdict = qdict_new();
44 // key "" will have tdb hash 12345
45 qdict_put_int(qdict, "", num);
47 g_assert(qdict_size(qdict) == 1);
48 ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]);
49 qn = qobject_to(QNum, ent->value);
50 g_assert_cmpint(qnum_get_int(qn), ==, num);
52 qobject_unref(qdict);
55 static void qdict_destroy_simple_test(void)
57 QDict *qdict;
59 qdict = qdict_new();
60 qdict_put_int(qdict, "num", 0);
61 qdict_put_str(qdict, "str", "foo");
63 qobject_unref(qdict);
66 static void qdict_get_test(void)
68 QNum *qn;
69 QObject *obj;
70 const int value = -42;
71 const char *key = "test";
72 QDict *tests_dict = qdict_new();
74 qdict_put_int(tests_dict, key, value);
76 obj = qdict_get(tests_dict, key);
77 g_assert(obj != NULL);
79 qn = qobject_to(QNum, obj);
80 g_assert_cmpint(qnum_get_int(qn), ==, value);
82 qobject_unref(tests_dict);
85 static void qdict_get_int_test(void)
87 int ret;
88 const int value = 100;
89 const char *key = "int";
90 QDict *tests_dict = qdict_new();
92 qdict_put_int(tests_dict, key, value);
94 ret = qdict_get_int(tests_dict, key);
95 g_assert(ret == value);
97 qobject_unref(tests_dict);
100 static void qdict_get_try_int_test(void)
102 int ret;
103 const int value = 100;
104 const char *key = "int";
105 QDict *tests_dict = qdict_new();
107 qdict_put_int(tests_dict, key, value);
108 qdict_put_str(tests_dict, "string", "test");
110 ret = qdict_get_try_int(tests_dict, key, 0);
111 g_assert(ret == value);
113 ret = qdict_get_try_int(tests_dict, "missing", -42);
114 g_assert_cmpuint(ret, ==, -42);
116 ret = qdict_get_try_int(tests_dict, "string", -42);
117 g_assert_cmpuint(ret, ==, -42);
119 qobject_unref(tests_dict);
122 static void qdict_get_str_test(void)
124 const char *p;
125 const char *key = "key";
126 const char *str = "string";
127 QDict *tests_dict = qdict_new();
129 qdict_put_str(tests_dict, key, str);
131 p = qdict_get_str(tests_dict, key);
132 g_assert(p != NULL);
133 g_assert(strcmp(p, str) == 0);
135 qobject_unref(tests_dict);
138 static void qdict_get_try_str_test(void)
140 const char *p;
141 const char *key = "key";
142 const char *str = "string";
143 QDict *tests_dict = qdict_new();
145 qdict_put_str(tests_dict, key, str);
147 p = qdict_get_try_str(tests_dict, key);
148 g_assert(p != NULL);
149 g_assert(strcmp(p, str) == 0);
151 qobject_unref(tests_dict);
154 static void qdict_haskey_not_test(void)
156 QDict *tests_dict = qdict_new();
157 g_assert(qdict_haskey(tests_dict, "test") == 0);
159 qobject_unref(tests_dict);
162 static void qdict_haskey_test(void)
164 const char *key = "test";
165 QDict *tests_dict = qdict_new();
167 qdict_put_int(tests_dict, key, 0);
168 g_assert(qdict_haskey(tests_dict, key) == 1);
170 qobject_unref(tests_dict);
173 static void qdict_del_test(void)
175 const char *key = "key test";
176 QDict *tests_dict = qdict_new();
178 qdict_put_str(tests_dict, key, "foo");
179 g_assert(qdict_size(tests_dict) == 1);
181 qdict_del(tests_dict, key);
183 g_assert(qdict_size(tests_dict) == 0);
184 g_assert(qdict_haskey(tests_dict, key) == 0);
186 qobject_unref(tests_dict);
189 static void qobject_to_qdict_test(void)
191 QDict *tests_dict = qdict_new();
192 g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict);
194 qobject_unref(tests_dict);
197 static void qdict_iterapi_test(void)
199 int count;
200 const QDictEntry *ent;
201 QDict *tests_dict = qdict_new();
203 g_assert(qdict_first(tests_dict) == NULL);
205 qdict_put_int(tests_dict, "key1", 1);
206 qdict_put_int(tests_dict, "key2", 2);
207 qdict_put_int(tests_dict, "key3", 3);
209 count = 0;
210 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
211 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
212 count++;
215 g_assert(count == qdict_size(tests_dict));
217 /* Do it again to test restarting */
218 count = 0;
219 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
220 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
221 count++;
224 g_assert(count == qdict_size(tests_dict));
226 qobject_unref(tests_dict);
230 * Errors test-cases
233 static void qdict_put_exists_test(void)
235 int value;
236 const char *key = "exists";
237 QDict *tests_dict = qdict_new();
239 qdict_put_int(tests_dict, key, 1);
240 qdict_put_int(tests_dict, key, 2);
242 value = qdict_get_int(tests_dict, key);
243 g_assert(value == 2);
245 g_assert(qdict_size(tests_dict) == 1);
247 qobject_unref(tests_dict);
250 static void qdict_get_not_exists_test(void)
252 QDict *tests_dict = qdict_new();
253 g_assert(qdict_get(tests_dict, "foo") == NULL);
255 qobject_unref(tests_dict);
259 * Stress test-case
261 * This is a lot big for a unit-test, but there is no other place
262 * to have it.
265 static void remove_dots(char *string)
267 char *p = strchr(string, ':');
268 if (p)
269 *p = '\0';
272 static QString *read_line(FILE *file, char *key)
274 char value[128];
276 if (fscanf(file, "%127s%127s", key, value) == EOF) {
277 return NULL;
279 remove_dots(key);
280 return qstring_from_str(value);
283 #define reset_file(file) fseek(file, 0L, SEEK_SET)
285 static void qdict_stress_test(void)
287 size_t lines;
288 char key[128];
289 FILE *test_file;
290 QDict *qdict;
291 QString *value;
292 const char *test_file_path = "qdict-test-data.txt";
294 test_file = fopen(test_file_path, "r");
295 g_assert(test_file != NULL);
297 // Create the dict
298 qdict = qdict_new();
299 g_assert(qdict != NULL);
301 // Add everything from the test file
302 for (lines = 0;; lines++) {
303 value = read_line(test_file, key);
304 if (!value)
305 break;
307 qdict_put(qdict, key, value);
309 g_assert(qdict_size(qdict) == lines);
311 // Check if everything is really in there
312 reset_file(test_file);
313 for (;;) {
314 const char *str1, *str2;
316 value = read_line(test_file, key);
317 if (!value)
318 break;
320 str1 = qstring_get_str(value);
322 str2 = qdict_get_str(qdict, key);
323 g_assert(str2 != NULL);
325 g_assert(strcmp(str1, str2) == 0);
327 qobject_unref(value);
330 // Delete everything
331 reset_file(test_file);
332 for (;;) {
333 value = read_line(test_file, key);
334 if (!value)
335 break;
337 qdict_del(qdict, key);
338 qobject_unref(value);
340 g_assert(qdict_haskey(qdict, key) == 0);
342 fclose(test_file);
344 g_assert(qdict_size(qdict) == 0);
345 qobject_unref(qdict);
348 int main(int argc, char **argv)
350 g_test_init(&argc, &argv, NULL);
352 g_test_add_func("/public/new", qdict_new_test);
353 g_test_add_func("/public/put_obj", qdict_put_obj_test);
354 g_test_add_func("/public/destroy_simple", qdict_destroy_simple_test);
356 /* Continue, but now with fixtures */
357 g_test_add_func("/public/get", qdict_get_test);
358 g_test_add_func("/public/get_int", qdict_get_int_test);
359 g_test_add_func("/public/get_try_int", qdict_get_try_int_test);
360 g_test_add_func("/public/get_str", qdict_get_str_test);
361 g_test_add_func("/public/get_try_str", qdict_get_try_str_test);
362 g_test_add_func("/public/haskey_not", qdict_haskey_not_test);
363 g_test_add_func("/public/haskey", qdict_haskey_test);
364 g_test_add_func("/public/del", qdict_del_test);
365 g_test_add_func("/public/to_qdict", qobject_to_qdict_test);
366 g_test_add_func("/public/iterapi", qdict_iterapi_test);
368 g_test_add_func("/errors/put_exists", qdict_put_exists_test);
369 g_test_add_func("/errors/get_not_exists", qdict_get_not_exists_test);
371 /* The Big one */
372 if (g_test_slow()) {
373 g_test_add_func("/stress/test", qdict_stress_test);
376 return g_test_run();