From patchwork Mon Feb 29 23:19:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 8460241 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id DE4929F65E for ; Mon, 29 Feb 2016 23:22:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1C77520160 for ; Mon, 29 Feb 2016 23:22:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9FEE2201BB for ; Mon, 29 Feb 2016 23:22:12 +0000 (UTC) Received: from localhost ([::1]:39646 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaX8l-0005Hv-UO for patchwork-qemu-devel@patchwork.kernel.org; Mon, 29 Feb 2016 18:22:11 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaX6T-0001Wf-6n for qemu-devel@nongnu.org; Mon, 29 Feb 2016 18:19:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aaX6R-0006AZ-Nd for qemu-devel@nongnu.org; Mon, 29 Feb 2016 18:19:49 -0500 Received: from mx1.redhat.com ([209.132.183.28]:13951) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaX6O-00069V-Pe; Mon, 29 Feb 2016 18:19:44 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 7A6BA1E28; Mon, 29 Feb 2016 23:19:44 +0000 (UTC) Received: from localhost (ovpn-116-81.ams2.redhat.com [10.36.116.81]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1TNJgAI022634 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 29 Feb 2016 18:19:43 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 1 Mar 2016 00:19:20 +0100 Message-Id: <1456787973-19348-4-git-send-email-mreitz@redhat.com> In-Reply-To: <1456787973-19348-1-git-send-email-mreitz@redhat.com> References: <1456787973-19348-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Paolo Bonzini , Luiz Capitulino Subject: [Qemu-devel] [PATCH v2 03/16] check-qdict: Add a test for qdict_unflatten() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Max Reitz --- tests/check-qdict.c | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) diff --git a/tests/check-qdict.c b/tests/check-qdict.c index a43056c..f6a5cda 100644 --- a/tests/check-qdict.c +++ b/tests/check-qdict.c @@ -325,6 +325,272 @@ static void qdict_flatten_test(void) QDECREF(dict3); } +static void qdict_unflatten_test(void) +{ + QDict *dict; + QList *list_a, *list_o; + QDict *dict_a1, *dict_d, *dict_df, *dict_dh, *dict_i, *dict_l; + const QListEntry *le; + + /* + * Test the unflattening of + * + * { + * "a.0": 0, + * "a.1.b": 1, + * "a.1.c": 2, + * "a.2": 3, + * "d.e": 4, + * "d.f.g": 5, + * "d.h.0": 6, + * "d.h.2": 7, + * "i.0": 8, + * "i.j": 9, + * "k": 10, + * "l": { + * "m": 11 + * }, + * "l.n": 12, + * "o": [], + * "o.0": 13 + * } + * + * to + * + * { + * "a": [ + * 0, + * { + * "b": 1, + * "c": 2 + * }, + * 3 + * ], + * "d": { + * "e": 4, + * "f": { + * "g": 5 + * }, + * "h": { + * "0": 6, + * "2": 7 + * }, + * }, + * "i": { + * "0": 8, + * "j": 9 + * }, + * "k": 10, + * "l": { + * "m": 11, + * "n": 12 + * }, + * "o": [ + * 13 + * ] + * } + * + * This tests: + * - Unflattening in general + * - Conversion of "x.0", "x.2" into a dict instead of a list + * - Conversion of "x.0", "x.y" into a dict instead of a list + * - Merging of previously existing and new unflattened dicts + * ({ "x": { "y": 0 }, "x.z": 1 } => { "x": { "y": 0, "z": 1 } }) + * - Merging of previously existing and new unflattened lists; only works + * if the previous list was empty + * ({ "x": [], "x.0": 0 } => { "x": [ 0 ] }) + */ + + dict = qdict_new(); + + qdict_put(dict, "a.0", qint_from_int( 0)); + qdict_put(dict, "a.1.b", qint_from_int( 1)); + qdict_put(dict, "a.1.c", qint_from_int( 2)); + qdict_put(dict, "a.2", qint_from_int( 3)); + qdict_put(dict, "d.e", qint_from_int( 4)); + qdict_put(dict, "d.f.g", qint_from_int( 5)); + qdict_put(dict, "d.h.0", qint_from_int( 6)); + qdict_put(dict, "d.h.2", qint_from_int( 7)); + qdict_put(dict, "i.0", qint_from_int( 8)); + qdict_put(dict, "i.j", qint_from_int( 9)); + qdict_put(dict, "k", qint_from_int(10)); + qdict_put(dict, "l", qdict_new()); + qdict_put(qdict_get_qdict(dict, "l"), "m", qint_from_int(11)); + qdict_put(dict, "l.n", qint_from_int(12)); + qdict_put(dict, "o", qlist_new()); + qdict_put(dict, "o.0", qint_from_int(13)); + + qdict_unflatten(dict, &error_abort); + + list_a = qdict_get_qlist(dict, "a"); + g_assert(list_a); + + /* a.0 */ + le = qlist_first(list_a); + g_assert(le); + g_assert(qint_get_int(qobject_to_qint(le->value)) == 0); + /* a.1 */ + le = qlist_next(le); + g_assert(le); + dict_a1 = qobject_to_qdict(le->value); + g_assert(dict_a1); + g_assert(qdict_get_int(dict_a1, "b") == 1); + g_assert(qdict_get_int(dict_a1, "c") == 2); + g_assert(qdict_size(dict_a1) == 2); + /* a.2 */ + le = qlist_next(le); + g_assert(le); + g_assert(qint_get_int(qobject_to_qint(le->value)) == 3); + + g_assert(!qlist_next(le)); + + dict_d = qdict_get_qdict(dict, "d"); + g_assert(dict_d); + g_assert(qdict_get_int(dict_d, "e") == 4); + + dict_df = qdict_get_qdict(dict_d, "f"); + g_assert(dict_df); + g_assert(qdict_get_int(dict_df, "g") == 5); + g_assert(qdict_size(dict_df) == 1); + + dict_dh = qdict_get_qdict(dict_d, "h"); + g_assert(dict_dh); + g_assert(qdict_get_int(dict_dh, "0") == 6); + g_assert(qdict_get_int(dict_dh, "2") == 7); + g_assert(qdict_size(dict_dh) == 2); + + g_assert(qdict_size(dict_d) == 3); + + dict_i = qdict_get_qdict(dict, "i"); + g_assert(dict_i); + g_assert(qdict_get_int(dict_i, "0") == 8); + g_assert(qdict_get_int(dict_i, "j") == 9); + g_assert(qdict_size(dict_i) == 2); + + g_assert(qdict_get_int(dict, "k") == 10); + + dict_l = qdict_get_qdict(dict, "l"); + g_assert(dict_l); + g_assert(qdict_get_int(dict_l, "m") == 11); + g_assert(qdict_get_int(dict_l, "n") == 12); + g_assert(qdict_size(dict_l) == 2); + + list_o = qdict_get_qlist(dict, "o"); + g_assert(list_o); + + /* o.0 */ + le = qlist_first(list_o); + g_assert(le); + g_assert(qint_get_int(qobject_to_qint(le->value)) == 13); + + g_assert(!qlist_next(le)); + + g_assert(qdict_size(dict) == 6); + + QDECREF(dict); + + + /* + * Test that unflattening + * + * { + * "a": 0, + * "a.b": 1 + * } + * + * fails. + * + * (Cannot create new QDict "a" because "a" is already a QString) + */ + + dict = qdict_new(); + + qdict_put(dict, "a", qint_from_int(0)); + qdict_put(dict, "a.b", qint_from_int(1)); + + g_assert(!qdict_unflatten(dict, NULL)); + + QDECREF(dict); + + + /* + * Test that unflattening + * + * { + * "a": [ + * 0 + * ], + * "a.0": 1 + * } + * + * fails. + * + * (Cannot replace list member "a.0") + */ + + dict = qdict_new(); + + qdict_put(dict, "a", qlist_new()); + qlist_append(qdict_get_qlist(dict, "a"), qint_from_int(0)); + qdict_put(dict, "a.0", qint_from_int(1)); + + g_assert(!qdict_unflatten(dict, NULL)); + + QDECREF(dict); + + + /* + * Test that unflattening + * + * { + * "a": { + * "b": 0 + * }, + * "a.b": 1 + * } + * + * fails. + * + * (Cannot replace existing nested dict member "a.b") + */ + + dict = qdict_new(); + + qdict_put(dict, "a", qdict_new()); + qdict_put(qdict_get_qdict(dict, "a"), "b", qint_from_int(0)); + qdict_put(dict, "a.b", qint_from_int(1)); + + g_assert(!qdict_unflatten(dict, NULL)); + + QDECREF(dict); + + + /* + * Test that unflattening + * + * { + * "a": { + * "b": 0 + * }, + * "a.0": 1 + * } + * + * fails. + * + * (Cannot replace existing dict "a" by a list) + */ + + dict = qdict_new(); + + qdict_put(dict, "a", qdict_new()); + qdict_put(qdict_get_qdict(dict, "a"), "b", qint_from_int(0)); + qdict_put(dict, "a.0", qint_from_int(1)); + + g_assert(!qdict_unflatten(dict, NULL)); + + QDECREF(dict); +} + static void qdict_array_split_test(void) { QDict *test_dict = qdict_new(); @@ -736,6 +1002,7 @@ int main(int argc, char **argv) g_test_add_func("/public/to_qdict", qobject_to_qdict_test); g_test_add_func("/public/iterapi", qdict_iterapi_test); g_test_add_func("/public/flatten", qdict_flatten_test); + g_test_add_func("/public/unflatten", qdict_unflatten_test); g_test_add_func("/public/array_split", qdict_array_split_test); g_test_add_func("/public/array_entries", qdict_array_entries_test); g_test_add_func("/public/join", qdict_join_test);