test-qobject-output-visitor: Wean off UserDefListUnion

The test_visitor_out_list_union_FOO() use simple union
UserDefListUnion to cover lists of builtin types.  Rewrite as
test_visitor_out_list_struct(), using struct ArrayStruct and a lot
less code.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20210917143134.412106-14-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2021-09-17 16:31:24 +02:00
parent e7a76fe25a
commit 00e6832f41
1 changed files with 93 additions and 298 deletions

View File

@ -437,289 +437,118 @@ static void test_visitor_out_null(TestOutputVisitorData *data,
g_assert(qobject_type(nil) == QTYPE_QNULL);
}
static void init_list_union(UserDefListUnion *cvalue)
{
int i;
switch (cvalue->type) {
case USER_DEF_LIST_UNION_KIND_INTEGER: {
intList **tail = &cvalue->u.integer.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_S8: {
int8List **tail = &cvalue->u.s8.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_S16: {
int16List **tail = &cvalue->u.s16.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_S32: {
int32List **tail = &cvalue->u.s32.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_S64: {
int64List **tail = &cvalue->u.s64.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_U8: {
uint8List **tail = &cvalue->u.u8.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_U16: {
uint16List **tail = &cvalue->u.u16.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_U32: {
uint32List **tail = &cvalue->u.u32.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_U64: {
uint64List **tail = &cvalue->u.u64.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, i);
}
break;
}
case USER_DEF_LIST_UNION_KIND_BOOLEAN: {
boolList **tail = &cvalue->u.boolean.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, QEMU_IS_ALIGNED(i, 3));
}
break;
}
case USER_DEF_LIST_UNION_KIND_STRING: {
strList **tail = &cvalue->u.string.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, g_strdup_printf("%d", i));
}
break;
}
case USER_DEF_LIST_UNION_KIND_NUMBER: {
numberList **tail = &cvalue->u.number.data;
for (i = 0; i < 32; i++) {
QAPI_LIST_APPEND(tail, (double)i / 3);
}
break;
}
default:
g_assert_not_reached();
}
}
static void check_list_union(QObject *qobj,
UserDefListUnionKind kind)
static void test_visitor_out_list_struct(TestOutputVisitorData *data,
const void *unused)
{
const char *int_member[] = {
"integer", "s8", "s16", "s32", "s64", "u8", "u16", "u32", "u64" };
g_autoptr(ArrayStruct) arrs = g_new0(ArrayStruct, 1);
int i, j;
QDict *qdict;
QList *qlist;
int i;
QListEntry *e;
qdict = qobject_to(QDict, qobj);
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->integer, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->s8, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->s16, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->s32, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->s64, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->u8, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->u16, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->u32, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->u64, i);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->number, (double)i / 3);
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->boolean, QEMU_IS_ALIGNED(i, 3));
}
for (i = 31; i >= 0; i--) {
QAPI_LIST_PREPEND(arrs->string, g_strdup_printf("%d", i));
}
visit_type_ArrayStruct(data->ov, NULL, &arrs, &error_abort);
qdict = qobject_to(QDict, visitor_get(data));
g_assert(qdict);
g_assert(qdict_haskey(qdict, "data"));
qlist = qlist_copy(qobject_to(QList, qdict_get(qdict, "data")));
switch (kind) {
case USER_DEF_LIST_UNION_KIND_U8:
case USER_DEF_LIST_UNION_KIND_U16:
case USER_DEF_LIST_UNION_KIND_U32:
case USER_DEF_LIST_UNION_KIND_U64:
for (i = 0; i < 32; i++) {
QObject *tmp;
QNum *qvalue;
uint64_t val;
tmp = qlist_peek(qlist);
g_assert(tmp);
qvalue = qobject_to(QNum, tmp);
g_assert(qnum_get_try_uint(qvalue, &val));
g_assert_cmpint(val, ==, i);
qobject_unref(qlist_pop(qlist));
for (i = 0; i < G_N_ELEMENTS(int_member); i++) {
qlist = qdict_get_qlist(qdict, int_member[i]);
g_assert(qlist);
j = 0;
QLIST_FOREACH_ENTRY(qlist, e) {
QNum *qvalue = qobject_to(QNum, qlist_entry_obj(e));
g_assert(qvalue);
g_assert_cmpint(qnum_get_int(qvalue), ==, j);
j++;
}
break;
case USER_DEF_LIST_UNION_KIND_S8:
case USER_DEF_LIST_UNION_KIND_S16:
case USER_DEF_LIST_UNION_KIND_S32:
case USER_DEF_LIST_UNION_KIND_S64:
/*
* All integer elements in JSON arrays get stored into QNums
* when we convert to QObjects, so we can check them all in
* the same fashion, so simply fall through here.
*/
case USER_DEF_LIST_UNION_KIND_INTEGER:
for (i = 0; i < 32; i++) {
QObject *tmp;
QNum *qvalue;
int64_t val;
tmp = qlist_peek(qlist);
g_assert(tmp);
qvalue = qobject_to(QNum, tmp);
g_assert(qnum_get_try_int(qvalue, &val));
g_assert_cmpint(val, ==, i);
qobject_unref(qlist_pop(qlist));
}
break;
case USER_DEF_LIST_UNION_KIND_BOOLEAN:
for (i = 0; i < 32; i++) {
QObject *tmp;
QBool *qvalue;
tmp = qlist_peek(qlist);
g_assert(tmp);
qvalue = qobject_to(QBool, tmp);
qlist = qdict_get_qlist(qdict, "number");
g_assert(qlist);
i = 0;
QLIST_FOREACH_ENTRY(qlist, e) {
QNum *qvalue = qobject_to(QNum, qlist_entry_obj(e));
char expected[32], actual[32];
g_assert(qvalue);
sprintf(expected, "%.6f", (double)i / 3);
sprintf(actual, "%.6f", qnum_get_double(qvalue));
g_assert_cmpstr(actual, ==, expected);
i++;
}
qlist = qdict_get_qlist(qdict, "boolean");
g_assert(qlist);
i = 0;
QLIST_FOREACH_ENTRY(qlist, e) {
QBool *qvalue = qobject_to(QBool, qlist_entry_obj(e));
g_assert(qvalue);
g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
qobject_unref(qlist_pop(qlist));
i++;
}
break;
case USER_DEF_LIST_UNION_KIND_STRING:
for (i = 0; i < 32; i++) {
QObject *tmp;
QString *qvalue;
gchar str[8];
tmp = qlist_peek(qlist);
g_assert(tmp);
qvalue = qobject_to(QString, tmp);
sprintf(str, "%d", i);
g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
qobject_unref(qlist_pop(qlist));
qlist = qdict_get_qlist(qdict, "string");
g_assert(qlist);
i = 0;
QLIST_FOREACH_ENTRY(qlist, e) {
QString *qvalue = qobject_to(QString, qlist_entry_obj(e));
char expected[32];
g_assert(qvalue);
sprintf(expected, "%d", i);
g_assert_cmpstr(qstring_get_str(qvalue), ==, expected);
i++;
}
break;
case USER_DEF_LIST_UNION_KIND_NUMBER:
for (i = 0; i < 32; i++) {
QObject *tmp;
QNum *qvalue;
GString *double_expected = g_string_new("");
GString *double_actual = g_string_new("");
tmp = qlist_peek(qlist);
g_assert(tmp);
qvalue = qobject_to(QNum, tmp);
g_string_printf(double_expected, "%.6f", (double)i / 3);
g_string_printf(double_actual, "%.6f", qnum_get_double(qvalue));
g_assert_cmpstr(double_actual->str, ==, double_expected->str);
qobject_unref(qlist_pop(qlist));
g_string_free(double_expected, true);
g_string_free(double_actual, true);
}
break;
default:
g_assert_not_reached();
}
qobject_unref(qlist);
}
static void test_list_union(TestOutputVisitorData *data,
const void *unused,
UserDefListUnionKind kind)
{
UserDefListUnion *cvalue = g_new0(UserDefListUnion, 1);
QObject *obj;
cvalue->type = kind;
init_list_union(cvalue);
visit_type_UserDefListUnion(data->ov, NULL, &cvalue, &error_abort);
obj = visitor_get(data);
check_list_union(obj, cvalue->type);
qapi_free_UserDefListUnion(cvalue);
}
static void test_visitor_out_list_union_int(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_INTEGER);
}
static void test_visitor_out_list_union_int8(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_S8);
}
static void test_visitor_out_list_union_int16(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_S16);
}
static void test_visitor_out_list_union_int32(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_S32);
}
static void test_visitor_out_list_union_int64(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_S64);
}
static void test_visitor_out_list_union_uint8(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_U8);
}
static void test_visitor_out_list_union_uint16(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_U16);
}
static void test_visitor_out_list_union_uint32(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_U32);
}
static void test_visitor_out_list_union_uint64(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_U64);
}
static void test_visitor_out_list_union_bool(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_BOOLEAN);
}
static void test_visitor_out_list_union_str(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_STRING);
}
static void test_visitor_out_list_union_number(TestOutputVisitorData *data,
const void *unused)
{
test_list_union(data, unused, USER_DEF_LIST_UNION_KIND_NUMBER);
}
static void output_visitor_test_add(const char *testpath,
@ -764,42 +593,8 @@ int main(int argc, char **argv)
&out_visitor_data, test_visitor_out_alternate);
output_visitor_test_add("/visitor/output/null",
&out_visitor_data, test_visitor_out_null);
output_visitor_test_add("/visitor/output/list_union/int",
&out_visitor_data,
test_visitor_out_list_union_int);
output_visitor_test_add("/visitor/output/list_union/int8",
&out_visitor_data,
test_visitor_out_list_union_int8);
output_visitor_test_add("/visitor/output/list_union/int16",
&out_visitor_data,
test_visitor_out_list_union_int16);
output_visitor_test_add("/visitor/output/list_union/int32",
&out_visitor_data,
test_visitor_out_list_union_int32);
output_visitor_test_add("/visitor/output/list_union/int64",
&out_visitor_data,
test_visitor_out_list_union_int64);
output_visitor_test_add("/visitor/output/list_union/uint8",
&out_visitor_data,
test_visitor_out_list_union_uint8);
output_visitor_test_add("/visitor/output/list_union/uint16",
&out_visitor_data,
test_visitor_out_list_union_uint16);
output_visitor_test_add("/visitor/output/list_union/uint32",
&out_visitor_data,
test_visitor_out_list_union_uint32);
output_visitor_test_add("/visitor/output/list_union/uint64",
&out_visitor_data,
test_visitor_out_list_union_uint64);
output_visitor_test_add("/visitor/output/list_union/bool",
&out_visitor_data,
test_visitor_out_list_union_bool);
output_visitor_test_add("/visitor/output/list_union/string",
&out_visitor_data,
test_visitor_out_list_union_str);
output_visitor_test_add("/visitor/output/list_union/number",
&out_visitor_data,
test_visitor_out_list_union_number);
output_visitor_test_add("/visitor/output/list_struct",
&out_visitor_data, test_visitor_out_list_struct);
g_test_run();