4 * Copyright (C) 2009 Red Hat Inc.
5 * Copyright IBM, Corp. 2009
8 * Luiz Capitulino <lcapitulino@redhat.com>
9 * Anthony Liguori <aliguori@us.ibm.com>
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.
15 #include "qemu/osdep.h"
17 #include "qapi/qmp/qnum.h"
18 #include "qapi/error.h"
19 #include "qemu-common.h"
22 * Public Interface test-cases
24 * (with some violations to access 'private' data)
27 static void qnum_from_int_test(void)
30 const int value
= -42;
32 qn
= qnum_from_int(value
);
34 g_assert_cmpint(qn
->kind
, ==, QNUM_I64
);
35 g_assert_cmpint(qn
->u
.i64
, ==, value
);
36 g_assert_cmpint(qn
->base
.refcnt
, ==, 1);
37 g_assert_cmpint(qobject_type(QOBJECT(qn
)), ==, QTYPE_QNUM
);
42 static void qnum_from_uint_test(void)
45 const uint64_t value
= UINT64_MAX
;
47 qn
= qnum_from_uint(value
);
49 g_assert_cmpint(qn
->kind
, ==, QNUM_U64
);
50 g_assert(qn
->u
.u64
== value
);
51 g_assert(qn
->base
.refcnt
== 1);
52 g_assert(qobject_type(QOBJECT(qn
)) == QTYPE_QNUM
);
57 static void qnum_from_double_test(void)
60 const double value
= -42.23423;
62 qn
= qnum_from_double(value
);
64 g_assert_cmpint(qn
->kind
, ==, QNUM_DOUBLE
);
65 g_assert_cmpfloat(qn
->u
.dbl
, ==, value
);
66 g_assert_cmpint(qn
->base
.refcnt
, ==, 1);
67 g_assert_cmpint(qobject_type(QOBJECT(qn
)), ==, QTYPE_QNUM
);
72 static void qnum_from_int64_test(void)
75 const int64_t value
= 0x1234567890abcdefLL
;
77 qn
= qnum_from_int(value
);
78 g_assert_cmpint((int64_t) qn
->u
.i64
, ==, value
);
83 static void qnum_get_int_test(void)
86 const int value
= 123456;
88 qn
= qnum_from_int(value
);
89 g_assert_cmpint(qnum_get_int(qn
), ==, value
);
94 static void qnum_get_uint_test(void)
97 const int value
= 123456;
101 qn
= qnum_from_uint(value
);
102 g_assert(qnum_get_try_uint(qn
, &val
));
103 g_assert_cmpuint(val
, ==, value
);
106 qn
= qnum_from_int(value
);
107 g_assert(qnum_get_try_uint(qn
, &val
));
108 g_assert_cmpuint(val
, ==, value
);
112 qn
= qnum_from_int(-1);
113 g_assert(!qnum_get_try_uint(qn
, &val
));
116 qn
= qnum_from_uint(-1ULL);
117 g_assert(!qnum_get_try_int(qn
, &ival
));
120 qn
= qnum_from_double(0.42);
121 g_assert(!qnum_get_try_uint(qn
, &val
));
125 static void qobject_to_qnum_test(void)
129 qn
= qnum_from_int(0);
130 g_assert(qobject_to_qnum(QOBJECT(qn
)) == qn
);
133 qn
= qnum_from_double(0);
134 g_assert(qobject_to_qnum(QOBJECT(qn
)) == qn
);
138 static void qnum_to_string_test(void)
143 qn
= qnum_from_int(123456);
144 tmp
= qnum_to_string(qn
);
145 g_assert_cmpstr(tmp
, ==, "123456");
149 qn
= qnum_from_double(0.42);
150 tmp
= qnum_to_string(qn
);
151 g_assert_cmpstr(tmp
, ==, "0.42");
156 int main(int argc
, char **argv
)
158 g_test_init(&argc
, &argv
, NULL
);
160 g_test_add_func("/qnum/from_int", qnum_from_int_test
);
161 g_test_add_func("/qnum/from_uint", qnum_from_uint_test
);
162 g_test_add_func("/qnum/from_double", qnum_from_double_test
);
163 g_test_add_func("/qnum/from_int64", qnum_from_int64_test
);
164 g_test_add_func("/qnum/get_int", qnum_get_int_test
);
165 g_test_add_func("/qnum/get_uint", qnum_get_uint_test
);
166 g_test_add_func("/qnum/to_qnum", qobject_to_qnum_test
);
167 g_test_add_func("/qnum/to_string", qnum_to_string_test
);