4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
14 #include "qemu-common.h"
17 * Public Interface test-cases
19 * (with some violations to access 'private' data)
22 START_TEST(qdict_new_test
)
27 fail_unless(qdict
!= NULL
);
28 fail_unless(qdict_size(qdict
) == 0);
29 fail_unless(qdict
->base
.refcnt
== 1);
30 fail_unless(qobject_type(QOBJECT(qdict
)) == QTYPE_QDICT
);
32 // destroy doesn't exit yet
37 START_TEST(qdict_put_obj_test
)
46 // key "" will have tdb hash 12345
47 qdict_put_obj(qdict
, "", QOBJECT(qint_from_int(num
)));
49 fail_unless(qdict_size(qdict
) == 1);
50 ent
= QLIST_FIRST(&qdict
->table
[12345 % QDICT_HASH_SIZE
]);
51 qi
= qobject_to_qint(ent
->value
);
52 fail_unless(qint_get_int(qi
) == num
);
54 // destroy doesn't exit yet
62 START_TEST(qdict_destroy_simple_test
)
67 qdict_put_obj(qdict
, "num", QOBJECT(qint_from_int(0)));
68 qdict_put_obj(qdict
, "str", QOBJECT(qstring_from_str("foo")));
74 static QDict
*tests_dict
= NULL
;
76 static void qdict_setup(void)
78 tests_dict
= qdict_new();
79 fail_unless(tests_dict
!= NULL
);
82 static void qdict_teardown(void)
88 START_TEST(qdict_get_test
)
92 const int value
= -42;
93 const char *key
= "test";
95 qdict_put(tests_dict
, key
, qint_from_int(value
));
97 obj
= qdict_get(tests_dict
, key
);
98 fail_unless(obj
!= NULL
);
100 qi
= qobject_to_qint(obj
);
101 fail_unless(qint_get_int(qi
) == value
);
105 START_TEST(qdict_get_int_test
)
108 const int value
= 100;
109 const char *key
= "int";
111 qdict_put(tests_dict
, key
, qint_from_int(value
));
113 ret
= qdict_get_int(tests_dict
, key
);
114 fail_unless(ret
== value
);
118 START_TEST(qdict_get_try_int_test
)
121 const int value
= 100;
122 const char *key
= "int";
124 qdict_put(tests_dict
, key
, qint_from_int(value
));
126 ret
= qdict_get_try_int(tests_dict
, key
, 0);
127 fail_unless(ret
== value
);
131 START_TEST(qdict_get_str_test
)
134 const char *key
= "key";
135 const char *str
= "string";
137 qdict_put(tests_dict
, key
, qstring_from_str(str
));
139 p
= qdict_get_str(tests_dict
, key
);
140 fail_unless(p
!= NULL
);
141 fail_unless(strcmp(p
, str
) == 0);
145 START_TEST(qdict_get_try_str_test
)
148 const char *key
= "key";
149 const char *str
= "string";
151 qdict_put(tests_dict
, key
, qstring_from_str(str
));
153 p
= qdict_get_try_str(tests_dict
, key
);
154 fail_unless(p
!= NULL
);
155 fail_unless(strcmp(p
, str
) == 0);
159 START_TEST(qdict_haskey_not_test
)
161 fail_unless(qdict_haskey(tests_dict
, "test") == 0);
165 START_TEST(qdict_haskey_test
)
167 const char *key
= "test";
169 qdict_put(tests_dict
, key
, qint_from_int(0));
170 fail_unless(qdict_haskey(tests_dict
, key
) == 1);
174 START_TEST(qdict_del_test
)
176 const char *key
= "key test";
178 qdict_put(tests_dict
, key
, qstring_from_str("foo"));
179 fail_unless(qdict_size(tests_dict
) == 1);
181 qdict_del(tests_dict
, key
);
183 fail_unless(qdict_size(tests_dict
) == 0);
184 fail_unless(qdict_haskey(tests_dict
, key
) == 0);
188 START_TEST(qobject_to_qdict_test
)
190 fail_unless(qobject_to_qdict(QOBJECT(tests_dict
)) == tests_dict
);
198 START_TEST(qdict_put_exists_test
)
201 const char *key
= "exists";
203 qdict_put(tests_dict
, key
, qint_from_int(1));
204 qdict_put(tests_dict
, key
, qint_from_int(2));
206 value
= qdict_get_int(tests_dict
, key
);
207 fail_unless(value
== 2);
211 START_TEST(qdict_get_not_exists_test
)
213 fail_unless(qdict_get(tests_dict
, "foo") == NULL
);
220 * This is a lot big for a unit-test, but there is no other place
224 static void remove_dots(char *string
)
226 char *p
= strchr(string
, ':');
231 static QString
*read_line(FILE *file
, char *key
)
235 if (fscanf(file
, "%s%s", key
, value
) == EOF
)
238 return qstring_from_str(value
);
241 #define reset_file(file) fseek(file, 0L, SEEK_SET)
243 START_TEST(qdict_stress_test
)
250 const char *test_file_path
= "qdict-test-data.txt";
252 test_file
= fopen(test_file_path
, "r");
253 fail_unless(test_file
!= NULL
);
257 fail_unless(qdict
!= NULL
);
259 // Add everything from the test file
260 for (lines
= 0;; lines
++) {
261 value
= read_line(test_file
, key
);
265 qdict_put(qdict
, key
, value
);
267 fail_unless(qdict_size(qdict
) == lines
);
269 // Check if everything is really in there
270 reset_file(test_file
);
272 const char *str1
, *str2
;
274 value
= read_line(test_file
, key
);
278 str1
= qstring_get_str(value
);
280 str2
= qdict_get_str(qdict
, key
);
281 fail_unless(str2
!= NULL
);
283 fail_unless(strcmp(str1
, str2
) == 0);
289 reset_file(test_file
);
291 value
= read_line(test_file
, key
);
295 qdict_del(qdict
, key
);
298 fail_unless(qdict_haskey(qdict
, key
) == 0);
302 fail_unless(qdict_size(qdict
) == 0);
307 static Suite
*qdict_suite(void)
310 TCase
*qdict_public_tcase
;
311 TCase
*qdict_public2_tcase
;
312 TCase
*qdict_stress_tcase
;
313 TCase
*qdict_errors_tcase
;
315 s
= suite_create("QDict test-suite");
317 qdict_public_tcase
= tcase_create("Public Interface");
318 suite_add_tcase(s
, qdict_public_tcase
);
319 tcase_add_test(qdict_public_tcase
, qdict_new_test
);
320 tcase_add_test(qdict_public_tcase
, qdict_put_obj_test
);
321 tcase_add_test(qdict_public_tcase
, qdict_destroy_simple_test
);
323 /* Continue, but now with fixtures */
324 qdict_public2_tcase
= tcase_create("Public Interface (2)");
325 suite_add_tcase(s
, qdict_public2_tcase
);
326 tcase_add_checked_fixture(qdict_public2_tcase
, qdict_setup
, qdict_teardown
);
327 tcase_add_test(qdict_public2_tcase
, qdict_get_test
);
328 tcase_add_test(qdict_public2_tcase
, qdict_get_int_test
);
329 tcase_add_test(qdict_public2_tcase
, qdict_get_try_int_test
);
330 tcase_add_test(qdict_public2_tcase
, qdict_get_str_test
);
331 tcase_add_test(qdict_public2_tcase
, qdict_get_try_str_test
);
332 tcase_add_test(qdict_public2_tcase
, qdict_haskey_not_test
);
333 tcase_add_test(qdict_public2_tcase
, qdict_haskey_test
);
334 tcase_add_test(qdict_public2_tcase
, qdict_del_test
);
335 tcase_add_test(qdict_public2_tcase
, qobject_to_qdict_test
);
337 qdict_errors_tcase
= tcase_create("Errors");
338 suite_add_tcase(s
, qdict_errors_tcase
);
339 tcase_add_checked_fixture(qdict_errors_tcase
, qdict_setup
, qdict_teardown
);
340 tcase_add_test(qdict_errors_tcase
, qdict_put_exists_test
);
341 tcase_add_test(qdict_errors_tcase
, qdict_get_not_exists_test
);
344 qdict_stress_tcase
= tcase_create("Stress Test");
345 suite_add_tcase(s
, qdict_stress_tcase
);
346 tcase_add_test(qdict_stress_tcase
, qdict_stress_test
);
358 sr
= srunner_create(s
);
360 srunner_run_all(sr
, CK_NORMAL
);
361 nf
= srunner_ntests_failed(sr
);
364 return (nf
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;