base: make GstQueueArray private to coreelements for now
[platform/upstream/gstreamer.git] / tests / check / libs / queuearray.c
1 /* GStreamer
2  *
3  * unit test for GstQueueArray
4  *
5  * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28 #include <gst/check/gstcheck.h>
29 #include "../../../plugins/elements/gstqueuearray.h"
30 #include "../../../plugins/elements/gstqueuearray.c"
31
32 /* Simplest test
33  * Initial size : 10
34  * Add 10, Remove 10
35  */
36 GST_START_TEST (test_array_1)
37 {
38   GstQueueArray *array;
39   guint i;
40
41   /* Create an array of initial size 10 */
42   array = gst_queue_array_new (10);
43
44   /* push 5 values in */
45   for (i = 0; i < 5; i++)
46     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
47
48   fail_unless_equals_int (array->length, 5);
49
50   /* pull 5 values out */
51   for (i = 0; i < 5; i++) {
52     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
53         i);
54   }
55
56   fail_unless_equals_int (array->length, 0);
57
58   gst_queue_array_free (array);
59 }
60
61 GST_END_TEST;
62
63 GST_START_TEST (test_array_grow)
64 {
65   GstQueueArray *array;
66   guint i;
67
68   /* Create an array of initial size 10 */
69   array = gst_queue_array_new (10);
70   fail_unless_equals_int (array->size, 10);
71
72   /* push 10 values in */
73   for (i = 0; i < 10; i++)
74     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
75
76   fail_unless_equals_int (array->length, 10);
77   /* It did not grow beyond initial size */
78   fail_unless_equals_int (array->size, 10);
79   /* The head is still at the beginning */
80   fail_unless_equals_int (array->head, 0);
81   /* The tail wrapped around to the head */
82   fail_unless_equals_int (array->tail, 0);
83
84
85   /* If we add one value, it will grow */
86   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
87
88   fail_unless_equals_int (array->length, 11);
89   /* It did grow beyond initial size */
90   fail_unless_equals_int (array->size, 15);
91   /* The head remains the same */
92   fail_unless_equals_int (array->head, 0);
93   /* The tail was brought to position 11 */
94   fail_unless_equals_int (array->tail, 11);
95
96   /* pull the 11 values out */
97   for (i = 0; i < 11; i++) {
98     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
99         i);
100   }
101
102   fail_unless_equals_int (array->length, 0);
103   gst_queue_array_free (array);
104 }
105
106 GST_END_TEST;
107
108 GST_START_TEST (test_array_grow_multiple)
109 {
110   GstQueueArray *array;
111   guint i;
112
113   /* Create an array of initial size 10 */
114   array = gst_queue_array_new (10);
115   fail_unless_equals_int (array->size, 10);
116
117   /* push 11 values in */
118   for (i = 0; i < 11; i++)
119     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
120
121   /* With 11 values, it should have grown once (15) */
122   fail_unless_equals_int (array->length, 11);
123   fail_unless_equals_int (array->size, 15);
124
125   for (i = 11; i < 20; i++)
126     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
127
128   /* With 20 total values, it should have grown another time (3 * 15) / 2 = 22) */
129   fail_unless_equals_int (array->length, 20);
130   /* It did grow beyond initial size */
131   fail_unless_equals_int (array->size, 22);
132
133   /* pull the 20 values out */
134   for (i = 0; i < 20; i++) {
135     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
136         i);
137   }
138
139   fail_unless_equals_int (array->length, 0);
140   gst_queue_array_free (array);
141 }
142
143 GST_END_TEST;
144
145 GST_START_TEST (test_array_grow_middle)
146 {
147   GstQueueArray *array;
148   guint i;
149
150   /* Create an array of initial size 10 */
151   array = gst_queue_array_new (10);
152   fail_unless_equals_int (array->size, 10);
153
154   /* push/pull 5 values to end up in the middle */
155   for (i = 0; i < 5; i++) {
156     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
157     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
158         i);
159   }
160
161   /* push 10 values in */
162   for (i = 0; i < 10; i++)
163     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
164
165   fail_unless_equals_int (array->length, 10);
166   /* It did not grow beyond initial size */
167   fail_unless_equals_int (array->size, 10);
168
169   /* If we add one value, it will grow */
170   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
171   fail_unless_equals_int (array->length, 11);
172   /* It did grow beyond initial size */
173   fail_unless_equals_int (array->size, 15);
174
175   /* pull the 11 values out */
176   for (i = 0; i < 11; i++) {
177     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
178         i);
179   }
180
181   fail_unless_equals_int (array->length, 0);
182   gst_queue_array_free (array);
183 }
184
185 GST_END_TEST;
186
187 GST_START_TEST (test_array_grow_end)
188 {
189   GstQueueArray *array;
190   guint i;
191
192   /* Create an array of initial size 10 */
193   array = gst_queue_array_new (10);
194   fail_unless_equals_int (array->size, 10);
195
196   /* push/pull 9 values to end up at the last position */
197   for (i = 0; i < 9; i++) {
198     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
199     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
200         i);
201   }
202
203   /* push 10 values in */
204   for (i = 0; i < 10; i++)
205     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
206
207   fail_unless_equals_int (array->length, 10);
208   /* It did not grow beyond initial size */
209   fail_unless_equals_int (array->size, 10);
210
211   /* If we add one value, it will grow */
212   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
213   fail_unless_equals_int (array->length, 11);
214   /* It did grow beyond initial size */
215   fail_unless_equals_int (array->size, 15);
216
217   /* pull the 11 values out */
218   for (i = 0; i < 11; i++) {
219     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
220         i);
221   }
222
223   fail_unless_equals_int (array->length, 0);
224   gst_queue_array_free (array);
225 }
226
227 GST_END_TEST;
228
229 static Suite *
230 gst_queue_array_suite (void)
231 {
232   Suite *s = suite_create ("GstQueueArray");
233   TCase *tc_chain = tcase_create ("general");
234
235   suite_add_tcase (s, tc_chain);
236
237   tcase_add_test (tc_chain, test_array_1);
238   tcase_add_test (tc_chain, test_array_grow);
239   tcase_add_test (tc_chain, test_array_grow_multiple);
240   tcase_add_test (tc_chain, test_array_grow_middle);
241   tcase_add_test (tc_chain, test_array_grow_end);
242
243   return s;
244 }
245
246
247 GST_CHECK_MAIN (gst_queue_array);