Skip to content

Commit

Permalink
qapi: Merge UserDefTwo and UserDefNested in tests
Browse files Browse the repository at this point in the history
In the testsuite, UserDefTwo and UserDefNested were identical
structs other than the member names.  Reduce code duplication by
having just one type, and choose names that also favor reuse.
This will also make it easier for a later patch to get rid of
inline nested types in QAPI.  When touching code related to
allocations, convert g_malloc0(sizeof(Type)) to the more typesafe
g_new0(Type, 1).

Ensure that 'make check-qapi-schema check-unit' still passes.

Signed-off-by: Eric Blake <[email protected]>
Reviewed-by: Markus Armbruster <[email protected]>
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
ebblake authored and Markus Armbruster committed May 5, 2015
1 parent 3e391d3 commit b6fcf32
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 103 deletions.
10 changes: 2 additions & 8 deletions tests/qapi-schema/qapi-schema-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
'data': { 'string': 'str', '*enum1': 'EnumOne' } }

{ 'struct': 'UserDefTwo',
'data': { 'string': 'str',
'dict': { 'string': 'str',
'dict': { 'userdef': 'UserDefOne', 'string': 'str' },
'*dict2': { 'userdef': 'UserDefOne', 'string': 'str' } } } }

{ 'struct': 'UserDefNested',
'data': { 'string0': 'str',
'dict1': { 'string1': 'str',
'dict2': { 'userdef1': 'UserDefOne', 'string2': 'str' },
'*dict3': { 'userdef2': 'UserDefOne', 'string3': 'str' } } } }
'dict2': { 'userdef': 'UserDefOne', 'string': 'str' },
'*dict3': { 'userdef': 'UserDefOne', 'string': 'str' } } } }

