All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Michael Roth <mdroth@linux.vnet.ibm.com>
Cc: aliguori@linux.vnet.ibm.com, lcapitulino@redhat.com,
	agl@linux.vnet.ibm.com, qemu-devel@nongnu.org,
	Jes.Sorensen@redhat.com
Subject: Re: [Qemu-devel] [PATCH v2][ 17/21] qapi: add test-visiter, tests for gen. visiter code
Date: Tue, 07 Jun 2011 14:08:59 -0500	[thread overview]
Message-ID: <4DEE774B.30308@codemonkey.ws> (raw)
In-Reply-To: <1307140399-9023-18-git-send-email-mdroth@linux.vnet.ibm.com>

On 06/03/2011 05:33 PM, Michael Roth wrote:
> Signed-off-by: Michael Roth<mdroth@linux.vnet.ibm.com>
> ---
>   test-visiter.c |  214 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 files changed, 214 insertions(+), 0 deletions(-)
>   create mode 100644 test-visiter.c
>
> diff --git a/test-visiter.c b/test-visiter.c
> new file mode 100644
> index 0000000..31596a0
> --- /dev/null
> +++ b/test-visiter.c
> @@ -0,0 +1,214 @@
> +#include<glib.h>
> +#include "qapi/qmp-output-visiter.h"
> +#include "qapi/qmp-input-visiter.h"
> +#include "test-qapi-types.h"
> +#include "test-qapi-visit.h"
> +#include "qemu-objects.h"
> +
> +typedef struct TestStruct
> +{
> +    int64_t x;
> +    int64_t y;
> +} TestStruct;
> +
> +typedef struct TestStructList
> +{
> +    TestStruct *value;
> +    struct TestStructList *next;
> +} TestStructList;
> +
> +static void visit_type_TestStruct(Visiter *v, TestStruct **obj, const char *name, Error **errp)
> +{
> +    visit_start_struct(v, (void **)obj, "TestStruct", name, errp);
> +    visit_type_int(v,&(*obj)->x, "x", errp);
> +    visit_type_int(v,&(*obj)->y, "y", errp);
> +    visit_end_struct(v, errp);
> +}
> +
> +static void visit_type_TestStructList(Visiter *m, TestStructList ** obj, const char *name, Error **errp)
> +{
> +    GenericList *i;
> +
> +    visit_start_list(m, name, errp);
> +
> +    for (i = visit_next_list(m, (GenericList **)obj, errp); i; i = visit_next_list(m,&i, errp)) {
> +        TestStructList *native_i = (TestStructList *)i;
> +        visit_type_TestStruct(m,&native_i->value, NULL, errp);
> +    }
> +
> +    visit_end_list(m, errp);
> +}
> +
> +/* test deep nesting with refs to other user-defined types */
> +static void test_nested_structs(void)
> +{
> +    QmpOutputVisiter *mo;
> +    QmpInputVisiter *mi;
> +    Visiter *v;
> +    UserDefOne ud1;
> +    UserDefOne *ud1_p =&ud1, *ud1c_p = NULL;
> +    UserDefTwo ud2;
> +    UserDefTwo *ud2_p =&ud2, *ud2c_p = NULL;
> +    Error *err = NULL;
> +    QObject *obj;
> +    QString *str;
> +
> +    ud1.integer = 42;
> +    ud1.string = strdup("fourty two");
> +
> +    /* sanity check */
> +    mo = qmp_output_visiter_new();
> +    v = qmp_output_get_visiter(mo);
> +    visit_type_UserDefOne(v,&ud1_p, "o_O",&err);
> +    if (err) {
> +        g_error("%s", error_get_pretty(err));
> +    }
> +    obj = qmp_output_get_qobject(mo);
> +    g_assert(obj);
> +    qobject_decref(obj);
> +
> +    ud2.string = strdup("fourty three");
> +    ud2.dict.string = strdup("fourty four");
> +    ud2.dict.dict.userdef = ud1_p;
> +    ud2.dict.dict.string = strdup("fourty five");
> +    ud2.dict.has_dict2 = true;
> +    ud2.dict.dict2.userdef = ud1_p;
> +    ud2.dict.dict2.string = strdup("fourty six");
> +
> +    /* c type ->  qobject */
> +    mo = qmp_output_visiter_new();
> +    v = qmp_output_get_visiter(mo);
> +    visit_type_UserDefTwo(v,&ud2_p, "unused",&err);
> +    if (err) {
> +        g_error("%s", error_get_pretty(err));
> +    }
> +    obj = qmp_output_get_qobject(mo);
> +    g_assert(obj);
> +    str = qobject_to_json_pretty(obj);
> +    g_print("%s\n", qstring_get_str(str));
> +    QDECREF(str);
> +
> +    /* qobject ->  c type, should match original struct */
> +    mi = qmp_input_visiter_new(obj);
> +    v = qmp_input_get_visiter(mi);
> +    visit_type_UserDefTwo(v,&ud2c_p, NULL,&err);
> +    if (err) {
> +        g_error("%s", error_get_pretty(err));
> +    }
> +
> +    g_assert(!g_strcmp0(ud2c_p->string, ud2.string));
> +    g_assert(!g_strcmp0(ud2c_p->dict.string, ud2.dict.string));
> +
> +    ud1c_p = ud2c_p->dict.dict.userdef;
> +    g_assert(ud1c_p->integer == ud1_p->integer);
> +    g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));
> +
> +    g_assert(!g_strcmp0(ud2c_p->dict.dict.string, ud2.dict.dict.string));
> +
> +    ud1c_p = ud2c_p->dict.dict2.userdef;
> +    g_assert(ud1c_p->integer == ud1_p->integer);
> +    g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));
> +
> +    g_assert(!g_strcmp0(ud2c_p->dict.dict2.string, ud2.dict.dict2.string));
> +    qemu_free(ud1.string);
> +    qemu_free(ud2.string);
> +    qemu_free(ud2.dict.string);
> +    qemu_free(ud2.dict.dict.string);
> +    qemu_free(ud2.dict.dict2.string);
> +
> +    qapi_free_UserDefTwo(ud2c_p);
> +
> +    qobject_decref(obj);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    QmpOutputVisiter *mo;
> +    QmpInputVisiter *mi;
> +    Visiter *v;
> +    TestStruct ts = { 42, 82 };
> +    TestStruct *pts =&ts;
> +    TestStructList *lts = NULL;
> +    Error *err = NULL;
> +    QObject *obj;
> +    QString *str;
> +
> +    g_test_init(&argc,&argv, NULL);
> +
> +    mo = qmp_output_visiter_new();
> +    v = qmp_output_get_visiter(mo);
> +
> +    visit_type_TestStruct(v,&pts, NULL,&err);
> +
> +    obj = qmp_output_get_qobject(mo);
> +
> +    str = qobject_to_json(obj);
> +
> +    printf("%s\n", qstring_get_str(str));
> +
> +    QDECREF(str);
> +
> +    obj = QOBJECT(qint_from_int(0x42));
> +
> +    mi = qmp_input_visiter_new(obj);
> +    v = qmp_input_get_visiter(mi);
> +
> +    int64_t value = 0;
> +
> +    visit_type_int(v,&value, NULL,&err);
> +    if (err) {
> +        printf("%s\n", error_get_pretty(err));
> +        return 1;
> +    }
> +
> +    g_assert(value == 0x42);
> +
> +    qobject_decref(obj);
> +
> +    obj = qobject_from_json("{'x': 42, 'y': 84}");
> +    mi = qmp_input_visiter_new(obj);
> +    v = qmp_input_get_visiter(mi);
> +
> +    pts = NULL;
> +
> +    visit_type_TestStruct(v,&pts, NULL,&err);
> +    if (err) {
> +        printf("%s\n", error_get_pretty(err));
> +        return 1;
> +    }
> +
> +    g_assert(pts != NULL);
> +    g_assert(pts->x == 42);
> +    g_assert(pts->y == 84);
> +
> +    qobject_decref(obj);
> +
> +    obj = qobject_from_json("[{'x': 42, 'y': 84}, {'x': 12, 'y': 24}]");
> +    mi = qmp_input_visiter_new(obj);
> +    v = qmp_input_get_visiter(mi);
> +
> +    visit_type_TestStructList(v,&lts, NULL,&err);
> +    if (err) {
> +        printf("%s\n", error_get_pretty(err));
> +        return 1;
> +    }
> +
> +    g_assert(lts != NULL);
> +    g_assert(lts->value->x == 42);
> +    g_assert(lts->value->y == 84);
> +
> +    lts = lts->next;
> +    g_assert(lts != NULL);
> +    g_assert(lts->value->x == 12);
> +    g_assert(lts->value->y == 24);
> +
> +    g_assert(lts->next == NULL);
> +
> +    qobject_decref(obj);

