qapi: add support for lists of native types
cover qapi: add support for lists of native types
These patches apply on top of qemu.git master, and can also be obtained from:git://github.com/mdroth/qemu.git qapi-native-lists
Sending this now since a number of series have popped up in the past that wanted this, and Amos has some pending patches (query-mac-tables) that rely on this as well.
These patches add support for specifying lists of native qapi types (int/bool/str/number) like so:
{ 'type': 'foo',
'data': { 'bar': ['int'] }}for a 'bar' field that is a list of type 'int',
{ 'type': 'foo2',
'data': { 'bar2': ['str'] }}for a 'bar2' field that is a list of type 'str', and so on.
This uses linked list types for the native C representations, just as we do for complex schema-defined types. In the future we may add schema annotations of some sort to specify a more natural/efficient array type for the C representations, but this should serve the majority of uses-cases for now.
Makefile | 6 +- qapi-schema-test.json | 8 ++ scripts/qapi-types.py | 44 ++++++- scripts/qapi-visit.py | 36 ++++- scripts/qapi.py | 21 +++ tests/test-qmp-input-visitor.c | 181 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 172 ++++++++++++++++++++++++ tests/test-visitor-serialization.c | 256 +++++++++++++++++++++++++++++++++--- 8 files changed, 692 insertions(+), 32 deletions(-)
PATCH 1/8 qapi: qapi-types.py, native list support
Teach type generators about native types so they can generate the appropriate linked list types.
--- scripts/qapi-types.py | 44 +++++++++++++++++++++++++++++++++++++++++--- scripts/qapi.py | 21 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-)
scripts/qapi-types.py +41 -3
@@ -16,7 +16,18 @@ import os import getopt import errno -def generate_fwd_struct(name, members): +def generate_fwd_struct(name, members, builtin_type=False): + if builtin_type: + return mcgen(''' +typedef struct %(name)sList +{ + %(type)s value; + struct %(name)sList *next; +} %(name)sList; +''', + type=c_type(name), + name=name) + return mcgen(''' typedef struct %(name)s %(name)s;
@@ -28,6 +39,7 @@ typedef struct %(name)sList ''', name=name) + def generate_fwd_enum_struct(name, members): return mcgen(''' typedef struct %(name)sList
@@ -164,6 +176,7 @@ void qapi_free_%(type)s(%(c_type)s obj); def generate_type_cleanup(name): ret = mcgen(''' + void qapi_free_%(type)s(%(c_type)s obj) { QapiDeallocVisitor *md;
@@ -184,8 +197,9 @@ void qapi_free_%(type)s(%(c_type)s obj) try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:", - ["source", "header", "prefix=", "output-dir="]) + opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:", + ["source", "header", "builtins", + "prefix=", "output-dir="]) except getopt.GetoptError, err: print str(err) sys.exit(1)
@@ -197,6 +211,7 @@ h_file = 'qapi-types.h' do_c = False do_h = False +do_builtins = False for o, a in opts: if o in ("-p", "--prefix"):
@@ -207,6 +222,8 @@ for o, a in opts: do_c = True elif o in ("-h", "--header"): do_h = True + elif o in ("-b", "--builtins"): + do_builtins = True if not do_c and not do_h: do_c = True
@@ -282,6 +299,11 @@ fdecl.write(mcgen(''' exprs = parse_schema(sys.stdin) exprs = filter(lambda expr: not expr.has_key('gen'), exprs) +fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL")) +for typename in builtin_types: + fdecl.write(generate_fwd_struct(typename, None, builtin_type=True)) +fdecl.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL")) + for expr in exprs: ret = "\n" if expr.has_key('type'):
@@ -298,6 +320,22 @@ for expr in exprs: continue fdecl.write(ret) +# to avoid header dependency hell, we always generate declarations +# for built-in types in our header files and simply guard them +fdecl.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL")) +for typename in builtin_types: + fdecl.write(generate_type_cleanup_decl(typename + "List")) +fdecl.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL")) + +# ...this doesn't work for cases where we link in multiple objects that +# have the functions defined, so we use -b option to provide control +# over these cases +if do_builtins: + fdef.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF")) + for typename in builtin_types: + fdef.write(generate_type_cleanup(typename + "List")) + fdef.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF")) + for expr in exprs: ret = "\n" if expr.has_key('type'):
scripts/qapi.py +21 -0
@@ -11,6 +11,10 @@ from ordereddict import OrderedDict +builtin_types = [ + 'str', 'int', 'number', 'bool' +] + def tokenize(data): while len(data): ch = data[0]
@@ -242,3 +246,20 @@ def guardname(filename): for substr in [".", " ", "-"]: guard = guard.replace(substr, "_") return guard.upper() + '_H' + +def guardstart(name): + return mcgen(''' + +#ifndef %(name)s +#define %(name)s + +''', + name=guardname(name)) + +def guardend(name): + return mcgen(''' + +#endif /* %(name)s */ + +''', + name=guardname(name))
Signature · 1 line
1.7.9.5
PATCH 2/8 qapi: qapi-visit.py, fix list handling for union types
In reply to Michael RothCurrently we assume non-list types when generating visitor routines for union types. This is broken, since values like ['Type'] need to mapped to 'TypeList'.
We already have a type_name() function to handle this that we use for generating struct visitors, so use that here as well.
--- scripts/qapi-visit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qapi-visit.py +1 -1
@@ -174,7 +174,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** ''', abbrev = de_camel_case(name).upper(), enum = c_fun(de_camel_case(key),False).upper(), - c_type=members[key], + c_type=type_name(members[key]), c_name=c_fun(key)) ret += mcgen('''
Signature · 1 line
1.7.9.5
PATCH 3/8 qapi: qapi-visit.py, native list support
In reply to Michael RothTeach visitor generators about native types so they can generate the appropriate visitor routines.
--- scripts/qapi-visit.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
scripts/qapi-visit.py +29 -5
@@ -202,12 +202,14 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** return ret -def generate_declaration(name, members, genlist=True): - ret = mcgen(''' +def generate_declaration(name, members, genlist=True, builtin_type=False): + ret = "" + if not builtin_type: + ret += mcgen(''' void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp); ''', - name=name) + name=name) if genlist: ret += mcgen('''
@@ -235,8 +237,9 @@ void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **e name=name) try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:", - ["source", "header", "prefix=", "output-dir="]) + opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:", + ["source", "header", "builtins", "prefix=", + "output-dir="]) except getopt.GetoptError, err: print str(err) sys.exit(1)
@@ -248,6 +251,7 @@ h_file = 'qapi-visit.h' do_c = False do_h = False +do_builtins = False for o, a in opts: if o in ("-p", "--prefix"):
@@ -258,6 +262,8 @@ for o, a in opts: do_c = True elif o in ("-h", "--header"): do_h = True + elif o in ("-b", "--builtins"): + do_builtins = True if not do_c and not do_h: do_c = True
@@ -324,11 +330,29 @@ fdecl.write(mcgen(''' #include "qapi/visitor.h" #include "%(prefix)sqapi-types.h" + ''', prefix=prefix, guard=guardname(h_file))) exprs = parse_schema(sys.stdin) +# to avoid header dependency hell, we always generate declarations +# for built-in types in our header files and simply guard them +fdecl.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL")) +for typename in builtin_types: + fdecl.write(generate_declaration(typename, None, genlist=True, + builtin_type=True)) +fdecl.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL")) + +# ...this doesn't work for cases where we link in multiple objects that +# have the functions defined, so we use -b option to provide control +# over these cases +if do_builtins: + fdef.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DEF")) + for typename in builtin_types: + fdef.write(generate_visit_list(typename, None)) + fdef.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DEF")) + for expr in exprs: if expr.has_key('type'): ret = generate_visit_struct(expr['type'], expr['data'])
Signature · 1 line
1.7.9.5
PATCH 4/8 qapi: enable generation of native list code
In reply to Michael RothAlso, fix a dependency issue with libqemuutil: qemu-sockets.c needs qapi-types.c/qapi-visit.c
--- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Makefile +3 -3
@@ -178,7 +178,7 @@ Makefile: $(version-obj-y) $(version-lobj-y) # Build libraries libqemustub.a: $(stub-obj-y) -libqemuutil.a: $(util-obj-y) +libqemuutil.a: $(util-obj-y) qapi-types.o qapi-visit.o ######################################################################
@@ -215,10 +215,10 @@ $(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) qapi-types.c qapi-types.h :\ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py) - $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o "." < $<, " GEN $@") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o "." -b < $<, " GEN $@") qapi-visit.c qapi-visit.h :\ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py) - $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o "." < $<, " GEN $@") + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o "." -b < $<, " GEN $@") qmp-commands.h qmp-marshal.c :\ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -m -o "." < $<, " GEN $@")
Signature · 1 line
1.7.9.5
PATCH 5/8 qapi: fix leak in unit tests
In reply to Michael Rothqmp_output_get_qobject() increments the qobject's reference count. Since we currently pass this straight into qobject_to_json() so we can feed the data into a QMP input visitor, we never actually free the underlying qobject when qmp_output_visitor_cleanup() is called. This causes leaks on all of the QMP serialization tests.
Fix this by holding a pointer to the qobject and decref'ing it before returning from qmp_deserialize().
--- tests/test-visitor-serialization.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tests/test-visitor-serialization.c +7 -2
@@ -657,11 +657,16 @@ static void qmp_deserialize(void **native_out, void *datap, VisitorFunc visit, Error **errp) { QmpSerializeData *d = datap; -QString *output_json =qobject_to_json (qmp_output_get_qobject (d->qov)); -QObject *obj =qobject_from_json (qstring_get_str (output_json)); +QString *output_json; +QObject *obj_orig, *obj; + + obj_orig =qmp_output_get_qobject (d->qov); + output_json =qobject_to_json (obj_orig); + obj =qobject_from_json (qstring_get_str (output_json));QDECREF (output_json); d->qiv =qmp_input_visitor_new (obj); +qobject_decref (obj_orig);qobject_decref (obj);visit (qmp_input_get_visitor (d->qiv), native_out, errp); }
Signature · 1 line
1.7.9.5
PATCH 6/8 qapi: add native list coverage for visitor serialization tests
In reply to Michael Roth--- tests/test-visitor-serialization.c | 247 +++++++++++++++++++++++++++++++++--- 1 file changed, 229 insertions(+), 18 deletions(-)
tests/test-visitor-serialization.c +229 -18
@@ -23,6 +23,25 @@#include "qapi/qmp-output-visitor.h" #include "qapi/string-input-visitor.h" #include "qapi/string-output-visitor.h" +#include "qapi-types.h" +#include "qapi-visit.h" +#include "qapi/dealloc-visitor.h" + +enum PrimitiveTypeKind { +PTYPE_STRING =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 , +}; typedef struct PrimitiveType { union {
@@ -40,26 +59,34 @@ typedef struct PrimitiveType {int64_t s64;intmax_t max; } value; -enum { -PTYPE_STRING =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; +enum PrimitiveTypeKind type;const char *description; } PrimitiveType; +typedef struct PrimitiveList { +union { +strList *strings; +boolList *booleans; +numberList *numbers; +intList *integers; + } value; +enum PrimitiveTypeKind type; +const char *description; +}PrimitiveList ; +/* test helpers */ +typedef void (*VisitorFunc )(Visitor *v,void **native,Error **errp); + +static void dealloc_helper (void *native_in,VisitorFunc visit,Error **errp) +{ +QapiDeallocVisitor *qdv =qapi_dealloc_visitor_new (); + +visit (qapi_dealloc_get_visitor (qdv), &native_in, errp); + +qapi_dealloc_visitor_cleanup (qdv); +} +static void visit_primitive_type (Visitor *v,void **native,Error **errp) {PrimitiveType *pt = *native;
@@ -105,6 +132,27 @@ static void visit_primitive_type(Visitor *v, void **native, Error **errp) } } +static void visit_primitive_list (Visitor *v,void **native,Error **errp) +{ +PrimitiveList *pl = *native; +switch (pl->type) { +case PTYPE_STRING : +visit_type_strList (v, &pl->value.strings,NULL , errp); +break ; +case PTYPE_BOOLEAN : +visit_type_boolList (v, &pl->value.booleans,NULL , errp); +break ; +case PTYPE_NUMBER : +visit_type_numberList (v, &pl->value.numbers,NULL , errp); +break ; +case PTYPE_INTEGER : +visit_type_intList (v, &pl->value.integers,NULL , errp); +break ; +default : +g_assert (false); + } +} + typedef struct TestStruct { int64_t integer;
@@ -206,12 +254,11 @@ static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)/* test cases */ -typedef void (*VisitorFunc )(Visitor *v,void **native,Error **errp); -typedef enum VisitorCapabilities {VCAP_PRIMITIVES =1 ,VCAP_STRUCTURES =2 ,VCAP_LISTS =4 , +VCAP_PRIMITIVE_LISTS =8 , }VisitorCapabilities ; typedef struct SerializeOps {
@@ -279,6 +326,151 @@ static void test_primitives(gconstpointer opaque)g_free (pt_copy); } +static void test_primitive_lists (gconstpointer opaque) +{ +TestArgs *args = (TestArgs *) opaque; +const SerializeOps *ops = args->ops; +PrimitiveType *pt = args->test_data; +PrimitiveList pl = { .value = {0 } }; +PrimitiveList pl_copy = { .value = {0 } }; +PrimitiveList *pl_copy_ptr = &pl_copy; +Error *err =NULL ; +void *serialize_data; +void *cur_head =NULL ; +int i; + + pl.type = pl_copy.type = pt->type; + +/* build up our list of primitive types */ +for (i =0 ; i <32 ; i++) { +switch (pl.type) { +case PTYPE_STRING : { +strList *tmp =g_new0 (strList,1 ); + tmp->value =g_strdup (pt->value.string); +if (pl.value.strings ==NULL ) { + pl.value.strings = tmp; + }else { + tmp->next = pl.value.strings; + pl.value.strings = tmp; + } +break ; + } +case PTYPE_INTEGER : { +intList *tmp =g_new0 (intList,1 ); + tmp->value = pt->value.integer; +if (pl.value.integers ==NULL ) { + pl.value.integers = tmp; + }else { + tmp->next = pl.value.integers; + pl.value.integers = tmp; + } +break ; + } +case PTYPE_NUMBER : { +numberList *tmp =g_new0 (numberList,1 ); + tmp->value = pt->value.number; +if (pl.value.numbers ==NULL ) { + pl.value.numbers = tmp; + }else { + tmp->next = pl.value.numbers; + pl.value.numbers = tmp; + } +break ; + } +case PTYPE_BOOLEAN : { +boolList *tmp =g_new0 (boolList,1 ); + tmp->value = pt->value.boolean; +if (pl.value.booleans ==NULL ) { + pl.value.booleans = tmp; + }else { + tmp->next = pl.value.booleans; + pl.value.booleans = tmp; + } +break ; + } +default : +g_assert (0 ); + } + } + + ops->serialize ((void **)&pl, &serialize_data, visit_primitive_list, &err); + ops->deserialize ((void **)&pl_copy_ptr, serialize_data, visit_primitive_list, &err); + +g_assert (err ==NULL ); + i =0 ; + +/* compare our deserialized list of primitives to the original */ +do { +switch (pl_copy.type) { +case PTYPE_STRING : { +strList *ptr; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.strings; + } + g_assert_cmpstr(pt->value.string, ==, ptr->value); +break ; + } +case PTYPE_INTEGER : { +intList *ptr; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.integers; + } + g_assert_cmpint(pt->value.integer, ==, ptr->value); +break ; + } +case PTYPE_NUMBER : { +numberList *ptr; +char *double1, *double2; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.numbers; + } +/* we serialize with %f for our reference visitors, so rather than +* fuzzy * floating math to test "equality", just compare the +* formatted values +*/ + double1 =g_malloc0 (calc_float_string_storage (pt->value.number)); + double2 =g_malloc0 (calc_float_string_storage (ptr->value)); + g_assert_cmpstr(double1, ==, double2); +g_free (double1); +g_free (double2); +break ; + } +case PTYPE_BOOLEAN : { +boolList *ptr; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.booleans; + } + g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value); +break ; + } +default : +g_assert (0 ); + } + i++; + }while (cur_head); + + g_assert_cmpint(i, ==, 33); + + ops->cleanup (serialize_data); +dealloc_helper (&pl, visit_primitive_list, &err); +g_assert (!err); +dealloc_helper (&pl_copy, visit_primitive_list, &err); +g_assert (!err); +g_free (args); +} +static void test_struct (gconstpointer opaque) {TestArgs *args = (TestArgs *) opaque;
@@ -728,7 +920,8 @@ static const SerializeOps visitors[] = { .serialize = qmp_serialize, .deserialize = qmp_deserialize, .cleanup = qmp_cleanup, - .caps =VCAP_PRIMITIVES |VCAP_STRUCTURES |VCAP_LISTS + .caps =VCAP_PRIMITIVES |VCAP_STRUCTURES |VCAP_LISTS | +VCAP_PRIMITIVE_LISTS }, { .type = "String",
@@ -782,6 +975,24 @@ static void add_visitor_type(const SerializeOps *ops) args->test_data =NULL ;g_test_add_data_func (testname, args, test_nested_struct_list); } + +if (ops->caps &VCAP_PRIMITIVE_LISTS ) { + i =0 ; +while (pt_values[i].type !=PTYPE_EOL ) { +if (pt_values[i].type ==PTYPE_STRING + || pt_values[i].type ==PTYPE_NUMBER + || pt_values[i].type ==PTYPE_BOOLEAN + || pt_values[i].type ==PTYPE_INTEGER ) { +sprintf (testname,"%s/primitive_list/%s" , testname_prefix, + pt_values[i].description); + args =g_malloc0 (sizeof (*args)); + args->ops = ops; + args->test_data = &pt_values[i]; +g_test_add_data_func (testname, args, test_primitive_lists); + } + i++; + } + } }int main (int argc,char **argv)
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 6/8] qapi: add native list coverage for visitor serialization tests
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 439
Quoted 13 lines · Michael Roth
tests/test-visitor-serialization.c +13
-0+case PTYPE_NUMBER : { +numberList *ptr; +char *double1, *double2; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.numbers; + } +/* we serialize with %f for our reference visitors, so rather than +* fuzzy * floating math to test "equality", just compare the +* formatted values +*/
I think this comment block has been copied from elsewhere in this file, indented more deeply and re-filled. There's an asterisk in the comment body now.
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 442
Quoted 3 lines
tests/test-visitor-serialization.c +3
-0+ double1 =g_malloc0 (calc_float_string_storage (pt->value.number)); + double2 =g_malloc0 (calc_float_string_storage (ptr->value)); + g_assert_cmpstr(double1, ==, double2);
Are you comparing empty strings? Space is allocated and zeroed, but I can't see where the values are actually formatted.
(Same holds for the original instance of this code, test_primitives().)
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 446
Quoted 4 lines
tests/test-visitor-serialization.c +4
-0+g_free (double1); +g_free (double2); +break ; + }
Thanks,
Laszlo
Re: [Qemu-devel] [PATCH 6/8] qapi: add native list coverage for visitor serialization tests
Quoted 16 lines · Laszlo Ersek
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 439
Quoted 13 lines · Michael Roth
tests/test-visitor-serialization.c +13
-0+case PTYPE_NUMBER : { +numberList *ptr; +char *double1, *double2; +if (cur_head) { + ptr = cur_head; + cur_head = ptr->next; + }else { + cur_head = ptr = pl_copy.value.numbers; + } +/* we serialize with %f for our reference visitors, so rather than +* fuzzy * floating math to test "equality", just compare the +* formatted values +*/
Quoted 3 lines
I think this comment block has been copied from elsewhere in this file, indented more deeply and re-filled. There's an asterisk in the comment body now.
Yes :) Will fix it on the next pass.
Quoted 8 lines
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 442
Quoted 5 lines
tests/test-visitor-serialization.c +3
-0+ double1 =g_malloc0 (calc_float_string_storage (pt->value.number)); + double2 =g_malloc0 (calc_float_string_storage (ptr->value)); + g_assert_cmpstr(double1, ==, double2);Are you comparing empty strings? Space is allocated and zeroed, but I can't see where the values are actually formatted.
Well, that's embarassing...
Quoted 3 lines
(Same holds for the original instance of this code,
test_primitives().)
but at least I can blame it on the fact that I copied it from here (we'll just ignore who the original author was :)
Will add a patch on the next pass to fix the original instance beforehand.
Thanks for the catch!
Quoted 8 lines
tests/test-visitor-serialization.c static void test_primitives(gconstpointer opaque) · line 446
Quoted 4 lines
tests/test-visitor-serialization.c +4
-0+g_free (double1); +g_free (double2); +break ; + }Thanks,
Laszlo
PATCH 7/8 qapi: add native list coverage for QMP output visitor tests
In reply to Michael RothThis exercises schema-generated visitors for native list types and does some sanity checking on validity of serialized data.
--- qapi-schema-test.json | 8 ++ tests/test-qmp-output-visitor.c | 172 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+)
qapi-schema-test.json +8 -0
@@ -32,6 +32,14 @@ { 'union': 'UserDefUnion', 'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } } +# for testing native lists +{ 'union': 'UserDefNativeListUnion', + 'data': { 'integer': ['int'], + 'number': ['number'], + 'boolean': ['bool'], + 'string': ['str'], + 'userdef': ['UserDefOne'] } } + # testing commands { 'command': 'user_def_cmd', 'data': {} } { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} }
tests/test-qmp-output-visitor.c +172 -0
184 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 8/8 qapi: add native list coverage for QMP input visitor tests
In reply to Michael RothThis exercises schema-generated visitors for native list types and does some sanity checking on validity of deserialized data.
--- tests/test-qmp-input-visitor.c | 181 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+)
tests/test-qmp-input-visitor.c +181 -0
@@ -61,6 +61,31 @@ Visitor *visitor_input_test_init(TestInputVisitorData *data,return v; } +/* similar to visitor_input_test_init(), but does not expect a string +* literal/format json_string argument and so can be used for +* programatically generated strings (and we can't pass in programatically +* generated strings via %s format parameters since qobject_from_jsonv() +* will wrap those in double-quotes and treat the entire object as a +* string) +*/ +static Visitor *visitor_input_test_init_raw (TestInputVisitorData *data, +const char *json_string) +{ +Visitor *v; + + data->obj =qobject_from_json (json_string); + +g_assert (data->obj !=NULL ); + + data->qiv =qmp_input_visitor_new (data->obj); +g_assert (data->qiv !=NULL ); + + v =qmp_input_get_visitor (data->qiv); +g_assert (v !=NULL ); + +return v; +} +static void test_visitor_in_int (TestInputVisitorData *data,const void *unused) {
@@ -259,6 +284,154 @@ static void test_visitor_in_union(TestInputVisitorData *data,qapi_free_UserDefUnion (tmp); } +static void test_visitor_in_native_list_int (TestInputVisitorData *data, +const void *unused) +{ +UserDefNativeListUnion *cvalue =NULL ; +intList *elem =NULL ; +Error *err =NULL ; +Visitor *v; +GString *gstr_list =g_string_new ("" ); +GString *gstr_union =g_string_new ("" ); +int i; + +for (i =0 ; i <32 ; i++) { +g_string_append_printf (gstr_list,"%d" , i); +if (i !=31 ) { +g_string_append (gstr_list,", " ); + } + } +g_string_append_printf (gstr_union,"{ 'type': 'integer', 'data': [ %s ] }" , + gstr_list->str); + v =visitor_input_test_init_raw (data, gstr_union->str); + +visit_type_UserDefNativeListUnion (v, &cvalue,NULL , &err); +g_assert (err ==NULL ); +g_assert (cvalue !=NULL ); + g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); + +for (i =0 , elem = cvalue->integer; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + +g_string_free (gstr_union, true); +g_string_free (gstr_list, true); +qapi_free_UserDefNativeListUnion (cvalue); +} + +static void test_visitor_in_native_list_bool (TestInputVisitorData *data, +const void *unused) +{ +UserDefNativeListUnion *cvalue =NULL ; +boolList *elem =NULL ; +Error *err =NULL ; +Visitor *v; +GString *gstr_list =g_string_new ("" ); +GString *gstr_union =g_string_new ("" ); +int i; + +for (i =0 ; i <32 ; i++) { +g_string_append_printf (gstr_list,"%s" , + (i %3 ==0 ) ?"true" :"false" ); +if (i !=31 ) { +g_string_append (gstr_list,", " ); + } + } +g_string_append_printf (gstr_union,"{ 'type': 'boolean', 'data': [ %s ] }" , + gstr_list->str); + v =visitor_input_test_init_raw (data, gstr_union->str); + +visit_type_UserDefNativeListUnion (v, &cvalue,NULL , &err); +g_assert (err ==NULL ); +g_assert (cvalue !=NULL ); + g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); + +for (i =0 , elem = cvalue->boolean; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); + } + +g_string_free (gstr_union, true); +g_string_free (gstr_list, true); +qapi_free_UserDefNativeListUnion (cvalue); +} + +static void test_visitor_in_native_list_string (TestInputVisitorData *data, +const void *unused) +{ +UserDefNativeListUnion *cvalue =NULL ; +strList *elem =NULL ; +Error *err =NULL ; +Visitor *v; +GString *gstr_list =g_string_new ("" ); +GString *gstr_union =g_string_new ("" ); +int i; + +for (i =0 ; i <32 ; i++) { +g_string_append_printf (gstr_list,"'%d'" , i); +if (i !=31 ) { +g_string_append (gstr_list,", " ); + } + } +g_string_append_printf (gstr_union,"{ 'type': 'string', 'data': [ %s ] }" , + gstr_list->str); + v =visitor_input_test_init_raw (data, gstr_union->str); + +visit_type_UserDefNativeListUnion (v, &cvalue,NULL , &err); +g_assert (err ==NULL ); +g_assert (cvalue !=NULL ); + g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); + +for (i =0 , elem = cvalue->string; elem; elem = elem->next, i++) { +gchar str[8 ]; +sprintf (str,"%d" , i); + g_assert_cmpstr(elem->value, ==, str); + } + +g_string_free (gstr_union, true); +g_string_free (gstr_list, true); +qapi_free_UserDefNativeListUnion (cvalue); +} + +#define DOUBLE_STR_MAX 16 + +static void test_visitor_in_native_list_number (TestInputVisitorData *data, +const void *unused) +{ +UserDefNativeListUnion *cvalue =NULL ; +numberList *elem =NULL ; +Error *err =NULL ; +Visitor *v; +GString *gstr_list =g_string_new ("" ); +GString *gstr_union =g_string_new ("" ); +int i; + +for (i =0 ; i <32 ; i++) { +g_string_append_printf (gstr_list,"%f" , (double )i /3 ); +if (i !=31 ) { +g_string_append (gstr_list,", " ); + } + } +g_string_append_printf (gstr_union,"{ 'type': 'number', 'data': [ %s ] }" , + gstr_list->str); + v =visitor_input_test_init_raw (data, gstr_union->str); + +visit_type_UserDefNativeListUnion (v, &cvalue,NULL , &err); +g_assert (err ==NULL ); +g_assert (cvalue !=NULL ); + g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); + +for (i =0 , elem = cvalue->number; elem; elem = elem->next, i++) { +gchar str_expected[DOUBLE_STR_MAX ], str_actual[DOUBLE_STR_MAX ]; +snprintf (str_expected,DOUBLE_STR_MAX ,"%3.4f" , (double )i /3 ); +snprintf (str_actual,DOUBLE_STR_MAX ,"%3.4f" , elem->value); + g_assert_cmpstr(str_actual, ==, str_expected); + } + +g_string_free (gstr_union, true); +g_string_free (gstr_list, true); +qapi_free_UserDefNativeListUnion (cvalue); +} +static void input_visitor_test_add (const char *testpath,TestInputVisitorData *data,void (*test_func)(TestInputVisitorData *data,const void *user_data))
@@ -310,6 +483,14 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_union);input_visitor_test_add ("/visitor/input/errors" , &in_visitor_data, test_visitor_in_errors); +input_visitor_test_add ("/visitor/input/native_list/int" , + &in_visitor_data, test_visitor_in_native_list_int); +input_visitor_test_add ("/visitor/input/native_list/bool" , + &in_visitor_data, test_visitor_in_native_list_bool); +input_visitor_test_add ("/visitor/input/native_list/str" , + &in_visitor_data, test_visitor_in_native_list_string); +input_visitor_test_add ("/visitor/input/native_list/number" , + &in_visitor_data, test_visitor_in_native_list_number);g_test_run ();
Signature · 1 line
1.7.9.5
Quoted 25 lines · Michael Roth
These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.gitqapi-native-listsSending this now since a number of series have popped up in the past that wanted this, and Amos has some pending patches (query-mac-tables) that rely on this as well.
These patches add support for specifying lists of native qapi types (int/bool/str/number) like so:
{ 'type': 'foo',
'data': { 'bar': ['int'] }}for a 'bar' field that is a list of type 'int',
{ 'type': 'foo2',
'data': { 'bar2': ['str'] }}for a 'bar2' field that is a list of type 'str', and so on.
This uses linked list types for the native C representations, just as we do for complex schema-defined types. In the future we may add schema annotations of some sort to specify a more natural/efficient array type for the C representations, but this should serve the majority of uses-cases for now.
Quoted 11 lines
Makefile | 6 +- qapi-schema-test.json | 8 ++ scripts/qapi-types.py | 44 ++++++- scripts/qapi-visit.py | 36 ++++- scripts/qapi.py | 21 +++ tests/test-qmp-input-visitor.c | 181 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 172 ++++++++++++++++++++++++ tests/test-visitor-serialization.c | 256 +++++++++++++++++++++++++++++++++--- 8 files changed, 692 insertions(+), 32 deletions(-)
Two notes:
- the remark I made for 6/8 (comparing empty strings),
- for 7/8 and 8/8: the format specification %3.4f is not very useful. "3" is the field width, "4" is the precision, and the latter means for %f the number of digits printed after the radix character. It's not useful to specify a smaller field width (which covers the entire output string) than precision here.
Other than these nothing pokes me in the eye.
Laszlo
Quoted 38 lines · Laszlo Ersek
Quoted 25 lines · Michael Roth
These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.gitqapi-native-listsSending this now since a number of series have popped up in the past that wanted this, and Amos has some pending patches (query-mac-tables) that rely on this as well.
These patches add support for specifying lists of native qapi types (int/bool/str/number) like so:
{ 'type': 'foo',
'data': { 'bar': ['int'] }}for a 'bar' field that is a list of type 'int',
{ 'type': 'foo2',
'data': { 'bar2': ['str'] }}for a 'bar2' field that is a list of type 'str', and so on.
This uses linked list types for the native C representations, just as we do for complex schema-defined types. In the future we may add schema annotations of some sort to specify a more natural/efficient array type for the C representations, but this should serve the majority of uses-cases for now.
Quoted 11 lines
Makefile | 6 +- qapi-schema-test.json | 8 ++ scripts/qapi-types.py | 44 ++++++- scripts/qapi-visit.py | 36 ++++- scripts/qapi.py | 21 +++ tests/test-qmp-input-visitor.c | 181 +++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 172 ++++++++++++++++++++++++ tests/test-visitor-serialization.c | 256 +++++++++++++++++++++++++++++++++--- 8 files changed, 692 insertions(+), 32 deletions(-)
Quoted 8 lines
Two notes:
- the remark I made for 6/8 (comparing empty strings),- for 7/8 and 8/8: the format specification %3.4f is not very useful. "3" is the field width, "4" is the precision, and the latter means for %f the number of digits printed after the radix character. It's not useful to specify a smaller field width (which covers the entire output string) than precision here.
Yup, that's a mistake. The field width isn't useful at all for what I was intending actually (to constrain the whole portion of float so that the fractional portion wouldn't get truncated by snprintf and make the test less likely to catch re-encoding issues). I think just using %.4f should suffice since we have plenty of extra buffer space for the values we're working with (max being 32 / 3) so I'll do that for the next pass.
Quoted 5 lines
Other than these nothing pokes me in the eye.
Laszlo