]>
Commit | Line | Data |
---|---|---|
3aa3dcff LC |
1 | /* |
2 | * QList unit-tests. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
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 MAL |
14 | #include "qapi/error.h" |
15 | #include "qapi/qmp/qnum.h" | |
7b1b5d19 | 16 | #include "qapi/qmp/qlist.h" |
3aa3dcff LC |
17 | |
18 | /* | |
19 | * Public Interface test-cases | |
20 | * | |
21 | * (with some violations to access 'private' data) | |
22 | */ | |
23 | ||
91479dd0 | 24 | static void qlist_new_test(void) |
3aa3dcff LC |
25 | { |
26 | QList *qlist; | |
27 | ||
28 | qlist = qlist_new(); | |
91479dd0 AL |
29 | g_assert(qlist != NULL); |
30 | g_assert(qlist->base.refcnt == 1); | |
31 | g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST); | |
3aa3dcff | 32 | |
36aeb609 | 33 | QDECREF(qlist); |
3aa3dcff | 34 | } |
3aa3dcff | 35 | |
91479dd0 | 36 | static void qlist_append_test(void) |
3aa3dcff | 37 | { |
01b2ffce | 38 | QNum *qi; |
3aa3dcff LC |
39 | QList *qlist; |
40 | QListEntry *entry; | |
41 | ||
01b2ffce | 42 | qi = qnum_from_int(42); |
3aa3dcff LC |
43 | |
44 | qlist = qlist_new(); | |
45 | qlist_append(qlist, qi); | |
46 | ||
47 | entry = QTAILQ_FIRST(&qlist->head); | |
91479dd0 AL |
48 | g_assert(entry != NULL); |
49 | g_assert(entry->value == QOBJECT(qi)); | |
3aa3dcff | 50 | |
36aeb609 | 51 | QDECREF(qlist); |
3aa3dcff | 52 | } |
3aa3dcff | 53 | |
91479dd0 | 54 | static void qobject_to_qlist_test(void) |
3aa3dcff LC |
55 | { |
56 | QList *qlist; | |
57 | ||
58 | qlist = qlist_new(); | |
59 | ||
91479dd0 | 60 | g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist); |
3aa3dcff | 61 | |
3aa3dcff LC |
62 | QDECREF(qlist); |
63 | } | |
3aa3dcff LC |
64 | |
65 | static int iter_called; | |
66 | static const int iter_max = 42; | |
67 | ||
68 | static void iter_func(QObject *obj, void *opaque) | |
69 | { | |
01b2ffce MAL |
70 | QNum *qi; |
71 | int64_t val; | |
3aa3dcff | 72 | |
91479dd0 | 73 | g_assert(opaque == NULL); |
3aa3dcff | 74 | |
01b2ffce | 75 | qi = qobject_to_qnum(obj); |
91479dd0 | 76 | g_assert(qi != NULL); |
01b2ffce MAL |
77 | |
78 | g_assert(qnum_get_try_int(qi, &val)); | |
79 | g_assert_cmpint(val, >=, 0); | |
80 | g_assert_cmpint(val, <=, iter_max); | |
3aa3dcff LC |
81 | |
82 | iter_called++; | |
83 | } | |
84 | ||
91479dd0 | 85 | static void qlist_iter_test(void) |
3aa3dcff LC |
86 | { |
87 | int i; | |
88 | QList *qlist; | |
89 | ||
90 | qlist = qlist_new(); | |
91 | ||
92 | for (i = 0; i < iter_max; i++) | |
46f5ac20 | 93 | qlist_append_int(qlist, i); |
3aa3dcff LC |
94 | |
95 | iter_called = 0; | |
96 | qlist_iter(qlist, iter_func, NULL); | |
97 | ||
91479dd0 | 98 | g_assert(iter_called == iter_max); |
3aa3dcff LC |
99 | |
100 | QDECREF(qlist); | |
101 | } | |
3aa3dcff | 102 | |
91479dd0 | 103 | int main(int argc, char **argv) |
3aa3dcff | 104 | { |
91479dd0 | 105 | g_test_init(&argc, &argv, NULL); |
3aa3dcff | 106 | |
91479dd0 AL |
107 | g_test_add_func("/public/new", qlist_new_test); |
108 | g_test_add_func("/public/append", qlist_append_test); | |
109 | g_test_add_func("/public/to_qlist", qobject_to_qlist_test); | |
91479dd0 | 110 | g_test_add_func("/public/iter", qlist_iter_test); |
3aa3dcff | 111 | |
91479dd0 | 112 | return g_test_run(); |
3aa3dcff | 113 | } |