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;
|
|
|
|
} TestOutputVisitorData;
|
|
|
|
|
|
|
|
static void visitor_output_setup(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
2014-02-08 11:01:50 +01:00
|
|
|
data->sov = string_output_visitor_new(false);
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
g_assert_cmpstr(str, ==, "-42");
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
g_assert_cmpstr(str, ==, string);
|
|
|
|
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);
|
|
|
|
g_assert_cmpstr(str, ==, "");
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < ENUM_ONE_MAX; i++) {
|
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
|
|
|
|
|
|
|
str = string_output_get_string(data->sov);
|
|
|
|
g_assert(str != NULL);
|
|
|
|
g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
|
|
|
|
const void *unused)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_visitor_test_add(const char *testpath,
|
|
|
|
TestOutputVisitorData *data,
|
|
|
|
void (*test_func)(TestOutputVisitorData *data, const void *user_data))
|
|
|
|
{
|
|
|
|
g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
|
|
|
|
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",
|
|
|
|
&out_visitor_data, test_visitor_out_int);
|
|
|
|
output_visitor_test_add("/string-visitor/output/bool",
|
|
|
|
&out_visitor_data, test_visitor_out_bool);
|
|
|
|
output_visitor_test_add("/string-visitor/output/number",
|
|
|
|
&out_visitor_data, test_visitor_out_number);
|
|
|
|
output_visitor_test_add("/string-visitor/output/string",
|
|
|
|
&out_visitor_data, test_visitor_out_string);
|
|
|
|
output_visitor_test_add("/string-visitor/output/no-string",
|
|
|
|
&out_visitor_data, test_visitor_out_no_string);
|
|
|
|
output_visitor_test_add("/string-visitor/output/enum",
|
|
|
|
&out_visitor_data, test_visitor_out_enum);
|
|
|
|
output_visitor_test_add("/string-visitor/output/enum-errors",
|
|
|
|
&out_visitor_data, test_visitor_out_enum_errors);
|
|
|
|
|
|
|
|
g_test_run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|