caps: improve _do_simplify
[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   GValue item = { 0, };
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_POINTER, m, &cookie, &l, 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 (g_value_get_pointer (&item)) == i);
62       g_value_reset (&item);
63       i++;
64       continue;
65     } else {
66       fail_unless (res == GST_ITERATOR_DONE);
67       break;
68     }
69   }
70   /* clean up */
71   g_value_unset (&item);
72   gst_iterator_free (iter);
73   g_mutex_free (m);
74   g_list_free (l);
75 }
76
77 GST_END_TEST;
78
79 GST_START_TEST (test_resync)
80 {
81   GList *l;
82   guint32 cookie = 0;
83   GMutex *m;
84   GstIterator *iter;
85   GstIteratorResult res;
86   GValue item = { 0, };
87   gint i = 0;
88   gboolean hacked_list = FALSE;
89
90   l = make_list_of_ints (NUM_ELEMENTS);
91   m = g_mutex_new ();
92
93   iter = gst_iterator_new_list (G_TYPE_POINTER, m, &cookie, &l, NULL, NULL);
94
95   fail_unless (iter != NULL);
96
97   while (1) {
98     res = gst_iterator_next (iter, &item);
99     if (i < NUM_ELEMENTS / 2) {
100       fail_unless (res == GST_ITERATOR_OK);
101       fail_unless (GPOINTER_TO_INT (g_value_get_pointer (&item)) == i);
102       g_value_reset (&item);
103       i++;
104       continue;
105     } else if (!hacked_list) {
106       /* here's where we test resync */
107       fail_unless (res == GST_ITERATOR_OK);
108       g_value_reset (&item);
109       l = g_list_prepend (l, GINT_TO_POINTER (-1));
110       cookie++;
111       hacked_list = TRUE;
112       continue;
113     } else {
114       fail_unless (res == GST_ITERATOR_RESYNC);
115       gst_iterator_resync (iter);
116       res = gst_iterator_next (iter, &item);
117       fail_unless (res == GST_ITERATOR_OK);
118       fail_unless (GPOINTER_TO_INT (g_value_get_pointer (&item)) == -1);
119       g_value_reset (&item);
120       break;
121     }
122   }
123
124   /* clean up */
125   g_value_unset (&item);
126   gst_iterator_free (iter);
127   g_mutex_free (m);
128   g_list_free (l);
129 }
130
131 GST_END_TEST;
132
133 static gboolean
134 add_fold_func (const GValue * item, GValue * ret, gpointer user_data)
135 {
136   g_value_set_int (ret,
137       g_value_get_int (ret) + GPOINTER_TO_INT (g_value_get_pointer (item)));
138   return TRUE;
139 }
140
141 GST_START_TEST (test_fold)
142 {
143   GList *l;
144   guint32 cookie = 0;
145   GMutex *m;
146   GstIterator *iter;
147   GstIteratorResult res;
148   gint i, expected;
149   GValue ret = { 0, };
150
151   l = make_list_of_ints (NUM_ELEMENTS);
152   m = g_mutex_new ();
153   iter = gst_iterator_new_list (G_TYPE_POINTER, m, &cookie, &l, NULL, NULL);
154   fail_unless (iter != NULL);
155
156   expected = 0;
157   for (i = 0; i < NUM_ELEMENTS; i++)
158     expected += i;
159
160   g_value_init (&ret, G_TYPE_INT);
161   g_value_set_int (&ret, 0);
162
163   res = gst_iterator_fold (iter, add_fold_func, &ret, NULL);
164
165   fail_unless (res == GST_ITERATOR_DONE);
166   fail_unless (g_value_get_int (&ret) == expected);
167
168   /* clean up */
169   gst_iterator_free (iter);
170   g_mutex_free (m);
171   g_list_free (l);
172 }
173
174 GST_END_TEST;
175
176 GST_START_TEST (test_single)
177 {
178   GstIterator *it;
179   GstStructure *s = gst_structure_new_empty ("test");
180   GValue v = { 0, };
181   GstStructure *i;
182
183   g_value_init (&v, GST_TYPE_STRUCTURE);
184   g_value_set_boxed (&v, s);
185   it = gst_iterator_new_single (GST_TYPE_STRUCTURE, &v);
186   g_value_reset (&v);
187
188   fail_unless (gst_iterator_next (it, &v) == GST_ITERATOR_OK);
189   i = g_value_get_boxed (&v);
190   fail_unless (strcmp (gst_structure_get_name (s),
191           gst_structure_get_name (i)) == 0);
192   i = NULL;
193   g_value_reset (&v);
194
195   fail_unless (gst_iterator_next (it, &v) == GST_ITERATOR_DONE);
196   fail_unless (g_value_get_boxed (&v) == NULL);
197
198   gst_iterator_free (it);
199   gst_structure_free (s);
200
201   it = gst_iterator_new_single (GST_TYPE_STRUCTURE, NULL);
202
203   fail_unless (gst_iterator_next (it, &v) == GST_ITERATOR_DONE);
204   fail_unless (g_value_get_boxed (&v) == NULL);
205
206   g_value_reset (&v);
207
208   gst_iterator_free (it);
209 }
210
211 GST_END_TEST;
212
213 static Suite *
214 gst_iterator_suite (void)
215 {
216   Suite *s = suite_create ("GstIterator");
217   TCase *tc_chain = tcase_create ("correctness");
218
219   tcase_set_timeout (tc_chain, 0);
220
221   suite_add_tcase (s, tc_chain);
222   tcase_add_test (tc_chain, test_manual_iteration);
223   tcase_add_test (tc_chain, test_resync);
224   tcase_add_test (tc_chain, test_fold);
225   tcase_add_test (tc_chain, test_single);
226   return s;
227 }
228
229 GST_CHECK_MAIN (gst_iterator);