diff options
author | Peter Maydell <peter.maydell@linaro.org> | 2017-06-22 11:34:38 +0100 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2017-06-22 11:34:39 +0100 |
commit | 84e3d0725b06bdf8c6985788caa7776d6b7353ce (patch) | |
tree | e4b273590132a034fe693debb52e7037bb7730f2 /tests/check-qnum.c | |
parent | db7a99cdc1d0f4d8cbf7c41ce9e570dce04f0a11 (diff) | |
parent | 269c20b2bbd2aa8531e0cdc741fb166f290d7a2b (diff) |
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2017-06-09-v2' into staging
QAPI patches for 2017-06-09
# gpg: Signature made Tue 20 Jun 2017 13:31:39 BST
# gpg: using RSA key 0x3870B400EB918653
# gpg: Good signature from "Markus Armbruster <armbru@redhat.com>"
# gpg: aka "Markus Armbruster <armbru@pond.sub.org>"
# Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653
* remotes/armbru/tags/pull-qapi-2017-06-09-v2: (41 commits)
tests/qdict: check more get_try_int() cases
console: use get_uint() for "head" property
i386/cpu: use get_uint() for "min-level"/"min-xlevel" properties
numa: use get_uint() for "size" property
pnv-core: use get_uint() for "core-pir" property
pvpanic: use get_uint() for "ioport" property
auxbus: use get_uint() for "addr" property
arm: use get_uint() for "mp-affinity" property
xen: use get_uint() for "max-ram-below-4g" property
pc: use get_uint() for "hpet-intcap" property
pc: use get_uint() for "apic-id" property
pc: use get_uint() for "iobase" property
acpi: use get_uint() for "pci-hole*" properties
acpi: use get_uint() for various acpi properties
acpi: use get_uint() for "acpi-pcihp-io*" properties
platform-bus: use get_uint() for "addr" property
bcm2835_fb: use {get, set}_uint() for "vcram-size" and "vcram-base"
aspeed: use {set, get}_uint() for "ram-size" property
pcihp: use get_uint() for "bsel" property
pc-dimm: make "size" property uint64
...
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'tests/check-qnum.c')
-rw-r--r-- | tests/check-qnum.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/tests/check-qnum.c b/tests/check-qnum.c new file mode 100644 index 0000000000..d702d5da9c --- /dev/null +++ b/tests/check-qnum.c @@ -0,0 +1,170 @@ +/* + * QNum unit-tests. + * + * Copyright (C) 2009 Red Hat Inc. + * Copyright IBM, Corp. 2009 + * + * Authors: + * Luiz Capitulino <lcapitulino@redhat.com> + * Anthony Liguori <aliguori@us.ibm.com> + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu/osdep.h" + +#include "qapi/qmp/qnum.h" +#include "qapi/error.h" +#include "qemu-common.h" + +/* + * Public Interface test-cases + * + * (with some violations to access 'private' data) + */ + +static void qnum_from_int_test(void) +{ + QNum *qn; + const int value = -42; + + qn = qnum_from_int(value); + g_assert(qn != NULL); + g_assert_cmpint(qn->kind, ==, QNUM_I64); + g_assert_cmpint(qn->u.i64, ==, value); + g_assert_cmpint(qn->base.refcnt, ==, 1); + g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); + + QDECREF(qn); +} + +static void qnum_from_uint_test(void) +{ + QNum *qn; + const uint64_t value = UINT64_MAX; + + qn = qnum_from_uint(value); + g_assert(qn != NULL); + g_assert_cmpint(qn->kind, ==, QNUM_U64); + g_assert(qn->u.u64 == value); + g_assert(qn->base.refcnt == 1); + g_assert(qobject_type(QOBJECT(qn)) == QTYPE_QNUM); + + QDECREF(qn); +} + +static void qnum_from_double_test(void) +{ + QNum *qn; + const double value = -42.23423; + + qn = qnum_from_double(value); + g_assert(qn != NULL); + g_assert_cmpint(qn->kind, ==, QNUM_DOUBLE); + g_assert_cmpfloat(qn->u.dbl, ==, value); + g_assert_cmpint(qn->base.refcnt, ==, 1); + g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM); + + QDECREF(qn); +} + +static void qnum_from_int64_test(void) +{ + QNum *qn; + const int64_t value = 0x1234567890abcdefLL; + + qn = qnum_from_int(value); + g_assert_cmpint((int64_t) qn->u.i64, ==, value); + + QDECREF(qn); +} + +static void qnum_get_int_test(void) +{ + QNum *qn; + const int value = 123456; + + qn = qnum_from_int(value); + g_assert_cmpint(qnum_get_int(qn), ==, value); + + QDECREF(qn); +} + +static void qnum_get_uint_test(void) +{ + QNum *qn; + const int value = 123456; + uint64_t val; + int64_t ival; + + qn = qnum_from_uint(value); + g_assert(qnum_get_try_uint(qn, &val)); + g_assert_cmpuint(val, ==, value); + QDECREF(qn); + + qn = qnum_from_int(value); + g_assert(qnum_get_try_uint(qn, &val)); + g_assert_cmpuint(val, ==, value); + QDECREF(qn); + + /* invalid cases */ + qn = qnum_from_int(-1); + g_assert(!qnum_get_try_uint(qn, &val)); + QDECREF(qn); + + qn = qnum_from_uint(-1ULL); + g_assert(!qnum_get_try_int(qn, &ival)); + QDECREF(qn); + + qn = qnum_from_double(0.42); + g_assert(!qnum_get_try_uint(qn, &val)); + QDECREF(qn); +} + +static void qobject_to_qnum_test(void) +{ + QNum *qn; + + qn = qnum_from_int(0); + g_assert(qobject_to_qnum(QOBJECT(qn)) == qn); + QDECREF(qn); + + qn = qnum_from_double(0); + g_assert(qobject_to_qnum(QOBJECT(qn)) == qn); + QDECREF(qn); +} + +static void qnum_to_string_test(void) +{ + QNum *qn; + char *tmp; + + qn = qnum_from_int(123456); + tmp = qnum_to_string(qn); + g_assert_cmpstr(tmp, ==, "123456"); + g_free(tmp); + QDECREF(qn); + + qn = qnum_from_double(0.42); + tmp = qnum_to_string(qn); + g_assert_cmpstr(tmp, ==, "0.42"); + g_free(tmp); + QDECREF(qn); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/qnum/from_int", qnum_from_int_test); + g_test_add_func("/qnum/from_uint", qnum_from_uint_test); + g_test_add_func("/qnum/from_double", qnum_from_double_test); + g_test_add_func("/qnum/from_int64", qnum_from_int64_test); + g_test_add_func("/qnum/get_int", qnum_get_int_test); + g_test_add_func("/qnum/get_uint", qnum_get_uint_test); + g_test_add_func("/qnum/to_qnum", qobject_to_qnum_test); + g_test_add_func("/qnum/to_string", qnum_to_string_test); + + return g_test_run(); +} |