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);
171 gst_iterator_suite (void)
173 Suite *s = suite_create ("GstIterator");
174 TCase *tc_chain = tcase_create ("correctness");
176 tcase_set_timeout (tc_chain, 0);
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);
185 GST_CHECK_MAIN (gst_iterator);