Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 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" |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 18 | #include "qemu-common.h" |
| 19 | |
| 20 | /* |
| 21 | * Public Interface test-cases |
| 22 | * |
| 23 | * (with some violations to access 'private' data) |
| 24 | */ |
| 25 | |
| 26 | static void qnum_from_int_test(void) |
| 27 | { |
| 28 | QNum *qn; |
| 29 | const int value = -42; |
| 30 | |
| 31 | qn = qnum_from_int(value); |
| 32 | g_assert(qn != NULL); |
| 33 | g_assert_cmpint(qn->kind, ==, QNUM_I64); |
| 34 | g_assert_cmpint(qn->u.i64, ==, value); |
| 35 | g_assert_cmpint(qn->base.refcnt, ==, 1); |
| 36 | g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); |
| 37 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 38 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 39 | } |
| 40 | |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 41 | static void qnum_from_uint_test(void) |
| 42 | { |
| 43 | QNum *qn; |
| 44 | const uint64_t value = UINT64_MAX; |
| 45 | |
| 46 | qn = qnum_from_uint(value); |
| 47 | g_assert(qn != NULL); |
| 48 | g_assert_cmpint(qn->kind, ==, QNUM_U64); |
| 49 | g_assert(qn->u.u64 == value); |
| 50 | g_assert(qn->base.refcnt == 1); |
| 51 | g_assert(qobject_type(QOBJECT(qn)) == QTYPE_QNUM); |
| 52 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 53 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 54 | } |
| 55 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 56 | static void qnum_from_double_test(void) |
| 57 | { |
| 58 | QNum *qn; |
| 59 | const double value = -42.23423; |
| 60 | |
| 61 | qn = qnum_from_double(value); |
| 62 | g_assert(qn != NULL); |
| 63 | g_assert_cmpint(qn->kind, ==, QNUM_DOUBLE); |
| 64 | g_assert_cmpfloat(qn->u.dbl, ==, value); |
| 65 | g_assert_cmpint(qn->base.refcnt, ==, 1); |
| 66 | g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); |
| 67 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 68 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void qnum_from_int64_test(void) |
| 72 | { |
| 73 | QNum *qn; |
| 74 | const int64_t value = 0x1234567890abcdefLL; |
| 75 | |
| 76 | qn = qnum_from_int(value); |
| 77 | g_assert_cmpint((int64_t) qn->u.i64, ==, value); |
| 78 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 79 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void qnum_get_int_test(void) |
| 83 | { |
| 84 | QNum *qn; |
| 85 | const int value = 123456; |
| 86 | |
| 87 | qn = qnum_from_int(value); |
| 88 | g_assert_cmpint(qnum_get_int(qn), ==, value); |
| 89 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 90 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 91 | } |
| 92 | |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 93 | static void qnum_get_uint_test(void) |
| 94 | { |
| 95 | QNum *qn; |
| 96 | const int value = 123456; |
| 97 | uint64_t val; |
| 98 | int64_t ival; |
| 99 | |
| 100 | qn = qnum_from_uint(value); |
| 101 | g_assert(qnum_get_try_uint(qn, &val)); |
| 102 | g_assert_cmpuint(val, ==, value); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 103 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 104 | |
| 105 | qn = qnum_from_int(value); |
| 106 | g_assert(qnum_get_try_uint(qn, &val)); |
| 107 | g_assert_cmpuint(val, ==, value); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 108 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 109 | |
| 110 | /* invalid cases */ |
| 111 | qn = qnum_from_int(-1); |
| 112 | g_assert(!qnum_get_try_uint(qn, &val)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 113 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 114 | |
| 115 | qn = qnum_from_uint(-1ULL); |
| 116 | g_assert(!qnum_get_try_int(qn, &ival)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 117 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 118 | |
| 119 | qn = qnum_from_double(0.42); |
| 120 | g_assert(!qnum_get_try_uint(qn, &val)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 121 | qobject_unref(qn); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 122 | } |
| 123 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 124 | static void qobject_to_qnum_test(void) |
| 125 | { |
| 126 | QNum *qn; |
| 127 | |
| 128 | qn = qnum_from_int(0); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 129 | g_assert(qobject_to(QNum, QOBJECT(qn)) == qn); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 130 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 131 | |
| 132 | qn = qnum_from_double(0); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 133 | g_assert(qobject_to(QNum, QOBJECT(qn)) == qn); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 134 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void qnum_to_string_test(void) |
| 138 | { |
| 139 | QNum *qn; |
| 140 | char *tmp; |
| 141 | |
| 142 | qn = qnum_from_int(123456); |
| 143 | tmp = qnum_to_string(qn); |
| 144 | g_assert_cmpstr(tmp, ==, "123456"); |
| 145 | g_free(tmp); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 146 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 147 | |
| 148 | qn = qnum_from_double(0.42); |
| 149 | tmp = qnum_to_string(qn); |
Markus Armbruster | f917eed | 2020-12-10 17:14:48 +0100 | [diff] [blame] | 150 | g_assert_cmpstr(tmp, ==, "0.41999999999999998"); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 151 | g_free(tmp); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 152 | qobject_unref(qn); |
Markus Armbruster | 1a90769 | 2020-12-10 17:14:47 +0100 | [diff] [blame] | 153 | |
| 154 | qn = qnum_from_double(2.718281828459045); |
| 155 | tmp = qnum_to_string(qn); |
Markus Armbruster | f917eed | 2020-12-10 17:14:48 +0100 | [diff] [blame] | 156 | g_assert_cmpstr(tmp, ==, "2.7182818284590451"); |
Markus Armbruster | 1a90769 | 2020-12-10 17:14:47 +0100 | [diff] [blame] | 157 | g_free(tmp); |
| 158 | qobject_unref(qn); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 159 | } |
| 160 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | g_test_init(&argc, &argv, NULL); |
| 164 | |
| 165 | g_test_add_func("/qnum/from_int", qnum_from_int_test); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 166 | g_test_add_func("/qnum/from_uint", qnum_from_uint_test); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 167 | g_test_add_func("/qnum/from_double", qnum_from_double_test); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 168 | g_test_add_func("/qnum/from_int64", qnum_from_int64_test); |
| 169 | g_test_add_func("/qnum/get_int", qnum_get_int_test); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 170 | g_test_add_func("/qnum/get_uint", qnum_get_uint_test); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 171 | g_test_add_func("/qnum/to_qnum", qobject_to_qnum_test); |
| 172 | g_test_add_func("/qnum/to_string", qnum_to_string_test); |
| 173 | |
| 174 | return g_test_run(); |
| 175 | } |