diff options
-rw-r--r-- | include/qapi/qmp/qnum.h | 7 | ||||
-rw-r--r-- | qobject/qnum.c | 64 | ||||
-rw-r--r-- | tests/check-qnum.c | 48 |
3 files changed, 119 insertions, 0 deletions
diff --git a/include/qapi/qmp/qnum.h b/include/qapi/qmp/qnum.h index e42b136141..09d745c490 100644 --- a/include/qapi/qmp/qnum.h +++ b/include/qapi/qmp/qnum.h @@ -19,6 +19,7 @@ typedef enum { QNUM_I64, + QNUM_U64, QNUM_DOUBLE } QNumKind; @@ -27,15 +28,21 @@ typedef struct QNum { QNumKind kind; union { int64_t i64; + uint64_t u64; double dbl; } u; } QNum; QNum *qnum_from_int(int64_t value); +QNum *qnum_from_uint(uint64_t value); QNum *qnum_from_double(double value); bool qnum_get_try_int(const QNum *qn, int64_t *val); int64_t qnum_get_int(const QNum *qn); + +bool qnum_get_try_uint(const QNum *qn, uint64_t *val); +uint64_t qnum_get_uint(const QNum *qn); + double qnum_get_double(QNum *qn); char *qnum_to_string(QNum *qn); diff --git a/qobject/qnum.c b/qobject/qnum.c index 7bb9006763..476e81c93b 100644 --- a/qobject/qnum.c +++ b/qobject/qnum.c @@ -35,6 +35,22 @@ QNum *qnum_from_int(int64_t value) } /** + * qnum_from_uint(): Create a new QNum from an uint64_t + * + * Return strong reference. + */ +QNum *qnum_from_uint(uint64_t value) +{ + QNum *qn = g_new(QNum, 1); + + qobject_init(QOBJECT(qn), QTYPE_QNUM); + qn->kind = QNUM_U64; + qn->u.u64 = value; + + return qn; +} + +/** * qnum_from_double(): Create a new QNum from a double * * Return strong reference. @@ -61,6 +77,12 @@ bool qnum_get_try_int(const QNum *qn, int64_t *val) case QNUM_I64: *val = qn->u.i64; return true; + case QNUM_U64: + if (qn->u.u64 > INT64_MAX) { + return false; + } + *val = qn->u.u64; + return true; case QNUM_DOUBLE: return false; } @@ -83,6 +105,44 @@ int64_t qnum_get_int(const QNum *qn) } /** + * qnum_get_uint(): Get an unsigned integer from the number + * + * Return true on success. + */ +bool qnum_get_try_uint(const QNum *qn, uint64_t *val) +{ + switch (qn->kind) { + case QNUM_I64: + if (qn->u.i64 < 0) { + return false; + } + *val = qn->u.i64; + return true; + case QNUM_U64: + *val = qn->u.u64; + return true; + case QNUM_DOUBLE: + return false; + } + + assert(0); + return false; +} + +/** + * qnum_get_uint(): Get an unsigned integer from the number + * + * assert() on failure. + */ +uint64_t qnum_get_uint(const QNum *qn) +{ + uint64_t val; + bool success = qnum_get_try_uint(qn, &val); + assert(success); + return val; +} + +/** * qnum_get_double(): Get a float representation of the number * * qnum_get_double() loses precision for integers beyond 53 bits. @@ -92,6 +152,8 @@ double qnum_get_double(QNum *qn) switch (qn->kind) { case QNUM_I64: return qn->u.i64; + case QNUM_U64: + return qn->u.u64; case QNUM_DOUBLE: return qn->u.dbl; } @@ -108,6 +170,8 @@ char *qnum_to_string(QNum *qn) switch (qn->kind) { case QNUM_I64: return g_strdup_printf("%" PRId64, qn->u.i64); + case QNUM_U64: + return g_strdup_printf("%" PRIu64, qn->u.u64); case QNUM_DOUBLE: /* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence diff --git a/tests/check-qnum.c b/tests/check-qnum.c index da7e0b0a9a..d702d5da9c 100644 --- a/tests/check-qnum.c +++ b/tests/check-qnum.c @@ -39,6 +39,21 @@ static void qnum_from_int_test(void) 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; @@ -76,6 +91,37 @@ static void qnum_get_int_test(void) 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; @@ -112,9 +158,11 @@ 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); |