4 * Copyright (C) 2009 Red Hat Inc.
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)
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
);
37 static void qdict_put_obj_test(void)
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
);
57 static void qdict_destroy_simple_test(void)
62 qdict_put_int(qdict
, "num", 0);
63 qdict_put_str(qdict
, "str", "foo");
68 static void qdict_get_test(void)
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)
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)
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)
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
);
135 g_assert(strcmp(p
, str
) == 0);
137 qobject_unref(tests_dict
);
140 static void qdict_get_try_str_test(void)
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
);
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)
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);
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);
217 g_assert(count
== qdict_size(tests_dict
));
219 /* Do it again to test restarting */
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);
226 g_assert(count
== qdict_size(tests_dict
));
228 qobject_unref(tests_dict
);
235 static void qdict_put_exists_test(void)
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
);
263 * This is a lot big for a unit-test, but there is no other place
267 static void remove_dots(char *string
)
269 char *p
= strchr(string
, ':');
274 static QString
*read_line(FILE *file
, char *key
)
278 if (fscanf(file
, "%127s%127s", key
, value
) == EOF
) {
282 return qstring_from_str(value
);
285 #define reset_file(file) fseek(file, 0L, SEEK_SET)
287 static void qdict_stress_test(void)
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
);
301 g_assert(qdict
!= NULL
);
303 // Add everything from the test file
304 for (lines
= 0;; lines
++) {
305 value
= read_line(test_file
, key
);
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
);
316 const char *str1
, *str2
;
318 value
= read_line(test_file
, key
);
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
);
333 reset_file(test_file
);
335 value
= read_line(test_file
, key
);
339 qdict_del(qdict
, key
);
340 qobject_unref(value
);
342 g_assert(qdict_haskey(qdict
, key
) == 0);
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
);
375 g_test_add_func("/stress/test", qdict_stress_test
);