From patchwork Wed Nov 11 19:24:38 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 38176 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 02472B70CF for ; Thu, 12 Nov 2009 06:29:12 +1100 (EST) Received: from localhost ([127.0.0.1]:45410 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8IsO-0008Ev-Sj for incoming@patchwork.ozlabs.org; Wed, 11 Nov 2009 14:29:08 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8IoH-0003LX-1x for qemu-devel@nongnu.org; Wed, 11 Nov 2009 14:24:53 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8IoB-0003Du-Az for qemu-devel@nongnu.org; Wed, 11 Nov 2009 14:24:51 -0500 Received: from [199.232.76.173] (port=52919 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8IoA-0003DP-B2 for qemu-devel@nongnu.org; Wed, 11 Nov 2009 14:24:46 -0500 Received: from e7.ny.us.ibm.com ([32.97.182.137]:39685) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1N8Io9-0004Oe-Sz for qemu-devel@nongnu.org; Wed, 11 Nov 2009 14:24:46 -0500 Received: from d01relay01.pok.ibm.com (d01relay01.pok.ibm.com [9.56.227.233]) by e7.ny.us.ibm.com (8.14.3/8.13.1) with ESMTP id nABJKoah001363 for ; Wed, 11 Nov 2009 14:20:50 -0500 Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d01relay01.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id nABJOhdx106746 for ; Wed, 11 Nov 2009 14:24:43 -0500 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id nABJQIqO026803 for ; Wed, 11 Nov 2009 12:26:18 -0700 Received: from localhost.localdomain (sig-9-65-32-87.mts.ibm.com [9.65.32.87]) by d03av06.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id nABJQG2V026731; Wed, 11 Nov 2009 12:26:17 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 11 Nov 2009 13:24:38 -0600 Message-Id: <1257967478-4847-3-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.2.5 In-Reply-To: <1257967478-4847-1-git-send-email-aliguori@us.ibm.com> References: <1257967478-4847-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Luiz Capitulino Subject: [Qemu-devel] [PATCH 3/3] Add test suite for json marshalling X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org By reusing the qjson test suite. After checking that we can demarshal, marshal again and compared to the expected decoded value. This doesn't work so well for floats because they cannot be accurately represented in decimal but we try our best. Signed-off-by: Anthony Liguori --- check-qjson.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 75 insertions(+), 5 deletions(-) diff --git a/check-qjson.c b/check-qjson.c index f763de6..4b591a5 100644 --- a/check-qjson.c +++ b/check-qjson.c @@ -27,12 +27,13 @@ START_TEST(escaped_string) struct { const char *encoded; const char *decoded; + int skip; } test_cases[] = { { "\"\\\"\"", "\"" }, { "\"hello world \\\"embedded string\\\"\"", "hello world \"embedded string\"" }, { "\"hello world\\nwith new line\"", "hello world\nwith new line" }, - { "\"single byte utf-8 \\u0020\"", "single byte utf-8 " }, + { "\"single byte utf-8 \\u0020\"", "single byte utf-8 ", .skip = 1 }, { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" }, { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" }, {} @@ -50,6 +51,13 @@ START_TEST(escaped_string) str = qobject_to_qstring(obj); fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0); + if (test_cases[i].skip == 0) { + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); + + qobject_decref(obj); + } + QDECREF(str); } } @@ -80,6 +88,11 @@ START_TEST(simple_string) str = qobject_to_qstring(obj); fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0); + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); + + qobject_decref(obj); + QDECREF(str); } } @@ -149,12 +162,13 @@ START_TEST(simple_number) struct { const char *encoded; int64_t decoded; + int skip; } test_cases[] = { { "0", 0 }, { "1234", 1234 }, { "1", 1 }, { "-32", -32 }, - { "-0", 0 }, + { "-0", 0, .skip = 1 }, { }, }; @@ -168,6 +182,13 @@ START_TEST(simple_number) qint = qobject_to_qint(obj); fail_unless(qint_get_int(qint) == test_cases[i].decoded); + if (test_cases[i].skip == 0) { + QString *str; + + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); + QDECREF(str); + } QDECREF(qint); } @@ -180,11 +201,12 @@ START_TEST(float_number) struct { const char *encoded; double decoded; + int skip; } test_cases[] = { { "32.43", 32.43 }, { "0.222", 0.222 }, { "-32.12313", -32.12313 }, - { "-32.20e-10", -32.20e-10 }, + { "-32.20e-10", -32.20e-10, .skip = 1 }, { }, }; @@ -199,6 +221,14 @@ START_TEST(float_number) qfloat = qobject_to_qfloat(obj); fail_unless(qfloat_get_double(qfloat) == test_cases[i].decoded); + if (test_cases[i].skip == 0) { + QString *str; + + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); + QDECREF(str); + } + QDECREF(qfloat); } } @@ -246,6 +276,7 @@ START_TEST(keyword_literal) { QObject *obj; QBool *qbool; + QString *str; obj = qobject_from_json("true"); fail_unless(obj != NULL); @@ -254,6 +285,10 @@ START_TEST(keyword_literal) qbool = qobject_to_qbool(obj); fail_unless(qbool_get_int(qbool) != 0); + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), "true") == 0); + QDECREF(str); + QDECREF(qbool); obj = qobject_from_json("false"); @@ -263,6 +298,10 @@ START_TEST(keyword_literal) qbool = qobject_to_qbool(obj); fail_unless(qbool_get_int(qbool) == 0); + str = qobject_to_json(obj); + fail_unless(strcmp(qstring_get_str(str), "false") == 0); + QDECREF(str); + QDECREF(qbool); obj = qobject_from_jsonf("%i", false); @@ -385,7 +424,7 @@ START_TEST(simple_dict) LiteralQObject decoded; } test_cases[] = { { - .encoded = "{\"foo\":42,\"bar\":\"hello world\"}", + .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}", .decoded = QLIT_QDICT(((LiteralQDictEntry[]){ { "foo", QLIT_QINT(42) }, { "bar", QLIT_QSTR("hello world") }, @@ -397,7 +436,7 @@ START_TEST(simple_dict) { } })), }, { - .encoded = "{\"foo\":43}", + .encoded = "{\"foo\": 43}", .decoded = QLIT_QDICT(((LiteralQDictEntry[]){ { "foo", QLIT_QINT(43) }, { } @@ -408,6 +447,7 @@ START_TEST(simple_dict) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; + QString *str; obj = qobject_from_json(test_cases[i].encoded); fail_unless(obj != NULL); @@ -415,7 +455,16 @@ START_TEST(simple_dict) fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); + str = qobject_to_json(obj); + qobject_decref(obj); + + obj = qobject_from_json(qstring_get_str(str)); + fail_unless(obj != NULL); + fail_unless(qobject_type(obj) == QTYPE_QDICT); + + fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); qobject_decref(obj); + QDECREF(str); } } END_TEST @@ -453,6 +502,7 @@ START_TEST(simple_list) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; + QString *str; obj = qobject_from_json(test_cases[i].encoded); fail_unless(obj != NULL); @@ -460,7 +510,16 @@ START_TEST(simple_list) fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); + str = qobject_to_json(obj); + qobject_decref(obj); + + obj = qobject_from_json(qstring_get_str(str)); + fail_unless(obj != NULL); + fail_unless(qobject_type(obj) == QTYPE_QLIST); + + fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); qobject_decref(obj); + QDECREF(str); } } END_TEST @@ -512,6 +571,7 @@ START_TEST(simple_whitespace) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; + QString *str; obj = qobject_from_json(test_cases[i].encoded); fail_unless(obj != NULL); @@ -519,7 +579,17 @@ START_TEST(simple_whitespace) fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); + str = qobject_to_json(obj); qobject_decref(obj); + + obj = qobject_from_json(qstring_get_str(str)); + fail_unless(obj != NULL); + fail_unless(qobject_type(obj) == QTYPE_QLIST); + + fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1); + + qobject_decref(obj); + QDECREF(str); } } END_TEST