And correct even more valid sparse warnings.
[platform/upstream/gstreamer.git] / tests / check / gst / gstiterator.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  *
4  * gstiterator.c: Unit test for iterators
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include <gst/check/gstcheck.h>
24
25
26 static GList *
27 make_list_of_ints (gint n)
28 {
29   GList *ret = NULL;
30   gint i;
31
32   for (i = 0; i < n; i++)
33     ret = g_list_prepend (ret, GINT_TO_POINTER (i));
34
35   return g_list_reverse (ret);
36 }
37
38 #define NUM_ELEMENTS 10
39
40 GST_START_TEST (test_manual_iteration)
41 {
42   GList *l;
43   guint32 cookie = 0;
44   GMutex *m;
45   GstIterator *iter;
46   GstIteratorResult res;
47   gpointer item;
48   gint i = 0;
49
50   l = make_list_of_ints (NUM_ELEMENTS);
51   m = g_mutex_new ();
52
53   iter = gst_iterator_new_list (G_TYPE_INT, m, &cookie, &l, NULL, NULL, NULL);
54
55   fail_unless (iter != NULL);
56
57   while (1) {
58     res = gst_iterator_next (iter, &item);
59     if (i < NUM_ELEMENTS) {
60       fail_unless (res == GST_ITERATOR_OK);
61       fail_unless (GPOINTER_TO_INT (item) == i);
62       i++;
63       continue;
64     } else {
65       fail_unless (res == GST_ITERATOR_DONE);
66       break;
67     }
68   }
69
70   /* clean up */
71   gst_iterator_free (iter);
72   g_mutex_free (m);
73   g_list_free (l);
74 }
75
76 GST_END_TEST;
77
78 GST_START_TEST (test_resync)
79 {
80   GList *l;
81   guint32 cookie = 0;
82   GMutex *m;
83   GstIterator *iter;
84   GstIteratorResult res;
85   gpointer item;
86   gint i = 0;
87   gboolean hacked_list = FALSE;
88
89   l = make_list_of_ints (NUM_ELEMENTS);
90   m = g_mutex_new ();
91
92   iter = gst_iterator_new_list (G_TYPE_INT, m, &cookie, &l, NULL, NULL, NULL);
93
94   fail_unless (iter != NULL);
95
96   while (1) {
97     res = gst_iterator_next (iter, &item);
98     if (i < NUM_ELEMENTS / 2) {
99       fail_unless (res == GST_ITERATOR_OK);
100       fail_unless (GPOINTER_TO_INT (item) == i);
101       i++;
102       continue;
103     } else if (!hacked_list) {
104       /* here's where we test resync */
105       fail_unless (res == GST_ITERATOR_OK);
106       l = g_list_prepend (l, GINT_TO_POINTER (-1));
107       cookie++;
108       hacked_list = TRUE;
109       continue;
110     } else {
111       fail_unless (res == GST_ITERATOR_RESYNC);
112       gst_iterator_resync (iter);
113       res = gst_iterator_next (iter, &item);
114       fail_unless (res == GST_ITERATOR_OK);
115       fail_unless (GPOINTER_TO_INT (item) == -1);
116       break;
117     }
118   }
119
120   /* clean up */
121   gst_iterator_free (iter);
122   g_mutex_free (m);
123   g_list_free (l);
124 }
125
126 GST_END_TEST;
127
128 static gboolean
129 add_fold_func (gpointer item, GValue * ret, gpointer user_data)
130 {
131   g_value_set_int (ret, g_value_get_int (ret) + GPOINTER_TO_INT (item));
132   return TRUE;
133 }
134
135 GST_START_TEST (test_fold)
136 {
137   GList *l;
138   guint32 cookie = 0;
139   GMutex *m;
140   GstIterator *iter;
141   GstIteratorResult res;
142   gint i, expected;
143   GValue ret = { 0, };
144
145   l = make_list_of_ints (NUM_ELEMENTS);
146   m = g_mutex_new ();
147   iter = gst_iterator_new_list (G_TYPE_INT, m, &cookie, &l, NULL, NULL, NULL);
148   fail_unless (iter != NULL);
149
150   expected = 0;
151   for (i = 0; i < NUM_ELEMENTS; i++)
152     expected += i;
153
154   g_value_init (&ret, G_TYPE_INT);
155   g_value_set_int (&ret, 0);
156
157   res = gst_iterator_fold (iter, add_fold_func, &ret, NULL);
158
159   fail_unless (res == GST_ITERATOR_DONE);
160   fail_unless (g_value_get_int (&ret) == expected);
161
162   /* clean up */
163   gst_iterator_free (iter);
164   g_mutex_free (m);
165   g_list_free (l);
166 }
167
168 GST_END_TEST;
169
170 static Suite *
171 gst_iterator_suite (void)
172 {
173   Suite *s = suite_create ("GstIterator");
174   TCase *tc_chain = tcase_create ("correctness");
175
176   tcase_set_timeout (tc_chain, 0);
177
178   suite_add_tcase (s, tc_chain);
179   tcase_add_test (tc_chain, test_manual_iteration);
180   tcase_add_test (tc_chain, test_resync);
181   tcase_add_test (tc_chain, test_fold);
182   return s;
183 }
184
185 GST_CHECK_MAIN (gst_iterator);