2 * Copyright IBM, Corp. 2009
5 * Anthony Liguori <aliguori@us.ibm.com>
7 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
8 * See the COPYING.LIB file in the top-level directory.
22 #include "qemu-common.h"
24 START_TEST(escaped_string
)
40 { "\"hello world \\\"embedded string\\\"\"",
41 "hello world \"embedded string\"" },
42 { "\"hello world\\nwith new line\"", "hello world\nwith new line" },
43 { "\"single byte utf-8 \\u0020\"", "single byte utf-8 ", .skip
= 1 },
44 { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
45 { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
49 for (i
= 0; test_cases
[i
].encoded
; i
++) {
53 obj
= qobject_from_json(test_cases
[i
].encoded
);
55 fail_unless(obj
!= NULL
);
56 fail_unless(qobject_type(obj
) == QTYPE_QSTRING
);
58 str
= qobject_to_qstring(obj
);
59 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].decoded
) == 0,
60 "%s != %s\n", qstring_get_str(str
), test_cases
[i
].decoded
);
62 if (test_cases
[i
].skip
== 0) {
63 str
= qobject_to_json(obj
);
64 fail_unless(strcmp(qstring_get_str(str
),test_cases
[i
].encoded
) == 0,
65 "%s != %s\n", qstring_get_str(str
),
66 test_cases
[i
].encoded
);
76 START_TEST(simple_string
)
83 { "\"hello world\"", "hello world" },
84 { "\"the quick brown fox jumped over the fence\"",
85 "the quick brown fox jumped over the fence" },
89 for (i
= 0; test_cases
[i
].encoded
; i
++) {
93 obj
= qobject_from_json(test_cases
[i
].encoded
);
95 fail_unless(obj
!= NULL
);
96 fail_unless(qobject_type(obj
) == QTYPE_QSTRING
);
98 str
= qobject_to_qstring(obj
);
99 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].decoded
) == 0);
101 str
= qobject_to_json(obj
);
102 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].encoded
) == 0);
111 START_TEST(single_quote_string
)
118 { "'hello world'", "hello world" },
119 { "'the quick brown fox \\' jumped over the fence'",
120 "the quick brown fox ' jumped over the fence" },
124 for (i
= 0; test_cases
[i
].encoded
; i
++) {
128 obj
= qobject_from_json(test_cases
[i
].encoded
);
130 fail_unless(obj
!= NULL
);
131 fail_unless(qobject_type(obj
) == QTYPE_QSTRING
);
133 str
= qobject_to_qstring(obj
);
134 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].decoded
) == 0);
141 START_TEST(vararg_string
)
148 { "the quick brown fox jumped over the fence" },
152 for (i
= 0; test_cases
[i
].decoded
; i
++) {
156 obj
= qobject_from_jsonf("%s", test_cases
[i
].decoded
);
158 fail_unless(obj
!= NULL
);
159 fail_unless(qobject_type(obj
) == QTYPE_QSTRING
);
161 str
= qobject_to_qstring(obj
);
162 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].decoded
) == 0);
169 START_TEST(simple_number
)
181 { "-0", 0, .skip
= 1 },
185 for (i
= 0; test_cases
[i
].encoded
; i
++) {
189 obj
= qobject_from_json(test_cases
[i
].encoded
);
190 fail_unless(obj
!= NULL
);
191 fail_unless(qobject_type(obj
) == QTYPE_QINT
);
193 qint
= qobject_to_qint(obj
);
194 fail_unless(qint_get_int(qint
) == test_cases
[i
].decoded
);
195 if (test_cases
[i
].skip
== 0) {
198 str
= qobject_to_json(obj
);
199 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].encoded
) == 0);
208 START_TEST(float_number
)
218 { "-32.12313", -32.12313 },
219 { "-32.20e-10", -32.20e-10, .skip
= 1 },
223 for (i
= 0; test_cases
[i
].encoded
; i
++) {
227 obj
= qobject_from_json(test_cases
[i
].encoded
);
228 fail_unless(obj
!= NULL
);
229 fail_unless(qobject_type(obj
) == QTYPE_QFLOAT
);
231 qfloat
= qobject_to_qfloat(obj
);
232 fail_unless(qfloat_get_double(qfloat
) == test_cases
[i
].decoded
);
234 if (test_cases
[i
].skip
== 0) {
237 str
= qobject_to_json(obj
);
238 fail_unless(strcmp(qstring_get_str(str
), test_cases
[i
].encoded
) == 0);
247 START_TEST(vararg_number
)
253 int64_t value64
= 0x2342342343LL
;
254 double valuef
= 2.323423423;
256 obj
= qobject_from_jsonf("%d", value
);
257 fail_unless(obj
!= NULL
);
258 fail_unless(qobject_type(obj
) == QTYPE_QINT
);
260 qint
= qobject_to_qint(obj
);
261 fail_unless(qint_get_int(qint
) == value
);
265 obj
= qobject_from_jsonf("%" PRId64
, value64
);
266 fail_unless(obj
!= NULL
);
267 fail_unless(qobject_type(obj
) == QTYPE_QINT
);
269 qint
= qobject_to_qint(obj
);
270 fail_unless(qint_get_int(qint
) == value64
);
274 obj
= qobject_from_jsonf("%f", valuef
);
275 fail_unless(obj
!= NULL
);
276 fail_unless(qobject_type(obj
) == QTYPE_QFLOAT
);
278 qfloat
= qobject_to_qfloat(obj
);
279 fail_unless(qfloat_get_double(qfloat
) == valuef
);
285 START_TEST(keyword_literal
)
291 obj
= qobject_from_json("true");
292 fail_unless(obj
!= NULL
);
293 fail_unless(qobject_type(obj
) == QTYPE_QBOOL
);
295 qbool
= qobject_to_qbool(obj
);
296 fail_unless(qbool_get_int(qbool
) != 0);
298 str
= qobject_to_json(obj
);
299 fail_unless(strcmp(qstring_get_str(str
), "true") == 0);
304 obj
= qobject_from_json("false");
305 fail_unless(obj
!= NULL
);
306 fail_unless(qobject_type(obj
) == QTYPE_QBOOL
);
308 qbool
= qobject_to_qbool(obj
);
309 fail_unless(qbool_get_int(qbool
) == 0);
311 str
= qobject_to_json(obj
);
312 fail_unless(strcmp(qstring_get_str(str
), "false") == 0);
317 obj
= qobject_from_jsonf("%i", false);
318 fail_unless(obj
!= NULL
);
319 fail_unless(qobject_type(obj
) == QTYPE_QBOOL
);
321 qbool
= qobject_to_qbool(obj
);
322 fail_unless(qbool_get_int(qbool
) == 0);
326 obj
= qobject_from_jsonf("%i", true);
327 fail_unless(obj
!= NULL
);
328 fail_unless(qobject_type(obj
) == QTYPE_QBOOL
);
330 qbool
= qobject_to_qbool(obj
);
331 fail_unless(qbool_get_int(qbool
) != 0);
337 typedef struct LiteralQDictEntry LiteralQDictEntry
;
338 typedef struct LiteralQObject LiteralQObject
;
340 struct LiteralQObject
346 LiteralQDictEntry
*qdict
;
347 LiteralQObject
*qlist
;
351 struct LiteralQDictEntry
354 LiteralQObject value
;
357 #define QLIT_QINT(val) (LiteralQObject){.type = QTYPE_QINT, .value.qint = (val)}
358 #define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
359 #define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
360 #define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
362 typedef struct QListCompareHelper
365 LiteralQObject
*objs
;
367 } QListCompareHelper
;
369 static int compare_litqobj_to_qobj(LiteralQObject
*lhs
, QObject
*rhs
);
371 static void compare_helper(QObject
*obj
, void *opaque
)
373 QListCompareHelper
*helper
= opaque
;
375 if (helper
->result
== 0) {
379 if (helper
->objs
[helper
->index
].type
== QTYPE_NONE
) {
384 helper
->result
= compare_litqobj_to_qobj(&helper
->objs
[helper
->index
++], obj
);
387 static int compare_litqobj_to_qobj(LiteralQObject
*lhs
, QObject
*rhs
)
389 if (lhs
->type
!= qobject_type(rhs
)) {
395 return lhs
->value
.qint
== qint_get_int(qobject_to_qint(rhs
));
397 return (strcmp(lhs
->value
.qstr
, qstring_get_str(qobject_to_qstring(rhs
))) == 0);
401 for (i
= 0; lhs
->value
.qdict
[i
].key
; i
++) {
402 QObject
*obj
= qdict_get(qobject_to_qdict(rhs
), lhs
->value
.qdict
[i
].key
);
404 if (!compare_litqobj_to_qobj(&lhs
->value
.qdict
[i
].value
, obj
)) {
412 QListCompareHelper helper
;
415 helper
.objs
= lhs
->value
.qlist
;
418 qlist_iter(qobject_to_qlist(rhs
), compare_helper
, &helper
);
420 return helper
.result
;
429 START_TEST(simple_dict
)
434 LiteralQObject decoded
;
437 .encoded
= "{\"foo\": 42, \"bar\": \"hello world\"}",
438 .decoded
= QLIT_QDICT(((LiteralQDictEntry
[]){
439 { "foo", QLIT_QINT(42) },
440 { "bar", QLIT_QSTR("hello world") },
445 .decoded
= QLIT_QDICT(((LiteralQDictEntry
[]){
449 .encoded
= "{\"foo\": 43}",
450 .decoded
= QLIT_QDICT(((LiteralQDictEntry
[]){
451 { "foo", QLIT_QINT(43) },
458 for (i
= 0; test_cases
[i
].encoded
; i
++) {
462 obj
= qobject_from_json(test_cases
[i
].encoded
);
463 fail_unless(obj
!= NULL
);
464 fail_unless(qobject_type(obj
) == QTYPE_QDICT
);
466 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
468 str
= qobject_to_json(obj
);
471 obj
= qobject_from_json(qstring_get_str(str
));
472 fail_unless(obj
!= NULL
);
473 fail_unless(qobject_type(obj
) == QTYPE_QDICT
);
475 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
482 START_TEST(simple_list
)
487 LiteralQObject decoded
;
490 .encoded
= "[43,42]",
491 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
499 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
506 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
512 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
513 QLIT_QDICT(((LiteralQDictEntry
[]){
522 for (i
= 0; test_cases
[i
].encoded
; i
++) {
526 obj
= qobject_from_json(test_cases
[i
].encoded
);
527 fail_unless(obj
!= NULL
);
528 fail_unless(qobject_type(obj
) == QTYPE_QLIST
);
530 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
532 str
= qobject_to_json(obj
);
535 obj
= qobject_from_json(qstring_get_str(str
));
536 fail_unless(obj
!= NULL
);
537 fail_unless(qobject_type(obj
) == QTYPE_QLIST
);
539 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
546 START_TEST(simple_whitespace
)
551 LiteralQObject decoded
;
554 .encoded
= " [ 43 , 42 ]",
555 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
562 .encoded
= " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
563 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
565 QLIT_QDICT(((LiteralQDictEntry
[]){
566 { "h", QLIT_QSTR("b") },
568 QLIT_QLIST(((LiteralQObject
[]){
575 .encoded
= " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
576 .decoded
= QLIT_QLIST(((LiteralQObject
[]){
578 QLIT_QDICT(((LiteralQDictEntry
[]){
579 { "h", QLIT_QSTR("b") },
580 { "a", QLIT_QINT(32) },
582 QLIT_QLIST(((LiteralQObject
[]){
591 for (i
= 0; test_cases
[i
].encoded
; i
++) {
595 obj
= qobject_from_json(test_cases
[i
].encoded
);
596 fail_unless(obj
!= NULL
);
597 fail_unless(qobject_type(obj
) == QTYPE_QLIST
);
599 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
601 str
= qobject_to_json(obj
);
604 obj
= qobject_from_json(qstring_get_str(str
));
605 fail_unless(obj
!= NULL
);
606 fail_unless(qobject_type(obj
) == QTYPE_QLIST
);
608 fail_unless(compare_litqobj_to_qobj(&test_cases
[i
].decoded
, obj
) == 1);
616 START_TEST(simple_varargs
)
618 QObject
*embedded_obj
;
620 LiteralQObject decoded
= QLIT_QLIST(((LiteralQObject
[]){
623 QLIT_QLIST(((LiteralQObject
[]){
629 embedded_obj
= qobject_from_json("[32, 42]");
630 fail_unless(embedded_obj
!= NULL
);
632 obj
= qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj
);
633 fail_unless(obj
!= NULL
);
635 fail_unless(compare_litqobj_to_qobj(&decoded
, obj
) == 1);
641 static Suite
*qjson_suite(void)
644 TCase
*string_literals
, *number_literals
, *keyword_literals
;
645 TCase
*dicts
, *lists
, *whitespace
, *varargs
;
647 string_literals
= tcase_create("String Literals");
648 tcase_add_test(string_literals
, simple_string
);
649 tcase_add_test(string_literals
, escaped_string
);
650 tcase_add_test(string_literals
, single_quote_string
);
651 tcase_add_test(string_literals
, vararg_string
);
653 number_literals
= tcase_create("Number Literals");
654 tcase_add_test(number_literals
, simple_number
);
655 tcase_add_test(number_literals
, float_number
);
656 tcase_add_test(number_literals
, vararg_number
);
658 keyword_literals
= tcase_create("Keywords");
659 tcase_add_test(keyword_literals
, keyword_literal
);
660 dicts
= tcase_create("Objects");
661 tcase_add_test(dicts
, simple_dict
);
662 lists
= tcase_create("Lists");
663 tcase_add_test(lists
, simple_list
);
665 whitespace
= tcase_create("Whitespace");
666 tcase_add_test(whitespace
, simple_whitespace
);
668 varargs
= tcase_create("Varargs");
669 tcase_add_test(varargs
, simple_varargs
);
671 suite
= suite_create("QJSON test-suite");
672 suite_add_tcase(suite
, string_literals
);
673 suite_add_tcase(suite
, number_literals
);
674 suite_add_tcase(suite
, keyword_literals
);
675 suite_add_tcase(suite
, dicts
);
676 suite_add_tcase(suite
, lists
);
677 suite_add_tcase(suite
, whitespace
);
678 suite_add_tcase(suite
, varargs
);
690 sr
= srunner_create(s
);
692 srunner_run_all(sr
, CK_NORMAL
);
693 nf
= srunner_ntests_failed(sr
);
696 return (nf
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;