]> git.proxmox.com Git - mirror_qemu.git/blame - tests/check-qnum.c
tests: remove /{qnum, qlist, dict}/destroy test
[mirror_qemu.git] / tests / check-qnum.c
CommitLineData
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"
18#include "qapi/error.h"
19#include "qemu-common.h"
20
21/*
22 * Public Interface test-cases
23 *
24 * (with some violations to access 'private' data)
25 */
26
27static void qnum_from_int_test(void)
28{
29 QNum *qn;
30 const int value = -42;
31
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);
38
36aeb609 39 QDECREF(qn);
01b2ffce
MAL
40}
41
42static void qnum_from_double_test(void)
43{
44 QNum *qn;
45 const double value = -42.23423;
46
47 qn = qnum_from_double(value);
48 g_assert(qn != NULL);
49 g_assert_cmpint(qn->kind, ==, QNUM_DOUBLE);
50 g_assert_cmpfloat(qn->u.dbl, ==, value);
51 g_assert_cmpint(qn->base.refcnt, ==, 1);
52 g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM);
53
36aeb609 54 QDECREF(qn);
01b2ffce
MAL
55}
56
57static void qnum_from_int64_test(void)
58{
59 QNum *qn;
60 const int64_t value = 0x1234567890abcdefLL;
61
62 qn = qnum_from_int(value);
63 g_assert_cmpint((int64_t) qn->u.i64, ==, value);
64
65 QDECREF(qn);
66}
67
68static void qnum_get_int_test(void)
69{
70 QNum *qn;
71 const int value = 123456;
72
73 qn = qnum_from_int(value);
74 g_assert_cmpint(qnum_get_int(qn), ==, value);
75
76 QDECREF(qn);
77}
78
79static void qobject_to_qnum_test(void)
80{
81 QNum *qn;
82
83 qn = qnum_from_int(0);
84 g_assert(qobject_to_qnum(QOBJECT(qn)) == qn);
85 QDECREF(qn);
86
87 qn = qnum_from_double(0);
88 g_assert(qobject_to_qnum(QOBJECT(qn)) == qn);
89 QDECREF(qn);
90}
91
92static void qnum_to_string_test(void)
93{
94 QNum *qn;
95 char *tmp;
96
97 qn = qnum_from_int(123456);
98 tmp = qnum_to_string(qn);
99 g_assert_cmpstr(tmp, ==, "123456");
100 g_free(tmp);
101 QDECREF(qn);
102
103 qn = qnum_from_double(0.42);
104 tmp = qnum_to_string(qn);
105 g_assert_cmpstr(tmp, ==, "0.42");
106 g_free(tmp);
107 QDECREF(qn);
108}
109
01b2ffce
MAL
110int main(int argc, char **argv)
111{
112 g_test_init(&argc, &argv, NULL);
113
114 g_test_add_func("/qnum/from_int", qnum_from_int_test);
115 g_test_add_func("/qnum/from_double", qnum_from_double_test);
01b2ffce
MAL
116 g_test_add_func("/qnum/from_int64", qnum_from_int64_test);
117 g_test_add_func("/qnum/get_int", qnum_get_int_test);
118 g_test_add_func("/qnum/to_qnum", qobject_to_qnum_test);
119 g_test_add_func("/qnum/to_string", qnum_to_string_test);
120
121 return g_test_run();
122}