blob: 501ba262da0b5ab0b90e95511affb139b117047a [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 */
Anthony Liguori91479dd2012-01-10 13:10:48 -060012#include <glib.h>
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030013
14#include "qint.h"
15#include "qlist.h"
16
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
32 // destroy doesn't exist yet
Anthony Liguori7267c092011-08-20 22:09:37 -050033 g_free(qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030034}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030035
Anthony Liguori91479dd2012-01-10 13:10:48 -060036static void qlist_append_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030037{
38 QInt *qi;
39 QList *qlist;
40 QListEntry *entry;
41
42 qi = qint_from_int(42);
43
44 qlist = qlist_new();
45 qlist_append(qlist, qi);
46
47 entry = QTAILQ_FIRST(&qlist->head);
Anthony Liguori91479dd2012-01-10 13:10:48 -060048 g_assert(entry != NULL);
49 g_assert(entry->value == QOBJECT(qi));
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030050
51 // destroy doesn't exist yet
52 QDECREF(qi);
Anthony Liguori7267c092011-08-20 22:09:37 -050053 g_free(entry);
54 g_free(qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030055}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030056
Anthony Liguori91479dd2012-01-10 13:10:48 -060057static void qobject_to_qlist_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030058{
59 QList *qlist;
60
61 qlist = qlist_new();
62
Anthony Liguori91479dd2012-01-10 13:10:48 -060063 g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030064
65 // destroy doesn't exist yet
Anthony Liguori7267c092011-08-20 22:09:37 -050066 g_free(qlist);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030067}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030068
Anthony Liguori91479dd2012-01-10 13:10:48 -060069static void qlist_destroy_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030070{
71 int i;
72 QList *qlist;
73
74 qlist = qlist_new();
75
76 for (i = 0; i < 42; i++)
77 qlist_append(qlist, qint_from_int(i));
78
79 QDECREF(qlist);
80}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030081
82static int iter_called;
83static const int iter_max = 42;
84
85static void iter_func(QObject *obj, void *opaque)
86{
87 QInt *qi;
88
Anthony Liguori91479dd2012-01-10 13:10:48 -060089 g_assert(opaque == NULL);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030090
91 qi = qobject_to_qint(obj);
Anthony Liguori91479dd2012-01-10 13:10:48 -060092 g_assert(qi != NULL);
93 g_assert((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max));
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030094
95 iter_called++;
96}
97
Anthony Liguori91479dd2012-01-10 13:10:48 -060098static void qlist_iter_test(void)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -030099{
100 int i;
101 QList *qlist;
102
103 qlist = qlist_new();
104
105 for (i = 0; i < iter_max; i++)
106 qlist_append(qlist, qint_from_int(i));
107
108 iter_called = 0;
109 qlist_iter(qlist, iter_func, NULL);
110
Anthony Liguori91479dd2012-01-10 13:10:48 -0600111 g_assert(iter_called == iter_max);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300112
113 QDECREF(qlist);
114}
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300115
Anthony Liguori91479dd2012-01-10 13:10:48 -0600116int main(int argc, char **argv)
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300117{
Anthony Liguori91479dd2012-01-10 13:10:48 -0600118 g_test_init(&argc, &argv, NULL);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300119
Anthony Liguori91479dd2012-01-10 13:10:48 -0600120 g_test_add_func("/public/new", qlist_new_test);
121 g_test_add_func("/public/append", qlist_append_test);
122 g_test_add_func("/public/to_qlist", qobject_to_qlist_test);
123 g_test_add_func("/public/destroy", qlist_destroy_test);
124 g_test_add_func("/public/iter", qlist_iter_test);
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300125
Anthony Liguori91479dd2012-01-10 13:10:48 -0600126 return g_test_run();
Luiz Capitulino3aa3dcf2009-10-07 13:41:49 -0300127}