3 * unit test for adapter
5 * Copyright (C) <2005> Wim Taymans <wim at fluendo dot com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <gst/check/gstcheck.h>
25 #include <gst/base/gstadapter.h>
27 /* does some implementation dependent checking that should
32 * Start peeking on an adapter with 1 buffer pushed.
34 GST_START_TEST (test_peek1)
39 const guint8 *bufdata, *data1, *data2;
41 adapter = gst_adapter_new ();
42 fail_if (adapter == NULL);
44 /* push single buffer in adapter */
45 buffer = gst_buffer_new_and_alloc (512);
46 bufdata = GST_BUFFER_DATA (buffer);
48 fail_if (buffer == NULL);
49 gst_adapter_push (adapter, buffer);
51 /* available and available_fast should return the size of the
53 avail = gst_adapter_available (adapter);
54 fail_if (avail != 512);
55 avail = gst_adapter_available_fast (adapter);
56 fail_if (avail != 512);
58 /* should g_critical with NULL as result */
59 ASSERT_CRITICAL (data1 = gst_adapter_peek (adapter, 0));
60 fail_if (data1 != NULL);
62 /* should return NULL as result */
63 data1 = gst_adapter_peek (adapter, 513);
64 fail_if (data1 != NULL);
66 /* this should work */
67 data1 = gst_adapter_peek (adapter, 512);
68 fail_if (data1 == NULL);
69 /* it should point to the buffer data as well */
70 fail_if (data1 != bufdata);
71 data2 = gst_adapter_peek (adapter, 512);
72 fail_if (data2 == NULL);
73 /* second peek should return the same pointer */
74 fail_if (data2 != data1);
76 /* this should fail since we don't have that many bytes */
77 ASSERT_CRITICAL (gst_adapter_flush (adapter, 513));
79 /* this should work fine */
80 gst_adapter_flush (adapter, 10);
82 /* see if we have 10 bytes less available */
83 avail = gst_adapter_available (adapter);
84 fail_if (avail != 502);
85 avail = gst_adapter_available_fast (adapter);
86 fail_if (avail != 502);
88 /* should return NULL as result */
89 data2 = gst_adapter_peek (adapter, 503);
90 fail_if (data2 != NULL);
92 /* should work fine */
93 data2 = gst_adapter_peek (adapter, 502);
94 fail_if (data2 == NULL);
95 /* peek should return the same old pointer + 10 */
96 fail_if (data2 != data1 + 10);
97 fail_if (data2 != bufdata + 10);
100 gst_adapter_flush (adapter, 500);
102 /* see if we have 2 bytes available */
103 avail = gst_adapter_available (adapter);
104 fail_if (avail != 2);
105 avail = gst_adapter_available_fast (adapter);
106 fail_if (avail != 2);
108 data2 = gst_adapter_peek (adapter, 2);
109 fail_if (data2 == NULL);
110 fail_if (data2 != data1 + 510);
111 fail_if (data2 != bufdata + 510);
113 /* flush some more */
114 gst_adapter_flush (adapter, 2);
116 /* see if we have 0 bytes available */
117 avail = gst_adapter_available (adapter);
118 fail_if (avail != 0);
119 avail = gst_adapter_available_fast (adapter);
120 fail_if (avail != 0);
122 /* silly clear just for fun */
123 gst_adapter_clear (adapter);
125 g_object_unref (adapter);
130 /* Start peeking on an adapter with 2 non-mergeable buffers
133 GST_START_TEST (test_peek2)
139 /* Start peeking on an adapter with 2 mergeable buffers
142 GST_START_TEST (test_peek3)
148 /* take data from an adapter with 1 buffer pushed.
150 GST_START_TEST (test_take1)
156 /* take data from an adapter with 2 non-mergeable buffers
159 GST_START_TEST (test_take2)
165 /* take data from an adapter with 2 mergeable buffers
168 GST_START_TEST (test_take3)
175 create_and_fill_adapter (void)
180 adapter = gst_adapter_new ();
181 fail_unless (adapter != NULL);
183 for (i = 0; i < 10000; i += 4) {
184 GstBuffer *buf = gst_buffer_new_and_alloc (sizeof (guint32) * 4);
187 fail_unless (buf != NULL);
188 data = GST_BUFFER_DATA (buf);
190 for (j = 0; j < 4; j++) {
191 GST_WRITE_UINT32_LE (data, i + j);
192 data += sizeof (guint32);
194 gst_adapter_push (adapter, buf);
200 /* Fill a buffer with a sequence of 32 bit ints and read them back out,
201 * checking that they're still in the right order */
202 GST_START_TEST (test_take_order)
207 adapter = create_and_fill_adapter ();
208 while (gst_adapter_available (adapter) >= sizeof (guint32)) {
209 guint8 *data = gst_adapter_take (adapter, sizeof (guint32));
211 fail_unless (GST_READ_UINT32_LE (data) == i);
215 fail_unless (gst_adapter_available (adapter) == 0,
216 "Data was left in the adapter");
218 g_object_unref (adapter);
223 /* Fill a buffer with a sequence of 32 bit ints and read them back out
224 * using take_buffer, checking that they're still in the right order */
225 GST_START_TEST (test_take_buf_order)
230 adapter = create_and_fill_adapter ();
231 while (gst_adapter_available (adapter) >= sizeof (guint32)) {
232 GstBuffer *buf = gst_adapter_take_buffer (adapter, sizeof (guint32));
234 fail_unless (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) == i);
237 gst_buffer_unref (buf);
239 fail_unless (gst_adapter_available (adapter) == 0,
240 "Data was left in the adapter");
242 g_object_unref (adapter);
248 gst_adapter_suite (void)
250 Suite *s = suite_create ("adapter");
251 TCase *tc_chain = tcase_create ("general");
253 suite_add_tcase (s, tc_chain);
254 tcase_add_test (tc_chain, test_peek1);
255 tcase_add_test (tc_chain, test_peek2);
256 tcase_add_test (tc_chain, test_peek3);
257 tcase_add_test (tc_chain, test_take1);
258 tcase_add_test (tc_chain, test_take2);
259 tcase_add_test (tc_chain, test_take3);
260 tcase_add_test (tc_chain, test_take_order);
261 tcase_add_test (tc_chain, test_take_buf_order);
266 GST_CHECK_MAIN (gst_adapter);