ab18800fec912ee7800abb476687269f742763ea
[platform/upstream/gstreamer.git] / tests / 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 <gst/gstcaps.h>
26 #include "capslist.h"
27
28 GST_START_TEST (test_from_string)
29 {
30   GstCaps *caps;
31   GstCaps *caps2;
32   gchar *to_str;
33   int i;
34
35   for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
36     caps = gst_caps_from_string (caps_list[i]);
37     fail_if (caps == NULL,
38         "Could not create caps from string %s\n", caps_list[i]);
39     to_str = gst_caps_to_string (caps);
40     fail_if (to_str == NULL,
41         "Could not convert caps back to string %s\n", caps_list[i]);
42     caps2 = gst_caps_from_string (caps_list[i]);
43     fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);
44
45     fail_unless (gst_caps_is_equal (caps, caps));
46     fail_unless (gst_caps_is_equal (caps, caps2));
47
48     gst_caps_unref (caps);
49     gst_caps_unref (caps2);
50     g_free (to_str);
51   }
52 }
53
54 GST_END_TEST;
55
56 GST_START_TEST (test_double_append)
57 {
58   GstStructure *s1;
59   GstCaps *c1;
60
61   c1 = gst_caps_new_any ();
62   s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
63   gst_caps_append_structure (c1, s1);
64   ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
65
66   gst_caps_unref (c1);
67 }
68
69 GST_END_TEST;
70
71 GST_START_TEST (test_mutability)
72 {
73   GstStructure *s1;
74   GstCaps *c1;
75   gint ret;
76
77   c1 = gst_caps_new_any ();
78   s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
79   gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
80   gst_caps_append_structure (c1, s1);
81   gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
82   gst_caps_ref (c1);
83   ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
84   fail_unless (gst_structure_get_int (s1, "rate", &ret));
85   fail_unless (ret == 22500);
86   ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
87   fail_unless (gst_structure_get_int (s1, "rate", &ret));
88   fail_unless (ret == 22500);
89   gst_caps_unref (c1);
90   gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
91   fail_unless (gst_structure_get_int (s1, "rate", &ret));
92   fail_unless (ret == 11250);
93   gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
94   fail_unless (gst_structure_get_int (s1, "rate", &ret));
95   fail_unless (ret == 1);
96   gst_caps_unref (c1);
97 }
98
99 GST_END_TEST;
100
101 GST_START_TEST (test_static_caps)
102 {
103   static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw-int,rate=44100");
104   GstCaps *caps1;
105   GstCaps *caps2;
106
107   /* caps creation */
108   caps1 = gst_static_caps_get (&scaps);
109   fail_unless (caps1 != NULL);
110   /* 1 refcount core, one from us */
111   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
112
113   /* caps should be the same */
114   caps2 = gst_static_caps_get (&scaps);
115   fail_unless (caps2 != NULL);
116   /* 1 refcount core, two from us */
117   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
118   /* caps must be equal */
119   fail_unless (caps1 == caps2);
120
121   gst_caps_unref (caps1);
122   gst_caps_unref (caps2);
123 }
124
125 GST_END_TEST;
126
127 static const gchar non_simple_caps_string[] =
128     "video/x-raw-yuv, format=(fourcc)I420, framerate=(fraction)[ 1/100, 100 ], "
129     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
130     "format=(fourcc)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
131     "height=(int)[ 16, 4096 ]; video/x-raw-rgb, bpp=(int)8, depth=(int)8, "
132     "endianness=(int)1234, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
133     "height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
134     "format=(fourcc){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
135     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
136
137 static gboolean
138 check_fourcc_list (const GValue * format_value)
139 {
140   const GValue *fourcc_value;
141   gboolean got_yv12 = FALSE;
142   gboolean got_i420 = FALSE;
143   gboolean got_yuy2 = FALSE;
144   guint32 fourcc;
145
146   fourcc_value = gst_value_list_get_value (format_value, 0);
147   fail_unless (fourcc_value != NULL);
148   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
149   fourcc = gst_value_get_fourcc (fourcc_value);
150   fail_unless (fourcc != 0);
151   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
152   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
153   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
154
155   fourcc_value = gst_value_list_get_value (format_value, 1);
156   fail_unless (fourcc_value != NULL);
157   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
158   fourcc = gst_value_get_fourcc (fourcc_value);
159   fail_unless (fourcc != 0);
160   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
161   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
162   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
163
164   fourcc_value = gst_value_list_get_value (format_value, 2);
165   fail_unless (fourcc_value != NULL);
166   fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
167   fourcc = gst_value_get_fourcc (fourcc_value);
168   fail_unless (fourcc != 0);
169   got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
170   got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
171   got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
172
173   return (got_i420 && got_yuy2 && got_yv12);
174 }
175
176 GST_START_TEST (test_simplify)
177 {
178   GstStructure *s1, *s2;
179   gboolean did_simplify;
180   GstCaps *caps;
181
182   caps = gst_caps_from_string (non_simple_caps_string);
183   fail_unless (caps != NULL,
184       "gst_caps_from_string (non_simple_caps_string) failed");
185
186   did_simplify = gst_caps_do_simplify (caps);
187   fail_unless (did_simplify == TRUE,
188       "gst_caps_do_simplify() should have worked");
189
190   /* check simplified caps, should be:
191    *
192    * video/x-raw-rgb, bpp=(int)8, depth=(int)8, endianness=(int)1234,
193    *     framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ],
194    *     height=(int)[ 16, 4096 ];
195    * video/x-raw-yuv, format=(fourcc){ YV12, YUY2, I420 },
196    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
197    *     framerate=(fraction)[ 1/100, 100 ]
198    */
199   fail_unless (gst_caps_get_size (caps) == 2);
200   s1 = gst_caps_get_structure (caps, 0);
201   s2 = gst_caps_get_structure (caps, 1);
202   fail_unless (s1 != NULL);
203   fail_unless (s2 != NULL);
204
205   if (!gst_structure_has_name (s1, "video/x-raw-rgb")) {
206     GstStructure *tmp;
207
208     tmp = s1;
209     s1 = s2;
210     s2 = tmp;
211   }
212
213   fail_unless (gst_structure_has_name (s1, "video/x-raw-rgb"));
214   {
215     const GValue *framerate_value;
216     const GValue *width_value;
217     const GValue *height_value;
218     const GValue *val_fps;
219     GValue test_fps = { 0, };
220     gint bpp, depth, endianness;
221     gint min_width, max_width;
222     gint min_height, max_height;
223
224     fail_unless (gst_structure_get_int (s1, "bpp", &bpp));
225     fail_unless (bpp == 8);
226
227     fail_unless (gst_structure_get_int (s1, "depth", &depth));
228     fail_unless (depth == 8);
229
230     fail_unless (gst_structure_get_int (s1, "endianness", &endianness));
231     fail_unless (endianness == G_LITTLE_ENDIAN);
232
233     g_value_init (&test_fps, GST_TYPE_FRACTION);
234     framerate_value = gst_structure_get_value (s1, "framerate");
235     fail_unless (framerate_value != NULL);
236     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
237
238     val_fps = gst_value_get_fraction_range_min (framerate_value);
239     gst_value_set_fraction (&test_fps, 1, 100);
240     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
241
242     val_fps = gst_value_get_fraction_range_max (framerate_value);
243     gst_value_set_fraction (&test_fps, 100, 1);
244     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
245
246     g_value_unset (&test_fps);
247
248     width_value = gst_structure_get_value (s1, "width");
249     fail_unless (width_value != NULL);
250     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
251     min_width = gst_value_get_int_range_min (width_value);
252     max_width = gst_value_get_int_range_max (width_value);
253     fail_unless (min_width == 16 && max_width == 4096);
254
255     height_value = gst_structure_get_value (s1, "height");
256     fail_unless (height_value != NULL);
257     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
258     min_height = gst_value_get_int_range_min (height_value);
259     max_height = gst_value_get_int_range_max (height_value);
260     fail_unless (min_height == 16 && max_height == 4096);
261   }
262
263   fail_unless (gst_structure_has_name (s2, "video/x-raw-yuv"));
264   {
265     const GValue *framerate_value;
266     const GValue *format_value;
267     const GValue *width_value;
268     const GValue *height_value;
269     const GValue *val_fps;
270     GValue test_fps = { 0, };
271     gint min_width, max_width;
272     gint min_height, max_height;
273
274     format_value = gst_structure_get_value (s2, "format");
275     fail_unless (format_value != NULL);
276     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
277     fail_unless (gst_value_list_get_size (format_value) == 3);
278     fail_unless (check_fourcc_list (format_value) == TRUE);
279
280     g_value_init (&test_fps, GST_TYPE_FRACTION);
281     framerate_value = gst_structure_get_value (s2, "framerate");
282     fail_unless (framerate_value != NULL);
283     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
284
285     val_fps = gst_value_get_fraction_range_min (framerate_value);
286     gst_value_set_fraction (&test_fps, 1, 100);
287     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
288
289     val_fps = gst_value_get_fraction_range_max (framerate_value);
290     gst_value_set_fraction (&test_fps, 100, 1);
291     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
292
293     g_value_unset (&test_fps);
294
295     width_value = gst_structure_get_value (s2, "width");
296     fail_unless (width_value != NULL);
297     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
298     min_width = gst_value_get_int_range_min (width_value);
299     max_width = gst_value_get_int_range_max (width_value);
300     fail_unless (min_width == 16 && max_width == 4096);
301
302     height_value = gst_structure_get_value (s2, "height");
303     fail_unless (height_value != NULL);
304     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
305     min_height = gst_value_get_int_range_min (height_value);
306     max_height = gst_value_get_int_range_max (height_value);
307     fail_unless (min_height == 16 && max_height == 4096);
308   }
309
310   gst_caps_unref (caps);
311 }
312
313 GST_END_TEST;
314
315 GST_START_TEST (test_truncate)
316 {
317   GstCaps *caps;
318
319   caps = gst_caps_from_string (non_simple_caps_string);
320   fail_unless (caps != NULL,
321       "gst_caps_from_string (non_simple_caps_string) failed");
322   fail_unless_equals_int (gst_caps_get_size (caps), 4);
323   gst_caps_truncate (caps);
324   fail_unless_equals_int (gst_caps_get_size (caps), 1);
325   gst_caps_unref (caps);
326 }
327
328 GST_END_TEST;
329
330 GST_START_TEST (test_subset)
331 {
332   GstCaps *c1, *c2;
333
334   c1 = gst_caps_from_string ("video/x-raw-yuv; video/x-raw-rgb");
335   c2 = gst_caps_from_string ("video/x-raw-yuv, format=(fourcc)YUY2");
336   fail_unless (gst_caps_is_subset (c2, c1));
337   fail_if (gst_caps_is_subset (c1, c2));
338   gst_caps_unref (c1);
339   gst_caps_unref (c2);
340
341   c1 = gst_caps_from_string
342       ("audio/x-raw-int, channels=(int)[ 1, 2 ], rate=(int)44100");
343   c2 = gst_caps_from_string
344       ("audio/x-raw-int, channels=(int)1, rate=(int)44100");
345   fail_unless (gst_caps_is_subset (c2, c1));
346   fail_if (gst_caps_is_subset (c1, c2));
347   gst_caps_unref (c1);
348   gst_caps_unref (c2);
349
350   c1 = gst_caps_from_string ("audio/x-raw-int, channels=(int) {1}");
351   c2 = gst_caps_from_string ("audio/x-raw-int, channels=(int)1");
352   fail_unless (gst_caps_is_subset (c2, c1));
353   fail_unless (gst_caps_is_subset (c1, c2));
354   fail_unless (gst_caps_is_equal (c1, c2));
355   gst_caps_unref (c1);
356   gst_caps_unref (c2);
357
358   c1 = gst_caps_from_string
359       ("audio/x-raw-int, rate=(int)44100, channels=(int)3, endianness=(int)1234, width=(int)16, depth=(int)16, signed=(boolean)false");
360   c2 = gst_caps_from_string
361       ("audio/x-raw-int, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], endianness=(int){ 1234, 4321 }, width=(int)16, depth=(int)[ 1, 16 ], signed=(boolean){ true, false }");
362   fail_unless (gst_caps_is_subset (c1, c2));
363   fail_if (gst_caps_is_subset (c2, c1));
364   gst_caps_unref (c1);
365   gst_caps_unref (c2);
366 }
367
368 GST_END_TEST;
369
370 GST_START_TEST (test_merge_fundamental)
371 {
372   GstCaps *c1, *c2;
373
374   /* ANY + specific = ANY */
375   c1 = gst_caps_from_string ("audio/x-raw-int,rate=44100");
376   c2 = gst_caps_new_any ();
377   gst_caps_merge (c2, c1);
378   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
379   fail_unless (gst_caps_get_size (c2) == 0, NULL);
380   fail_unless (gst_caps_is_any (c2), NULL);
381   gst_caps_unref (c2);
382
383   /* specific + ANY = ANY */
384   c2 = gst_caps_from_string ("audio/x-raw-int,rate=44100");
385   c1 = gst_caps_new_any ();
386   gst_caps_merge (c2, c1);
387   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
388   fail_unless (gst_caps_get_size (c2) == 0, NULL);
389   fail_unless (gst_caps_is_any (c2), NULL);
390   gst_caps_unref (c2);
391
392   /* EMPTY + specific = specific */
393   c1 = gst_caps_from_string ("audio/x-raw-int,rate=44100");
394   c2 = gst_caps_new_empty ();
395   gst_caps_merge (c2, c1);
396   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
397   fail_unless (gst_caps_get_size (c2) == 1, NULL);
398   fail_if (gst_caps_is_empty (c2), NULL);
399   gst_caps_unref (c2);
400
401   /* specific + EMPTY = specific */
402   c2 = gst_caps_from_string ("audio/x-raw-int,rate=44100");
403   c1 = gst_caps_new_empty ();
404   gst_caps_merge (c2, c1);
405   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
406   fail_unless (gst_caps_get_size (c2) == 1, NULL);
407   fail_if (gst_caps_is_empty (c2), NULL);
408   gst_caps_unref (c2);
409 }
410
411 GST_END_TEST;
412
413 GST_START_TEST (test_merge_same)
414 {
415   GstCaps *c1, *c2, *test;
416
417   /* this is the same */
418   c1 = gst_caps_from_string ("audio/x-raw-int,rate=44100,channels=1");
419   c2 = gst_caps_from_string ("audio/x-raw-int,rate=44100,channels=1");
420   gst_caps_merge (c2, c1);
421   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
422   fail_unless (gst_caps_get_size (c2) == 1, NULL);
423   test = gst_caps_from_string ("audio/x-raw-int,rate=44100,channels=1");
424   fail_unless (gst_caps_is_equal (c2, test));
425   gst_caps_unref (test);
426   gst_caps_unref (c2);
427
428   /* and so is this */
429   c1 = gst_caps_from_string ("audio/x-raw-int,rate=44100,channels=1");
430   c2 = gst_caps_from_string ("audio/x-raw-int,channels=1,rate=44100");
431   gst_caps_merge (c2, c1);
432   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
433   fail_unless (gst_caps_get_size (c2) == 1, NULL);
434   gst_caps_unref (c2);
435
436   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
437   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
438   gst_caps_merge (c2, c1);
439   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
440   fail_unless (gst_caps_get_size (c2) == 2, NULL);
441   gst_caps_unref (c2);
442
443   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
444   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
445   gst_caps_merge (c2, c1);
446   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
447   fail_unless (gst_caps_get_size (c2) == 2, NULL);
448   gst_caps_unref (c2);
449
450   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
451   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
452   gst_caps_merge (c2, c1);
453   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
454   fail_unless (gst_caps_get_size (c2) == 1, NULL);
455   gst_caps_unref (c2);
456
457   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
458   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
459   gst_caps_merge (c2, c1);
460   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
461   fail_unless (gst_caps_get_size (c2) == 2, NULL);
462   gst_caps_unref (c2);
463 }
464
465 GST_END_TEST;
466
467 GST_START_TEST (test_merge_subset)
468 {
469   GstCaps *c1, *c2, *test;
470
471   /* the 2nd is already covered */
472   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,2]");
473   c1 = gst_caps_from_string ("audio/x-raw-int,channels=1");
474   gst_caps_merge (c2, c1);
475   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
476   fail_unless (gst_caps_get_size (c2) == 1, NULL);
477   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,2]");
478   fail_unless (gst_caps_is_equal (c2, test));
479   gst_caps_unref (c2);
480   gst_caps_unref (test);
481
482   /* here it is not */
483   c2 = gst_caps_from_string ("audio/x-raw-int,channels=1,rate=44100");
484   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[1,2],rate=44100");
485   gst_caps_merge (c2, c1);
486   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
487   fail_unless (gst_caps_get_size (c2) == 2, NULL);
488   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,2],rate=44100");
489   fail_unless (gst_caps_is_equal (c2, test));
490   gst_caps_unref (c2);
491   gst_caps_unref (test);
492
493   /* second one was already contained in the first one */
494   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,3]");
495   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[1,2]");
496   gst_caps_merge (c2, c1);
497   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
498   fail_unless (gst_caps_get_size (c2) == 1, NULL);
499   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,3]");
500   fail_unless (gst_caps_is_equal (c2, test));
501   gst_caps_unref (c2);
502   gst_caps_unref (test);
503
504   /* second one was already contained in the first one */
505   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
506   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[1,2]");
507   gst_caps_merge (c2, c1);
508   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
509   fail_unless (gst_caps_get_size (c2) == 1, NULL);
510   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
511   fail_unless (gst_caps_is_equal (c2, test));
512   gst_caps_unref (c2);
513   gst_caps_unref (test);
514
515   /* second one was already contained in the first one */
516   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
517   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[2,4]");
518   gst_caps_merge (c2, c1);
519   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
520   fail_unless (gst_caps_get_size (c2) == 1, NULL);
521   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
522   fail_unless (gst_caps_is_equal (c2, test));
523   gst_caps_unref (c2);
524   gst_caps_unref (test);
525
526   /* second one was already contained in the first one */
527   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
528   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[2,3]");
529   gst_caps_merge (c2, c1);
530   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
531   fail_unless (gst_caps_get_size (c2) == 1, NULL);
532   test = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
533   fail_unless (gst_caps_is_equal (c2, test));
534   gst_caps_unref (c2);
535   gst_caps_unref (test);
536
537   /* these caps cannot be merged */
538   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[2,3]");
539   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[1,4]");
540   gst_caps_merge (c2, c1);
541   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
542   fail_unless (gst_caps_get_size (c2) == 2, NULL);
543   test =
544       gst_caps_from_string
545       ("audio/x-raw-int,channels=[2,3];audio/x-raw-int,channels=[1,4]");
546   fail_unless (gst_caps_is_equal (c2, test));
547   gst_caps_unref (c2);
548   gst_caps_unref (test);
549
550   /* these caps cannot be merged */
551   c2 = gst_caps_from_string ("audio/x-raw-int,channels=[1,2]");
552   c1 = gst_caps_from_string ("audio/x-raw-int,channels=[1,3]");
553   gst_caps_merge (c2, c1);
554   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
555   fail_unless (gst_caps_get_size (c2) == 2, NULL);
556   test =
557       gst_caps_from_string
558       ("audio/x-raw-int,channels=[1,2];audio/x-raw-int,channels=[1,3]");
559   fail_unless (gst_caps_is_equal (c2, test));
560   gst_caps_unref (c2);
561   gst_caps_unref (test);
562
563   c2 = gst_caps_from_string ("audio/x-raw-int,channels={1,2}");
564   c1 = gst_caps_from_string ("audio/x-raw-int,channels={1,2,3,4}");
565   gst_caps_merge (c2, c1);
566   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
567   fail_unless (gst_caps_get_size (c2) == 2, NULL);
568   test = gst_caps_from_string ("audio/x-raw-int,channels={1,2};"
569       "audio/x-raw-int,channels={1,2,3,4}");
570   fail_unless (gst_caps_is_equal (c2, test));
571   gst_caps_unref (c2);
572   gst_caps_unref (test);
573
574   c2 = gst_caps_from_string ("audio/x-raw-int,channels={1,2}");
575   c1 = gst_caps_from_string ("audio/x-raw-int,channels={1,3}");
576   gst_caps_merge (c2, c1);
577   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
578   fail_unless (gst_caps_get_size (c2) == 2, NULL);
579   test = gst_caps_from_string ("audio/x-raw-int,channels={1,2};"
580       "audio/x-raw-int,channels={1,3}");
581   fail_unless (gst_caps_is_equal (c2, test));
582   gst_caps_unref (c2);
583   gst_caps_unref (test);
584
585   c2 = gst_caps_from_string
586       ("video/x-raw-yuv, framerate=(fraction){ 15/2, 5/1 }");
587   c1 = gst_caps_from_string
588       ("video/x-raw-yuv, framerate=(fraction){ 15/1, 5/1 }");
589   test = gst_caps_copy (c1);
590   gst_caps_merge (c2, c1);
591   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
592   fail_unless (gst_caps_is_subset (test, c2));
593   gst_caps_unref (test);
594   gst_caps_unref (c2);
595
596   c2 = gst_caps_from_string ("audio/x-raw-int");
597   c1 = gst_caps_from_string ("audio/x-raw-int,channels=1");
598   gst_caps_merge (c2, c1);
599   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
600   fail_unless (gst_caps_get_size (c2) == 1, NULL);
601   test = gst_caps_from_string ("audio/x-raw-int");
602   fail_unless (gst_caps_is_equal (c2, test));
603   gst_caps_unref (c2);
604   gst_caps_unref (test);
605
606   c2 = gst_caps_from_string ("audio/x-raw-int,channels=1");
607   c1 = gst_caps_from_string ("audio/x-raw-int");
608   gst_caps_merge (c2, c1);
609   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
610   fail_unless (gst_caps_get_size (c2) == 2, NULL);
611   test = gst_caps_from_string ("audio/x-raw-int,channels=1; audio/x-raw-int");
612   fail_unless (gst_caps_is_equal (c2, test));
613   gst_caps_unref (c2);
614   gst_caps_unref (test);
615 }
616
617 GST_END_TEST;
618
619 GST_START_TEST (test_intersect)
620 {
621   GstStructure *s;
622   GstCaps *c1, *c2, *ci1, *ci2;
623
624   /* field not specified = any value possible, so the intersection
625    * should keep fields which are only part of one set of caps */
626   c2 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,width=20");
627   c1 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420");
628
629   ci1 = gst_caps_intersect (c2, c1);
630   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
631   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
632   s = gst_caps_get_structure (ci1, 0);
633   fail_unless (gst_structure_has_name (s, "video/x-raw-yuv"));
634   fail_unless (gst_structure_get_value (s, "format") != NULL);
635   fail_unless (gst_structure_get_value (s, "width") != NULL);
636
637   /* with changed order */
638   ci2 = gst_caps_intersect (c1, c2);
639   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
640   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
641   s = gst_caps_get_structure (ci2, 0);
642   fail_unless (gst_structure_has_name (s, "video/x-raw-yuv"));
643   fail_unless (gst_structure_get_value (s, "format") != NULL);
644   fail_unless (gst_structure_get_value (s, "width") != NULL);
645
646   fail_unless (gst_caps_is_equal (ci1, ci2));
647
648   gst_caps_unref (ci1);
649   gst_caps_unref (ci2);
650
651   gst_caps_unref (c1);
652   gst_caps_unref (c2);
653
654   /* ========== */
655
656   c2 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,width=20");
657   c1 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,width=30");
658
659   ci1 = gst_caps_intersect (c2, c1);
660   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
661   fail_unless (gst_caps_is_empty (ci1), NULL);
662
663   /* with changed order */
664   ci2 = gst_caps_intersect (c1, c2);
665   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
666   fail_unless (gst_caps_is_empty (ci2), NULL);
667
668   fail_unless (gst_caps_is_equal (ci1, ci2));
669
670   gst_caps_unref (ci1);
671   gst_caps_unref (ci2);
672
673   gst_caps_unref (c1);
674   gst_caps_unref (c2);
675
676   /* ========== */
677
678   c2 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,width=20");
679   c1 = gst_caps_from_string ("video/x-raw-rgb,format=(fourcc)I420,width=20");
680
681   ci1 = gst_caps_intersect (c2, c1);
682   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
683   fail_unless (gst_caps_is_empty (ci1), NULL);
684
685   /* with changed order */
686   ci2 = gst_caps_intersect (c1, c2);
687   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
688   fail_unless (gst_caps_is_empty (ci2), NULL);
689
690   fail_unless (gst_caps_is_equal (ci1, ci2));
691
692   gst_caps_unref (ci1);
693   gst_caps_unref (ci2);
694
695   gst_caps_unref (c1);
696   gst_caps_unref (c2);
697
698   /* ========== */
699
700   c2 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,width=20");
701   c1 = gst_caps_from_string ("video/x-raw-yuv,format=(fourcc)I420,height=30");
702
703   ci1 = gst_caps_intersect (c2, c1);
704   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
705   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
706   s = gst_caps_get_structure (ci1, 0);
707   fail_unless (gst_structure_has_name (s, "video/x-raw-yuv"));
708   fail_unless (gst_structure_get_value (s, "format") != NULL);
709   fail_unless (gst_structure_get_value (s, "width") != NULL);
710   fail_unless (gst_structure_get_value (s, "height") != NULL);
711
712   /* with changed order */
713   ci2 = gst_caps_intersect (c1, c2);
714   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
715   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
716   s = gst_caps_get_structure (ci2, 0);
717   fail_unless (gst_structure_has_name (s, "video/x-raw-yuv"));
718   fail_unless (gst_structure_get_value (s, "format") != NULL);
719   fail_unless (gst_structure_get_value (s, "height") != NULL);
720   fail_unless (gst_structure_get_value (s, "width") != NULL);
721
722   fail_unless (gst_caps_is_equal (ci1, ci2));
723
724   gst_caps_unref (ci1);
725   gst_caps_unref (ci2);
726
727   gst_caps_unref (c1);
728   gst_caps_unref (c2);
729 }
730
731 GST_END_TEST;
732
733 GST_START_TEST (test_intersect2)
734 {
735   GstCaps *caps1, *caps2, *icaps;
736
737   /* tests array subtraction */
738   caps1 = gst_caps_from_string ("audio/x-raw-float, "
739       "channel-positions=(int)<                      "
740       "{ 1, 2, 3, 4, 5, 6 },                         "
741       "{ 1, 2, 3, 4, 5, 6 },                         "
742       "{ 1, 2, 3, 4, 5, 6 },                         "
743       "{ 1, 2, 3, 4, 5, 6 },                         "
744       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
745   caps2 = gst_caps_from_string ("audio/x-raw-float, "
746       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
747   icaps = gst_caps_intersect (caps1, caps2);
748   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
749   fail_if (gst_caps_is_empty (icaps));
750   fail_unless (gst_caps_is_equal (icaps, caps2));
751   gst_caps_unref (caps1);
752   gst_caps_unref (caps2);
753   gst_caps_unref (icaps);
754
755   /* ===== */
756
757   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
758   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
759   icaps = gst_caps_intersect (caps1, caps2);
760   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
761   fail_if (gst_caps_is_empty (icaps));
762   fail_unless (gst_caps_is_equal (icaps, caps2));
763   gst_caps_unref (caps1);
764   gst_caps_unref (caps2);
765   gst_caps_unref (icaps);
766 }
767
768 GST_END_TEST;
769
770
771 GST_START_TEST (test_intersect_zigzag)
772 {
773   GstCaps *caps1, *caps2, *icaps, *result;
774
775   /* tests if caps order is maintained */
776   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
777   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
778
779   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
780   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
781   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
782   fail_if (gst_caps_is_empty (icaps));
783   fail_unless (gst_caps_is_equal (icaps, result));
784   gst_caps_unref (icaps);
785   gst_caps_unref (result);
786
787   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
788   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
789   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
790   fail_if (gst_caps_is_empty (icaps));
791   fail_unless (gst_caps_is_equal (icaps, result));
792   gst_caps_unref (icaps);
793   gst_caps_unref (result);
794
795   gst_caps_unref (caps1);
796   gst_caps_unref (caps2);
797 }
798
799 GST_END_TEST;
800
801
802 GST_START_TEST (test_intersect_first)
803 {
804   GstCaps *caps1, *caps2, *icaps, *result;
805
806   /* tests if caps order is maintained */
807   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
808   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
809   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
810   result = gst_caps_from_string ("format/A; format/C; format/D");
811   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
812   fail_if (gst_caps_is_empty (icaps));
813   fail_unless (gst_caps_is_equal (icaps, result));
814   gst_caps_unref (caps1);
815   gst_caps_unref (caps2);
816   gst_caps_unref (icaps);
817   gst_caps_unref (result);
818 }
819
820 GST_END_TEST;
821
822
823 GST_START_TEST (test_intersect_first2)
824 {
825   GstCaps *caps1, *caps2, *icaps, *result;
826
827   /* tests if caps order is maintained */
828   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
829   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
830
831   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
832   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
833   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
834   fail_if (gst_caps_is_empty (icaps));
835   fail_unless (gst_caps_is_equal (icaps, result));
836   gst_caps_unref (icaps);
837   gst_caps_unref (result);
838
839   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
840   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
841   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
842   fail_if (gst_caps_is_empty (icaps));
843   fail_unless (gst_caps_is_equal (icaps, result));
844   gst_caps_unref (icaps);
845   gst_caps_unref (result);
846
847   gst_caps_unref (caps1);
848   gst_caps_unref (caps2);
849 }
850
851 GST_END_TEST;
852
853 GST_START_TEST (test_intersect_duplication)
854 {
855   GstCaps *c1, *c2, *test;
856
857   c1 = gst_caps_from_string
858       ("audio/x-raw-int, endianness=(int)1234, signed=(boolean)true, width=(int)16, depth=(int)16, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
859   c2 = gst_caps_from_string
860       ("audio/x-raw-int, width=(int)16, depth=(int)16, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ], endianness=(int){ 1234, 4321 }, signed=(boolean){ true, false }; audio/x-raw-int, width=(int)16, depth=(int)16, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ], endianness=(int){ 1234, 4321 }, signed=(boolean){ true, false }; audio/x-raw-int, width=(int)16, depth=(int)[ 1, 16 ], rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ], endianness=(int){ 1234, 4321 }, signed=(boolean){ true, false }");
861
862   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
863   fail_unless_equals_int (gst_caps_get_size (test), 1);
864   fail_unless (gst_caps_is_equal (c1, test));
865   gst_caps_unref (c1);
866   gst_caps_unref (c2);
867   gst_caps_unref (test);
868 }
869
870 GST_END_TEST;
871
872 static gboolean
873 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
874 {
875   return gst_value_is_fixed (value);
876 }
877
878
879 GST_START_TEST (test_normalize)
880 {
881   GstCaps *in, *norm, *out;
882   guint i;
883
884   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
885   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
886   norm = gst_caps_normalize (in);
887   fail_if (gst_caps_is_empty (norm));
888   fail_unless (gst_caps_is_equal (norm, out));
889   for (i = 0; i < gst_caps_get_size (norm); i++) {
890     GstStructure *st = gst_caps_get_structure (norm, i);
891     /* Make sure all fields of all structures are fixed */
892     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
893   }
894
895   gst_caps_unref (in);
896   gst_caps_unref (out);
897   gst_caps_unref (norm);
898
899   in = gst_caps_from_string
900       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
901   out =
902       gst_caps_from_string
903       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
904       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
905   norm = gst_caps_normalize (in);
906   fail_if (gst_caps_is_empty (norm));
907   fail_unless (gst_caps_is_equal (norm, out));
908   for (i = 0; i < gst_caps_get_size (norm); i++) {
909     GstStructure *st = gst_caps_get_structure (norm, i);
910     /* Make sure all fields of all structures are fixed */
911     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
912   }
913
914   gst_caps_unref (in);
915   gst_caps_unref (out);
916   gst_caps_unref (norm);
917
918   in = gst_caps_from_string
919       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
920   out =
921       gst_caps_from_string
922       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
923   norm = gst_caps_normalize (in);
924   fail_if (gst_caps_is_empty (norm));
925   fail_unless (gst_caps_is_equal (norm, out));
926   for (i = 0; i < gst_caps_get_size (norm); i++) {
927     GstStructure *st = gst_caps_get_structure (norm, i);
928     /* Make sure all fields of all structures are fixed */
929     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
930   }
931
932   gst_caps_unref (in);
933   gst_caps_unref (out);
934   gst_caps_unref (norm);
935 }
936
937 GST_END_TEST;
938
939 GST_START_TEST (test_broken)
940 {
941   GstCaps *c1;
942
943   /* NULL is not valid for media_type */
944   ASSERT_CRITICAL (c1 =
945       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
946   fail_if (c1);
947
948 #ifndef G_DISABLE_CHECKS
949   /* such a name is not valid, see gst_structure_validate_name() */
950   ASSERT_CRITICAL (c1 =
951       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
952   fail_if (c1);
953 #endif
954 }
955
956 GST_END_TEST;
957
958
959 static Suite *
960 gst_caps_suite (void)
961 {
962   Suite *s = suite_create ("GstCaps");
963   TCase *tc_chain = tcase_create ("operations");
964
965   suite_add_tcase (s, tc_chain);
966   tcase_add_test (tc_chain, test_from_string);
967   tcase_add_test (tc_chain, test_double_append);
968   tcase_add_test (tc_chain, test_mutability);
969   tcase_add_test (tc_chain, test_static_caps);
970   tcase_add_test (tc_chain, test_simplify);
971   tcase_add_test (tc_chain, test_truncate);
972   tcase_add_test (tc_chain, test_subset);
973   tcase_add_test (tc_chain, test_merge_fundamental);
974   tcase_add_test (tc_chain, test_merge_same);
975   tcase_add_test (tc_chain, test_merge_subset);
976   tcase_add_test (tc_chain, test_intersect);
977   tcase_add_test (tc_chain, test_intersect2);
978   tcase_add_test (tc_chain, test_intersect_zigzag);
979   tcase_add_test (tc_chain, test_intersect_first);
980   tcase_add_test (tc_chain, test_intersect_first2);
981   tcase_add_test (tc_chain, test_intersect_duplication);
982   tcase_add_test (tc_chain, test_normalize);
983   tcase_add_test (tc_chain, test_broken);
984
985   return s;
986 }
987
988 GST_CHECK_MAIN (gst_caps);