7dc847ebba
This patch was generated using the following Coccinelle script: @@ expression Obj; @@ ( - qobject_to_qnum(Obj) + qobject_to(QNum, Obj) | - qobject_to_qstring(Obj) + qobject_to(QString, Obj) | - qobject_to_qdict(Obj) + qobject_to(QDict, Obj) | - qobject_to_qlist(Obj) + qobject_to(QList, Obj) | - qobject_to_qbool(Obj) + qobject_to(QBool, Obj) ) and a bit of manual fix-up for overly long lines and three places in tests/check-qjson.c that Coccinelle did not find. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Alberto Garcia <berto@igalia.com> Message-Id: <20180224154033.29559-4-mreitz@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> [eblake: swap order from qobject_to(o, X), rebase to master, also a fix to latent false-positive compiler complaint about hw/i386/acpi-build.c] Signed-off-by: Eric Blake <eblake@redhat.com>
170 lines
3.8 KiB
C
170 lines
3.8 KiB
C
/*
|
|
* 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 "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();
|
|
}
|