d9931d6ca9dd416dfbf149ce35cc8307de0158f8
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/check/gstcheck.h>
27 #include <gst/gstcaps.h>
28 #include "capslist.h"
29
30 GST_START_TEST (test_from_string)
31 {
32   GstCaps *caps;
33   GstCaps *caps2;
34   gchar *to_str;
35   int i;
36
37   for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
38     caps = gst_caps_from_string (caps_list[i]);
39     fail_if (caps == NULL,
40         "Could not create caps from string %s\n", caps_list[i]);
41     to_str = gst_caps_to_string (caps);
42     fail_if (to_str == NULL,
43         "Could not convert caps back to string %s\n", caps_list[i]);
44     caps2 = gst_caps_from_string (to_str);
45     fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);
46     g_free (to_str);
47
48     fail_unless (gst_caps_is_equal (caps, caps));
49     fail_unless (gst_caps_is_equal (caps, caps2));
50     gst_caps_unref (caps2);
51
52     to_str = gst_caps_serialize (caps, GST_SERIALIZE_FLAG_NONE);
53     fail_if (to_str == NULL,
54         "Could not convert caps back to string %s\n", caps_list[i]);
55     caps2 = gst_caps_from_string (to_str);
56     fail_unless (gst_caps_is_equal (caps, caps));
57     fail_unless (gst_caps_is_equal (caps, caps2));
58
59     gst_caps_unref (caps);
60     gst_caps_unref (caps2);
61     g_free (to_str);
62   }
63 }
64
65 GST_END_TEST;
66
67 GST_START_TEST (test_double_append)
68 {
69   GstStructure *s1;
70   GstCaps *c1;
71
72   c1 = gst_caps_new_empty ();
73   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
74   gst_caps_append_structure (c1, s1);
75   ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
76
77   gst_caps_unref (c1);
78 }
79
80 GST_END_TEST;
81
82 GST_START_TEST (test_mutability)
83 {
84   GstStructure *s1;
85   GstCaps *c1;
86   gint ret;
87
88   c1 = gst_caps_new_empty ();
89   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
90   gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
91   gst_caps_append_structure (c1, s1);
92   gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
93   gst_caps_ref (c1);
94   ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
95   fail_unless (gst_structure_get_int (s1, "rate", &ret));
96   fail_unless (ret == 22500);
97   ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
98   fail_unless (gst_structure_get_int (s1, "rate", &ret));
99   fail_unless (ret == 22500);
100   gst_caps_unref (c1);
101   gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
102   fail_unless (gst_structure_get_int (s1, "rate", &ret));
103   fail_unless (ret == 11250);
104   gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
105   fail_unless (gst_structure_get_int (s1, "rate", &ret));
106   fail_unless (ret == 1);
107   gst_caps_unref (c1);
108 }
109
110 GST_END_TEST;
111
112 GST_START_TEST (test_static_caps)
113 {
114   static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw,rate=44100");
115   GstCaps *caps1;
116   GstCaps *caps2;
117   static GstStaticCaps sany = GST_STATIC_CAPS_ANY;
118   static GstStaticCaps snone = GST_STATIC_CAPS_NONE;
119
120   /* caps creation */
121   caps1 = gst_static_caps_get (&scaps);
122   fail_unless (caps1 != NULL);
123   /* 1 refcount core, one from us */
124   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
125
126   /* caps should be the same */
127   caps2 = gst_static_caps_get (&scaps);
128   fail_unless (caps2 != NULL);
129   /* 1 refcount core, two from us */
130   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
131   /* caps must be equal */
132   fail_unless (caps1 == caps2);
133
134   gst_caps_unref (caps1);
135   gst_caps_unref (caps2);
136
137   caps1 = gst_static_caps_get (&sany);
138   fail_unless (gst_caps_is_equal (caps1, GST_CAPS_ANY));
139   caps2 = gst_static_caps_get (&snone);
140   fail_unless (gst_caps_is_equal (caps2, GST_CAPS_NONE));
141   fail_if (gst_caps_is_equal (caps1, caps2));
142   gst_caps_unref (caps1);
143   gst_caps_unref (caps2);
144 }
145
146 GST_END_TEST;
147
148 static const gchar non_simple_caps_string[] =
149     "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
150     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
151     "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
152     "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
153     "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
154     "height=(int)[ 16, 4096 ]; video/x-raw, "
155     "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
156     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
157
158 static gboolean
159 check_string_list (const GValue * format_value)
160 {
161   const GValue *string_value;
162   gboolean got_rgb8 = FALSE;
163   gboolean got_yv12 = FALSE;
164   gboolean got_i420 = FALSE;
165   gboolean got_yuy2 = FALSE;
166   const gchar *string;
167
168   string_value = gst_value_list_get_value (format_value, 0);
169   fail_unless (string_value != NULL);
170   fail_unless (G_VALUE_HOLDS_STRING (string_value));
171   string = g_value_get_string (string_value);
172   fail_unless (string != NULL);
173   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
174   got_i420 = got_i420 || (g_str_equal (string, "I420"));
175   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
176   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
177
178   string_value = gst_value_list_get_value (format_value, 1);
179   fail_unless (string_value != NULL);
180   fail_unless (G_VALUE_HOLDS_STRING (string_value));
181   string = g_value_get_string (string_value);
182   fail_unless (string != NULL);
183   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
184   got_i420 = got_i420 || (g_str_equal (string, "I420"));
185   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
186   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
187
188   string_value = gst_value_list_get_value (format_value, 2);
189   fail_unless (string_value != NULL);
190   fail_unless (G_VALUE_HOLDS_STRING (string_value));
191   string = g_value_get_string (string_value);
192   fail_unless (string != NULL);
193   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
194   got_i420 = got_i420 || (g_str_equal (string, "I420"));
195   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
196   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
197
198   string_value = gst_value_list_get_value (format_value, 3);
199   fail_unless (string_value != NULL);
200   fail_unless (G_VALUE_HOLDS_STRING (string_value));
201   string = g_value_get_string (string_value);
202   fail_unless (string != NULL);
203   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
204   got_i420 = got_i420 || (g_str_equal (string, "I420"));
205   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
206   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
207
208   return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
209 }
210
211 GST_START_TEST (test_simplify)
212 {
213   GstStructure *s1;
214   GstCaps *caps;
215
216   caps = gst_caps_from_string (non_simple_caps_string);
217   fail_unless (caps != NULL,
218       "gst_caps_from_string (non_simple_caps_string) failed");
219
220   caps = gst_caps_simplify (caps);
221   fail_unless (caps != NULL, "gst_caps_simplify() should have worked");
222
223   /* check simplified caps, should be:
224    *
225    * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
226    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
227    *     framerate=(fraction)[ 1/100, 100 ]
228    */
229   GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
230   fail_unless (gst_caps_get_size (caps) == 1);
231   s1 = gst_caps_get_structure (caps, 0);
232   fail_unless (s1 != NULL);
233
234   fail_unless (gst_structure_has_name (s1, "video/x-raw"));
235   {
236     const GValue *framerate_value;
237     const GValue *format_value;
238     const GValue *width_value;
239     const GValue *height_value;
240     const GValue *val_fps;
241     GValue test_fps = { 0, };
242     gint min_width, max_width;
243     gint min_height, max_height;
244
245     format_value = gst_structure_get_value (s1, "format");
246     fail_unless (format_value != NULL);
247     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
248     fail_unless (gst_value_list_get_size (format_value) == 4);
249     fail_unless (check_string_list (format_value) == TRUE);
250
251     g_value_init (&test_fps, GST_TYPE_FRACTION);
252     framerate_value = gst_structure_get_value (s1, "framerate");
253     fail_unless (framerate_value != NULL);
254     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
255
256     val_fps = gst_value_get_fraction_range_min (framerate_value);
257     gst_value_set_fraction (&test_fps, 1, 100);
258     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
259
260     val_fps = gst_value_get_fraction_range_max (framerate_value);
261     gst_value_set_fraction (&test_fps, 100, 1);
262     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
263
264     g_value_unset (&test_fps);
265
266     width_value = gst_structure_get_value (s1, "width");
267     fail_unless (width_value != NULL);
268     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
269     min_width = gst_value_get_int_range_min (width_value);
270     max_width = gst_value_get_int_range_max (width_value);
271     fail_unless (min_width == 16 && max_width == 4096);
272
273     height_value = gst_structure_get_value (s1, "height");
274     fail_unless (height_value != NULL);
275     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
276     min_height = gst_value_get_int_range_min (height_value);
277     max_height = gst_value_get_int_range_max (height_value);
278     fail_unless (min_height == 16 && max_height == 4096);
279   }
280
281   gst_caps_unref (caps);
282
283   caps = gst_caps_new_empty ();
284   caps = gst_caps_simplify (caps);
285   fail_unless (gst_caps_is_empty (caps));
286   gst_caps_unref (caps);
287
288   caps = gst_caps_new_any ();
289   caps = gst_caps_simplify (caps);
290   fail_unless (gst_caps_is_any (caps));
291   gst_caps_unref (caps);
292 }
293
294 GST_END_TEST;
295
296 GST_START_TEST (test_truncate)
297 {
298   GstCaps *caps;
299
300   caps = gst_caps_from_string (non_simple_caps_string);
301   fail_unless (caps != NULL,
302       "gst_caps_from_string (non_simple_caps_string) failed");
303   fail_unless_equals_int (gst_caps_get_size (caps), 4);
304   caps = gst_caps_truncate (caps);
305   fail_unless_equals_int (gst_caps_get_size (caps), 1);
306   gst_caps_unref (caps);
307
308   caps = gst_caps_new_empty ();
309   caps = gst_caps_truncate (caps);
310   fail_if (caps == NULL);
311   fail_unless (gst_caps_is_empty (caps));
312   gst_caps_unref (caps);
313
314   caps = gst_caps_new_any ();
315   caps = gst_caps_truncate (caps);
316   fail_if (caps == NULL);
317   fail_unless (gst_caps_is_any (caps));
318   gst_caps_unref (caps);
319 }
320
321 GST_END_TEST;
322
323 GST_START_TEST (test_fixate)
324 {
325   GstCaps *caps, *expected;
326
327   caps = gst_caps_from_string (non_simple_caps_string);
328   fail_unless (caps != NULL,
329       "gst_caps_from_string (non_simple_caps_string) failed");
330   fail_unless_equals_int (gst_caps_get_size (caps), 4);
331   caps = gst_caps_fixate (caps);
332   fail_unless_equals_int (gst_caps_get_size (caps), 1);
333   expected =
334       gst_caps_from_string
335       ("video/x-raw, format=(string)I420, framerate=(fraction)1/100, "
336       "width=(int)16, height=(int)16");
337   fail_unless (gst_caps_is_equal (caps, expected));
338   gst_caps_unref (caps);
339   gst_caps_unref (expected);
340
341   caps = gst_caps_new_empty ();
342   caps = gst_caps_fixate (caps);
343   fail_if (caps == NULL);
344   fail_unless (gst_caps_is_empty (caps));
345   gst_caps_unref (caps);
346 }
347
348 GST_END_TEST;
349
350 GST_START_TEST (test_subset)
351 {
352   GstCaps *c1, *c2;
353
354   c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
355   c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
356   fail_unless (gst_caps_is_subset (c2, c1));
357   fail_if (gst_caps_is_subset (c1, c2));
358   gst_caps_unref (c1);
359   gst_caps_unref (c2);
360
361   c1 = gst_caps_from_string
362       ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
363   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
364   fail_unless (gst_caps_is_subset (c2, c1));
365   fail_if (gst_caps_is_subset (c1, c2));
366   gst_caps_unref (c1);
367   gst_caps_unref (c2);
368
369   c1 = gst_caps_from_string ("audio/x-raw, channels=(int) {1}");
370   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
371   fail_unless (gst_caps_is_subset (c2, c1));
372   fail_unless (gst_caps_is_subset (c1, c2));
373   fail_unless (gst_caps_is_equal (c1, c2));
374   gst_caps_unref (c1);
375   gst_caps_unref (c2);
376
377   c1 = gst_caps_from_string
378       ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
379   c2 = gst_caps_from_string
380       ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
381   fail_unless (gst_caps_is_subset (c1, c2));
382   fail_if (gst_caps_is_subset (c2, c1));
383   gst_caps_unref (c1);
384   gst_caps_unref (c2);
385
386   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
387   c2 = gst_caps_from_string
388       ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)nal");
389   fail_if (gst_caps_is_subset (c2, c1));
390   fail_if (gst_caps_is_subset (c1, c2));
391   fail_if (gst_caps_is_equal (c1, c2));
392   gst_caps_unref (c1);
393   gst_caps_unref (c2);
394
395   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
396   c2 = gst_caps_new_empty ();
397   fail_unless (gst_caps_is_subset (c2, c1));
398   fail_if (gst_caps_is_subset (c1, c2));
399   fail_if (gst_caps_is_equal (c1, c2));
400   gst_caps_unref (c1);
401   gst_caps_unref (c2);
402
403   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
404   c2 = gst_caps_new_any ();
405   fail_if (gst_caps_is_subset (c2, c1));
406   fail_unless (gst_caps_is_subset (c1, c2));
407   fail_if (gst_caps_is_equal (c1, c2));
408   gst_caps_unref (c1);
409   gst_caps_unref (c2);
410 }
411
412 GST_END_TEST;
413
414 GST_START_TEST (test_subset_duplication)
415 {
416   GstCaps *c1, *c2;
417
418   c1 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
419   c2 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
420
421   fail_unless (gst_caps_is_subset (c1, c2));
422   fail_unless (gst_caps_is_subset (c2, c1));
423
424   gst_caps_unref (c2);
425   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE }");
426
427   fail_unless (gst_caps_is_subset (c1, c2));
428   fail_unless (gst_caps_is_subset (c2, c1));
429
430   gst_caps_unref (c2);
431   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE, F32LE }");
432
433   fail_unless (gst_caps_is_subset (c1, c2));
434   fail_unless (gst_caps_is_subset (c2, c1));
435
436   gst_caps_unref (c2);
437   c2 = gst_caps_from_string
438       ("audio/x-raw, format=(string){ F32LE, F32LE, F32LE }");
439
440   fail_unless (gst_caps_is_subset (c1, c2));
441   fail_unless (gst_caps_is_subset (c2, c1));
442
443   gst_caps_unref (c1);
444   gst_caps_unref (c2);
445 }
446
447 GST_END_TEST;
448
449 GST_START_TEST (test_merge_fundamental)
450 {
451   GstCaps *c1, *c2;
452
453   /* ANY + specific = ANY */
454   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
455   c2 = gst_caps_new_any ();
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) == 0, NULL);
459   fail_unless (gst_caps_is_any (c2), NULL);
460   gst_caps_unref (c2);
461
462   /* specific + ANY = ANY */
463   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
464   c1 = gst_caps_new_any ();
465   c2 = gst_caps_merge (c2, c1);
466   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
467   fail_unless (gst_caps_get_size (c2) == 0, NULL);
468   fail_unless (gst_caps_is_any (c2), NULL);
469   gst_caps_unref (c2);
470
471   /* EMPTY + specific = specific */
472   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
473   c2 = gst_caps_new_empty ();
474   c2 = 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   fail_if (gst_caps_is_empty (c2), NULL);
478   gst_caps_unref (c2);
479
480   /* specific + EMPTY = specific */
481   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
482   c1 = gst_caps_new_empty ();
483   c2 = gst_caps_merge (c2, c1);
484   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
485   fail_unless (gst_caps_get_size (c2) == 1, NULL);
486   fail_if (gst_caps_is_empty (c2), NULL);
487   gst_caps_unref (c2);
488 }
489
490 GST_END_TEST;
491
492 GST_START_TEST (test_merge_same)
493 {
494   GstCaps *c1, *c2, *test;
495
496   /* this is the same */
497   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
498   c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
499   c2 = gst_caps_merge (c2, c1);
500   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
501   fail_unless (gst_caps_get_size (c2) == 1, NULL);
502   test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
503   fail_unless (gst_caps_is_equal (c2, test));
504   gst_caps_unref (test);
505   gst_caps_unref (c2);
506
507   /* and so is this */
508   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
509   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
510   c2 = gst_caps_merge (c2, c1);
511   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
512   fail_unless (gst_caps_get_size (c2) == 1, NULL);
513   gst_caps_unref (c2);
514
515   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
516   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
517   c2 = gst_caps_merge (c2, c1);
518   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
519   fail_unless (gst_caps_get_size (c2) == 2, NULL);
520   gst_caps_unref (c2);
521
522   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
523   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
524   c2 = gst_caps_merge (c2, c1);
525   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
526   fail_unless (gst_caps_get_size (c2) == 2, NULL);
527   gst_caps_unref (c2);
528
529   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
530   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
531   c2 = gst_caps_merge (c2, c1);
532   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
533   fail_unless (gst_caps_get_size (c2) == 1, NULL);
534   gst_caps_unref (c2);
535
536   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
537   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
538   c2 = gst_caps_merge (c2, c1);
539   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
540   fail_unless (gst_caps_get_size (c2) == 2, NULL);
541   gst_caps_unref (c2);
542 }
543
544 GST_END_TEST;
545
546 GST_START_TEST (test_merge_subset)
547 {
548   GstCaps *c1, *c2, *test;
549
550   /* the 2nd is already covered */
551   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
552   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
553   c2 = 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) == 1, NULL);
556   test = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
557   fail_unless (gst_caps_is_equal (c2, test));
558   gst_caps_unref (c2);
559   gst_caps_unref (test);
560
561   /* here it is not */
562   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
563   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
564   c2 = gst_caps_merge (c2, c1);
565   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
566   fail_unless (gst_caps_get_size (c2) == 2, NULL);
567   test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
568   fail_unless (gst_caps_is_equal (c2, test));
569   gst_caps_unref (c2);
570   gst_caps_unref (test);
571
572   /* second one was already contained in the first one */
573   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
574   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
575   c2 = gst_caps_merge (c2, c1);
576   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
577   fail_unless (gst_caps_get_size (c2) == 1, NULL);
578   test = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
579   fail_unless (gst_caps_is_equal (c2, test));
580   gst_caps_unref (c2);
581   gst_caps_unref (test);
582
583   /* second one was already contained in the first one */
584   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
585   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
586   c2 = gst_caps_merge (c2, c1);
587   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
588   fail_unless (gst_caps_get_size (c2) == 1, NULL);
589   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
590   fail_unless (gst_caps_is_equal (c2, test));
591   gst_caps_unref (c2);
592   gst_caps_unref (test);
593
594   /* second one was already contained in the first one */
595   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
596   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
597   c2 = gst_caps_merge (c2, c1);
598   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
599   fail_unless (gst_caps_get_size (c2) == 1, NULL);
600   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
601   fail_unless (gst_caps_is_equal (c2, test));
602   gst_caps_unref (c2);
603   gst_caps_unref (test);
604
605   /* second one was already contained in the first one */
606   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
607   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
608   c2 = 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) == 1, NULL);
611   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
612   fail_unless (gst_caps_is_equal (c2, test));
613   gst_caps_unref (c2);
614   gst_caps_unref (test);
615
616   /* these caps cannot be merged */
617   c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
618   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
619   c2 = gst_caps_merge (c2, c1);
620   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
621   fail_unless (gst_caps_get_size (c2) == 2, NULL);
622   test =
623       gst_caps_from_string
624       ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
625   fail_unless (gst_caps_is_equal (c2, test));
626   gst_caps_unref (c2);
627   gst_caps_unref (test);
628
629   /* these caps cannot be merged */
630   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
631   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
632   c2 = gst_caps_merge (c2, c1);
633   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
634   fail_unless (gst_caps_get_size (c2) == 2, NULL);
635   test =
636       gst_caps_from_string
637       ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
638   fail_unless (gst_caps_is_equal (c2, test));
639   gst_caps_unref (c2);
640   gst_caps_unref (test);
641
642   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
643   c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
644   c2 = gst_caps_merge (c2, c1);
645   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
646   fail_unless (gst_caps_get_size (c2) == 2, NULL);
647   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
648       "audio/x-raw,channels={1,2,3,4}");
649   fail_unless (gst_caps_is_equal (c2, test));
650   gst_caps_unref (c2);
651   gst_caps_unref (test);
652
653   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
654   c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
655   c2 = gst_caps_merge (c2, c1);
656   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
657   fail_unless (gst_caps_get_size (c2) == 2, NULL);
658   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
659       "audio/x-raw,channels={1,3}");
660   fail_unless (gst_caps_is_equal (c2, test));
661   gst_caps_unref (c2);
662   gst_caps_unref (test);
663
664   c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
665   c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
666   test = gst_caps_copy (c1);
667   c2 = gst_caps_merge (c2, c1);
668   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
669   fail_unless (gst_caps_is_subset (test, c2));
670   gst_caps_unref (test);
671   gst_caps_unref (c2);
672
673   c2 = gst_caps_from_string ("audio/x-raw");
674   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
675   c2 = gst_caps_merge (c2, c1);
676   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
677   fail_unless (gst_caps_get_size (c2) == 1, NULL);
678   test = gst_caps_from_string ("audio/x-raw");
679   fail_unless (gst_caps_is_equal (c2, test));
680   gst_caps_unref (c2);
681   gst_caps_unref (test);
682
683   c2 = gst_caps_from_string ("audio/x-raw,channels=1");
684   c1 = gst_caps_from_string ("audio/x-raw");
685   c2 = gst_caps_merge (c2, c1);
686   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
687   fail_unless (gst_caps_get_size (c2) == 2, NULL);
688   test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
689   fail_unless (gst_caps_is_equal (c2, test));
690   gst_caps_unref (c2);
691   gst_caps_unref (test);
692 }
693
694 GST_END_TEST;
695
696 GST_START_TEST (test_intersect)
697 {
698   GstStructure *s;
699   GstCaps *c1, *c2, *ci1, *ci2;
700
701   /* field not specified = any value possible, so the intersection
702    * should keep fields which are only part of one set of caps */
703   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
704   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
705
706   ci1 = gst_caps_intersect (c2, c1);
707   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
708   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
709   s = gst_caps_get_structure (ci1, 0);
710   fail_unless (gst_structure_has_name (s, "video/x-raw"));
711   fail_unless (gst_structure_get_value (s, "format") != NULL);
712   fail_unless (gst_structure_get_value (s, "width") != NULL);
713
714   /* with changed order */
715   ci2 = gst_caps_intersect (c1, c2);
716   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
717   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
718   s = gst_caps_get_structure (ci2, 0);
719   fail_unless (gst_structure_has_name (s, "video/x-raw"));
720   fail_unless (gst_structure_get_value (s, "format") != NULL);
721   fail_unless (gst_structure_get_value (s, "width") != NULL);
722
723   fail_unless (gst_caps_is_equal (ci1, ci2));
724
725   gst_caps_unref (ci1);
726   gst_caps_unref (ci2);
727
728   gst_caps_unref (c1);
729   gst_caps_unref (c2);
730
731   /* ========== */
732
733   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
734   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
735
736   ci1 = gst_caps_intersect (c2, c1);
737   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
738   fail_unless (gst_caps_is_empty (ci1), NULL);
739
740   /* with changed order */
741   ci2 = gst_caps_intersect (c1, c2);
742   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
743   fail_unless (gst_caps_is_empty (ci2), NULL);
744
745   fail_unless (gst_caps_is_equal (ci1, ci2));
746
747   gst_caps_unref (ci1);
748   gst_caps_unref (ci2);
749
750   gst_caps_unref (c1);
751   gst_caps_unref (c2);
752
753   /* ========== */
754
755   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
756   c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
757
758   ci1 = gst_caps_intersect (c2, c1);
759   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
760   fail_unless (gst_caps_is_empty (ci1), NULL);
761
762   /* with changed order */
763   ci2 = gst_caps_intersect (c1, c2);
764   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
765   fail_unless (gst_caps_is_empty (ci2), NULL);
766
767   fail_unless (gst_caps_is_equal (ci1, ci2));
768
769   gst_caps_unref (ci1);
770   gst_caps_unref (ci2);
771
772   gst_caps_unref (c1);
773   gst_caps_unref (c2);
774
775   /* ========== */
776
777   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
778   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,height=30");
779
780   ci1 = gst_caps_intersect (c2, c1);
781   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
782   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
783   s = gst_caps_get_structure (ci1, 0);
784   fail_unless (gst_structure_has_name (s, "video/x-raw"));
785   fail_unless (gst_structure_get_value (s, "format") != NULL);
786   fail_unless (gst_structure_get_value (s, "width") != NULL);
787   fail_unless (gst_structure_get_value (s, "height") != NULL);
788
789   /* with changed order */
790   ci2 = gst_caps_intersect (c1, c2);
791   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
792   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
793   s = gst_caps_get_structure (ci2, 0);
794   fail_unless (gst_structure_has_name (s, "video/x-raw"));
795   fail_unless (gst_structure_get_value (s, "format") != NULL);
796   fail_unless (gst_structure_get_value (s, "height") != NULL);
797   fail_unless (gst_structure_get_value (s, "width") != NULL);
798
799   fail_unless (gst_caps_is_equal (ci1, ci2));
800
801   gst_caps_unref (ci1);
802   gst_caps_unref (ci2);
803
804   gst_caps_unref (c1);
805   gst_caps_unref (c2);
806
807   /* ========== */
808
809   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
810   c2 = gst_caps_new_empty ();
811
812   ci1 = gst_caps_intersect (c1, c2);
813   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
814
815   fail_unless (gst_caps_is_empty (ci1));
816   fail_unless (gst_caps_get_size (ci1) == 0);
817
818   ci2 = gst_caps_intersect (c2, c1);
819   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
820
821   fail_unless (gst_caps_is_empty (ci2));
822   fail_unless (gst_caps_get_size (ci2) == 0);
823
824   fail_unless (gst_caps_is_equal (ci1, ci2));
825
826   gst_caps_unref (ci1);
827   gst_caps_unref (ci2);
828
829   gst_caps_unref (c1);
830   gst_caps_unref (c2);
831
832
833   /* ========== */
834
835   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
836   c2 = gst_caps_new_any ();
837
838   ci1 = gst_caps_intersect (c1, c2);
839   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
840
841   fail_unless (gst_caps_is_equal (ci1, c1));
842
843   ci2 = gst_caps_intersect (c2, c1);
844   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
845
846   fail_unless (gst_caps_is_equal (ci2, c1));
847
848   fail_unless (gst_caps_is_equal (ci1, ci2));
849
850   gst_caps_unref (ci1);
851   gst_caps_unref (ci2);
852
853   gst_caps_unref (c1);
854   gst_caps_unref (c2);
855 }
856
857 GST_END_TEST;
858
859 GST_START_TEST (test_intersect2)
860 {
861   GstCaps *caps1, *caps2, *icaps;
862
863   /* tests array subtraction */
864   caps1 = gst_caps_from_string ("audio/x-raw, "
865       "channel-positions=(int)<                      "
866       "{ 1, 2, 3, 4, 5, 6 },                         "
867       "{ 1, 2, 3, 4, 5, 6 },                         "
868       "{ 1, 2, 3, 4, 5, 6 },                         "
869       "{ 1, 2, 3, 4, 5, 6 },                         "
870       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
871   caps2 = gst_caps_from_string ("audio/x-raw, "
872       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
873   icaps = gst_caps_intersect (caps1, caps2);
874   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
875   fail_if (gst_caps_is_empty (icaps));
876   fail_unless (gst_caps_is_equal (icaps, caps2));
877   gst_caps_unref (caps1);
878   gst_caps_unref (caps2);
879   gst_caps_unref (icaps);
880
881   /* ===== */
882
883   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
884   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
885   icaps = gst_caps_intersect (caps1, caps2);
886   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
887   fail_if (gst_caps_is_empty (icaps));
888   fail_unless (gst_caps_is_equal (icaps, caps2));
889   gst_caps_unref (caps1);
890   gst_caps_unref (caps2);
891   gst_caps_unref (icaps);
892 }
893
894 GST_END_TEST;
895
896 GST_START_TEST (test_intersect_list_duplicate)
897 {
898   GstCaps *caps1, *caps2, *icaps;
899
900   /* make sure we don't take too long to intersect these.. */
901   caps1 = gst_caps_from_string ("video/x-raw, format=(string)YV12; "
902       "video/x-raw, format=(string)I420; video/x-raw, format=(string)YUY2; "
903       "video/x-raw, format=(string)UYVY; "
904       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx,"
905       " xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
906       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
907       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
908       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
909       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
910       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
911       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
912       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
913       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, { ABGR, ABGR, "
914       "{ ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
915       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
916       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
917       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
918       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
919       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
920       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
921       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
922       "RGB, BGR, Y41B, Y42B, YVYU, Y444 }; "
923       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, "
924       "xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
925       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
926       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
927       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
928       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
929       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
930       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
931       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
932       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, "
933       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
934       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
935       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
936       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
937       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
938       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
939       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
940       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
941       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }; "
942       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, "
943       "BGRx, xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
944       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
945       "{ RGBA, RGBA, { RGBA, RGBA } } } }, BGRA, ARGB, "
946       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
947       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
948       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }");
949
950   caps2 = gst_caps_copy (caps1);
951
952   icaps = gst_caps_intersect (caps1, caps2);
953
954   gst_caps_unref (caps1);
955   gst_caps_unref (caps2);
956   gst_caps_unref (icaps);
957 }
958
959 GST_END_TEST;
960
961 GST_START_TEST (test_intersect_zigzag)
962 {
963   GstCaps *caps1, *caps2, *icaps, *result;
964
965   /* tests if caps order is maintained */
966   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
967   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
968
969   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
970   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
971   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
972   fail_if (gst_caps_is_empty (icaps));
973   fail_unless (gst_caps_is_equal (icaps, result));
974   gst_caps_unref (icaps);
975   gst_caps_unref (result);
976
977   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
978   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
979   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
980   fail_if (gst_caps_is_empty (icaps));
981   fail_unless (gst_caps_is_equal (icaps, result));
982   gst_caps_unref (icaps);
983   gst_caps_unref (result);
984
985   gst_caps_unref (caps1);
986   gst_caps_unref (caps2);
987 }
988
989 GST_END_TEST;
990
991
992 GST_START_TEST (test_intersect_first)
993 {
994   GstCaps *caps1, *caps2, *icaps, *result;
995
996   /* tests if caps order is maintained */
997   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
998   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
999   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
1000   result = gst_caps_from_string ("format/A; format/C; format/D");
1001   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
1002   fail_if (gst_caps_is_empty (icaps));
1003   fail_unless (gst_caps_is_equal (icaps, result));
1004   gst_caps_unref (caps1);
1005   gst_caps_unref (caps2);
1006   gst_caps_unref (icaps);
1007   gst_caps_unref (result);
1008 }
1009
1010 GST_END_TEST;
1011
1012
1013 GST_START_TEST (test_intersect_first2)
1014 {
1015   GstCaps *caps1, *caps2, *icaps, *result;
1016
1017   /* tests if caps order is maintained */
1018   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
1019   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
1020
1021   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
1022   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
1023   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
1024   fail_if (gst_caps_is_empty (icaps));
1025   fail_unless (gst_caps_is_equal (icaps, result));
1026   gst_caps_unref (icaps);
1027   gst_caps_unref (result);
1028
1029   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
1030   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
1031   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
1032   fail_if (gst_caps_is_empty (icaps));
1033   fail_unless (gst_caps_is_equal (icaps, result));
1034   gst_caps_unref (icaps);
1035   gst_caps_unref (result);
1036
1037   gst_caps_unref (caps1);
1038   gst_caps_unref (caps2);
1039 }
1040
1041 GST_END_TEST;
1042
1043 GST_START_TEST (test_intersect_duplication)
1044 {
1045   GstCaps *c1, *c2, *test;
1046
1047   c1 = gst_caps_from_string
1048       ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
1049   c2 = gst_caps_from_string
1050       ("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 ]");
1051
1052   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
1053   fail_unless_equals_int (gst_caps_get_size (test), 1);
1054   fail_unless (gst_caps_is_equal (c1, test));
1055   gst_caps_unref (c1);
1056   gst_caps_unref (c2);
1057   gst_caps_unref (test);
1058 }
1059
1060 GST_END_TEST;
1061
1062 GST_START_TEST (test_intersect_flagset)
1063 {
1064   GstCaps *c1, *c2, *test;
1065   GType test_flagset_type;
1066   GstSeekFlags test_flags, test_mask;
1067   gchar *test_string;
1068
1069   /* Test that matching bits inside the mask intersect,
1070    * and bits outside the mask don't matter */
1071   c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:fffff0");
1072   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
1073
1074   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
1075   fail_unless_equals_int (gst_caps_get_size (test), 1);
1076   fail_unless (gst_caps_is_equal (c1, test));
1077   gst_caps_unref (c1);
1078   gst_caps_unref (c2);
1079   gst_caps_unref (test);
1080
1081   /* Test that non-matching bits in the mask don't intersect */
1082   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
1083   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
1084
1085   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
1086   fail_unless (gst_caps_is_empty (test));
1087   gst_caps_unref (c1);
1088   gst_caps_unref (c2);
1089   gst_caps_unref (test);
1090
1091   /* Check custom flags type serialisation and de-serialisation */
1092   test_flagset_type = gst_flagset_register (GST_TYPE_SEEK_FLAGS);
1093   fail_unless (g_type_is_a (test_flagset_type, GST_TYPE_FLAG_SET));
1094
1095   test_flags =
1096       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
1097       GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
1098   test_mask =
1099       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
1100       GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
1101
1102   c1 = gst_caps_new_simple ("test/x-caps", "field", test_flagset_type,
1103       test_flags, test_mask, NULL);
1104
1105   test_string = gst_caps_to_string (c1);
1106   fail_if (test_string == NULL);
1107
1108   GST_DEBUG ("Serialised caps to %s", test_string);
1109   c2 = gst_caps_from_string (test_string);
1110   g_free (test_string);
1111
1112   fail_unless (gst_caps_is_equal (c1, c2), "Caps %s != %s",
1113       gst_caps_to_string (c1), gst_caps_to_string (c2));
1114
1115   gst_caps_unref (c1);
1116   gst_caps_unref (c2);
1117 }
1118
1119 GST_END_TEST;
1120
1121 GST_START_TEST (test_union)
1122 {
1123   GstCaps *c1, *c2, *test, *expect;
1124
1125   /* Test that matching bits inside the masks union OK, */
1126   c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:0ffff0");
1127   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
1128
1129   test = gst_caps_merge (c1, c2);
1130   test = gst_caps_simplify (test);
1131   /* c1, c2 now invalid */
1132   fail_unless_equals_int (gst_caps_get_size (test), 1);
1133   gst_caps_unref (test);
1134
1135   /* Test that non-intersecting sets of masked bits are OK */
1136   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
1137   c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:f00000");
1138   expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
1139   test = gst_caps_simplify (gst_caps_merge (c1, c2));
1140   /* c1, c2 now invalid */
1141   GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
1142       expect, test);
1143   fail_unless (gst_caps_is_equal (test, expect));
1144   gst_caps_unref (test);
1145   gst_caps_unref (expect);
1146
1147   /* Test that partially-intersecting sets of masked bits that match are OK */
1148   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
1149   c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:ff0000");
1150   expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
1151   test = gst_caps_simplify (gst_caps_merge (c1, c2));
1152   /* c1, c2 now invalid */
1153   GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
1154       expect, test);
1155   fail_unless (gst_caps_is_equal (test, expect));
1156   gst_caps_unref (test);
1157   gst_caps_unref (expect);
1158 }
1159
1160 GST_END_TEST;
1161
1162 static gboolean
1163 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
1164 {
1165   return gst_value_is_fixed (value);
1166 }
1167
1168
1169 GST_START_TEST (test_normalize)
1170 {
1171   GstCaps *in, *norm, *out;
1172   guint i;
1173
1174   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
1175   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
1176   norm = gst_caps_normalize (in);
1177   fail_if (gst_caps_is_empty (norm));
1178   fail_unless (gst_caps_is_equal (norm, out));
1179   for (i = 0; i < gst_caps_get_size (norm); i++) {
1180     GstStructure *st = gst_caps_get_structure (norm, i);
1181     /* Make sure all fields of all structures are fixed */
1182     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1183   }
1184
1185   gst_caps_unref (out);
1186   gst_caps_unref (norm);
1187
1188   in = gst_caps_from_string
1189       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
1190   out =
1191       gst_caps_from_string
1192       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
1193       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
1194   norm = gst_caps_normalize (in);
1195   fail_if (gst_caps_is_empty (norm));
1196   fail_unless (gst_caps_is_equal (norm, out));
1197   for (i = 0; i < gst_caps_get_size (norm); i++) {
1198     GstStructure *st = gst_caps_get_structure (norm, i);
1199     /* Make sure all fields of all structures are fixed */
1200     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1201   }
1202
1203   gst_caps_unref (out);
1204   gst_caps_unref (norm);
1205
1206   in = gst_caps_from_string
1207       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
1208   out =
1209       gst_caps_from_string
1210       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
1211   norm = gst_caps_normalize (in);
1212   fail_if (gst_caps_is_empty (norm));
1213   fail_unless (gst_caps_is_equal (norm, out));
1214   for (i = 0; i < gst_caps_get_size (norm); i++) {
1215     GstStructure *st = gst_caps_get_structure (norm, i);
1216     /* Make sure all fields of all structures are fixed */
1217     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1218   }
1219
1220   gst_caps_unref (out);
1221   gst_caps_unref (norm);
1222 }
1223
1224 GST_END_TEST;
1225
1226 GST_START_TEST (test_broken)
1227 {
1228   GstCaps *c1 = NULL;
1229
1230   /* NULL is not valid for media_type */
1231   ASSERT_CRITICAL (c1 =
1232       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
1233   fail_if (c1);
1234
1235   /* such a name is not valid, see gst_structure_validate_name() */
1236   ASSERT_CRITICAL (c1 =
1237       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
1238   fail_if (c1);
1239
1240   c1 = gst_caps_new_empty_simple ("test/broken-fraction");
1241   fail_unless (c1);
1242   /* denominator must be non-zero value */
1243   ASSERT_CRITICAL (gst_caps_set_simple (c1, "foo", GST_TYPE_FRACTION, 1, 0,
1244           NULL));
1245   gst_caps_unref (c1);
1246 }
1247
1248 GST_END_TEST;
1249
1250 GST_START_TEST (test_features)
1251 {
1252   GstCaps *c1, *c2, *c3;
1253   GstStructure *s1, *s2;
1254   GstCapsFeatures *f1, *f2;
1255   gchar *str1;
1256   static GstStaticCaps scaps =
1257       GST_STATIC_CAPS
1258       ("video/x-raw(memory:EGLImage), width=320, height=[ 240, 260 ]");
1259
1260   c1 = gst_caps_new_empty ();
1261   fail_unless (c1 != NULL);
1262   s1 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1263       GST_TYPE_INT_RANGE, 240, 260, NULL);
1264   fail_unless (s1 != NULL);
1265   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1266   fail_unless (f1 != NULL);
1267
1268   gst_caps_append_structure_full (c1, s1, f1);
1269   s2 = gst_caps_get_structure (c1, 0);
1270   fail_unless (s1 == s2);
1271   f2 = gst_caps_get_features (c1, 0);
1272   fail_unless (f1 == f2);
1273
1274   str1 = gst_caps_to_string (c1);
1275   fail_unless (str1 != NULL);
1276   c2 = gst_caps_from_string (str1);
1277   fail_unless (c2 != NULL);
1278   g_free (str1);
1279
1280   fail_unless (gst_caps_is_equal (c1, c2));
1281   fail_unless (gst_caps_is_subset (c1, c2));
1282   fail_unless (gst_caps_is_subset (c2, c1));
1283   fail_unless (gst_caps_can_intersect (c1, c2));
1284
1285   gst_caps_unref (c2);
1286
1287   c2 = gst_caps_new_empty ();
1288   fail_unless (c2 != NULL);
1289   s2 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1290       GST_TYPE_INT_RANGE, 240, 260, NULL);
1291   fail_unless (s2 != NULL);
1292   f2 = gst_caps_features_new ("memory:VASurface", "meta:VAMeta", NULL);
1293   fail_unless (f2 != NULL);
1294   gst_caps_append_structure_full (c2, s2, f2);
1295
1296   fail_if (gst_caps_is_equal (c1, c2));
1297   fail_if (gst_caps_is_subset (c1, c2));
1298   fail_if (gst_caps_is_subset (c2, c1));
1299   fail_if (gst_caps_can_intersect (c1, c2));
1300
1301   str1 = gst_caps_to_string (c2);
1302   fail_unless (str1 != NULL);
1303   c3 = gst_caps_from_string (str1);
1304   fail_unless (c3 != NULL);
1305   g_free (str1);
1306
1307   fail_unless (gst_caps_is_equal (c2, c3));
1308   fail_unless (gst_caps_is_subset (c2, c3));
1309   fail_unless (gst_caps_is_subset (c3, c2));
1310   fail_unless (gst_caps_can_intersect (c2, c3));
1311
1312   f1 = gst_caps_get_features (c3, 0);
1313   fail_unless (f1 != NULL);
1314   fail_if (f1 == f2);
1315   gst_caps_features_contains (f1, "memory:VASurface");
1316   gst_caps_features_remove (f1, "memory:VASurface");
1317   fail_if (gst_caps_is_equal (c2, c3));
1318   fail_if (gst_caps_is_subset (c2, c3));
1319   fail_if (gst_caps_is_subset (c3, c2));
1320   fail_if (gst_caps_can_intersect (c2, c3));
1321
1322   gst_caps_unref (c3);
1323   gst_caps_unref (c2);
1324
1325   c2 = gst_static_caps_get (&scaps);
1326   fail_unless (c2 != NULL);
1327   fail_unless (gst_caps_is_equal (c1, c2));
1328   fail_unless (gst_caps_is_subset (c1, c2));
1329   fail_unless (gst_caps_is_subset (c2, c1));
1330   fail_unless (gst_caps_can_intersect (c1, c2));
1331   gst_caps_unref (c2);
1332
1333   c2 = gst_caps_from_string
1334       ("video/x-raw(ANY), width=320, height=[ 240, 260 ]");
1335   fail_unless (c2 != NULL);
1336   fail_if (gst_caps_is_equal (c1, c2));
1337   fail_unless (gst_caps_is_subset (c1, c2));
1338   fail_if (gst_caps_is_subset (c2, c1));
1339   fail_unless (gst_caps_can_intersect (c1, c2));
1340
1341   c3 = gst_caps_intersect (c1, c2);
1342   fail_unless (gst_caps_is_equal (c3, c1));
1343
1344   gst_caps_unref (c3);
1345   gst_caps_unref (c2);
1346   gst_caps_unref (c1);
1347
1348   c1 = gst_caps_from_string ("video/x-raw");
1349   c2 = gst_caps_from_string ("video/x-raw");
1350
1351   f1 = gst_caps_get_features (c1, 0);
1352   gst_caps_features_add (f1, "memory:VASurface");
1353
1354   fail_unless (gst_caps_features_is_equal (f1, gst_caps_get_features (c1, 0)));
1355   fail_if (gst_caps_can_intersect (c1, c2));
1356
1357   f2 = gst_caps_get_features (c2, 0);
1358   fail_unless (gst_caps_features_is_equal
1359       (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY, f2));
1360
1361   gst_caps_unref (c2);
1362   gst_caps_unref (c1);
1363
1364   c1 = gst_caps_from_string ("video/x-raw");
1365   f1 = gst_caps_get_features (c1, 0);
1366   f2 = gst_caps_features_new ("memory:dmabuf", NULL);
1367   gst_caps_set_features (c1, 0, f2);
1368
1369   gst_caps_unref (c1);
1370
1371   /* gst_caps_set_features_simple() */
1372   c1 = gst_caps_from_string
1373       ("video/x-raw, format=NV12; video/x-raw, format=NV16");
1374   fail_unless_equals_int (gst_caps_get_size (c1), 2);
1375
1376   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1377   gst_caps_set_features_simple (c1, f1);
1378
1379   f2 = gst_caps_get_features (c1, 0);
1380   fail_unless (gst_caps_features_is_equal (f1, f2));
1381   f2 = gst_caps_get_features (c1, 1);
1382   fail_unless (gst_caps_features_is_equal (f1, f2));
1383
1384   gst_caps_unref (c1);
1385
1386   c1 = gst_caps_new_any ();
1387   fail_unless_equals_int (gst_caps_get_size (c1), 0);
1388
1389   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1390   /* Nothing to set the features on, but method should still take
1391    * ownership of the given features */
1392   gst_caps_set_features_simple (c1, f1);
1393   fail_unless_equals_int (gst_caps_get_size (c1), 0);
1394   fail_unless (gst_caps_is_any (c1));
1395
1396   gst_caps_unref (c1);
1397 }
1398
1399 GST_END_TEST;
1400
1401 GST_START_TEST (test_special_caps)
1402 {
1403   GstCaps *caps;
1404
1405   caps = gst_caps_new_any ();
1406   fail_unless (gst_caps_is_any (caps));
1407   fail_unless (gst_caps_is_any (caps) == TRUE);
1408   fail_if (gst_caps_is_empty (caps));
1409   fail_unless (gst_caps_is_empty (caps) == FALSE);
1410   gst_caps_unref (caps);
1411
1412   caps = gst_caps_new_empty ();
1413   fail_if (gst_caps_is_any (caps));
1414   fail_unless (gst_caps_is_any (caps) == FALSE);
1415   fail_unless (gst_caps_is_empty (caps));
1416   fail_unless (gst_caps_is_empty (caps) == TRUE);
1417   gst_caps_unref (caps);
1418 }
1419
1420 GST_END_TEST;
1421
1422 static gboolean
1423 foreach_append_function (GstCapsFeatures * features, GstStructure * structure,
1424     gpointer user_data)
1425 {
1426   GstCaps *caps = user_data;
1427
1428   gst_caps_append_structure_full (caps, gst_structure_copy (structure),
1429       features ? gst_caps_features_copy (features) : NULL);
1430
1431   return TRUE;
1432 }
1433
1434 GST_START_TEST (test_foreach)
1435 {
1436   GstCaps *caps, *caps2;
1437
1438   caps =
1439       gst_caps_from_string
1440       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1441   caps2 = gst_caps_new_empty ();
1442   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1443   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1444   gst_caps_unref (caps);
1445   gst_caps_unref (caps2);
1446
1447   caps = gst_caps_new_empty ();
1448   caps2 = gst_caps_new_empty ();
1449   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1450   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1451   gst_caps_unref (caps);
1452   gst_caps_unref (caps2);
1453 }
1454
1455 GST_END_TEST;
1456
1457 static gboolean
1458 map_function (GstCapsFeatures * features, GstStructure * structure,
1459     gpointer user_data)
1460 {
1461   /* Remove caps features if there are any, otherwise add some dummy */
1462   if (gst_caps_features_contains (features, "foo:bar")) {
1463     gst_caps_features_remove (features, "foo:bar");
1464   } else {
1465     gst_caps_features_add (features, "foo:bar");
1466     gst_caps_features_remove (features, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
1467   }
1468
1469   /* Set some dummy integer in the structure */
1470   gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);
1471
1472   return TRUE;
1473 }
1474
1475 GST_START_TEST (test_map_in_place)
1476 {
1477   GstCaps *caps, *caps2;
1478
1479   caps =
1480       gst_caps_from_string
1481       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1482   caps2 =
1483       gst_caps_from_string
1484       ("video/x-raw(foo:bar), foo=(int)123, format=I420; video/x-raw, foo=(int)123; video/x-h264(foo:bar), foo=(int)123");
1485   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1486   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1487   gst_caps_unref (caps);
1488   gst_caps_unref (caps2);
1489
1490   caps = gst_caps_new_empty ();
1491   caps2 = gst_caps_new_empty ();
1492   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1493   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1494   gst_caps_unref (caps);
1495   gst_caps_unref (caps2);
1496 }
1497
1498 GST_END_TEST;
1499
1500 static gboolean
1501 filter_map_function (GstCapsFeatures * features, GstStructure * structure,
1502     gpointer user_data)
1503 {
1504   if (!gst_structure_has_name (structure, "video/x-raw"))
1505     return FALSE;
1506
1507   if (!gst_caps_features_contains (features, "foo:bar"))
1508     return FALSE;
1509
1510   /* Set some dummy integer in the structure */
1511   gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);
1512
1513   return TRUE;
1514 }
1515
1516 GST_START_TEST (test_filter_and_map_in_place)
1517 {
1518   GstCaps *caps, *caps2;
1519
1520   caps =
1521       gst_caps_from_string
1522       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1523   caps2 = gst_caps_from_string ("video/x-raw(foo:bar), foo=(int)123");
1524   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1525   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1526   gst_caps_unref (caps);
1527   gst_caps_unref (caps2);
1528
1529   caps = gst_caps_from_string ("video/x-raw, format=I420; video/x-h264");
1530   caps2 = gst_caps_new_empty ();
1531   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1532   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1533   gst_caps_unref (caps);
1534   gst_caps_unref (caps2);
1535
1536   caps = gst_caps_new_empty ();
1537   caps2 = gst_caps_new_empty ();
1538   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1539   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1540   gst_caps_unref (caps);
1541   gst_caps_unref (caps2);
1542 }
1543
1544 GST_END_TEST;
1545
1546 GST_START_TEST (test_equality)
1547 {
1548   GstCaps *empty1, *empty2, *any1, *any2, *caps1, *caps2, *caps3, *caps4,
1549       *caps5, *caps6, *caps7, *caps8, *caps9;
1550   GstStructure *s;
1551
1552   empty1 = gst_caps_new_empty ();
1553
1554   empty2 = gst_caps_new_empty ();
1555
1556   any1 = gst_caps_new_any ();
1557
1558   any2 = gst_caps_new_any ();
1559   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
1560       G_TYPE_FLOAT, 5.7, NULL);
1561   fail_unless (s);
1562   gst_caps_append_structure (any2, s);
1563   /* should still be ANY after append */
1564
1565   caps1 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
1566       G_TYPE_FLOAT, 5.7, NULL);
1567   fail_unless (caps1);
1568
1569   caps2 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
1570       G_TYPE_FLOAT, 5.7, NULL);
1571   fail_unless (caps2);
1572   /* append an identical structure */
1573   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
1574       G_TYPE_FLOAT, 5.7, NULL);
1575   fail_unless (s);
1576   gst_caps_append_structure (caps2, s);
1577
1578   /* change field name */
1579   caps3 = gst_caps_new_simple ("structure", "intX", G_TYPE_INT, 4, "float",
1580       G_TYPE_FLOAT, 5.7, NULL);
1581   fail_unless (caps3);
1582
1583   /* change field type */
1584   caps4 = gst_caps_new_simple ("structure", "int", G_TYPE_UINT, 4, "float",
1585       G_TYPE_FLOAT, 5.7, NULL);
1586   fail_unless (caps4);
1587
1588   /* change structure name */
1589   caps5 = gst_caps_new_simple ("structureX", "int", G_TYPE_INT, 4, "float",
1590       G_TYPE_FLOAT, 5.7, NULL);
1591   fail_unless (caps5);
1592
1593   /* change field value */
1594   caps6 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 3, "float",
1595       G_TYPE_FLOAT, 5.7, NULL);
1596   fail_unless (caps6);
1597
1598   /* change caps features */
1599   caps7 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
1600       G_TYPE_FLOAT, 5.7, NULL);
1601   fail_unless (caps6);
1602   gst_caps_set_features (caps7, 0, gst_caps_features_new_any ());
1603
1604   /* add structure */
1605   caps8 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
1606       G_TYPE_FLOAT, 5.7, NULL);
1607   fail_unless (caps8);
1608   s = gst_structure_new ("structure2", "string", G_TYPE_STRING, "val", NULL);
1609   fail_unless (s);
1610   gst_caps_append_structure (caps8, s);
1611
1612   /* reverse the order of the structures */
1613   caps9 = gst_caps_new_simple ("structure2", "string", G_TYPE_STRING, "val",
1614       NULL);
1615   fail_unless (caps9);
1616   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
1617       G_TYPE_FLOAT, 5.7, NULL);
1618   fail_unless (s);
1619   gst_caps_append_structure (caps9, s);
1620
1621   fail_unless (gst_caps_is_equal (empty1, empty2) == TRUE);
1622   fail_unless (gst_caps_is_strictly_equal (empty1, empty2) == TRUE);
1623   fail_unless (gst_caps_is_equal (empty1, any1) == FALSE);
1624   fail_unless (gst_caps_is_strictly_equal (empty1, any1) == FALSE);
1625   fail_unless (gst_caps_is_equal (empty1, any2) == FALSE);
1626   fail_unless (gst_caps_is_strictly_equal (empty1, any2) == FALSE);
1627   fail_unless (gst_caps_is_equal (empty1, caps1) == FALSE);
1628   fail_unless (gst_caps_is_strictly_equal (empty1, caps1) == FALSE);
1629   fail_unless (gst_caps_is_equal (empty1, caps2) == FALSE);
1630   fail_unless (gst_caps_is_strictly_equal (empty1, caps2) == FALSE);
1631   fail_unless (gst_caps_is_equal (empty1, caps3) == FALSE);
1632   fail_unless (gst_caps_is_strictly_equal (empty1, caps3) == FALSE);
1633   fail_unless (gst_caps_is_equal (empty1, caps4) == FALSE);
1634   fail_unless (gst_caps_is_strictly_equal (empty1, caps4) == FALSE);
1635   fail_unless (gst_caps_is_equal (empty1, caps5) == FALSE);
1636   fail_unless (gst_caps_is_strictly_equal (empty1, caps5) == FALSE);
1637   fail_unless (gst_caps_is_equal (empty1, caps6) == FALSE);
1638   fail_unless (gst_caps_is_strictly_equal (empty1, caps6) == FALSE);
1639   fail_unless (gst_caps_is_equal (empty1, caps7) == FALSE);
1640   fail_unless (gst_caps_is_strictly_equal (empty1, caps7) == FALSE);
1641   fail_unless (gst_caps_is_equal (empty1, caps8) == FALSE);
1642   fail_unless (gst_caps_is_strictly_equal (empty1, caps8) == FALSE);
1643   fail_unless (gst_caps_is_equal (empty1, caps9) == FALSE);
1644   fail_unless (gst_caps_is_strictly_equal (empty1, caps9) == FALSE);
1645
1646   fail_unless (gst_caps_is_equal (any1, any2) == TRUE);
1647   fail_unless (gst_caps_is_strictly_equal (any1, any2) == TRUE);
1648   fail_unless (gst_caps_is_equal (any1, caps1) == FALSE);
1649   fail_unless (gst_caps_is_strictly_equal (any1, caps1) == FALSE);
1650   fail_unless (gst_caps_is_equal (any1, caps2) == FALSE);
1651   fail_unless (gst_caps_is_strictly_equal (any1, caps2) == FALSE);
1652   fail_unless (gst_caps_is_equal (any1, caps3) == FALSE);
1653   fail_unless (gst_caps_is_strictly_equal (any1, caps3) == FALSE);
1654   fail_unless (gst_caps_is_equal (any1, caps4) == FALSE);
1655   fail_unless (gst_caps_is_strictly_equal (any1, caps4) == FALSE);
1656   fail_unless (gst_caps_is_equal (any1, caps5) == FALSE);
1657   fail_unless (gst_caps_is_strictly_equal (any1, caps5) == FALSE);
1658   fail_unless (gst_caps_is_equal (any1, caps6) == FALSE);
1659   fail_unless (gst_caps_is_strictly_equal (any1, caps6) == FALSE);
1660   fail_unless (gst_caps_is_equal (any1, caps7) == FALSE);
1661   fail_unless (gst_caps_is_strictly_equal (any1, caps7) == FALSE);
1662   fail_unless (gst_caps_is_equal (any1, caps8) == FALSE);
1663   fail_unless (gst_caps_is_strictly_equal (any1, caps8) == FALSE);
1664   fail_unless (gst_caps_is_equal (any1, caps9) == FALSE);
1665   fail_unless (gst_caps_is_strictly_equal (any1, caps9) == FALSE);
1666
1667   fail_unless (gst_caps_is_equal (any2, caps1) == FALSE);
1668   fail_unless (gst_caps_is_strictly_equal (any2, caps1) == FALSE);
1669   fail_unless (gst_caps_is_equal (any2, caps2) == FALSE);
1670   fail_unless (gst_caps_is_strictly_equal (any2, caps2) == FALSE);
1671   fail_unless (gst_caps_is_equal (any2, caps3) == FALSE);
1672   fail_unless (gst_caps_is_strictly_equal (any2, caps3) == FALSE);
1673   fail_unless (gst_caps_is_equal (any2, caps4) == FALSE);
1674   fail_unless (gst_caps_is_strictly_equal (any2, caps4) == FALSE);
1675   fail_unless (gst_caps_is_equal (any2, caps5) == FALSE);
1676   fail_unless (gst_caps_is_strictly_equal (any2, caps5) == FALSE);
1677   fail_unless (gst_caps_is_equal (any2, caps6) == FALSE);
1678   fail_unless (gst_caps_is_strictly_equal (any2, caps6) == FALSE);
1679   fail_unless (gst_caps_is_equal (any2, caps7) == FALSE);
1680   fail_unless (gst_caps_is_strictly_equal (any2, caps7) == FALSE);
1681   fail_unless (gst_caps_is_equal (any2, caps8) == FALSE);
1682   fail_unless (gst_caps_is_strictly_equal (any2, caps8) == FALSE);
1683   fail_unless (gst_caps_is_equal (any2, caps9) == FALSE);
1684   fail_unless (gst_caps_is_strictly_equal (any2, caps9) == FALSE);
1685
1686   /* caps1 and caps2 are equal, but not strictly equal because one has
1687    * two copies of the same structure */
1688   fail_unless (gst_caps_is_equal (caps1, caps2) == TRUE);
1689   fail_unless (gst_caps_is_strictly_equal (caps1, caps2) == FALSE);
1690   fail_unless (gst_caps_is_equal (caps1, caps3) == FALSE);
1691   fail_unless (gst_caps_is_strictly_equal (caps1, caps3) == FALSE);
1692   fail_unless (gst_caps_is_equal (caps1, caps4) == FALSE);
1693   fail_unless (gst_caps_is_strictly_equal (caps1, caps4) == FALSE);
1694   fail_unless (gst_caps_is_equal (caps1, caps5) == FALSE);
1695   fail_unless (gst_caps_is_strictly_equal (caps1, caps5) == FALSE);
1696   fail_unless (gst_caps_is_equal (caps1, caps6) == FALSE);
1697   fail_unless (gst_caps_is_strictly_equal (caps1, caps6) == FALSE);
1698   fail_unless (gst_caps_is_equal (caps1, caps7) == FALSE);
1699   fail_unless (gst_caps_is_strictly_equal (caps1, caps7) == FALSE);
1700   fail_unless (gst_caps_is_equal (caps1, caps8) == FALSE);
1701   fail_unless (gst_caps_is_strictly_equal (caps1, caps8) == FALSE);
1702   fail_unless (gst_caps_is_equal (caps1, caps9) == FALSE);
1703   fail_unless (gst_caps_is_strictly_equal (caps1, caps9) == FALSE);
1704
1705   fail_unless (gst_caps_is_equal (caps2, caps3) == FALSE);
1706   fail_unless (gst_caps_is_strictly_equal (caps2, caps3) == FALSE);
1707   fail_unless (gst_caps_is_equal (caps2, caps4) == FALSE);
1708   fail_unless (gst_caps_is_strictly_equal (caps2, caps4) == FALSE);
1709   fail_unless (gst_caps_is_equal (caps2, caps5) == FALSE);
1710   fail_unless (gst_caps_is_strictly_equal (caps2, caps5) == FALSE);
1711   fail_unless (gst_caps_is_equal (caps2, caps6) == FALSE);
1712   fail_unless (gst_caps_is_strictly_equal (caps2, caps6) == FALSE);
1713   fail_unless (gst_caps_is_equal (caps2, caps7) == FALSE);
1714   fail_unless (gst_caps_is_strictly_equal (caps2, caps7) == FALSE);
1715   fail_unless (gst_caps_is_equal (caps2, caps8) == FALSE);
1716   fail_unless (gst_caps_is_strictly_equal (caps2, caps8) == FALSE);
1717   fail_unless (gst_caps_is_equal (caps2, caps9) == FALSE);
1718   fail_unless (gst_caps_is_strictly_equal (caps2, caps9) == FALSE);
1719
1720   /* caps8 and caps9 are equal, but not strictly equal because their
1721    * order of structures is different */
1722   fail_unless (gst_caps_is_equal (caps8, caps9) == TRUE);
1723   fail_unless (gst_caps_is_strictly_equal (caps8, caps9) == FALSE);
1724
1725   gst_caps_unref (empty1);
1726   gst_caps_unref (empty2);
1727   gst_caps_unref (any1);
1728   gst_caps_unref (any2);
1729   gst_caps_unref (caps1);
1730   gst_caps_unref (caps2);
1731   gst_caps_unref (caps3);
1732   gst_caps_unref (caps4);
1733   gst_caps_unref (caps5);
1734   gst_caps_unref (caps6);
1735   gst_caps_unref (caps7);
1736   gst_caps_unref (caps8);
1737   gst_caps_unref (caps9);
1738 }
1739
1740 GST_END_TEST;
1741
1742 GST_START_TEST (test_remains_any)
1743 {
1744   GstCaps *any, *caps;
1745   GstStructure *s;
1746
1747   /* test append structure to ANY */
1748   any = gst_caps_new_any ();
1749   fail_unless (gst_caps_is_any (any));
1750   fail_if (gst_caps_get_size (any));
1751   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, NULL);
1752   fail_unless (s);
1753   gst_caps_append_structure (any, s);
1754   fail_unless (gst_caps_is_any (any));
1755   fail_if (gst_caps_get_size (any));
1756   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1757
1758   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, NULL);
1759   fail_unless (s);
1760   gst_caps_append_structure_full (any, s, gst_caps_features_new_any ());
1761   fail_unless (gst_caps_is_any (any));
1762   fail_if (gst_caps_get_size (any));
1763   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1764
1765   /* test merge structure with ANY */
1766   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, NULL);
1767   fail_unless (s);
1768   any = gst_caps_merge_structure (any, s);
1769   fail_unless (gst_caps_is_any (any));
1770   fail_if (gst_caps_get_size (any));
1771   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1772
1773   s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, NULL);
1774   fail_unless (s);
1775   any = gst_caps_merge_structure_full (any, s, gst_caps_features_new_any ());
1776   fail_unless (gst_caps_is_any (any));
1777   fail_if (gst_caps_get_size (any));
1778   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1779
1780   /* test appending non-ANY to ANY */
1781   caps = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, NULL);
1782   fail_unless (caps);
1783   gst_caps_append (any, caps);
1784   fail_unless (gst_caps_is_any (any));
1785   fail_if (gst_caps_get_size (any));
1786   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1787
1788   /* test merging non-ANY with ANY */
1789   caps = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, NULL);
1790   fail_unless (caps);
1791   any = gst_caps_merge (any, caps);
1792   fail_unless (gst_caps_is_any (any));
1793   fail_if (gst_caps_get_size (any));
1794   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1795
1796   caps = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, NULL);
1797   fail_unless (caps);
1798   any = gst_caps_merge (caps, any);
1799   fail_unless (gst_caps_is_any (any));
1800   fail_if (gst_caps_get_size (any));
1801   ASSERT_CRITICAL (gst_caps_get_structure (any, 0));
1802
1803   gst_caps_unref (any);
1804
1805   /* test appending ANY to non-ANY */
1806   caps = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, NULL);
1807   fail_unless (caps);
1808   fail_unless_equals_int (gst_caps_get_size (caps), 1);
1809   fail_unless (gst_caps_get_structure (caps, 0));
1810   gst_caps_append (caps, gst_caps_new_any ());
1811   fail_unless (gst_caps_is_any (caps));
1812   fail_if (gst_caps_get_size (caps));
1813   ASSERT_CRITICAL (gst_caps_get_structure (caps, 0));
1814
1815   gst_caps_unref (caps);
1816 }
1817
1818 GST_END_TEST;
1819
1820 static Suite *
1821 gst_caps_suite (void)
1822 {
1823   Suite *s = suite_create ("GstCaps");
1824   TCase *tc_chain = tcase_create ("operations");
1825
1826   suite_add_tcase (s, tc_chain);
1827   tcase_add_test (tc_chain, test_from_string);
1828   tcase_add_test (tc_chain, test_double_append);
1829   tcase_add_test (tc_chain, test_mutability);
1830   tcase_add_test (tc_chain, test_static_caps);
1831   tcase_add_test (tc_chain, test_simplify);
1832   tcase_add_test (tc_chain, test_truncate);
1833   tcase_add_test (tc_chain, test_fixate);
1834   tcase_add_test (tc_chain, test_subset);
1835   tcase_add_test (tc_chain, test_subset_duplication);
1836   tcase_add_test (tc_chain, test_merge_fundamental);
1837   tcase_add_test (tc_chain, test_merge_same);
1838   tcase_add_test (tc_chain, test_merge_subset);
1839   tcase_add_test (tc_chain, test_intersect);
1840   tcase_add_test (tc_chain, test_intersect2);
1841   tcase_add_test (tc_chain, test_intersect_list_duplicate);
1842   tcase_add_test (tc_chain, test_intersect_zigzag);
1843   tcase_add_test (tc_chain, test_intersect_first);
1844   tcase_add_test (tc_chain, test_intersect_first2);
1845   tcase_add_test (tc_chain, test_intersect_duplication);
1846   tcase_add_test (tc_chain, test_intersect_flagset);
1847   tcase_add_test (tc_chain, test_union);
1848   tcase_add_test (tc_chain, test_normalize);
1849   tcase_add_test (tc_chain, test_broken);
1850   tcase_add_test (tc_chain, test_features);
1851   tcase_add_test (tc_chain, test_special_caps);
1852   tcase_add_test (tc_chain, test_foreach);
1853   tcase_add_test (tc_chain, test_map_in_place);
1854   tcase_add_test (tc_chain, test_filter_and_map_in_place);
1855   tcase_add_test (tc_chain, test_equality);
1856   tcase_add_test (tc_chain, test_remains_any);
1857
1858   return s;
1859 }
1860
1861 GST_CHECK_MAIN (gst_caps);