Please stick the above into a function so you can make it a named tested.

Regards,

Anthony Liguori

> +
> +    g_test_add_func("/0.15/nested_structs", test_nested_structs);
> +
> +    g_test_run();
> +
> +    return 0;
> +}

  reply	other threads:[~2011-06-07 19:09 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-03 22:32 [Qemu-devel] [QAPI+QGA 2/3] QAPI code generation infrastructure v2 Michael Roth
2011-06-03 22:32 ` [Qemu-devel] [PATCH v2][ 01/21] Add hard build dependency on glib Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 02/21] qlist: add qlist_first()/qlist_next() Michael Roth
2011-06-08 17:50   ` Luiz Capitulino
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 03/21] qapi: add module init types for qapi Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 04/21] qapi: add ordereddict/qapi.py helper libraries Michael Roth
2011-06-07 19:04   ` Anthony Liguori
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 05/21] qapi: add qapi-types.py code generator Michael Roth
2011-06-07 19:06   ` Anthony Liguori
2011-06-07 19:12     ` Luiz Capitulino
2011-06-07 19:54       ` Anthony Liguori
2011-06-09  7:00     ` Markus Armbruster
2011-06-09 15:05   ` Luiz Capitulino
2011-06-09 15:28     ` Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 06/21] qapi: add qapi-visit.py " Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 07/21] qapi: add qapi-commands.py " Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 08/21] qapi: add qapi-types-core.h Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 09/21] qapi: add qapi-visit-core.h Michael Roth
2011-06-09 15:14   ` Luiz Capitulino
2011-06-09 18:08     ` Anthony Liguori
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 10/21] qapi: add QMP input visiter Michael Roth
2011-06-09 15:30   ` Luiz Capitulino
2011-06-09 15:41     ` Michael Roth
2011-06-09 15:55       ` Luiz Capitulino
2011-06-09 16:26         ` Michael Roth
2011-06-09 16:26   ` Peter Maydell
2011-06-09 16:41     ` Michael Roth
2011-06-09 18:13       ` Anthony Liguori
2011-06-13 19:12   ` Luiz Capitulino
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 11/21] qapi: add QMP output visiter Michael Roth
2011-06-09 17:47   ` Luiz Capitulino
2011-06-09 19:42     ` Anthony Liguori
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 12/21] qapi: add QAPI dealloc visiter Michael Roth
2011-06-07 19:07   ` Anthony Liguori
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 13/21] qapi: add command registration/lookup functions Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 14/21] qapi: add QMP dispatch functions Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 15/21] qapi: add base declaration/types for QMP Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 16/21] qapi: test schema used for unit tests Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 17/21] qapi: add test-visiter, tests for gen. visiter code Michael Roth
2011-06-07 19:08   ` Anthony Liguori [this message]
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 18/21] qapi: Makefile changes to build test-visiter Michael Roth
2011-06-08 17:39   ` Luiz Capitulino
2011-06-08 17:55     ` Michael Roth
2011-06-08 18:00       ` Luiz Capitulino
2011-06-08 18:12     ` Anthony Liguori
2011-06-08 18:16       ` Luiz Capitulino
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 19/21] qapi: add test-qmp-commands, tests for gen. marshalling/dispatch code Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 20/21] qapi: Makefile changes to build test-qmp-commands Michael Roth
2011-06-03 22:33 ` [Qemu-devel] [PATCH v2][ 21/21] qapi: add QAPI code generation documentation Michael Roth
2011-06-08 16:43 ` [Qemu-devel] [QAPI+QGA 2/3] QAPI code generation infrastructure v2 Luiz Capitulino
2011-06-08 17:03   ` Michael Roth
2011-06-08 17:59     ` Luiz Capitulino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DEE774B.30308@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=Jes.Sorensen@redhat.com \
    --cc=agl@linux.vnet.ibm.com \
    --cc=aliguori@linux.vnet.ibm.com \
    --cc=lcapitulino@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.