summaryrefslogtreecommitdiff
path: root/check-qlist.c
blob: 0117ef32e8ffbbbe66c832bb6934ed9478867bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * QList unit-tests.
 *
 * Copyright (C) 2009 Red Hat Inc.
 *
 * Authors:
 *  Luiz Capitulino <lcapitulino@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2. See
 * the COPYING file in the top-level directory.
 */
#include <check.h>

#include "qint.h"
#include "qlist.h"

/*
 * Public Interface test-cases
 *
 * (with some violations to access 'private' data)
 */

START_TEST(qlist_new_test)
{
    QList *qlist;

    qlist = qlist_new();
    fail_unless(qlist != NULL);
    fail_unless(qlist->base.refcnt == 1);
    fail_unless(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);

    // destroy doesn't exist yet
    qemu_free(qlist);
}
END_TEST

START_TEST(qlist_append_test)
{
    QInt *qi;
    QList *qlist;
    QListEntry *entry;

    qi = qint_from_int(42);

    qlist = qlist_new();
    qlist_append(qlist, qi);

    entry = QTAILQ_FIRST(&qlist->head);
    fail_unless(entry != NULL);
    fail_unless(entry->value == QOBJECT(qi));

    // destroy doesn't exist yet
    QDECREF(qi);
    qemu_free(entry);
    qemu_free(qlist);
}
END_TEST

START_TEST(qobject_to_qlist_test)
{
    QList *qlist;

    qlist = qlist_new();

    fail_unless(qobject_to_qlist(QOBJECT(qlist)) == qlist);

    // destroy doesn't exist yet
    qemu_free(qlist);
}
END_TEST

START_TEST(qlist_destroy_test)
{
    int i;
    QList *qlist;

    qlist = qlist_new();

    for (i = 0; i < 42; i++)
        qlist_append(qlist, qint_from_int(i));

    QDECREF(qlist);
}
END_TEST

static int iter_called;
static const int iter_max = 42;

static void iter_func(QObject *obj, void *opaque)
{
    QInt *qi;

    fail_unless(opaque == NULL);

    qi = qobject_to_qint(obj);
    fail_unless(qi != NULL);
    fail_unless((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max));

    iter_called++;
}

START_TEST(qlist_iter_test)
{
    int i;
    QList *qlist;

    qlist = qlist_new();

    for (i = 0; i < iter_max; i++)
        qlist_append(qlist, qint_from_int(i));

    iter_called = 0;
    qlist_iter(qlist, iter_func, NULL);

    fail_unless(iter_called == iter_max);

    QDECREF(qlist);
}
END_TEST

static Suite *QList_suite(void)
{
    Suite *s;
    TCase *qlist_public_tcase;

    s = suite_create("QList suite");

    qlist_public_tcase = tcase_create("Public Interface");
    suite_add_tcase(s, qlist_public_tcase);
    tcase_add_test(qlist_public_tcase, qlist_new_test);
    tcase_add_test(qlist_public_tcase, qlist_append_test);
    tcase_add_test(qlist_public_tcase, qobject_to_qlist_test);
    tcase_add_test(qlist_public_tcase, qlist_destroy_test);
    tcase_add_test(qlist_public_tcase, qlist_iter_test);

    return s;
}

int main(void)
{
	int nf;
	Suite *s;
	SRunner *sr;

	s = QList_suite();
	sr = srunner_create(s);

	srunner_run_all(sr, CK_NORMAL);
	nf = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}