And correct even more valid sparse warnings.
[platform/upstream/gstreamer.git] / tests / check / libs / adapter.c
1 /* GStreamer
2  *
3  * unit test for adapter
4  *
5  * Copyright (C) <2005> Wim Taymans <wim at fluendo dot com>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 #include <gst/base/gstadapter.h>
26
27 /* does some implementation dependent checking that should 
28  * also be optimal 
29  */
30
31 /*
32  * Start peeking on an adapter with 1 buffer pushed. 
33  */
34 GST_START_TEST (test_peek1)
35 {
36   GstAdapter *adapter;
37   GstBuffer *buffer;
38   guint avail;
39   const guint8 *bufdata, *data1, *data2;
40
41   adapter = gst_adapter_new ();
42   fail_if (adapter == NULL);
43
44   /* push single buffer in adapter */
45   buffer = gst_buffer_new_and_alloc (512);
46   bufdata = GST_BUFFER_DATA (buffer);
47
48   fail_if (buffer == NULL);
49   gst_adapter_push (adapter, buffer);
50
51   /* available and available_fast should return the size of the 
52    * buffer */
53   avail = gst_adapter_available (adapter);
54   fail_if (avail != 512);
55   avail = gst_adapter_available_fast (adapter);
56   fail_if (avail != 512);
57
58   /* should g_critical with NULL as result */
59   ASSERT_CRITICAL (data1 = gst_adapter_peek (adapter, 0));
60   fail_if (data1 != NULL);
61
62   /* should return NULL as result */
63   data1 = gst_adapter_peek (adapter, 513);
64   fail_if (data1 != NULL);
65
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);
75
76   /* this should fail since we don't have that many bytes */
77   ASSERT_CRITICAL (gst_adapter_flush (adapter, 513));
78
79   /* this should work fine */
80   gst_adapter_flush (adapter, 10);
81
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);
87
88   /* should return NULL as result */
89   data2 = gst_adapter_peek (adapter, 503);
90   fail_if (data2 != NULL);
91
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);
98
99   /* flush some more */
100   gst_adapter_flush (adapter, 500);
101
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);
107
108   data2 = gst_adapter_peek (adapter, 2);
109   fail_if (data2 == NULL);
110   fail_if (data2 != data1 + 510);
111   fail_if (data2 != bufdata + 510);
112
113   /* flush some more */
114   gst_adapter_flush (adapter, 2);
115
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);
121
122   /* silly clear just for fun */
123   gst_adapter_clear (adapter);
124
125   g_object_unref (adapter);
126 }
127
128 GST_END_TEST;
129
130 /* Start peeking on an adapter with 2 non-mergeable buffers 
131  * pushed. 
132  */
133 GST_START_TEST (test_peek2)
134 {
135 }
136
137 GST_END_TEST;
138
139 /* Start peeking on an adapter with 2 mergeable buffers 
140  * pushed. 
141  */
142 GST_START_TEST (test_peek3)
143 {
144 }
145
146 GST_END_TEST;
147
148 /* take data from an adapter with 1 buffer pushed.
149  */
150 GST_START_TEST (test_take1)
151 {
152 }
153
154 GST_END_TEST;
155
156 /* take data from an adapter with 2 non-mergeable buffers 
157  * pushed.
158  */
159 GST_START_TEST (test_take2)
160 {
161 }
162
163 GST_END_TEST;
164
165 /* take data from an adapter with 2 mergeable buffers 
166  * pushed.
167  */
168 GST_START_TEST (test_take3)
169 {
170 }
171
172 GST_END_TEST;
173
174 static GstAdapter *
175 create_and_fill_adapter (void)
176 {
177   GstAdapter *adapter;
178   gint i, j;
179
180   adapter = gst_adapter_new ();
181   fail_unless (adapter != NULL);
182
183   for (i = 0; i < 10000; i += 4) {
184     GstBuffer *buf = gst_buffer_new_and_alloc (sizeof (guint32) * 4);
185     guint8 *data;
186
187     fail_unless (buf != NULL);
188     data = GST_BUFFER_DATA (buf);
189
190     for (j = 0; j < 4; j++) {
191       GST_WRITE_UINT32_LE (data, i + j);
192       data += sizeof (guint32);
193     }
194     gst_adapter_push (adapter, buf);
195   }
196
197   return adapter;
198 }
199
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)
203 {
204   GstAdapter *adapter;
205   int i = 0;
206
207   adapter = create_and_fill_adapter ();
208   while (gst_adapter_available (adapter) >= sizeof (guint32)) {
209     guint8 *data = gst_adapter_take (adapter, sizeof (guint32));
210
211     fail_unless (GST_READ_UINT32_LE (data) == i);
212     i++;
213     g_free (data);
214   }
215   fail_unless (gst_adapter_available (adapter) == 0,
216       "Data was left in the adapter");
217
218   g_object_unref (adapter);
219 }
220
221 GST_END_TEST;
222
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)
226 {
227   GstAdapter *adapter;
228   int i = 0;
229
230   adapter = create_and_fill_adapter ();
231   while (gst_adapter_available (adapter) >= sizeof (guint32)) {
232     GstBuffer *buf = gst_adapter_take_buffer (adapter, sizeof (guint32));
233
234     fail_unless (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) == i);
235     i++;
236
237     gst_buffer_unref (buf);
238   }
239   fail_unless (gst_adapter_available (adapter) == 0,
240       "Data was left in the adapter");
241
242   g_object_unref (adapter);
243 }
244
245 GST_END_TEST;
246
247 static Suite *
248 gst_adapter_suite (void)
249 {
250   Suite *s = suite_create ("adapter");
251   TCase *tc_chain = tcase_create ("general");
252
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);
262
263   return s;
264 }
265
266 GST_CHECK_MAIN (gst_adapter);