blob: 259980d52391c11f140d67d0a606caf216e51f31 [file] [log] [blame]
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -03001/*
2 * QList unit-tests.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
Luiz Capitulino41836a92010-05-12 16:34:42 -03009 * 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 Capitulino3aa3dcf2009-10-07 13:41:49 -030011 */
Peter Maydell681c28a2016-02-08 18:08:51 +000012#include "qemu/osdep.h"
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030013
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040014#include "qapi/qmp/qnum.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/qmp/qlist.h"
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030016
17/*
18 * Public Interface test-cases
19 *
20 * (with some violations to access 'private' data)
21 */
22
Anthony Liguori91479dd2012-01-10 13:10:48 -060023static void qlist_new_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030024{
25 QList *qlist;
26
27 qlist = qlist_new();
Anthony Liguori91479dd2012-01-10 13:10:48 -060028 g_assert(qlist != NULL);
29 g_assert(qlist->base.refcnt == 1);
30 g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030031
Marc-André Lureau36aeb602017-06-07 20:36:00 +040032 QDECREF(qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030033}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030034
Anthony Liguori91479dd2012-01-10 13:10:48 -060035static void qlist_append_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030036{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040037 QNum *qi;
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030038 QList *qlist;
39 QListEntry *entry;
40
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040041 qi = qnum_from_int(42);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030042
43 qlist = qlist_new();
44 qlist_append(qlist, qi);
45
46 entry = QTAILQ_FIRST(&qlist->head);
Anthony Liguori91479dd2012-01-10 13:10:48 -060047 g_assert(entry != NULL);
48 g_assert(entry->value == QOBJECT(qi));
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030049
Marc-André Lureau36aeb602017-06-07 20:36:00 +040050 QDECREF(qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030051}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030052
Anthony Liguori91479dd2012-01-10 13:10:48 -060053static void qobject_to_qlist_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030054{
55 QList *qlist;
56
57 qlist = qlist_new();
58
Anthony Liguori91479dd2012-01-10 13:10:48 -060059 g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030060
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030061 QDECREF(qlist);
62}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030063
64static int iter_called;
65static const int iter_max = 42;
66
67static void iter_func(QObject *obj, void *opaque)
68{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040069 QNum *qi;
70 int64_t val;
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030071
Anthony Liguori91479dd2012-01-10 13:10:48 -060072 g_assert(opaque == NULL);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030073
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040074 qi = qobject_to_qnum(obj);
Anthony Liguori91479dd2012-01-10 13:10:48 -060075 g_assert(qi != NULL);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040076
77 g_assert(qnum_get_try_int(qi, &val));
78 g_assert_cmpint(val, >=, 0);
79 g_assert_cmpint(val, <=, iter_max);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030080
81 iter_called++;
82}
83
Anthony Liguori91479dd2012-01-10 13:10:48 -060084static void qlist_iter_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030085{
86 int i;
87 QList *qlist;
88
89 qlist = qlist_new();
90
91 for (i = 0; i < iter_max; i++)
Eric Blake46f5ac22017-04-27 16:58:17 -050092 qlist_append_int(qlist, i);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030093
94 iter_called = 0;
95 qlist_iter(qlist, iter_func, NULL);
96
Anthony Liguori91479dd2012-01-10 13:10:48 -060097 g_assert(iter_called == iter_max);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030098
99 QDECREF(qlist);
100}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300101
Anthony Liguori91479dd2012-01-10 13:10:48 -0600102int main(int argc, char **argv)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300103{
Anthony Liguori91479dd2012-01-10 13:10:48 -0600104 g_test_init(&argc, &argv, NULL);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300105
Anthony Liguori91479dd2012-01-10 13:10:48 -0600106 g_test_add_func("/public/new", qlist_new_test);
107 g_test_add_func("/public/append", qlist_append_test);
108 g_test_add_func("/public/to_qlist", qobject_to_qlist_test);
Anthony Liguori91479dd2012-01-10 13:10:48 -0600109 g_test_add_func("/public/iter", qlist_iter_test);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300110
Anthony Liguori91479dd2012-01-10 13:10:48 -0600111 return g_test_run();
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300112}