blockjob: kick jobs on set-speed
[qemu/ar7.git] / tests / check-qnum.c
blobd702d5da9c63d98d8fdc50b1ee1d339ad05f7e17
1 /*
2 * QNum unit-tests.
4 * Copyright (C) 2009 Red Hat Inc.
5 * Copyright IBM, Corp. 2009
7 * Authors:
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)
29 QNum *qn;
30 const int value = -42;
32 qn = qnum_from_int(value);
33 g_assert(qn != NULL);
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);
39 QDECREF(qn);
42 static void qnum_from_uint_test(void)
44 QNum *qn;
45 const uint64_t value = UINT64_MAX;
47 qn = qnum_from_uint(value);
48 g_assert(qn != NULL);
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);
54 QDECREF(qn);
57 static void qnum_from_double_test(void)
59 QNum *qn;
60 const double value = -42.23423;
62 qn = qnum_from_double(value);
63 g_assert(qn != NULL);
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);
69 QDECREF(qn);
72 static void qnum_from_int64_test(void)
74 QNum *qn;
75 const int64_t value = 0x1234567890abcdefLL;
77 qn = qnum_from_int(value);
78 g_assert_cmpint((int64_t) qn->u.i64, ==, value);
80 QDECREF(qn);
83 static void qnum_get_int_test(void)
85 QNum *qn;
86 const int value = 123456;
88 qn = qnum_from_int(value);
89 g_assert_cmpint(qnum_get_int(qn), ==, value);
91 QDECREF(qn);
94 static void qnum_get_uint_test(void)
96 QNum *qn;
97 const int value = 123456;
98 uint64_t val;
99 int64_t ival;
101 qn = qnum_from_uint(value);
102 g_assert(qnum_get_try_uint(qn, &val));
103 g_assert_cmpuint(val, ==, value);
104 QDECREF(qn);
106 qn = qnum_from_int(value);
107 g_assert(qnum_get_try_uint(qn, &val));
108 g_assert_cmpuint(val, ==, value);
109 QDECREF(qn);
111 /* invalid cases */
112 qn = qnum_from_int(-1);
113 g_assert(!qnum_get_try_uint(qn, &val));
114 QDECREF(qn);
116 qn = qnum_from_uint(-1ULL);
117 g_assert(!qnum_get_try_int(qn, &ival));
118 QDECREF(qn);
120 qn = qnum_from_double(0.42);
121 g_assert(!qnum_get_try_uint(qn, &val));
122 QDECREF(qn);
125 static void qobject_to_qnum_test(void)
127 QNum *qn;
129 qn = qnum_from_int(0);
130 g_assert(qobject_to_qnum(QOBJECT(qn)) == qn);
131 QDECREF(qn);
133 qn = qnum_from_double(0);
134 g_assert(qobject_to_qnum(QOBJECT(qn)) == qn);
135 QDECREF(qn);
138 static void qnum_to_string_test(void)
140 QNum *qn;
141 char *tmp;
143 qn = qnum_from_int(123456);
144 tmp = qnum_to_string(qn);
145 g_assert_cmpstr(tmp, ==, "123456");
146 g_free(tmp);
147 QDECREF(qn);
149 qn = qnum_from_double(0.42);
150 tmp = qnum_to_string(qn);
151 g_assert_cmpstr(tmp, ==, "0.42");
152 g_free(tmp);
153 QDECREF(qn);
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);
169 return g_test_run();