whitespace fixes
[platform/upstream/gstreamer.git] / check / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * gstcaps.c: Unit test for GstCaps
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
24 #include <gst/check/gstcheck.h>
25 #include "capslist.h"
26
27 GST_START_TEST (test_from_string)
28 {
29   GstCaps *caps;
30   int i;
31
32   for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
33     caps = gst_caps_from_string (caps_list[i]);
34     fail_if (caps == NULL,
35         "Could not create caps from string %s\n", caps_list[i]);
36     g_free (caps);
37   }
38 }
39
40 GST_END_TEST;
41
42 GST_START_TEST (test_buffer)
43 {
44   GstCaps *c1;
45   GstBuffer *buffer;
46
47   buffer = gst_buffer_new_and_alloc (1000);
48   c1 = gst_caps_new_simple ("audio/x-raw-int",
49       "buffer", GST_TYPE_BUFFER, buffer, NULL);
50
51   GST_DEBUG ("caps: %" GST_PTR_FORMAT, c1);
52
53   gst_buffer_set_caps (buffer, c1);     /* gives away our c1 ref */
54   gst_buffer_unref (buffer);
55 }
56
57 GST_END_TEST;
58
59 GST_START_TEST (test_double_append)
60 {
61   GstStructure *s1;
62   GstCaps *c1;
63
64   c1 = gst_caps_new_any ();
65   s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
66   gst_caps_append_structure (c1, s1);
67   ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
68
69   gst_caps_unref (c1);
70 }
71
72 GST_END_TEST;
73
74 GST_START_TEST (test_mutability)
75 {
76   GstStructure *s1;
77   GstCaps *c1;
78   gint ret;
79
80   c1 = gst_caps_new_any ();
81   s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
82   gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
83   gst_caps_append_structure (c1, s1);
84   gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
85   gst_caps_ref (c1);
86   ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
87   fail_unless (gst_structure_get_int (s1, "rate", &ret));
88   fail_unless (ret == 22500);
89   ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
90   fail_unless (gst_structure_get_int (s1, "rate", &ret));
91   fail_unless (ret == 22500);
92   gst_caps_unref (c1);
93   gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
94   fail_unless (gst_structure_get_int (s1, "rate", &ret));
95   fail_unless (ret == 11250);
96   gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
97   fail_unless (gst_structure_get_int (s1, "rate", &ret));
98   fail_unless (ret == 1);
99   gst_caps_unref (c1);
100 }
101
102 GST_END_TEST;
103
104 GST_START_TEST (test_static_caps)
105 {
106   GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw-int,rate=44100");
107   GstCaps *caps1;
108   GstCaps *caps2;
109
110   /* caps creation */
111   caps1 = gst_static_caps_get (&scaps);
112   fail_unless (caps1 != NULL);
113   /* 1 refcount core, one from us */
114   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
115
116   /* caps should be the same */
117   caps2 = gst_static_caps_get (&scaps);
118   fail_unless (caps2 != NULL);
119   /* 1 refcount core, two from us */
120   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
121   /* caps must be equal */
122   fail_unless (caps1 == caps2);
123
124   gst_caps_unref (caps1);
125   gst_caps_unref (caps2);
126 }
127
128 GST_END_TEST;
129
130 static const gchar non_simple_caps_string[] =
131     "video/x-raw-yuv, format=(fourcc)I420, framerate=(double)[ 1, 100 ], "
132     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
133     "format=(fourcc)YUY2, framerate=(double)[ 1, 100 ], width=(int)[ 16, 4096 ], "
134     "height=(int)[ 16, 4096 ]; video/x-raw-rgb, bpp=(int)8, depth=(int)8, "
135     "endianness=(int)1234, framerate=(double)[ 1, 100 ], width=(int)[ 16, 4096 ], "
136     "height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
137     "format=(fourcc){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
138     "height=(int)[ 16, 4096 ], framerate=(double)[ 1, 100 ]";
139
140 static gboolean
141 check_fourcc_list (const GValue * format_value)
142 {
143   const GValue *fourcc_value;
144   gboolean got_yv12 = FALSE;
145   gboolean got_i420 = FALSE;
146   gboolean got_yuy2 = FALSE;
147   guint32 fourcc;
148
149   fourcc_value = gst_value_list_get_value (format_value, 0);
150   fail_unless (fourcc_value != NULL);
151   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
152   fourcc = gst_value_get_fourcc (fourcc_value);
153   fail_unless (fourcc != 0);
154   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
155   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
156   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
157
158   fourcc_value = gst_value_list_get_value (format_value, 1);
159   fail_unless (fourcc_value != NULL);
160   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
161   fourcc = gst_value_get_fourcc (fourcc_value);
162   fail_unless (fourcc != 0);
163   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
164   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
165   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
166
167   fourcc_value = gst_value_list_get_value (format_value, 2);
168   fail_unless (fourcc_value != NULL);
169   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
170   fourcc = gst_value_get_fourcc (fourcc_value);
171   fail_unless (fourcc != 0);
172   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
173   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
174   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
175
176   return (got_i420 && got_yuy2 && got_yv12);
177 }
178
179 GST_START_TEST (test_simplify)
180 {
181   GstStructure *s1, *s2;
182   gboolean did_simplify;
183   GstCaps *caps;
184
185   caps = gst_caps_from_string (non_simple_caps_string);
186   fail_unless (caps != NULL,
187       "gst_caps_from_string (non_simple_caps_string) failed");
188
189   did_simplify = gst_caps_do_simplify (caps);
190   fail_unless (did_simplify == TRUE,
191       "gst_caps_do_simplify() should have worked");
192
193   /* check simplified caps, should be:
194    *
195    * video/x-raw-rgb, bpp=(int)8, depth=(int)8, endianness=(int)1234,
196    *     framerate=(double)[ 1, 100 ], width=(int)[ 16, 4096 ],
197    *     height=(int)[ 16, 4096 ];
198    * video/x-raw-yuv, format=(fourcc){ YV12, YUY2, I420 },
199    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
200    *     framerate=(double)[ 1, 100 ]
201    */
202   fail_unless (gst_caps_get_size (caps) == 2);
203   s1 = gst_caps_get_structure (caps, 0);
204   s2 = gst_caps_get_structure (caps, 1);
205   fail_unless (s1 != NULL);
206   fail_unless (s2 != NULL);
207
208   if (!gst_structure_has_name (s1, "video/x-raw-rgb")) {
209     GstStructure *tmp;
210
211     tmp = s1;
212     s1 = s2;
213     s2 = tmp;
214   }
215
216   fail_unless (gst_structure_has_name (s1, "video/x-raw-rgb"));
217   {
218     const GValue *framerate_value;
219     const GValue *width_value;
220     const GValue *height_value;
221     gdouble min_fps, max_fps;
222     gint bpp, depth, endianness;
223     gint min_width, max_width;
224     gint min_height, max_height;
225
226     fail_unless (gst_structure_get_int (s1, "bpp", &bpp));
227     fail_unless (bpp == 8);
228
229     fail_unless (gst_structure_get_int (s1, "depth", &depth));
230     fail_unless (depth == 8);
231
232     fail_unless (gst_structure_get_int (s1, "endianness", &endianness));
233     fail_unless (endianness == G_LITTLE_ENDIAN);
234
235     framerate_value = gst_structure_get_value (s1, "framerate");
236     fail_unless (framerate_value != NULL);
237     fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (framerate_value));
238     min_fps = gst_value_get_double_range_min (framerate_value);
239     max_fps = gst_value_get_double_range_max (framerate_value);
240     fail_unless (min_fps == 1.0 && max_fps == 100.0);
241
242     width_value = gst_structure_get_value (s1, "width");
243     fail_unless (width_value != NULL);
244     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
245     min_width = gst_value_get_int_range_min (width_value);
246     max_width = gst_value_get_int_range_max (width_value);
247     fail_unless (min_width == 16 && max_width == 4096);
248
249     height_value = gst_structure_get_value (s1, "height");
250     fail_unless (height_value != NULL);
251     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
252     min_height = gst_value_get_int_range_min (height_value);
253     max_height = gst_value_get_int_range_max (height_value);
254     fail_unless (min_height == 16 && max_height == 4096);
255   }
256
257   fail_unless (gst_structure_has_name (s2, "video/x-raw-yuv"));
258   {
259     const GValue *framerate_value;
260     const GValue *format_value;
261     const GValue *width_value;
262     const GValue *height_value;
263     gdouble min_fps, max_fps;
264     gint min_width, max_width;
265     gint min_height, max_height;
266
267     format_value = gst_structure_get_value (s2, "format");
268     fail_unless (format_value != NULL);
269     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
270     fail_unless (gst_value_list_get_size (format_value) == 3);
271     fail_unless (check_fourcc_list (format_value) == TRUE);
272
273     framerate_value = gst_structure_get_value (s2, "framerate");
274     fail_unless (framerate_value != NULL);
275     fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (framerate_value));
276     min_fps = gst_value_get_double_range_min (framerate_value);
277     max_fps = gst_value_get_double_range_max (framerate_value);
278     fail_unless (min_fps == 1.0 && max_fps == 100.0);
279
280     width_value = gst_structure_get_value (s2, "width");
281     fail_unless (width_value != NULL);
282     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
283     min_width = gst_value_get_int_range_min (width_value);
284     max_width = gst_value_get_int_range_max (width_value);
285     fail_unless (min_width == 16 && max_width == 4096);
286
287     height_value = gst_structure_get_value (s2, "height");
288     fail_unless (height_value != NULL);
289     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
290     min_height = gst_value_get_int_range_min (height_value);
291     max_height = gst_value_get_int_range_max (height_value);
292     fail_unless (min_height == 16 && max_height == 4096);
293   }
294
295   gst_caps_unref (caps);
296 }
297
298 GST_END_TEST;
299
300 Suite *
301 gst_caps_suite (void)
302 {
303   Suite *s = suite_create ("GstCaps");
304   TCase *tc_chain = tcase_create ("mutability");
305
306   suite_add_tcase (s, tc_chain);
307   tcase_add_test (tc_chain, test_from_string);
308   tcase_add_test (tc_chain, test_double_append);
309   tcase_add_test (tc_chain, test_mutability);
310   tcase_add_test (tc_chain, test_buffer);
311   tcase_add_test (tc_chain, test_static_caps);
312   tcase_add_test (tc_chain, test_simplify);
313
314   return s;
315 }
316
317 int
318 main (int argc, char **argv)
319 {
320   int nf;
321
322   Suite *s = gst_caps_suite ();
323   SRunner *sr = srunner_create (s);
324
325   gst_check_init (&argc, &argv);
326
327   srunner_run_all (sr, CK_NORMAL);
328   nf = srunner_ntests_failed (sr);
329   srunner_free (sr);
330
331   return nf;
332 }