-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qapi: add tests for string-based visitors
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
- Loading branch information
Showing
4 changed files
with
395 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* String Input Visitor unit-tests. | ||
* | ||
* Copyright (C) 2012 Red Hat Inc. | ||
* | ||
* Authors: | ||
* Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-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> | ||
#include <stdarg.h> | ||
|
||
#include "qapi/string-input-visitor.h" | ||
#include "test-qapi-types.h" | ||
#include "test-qapi-visit.h" | ||
#include "qemu-objects.h" | ||
|
||
typedef struct TestInputVisitorData { | ||
StringInputVisitor *siv; | ||
} TestInputVisitorData; | ||
|
||
static void visitor_input_teardown(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
if (data->siv) { | ||
string_input_visitor_cleanup(data->siv); | ||
data->siv = NULL; | ||
} | ||
} | ||
|
||
/* This is provided instead of a test setup function so that the JSON | ||
string used by the tests are kept in the test functions (and not | ||
int main()) */ | ||
static | ||
Visitor *visitor_input_test_init(TestInputVisitorData *data, | ||
const char *string) | ||
{ | ||
Visitor *v; | ||
|
||
data->siv = string_input_visitor_new(string); | ||
g_assert(data->siv != NULL); | ||
|
||
v = string_input_get_visitor(data->siv); | ||
g_assert(v != NULL); | ||
|
||
return v; | ||
} | ||
|
||
static void test_visitor_in_int(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
int64_t res = 0, value = -42; | ||
Error *errp = NULL; | ||
Visitor *v; | ||
|
||
v = visitor_input_test_init(data, "-42"); | ||
|
||
visit_type_int(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, value); | ||
} | ||
|
||
static void test_visitor_in_bool(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
Error *errp = NULL; | ||
bool res = false; | ||
Visitor *v; | ||
|
||
v = visitor_input_test_init(data, "true"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, true); | ||
visitor_input_teardown(data, unused); | ||
|
||
v = visitor_input_test_init(data, "yes"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, true); | ||
visitor_input_teardown(data, unused); | ||
|
||
v = visitor_input_test_init(data, "on"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, true); | ||
visitor_input_teardown(data, unused); | ||
|
||
v = visitor_input_test_init(data, "false"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, false); | ||
visitor_input_teardown(data, unused); | ||
|
||
v = visitor_input_test_init(data, "no"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, false); | ||
visitor_input_teardown(data, unused); | ||
|
||
v = visitor_input_test_init(data, "off"); | ||
|
||
visit_type_bool(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(res, ==, false); | ||
} | ||
|
||
static void test_visitor_in_number(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
double res = 0, value = 3.14; | ||
Error *errp = NULL; | ||
Visitor *v; | ||
|
||
v = visitor_input_test_init(data, "3.14"); | ||
|
||
visit_type_number(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpfloat(res, ==, value); | ||
} | ||
|
||
static void test_visitor_in_string(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
char *res = NULL, *value = (char *) "Q E M U"; | ||
Error *errp = NULL; | ||
Visitor *v; | ||
|
||
v = visitor_input_test_init(data, value); | ||
|
||
visit_type_str(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpstr(res, ==, value); | ||
|
||
g_free(res); | ||
} | ||
|
||
static void test_visitor_in_enum(TestInputVisitorData *data, | ||
const void *unused) | ||
{ | ||
Error *errp = NULL; | ||
Visitor *v; | ||
EnumOne i; | ||
|
||
for (i = 0; EnumOne_lookup[i]; i++) { | ||
EnumOne res = -1; | ||
|
||
v = visitor_input_test_init(data, EnumOne_lookup[i]); | ||
|
||
visit_type_EnumOne(v, &res, NULL, &errp); | ||
g_assert(!error_is_set(&errp)); | ||
g_assert_cmpint(i, ==, res); | ||
|
||
visitor_input_teardown(data, NULL); | ||
} | ||
|
||
data->siv = NULL; | ||
} | ||
|
||
static void input_visitor_test_add(const char *testpath, | ||
TestInputVisitorData *data, | ||
void (*test_func)(TestInputVisitorData *data, const void *user_data)) | ||
{ | ||
g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | ||
visitor_input_teardown); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
TestInputVisitorData in_visitor_data; | ||
|
||
g_test_init(&argc, &argv, NULL); | ||
|
||
input_visitor_test_add("/string-visitor/input/int", | ||
&in_visitor_data, test_visitor_in_int); | ||
input_visitor_test_add("/string-visitor/input/bool", | ||
&in_visitor_data, test_visitor_in_bool); | ||
input_visitor_test_add("/string-visitor/input/number", | ||
&in_visitor_data, test_visitor_in_number); | ||
input_visitor_test_add("/string-visitor/input/string", | ||
&in_visitor_data, test_visitor_in_string); | ||
input_visitor_test_add("/string-visitor/input/enum", | ||
&in_visitor_data, test_visitor_in_enum); | ||
|
||
g_test_run(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* 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> | ||
|
||
#include "qapi/string-output-visitor.h" | ||
#include "test-qapi-types.h" | ||
#include "test-qapi-visit.h" | ||
#include "qemu-objects.h" | ||
|
||
typedef struct TestOutputVisitorData { | ||
StringOutputVisitor *sov; | ||
Visitor *ov; | ||
} TestOutputVisitorData; | ||
|
||
static void visitor_output_setup(TestOutputVisitorData *data, | ||
const void *unused) | ||
{ | ||
data->sov = string_output_visitor_new(); | ||
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; | ||
Error *errp = NULL; | ||
char *str; | ||
|
||
visit_type_int(data->ov, &value, NULL, &errp); | ||
g_assert(error_is_set(&errp) == 0); | ||
|
||
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) | ||
{ | ||
Error *errp = NULL; | ||
bool value = true; | ||
char *str; | ||
|
||
visit_type_bool(data->ov, &value, NULL, &errp); | ||
g_assert(error_is_set(&errp) == 0); | ||
|
||
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; | ||
Error *errp = NULL; | ||
char *str; | ||
|
||
visit_type_number(data->ov, &value, NULL, &errp); | ||
g_assert(error_is_set(&errp) == 0); | ||
|
||
str = string_output_get_string(data->sov); | ||
g_assert(str != NULL); | ||
g_assert_cmpstr(str, ==, "3.14"); | ||
g_free(str); | ||
} | ||
|
||
static void test_visitor_out_string(TestOutputVisitorData *data, | ||
const void *unused) | ||
{ | ||
char *string = (char *) "Q E M U"; | ||
Error *errp = NULL; | ||
char *str; | ||
|
||
visit_type_str(data->ov, &string, NULL, &errp); | ||
g_assert(error_is_set(&errp) == 0); | ||
|
||
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; | ||
Error *errp = NULL; | ||
char *str; | ||
|
||
/* A null string should return "" */ | ||
visit_type_str(data->ov, &string, NULL, &errp); | ||
g_assert(error_is_set(&errp) == 0); | ||
|
||
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) | ||
{ | ||
Error *errp = NULL; | ||
char *str; | ||
EnumOne i; | ||
|
||
for (i = 0; i < ENUM_ONE_MAX; i++) { | ||
visit_type_EnumOne(data->ov, &i, "unused", &errp); | ||
g_assert(!error_is_set(&errp)); | ||
|
||
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 }; | ||
Error *errp; | ||
|
||
for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { | ||
errp = NULL; | ||
visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp); | ||
g_assert(error_is_set(&errp) == true); | ||
error_free(errp); | ||
} | ||
} | ||
|
||
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; | ||
} |
Oops, something went wrong.