2012-02-09 11:21:03 +01:00
|
|
|
/*
|
|
|
|
* String Output Visitor unit-tests.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2012-12-06 11:22:34 +01:00
|
|
|
#include "qemu-common.h"
|
2012-02-09 11:21:03 +01:00
|
|
|
#include "qapi/string-output-visitor.h"
|
|
|
|
#include "test-qapi-types.h"
|
|
|
|
#include "test-qapi-visit.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/types.h"
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
typedef struct TestOutputVisitorData {
|
|
|
|
StringOutputVisitor *sov;
|
|
|
|
Visitor *ov;
|
2014-06-20 07:55:43 +02:00
|
|
|
bool human;
|
2012-02-09 11:21:03 +01:00
|
|
|
} TestOutputVisitorData;
|
|
|
|
|
|
|
|
static void visitor_output_setup(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
2014-06-20 07:55:43 +02:00
|
|
|
data->human = false;
|
|
|
|
data->sov = string_output_visitor_new(data->human);
|
|
|
|
g_assert(data->sov != NULL);
|
|
|
|
|
|
|
|
data->ov = string_output_get_visitor(data->sov);
|
|
|
|
g_assert(data->ov != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void visitor_output_setup_human(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
data->human = true;
|
|
|
|
data->sov = string_output_visitor_new(data->human);
|
2012-02-09 11:21:03 +01:00
|
|
|
g_assert(data->sov != NULL);
|
|
|
|
|
|
|
|
data->ov = string_output_get_visitor(data->sov);
|
|
|
|
g_assert(data->ov != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void visitor_output_teardown(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
string_output_visitor_cleanup(data->sov);
|
|
|
|
data->sov = NULL;
|
|
|
|
data->ov = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_int(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
2014-06-10 13:15:28 +02:00
|
|
|
int64_t value = 42;
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
char *str;
|
|
|
|
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_int(data->ov, &value, NULL, &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2014-06-20 07:55:43 +02:00
|
|
|
if (data->human) {
|
|
|
|
g_assert_cmpstr(str, ==, "42 (0x2a)");
|
|
|
|
} else {
|
|
|
|
g_assert_cmpstr(str, ==, "42");
|
|
|
|
}
|
2012-02-09 11:21:03 +01:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:15:28 +02:00
|
|
|
static void test_visitor_out_intList(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
|
|
|
|
3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
|
|
|
|
intList *list = NULL, **tmp = &list;
|
|
|
|
int i;
|
2015-12-18 16:35:27 +01:00
|
|
|
Error *err = NULL;
|
2014-06-10 13:15:28 +02:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
|
|
|
|
*tmp = g_malloc0(sizeof(**tmp));
|
|
|
|
(*tmp)->value = value[i];
|
|
|
|
tmp = &(*tmp)->next;
|
|
|
|
}
|
|
|
|
|
2015-12-18 16:35:27 +01:00
|
|
|
visit_type_intList(data->ov, &list, NULL, &err);
|
|
|
|
g_assert(err == NULL);
|
2014-06-10 13:15:28 +02:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2014-06-20 07:55:43 +02:00
|
|
|
if (data->human) {
|
|
|
|
g_assert_cmpstr(str, ==,
|
|
|
|
"0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
|
|
|
|
"(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
|
|
|
|
"0x7ffffffffffffffe-0x7fffffffffffffff)");
|
|
|
|
} else {
|
|
|
|
g_assert_cmpstr(str, ==,
|
|
|
|
"0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
|
|
|
|
}
|
2014-06-10 13:15:28 +02:00
|
|
|
g_free(str);
|
|
|
|
while (list) {
|
|
|
|
intList *tmp2;
|
|
|
|
tmp2 = list->next;
|
|
|
|
g_free(list);
|
|
|
|
list = tmp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 11:21:03 +01:00
|
|
|
static void test_visitor_out_bool(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
bool value = true;
|
|
|
|
char *str;
|
|
|
|
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_bool(data->ov, &value, NULL, &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
|
|
|
g_assert_cmpstr(str, ==, "true");
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_number(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
double value = 3.14;
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
char *str;
|
|
|
|
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_number(data->ov, &value, NULL, &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2012-04-30 16:33:30 +02:00
|
|
|
g_assert_cmpstr(str, ==, "3.140000");
|
2012-02-09 11:21:03 +01:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_string(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
char *string = (char *) "Q E M U";
|
2014-06-20 07:55:43 +02:00
|
|
|
const char *string_human = "\"Q E M U\"";
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
char *str;
|
|
|
|
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_str(data->ov, &string, NULL, &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2014-06-20 07:55:43 +02:00
|
|
|
if (data->human) {
|
|
|
|
g_assert_cmpstr(str, ==, string_human);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpstr(str, ==, string);
|
|
|
|
}
|
2012-02-09 11:21:03 +01:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_no_string(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
char *string = NULL;
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* A null string should return "" */
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_str(data->ov, &string, NULL, &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2014-06-20 07:55:43 +02:00
|
|
|
if (data->human) {
|
|
|
|
g_assert_cmpstr(str, ==, "<null>");
|
|
|
|
} else {
|
|
|
|
g_assert_cmpstr(str, ==, "");
|
|
|
|
}
|
2012-02-09 11:21:03 +01:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_enum(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err = NULL;
|
2012-02-09 11:21:03 +01:00
|
|
|
char *str;
|
|
|
|
EnumOne i;
|
|
|
|
|
qapi: Don't let implicit enum MAX member collide
Now that we guarantee the user doesn't have any enum values
beginning with a single underscore, we can use that for our
own purposes. Renaming ENUM_MAX to ENUM__MAX makes it obvious
that the sentinel is generated.
This patch was mostly generated by applying a temporary patch:
|diff --git a/scripts/qapi.py b/scripts/qapi.py
|index e6d014b..b862ec9 100644
|--- a/scripts/qapi.py
|+++ b/scripts/qapi.py
|@@ -1570,6 +1570,7 @@ const char *const %(c_name)s_lookup[] = {
| max_index = c_enum_const(name, 'MAX', prefix)
| ret += mcgen('''
| [%(max_index)s] = NULL,
|+// %(max_index)s
| };
| ''',
| max_index=max_index)
then running:
$ cat qapi-{types,event}.c tests/test-qapi-types.c |
sed -n 's,^// \(.*\)MAX,s|\1MAX|\1_MAX|g,p' > list
$ git grep -l _MAX | xargs sed -i -f list
The only things not generated are the changes in scripts/qapi.py.
Rejecting enum members named 'MAX' is now useless, and will be dropped
in the next patch.
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1447836791-369-23-git-send-email-eblake@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
[Rebased to current master, commit message tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-11-18 09:52:57 +01:00
|
|
|
for (i = 0; i < ENUM_ONE__MAX; i++) {
|
2014-06-20 07:55:43 +02:00
|
|
|
char *str_human;
|
|
|
|
|
2014-05-02 13:26:29 +02:00
|
|
|
visit_type_EnumOne(data->ov, &i, "unused", &err);
|
|
|
|
g_assert(!err);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
2014-07-09 22:28:49 +02:00
|
|
|
str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
|
2014-06-20 07:55:43 +02:00
|
|
|
|
2012-02-09 11:21:03 +01:00
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
2014-06-20 07:55:43 +02:00
|
|
|
if (data->human) {
|
|
|
|
g_assert_cmpstr(str, ==, str_human);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
|
|
|
|
}
|
|
|
|
g_free(str_human);
|
2012-02-09 11:21:03 +01:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
qapi: Don't let implicit enum MAX member collide
Now that we guarantee the user doesn't have any enum values
beginning with a single underscore, we can use that for our
own purposes. Renaming ENUM_MAX to ENUM__MAX makes it obvious
that the sentinel is generated.
This patch was mostly generated by applying a temporary patch:
|diff --git a/scripts/qapi.py b/scripts/qapi.py
|index e6d014b..b862ec9 100644
|--- a/scripts/qapi.py
|+++ b/scripts/qapi.py
|@@ -1570,6 +1570,7 @@ const char *const %(c_name)s_lookup[] = {
| max_index = c_enum_const(name, 'MAX', prefix)
| ret += mcgen('''
| [%(max_index)s] = NULL,
|+// %(max_index)s
| };
| ''',
| max_index=max_index)
then running:
$ cat qapi-{types,event}.c tests/test-qapi-types.c |
sed -n 's,^// \(.*\)MAX,s|\1MAX|\1_MAX|g,p' > list
$ git grep -l _MAX | xargs sed -i -f list
The only things not generated are the changes in scripts/qapi.py.
Rejecting enum members named 'MAX' is now useless, and will be dropped
in the next patch.
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1447836791-369-23-git-send-email-eblake@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
[Rebased to current master, commit message tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-11-18 09:52:57 +01:00
|
|
|
EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
|
2014-05-02 13:26:29 +02:00
|
|
|
Error *err;
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
|
2014-05-02 13:26:29 +02:00
|
|
|
err = NULL;
|
|
|
|
visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
|
|
|
|
g_assert(err);
|
|
|
|
error_free(err);
|
2012-02-09 11:21:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 07:55:43 +02:00
|
|
|
static void
|
|
|
|
output_visitor_test_add(const char *testpath,
|
|
|
|
TestOutputVisitorData *data,
|
|
|
|
void (*test_func)(TestOutputVisitorData *data,
|
|
|
|
const void *user_data),
|
|
|
|
bool human)
|
2012-02-09 11:21:03 +01:00
|
|
|
{
|
2014-06-20 07:55:43 +02:00
|
|
|
g_test_add(testpath, TestOutputVisitorData, data,
|
|
|
|
human ? visitor_output_setup_human : visitor_output_setup,
|
2012-02-09 11:21:03 +01:00
|
|
|
test_func, visitor_output_teardown);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
TestOutputVisitorData out_visitor_data;
|
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
output_visitor_test_add("/string-visitor/output/int",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_int, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/int-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_int, true);
|
|
|
|
output_visitor_test_add("/string-visitor/output/bool",
|
|
|
|
&out_visitor_data, test_visitor_out_bool, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/bool-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_bool, true);
|
|
|
|
output_visitor_test_add("/string-visitor/output/number",
|
|
|
|
&out_visitor_data, test_visitor_out_number, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/number-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_number, true);
|
2012-02-09 11:21:03 +01:00
|
|
|
output_visitor_test_add("/string-visitor/output/string",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_string, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/string-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_string, true);
|
|
|
|
output_visitor_test_add("/string-visitor/output/no-string",
|
|
|
|
&out_visitor_data, test_visitor_out_no_string,
|
|
|
|
false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/no-string-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_no_string,
|
|
|
|
true);
|
|
|
|
output_visitor_test_add("/string-visitor/output/enum",
|
|
|
|
&out_visitor_data, test_visitor_out_enum, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/enum-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_enum, true);
|
2012-02-09 11:21:03 +01:00
|
|
|
output_visitor_test_add("/string-visitor/output/enum-errors",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_enum_errors,
|
|
|
|
false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/enum-errors-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_enum_errors,
|
|
|
|
true);
|
|
|
|
output_visitor_test_add("/string-visitor/output/intList",
|
|
|
|
&out_visitor_data, test_visitor_out_intList, false);
|
2015-10-05 13:04:20 +02:00
|
|
|
output_visitor_test_add("/string-visitor/output/intList-human",
|
2014-06-20 07:55:43 +02:00
|
|
|
&out_visitor_data, test_visitor_out_intList, true);
|
2012-02-09 11:21:03 +01:00
|
|
|
|
|
|
|
g_test_run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|