Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * String Output Visitor unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor) |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include <glib.h> |
| 14 | |
| 15 | #include "qapi/string-output-visitor.h" |
| 16 | #include "test-qapi-types.h" |
| 17 | #include "test-qapi-visit.h" |
| 18 | #include "qemu-objects.h" |
| 19 | |
| 20 | typedef struct TestOutputVisitorData { |
| 21 | StringOutputVisitor *sov; |
| 22 | Visitor *ov; |
| 23 | } TestOutputVisitorData; |
| 24 | |
| 25 | static void visitor_output_setup(TestOutputVisitorData *data, |
| 26 | const void *unused) |
| 27 | { |
| 28 | data->sov = string_output_visitor_new(); |
| 29 | g_assert(data->sov != NULL); |
| 30 | |
| 31 | data->ov = string_output_get_visitor(data->sov); |
| 32 | g_assert(data->ov != NULL); |
| 33 | } |
| 34 | |
| 35 | static void visitor_output_teardown(TestOutputVisitorData *data, |
| 36 | const void *unused) |
| 37 | { |
| 38 | string_output_visitor_cleanup(data->sov); |
| 39 | data->sov = NULL; |
| 40 | data->ov = NULL; |
| 41 | } |
| 42 | |
| 43 | static void test_visitor_out_int(TestOutputVisitorData *data, |
| 44 | const void *unused) |
| 45 | { |
| 46 | int64_t value = -42; |
| 47 | Error *errp = NULL; |
| 48 | char *str; |
| 49 | |
| 50 | visit_type_int(data->ov, &value, NULL, &errp); |
| 51 | g_assert(error_is_set(&errp) == 0); |
| 52 | |
| 53 | str = string_output_get_string(data->sov); |
| 54 | g_assert(str != NULL); |
| 55 | g_assert_cmpstr(str, ==, "-42"); |
| 56 | g_free(str); |
| 57 | } |
| 58 | |
| 59 | static void test_visitor_out_bool(TestOutputVisitorData *data, |
| 60 | const void *unused) |
| 61 | { |
| 62 | Error *errp = NULL; |
| 63 | bool value = true; |
| 64 | char *str; |
| 65 | |
| 66 | visit_type_bool(data->ov, &value, NULL, &errp); |
| 67 | g_assert(error_is_set(&errp) == 0); |
| 68 | |
| 69 | str = string_output_get_string(data->sov); |
| 70 | g_assert(str != NULL); |
| 71 | g_assert_cmpstr(str, ==, "true"); |
| 72 | g_free(str); |
| 73 | } |
| 74 | |
| 75 | static void test_visitor_out_number(TestOutputVisitorData *data, |
| 76 | const void *unused) |
| 77 | { |
| 78 | double value = 3.14; |
| 79 | Error *errp = NULL; |
| 80 | char *str; |
| 81 | |
| 82 | visit_type_number(data->ov, &value, NULL, &errp); |
| 83 | g_assert(error_is_set(&errp) == 0); |
| 84 | |
| 85 | str = string_output_get_string(data->sov); |
| 86 | g_assert(str != NULL); |
Michael Roth | 173bbb7 | 2012-04-30 09:33:30 -0500 | [diff] [blame] | 87 | g_assert_cmpstr(str, ==, "3.140000"); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 88 | g_free(str); |
| 89 | } |
| 90 | |
| 91 | static void test_visitor_out_string(TestOutputVisitorData *data, |
| 92 | const void *unused) |
| 93 | { |
| 94 | char *string = (char *) "Q E M U"; |
| 95 | Error *errp = NULL; |
| 96 | char *str; |
| 97 | |
| 98 | visit_type_str(data->ov, &string, NULL, &errp); |
| 99 | g_assert(error_is_set(&errp) == 0); |
| 100 | |
| 101 | str = string_output_get_string(data->sov); |
| 102 | g_assert(str != NULL); |
| 103 | g_assert_cmpstr(str, ==, string); |
| 104 | g_free(str); |
| 105 | } |
| 106 | |
| 107 | static void test_visitor_out_no_string(TestOutputVisitorData *data, |
| 108 | const void *unused) |
| 109 | { |
| 110 | char *string = NULL; |
| 111 | Error *errp = NULL; |
| 112 | char *str; |
| 113 | |
| 114 | /* A null string should return "" */ |
| 115 | visit_type_str(data->ov, &string, NULL, &errp); |
| 116 | g_assert(error_is_set(&errp) == 0); |
| 117 | |
| 118 | str = string_output_get_string(data->sov); |
| 119 | g_assert(str != NULL); |
| 120 | g_assert_cmpstr(str, ==, ""); |
| 121 | g_free(str); |
| 122 | } |
| 123 | |
| 124 | static void test_visitor_out_enum(TestOutputVisitorData *data, |
| 125 | const void *unused) |
| 126 | { |
| 127 | Error *errp = NULL; |
| 128 | char *str; |
| 129 | EnumOne i; |
| 130 | |
| 131 | for (i = 0; i < ENUM_ONE_MAX; i++) { |
| 132 | visit_type_EnumOne(data->ov, &i, "unused", &errp); |
| 133 | g_assert(!error_is_set(&errp)); |
| 134 | |
| 135 | str = string_output_get_string(data->sov); |
| 136 | g_assert(str != NULL); |
| 137 | g_assert_cmpstr(str, ==, EnumOne_lookup[i]); |
| 138 | g_free(str); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void test_visitor_out_enum_errors(TestOutputVisitorData *data, |
| 143 | const void *unused) |
| 144 | { |
| 145 | EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 }; |
| 146 | Error *errp; |
| 147 | |
| 148 | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { |
| 149 | errp = NULL; |
| 150 | visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp); |
| 151 | g_assert(error_is_set(&errp) == true); |
| 152 | error_free(errp); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void output_visitor_test_add(const char *testpath, |
| 157 | TestOutputVisitorData *data, |
| 158 | void (*test_func)(TestOutputVisitorData *data, const void *user_data)) |
| 159 | { |
| 160 | g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup, |
| 161 | test_func, visitor_output_teardown); |
| 162 | } |
| 163 | |
| 164 | int main(int argc, char **argv) |
| 165 | { |
| 166 | TestOutputVisitorData out_visitor_data; |
| 167 | |
| 168 | g_test_init(&argc, &argv, NULL); |
| 169 | |
| 170 | output_visitor_test_add("/string-visitor/output/int", |
| 171 | &out_visitor_data, test_visitor_out_int); |
| 172 | output_visitor_test_add("/string-visitor/output/bool", |
| 173 | &out_visitor_data, test_visitor_out_bool); |
| 174 | output_visitor_test_add("/string-visitor/output/number", |
| 175 | &out_visitor_data, test_visitor_out_number); |
| 176 | output_visitor_test_add("/string-visitor/output/string", |
| 177 | &out_visitor_data, test_visitor_out_string); |
| 178 | output_visitor_test_add("/string-visitor/output/no-string", |
| 179 | &out_visitor_data, test_visitor_out_no_string); |
| 180 | output_visitor_test_add("/string-visitor/output/enum", |
| 181 | &out_visitor_data, test_visitor_out_enum); |
| 182 | output_visitor_test_add("/string-visitor/output/enum-errors", |
| 183 | &out_visitor_data, test_visitor_out_enum_errors); |
| 184 | |
| 185 | g_test_run(); |
| 186 | |
| 187 | return 0; |
| 188 | } |