]> git.proxmox.com Git - mirror_qemu.git/blame - tests/check-qdict.c
block: Add flags to BlockDriver.bdrv_co_truncate()
[mirror_qemu.git] / tests / check-qdict.c
CommitLineData
7b8c51ad
LC
1/*
2 * QDict unit-tests.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
41836a9f
LC
8 *
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.
7b8c51ad 11 */
7b8c51ad 12
47e6b297 13#include "qemu/osdep.h"
7b1b5d19 14#include "qapi/qmp/qdict.h"
992159c7
MA
15#include "qapi/qmp/qnum.h"
16#include "qapi/qmp/qstring.h"
7b8c51ad
LC
17
18/*
19 * Public Interface test-cases
20 *
21 * (with some violations to access 'private' data)
22 */
23
ac531cb6 24static void qdict_new_test(void)
7b8c51ad
LC
25{
26 QDict *qdict;
27
28 qdict = qdict_new();
ac531cb6
AL
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);
7b8c51ad 33
cb3e7f08 34 qobject_unref(qdict);
7b8c51ad 35}
7b8c51ad 36
ac531cb6 37static void qdict_put_obj_test(void)
7b8c51ad 38{
01b2ffce 39 QNum *qn;
7b8c51ad
LC
40 QDict *qdict;
41 QDictEntry *ent;
42 const int num = 42;
43
44 qdict = qdict_new();
45
46 // key "" will have tdb hash 12345
46f5ac20 47 qdict_put_int(qdict, "", num);
7b8c51ad 48
ac531cb6 49 g_assert(qdict_size(qdict) == 1);
c8bc3cd7 50 ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]);
7dc847eb 51 qn = qobject_to(QNum, ent->value);
01b2ffce 52 g_assert_cmpint(qnum_get_int(qn), ==, num);
7b8c51ad 53
cb3e7f08 54 qobject_unref(qdict);
7b8c51ad 55}
7b8c51ad 56
ac531cb6 57static void qdict_destroy_simple_test(void)
7b8c51ad
LC
58{
59 QDict *qdict;
60
61 qdict = qdict_new();
46f5ac20
EB
62 qdict_put_int(qdict, "num", 0);
63 qdict_put_str(qdict, "str", "foo");
7b8c51ad 64
cb3e7f08 65 qobject_unref(qdict);
7b8c51ad 66}
7b8c51ad 67
ac531cb6 68static void qdict_get_test(void)
7b8c51ad 69{
01b2ffce 70 QNum *qn;
7b8c51ad
LC
71 QObject *obj;
72 const int value = -42;
73 const char *key = "test";
ac531cb6 74 QDict *tests_dict = qdict_new();
7b8c51ad 75
46f5ac20 76 qdict_put_int(tests_dict, key, value);
7b8c51ad
LC
77
78 obj = qdict_get(tests_dict, key);
ac531cb6 79 g_assert(obj != NULL);
7b8c51ad 80
7dc847eb 81 qn = qobject_to(QNum, obj);
01b2ffce 82 g_assert_cmpint(qnum_get_int(qn), ==, value);
ac531cb6 83
cb3e7f08 84 qobject_unref(tests_dict);
7b8c51ad 85}
7b8c51ad 86
ac531cb6 87static void qdict_get_int_test(void)
7b8c51ad
LC
88{
89 int ret;
90 const int value = 100;
91 const char *key = "int";
ac531cb6 92 QDict *tests_dict = qdict_new();
7b8c51ad 93
46f5ac20 94 qdict_put_int(tests_dict, key, value);
7b8c51ad
LC
95
96 ret = qdict_get_int(tests_dict, key);
ac531cb6
AL
97 g_assert(ret == value);
98
cb3e7f08 99 qobject_unref(tests_dict);
7b8c51ad 100}
7b8c51ad 101
ac531cb6 102static void qdict_get_try_int_test(void)
7b8c51ad
LC
103{
104 int ret;
105 const int value = 100;
106 const char *key = "int";
ac531cb6 107 QDict *tests_dict = qdict_new();
7b8c51ad 108
46f5ac20 109 qdict_put_int(tests_dict, key, value);
269c20b2 110 qdict_put_str(tests_dict, "string", "test");
7b8c51ad
LC
111
112 ret = qdict_get_try_int(tests_dict, key, 0);
ac531cb6
AL
113 g_assert(ret == value);
114
269c20b2
MAL
115 ret = qdict_get_try_int(tests_dict, "missing", -42);
116 g_assert_cmpuint(ret, ==, -42);
117
118 ret = qdict_get_try_int(tests_dict, "string", -42);
119 g_assert_cmpuint(ret, ==, -42);
120
cb3e7f08 121 qobject_unref(tests_dict);
7b8c51ad 122}
7b8c51ad 123
ac531cb6 124static void qdict_get_str_test(void)
7b8c51ad
LC
125{
126 const char *p;
127 const char *key = "key";
128 const char *str = "string";
ac531cb6 129 QDict *tests_dict = qdict_new();
7b8c51ad 130
46f5ac20 131 qdict_put_str(tests_dict, key, str);
7b8c51ad
LC
132
133 p = qdict_get_str(tests_dict, key);
ac531cb6
AL
134 g_assert(p != NULL);
135 g_assert(strcmp(p, str) == 0);
136
cb3e7f08 137 qobject_unref(tests_dict);
7b8c51ad 138}
7b8c51ad 139
ac531cb6 140static void qdict_get_try_str_test(void)
7b8c51ad
LC
141{
142 const char *p;
143 const char *key = "key";
144 const char *str = "string";
ac531cb6 145 QDict *tests_dict = qdict_new();
7b8c51ad 146
46f5ac20 147 qdict_put_str(tests_dict, key, str);
7b8c51ad
LC
148
149 p = qdict_get_try_str(tests_dict, key);
ac531cb6
AL
150 g_assert(p != NULL);
151 g_assert(strcmp(p, str) == 0);
152
cb3e7f08 153 qobject_unref(tests_dict);
7b8c51ad 154}
7b8c51ad 155
ac531cb6 156static void qdict_haskey_not_test(void)
7b8c51ad 157{
ac531cb6
AL
158 QDict *tests_dict = qdict_new();
159 g_assert(qdict_haskey(tests_dict, "test") == 0);
160
cb3e7f08 161 qobject_unref(tests_dict);
7b8c51ad 162}
7b8c51ad 163
ac531cb6 164static void qdict_haskey_test(void)
7b8c51ad
LC
165{
166 const char *key = "test";
ac531cb6 167 QDict *tests_dict = qdict_new();
7b8c51ad 168
46f5ac20 169 qdict_put_int(tests_dict, key, 0);
ac531cb6
AL
170 g_assert(qdict_haskey(tests_dict, key) == 1);
171
cb3e7f08 172 qobject_unref(tests_dict);
7b8c51ad 173}
7b8c51ad 174
ac531cb6 175static void qdict_del_test(void)
7b8c51ad
LC
176{
177 const char *key = "key test";
ac531cb6 178 QDict *tests_dict = qdict_new();
7b8c51ad 179
46f5ac20 180 qdict_put_str(tests_dict, key, "foo");
ac531cb6 181 g_assert(qdict_size(tests_dict) == 1);
7b8c51ad
LC
182
183 qdict_del(tests_dict, key);
184
ac531cb6
AL
185 g_assert(qdict_size(tests_dict) == 0);
186 g_assert(qdict_haskey(tests_dict, key) == 0);
187
cb3e7f08 188 qobject_unref(tests_dict);
7b8c51ad 189}
7b8c51ad 190
ac531cb6 191static void qobject_to_qdict_test(void)
7b8c51ad 192{
ac531cb6 193 QDict *tests_dict = qdict_new();
7dc847eb 194 g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict);
ac531cb6 195
cb3e7f08 196 qobject_unref(tests_dict);
7b8c51ad 197}
7b8c51ad 198
ac531cb6 199static void qdict_iterapi_test(void)
d02c6bd4
LC
200{
201 int count;
202 const QDictEntry *ent;
ac531cb6 203 QDict *tests_dict = qdict_new();
d02c6bd4 204
ac531cb6 205 g_assert(qdict_first(tests_dict) == NULL);
d02c6bd4 206
46f5ac20
EB
207 qdict_put_int(tests_dict, "key1", 1);
208 qdict_put_int(tests_dict, "key2", 2);
209 qdict_put_int(tests_dict, "key3", 3);
d02c6bd4
LC
210
211 count = 0;
212 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
ac531cb6 213 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
d02c6bd4
LC
214 count++;
215 }
216
ac531cb6 217 g_assert(count == qdict_size(tests_dict));
d02c6bd4
LC
218
219 /* Do it again to test restarting */
220 count = 0;
221 for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
ac531cb6 222 g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
d02c6bd4
LC
223 count++;
224 }
225
ac531cb6
AL
226 g_assert(count == qdict_size(tests_dict));
227
cb3e7f08 228 qobject_unref(tests_dict);
d02c6bd4 229}
d02c6bd4 230
7b8c51ad
LC
231/*
232 * Errors test-cases
233 */
234
ac531cb6 235static void qdict_put_exists_test(void)
7b8c51ad
LC
236{
237 int value;
238 const char *key = "exists";
ac531cb6 239 QDict *tests_dict = qdict_new();
7b8c51ad 240
46f5ac20
EB
241 qdict_put_int(tests_dict, key, 1);
242 qdict_put_int(tests_dict, key, 2);
7b8c51ad
LC
243
244 value = qdict_get_int(tests_dict, key);
ac531cb6 245 g_assert(value == 2);
29ec3156 246
ac531cb6
AL
247 g_assert(qdict_size(tests_dict) == 1);
248
cb3e7f08 249 qobject_unref(tests_dict);
7b8c51ad 250}
7b8c51ad 251
ac531cb6 252static void qdict_get_not_exists_test(void)
7b8c51ad 253{
ac531cb6
AL
254 QDict *tests_dict = qdict_new();
255 g_assert(qdict_get(tests_dict, "foo") == NULL);
256
cb3e7f08 257 qobject_unref(tests_dict);
7b8c51ad 258}
7b8c51ad
LC
259
260/*
261 * Stress test-case
262 *
263 * This is a lot big for a unit-test, but there is no other place
264 * to have it.
265 */
266
267static void remove_dots(char *string)
268{
269 char *p = strchr(string, ':');
270 if (p)
271 *p = '\0';
272}
273
274static QString *read_line(FILE *file, char *key)
275{
276 char value[128];
277
7464f058 278 if (fscanf(file, "%127s%127s", key, value) == EOF) {
7b8c51ad 279 return NULL;
7464f058 280 }
7b8c51ad
LC
281 remove_dots(key);
282 return qstring_from_str(value);
283}
284
285#define reset_file(file) fseek(file, 0L, SEEK_SET)
286
ac531cb6 287static void qdict_stress_test(void)
7b8c51ad
LC
288{
289 size_t lines;
290 char key[128];
291 FILE *test_file;
292 QDict *qdict;
293 QString *value;
fe07b62c 294 const char *test_file_path = "tests/data/qobject/qdict.txt";
7b8c51ad
LC
295
296 test_file = fopen(test_file_path, "r");
ac531cb6 297 g_assert(test_file != NULL);
7b8c51ad
LC
298
299 // Create the dict
300 qdict = qdict_new();
ac531cb6 301 g_assert(qdict != NULL);
7b8c51ad
LC
302
303 // Add everything from the test file
304 for (lines = 0;; lines++) {
305 value = read_line(test_file, key);
306 if (!value)
307 break;
308
309 qdict_put(qdict, key, value);
310 }
ac531cb6 311 g_assert(qdict_size(qdict) == lines);
7b8c51ad
LC
312
313 // Check if everything is really in there
314 reset_file(test_file);
315 for (;;) {
316 const char *str1, *str2;
317
318 value = read_line(test_file, key);
319 if (!value)
320 break;
321
322 str1 = qstring_get_str(value);
323
324 str2 = qdict_get_str(qdict, key);
ac531cb6 325 g_assert(str2 != NULL);
7b8c51ad 326
ac531cb6 327 g_assert(strcmp(str1, str2) == 0);
7b8c51ad 328
cb3e7f08 329 qobject_unref(value);
7b8c51ad
LC
330 }
331
332 // Delete everything
333 reset_file(test_file);
334 for (;;) {
335 value = read_line(test_file, key);
336 if (!value)
337 break;
338
339 qdict_del(qdict, key);
cb3e7f08 340 qobject_unref(value);
7b8c51ad 341
ac531cb6 342 g_assert(qdict_haskey(qdict, key) == 0);
7b8c51ad
LC
343 }
344 fclose(test_file);
345
ac531cb6 346 g_assert(qdict_size(qdict) == 0);
cb3e7f08 347 qobject_unref(qdict);
7b8c51ad 348}
7b8c51ad 349
ac531cb6 350int main(int argc, char **argv)
7b8c51ad 351{
ac531cb6 352 g_test_init(&argc, &argv, NULL);
7b8c51ad 353
ac531cb6
AL
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);
7b8c51ad
LC
357
358 /* Continue, but now with fixtures */
ac531cb6
AL
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);
369
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);
7b8c51ad
LC
372
373 /* The Big one */
ac531cb6
AL
374 if (g_test_slow()) {
375 g_test_add_func("/stress/test", qdict_stress_test);
376 }
7b8c51ad 377
ac531cb6 378 return g_test_run();
7b8c51ad 379}