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 "../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 (m, &cookie, &l, NULL, NULL, NULL);
55 g_return_if_fail (iter != NULL);
58 res = gst_iterator_next (iter, &item);
59 if (i < NUM_ELEMENTS) {
60 g_return_if_fail (res == GST_ITERATOR_OK);
61 g_return_if_fail (GPOINTER_TO_INT (item) == i);
65 g_return_if_fail (res == GST_ITERATOR_DONE);
70 gst_iterator_free (iter);
74 GST_START_TEST (test_resync)
80 GstIteratorResult res;
83 gboolean hacked_list = FALSE;
85 l = make_list_of_ints (NUM_ELEMENTS);
88 iter = gst_iterator_new_list (m, &cookie, &l, NULL, NULL, NULL);
90 g_return_if_fail (iter != NULL);
93 res = gst_iterator_next (iter, &item);
94 if (i < NUM_ELEMENTS / 2) {
95 g_return_if_fail (res == GST_ITERATOR_OK);
96 g_return_if_fail (GPOINTER_TO_INT (item) == i);
99 } else if (!hacked_list) {
100 /* here's where we test resync */
101 g_return_if_fail (res == GST_ITERATOR_OK);
102 l = g_list_prepend (l, GINT_TO_POINTER (-1));
107 g_return_if_fail (res == GST_ITERATOR_RESYNC);
108 gst_iterator_resync (iter);
109 res = gst_iterator_next (iter, &item);
110 g_return_if_fail (res == GST_ITERATOR_OK);
111 g_return_if_fail (GPOINTER_TO_INT (item) == -1);
116 gst_iterator_free (iter);
118 GST_END_TEST static gboolean
119 add_fold_func (gpointer item, GValue * ret, gpointer user_data)
121 g_value_set_int (ret, g_value_get_int (ret) + GPOINTER_TO_INT (item));
125 GST_START_TEST (test_fold)
131 GstIteratorResult res;
135 l = make_list_of_ints (NUM_ELEMENTS);
137 iter = gst_iterator_new_list (m, &cookie, &l, NULL, NULL, NULL);
138 g_return_if_fail (iter != NULL);
141 for (i = 0; i < NUM_ELEMENTS; i++)
144 g_value_init (&ret, G_TYPE_INT);
145 g_value_set_int (&ret, 0);
147 res = gst_iterator_fold (iter, add_fold_func, &ret, NULL);
149 g_return_if_fail (res == GST_ITERATOR_DONE);
150 g_return_if_fail (g_value_get_int (&ret) == expected);
153 gstiterator_suite (void)
155 Suite *s = suite_create ("GstIterator");
156 TCase *tc_chain = tcase_create ("correctness");
158 tcase_set_timeout (tc_chain, 0);
160 suite_add_tcase (s, tc_chain);
161 tcase_add_test (tc_chain, test_manual_iteration);
162 tcase_add_test (tc_chain, test_resync);
163 tcase_add_test (tc_chain, test_fold);
168 main (int argc, char **argv)
172 Suite *s = gstiterator_suite ();
173 SRunner *sr = srunner_create (s);
175 gst_check_init (&argc, &argv);
177 srunner_run_all (sr, CK_NORMAL);
178 nf = srunner_ntests_failed (sr);