]> Git Repo - qemu.git/blob - tests/check-qdict.c
86e9fe7dc488ade189a4131ccd1e484ae25bcafc
[qemu.git] / tests / check-qdict.c
1 /*
2  * QDict unit-tests.
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <[email protected]>
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.
11  */
12
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qdict.h"
15
16 /*
17  * Public Interface test-cases
18  *
19  * (with some violations to access 'private' data)
20  */
21
22 static void qdict_new_test(void)
23 {
24     QDict *qdict;
25
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);
31
32     qobject_unref(qdict);
33 }
34
35 static void qdict_put_obj_test(void)
36 {
37     QNum *qn;
38     QDict *qdict;
39     QDictEntry *ent;
40     const int num = 42;
41
42     qdict = qdict_new();
43
44     // key "" will have tdb hash 12345
45     qdict_put_int(qdict, "", num);
46
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);
51
52     qobject_unref(qdict);
53 }
54
55 static void qdict_destroy_simple_test(void)
56 {
57     QDict *qdict;
58
59     qdict = qdict_new();
60     qdict_put_int(qdict, "num", 0);
61     qdict_put_str(qdict, "str", "foo");
62
63     qobject_unref(qdict);
64 }
65
66 static void qdict_get_test(void)
67 {
68     QNum *qn;
69     QObject *obj;
70     const int value = -42;
71     const char *key = "test";
72     QDict *tests_dict = qdict_new();
73
74     qdict_put_int(tests_dict, key, value);
75
76     obj = qdict_get(tests_dict, key);
77     g_assert(obj != NULL);
78
79     qn = qobject_to(QNum, obj);
80     g_assert_cmpint(qnum_get_int(qn), ==, value);
81
82     qobject_unref(tests_dict);
83 }
84
85 static void qdict_get_int_test(void)
86 {
87     int ret;
88     const int value = 100;
89     const char *key = "int";
90     QDict *tests_dict = qdict_new();
91
92     qdict_put_int(tests_dict, key, value);
93
94     ret = qdict_get_int(tests_dict, key);
95     g_assert(ret == value);
96
97     qobject_unref(tests_dict);
98 }
99
100 static void qdict_get_try_int_test(void)
101 {
102     int ret;
103     const int value = 100;
104     const char *key = "int";
105     QDict *tests_dict = qdict_new();
106
107     qdict_put_int(tests_dict, key, value);
108     qdict_put_str(tests_dict, "string", "test");
109
110     ret = qdict_get_try_int(tests_dict, key, 0);
111     g_assert(ret == value);
112
113     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
119     qobject_unref(tests_dict);
120 }
121
122 static void qdict_get_str_test(void)
123 {
124     const char *p;
125     const char *key = "key";
126     const char *str = "string";
127     QDict *tests_dict = qdict_new();
128
129     qdict_put_str(tests_dict, key, str);
130
131     p = qdict_get_str(tests_dict, key);
132     g_assert(p != NULL);
133     g_assert(strcmp(p, str) == 0);
134
135     qobject_unref(tests_dict);
136 }
137
138 static void qdict_get_try_str_test(void)
139 {
140     const char *p;
141     const char *key = "key";
142     const char *str = "string";
143     QDict *tests_dict = qdict_new();
144
145     qdict_put_str(tests_dict, key, str);
146
147     p = qdict_get_try_str(tests_dict, key);
148     g_assert(p != NULL);
149     g_assert(strcmp(p, str) == 0);
150
151     qobject_unref(tests_dict);
152 }
153
154 static void qdict_haskey_not_test(void)
155 {
156     QDict *tests_dict = qdict_new();
157     g_assert(qdict_haskey(tests_dict, "test") == 0);
158
159     qobject_unref(tests_dict);
160 }
161
162 static void qdict_haskey_test(void)
163 {
164     const char *key = "test";
165     QDict *tests_dict = qdict_new();
166
167     qdict_put_int(tests_dict, key, 0);
168     g_assert(qdict_haskey(tests_dict, key) == 1);
169
170     qobject_unref(tests_dict);
171 }
172
173 static void qdict_del_test(void)
174 {
175     const char *key = "key test";
176     QDict *tests_dict = qdict_new();
177
178     qdict_put_str(tests_dict, key, "foo");
179     g_assert(qdict_size(tests_dict) == 1);
180
181     qdict_del(tests_dict, key);
182
183     g_assert(qdict_size(tests_dict) == 0);
184     g_assert(qdict_haskey(tests_dict, key) == 0);
185
186     qobject_unref(tests_dict);
187 }
188
189 static void qobject_to_qdict_test(void)
190 {
191     QDict *tests_dict = qdict_new();
192     g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict);
193
194     qobject_unref(tests_dict);
195 }
196
197 static void qdict_iterapi_test(void)
198 {
199     int count;
200     const QDictEntry *ent;
201     QDict *tests_dict = qdict_new();
202
203     g_assert(qdict_first(tests_dict) == NULL);
204
205     qdict_put_int(tests_dict, "key1", 1);
206     qdict_put_int(tests_dict, "key2", 2);
207     qdict_put_int(tests_dict, "key3", 3);
208
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++;
213     }
214
215     g_assert(count == qdict_size(tests_dict));
216
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++;
222     }
223
224     g_assert(count == qdict_size(tests_dict));
225
226     qobject_unref(tests_dict);
227 }
228
229 /*
230  * Errors test-cases
231  */
232
233 static void qdict_put_exists_test(void)
234 {
235     int value;
236     const char *key = "exists";
237     QDict *tests_dict = qdict_new();
238
239     qdict_put_int(tests_dict, key, 1);
240     qdict_put_int(tests_dict, key, 2);
241
242     value = qdict_get_int(tests_dict, key);
243     g_assert(value == 2);
244
245     g_assert(qdict_size(tests_dict) == 1);
246
247     qobject_unref(tests_dict);
248 }
249
250 static void qdict_get_not_exists_test(void)
251 {
252     QDict *tests_dict = qdict_new();
253     g_assert(qdict_get(tests_dict, "foo") == NULL);
254
255     qobject_unref(tests_dict);
256 }
257
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
265 static void remove_dots(char *string)
266 {
267     char *p = strchr(string, ':');
268     if (p)
269         *p = '\0';
270 }
271
272 static QString *read_line(FILE *file, char *key)
273 {
274     char value[128];
275
276     if (fscanf(file, "%127s%127s", key, value) == EOF) {
277         return NULL;
278     }
279     remove_dots(key);
280     return qstring_from_str(value);
281 }
282
283 #define reset_file(file)    fseek(file, 0L, SEEK_SET)
284
285 static void qdict_stress_test(void)
286 {
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");
295     g_assert(test_file != NULL);
296
297     // Create the dict
298     qdict = qdict_new();
299     g_assert(qdict != NULL);
300
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     }
309     g_assert(qdict_size(qdict) == lines);
310
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);
323         g_assert(str2 != NULL);
324
325         g_assert(strcmp(str1, str2) == 0);
326
327         qobject_unref(value);
328     }
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);
338         qobject_unref(value);
339
340         g_assert(qdict_haskey(qdict, key) == 0);
341     }
342     fclose(test_file);
343
344     g_assert(qdict_size(qdict) == 0);
345     qobject_unref(qdict);
346 }
347
348 int main(int argc, char **argv)
349 {
350     g_test_init(&argc, &argv, NULL);
351
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);
355
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);
367
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);
370
371     /* The Big one */
372     if (g_test_slow()) {
373         g_test_add_func("/stress/test", qdict_stress_test);
374     }
375
376     return g_test_run();
377 }
This page took 0.039289 seconds and 2 git commands to generate.