2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * gstiterator.c: Unit test for iterators
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.
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.
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.
23 #include <gst/check/gstcheck.h>
27 make_list_of_ints (gint n)
32 for (i = 0; i < n; i++)
33 ret = g_list_prepend (ret, GINT_TO_POINTER (i));
35 return g_list_reverse (ret);
38 #define NUM_ELEMENTS 10
40 GST_START_TEST (test_manual_iteration)
46 GstIteratorResult res;
50 l = make_list_of_ints (NUM_ELEMENTS);
53 iter = gst_iterator_new_list (G_TYPE_POINTER, m, &cookie, &l, NULL, NULL);
55 fail_unless (iter != NULL);
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);
66 fail_unless (res == GST_ITERATOR_DONE);
71 g_value_unset (&item);
72 gst_iterator_free (iter);
79 GST_START_TEST (test_resync)
85 GstIteratorResult res;
88 gboolean hacked_list = FALSE;
90 l = make_list_of_ints (NUM_ELEMENTS);
93 iter = gst_iterator_new_list (G_TYPE_POINTER, m, &cookie, &l, NULL, NULL);
95 fail_unless (iter != NULL);
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);
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));
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);
125 g_value_unset (&item);
126 gst_iterator_free (iter);
134 add_fold_func (const GValue * item, GValue * ret, gpointer user_data)
136 g_value_set_int (ret,
137 g_value_get_int (ret) + GPOINTER_TO_INT (g_value_get_pointer (item)));
141 GST_START_TEST (test_fold)
147 GstIteratorResult res;
151 l = make_list_of_ints (NUM_ELEMENTS);
153 iter = gst_iterator_new_list (G_TYPE_POINTER, m, &cookie, &l, NULL, NULL);
154 fail_unless (iter != NULL);
157 for (i = 0; i < NUM_ELEMENTS; i++)
160 g_value_init (&ret, G_TYPE_INT);
161 g_value_set_int (&ret, 0);
163 res = gst_iterator_fold (iter, add_fold_func, &ret, NULL);
165 fail_unless (res == GST_ITERATOR_DONE);
166 fail_unless (g_value_get_int (&ret) == expected);
169 gst_iterator_free (iter);
176 GST_START_TEST (test_single)
179 GstStructure *s = gst_structure_new_empty ("test");
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);
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);
195 fail_unless (gst_iterator_next (it, &v) == GST_ITERATOR_DONE);
196 fail_unless (g_value_get_boxed (&v) == NULL);
198 gst_iterator_free (it);
199 gst_structure_free (s);
201 it = gst_iterator_new_single (GST_TYPE_STRUCTURE, NULL);
203 fail_unless (gst_iterator_next (it, &v) == GST_ITERATOR_DONE);
204 fail_unless (g_value_get_boxed (&v) == NULL);
208 gst_iterator_free (it);
214 gst_iterator_suite (void)
216 Suite *s = suite_create ("GstIterator");
217 TCase *tc_chain = tcase_create ("correctness");
219 tcase_set_timeout (tc_chain, 0);
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);
229 GST_CHECK_MAIN (gst_iterator);