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_INT, m, &cookie, &l, NULL, 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 (item) == i);
65 fail_unless (res == GST_ITERATOR_DONE);
71 gst_iterator_free (iter);
78 GST_START_TEST (test_resync)
84 GstIteratorResult res;
87 gboolean hacked_list = FALSE;
89 l = make_list_of_ints (NUM_ELEMENTS);
92 iter = gst_iterator_new_list (G_TYPE_INT, m, &cookie, &l, NULL, NULL, NULL);
94 fail_unless (iter != NULL);
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);
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));
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);
121 gst_iterator_free (iter);
129 add_fold_func (gpointer item, GValue * ret, gpointer user_data)
131 g_value_set_int (ret, g_value_get_int (ret) + GPOINTER_TO_INT (item));
135 GST_START_TEST (test_fold)
141 GstIteratorResult res;
145 l = make_list_of_ints (NUM_ELEMENTS);
147 iter = gst_iterator_new_list (G_TYPE_INT, m, &cookie, &l, NULL, NULL, NULL);
148 fail_unless (iter != NULL);
151 for (i = 0; i < NUM_ELEMENTS; i++)
154 g_value_init (&ret, G_TYPE_INT);
155 g_value_set_int (&ret, 0);
157 res = gst_iterator_fold (iter, add_fold_func, &ret, NULL);
159 fail_unless (res == GST_ITERATOR_DONE);
160 fail_unless (g_value_get_int (&ret) == expected);
163 gst_iterator_free (iter);
170 GST_START_TEST (test_single)
173 GstStructure *s = gst_structure_new ("test", NULL);
176 it = gst_iterator_new_single (GST_TYPE_STRUCTURE, s,
177 (GstCopyFunction) gst_structure_copy, (GFreeFunc) gst_structure_free);
179 fail_unless (gst_iterator_next (it, (gpointer *) & i) == GST_ITERATOR_OK);
180 fail_unless (strcmp (gst_structure_get_name (s),
181 gst_structure_get_name (i)) == 0);
182 gst_structure_free (i);
184 fail_unless (gst_iterator_next (it, (gpointer *) & i) == GST_ITERATOR_DONE);
185 fail_unless (i == NULL);
187 gst_iterator_free (it);
188 gst_structure_free (s);
190 it = gst_iterator_new_single (GST_TYPE_STRUCTURE, NULL,
191 (GstCopyFunction) gst_structure_copy, (GFreeFunc) gst_structure_free);
193 fail_unless (gst_iterator_next (it, (gpointer *) & i) == GST_ITERATOR_DONE);
194 fail_unless (i == NULL);
196 gst_iterator_free (it);
202 gst_iterator_suite (void)
204 Suite *s = suite_create ("GstIterator");
205 TCase *tc_chain = tcase_create ("correctness");
207 tcase_set_timeout (tc_chain, 0);
209 suite_add_tcase (s, tc_chain);
210 tcase_add_test (tc_chain, test_manual_iteration);
211 tcase_add_test (tc_chain, test_resync);
212 tcase_add_test (tc_chain, test_fold);
213 tcase_add_test (tc_chain, test_single);
217 GST_CHECK_MAIN (gst_iterator);