blob: 86868844abbfd2ef333d80a8301d46a9f84130de [file] [log] [blame]
Luiz Capitulino33837ba2009-08-28 15:27:29 -03001/*
2 * QInt unit-tests.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
Luiz Capitulino41836a92010-05-12 16:34:42 -03008 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
Luiz Capitulino33837ba2009-08-28 15:27:29 -030011 */
Anthony Liguori65cdadd2012-01-10 13:10:46 -060012#include <glib.h>
Luiz Capitulino33837ba2009-08-28 15:27:29 -030013
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/qmp/qint.h"
Luiz Capitulino33837ba2009-08-28 15:27:29 -030015#include "qemu-common.h"
16
17/*
18 * Public Interface test-cases
19 *
20 * (with some violations to access 'private' data)
21 */
22
Anthony Liguori65cdadd2012-01-10 13:10:46 -060023static void qint_from_int_test(void)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030024{
25 QInt *qi;
26 const int value = -42;
27
28 qi = qint_from_int(value);
Anthony Liguori65cdadd2012-01-10 13:10:46 -060029 g_assert(qi != NULL);
30 g_assert(qi->value == value);
31 g_assert(qi->base.refcnt == 1);
32 g_assert(qobject_type(QOBJECT(qi)) == QTYPE_QINT);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030033
34 // destroy doesn't exit yet
Anthony Liguori7267c092011-08-20 22:09:37 -050035 g_free(qi);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030036}
Luiz Capitulino33837ba2009-08-28 15:27:29 -030037
Anthony Liguori65cdadd2012-01-10 13:10:46 -060038static void qint_destroy_test(void)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030039{
40 QInt *qi = qint_from_int(0);
41 QDECREF(qi);
42}
Luiz Capitulino33837ba2009-08-28 15:27:29 -030043
Anthony Liguori65cdadd2012-01-10 13:10:46 -060044static void qint_from_int64_test(void)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030045{
46 QInt *qi;
Pierre Riteau02c068c2009-09-08 11:53:37 +020047 const int64_t value = 0x1234567890abcdefLL;
Luiz Capitulino33837ba2009-08-28 15:27:29 -030048
49 qi = qint_from_int(value);
Anthony Liguori65cdadd2012-01-10 13:10:46 -060050 g_assert((int64_t) qi->value == value);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030051
52 QDECREF(qi);
53}
Luiz Capitulino33837ba2009-08-28 15:27:29 -030054
Anthony Liguori65cdadd2012-01-10 13:10:46 -060055static void qint_get_int_test(void)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030056{
57 QInt *qi;
58 const int value = 123456;
59
60 qi = qint_from_int(value);
Anthony Liguori65cdadd2012-01-10 13:10:46 -060061 g_assert(qint_get_int(qi) == value);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030062
63 QDECREF(qi);
64}
Luiz Capitulino33837ba2009-08-28 15:27:29 -030065
Anthony Liguori65cdadd2012-01-10 13:10:46 -060066static void qobject_to_qint_test(void)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030067{
68 QInt *qi;
69
70 qi = qint_from_int(0);
Anthony Liguori65cdadd2012-01-10 13:10:46 -060071 g_assert(qobject_to_qint(QOBJECT(qi)) == qi);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030072
73 QDECREF(qi);
74}
Luiz Capitulino33837ba2009-08-28 15:27:29 -030075
Anthony Liguori65cdadd2012-01-10 13:10:46 -060076int main(int argc, char **argv)
Luiz Capitulino33837ba2009-08-28 15:27:29 -030077{
Anthony Liguori65cdadd2012-01-10 13:10:46 -060078 g_test_init(&argc, &argv, NULL);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030079
Anthony Liguori65cdadd2012-01-10 13:10:46 -060080 g_test_add_func("/public/from_int", qint_from_int_test);
81 g_test_add_func("/public/destroy", qint_destroy_test);
82 g_test_add_func("/public/from_int64", qint_from_int64_test);
83 g_test_add_func("/public/get_int", qint_get_int_test);
84 g_test_add_func("/public/to_qint", qobject_to_qint_test);
Luiz Capitulino33837ba2009-08-28 15:27:29 -030085
Anthony Liguori65cdadd2012-01-10 13:10:46 -060086 return g_test_run();
Luiz Capitulino33837ba2009-08-28 15:27:29 -030087}