]> git.ipfire.org Git - thirdparty/qemu.git/blame - tests/check-qlist.c
tests/tcg: better detect confused gdb which can't connect
[thirdparty/qemu.git] / tests / check-qlist.c
CommitLineData
3aa3dcff
LC
1/*
2 * QList unit-tests.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
41836a9f
LC
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.
3aa3dcff 11 */
681c28a3 12#include "qemu/osdep.h"
3aa3dcff 13
01b2ffce 14#include "qapi/qmp/qnum.h"
7b1b5d19 15#include "qapi/qmp/qlist.h"
3aa3dcff
LC
16
17/*
18 * Public Interface test-cases
19 *
20 * (with some violations to access 'private' data)
21 */
22
91479dd0 23static void qlist_new_test(void)
3aa3dcff
LC
24{
25 QList *qlist;
26
27 qlist = qlist_new();
91479dd0
AL
28 g_assert(qlist != NULL);
29 g_assert(qlist->base.refcnt == 1);
30 g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
3aa3dcff 31
cb3e7f08 32 qobject_unref(qlist);
3aa3dcff 33}
3aa3dcff 34
91479dd0 35static void qlist_append_test(void)
3aa3dcff 36{
01b2ffce 37 QNum *qi;
3aa3dcff
LC
38 QList *qlist;
39 QListEntry *entry;
40
01b2ffce 41 qi = qnum_from_int(42);
3aa3dcff
LC
42
43 qlist = qlist_new();
44 qlist_append(qlist, qi);
45
46 entry = QTAILQ_FIRST(&qlist->head);
91479dd0
AL
47 g_assert(entry != NULL);
48 g_assert(entry->value == QOBJECT(qi));
3aa3dcff 49
cb3e7f08 50 qobject_unref(qlist);
3aa3dcff 51}
3aa3dcff 52
91479dd0 53static void qobject_to_qlist_test(void)
3aa3dcff
LC
54{
55 QList *qlist;
56
57 qlist = qlist_new();
58
7dc847eb 59 g_assert(qobject_to(QList, QOBJECT(qlist)) == qlist);
3aa3dcff 60
cb3e7f08 61 qobject_unref(qlist);
3aa3dcff 62}
3aa3dcff 63
91479dd0 64static void qlist_iter_test(void)
3aa3dcff 65{
2f2ec111 66 const int iter_max = 42;
3aa3dcff
LC
67 int i;
68 QList *qlist;
2f2ec111
MA
69 QListEntry *entry;
70 QNum *qi;
71 int64_t val;
3aa3dcff
LC
72
73 qlist = qlist_new();
74
75 for (i = 0; i < iter_max; i++)
46f5ac20 76 qlist_append_int(qlist, i);
3aa3dcff 77
2f2ec111
MA
78 i = 0;
79 QLIST_FOREACH_ENTRY(qlist, entry) {
80 qi = qobject_to(QNum, qlist_entry_obj(entry));
81 g_assert(qi != NULL);
82
83 g_assert(qnum_get_try_int(qi, &val));
84 g_assert_cmpint(val, ==, i);
85 i++;
86 }
3aa3dcff 87
2f2ec111 88 g_assert(i == iter_max);
3aa3dcff 89
cb3e7f08 90 qobject_unref(qlist);
3aa3dcff 91}
3aa3dcff 92
91479dd0 93int main(int argc, char **argv)
3aa3dcff 94{
91479dd0 95 g_test_init(&argc, &argv, NULL);
3aa3dcff 96
91479dd0
AL
97 g_test_add_func("/public/new", qlist_new_test);
98 g_test_add_func("/public/append", qlist_append_test);
99 g_test_add_func("/public/to_qlist", qobject_to_qlist_test);
91479dd0 100 g_test_add_func("/public/iter", qlist_iter_test);
3aa3dcff 101
91479dd0 102 return g_test_run();
3aa3dcff 103}