]>
Commit | Line | Data |
---|---|---|
01b2ffce MAL |
1 | /* |
2 | * QNum unit-tests. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * Copyright IBM, Corp. 2009 | |
6 | * | |
7 | * Authors: | |
8 | * Luiz Capitulino <lcapitulino@redhat.com> | |
9 | * Anthony Liguori <aliguori@us.ibm.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
12 | * See the COPYING.LIB file in the top-level directory. | |
13 | */ | |
14 | ||
15 | #include "qemu/osdep.h" | |
16 | ||
17 | #include "qapi/qmp/qnum.h" | |
01b2ffce MAL |
18 | |
19 | /* | |
20 | * Public Interface test-cases | |
21 | * | |
22 | * (with some violations to access 'private' data) | |
23 | */ | |
24 | ||
25 | static void qnum_from_int_test(void) | |
26 | { | |
27 | QNum *qn; | |
28 | const int value = -42; | |
29 | ||
30 | qn = qnum_from_int(value); | |
31 | g_assert(qn != NULL); | |
32 | g_assert_cmpint(qn->kind, ==, QNUM_I64); | |
33 | g_assert_cmpint(qn->u.i64, ==, value); | |
34 | g_assert_cmpint(qn->base.refcnt, ==, 1); | |
35 | g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); | |
36 | ||
cb3e7f08 | 37 | qobject_unref(qn); |
01b2ffce MAL |
38 | } |
39 | ||
61a8f418 MAL |
40 | static void qnum_from_uint_test(void) |
41 | { | |
42 | QNum *qn; | |
43 | const uint64_t value = UINT64_MAX; | |
44 | ||
45 | qn = qnum_from_uint(value); | |
46 | g_assert(qn != NULL); | |
47 | g_assert_cmpint(qn->kind, ==, QNUM_U64); | |
48 | g_assert(qn->u.u64 == value); | |
49 | g_assert(qn->base.refcnt == 1); | |
50 | g_assert(qobject_type(QOBJECT(qn)) == QTYPE_QNUM); | |
51 | ||
cb3e7f08 | 52 | qobject_unref(qn); |
61a8f418 MAL |
53 | } |
54 | ||
01b2ffce MAL |
55 | static void qnum_from_double_test(void) |
56 | { | |
57 | QNum *qn; | |
58 | const double value = -42.23423; | |
59 | ||
60 | qn = qnum_from_double(value); | |
61 | g_assert(qn != NULL); | |
62 | g_assert_cmpint(qn->kind, ==, QNUM_DOUBLE); | |
63 | g_assert_cmpfloat(qn->u.dbl, ==, value); | |
64 | g_assert_cmpint(qn->base.refcnt, ==, 1); | |
65 | g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); | |
66 | ||
cb3e7f08 | 67 | qobject_unref(qn); |
01b2ffce MAL |
68 | } |
69 | ||
70 | static void qnum_from_int64_test(void) | |
71 | { | |
72 | QNum *qn; | |
73 | const int64_t value = 0x1234567890abcdefLL; | |
74 | ||
75 | qn = qnum_from_int(value); | |
76 | g_assert_cmpint((int64_t) qn->u.i64, ==, value); | |
77 | ||
cb3e7f08 | 78 | qobject_unref(qn); |
01b2ffce MAL |
79 | } |
80 | ||
81 | static void qnum_get_int_test(void) | |
82 | { | |
83 | QNum *qn; | |
84 | const int value = 123456; | |
85 | ||
86 | qn = qnum_from_int(value); | |
87 | g_assert_cmpint(qnum_get_int(qn), ==, value); | |
88 | ||
cb3e7f08 | 89 | qobject_unref(qn); |
01b2ffce MAL |
90 | } |
91 | ||
61a8f418 MAL |
92 | static void qnum_get_uint_test(void) |
93 | { | |
94 | QNum *qn; | |
95 | const int value = 123456; | |
96 | uint64_t val; | |
97 | int64_t ival; | |
98 | ||
99 | qn = qnum_from_uint(value); | |
100 | g_assert(qnum_get_try_uint(qn, &val)); | |
101 | g_assert_cmpuint(val, ==, value); | |
cb3e7f08 | 102 | qobject_unref(qn); |
61a8f418 MAL |
103 | |
104 | qn = qnum_from_int(value); | |
105 | g_assert(qnum_get_try_uint(qn, &val)); | |
106 | g_assert_cmpuint(val, ==, value); | |
cb3e7f08 | 107 | qobject_unref(qn); |
61a8f418 MAL |
108 | |
109 | /* invalid cases */ | |
110 | qn = qnum_from_int(-1); | |
111 | g_assert(!qnum_get_try_uint(qn, &val)); | |
cb3e7f08 | 112 | qobject_unref(qn); |
61a8f418 MAL |
113 | |
114 | qn = qnum_from_uint(-1ULL); | |
115 | g_assert(!qnum_get_try_int(qn, &ival)); | |
cb3e7f08 | 116 | qobject_unref(qn); |
61a8f418 MAL |
117 | |
118 | qn = qnum_from_double(0.42); | |
119 | g_assert(!qnum_get_try_uint(qn, &val)); | |
cb3e7f08 | 120 | qobject_unref(qn); |
61a8f418 MAL |
121 | } |
122 | ||
01b2ffce MAL |
123 | static void qobject_to_qnum_test(void) |
124 | { | |
125 | QNum *qn; | |
126 | ||
127 | qn = qnum_from_int(0); | |
7dc847eb | 128 | g_assert(qobject_to(QNum, QOBJECT(qn)) == qn); |
cb3e7f08 | 129 | qobject_unref(qn); |
01b2ffce MAL |
130 | |
131 | qn = qnum_from_double(0); | |
7dc847eb | 132 | g_assert(qobject_to(QNum, QOBJECT(qn)) == qn); |
cb3e7f08 | 133 | qobject_unref(qn); |
01b2ffce MAL |
134 | } |
135 | ||
136 | static void qnum_to_string_test(void) | |
137 | { | |
138 | QNum *qn; | |
139 | char *tmp; | |
140 | ||
141 | qn = qnum_from_int(123456); | |
142 | tmp = qnum_to_string(qn); | |
143 | g_assert_cmpstr(tmp, ==, "123456"); | |
144 | g_free(tmp); | |
cb3e7f08 | 145 | qobject_unref(qn); |
01b2ffce MAL |
146 | |
147 | qn = qnum_from_double(0.42); | |
148 | tmp = qnum_to_string(qn); | |
f917eed3 | 149 | g_assert_cmpstr(tmp, ==, "0.41999999999999998"); |
01b2ffce | 150 | g_free(tmp); |
cb3e7f08 | 151 | qobject_unref(qn); |
1a907691 MA |
152 | |
153 | qn = qnum_from_double(2.718281828459045); | |
154 | tmp = qnum_to_string(qn); | |
f917eed3 | 155 | g_assert_cmpstr(tmp, ==, "2.7182818284590451"); |
1a907691 MA |
156 | g_free(tmp); |
157 | qobject_unref(qn); | |
01b2ffce MAL |
158 | } |
159 | ||
01b2ffce MAL |
160 | int main(int argc, char **argv) |
161 | { | |
162 | g_test_init(&argc, &argv, NULL); | |
163 | ||
164 | g_test_add_func("/qnum/from_int", qnum_from_int_test); | |
61a8f418 | 165 | g_test_add_func("/qnum/from_uint", qnum_from_uint_test); |
01b2ffce | 166 | g_test_add_func("/qnum/from_double", qnum_from_double_test); |
01b2ffce MAL |
167 | g_test_add_func("/qnum/from_int64", qnum_from_int64_test); |
168 | g_test_add_func("/qnum/get_int", qnum_get_int_test); | |
61a8f418 | 169 | g_test_add_func("/qnum/get_uint", qnum_get_uint_test); |
01b2ffce MAL |
170 | g_test_add_func("/qnum/to_qnum", qobject_to_qnum_test); |
171 | g_test_add_func("/qnum/to_string", qnum_to_string_test); | |
172 | ||
173 | return g_test_run(); | |
174 | } |