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