From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:58782) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sd1Dp-0004sD-2x for qemu-devel@nongnu.org; Fri, 08 Jun 2012 11:35:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Sd1Di-0005Qy-GU for qemu-devel@nongnu.org; Fri, 08 Jun 2012 11:35:32 -0400 Received: from cantor2.suse.de ([195.135.220.15]:55337 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sd1Dh-0005QV-Sz for qemu-devel@nongnu.org; Fri, 08 Jun 2012 11:35:26 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Fri, 8 Jun 2012 17:35:07 +0200 Message-Id: <1339169713-31205-3-git-send-email-afaerber@suse.de> In-Reply-To: <1339169713-31205-1-git-send-email-afaerber@suse.de> References: <1339169713-31205-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 2/8] qapi: Unit tests for visitor-based serialization List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Michael Roth , Anthony Liguori , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Michael Roth Currently we test our visitors individually, and seperately for input vs. output. This is useful for validating internal representations against the native C types and vice-versa, and other visitor-specific testing, but it doesn't cover the potential use-case of using visitor pairs for serialization/deserialization very well, and makes it hard to easily extend the coverage for different C types / boundary conditions. To cover that we add a set of unit tests that takes a number of native C values, passes them into an output visitor, extracts the values with an input visitor, then compares the result to the original. Plugging in new visitors to the test harness only requires a user to implement the SerializeOps interface and add it to a list. Signed-off-by: Michael Roth Signed-off-by: Andreas F=C3=A4rber --- tests/Makefile | 4 +- tests/test-visitor-serialization.c | 744 ++++++++++++++++++++++++++++++= ++++++ 2 files changed, 747 insertions(+), 1 deletions(-) create mode 100644 tests/test-visitor-serialization.c diff --git a/tests/Makefile b/tests/Makefile index 2e754c3..d66ab19 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,6 +13,7 @@ check-unit-y +=3D tests/test-qmp-commands$(EXESUF) check-unit-y +=3D tests/test-string-input-visitor$(EXESUF) check-unit-y +=3D tests/test-string-output-visitor$(EXESUF) check-unit-y +=3D tests/test-coroutine$(EXESUF) +check-unit-y +=3D tests/test-visitor-serialization$(EXESUF) =20 check-block-$(CONFIG_POSIX) +=3D tests/qemu-iotests-quick.sh =20 @@ -31,7 +32,7 @@ test-obj-y =3D tests/check-qint.o tests/check-qstring.o= tests/check-qdict.o \ tests/test-coroutine.o tests/test-string-output-visitor.o \ tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \ - tests/test-qmp-commands.o + tests/test-qmp-commands.o tests/test-visitor-serialization.o =20 test-qapi-obj-y =3D $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) test-qapi-obj-y +=3D tests/test-qapi-visit.o tests/test-qapi-types.o @@ -64,6 +65,7 @@ tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-= output-visitor.o $(test-q tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(= test-qapi-obj-y) tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(te= st-qapi-obj-y) tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-q= mp-marshal.o $(test-qapi-obj-y) +tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serializat= ion.o $(test-qapi-obj-y) =20 tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y) tests/m48t59-test$(EXESUF): tests/m48t59-test.o $(trace-obj-y) diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-seri= alization.c new file mode 100644 index 0000000..6ef57d0 --- /dev/null +++ b/tests/test-visitor-serialization.c @@ -0,0 +1,744 @@ +/* + * Unit-tests for visitor-based serialization + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Michael Roth + * + * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. + * See the COPYING file in the top-level directory. + */ + +#include +#include +#include +#include +#include "test-qapi-types.h" +#include "test-qapi-visit.h" +#include "qemu-objects.h" +#include "qapi/qmp-input-visitor.h" +#include "qapi/qmp-output-visitor.h" + +typedef struct PrimitiveType { + union { + const char *string; + bool boolean; + double number; + int64_t integer; + uint8_t u8; + uint16_t u16; + uint32_t u32; + uint64_t u64; + int8_t s8; + int16_t s16; + int32_t s32; + int64_t s64; + intmax_t max; + } value; + enum { + PTYPE_STRING =3D 0, + PTYPE_BOOLEAN, + PTYPE_NUMBER, + PTYPE_INTEGER, + PTYPE_U8, + PTYPE_U16, + PTYPE_U32, + PTYPE_U64, + PTYPE_S8, + PTYPE_S16, + PTYPE_S32, + PTYPE_S64, + PTYPE_EOL, + } type; + const char *description; +} PrimitiveType; + +/* test helpers */ + +static void visit_primitive_type(Visitor *v, void **native, Error **errp= ) +{ + PrimitiveType *pt =3D *native; + switch(pt->type) { + case PTYPE_STRING: + visit_type_str(v, (char **)&pt->value.string, NULL, errp); + break; + case PTYPE_BOOLEAN: + visit_type_bool(v, &pt->value.boolean, NULL, errp); + break; + case PTYPE_NUMBER: + visit_type_number(v, &pt->value.number, NULL, errp); + break; + case PTYPE_INTEGER: + visit_type_int(v, &pt->value.integer, NULL, errp); + break; + case PTYPE_U8: + visit_type_uint8(v, &pt->value.u8, NULL, errp); + break; + case PTYPE_U16: + visit_type_uint16(v, &pt->value.u16, NULL, errp); + break; + case PTYPE_U32: + visit_type_uint32(v, &pt->value.u32, NULL, errp); + break; + case PTYPE_U64: + visit_type_uint64(v, &pt->value.u64, NULL, errp); + break; + case PTYPE_S8: + visit_type_int8(v, &pt->value.s8, NULL, errp); + break; + case PTYPE_S16: + visit_type_int16(v, &pt->value.s16, NULL, errp); + break; + case PTYPE_S32: + visit_type_int32(v, &pt->value.s32, NULL, errp); + break; + case PTYPE_S64: + visit_type_int64(v, &pt->value.s64, NULL, errp); + break; + case PTYPE_EOL: + g_assert(false); + } +} + +typedef struct TestStruct +{ + int64_t integer; + bool boolean; + char *string; +} TestStruct; + +static void visit_type_TestStruct(Visitor *v, TestStruct **obj, + const char *name, Error **errp) +{ + visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), = errp); + + visit_type_int(v, &(*obj)->integer, "integer", errp); + visit_type_bool(v, &(*obj)->boolean, "boolean", errp); + visit_type_str(v, &(*obj)->string, "string", errp); + + visit_end_struct(v, errp); +} + +static TestStruct *struct_create(void) +{ + TestStruct *ts =3D g_malloc0(sizeof(*ts)); + ts->integer =3D -42; + ts->boolean =3D true; + ts->string =3D strdup("test string"); + return ts; +} + +static void struct_compare(TestStruct *ts1, TestStruct *ts2) +{ + g_assert(ts1); + g_assert(ts2); + g_assert_cmpint(ts1->integer, =3D=3D, ts2->integer); + g_assert(ts1->boolean =3D=3D ts2->boolean); + g_assert_cmpstr(ts1->string, =3D=3D, ts2->string); +} + +static void struct_cleanup(TestStruct *ts) +{ + g_free(ts->string); + g_free(ts); +} + +static void visit_struct(Visitor *v, void **native, Error **errp) +{ + visit_type_TestStruct(v, (TestStruct **)native, NULL, errp); +} + +static UserDefNested *nested_struct_create(void) +{ + UserDefNested *udnp =3D g_malloc0(sizeof(*udnp)); + udnp->string0 =3D strdup("test_string0"); + udnp->dict1.string1 =3D strdup("test_string1"); + udnp->dict1.dict2.userdef1 =3D g_malloc0(sizeof(UserDefOne)); + udnp->dict1.dict2.userdef1->integer =3D 42; + udnp->dict1.dict2.userdef1->string =3D strdup("test_string"); + udnp->dict1.dict2.string2 =3D strdup("test_string2"); + udnp->dict1.has_dict3 =3D true; + udnp->dict1.dict3.userdef2 =3D g_malloc0(sizeof(UserDefOne)); + udnp->dict1.dict3.userdef2->integer =3D 43; + udnp->dict1.dict3.userdef2->string =3D strdup("test_string"); + udnp->dict1.dict3.string3 =3D strdup("test_string3"); + return udnp; +} + +static void nested_struct_compare(UserDefNested *udnp1, UserDefNested *u= dnp2) +{ + g_assert(udnp1); + g_assert(udnp2); + g_assert_cmpstr(udnp1->string0, =3D=3D, udnp2->string0); + g_assert_cmpstr(udnp1->dict1.string1, =3D=3D, udnp2->dict1.string1); + g_assert_cmpint(udnp1->dict1.dict2.userdef1->integer, =3D=3D, + udnp2->dict1.dict2.userdef1->integer); + g_assert_cmpstr(udnp1->dict1.dict2.userdef1->string, =3D=3D, + udnp2->dict1.dict2.userdef1->string); + g_assert_cmpstr(udnp1->dict1.dict2.string2, =3D=3D, udnp2->dict1.dic= t2.string2); + g_assert(udnp1->dict1.has_dict3 =3D=3D udnp2->dict1.has_dict3); + g_assert_cmpint(udnp1->dict1.dict3.userdef2->integer, =3D=3D, + udnp2->dict1.dict3.userdef2->integer); + g_assert_cmpstr(udnp1->dict1.dict3.userdef2->string, =3D=3D, + udnp2->dict1.dict3.userdef2->string); + g_assert_cmpstr(udnp1->dict1.dict3.string3, =3D=3D, udnp2->dict1.dic= t3.string3); +} + +static void nested_struct_cleanup(UserDefNested *udnp) +{ + qapi_free_UserDefNested(udnp); +} + +static void visit_nested_struct(Visitor *v, void **native, Error **errp) +{ + visit_type_UserDefNested(v, (UserDefNested **)native, NULL, errp); +} + +static void visit_nested_struct_list(Visitor *v, void **native, Error **= errp) +{ + visit_type_UserDefNestedList(v, (UserDefNestedList **)native, NULL, = errp); +} + +/* test cases */ + +typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp); + +typedef enum VisitorCapabilities { + VCAP_PRIMITIVES =3D 1, + VCAP_STRUCTURES =3D 2, + VCAP_LISTS =3D 4, +} VisitorCapabilities; + +typedef struct SerializeOps { + void (*serialize)(void *native_in, void **datap, + VisitorFunc visit, Error **errp); + void (*deserialize)(void **native_out, void *datap, + VisitorFunc visit, Error **errp); + void (*cleanup)(void *datap); + const char *type; + VisitorCapabilities caps; +} SerializeOps; + +typedef struct TestArgs { + const SerializeOps *ops; + void *test_data; +} TestArgs; + +#define FLOAT_STRING_PRECISION 6 /* corresponding to n in %.nf formattin= g */ +static gsize calc_float_string_storage(double value) +{ + int whole_value =3D value; + gsize i =3D 0; + do { + i++; + } while (whole_value /=3D 10); + return i + 2 + FLOAT_STRING_PRECISION; +} + +static void test_primitives(gconstpointer opaque) +{ + TestArgs *args =3D (TestArgs *) opaque; + const SerializeOps *ops =3D args->ops; + PrimitiveType *pt =3D args->test_data; + PrimitiveType *pt_copy =3D g_malloc0(sizeof(*pt_copy)); + Error *err =3D NULL; + void *serialize_data; + char *double1, *double2; + + pt_copy->type =3D pt->type; + ops->serialize(pt, &serialize_data, visit_primitive_type, &err); + ops->deserialize((void **)&pt_copy, serialize_data, visit_primitive_= type, &err); + + g_assert(err =3D=3D NULL); + g_assert(pt_copy !=3D NULL); + if (pt->type =3D=3D PTYPE_STRING) { + g_assert_cmpstr(pt->value.string, =3D=3D, pt_copy->value.string)= ; + } else if (pt->type =3D=3D PTYPE_NUMBER) { + /* we serialize with %f for our reference visitors, so rather th= an fuzzy + * floating math to test "equality", just compare the formatted = values + */ + double1 =3D g_malloc0(calc_float_string_storage(pt->value.number= )); + double2 =3D g_malloc0(calc_float_string_storage(pt_copy->value.n= umber)); + g_assert_cmpstr(double1, =3D=3D, double2); + g_free(double1); + g_free(double2); + } else if (pt->type =3D=3D PTYPE_BOOLEAN) { + g_assert_cmpint(!!pt->value.max, =3D=3D, !!pt->value.max); + } else { + g_assert_cmpint(pt->value.max, =3D=3D, pt_copy->value.max); + } + + ops->cleanup(serialize_data); + g_free(args); +} + +static void test_struct(gconstpointer opaque) +{ + TestArgs *args =3D (TestArgs *) opaque; + const SerializeOps *ops =3D args->ops; + TestStruct *ts =3D struct_create(); + TestStruct *ts_copy =3D NULL; + Error *err =3D NULL; + void *serialize_data; + + ops->serialize(ts, &serialize_data, visit_struct, &err); + ops->deserialize((void **)&ts_copy, serialize_data, visit_struct, &e= rr);=20 + + g_assert(err =3D=3D NULL); + struct_compare(ts, ts_copy); + + struct_cleanup(ts); + struct_cleanup(ts_copy); + + ops->cleanup(serialize_data); + g_free(args); +} + +static void test_nested_struct(gconstpointer opaque) +{ + TestArgs *args =3D (TestArgs *) opaque; + const SerializeOps *ops =3D args->ops; + UserDefNested *udnp =3D nested_struct_create(); + UserDefNested *udnp_copy =3D NULL; + Error *err =3D NULL; + void *serialize_data; + =20 + ops->serialize(udnp, &serialize_data, visit_nested_struct, &err); + ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_s= truct, &err);=20 + + g_assert(err =3D=3D NULL); + nested_struct_compare(udnp, udnp_copy); + + nested_struct_cleanup(udnp); + nested_struct_cleanup(udnp_copy); + + ops->cleanup(serialize_data); + g_free(args); +} + +static void test_nested_struct_list(gconstpointer opaque) +{ + TestArgs *args =3D (TestArgs *) opaque; + const SerializeOps *ops =3D args->ops; + UserDefNestedList *listp =3D NULL, *tmp, *tmp_copy, *listp_copy =3D = NULL; + Error *err =3D NULL; + void *serialize_data; + int i =3D 0; + + for (i =3D 0; i < 8; i++) { + tmp =3D g_malloc0(sizeof(UserDefNestedList)); + tmp->value =3D nested_struct_create(); + tmp->next =3D listp; + listp =3D tmp; + } + =20 + ops->serialize(listp, &serialize_data, visit_nested_struct_list, &er= r); + ops->deserialize((void **)&listp_copy, serialize_data, + visit_nested_struct_list, &err);=20 + + g_assert(err =3D=3D NULL); + + tmp =3D listp; + tmp_copy =3D listp_copy; + while (listp_copy) { + g_assert(listp); + nested_struct_compare(listp->value, listp_copy->value); + listp =3D listp->next; + listp_copy =3D listp_copy->next; + } + + qapi_free_UserDefNestedList(tmp); + qapi_free_UserDefNestedList(tmp_copy); + + ops->cleanup(serialize_data); + g_free(args); +} + +PrimitiveType pt_values[] =3D { + /* string tests */ + { + .description =3D "string_empty", + .type =3D PTYPE_STRING, + .value.string =3D "", + }, + { + .description =3D "string_whitespace", + .type =3D PTYPE_STRING, + .value.string =3D "a b c\td", + }, + { + .description =3D "string_newlines", + .type =3D PTYPE_STRING, + .value.string =3D "a\nb\n", + }, + { + .description =3D "string_commas", + .type =3D PTYPE_STRING, + .value.string =3D "a,b, c,d", + }, + { + .description =3D "string_single_quoted", + .type =3D PTYPE_STRING, + .value.string =3D "'a b',cd", + }, + { + .description =3D "string_double_quoted", + .type =3D PTYPE_STRING, + .value.string =3D "\"a b\",cd", + }, + /* boolean tests */ + { + .description =3D "boolean_true1", + .type =3D PTYPE_BOOLEAN, + .value.boolean =3D true, + }, + { + .description =3D "boolean_true2", + .type =3D PTYPE_BOOLEAN, + .value.boolean =3D 8, + }, + { + .description =3D "boolean_true3", + .type =3D PTYPE_BOOLEAN, + .value.boolean =3D -1, + }, + { + .description =3D "boolean_false1", + .type =3D PTYPE_BOOLEAN, + .value.boolean =3D false, + }, + { + .description =3D "boolean_false2", + .type =3D PTYPE_BOOLEAN, + .value.boolean =3D 0, + }, + /* number tests (double) */ + /* note: we format these to %.6f before comparing, since that's how + * we serialize them and it doesn't make sense to check precision + * beyond that. + */ + { + .description =3D "number_sanity1", + .type =3D PTYPE_NUMBER, + .value.number =3D -1, + }, + { + .description =3D "number_sanity2", + .type =3D PTYPE_NUMBER, + .value.number =3D 3.14159265, + }, + { + .description =3D "number_min", + .type =3D PTYPE_NUMBER, + .value.number =3D DBL_MIN, + }, + { + .description =3D "number_max", + .type =3D PTYPE_NUMBER, + .value.number =3D DBL_MAX, + }, + /* integer tests (int64) */ + { + .description =3D "integer_sanity1", + .type =3D PTYPE_INTEGER, + .value.integer =3D -1, + }, + { + .description =3D "integer_sanity2", + .type =3D PTYPE_INTEGER, + .value.integer =3D INT64_MAX / 2 + 1, + }, + { + .description =3D "integer_min", + .type =3D PTYPE_INTEGER, + .value.integer =3D INT64_MIN, + }, + { + .description =3D "integer_max", + .type =3D PTYPE_INTEGER, + .value.integer =3D INT64_MAX, + }, + /* uint8 tests */ + { + .description =3D "uint8_sanity1", + .type =3D PTYPE_U8, + .value.u8 =3D 1, + }, + { + .description =3D "uint8_sanity2", + .type =3D PTYPE_U8, + .value.u8 =3D UINT8_MAX / 2 + 1, + }, + { + .description =3D "uint8_min", + .type =3D PTYPE_U8, + .value.u8 =3D 0, + }, + { + .description =3D "uint8_max", + .type =3D PTYPE_U8, + .value.u8 =3D UINT8_MAX, + }, + /* uint16 tests */ + { + .description =3D "uint16_sanity1", + .type =3D PTYPE_U16, + .value.u16 =3D 1, + }, + { + .description =3D "uint16_sanity2", + .type =3D PTYPE_U16, + .value.u16 =3D UINT16_MAX / 2 + 1, + }, + { + .description =3D "uint16_min", + .type =3D PTYPE_U16, + .value.u16 =3D 0, + }, + { + .description =3D "uint16_max", + .type =3D PTYPE_U16, + .value.u16 =3D UINT16_MAX, + }, + /* uint32 tests */ + { + .description =3D "uint32_sanity1", + .type =3D PTYPE_U32, + .value.u32 =3D 1, + }, + { + .description =3D "uint32_sanity2", + .type =3D PTYPE_U32, + .value.u32 =3D UINT32_MAX / 2 + 1, + }, + { + .description =3D "uint32_min", + .type =3D PTYPE_U32, + .value.u32 =3D 0, + }, + { + .description =3D "uint32_max", + .type =3D PTYPE_U32, + .value.u32 =3D UINT32_MAX, + }, + /* uint64 tests */ + { + .description =3D "uint64_sanity1", + .type =3D PTYPE_U64, + .value.u64 =3D 1, + }, + { + .description =3D "uint64_sanity2", + .type =3D PTYPE_U64, + .value.u64 =3D UINT64_MAX / 2 + 1, + }, + { + .description =3D "uint64_min", + .type =3D PTYPE_U64, + .value.u64 =3D 0, + }, + { + .description =3D "uint64_max", + .type =3D PTYPE_U64, + .value.u64 =3D UINT64_MAX, + }, + /* int8 tests */ + { + .description =3D "int8_sanity1", + .type =3D PTYPE_S8, + .value.s8 =3D -1, + }, + { + .description =3D "int8_sanity2", + .type =3D PTYPE_S8, + .value.s8 =3D INT8_MAX / 2 + 1, + }, + { + .description =3D "int8_min", + .type =3D PTYPE_S8, + .value.s8 =3D INT8_MIN, + }, + { + .description =3D "int8_max", + .type =3D PTYPE_S8, + .value.s8 =3D INT8_MAX, + }, + /* int16 tests */ + { + .description =3D "int16_sanity1", + .type =3D PTYPE_S16, + .value.s16 =3D -1, + }, + { + .description =3D "int16_sanity2", + .type =3D PTYPE_S16, + .value.s16 =3D INT16_MAX / 2 + 1, + }, + { + .description =3D "int16_min", + .type =3D PTYPE_S16, + .value.s16 =3D INT16_MIN, + }, + { + .description =3D "int16_max", + .type =3D PTYPE_S16, + .value.s16 =3D INT16_MAX, + }, + /* int32 tests */ + { + .description =3D "int32_sanity1", + .type =3D PTYPE_S32, + .value.s32 =3D -1, + }, + { + .description =3D "int32_sanity2", + .type =3D PTYPE_S32, + .value.s32 =3D INT32_MAX / 2 + 1, + }, + { + .description =3D "int32_min", + .type =3D PTYPE_S32, + .value.s32 =3D INT32_MIN, + }, + { + .description =3D "int32_max", + .type =3D PTYPE_S32, + .value.s32 =3D INT32_MAX, + }, + /* int64 tests */ + { + .description =3D "int64_sanity1", + .type =3D PTYPE_S64, + .value.s64 =3D -1, + }, + { + .description =3D "int64_sanity2", + .type =3D PTYPE_S64, + .value.s64 =3D INT64_MAX / 2 + 1, + }, + { + .description =3D "int64_min", + .type =3D PTYPE_S64, + .value.s64 =3D INT64_MIN, + }, + { + .description =3D "int64_max", + .type =3D PTYPE_S64, + .value.s64 =3D INT64_MAX, + }, + { .type =3D PTYPE_EOL } +}; + +/* visitor-specific op implementations */ + +typedef struct QmpSerializeData { + QmpOutputVisitor *qov; + QmpInputVisitor *qiv; +} QmpSerializeData; + +static void qmp_serialize(void *native_in, void **datap, + VisitorFunc visit, Error **errp) +{ + QmpSerializeData *d =3D g_malloc0(sizeof(*d)); + + d->qov =3D qmp_output_visitor_new(); + visit(qmp_output_get_visitor(d->qov), &native_in, errp); + *datap =3D d; +} + +static void qmp_deserialize(void **native_out, void *datap, + VisitorFunc visit, Error **errp) +{ + QmpSerializeData *d =3D datap; + QString *output_json =3D qobject_to_json(qmp_output_get_qobject(d->q= ov)); + QObject *obj =3D qobject_from_json(qstring_get_str(output_json)); + + QDECREF(output_json); + d->qiv =3D qmp_input_visitor_new(obj); + visit(qmp_input_get_visitor(d->qiv), native_out, errp); +} + +static void qmp_cleanup(void *datap) +{ + QmpSerializeData *d =3D datap; + qmp_output_visitor_cleanup(d->qov); + qmp_input_visitor_cleanup(d->qiv); +} + +/* visitor registration, test harness */ + +/* note: to function interchangeably as a serialization mechanism your + * visitor test implementation should pass the test cases for all visito= r + * capabilities: primitives, structures, and lists + */ +static const SerializeOps visitors[] =3D { + { + .type =3D "QMP", + .serialize =3D qmp_serialize, + .deserialize =3D qmp_deserialize, + .cleanup =3D qmp_cleanup, + .caps =3D VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS + }, + { NULL } +}; + +static void add_visitor_type(const SerializeOps *ops) +{ + char testname_prefix[128]; + char testname[128]; + TestArgs *args; + int i =3D 0; + + sprintf(testname_prefix, "/visitor/serialization/%s", ops->type); + + if (ops->caps & VCAP_PRIMITIVES) { + while (pt_values[i].type !=3D PTYPE_EOL) { + sprintf(testname, "%s/primitives/%s", testname_prefix, + pt_values[i].description); + args =3D g_malloc0(sizeof(*args)); + args->ops =3D ops; + args->test_data =3D &pt_values[i]; + g_test_add_data_func(testname, args, test_primitives); + i++; + } + } + + if (ops->caps & VCAP_STRUCTURES) { + sprintf(testname, "%s/struct", testname_prefix); + args =3D g_malloc0(sizeof(*args)); + args->ops =3D ops; + args->test_data =3D NULL; + g_test_add_data_func(testname, args, test_struct); + + sprintf(testname, "%s/nested_struct", testname_prefix); + args =3D g_malloc0(sizeof(*args)); + args->ops =3D ops; + args->test_data =3D NULL; + g_test_add_data_func(testname, args, test_nested_struct); + } + + if (ops->caps & VCAP_LISTS) { + sprintf(testname, "%s/nested_struct_list", testname_prefix); + args =3D g_malloc0(sizeof(*args)); + args->ops =3D ops; + args->test_data =3D NULL; + g_test_add_data_func(testname, args, test_nested_struct_list); + } +} + +int main(int argc, char **argv) +{ + int i =3D 0; + + g_test_init(&argc, &argv, NULL); + + while (visitors[i].type !=3D NULL) { + add_visitor_type(&visitors[i]); + i++; + } + + g_test_run(); + + return 0; +} --=20 1.7.7