blob: 86e9fe7dc488ade189a4131ccd1e484ae25bcafc [file] [log] [blame]
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -03001/*
2 * QDict unit-tests.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
Luiz Capitulino41836a92010-05-12 16:34:42 -03008 *
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.
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030011 */
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030012
Markus Armbruster47e6b292018-02-01 12:18:38 +010013#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/qmp/qdict.h"
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030015
16/*
17 * Public Interface test-cases
18 *
19 * (with some violations to access 'private' data)
20 */
21
Anthony Liguoriac531cb2012-01-10 13:10:44 -060022static void qdict_new_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030023{
24 QDict *qdict;
25
26 qdict = qdict_new();
Anthony Liguoriac531cb2012-01-10 13:10:44 -060027 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);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030031
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020032 qobject_unref(qdict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030033}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030034
Anthony Liguoriac531cb2012-01-10 13:10:44 -060035static void qdict_put_obj_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030036{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040037 QNum *qn;
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030038 QDict *qdict;
39 QDictEntry *ent;
40 const int num = 42;
41
42 qdict = qdict_new();
43
44 // key "" will have tdb hash 12345
Eric Blake46f5ac22017-04-27 16:58:17 -050045 qdict_put_int(qdict, "", num);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030046
Anthony Liguoriac531cb2012-01-10 13:10:44 -060047 g_assert(qdict_size(qdict) == 1);
Luiz Capitulinoc8bc3cd2010-06-07 15:45:22 -030048 ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]);
Max Reitz7dc847e2018-02-24 16:40:29 +010049 qn = qobject_to(QNum, ent->value);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040050 g_assert_cmpint(qnum_get_int(qn), ==, num);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030051
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020052 qobject_unref(qdict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030053}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030054
Anthony Liguoriac531cb2012-01-10 13:10:44 -060055static void qdict_destroy_simple_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030056{
57 QDict *qdict;
58
59 qdict = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -050060 qdict_put_int(qdict, "num", 0);
61 qdict_put_str(qdict, "str", "foo");
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030062
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020063 qobject_unref(qdict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030064}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030065
Anthony Liguoriac531cb2012-01-10 13:10:44 -060066static void qdict_get_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030067{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040068 QNum *qn;
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030069 QObject *obj;
70 const int value = -42;
71 const char *key = "test";
Anthony Liguoriac531cb2012-01-10 13:10:44 -060072 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030073
Eric Blake46f5ac22017-04-27 16:58:17 -050074 qdict_put_int(tests_dict, key, value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030075
76 obj = qdict_get(tests_dict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -060077 g_assert(obj != NULL);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030078
Max Reitz7dc847e2018-02-24 16:40:29 +010079 qn = qobject_to(QNum, obj);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040080 g_assert_cmpint(qnum_get_int(qn), ==, value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030081
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020082 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -060083}
84
85static void qdict_get_int_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030086{
87 int ret;
88 const int value = 100;
89 const char *key = "int";
Anthony Liguoriac531cb2012-01-10 13:10:44 -060090 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030091
Eric Blake46f5ac22017-04-27 16:58:17 -050092 qdict_put_int(tests_dict, key, value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030093
94 ret = qdict_get_int(tests_dict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -060095 g_assert(ret == value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -030096
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020097 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -060098}
99
100static void qdict_get_try_int_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300101{
102 int ret;
103 const int value = 100;
104 const char *key = "int";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600105 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300106
Eric Blake46f5ac22017-04-27 16:58:17 -0500107 qdict_put_int(tests_dict, key, value);
Marc-André Lureau269c20b2017-06-07 20:36:33 +0400108 qdict_put_str(tests_dict, "string", "test");
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300109
110 ret = qdict_get_try_int(tests_dict, key, 0);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600111 g_assert(ret == value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300112
Marc-André Lureau269c20b2017-06-07 20:36:33 +0400113 ret = qdict_get_try_int(tests_dict, "missing", -42);
114 g_assert_cmpuint(ret, ==, -42);
115
116 ret = qdict_get_try_int(tests_dict, "string", -42);
117 g_assert_cmpuint(ret, ==, -42);
118
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200119 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600120}
121
122static void qdict_get_str_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300123{
124 const char *p;
125 const char *key = "key";
126 const char *str = "string";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600127 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300128
Eric Blake46f5ac22017-04-27 16:58:17 -0500129 qdict_put_str(tests_dict, key, str);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300130
131 p = qdict_get_str(tests_dict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600132 g_assert(p != NULL);
133 g_assert(strcmp(p, str) == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300134
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200135 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600136}
137
138static void qdict_get_try_str_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300139{
140 const char *p;
141 const char *key = "key";
142 const char *str = "string";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600143 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300144
Eric Blake46f5ac22017-04-27 16:58:17 -0500145 qdict_put_str(tests_dict, key, str);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300146
147 p = qdict_get_try_str(tests_dict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600148 g_assert(p != NULL);
149 g_assert(strcmp(p, str) == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300150
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200151 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600152}
153
154static void qdict_haskey_not_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300155{
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600156 QDict *tests_dict = qdict_new();
157 g_assert(qdict_haskey(tests_dict, "test") == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300158
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200159 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600160}
161
162static void qdict_haskey_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300163{
164 const char *key = "test";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600165 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300166
Eric Blake46f5ac22017-04-27 16:58:17 -0500167 qdict_put_int(tests_dict, key, 0);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600168 g_assert(qdict_haskey(tests_dict, key) == 1);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300169
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200170 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600171}
172
173static void qdict_del_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300174{
175 const char *key = "key test";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600176 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300177
Eric Blake46f5ac22017-04-27 16:58:17 -0500178 qdict_put_str(tests_dict, key, "foo");
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600179 g_assert(qdict_size(tests_dict) == 1);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300180
181 qdict_del(tests_dict, key);
182
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600183 g_assert(qdict_size(tests_dict) == 0);
184 g_assert(qdict_haskey(tests_dict, key) == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300185
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200186 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600187}
188
189static void qobject_to_qdict_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300190{
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600191 QDict *tests_dict = qdict_new();
Max Reitz7dc847e2018-02-24 16:40:29 +0100192 g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300193
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200194 qobject_unref(tests_dict);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600195}
196
197static void qdict_iterapi_test(void)
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300198{
199 int count;
200 const QDictEntry *ent;
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600201 QDict *tests_dict = qdict_new();
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300202
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600203 g_assert(qdict_first(tests_dict) == NULL);
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300204
Eric Blake46f5ac22017-04-27 16:58:17 -0500205 qdict_put_int(tests_dict, "key1", 1);
206 qdict_put_int(tests_dict, "key2", 2);
207 qdict_put_int(tests_dict, "key3", 3);
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300208
209 count = 0;
210 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600211 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300212 count++;
213 }
214
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600215 g_assert(count == qdict_size(tests_dict));
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300216
217 /* Do it again to test restarting */
218 count = 0;
219 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600220 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300221 count++;
222 }
223
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600224 g_assert(count == qdict_size(tests_dict));
225
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200226 qobject_unref(tests_dict);
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300227}
Luiz Capitulinod02c6bd2010-06-07 15:29:58 -0300228
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300229/*
230 * Errors test-cases
231 */
232
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600233static void qdict_put_exists_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300234{
235 int value;
236 const char *key = "exists";
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600237 QDict *tests_dict = qdict_new();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300238
Eric Blake46f5ac22017-04-27 16:58:17 -0500239 qdict_put_int(tests_dict, key, 1);
240 qdict_put_int(tests_dict, key, 2);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300241
242 value = qdict_get_int(tests_dict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600243 g_assert(value == 2);
Luiz Capitulino29ec3152009-12-14 18:53:20 -0200244
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600245 g_assert(qdict_size(tests_dict) == 1);
246
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200247 qobject_unref(tests_dict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300248}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300249
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600250static void qdict_get_not_exists_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300251{
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600252 QDict *tests_dict = qdict_new();
253 g_assert(qdict_get(tests_dict, "foo") == NULL);
254
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200255 qobject_unref(tests_dict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300256}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300257
258/*
259 * Stress test-case
260 *
261 * This is a lot big for a unit-test, but there is no other place
262 * to have it.
263 */
264
265static void remove_dots(char *string)
266{
267 char *p = strchr(string, ':');
268 if (p)
269 *p = '\0';
270}
271
272static QString *read_line(FILE *file, char *key)
273{
274 char value[128];
275
Stefan Weil7464f052011-01-21 22:50:30 +0100276 if (fscanf(file, "%127s%127s", key, value) == EOF) {
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300277 return NULL;
Stefan Weil7464f052011-01-21 22:50:30 +0100278 }
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300279 remove_dots(key);
280 return qstring_from_str(value);
281}
282
283#define reset_file(file) fseek(file, 0L, SEEK_SET)
284
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600285static void qdict_stress_test(void)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300286{
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";
293
294 test_file = fopen(test_file_path, "r");
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600295 g_assert(test_file != NULL);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300296
297 // Create the dict
298 qdict = qdict_new();
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600299 g_assert(qdict != NULL);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300300
301 // Add everything from the test file
302 for (lines = 0;; lines++) {
303 value = read_line(test_file, key);
304 if (!value)
305 break;
306
307 qdict_put(qdict, key, value);
308 }
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600309 g_assert(qdict_size(qdict) == lines);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300310
311 // Check if everything is really in there
312 reset_file(test_file);
313 for (;;) {
314 const char *str1, *str2;
315
316 value = read_line(test_file, key);
317 if (!value)
318 break;
319
320 str1 = qstring_get_str(value);
321
322 str2 = qdict_get_str(qdict, key);
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600323 g_assert(str2 != NULL);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300324
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600325 g_assert(strcmp(str1, str2) == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300326
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200327 qobject_unref(value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300328 }
329
330 // Delete everything
331 reset_file(test_file);
332 for (;;) {
333 value = read_line(test_file, key);
334 if (!value)
335 break;
336
337 qdict_del(qdict, key);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200338 qobject_unref(value);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300339
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600340 g_assert(qdict_haskey(qdict, key) == 0);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300341 }
342 fclose(test_file);
343
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600344 g_assert(qdict_size(qdict) == 0);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200345 qobject_unref(qdict);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300346}
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300347
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600348int main(int argc, char **argv)
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300349{
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600350 g_test_init(&argc, &argv, NULL);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300351
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600352 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);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300355
356 /* Continue, but now with fixtures */
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600357 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);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300367
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600368 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);
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300370
371 /* The Big one */
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600372 if (g_test_slow()) {
373 g_test_add_func("/stress/test", qdict_stress_test);
374 }
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300375
Anthony Liguoriac531cb2012-01-10 13:10:44 -0600376 return g_test_run();
Luiz Capitulino7b8c51a2009-08-28 15:27:32 -0300377}