# for testing unions
{ 'struct': 'UserDefA',
Expand Down
6 changes: 2 additions & 4 deletions tests/qapi-schema/qapi-schema-test.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
OrderedDict([('struct', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
OrderedDict([('struct', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
OrderedDict([('struct', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
OrderedDict([('struct', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
OrderedDict([('struct', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
OrderedDict([('struct', 'UserDefC'), ('data', OrderedDict([('string1', 'str'), ('string2', 'str')]))]),
Expand All @@ -28,8 +27,7 @@
[OrderedDict([('struct', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),
OrderedDict([('struct', 'UserDefZero'), ('data', OrderedDict([('integer', 'int')]))]),
OrderedDict([('struct', 'UserDefOne'), ('base', 'UserDefZero'), ('data', OrderedDict([('string', 'str'), ('*enum1', 'EnumOne')]))]),
OrderedDict([('struct', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefTwo'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict3', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]),
OrderedDict([('struct', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
OrderedDict([('struct', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
OrderedDict([('struct', 'UserDefC'), ('data', OrderedDict([('string1', 'str'), ('string2', 'str')]))]),
Expand Down
34 changes: 17 additions & 17 deletions tests/test-qmp-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
ud1d->base = g_new0(UserDefZero, 1);
ud1d->base->integer = has_udb1 ? ud1b->base->integer : 0;

ret = g_malloc0(sizeof(UserDefTwo));
ret->string = strdup("blah1");
ret->dict.string = strdup("blah2");
ret->dict.dict.userdef = ud1c;
ret->dict.dict.string = strdup("blah3");
ret->dict.has_dict2 = true;
ret->dict.dict2.userdef = ud1d;
ret->dict.dict2.string = strdup("blah4");
ret = g_new0(UserDefTwo, 1);
ret->string0 = strdup("blah1");
ret->dict1.string1 = strdup("blah2");
ret->dict1.dict2.userdef = ud1c;
ret->dict1.dict2.string = strdup("blah3");
ret->dict1.has_dict3 = true;
ret->dict1.dict3.userdef = ud1d;
ret->dict1.dict3.string = strdup("blah4");

return ret;
}
Expand Down Expand Up @@ -120,15 +120,15 @@ static void test_dispatch_cmd_io(void)

ret = qobject_to_qdict(test_qmp_dispatch(req));

assert(!strcmp(qdict_get_str(ret, "string"), "blah1"));
ret_dict = qdict_get_qdict(ret, "dict");
assert(!strcmp(qdict_get_str(ret_dict, "string"), "blah2"));
ret_dict_dict = qdict_get_qdict(ret_dict, "dict");
assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
ret_dict = qdict_get_qdict(ret, "dict1");
assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict2");
ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
Expand Down Expand Up @@ -192,7 +192,7 @@ static void test_dealloc_partial(void)
QmpInputVisitor *qiv;

ud2_dict = qdict_new();
qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text)));
qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));

qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
Expand All @@ -202,9 +202,9 @@ static void test_dealloc_partial(void)

/* verify partial success */
assert(ud2 != NULL);
assert(ud2->string != NULL);
assert(strcmp(ud2->string, text) == 0);
assert(ud2->dict.dict.userdef == NULL);
assert(ud2->string0 != NULL);
assert(strcmp(ud2->string0, text) == 0);
assert(ud2->dict1.dict2.userdef == NULL);

/* confirm & release construction error */
assert(err != NULL);
Expand Down
17 changes: 10 additions & 7 deletions tests/test-qmp-input-strict.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,18 @@ static void test_validate_struct(TestInputVisitorData *data,
static void test_validate_struct_nested(TestInputVisitorData *data,
const void *unused)
{
UserDefNested *udp = NULL;
UserDefTwo *udp = NULL;
Error *err = NULL;
Visitor *v;

v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
v = validate_test_init(data, "{ 'string0': 'string0', "
"'dict1': { 'string1': 'string1', "
"'dict2': { 'userdef': { 'integer': 42, "
"'string': 'string' }, 'string': 'string2'}}}");

visit_type_UserDefNested(v, &udp, NULL, &err);
visit_type_UserDefTwo(v, &udp, NULL, &err);
g_assert(!err);
qapi_free_UserDefNested(udp);
qapi_free_UserDefTwo(udp);
}

static void test_validate_list(TestInputVisitorData *data,
Expand Down Expand Up @@ -207,15 +210,15 @@ static void test_validate_fail_struct(TestInputVisitorData *data,
static void test_validate_fail_struct_nested(TestInputVisitorData *data,
const void *unused)
{
UserDefNested *udp = NULL;
UserDefTwo *udp = NULL;
Error *err = NULL;
Visitor *v;

v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");

visit_type_UserDefNested(v, &udp, NULL, &err);
visit_type_UserDefTwo(v, &udp, NULL, &err);
g_assert(err);
qapi_free_UserDefNested(udp);
qapi_free_UserDefTwo(udp);
}

static void test_validate_fail_list(TestInputVisitorData *data,
Expand Down
17 changes: 10 additions & 7 deletions tests/test-qmp-input-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,26 @@ static void check_and_free_str(char *str, const char *cmp)
static void test_visitor_in_struct_nested(TestInputVisitorData *data,
const void *unused)
{
UserDefNested *udp = NULL;
UserDefTwo *udp = NULL;
Error *err = NULL;
Visitor *v;

v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
v = visitor_input_test_init(data, "{ 'string0': 'string0', "
"'dict1': { 'string1': 'string1', "
"'dict2': { 'userdef': { 'integer': 42, "
"'string': 'string' }, 'string': 'string2'}}}");

visit_type_UserDefNested(v, &udp, NULL, &err);
visit_type_UserDefTwo(v, &udp, NULL, &err);
g_assert(!err);

check_and_free_str(udp->string0, "string0");
check_and_free_str(udp->dict1.string1, "string1");
g_assert_cmpint(udp->dict1.dict2.userdef1->base->integer, ==, 42);
check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
check_and_free_str(udp->dict1.dict2.string2, "string2");
g_assert_cmpint(udp->dict1.dict2.userdef->base->integer, ==, 42);
check_and_free_str(udp->dict1.dict2.userdef->string, "string");
check_and_free_str(udp->dict1.dict2.string, "string2");
g_assert(udp->dict1.has_dict3 == false);

g_free(udp->dict1.dict2.userdef1);
g_free(udp->dict1.dict2.userdef);
g_free(udp);
}

Expand Down
48 changes: 24 additions & 24 deletions tests/test-qmp-output-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
{
int64_t value = 42;
Error *err = NULL;
UserDefNested *ud2;
UserDefTwo *ud2;
QObject *obj;
QDict *qdict, *dict1, *dict2, *dict3, *userdef;
const char *string = "user def string";
Expand All @@ -245,20 +245,20 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
ud2->string0 = g_strdup(strings[0]);

ud2->dict1.string1 = g_strdup(strings[1]);
ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
ud2->dict1.dict2.userdef1->string = g_strdup(string);
ud2->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
ud2->dict1.dict2.userdef1->base->integer = value;
ud2->dict1.dict2.string2 = g_strdup(strings[2]);
ud2->dict1.dict2.userdef = g_new0(UserDefOne, 1);
ud2->dict1.dict2.userdef->string = g_strdup(string);
ud2->dict1.dict2.userdef->base = g_new0(UserDefZero, 1);
ud2->dict1.dict2.userdef->base->integer = value;
ud2->dict1.dict2.string = g_strdup(strings[2]);

ud2->dict1.has_dict3 = true;
ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
ud2->dict1.dict3.userdef2->string = g_strdup(string);
ud2->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);
ud2->dict1.dict3.userdef2->base->integer = value;
ud2->dict1.dict3.string3 = g_strdup(strings[3]);
ud2->dict1.dict3.userdef = g_new0(UserDefOne, 1);
ud2->dict1.dict3.userdef->string = g_strdup(string);
ud2->dict1.dict3.userdef->base = g_new0(UserDefZero, 1);
ud2->dict1.dict3.userdef->base->integer = value;
ud2->dict1.dict3.string = g_strdup(strings[3]);

visit_type_UserDefNested(data->ov, &ud2, "unused", &err);
visit_type_UserDefTwo(data->ov, &ud2, "unused", &err);
g_assert(!err);

obj = qmp_output_get_qobject(data->qov);
Expand All @@ -275,22 +275,22 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,

dict2 = qdict_get_qdict(dict1, "dict2");
g_assert_cmpint(qdict_size(dict2), ==, 2);
g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
userdef = qdict_get_qdict(dict2, "userdef1");
g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
userdef = qdict_get_qdict(dict2, "userdef");
g_assert_cmpint(qdict_size(userdef), ==, 2);
g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

dict3 = qdict_get_qdict(dict1, "dict3");
g_assert_cmpint(qdict_size(dict3), ==, 2);
g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
userdef = qdict_get_qdict(dict3, "userdef2");
g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
userdef = qdict_get_qdict(dict3, "userdef");
g_assert_cmpint(qdict_size(userdef), ==, 2);
g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

QDECREF(qdict);
qapi_free_UserDefNested(ud2);
qapi_free_UserDefTwo(ud2);
}

static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
Expand Down Expand Up @@ -398,7 +398,7 @@ static void test_visitor_out_list(TestOutputVisitorData *data,
static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
const void *unused)
{
UserDefNestedList *p, *head = NULL;
UserDefTwoList *p, *head = NULL;
const char string[] = "foo bar";
int i, max_count = 1024;

Expand All @@ -408,18 +408,18 @@ static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,

p->value->string0 = g_strdup(string);
p->value->dict1.string1 = g_strdup(string);
p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
p->value->dict1.dict2.userdef1->string = g_strdup(string);
p->value->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
p->value->dict1.dict2.userdef1->base->integer = 42;
p->value->dict1.dict2.string2 = g_strdup(string);
p->value->dict1.dict2.userdef = g_new0(UserDefOne, 1);
p->value->dict1.dict2.userdef->string = g_strdup(string);
p->value->dict1.dict2.userdef->base = g_new0(UserDefZero, 1);
p->value->dict1.dict2.userdef->base->integer = 42;
p->value->dict1.dict2.string = g_strdup(string);
p->value->dict1.has_dict3 = false;

p->next = head;
head = p;
}

qapi_free_UserDefNestedList(head);
qapi_free_UserDefTwoList(head);
}

static void test_visitor_out_union_flat(TestOutputVisitorData *data,
Expand Down
Loading

0 comments on commit b6fcf32

Please sign in to comment.