caps: Add a testcase for subset checks on lists with duplicated items
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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,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,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,rate=44100");
104   GstCaps *caps1;
105   GstCaps *caps2;
106   static GstStaticCaps sany = GST_STATIC_CAPS_ANY;
107   static GstStaticCaps snone = GST_STATIC_CAPS_NONE;
108
109   /* caps creation */
110   caps1 = gst_static_caps_get (&scaps);
111   fail_unless (caps1 != NULL);
112   /* 1 refcount core, one from us */
113   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
114
115   /* caps should be the same */
116   caps2 = gst_static_caps_get (&scaps);
117   fail_unless (caps2 != NULL);
118   /* 1 refcount core, two from us */
119   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
120   /* caps must be equal */
121   fail_unless (caps1 == caps2);
122
123   gst_caps_unref (caps1);
124   gst_caps_unref (caps2);
125
126   caps1 = gst_static_caps_get (&sany);
127   fail_unless (gst_caps_is_equal (caps1, GST_CAPS_ANY));
128   caps2 = gst_static_caps_get (&snone);
129   fail_unless (gst_caps_is_equal (caps2, GST_CAPS_NONE));
130   fail_if (gst_caps_is_equal (caps1, caps2));
131   gst_caps_unref (caps1);
132   gst_caps_unref (caps2);
133 }
134
135 GST_END_TEST;
136
137 static const gchar non_simple_caps_string[] =
138     "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
139     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
140     "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
141     "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
142     "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
143     "height=(int)[ 16, 4096 ]; video/x-raw, "
144     "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
145     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
146
147 static gboolean
148 check_string_list (const GValue * format_value)
149 {
150   const GValue *string_value;
151   gboolean got_rgb8 = FALSE;
152   gboolean got_yv12 = FALSE;
153   gboolean got_i420 = FALSE;
154   gboolean got_yuy2 = FALSE;
155   const gchar *string;
156
157   string_value = gst_value_list_get_value (format_value, 0);
158   fail_unless (string_value != NULL);
159   fail_unless (G_VALUE_HOLDS_STRING (string_value));
160   string = g_value_get_string (string_value);
161   fail_unless (string != NULL);
162   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
163   got_i420 = got_i420 || (g_str_equal (string, "I420"));
164   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
165   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
166
167   string_value = gst_value_list_get_value (format_value, 1);
168   fail_unless (string_value != NULL);
169   fail_unless (G_VALUE_HOLDS_STRING (string_value));
170   string = g_value_get_string (string_value);
171   fail_unless (string != NULL);
172   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
173   got_i420 = got_i420 || (g_str_equal (string, "I420"));
174   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
175   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
176
177   string_value = gst_value_list_get_value (format_value, 2);
178   fail_unless (string_value != NULL);
179   fail_unless (G_VALUE_HOLDS_STRING (string_value));
180   string = g_value_get_string (string_value);
181   fail_unless (string != NULL);
182   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
183   got_i420 = got_i420 || (g_str_equal (string, "I420"));
184   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
185   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
186
187   string_value = gst_value_list_get_value (format_value, 3);
188   fail_unless (string_value != NULL);
189   fail_unless (G_VALUE_HOLDS_STRING (string_value));
190   string = g_value_get_string (string_value);
191   fail_unless (string != NULL);
192   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
193   got_i420 = got_i420 || (g_str_equal (string, "I420"));
194   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
195   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
196
197   return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
198 }
199
200 GST_START_TEST (test_simplify)
201 {
202   GstStructure *s1;
203   GstCaps *caps;
204
205   caps = gst_caps_from_string (non_simple_caps_string);
206   fail_unless (caps != NULL,
207       "gst_caps_from_string (non_simple_caps_string) failed");
208
209   caps = gst_caps_simplify (caps);
210   fail_unless (caps != NULL, "gst_caps_simplify() should have worked");
211
212   /* check simplified caps, should be:
213    *
214    * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
215    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
216    *     framerate=(fraction)[ 1/100, 100 ]
217    */
218   GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
219   fail_unless (gst_caps_get_size (caps) == 1);
220   s1 = gst_caps_get_structure (caps, 0);
221   fail_unless (s1 != NULL);
222
223   fail_unless (gst_structure_has_name (s1, "video/x-raw"));
224   {
225     const GValue *framerate_value;
226     const GValue *format_value;
227     const GValue *width_value;
228     const GValue *height_value;
229     const GValue *val_fps;
230     GValue test_fps = { 0, };
231     gint min_width, max_width;
232     gint min_height, max_height;
233
234     format_value = gst_structure_get_value (s1, "format");
235     fail_unless (format_value != NULL);
236     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
237     fail_unless (gst_value_list_get_size (format_value) == 4);
238     fail_unless (check_string_list (format_value) == TRUE);
239
240     g_value_init (&test_fps, GST_TYPE_FRACTION);
241     framerate_value = gst_structure_get_value (s1, "framerate");
242     fail_unless (framerate_value != NULL);
243     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
244
245     val_fps = gst_value_get_fraction_range_min (framerate_value);
246     gst_value_set_fraction (&test_fps, 1, 100);
247     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
248
249     val_fps = gst_value_get_fraction_range_max (framerate_value);
250     gst_value_set_fraction (&test_fps, 100, 1);
251     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
252
253     g_value_unset (&test_fps);
254
255     width_value = gst_structure_get_value (s1, "width");
256     fail_unless (width_value != NULL);
257     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
258     min_width = gst_value_get_int_range_min (width_value);
259     max_width = gst_value_get_int_range_max (width_value);
260     fail_unless (min_width == 16 && max_width == 4096);
261
262     height_value = gst_structure_get_value (s1, "height");
263     fail_unless (height_value != NULL);
264     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
265     min_height = gst_value_get_int_range_min (height_value);
266     max_height = gst_value_get_int_range_max (height_value);
267     fail_unless (min_height == 16 && max_height == 4096);
268   }
269
270   gst_caps_unref (caps);
271 }
272
273 GST_END_TEST;
274
275 GST_START_TEST (test_truncate)
276 {
277   GstCaps *caps;
278
279   caps = gst_caps_from_string (non_simple_caps_string);
280   fail_unless (caps != NULL,
281       "gst_caps_from_string (non_simple_caps_string) failed");
282   fail_unless_equals_int (gst_caps_get_size (caps), 4);
283   caps = gst_caps_truncate (caps);
284   fail_unless_equals_int (gst_caps_get_size (caps), 1);
285   gst_caps_unref (caps);
286 }
287
288 GST_END_TEST;
289
290 GST_START_TEST (test_subset)
291 {
292   GstCaps *c1, *c2;
293
294   c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
295   c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
296   fail_unless (gst_caps_is_subset (c2, c1));
297   fail_if (gst_caps_is_subset (c1, c2));
298   gst_caps_unref (c1);
299   gst_caps_unref (c2);
300
301   c1 = gst_caps_from_string
302       ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
303   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
304   fail_unless (gst_caps_is_subset (c2, c1));
305   fail_if (gst_caps_is_subset (c1, c2));
306   gst_caps_unref (c1);
307   gst_caps_unref (c2);
308
309   c1 = gst_caps_from_string ("audio/x-raw, channels=(int) {1}");
310   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
311   fail_unless (gst_caps_is_subset (c2, c1));
312   fail_unless (gst_caps_is_subset (c1, c2));
313   fail_unless (gst_caps_is_equal (c1, c2));
314   gst_caps_unref (c1);
315   gst_caps_unref (c2);
316
317   c1 = gst_caps_from_string
318       ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
319   c2 = gst_caps_from_string
320       ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
321   fail_unless (gst_caps_is_subset (c1, c2));
322   fail_if (gst_caps_is_subset (c2, c1));
323   gst_caps_unref (c1);
324   gst_caps_unref (c2);
325
326   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
327   c2 = gst_caps_from_string
328       ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)nal");
329   fail_if (gst_caps_is_subset (c2, c1));
330   fail_if (gst_caps_is_subset (c1, c2));
331   fail_if (gst_caps_is_equal (c1, c2));
332   gst_caps_unref (c1);
333   gst_caps_unref (c2);
334 }
335
336 GST_END_TEST;
337
338 GST_START_TEST (test_subset_duplication)
339 {
340   GstCaps *c1, *c2;
341
342   c1 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
343   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE, F32LE }");
344
345   fail_unless (gst_caps_is_subset (c1, c2));
346   gst_caps_unref (c1);
347   gst_caps_unref (c2);
348 }
349
350 GST_END_TEST;
351
352 GST_START_TEST (test_merge_fundamental)
353 {
354   GstCaps *c1, *c2;
355
356   /* ANY + specific = ANY */
357   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
358   c2 = gst_caps_new_any ();
359   c2 = gst_caps_merge (c2, c1);
360   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
361   fail_unless (gst_caps_get_size (c2) == 0, NULL);
362   fail_unless (gst_caps_is_any (c2), NULL);
363   gst_caps_unref (c2);
364
365   /* specific + ANY = ANY */
366   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
367   c1 = gst_caps_new_any ();
368   c2 = gst_caps_merge (c2, c1);
369   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
370   fail_unless (gst_caps_get_size (c2) == 0, NULL);
371   fail_unless (gst_caps_is_any (c2), NULL);
372   gst_caps_unref (c2);
373
374   /* EMPTY + specific = specific */
375   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
376   c2 = gst_caps_new_empty ();
377   c2 = 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) == 1, NULL);
380   fail_if (gst_caps_is_empty (c2), NULL);
381   gst_caps_unref (c2);
382
383   /* specific + EMPTY = specific */
384   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
385   c1 = gst_caps_new_empty ();
386   c2 = 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) == 1, NULL);
389   fail_if (gst_caps_is_empty (c2), NULL);
390   gst_caps_unref (c2);
391 }
392
393 GST_END_TEST;
394
395 GST_START_TEST (test_merge_same)
396 {
397   GstCaps *c1, *c2, *test;
398
399   /* this is the same */
400   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
401   c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
402   c2 = gst_caps_merge (c2, c1);
403   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
404   fail_unless (gst_caps_get_size (c2) == 1, NULL);
405   test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
406   fail_unless (gst_caps_is_equal (c2, test));
407   gst_caps_unref (test);
408   gst_caps_unref (c2);
409
410   /* and so is this */
411   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
412   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
413   c2 = gst_caps_merge (c2, c1);
414   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
415   fail_unless (gst_caps_get_size (c2) == 1, NULL);
416   gst_caps_unref (c2);
417
418   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
419   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
420   c2 = 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) == 2, NULL);
423   gst_caps_unref (c2);
424
425   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
426   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
427   c2 = gst_caps_merge (c2, c1);
428   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
429   fail_unless (gst_caps_get_size (c2) == 2, NULL);
430   gst_caps_unref (c2);
431
432   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
433   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
434   c2 = gst_caps_merge (c2, c1);
435   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
436   fail_unless (gst_caps_get_size (c2) == 1, NULL);
437   gst_caps_unref (c2);
438
439   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
440   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
441   c2 = gst_caps_merge (c2, c1);
442   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
443   fail_unless (gst_caps_get_size (c2) == 2, NULL);
444   gst_caps_unref (c2);
445 }
446
447 GST_END_TEST;
448
449 GST_START_TEST (test_merge_subset)
450 {
451   GstCaps *c1, *c2, *test;
452
453   /* the 2nd is already covered */
454   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
455   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
456   c2 = gst_caps_merge (c2, c1);
457   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
458   fail_unless (gst_caps_get_size (c2) == 1, NULL);
459   test = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
460   fail_unless (gst_caps_is_equal (c2, test));
461   gst_caps_unref (c2);
462   gst_caps_unref (test);
463
464   /* here it is not */
465   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
466   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
467   c2 = gst_caps_merge (c2, c1);
468   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
469   fail_unless (gst_caps_get_size (c2) == 2, NULL);
470   test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
471   fail_unless (gst_caps_is_equal (c2, test));
472   gst_caps_unref (c2);
473   gst_caps_unref (test);
474
475   /* second one was already contained in the first one */
476   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
477   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
478   c2 = gst_caps_merge (c2, c1);
479   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
480   fail_unless (gst_caps_get_size (c2) == 1, NULL);
481   test = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
482   fail_unless (gst_caps_is_equal (c2, test));
483   gst_caps_unref (c2);
484   gst_caps_unref (test);
485
486   /* second one was already contained in the first one */
487   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
488   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
489   c2 = gst_caps_merge (c2, c1);
490   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
491   fail_unless (gst_caps_get_size (c2) == 1, NULL);
492   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
493   fail_unless (gst_caps_is_equal (c2, test));
494   gst_caps_unref (c2);
495   gst_caps_unref (test);
496
497   /* second one was already contained in the first one */
498   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
499   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
500   c2 = gst_caps_merge (c2, c1);
501   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
502   fail_unless (gst_caps_get_size (c2) == 1, NULL);
503   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
504   fail_unless (gst_caps_is_equal (c2, test));
505   gst_caps_unref (c2);
506   gst_caps_unref (test);
507
508   /* second one was already contained in the first one */
509   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
510   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
511   c2 = gst_caps_merge (c2, c1);
512   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
513   fail_unless (gst_caps_get_size (c2) == 1, NULL);
514   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
515   fail_unless (gst_caps_is_equal (c2, test));
516   gst_caps_unref (c2);
517   gst_caps_unref (test);
518
519   /* these caps cannot be merged */
520   c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
521   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
522   c2 = gst_caps_merge (c2, c1);
523   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
524   fail_unless (gst_caps_get_size (c2) == 2, NULL);
525   test =
526       gst_caps_from_string
527       ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
528   fail_unless (gst_caps_is_equal (c2, test));
529   gst_caps_unref (c2);
530   gst_caps_unref (test);
531
532   /* these caps cannot be merged */
533   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
534   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
535   c2 = gst_caps_merge (c2, c1);
536   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
537   fail_unless (gst_caps_get_size (c2) == 2, NULL);
538   test =
539       gst_caps_from_string
540       ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
541   fail_unless (gst_caps_is_equal (c2, test));
542   gst_caps_unref (c2);
543   gst_caps_unref (test);
544
545   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
546   c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
547   c2 = gst_caps_merge (c2, c1);
548   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
549   fail_unless (gst_caps_get_size (c2) == 2, NULL);
550   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
551       "audio/x-raw,channels={1,2,3,4}");
552   fail_unless (gst_caps_is_equal (c2, test));
553   gst_caps_unref (c2);
554   gst_caps_unref (test);
555
556   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
557   c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
558   c2 = gst_caps_merge (c2, c1);
559   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
560   fail_unless (gst_caps_get_size (c2) == 2, NULL);
561   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
562       "audio/x-raw,channels={1,3}");
563   fail_unless (gst_caps_is_equal (c2, test));
564   gst_caps_unref (c2);
565   gst_caps_unref (test);
566
567   c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
568   c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
569   test = gst_caps_copy (c1);
570   c2 = gst_caps_merge (c2, c1);
571   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
572   fail_unless (gst_caps_is_subset (test, c2));
573   gst_caps_unref (test);
574   gst_caps_unref (c2);
575
576   c2 = gst_caps_from_string ("audio/x-raw");
577   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
578   c2 = gst_caps_merge (c2, c1);
579   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
580   fail_unless (gst_caps_get_size (c2) == 1, NULL);
581   test = gst_caps_from_string ("audio/x-raw");
582   fail_unless (gst_caps_is_equal (c2, test));
583   gst_caps_unref (c2);
584   gst_caps_unref (test);
585
586   c2 = gst_caps_from_string ("audio/x-raw,channels=1");
587   c1 = gst_caps_from_string ("audio/x-raw");
588   c2 = gst_caps_merge (c2, c1);
589   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
590   fail_unless (gst_caps_get_size (c2) == 2, NULL);
591   test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
592   fail_unless (gst_caps_is_equal (c2, test));
593   gst_caps_unref (c2);
594   gst_caps_unref (test);
595 }
596
597 GST_END_TEST;
598
599 GST_START_TEST (test_intersect)
600 {
601   GstStructure *s;
602   GstCaps *c1, *c2, *ci1, *ci2;
603
604   /* field not specified = any value possible, so the intersection
605    * should keep fields which are only part of one set of caps */
606   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
607   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
608
609   ci1 = gst_caps_intersect (c2, c1);
610   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
611   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
612   s = gst_caps_get_structure (ci1, 0);
613   fail_unless (gst_structure_has_name (s, "video/x-raw"));
614   fail_unless (gst_structure_get_value (s, "format") != NULL);
615   fail_unless (gst_structure_get_value (s, "width") != NULL);
616
617   /* with changed order */
618   ci2 = gst_caps_intersect (c1, c2);
619   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
620   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
621   s = gst_caps_get_structure (ci2, 0);
622   fail_unless (gst_structure_has_name (s, "video/x-raw"));
623   fail_unless (gst_structure_get_value (s, "format") != NULL);
624   fail_unless (gst_structure_get_value (s, "width") != NULL);
625
626   fail_unless (gst_caps_is_equal (ci1, ci2));
627
628   gst_caps_unref (ci1);
629   gst_caps_unref (ci2);
630
631   gst_caps_unref (c1);
632   gst_caps_unref (c2);
633
634   /* ========== */
635
636   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
637   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
638
639   ci1 = gst_caps_intersect (c2, c1);
640   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
641   fail_unless (gst_caps_is_empty (ci1), NULL);
642
643   /* with changed order */
644   ci2 = gst_caps_intersect (c1, c2);
645   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
646   fail_unless (gst_caps_is_empty (ci2), NULL);
647
648   fail_unless (gst_caps_is_equal (ci1, ci2));
649
650   gst_caps_unref (ci1);
651   gst_caps_unref (ci2);
652
653   gst_caps_unref (c1);
654   gst_caps_unref (c2);
655
656   /* ========== */
657
658   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
659   c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
660
661   ci1 = gst_caps_intersect (c2, c1);
662   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
663   fail_unless (gst_caps_is_empty (ci1), NULL);
664
665   /* with changed order */
666   ci2 = gst_caps_intersect (c1, c2);
667   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
668   fail_unless (gst_caps_is_empty (ci2), NULL);
669
670   fail_unless (gst_caps_is_equal (ci1, ci2));
671
672   gst_caps_unref (ci1);
673   gst_caps_unref (ci2);
674
675   gst_caps_unref (c1);
676   gst_caps_unref (c2);
677
678   /* ========== */
679
680   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
681   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,height=30");
682
683   ci1 = gst_caps_intersect (c2, c1);
684   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
685   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
686   s = gst_caps_get_structure (ci1, 0);
687   fail_unless (gst_structure_has_name (s, "video/x-raw"));
688   fail_unless (gst_structure_get_value (s, "format") != NULL);
689   fail_unless (gst_structure_get_value (s, "width") != NULL);
690   fail_unless (gst_structure_get_value (s, "height") != NULL);
691
692   /* with changed order */
693   ci2 = gst_caps_intersect (c1, c2);
694   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
695   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
696   s = gst_caps_get_structure (ci2, 0);
697   fail_unless (gst_structure_has_name (s, "video/x-raw"));
698   fail_unless (gst_structure_get_value (s, "format") != NULL);
699   fail_unless (gst_structure_get_value (s, "height") != NULL);
700   fail_unless (gst_structure_get_value (s, "width") != NULL);
701
702   fail_unless (gst_caps_is_equal (ci1, ci2));
703
704   gst_caps_unref (ci1);
705   gst_caps_unref (ci2);
706
707   gst_caps_unref (c1);
708   gst_caps_unref (c2);
709 }
710
711 GST_END_TEST;
712
713 GST_START_TEST (test_intersect2)
714 {
715   GstCaps *caps1, *caps2, *icaps;
716
717   /* tests array subtraction */
718   caps1 = gst_caps_from_string ("audio/x-raw, "
719       "channel-positions=(int)<                      "
720       "{ 1, 2, 3, 4, 5, 6 },                         "
721       "{ 1, 2, 3, 4, 5, 6 },                         "
722       "{ 1, 2, 3, 4, 5, 6 },                         "
723       "{ 1, 2, 3, 4, 5, 6 },                         "
724       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
725   caps2 = gst_caps_from_string ("audio/x-raw, "
726       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
727   icaps = gst_caps_intersect (caps1, caps2);
728   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
729   fail_if (gst_caps_is_empty (icaps));
730   fail_unless (gst_caps_is_equal (icaps, caps2));
731   gst_caps_unref (caps1);
732   gst_caps_unref (caps2);
733   gst_caps_unref (icaps);
734
735   /* ===== */
736
737   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
738   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
739   icaps = gst_caps_intersect (caps1, caps2);
740   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
741   fail_if (gst_caps_is_empty (icaps));
742   fail_unless (gst_caps_is_equal (icaps, caps2));
743   gst_caps_unref (caps1);
744   gst_caps_unref (caps2);
745   gst_caps_unref (icaps);
746 }
747
748 GST_END_TEST;
749
750 GST_START_TEST (test_intersect_list_duplicate)
751 {
752   GstCaps *caps1, *caps2, *icaps;
753
754   /* make sure we don't take too long to intersect these.. */
755   caps1 = gst_caps_from_string ("video/x-raw, format=(string)YV12; "
756       "video/x-raw, format=(string)I420; video/x-raw, format=(string)YUY2; "
757       "video/x-raw, format=(string)UYVY; "
758       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx,"
759       " xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
760       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
761       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
762       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
763       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
764       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
765       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
766       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
767       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, { ABGR, ABGR, "
768       "{ ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
769       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
770       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
771       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
772       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
773       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
774       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
775       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
776       "RGB, BGR, Y41B, Y42B, YVYU, Y444 }; "
777       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, "
778       "xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
779       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
780       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
781       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
782       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
783       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
784       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
785       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
786       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, "
787       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
788       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
789       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
790       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
791       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
792       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
793       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
794       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
795       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }; "
796       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, "
797       "BGRx, xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
798       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
799       "{ RGBA, RGBA, { RGBA, RGBA } } } }, BGRA, ARGB, "
800       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
801       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
802       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }");
803
804   caps2 = gst_caps_copy (caps1);
805
806   icaps = gst_caps_intersect (caps1, caps2);
807
808   gst_caps_unref (caps1);
809   gst_caps_unref (caps2);
810   gst_caps_unref (icaps);
811 }
812
813 GST_END_TEST;
814
815 GST_START_TEST (test_intersect_zigzag)
816 {
817   GstCaps *caps1, *caps2, *icaps, *result;
818
819   /* tests if caps order is maintained */
820   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
821   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
822
823   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
824   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
825   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
826   fail_if (gst_caps_is_empty (icaps));
827   fail_unless (gst_caps_is_equal (icaps, result));
828   gst_caps_unref (icaps);
829   gst_caps_unref (result);
830
831   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
832   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
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   gst_caps_unref (caps1);
840   gst_caps_unref (caps2);
841 }
842
843 GST_END_TEST;
844
845
846 GST_START_TEST (test_intersect_first)
847 {
848   GstCaps *caps1, *caps2, *icaps, *result;
849
850   /* tests if caps order is maintained */
851   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
852   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
853   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
854   result = gst_caps_from_string ("format/A; format/C; format/D");
855   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
856   fail_if (gst_caps_is_empty (icaps));
857   fail_unless (gst_caps_is_equal (icaps, result));
858   gst_caps_unref (caps1);
859   gst_caps_unref (caps2);
860   gst_caps_unref (icaps);
861   gst_caps_unref (result);
862 }
863
864 GST_END_TEST;
865
866
867 GST_START_TEST (test_intersect_first2)
868 {
869   GstCaps *caps1, *caps2, *icaps, *result;
870
871   /* tests if caps order is maintained */
872   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
873   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
874
875   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
876   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
877   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
878   fail_if (gst_caps_is_empty (icaps));
879   fail_unless (gst_caps_is_equal (icaps, result));
880   gst_caps_unref (icaps);
881   gst_caps_unref (result);
882
883   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
884   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
885   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
886   fail_if (gst_caps_is_empty (icaps));
887   fail_unless (gst_caps_is_equal (icaps, result));
888   gst_caps_unref (icaps);
889   gst_caps_unref (result);
890
891   gst_caps_unref (caps1);
892   gst_caps_unref (caps2);
893 }
894
895 GST_END_TEST;
896
897 GST_START_TEST (test_intersect_duplication)
898 {
899   GstCaps *c1, *c2, *test;
900
901   c1 = gst_caps_from_string
902       ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
903   c2 = gst_caps_from_string
904       ("audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]; audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ]; audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ]");
905
906   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
907   fail_unless_equals_int (gst_caps_get_size (test), 1);
908   fail_unless (gst_caps_is_equal (c1, test));
909   gst_caps_unref (c1);
910   gst_caps_unref (c2);
911   gst_caps_unref (test);
912 }
913
914 GST_END_TEST;
915
916 static gboolean
917 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
918 {
919   return gst_value_is_fixed (value);
920 }
921
922
923 GST_START_TEST (test_normalize)
924 {
925   GstCaps *in, *norm, *out;
926   guint i;
927
928   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
929   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
930   norm = gst_caps_normalize (in);
931   fail_if (gst_caps_is_empty (norm));
932   fail_unless (gst_caps_is_equal (norm, out));
933   for (i = 0; i < gst_caps_get_size (norm); i++) {
934     GstStructure *st = gst_caps_get_structure (norm, i);
935     /* Make sure all fields of all structures are fixed */
936     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
937   }
938
939   gst_caps_unref (out);
940   gst_caps_unref (norm);
941
942   in = gst_caps_from_string
943       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
944   out =
945       gst_caps_from_string
946       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
947       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
948   norm = gst_caps_normalize (in);
949   fail_if (gst_caps_is_empty (norm));
950   fail_unless (gst_caps_is_equal (norm, out));
951   for (i = 0; i < gst_caps_get_size (norm); i++) {
952     GstStructure *st = gst_caps_get_structure (norm, i);
953     /* Make sure all fields of all structures are fixed */
954     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
955   }
956
957   gst_caps_unref (out);
958   gst_caps_unref (norm);
959
960   in = gst_caps_from_string
961       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
962   out =
963       gst_caps_from_string
964       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
965   norm = gst_caps_normalize (in);
966   fail_if (gst_caps_is_empty (norm));
967   fail_unless (gst_caps_is_equal (norm, out));
968   for (i = 0; i < gst_caps_get_size (norm); i++) {
969     GstStructure *st = gst_caps_get_structure (norm, i);
970     /* Make sure all fields of all structures are fixed */
971     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
972   }
973
974   gst_caps_unref (out);
975   gst_caps_unref (norm);
976 }
977
978 GST_END_TEST;
979
980 GST_START_TEST (test_broken)
981 {
982   GstCaps *c1;
983
984   /* NULL is not valid for media_type */
985   ASSERT_CRITICAL (c1 =
986       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
987   fail_if (c1);
988
989 #ifndef G_DISABLE_CHECKS
990   /* such a name is not valid, see gst_structure_validate_name() */
991   ASSERT_CRITICAL (c1 =
992       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
993   fail_if (c1);
994 #endif
995 }
996
997 GST_END_TEST;
998
999 GST_START_TEST (test_features)
1000 {
1001   GstCaps *c1, *c2, *c3;
1002   GstStructure *s1, *s2;
1003   GstCapsFeatures *f1, *f2;
1004   gchar *str1;
1005   static GstStaticCaps scaps =
1006       GST_STATIC_CAPS
1007       ("video/x-raw(memory:EGLImage), width=320, height=[ 240, 260 ]");
1008
1009   c1 = gst_caps_new_empty ();
1010   fail_unless (c1 != NULL);
1011   s1 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1012       GST_TYPE_INT_RANGE, 240, 260, NULL);
1013   fail_unless (s1 != NULL);
1014   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1015   fail_unless (f1 != NULL);
1016
1017   gst_caps_append_structure_full (c1, s1, f1);
1018   s2 = gst_caps_get_structure (c1, 0);
1019   fail_unless (s1 == s2);
1020   f2 = gst_caps_get_features (c1, 0);
1021   fail_unless (f1 == f2);
1022
1023   str1 = gst_caps_to_string (c1);
1024   fail_unless (str1 != NULL);
1025   c2 = gst_caps_from_string (str1);
1026   fail_unless (c2 != NULL);
1027   g_free (str1);
1028
1029   fail_unless (gst_caps_is_equal (c1, c2));
1030   fail_unless (gst_caps_is_subset (c1, c2));
1031   fail_unless (gst_caps_is_subset (c2, c1));
1032   fail_unless (gst_caps_can_intersect (c1, c2));
1033
1034   gst_caps_unref (c2);
1035
1036   c2 = gst_caps_new_empty ();
1037   fail_unless (c2 != NULL);
1038   s2 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1039       GST_TYPE_INT_RANGE, 240, 260, NULL);
1040   fail_unless (s2 != NULL);
1041   f2 = gst_caps_features_new ("memory:VASurface", "meta:VAMeta", NULL);
1042   fail_unless (f2 != NULL);
1043   gst_caps_append_structure_full (c2, s2, f2);
1044
1045   fail_if (gst_caps_is_equal (c1, c2));
1046   fail_if (gst_caps_is_subset (c1, c2));
1047   fail_if (gst_caps_is_subset (c2, c1));
1048   fail_if (gst_caps_can_intersect (c1, c2));
1049
1050   str1 = gst_caps_to_string (c2);
1051   fail_unless (str1 != NULL);
1052   c3 = gst_caps_from_string (str1);
1053   fail_unless (c3 != NULL);
1054   g_free (str1);
1055
1056   fail_unless (gst_caps_is_equal (c2, c3));
1057   fail_unless (gst_caps_is_subset (c2, c3));
1058   fail_unless (gst_caps_is_subset (c3, c2));
1059   fail_unless (gst_caps_can_intersect (c2, c3));
1060
1061   f1 = gst_caps_get_features (c3, 0);
1062   fail_unless (f1 != NULL);
1063   fail_if (f1 == f2);
1064   gst_caps_features_contains (f1, "memory:VASurface");
1065   gst_caps_features_remove (f1, "memory:VASurface");
1066   fail_if (gst_caps_is_equal (c2, c3));
1067   fail_if (gst_caps_is_subset (c2, c3));
1068   fail_if (gst_caps_is_subset (c3, c2));
1069   fail_if (gst_caps_can_intersect (c2, c3));
1070
1071   gst_caps_unref (c3);
1072   gst_caps_unref (c2);
1073
1074   c2 = gst_static_caps_get (&scaps);
1075   fail_unless (c2 != NULL);
1076   fail_unless (gst_caps_is_equal (c1, c2));
1077   fail_unless (gst_caps_is_subset (c1, c2));
1078   fail_unless (gst_caps_is_subset (c2, c1));
1079   fail_unless (gst_caps_can_intersect (c1, c2));
1080   gst_caps_unref (c2);
1081
1082   c2 = gst_caps_from_string
1083       ("video/x-raw(ANY), width=320, height=[ 240, 260 ]");
1084   fail_unless (c2 != NULL);
1085   fail_if (gst_caps_is_equal (c1, c2));
1086   fail_unless (gst_caps_is_subset (c1, c2));
1087   fail_if (gst_caps_is_subset (c2, c1));
1088   fail_unless (gst_caps_can_intersect (c1, c2));
1089
1090   c3 = gst_caps_intersect (c1, c2);
1091   fail_unless (gst_caps_is_equal (c3, c1));
1092
1093   gst_caps_unref (c3);
1094   gst_caps_unref (c2);
1095   gst_caps_unref (c1);
1096 }
1097
1098 GST_END_TEST;
1099
1100 static Suite *
1101 gst_caps_suite (void)
1102 {
1103   Suite *s = suite_create ("GstCaps");
1104   TCase *tc_chain = tcase_create ("operations");
1105
1106   suite_add_tcase (s, tc_chain);
1107   tcase_add_test (tc_chain, test_from_string);
1108   tcase_add_test (tc_chain, test_double_append);
1109   tcase_add_test (tc_chain, test_mutability);
1110   tcase_add_test (tc_chain, test_static_caps);
1111   tcase_add_test (tc_chain, test_simplify);
1112   tcase_add_test (tc_chain, test_truncate);
1113   tcase_add_test (tc_chain, test_subset);
1114   tcase_add_test (tc_chain, test_subset_duplication);
1115   tcase_add_test (tc_chain, test_merge_fundamental);
1116   tcase_add_test (tc_chain, test_merge_same);
1117   tcase_add_test (tc_chain, test_merge_subset);
1118   tcase_add_test (tc_chain, test_intersect);
1119   tcase_add_test (tc_chain, test_intersect2);
1120   tcase_add_test (tc_chain, test_intersect_list_duplicate);
1121   tcase_add_test (tc_chain, test_intersect_zigzag);
1122   tcase_add_test (tc_chain, test_intersect_first);
1123   tcase_add_test (tc_chain, test_intersect_first2);
1124   tcase_add_test (tc_chain, test_intersect_duplication);
1125   tcase_add_test (tc_chain, test_normalize);
1126   tcase_add_test (tc_chain, test_broken);
1127   tcase_add_test (tc_chain, test_features);
1128
1129   return s;
1130 }
1131
1132 GST_CHECK_MAIN (gst_caps);