acceb6f83a81ee3d8efb608146f7bed56fbd3122
[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 #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
47     fail_unless (gst_caps_is_equal (caps, caps));
48     fail_unless (gst_caps_is_equal (caps, caps2));
49
50     gst_caps_unref (caps);
51     gst_caps_unref (caps2);
52     g_free (to_str);
53   }
54 }
55
56 GST_END_TEST;
57
58 GST_START_TEST (test_double_append)
59 {
60   GstStructure *s1;
61   GstCaps *c1;
62
63   c1 = gst_caps_new_any ();
64   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
65   gst_caps_append_structure (c1, s1);
66   ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
67
68   gst_caps_unref (c1);
69 }
70
71 GST_END_TEST;
72
73 GST_START_TEST (test_mutability)
74 {
75   GstStructure *s1;
76   GstCaps *c1;
77   gint ret;
78
79   c1 = gst_caps_new_any ();
80   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
81   gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
82   gst_caps_append_structure (c1, s1);
83   gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
84   gst_caps_ref (c1);
85   ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
86   fail_unless (gst_structure_get_int (s1, "rate", &ret));
87   fail_unless (ret == 22500);
88   ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
89   fail_unless (gst_structure_get_int (s1, "rate", &ret));
90   fail_unless (ret == 22500);
91   gst_caps_unref (c1);
92   gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
93   fail_unless (gst_structure_get_int (s1, "rate", &ret));
94   fail_unless (ret == 11250);
95   gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
96   fail_unless (gst_structure_get_int (s1, "rate", &ret));
97   fail_unless (ret == 1);
98   gst_caps_unref (c1);
99 }
100
101 GST_END_TEST;
102
103 GST_START_TEST (test_static_caps)
104 {
105   static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw,rate=44100");
106   GstCaps *caps1;
107   GstCaps *caps2;
108   static GstStaticCaps sany = GST_STATIC_CAPS_ANY;
109   static GstStaticCaps snone = GST_STATIC_CAPS_NONE;
110
111   /* caps creation */
112   caps1 = gst_static_caps_get (&scaps);
113   fail_unless (caps1 != NULL);
114   /* 1 refcount core, one from us */
115   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
116
117   /* caps should be the same */
118   caps2 = gst_static_caps_get (&scaps);
119   fail_unless (caps2 != NULL);
120   /* 1 refcount core, two from us */
121   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
122   /* caps must be equal */
123   fail_unless (caps1 == caps2);
124
125   gst_caps_unref (caps1);
126   gst_caps_unref (caps2);
127
128   caps1 = gst_static_caps_get (&sany);
129   fail_unless (gst_caps_is_equal (caps1, GST_CAPS_ANY));
130   caps2 = gst_static_caps_get (&snone);
131   fail_unless (gst_caps_is_equal (caps2, GST_CAPS_NONE));
132   fail_if (gst_caps_is_equal (caps1, caps2));
133   gst_caps_unref (caps1);
134   gst_caps_unref (caps2);
135 }
136
137 GST_END_TEST;
138
139 static const gchar non_simple_caps_string[] =
140     "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
141     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
142     "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
143     "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
144     "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
145     "height=(int)[ 16, 4096 ]; video/x-raw, "
146     "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
147     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
148
149 static gboolean
150 check_string_list (const GValue * format_value)
151 {
152   const GValue *string_value;
153   gboolean got_rgb8 = FALSE;
154   gboolean got_yv12 = FALSE;
155   gboolean got_i420 = FALSE;
156   gboolean got_yuy2 = FALSE;
157   const gchar *string;
158
159   string_value = gst_value_list_get_value (format_value, 0);
160   fail_unless (string_value != NULL);
161   fail_unless (G_VALUE_HOLDS_STRING (string_value));
162   string = g_value_get_string (string_value);
163   fail_unless (string != NULL);
164   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
165   got_i420 = got_i420 || (g_str_equal (string, "I420"));
166   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
167   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
168
169   string_value = gst_value_list_get_value (format_value, 1);
170   fail_unless (string_value != NULL);
171   fail_unless (G_VALUE_HOLDS_STRING (string_value));
172   string = g_value_get_string (string_value);
173   fail_unless (string != NULL);
174   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
175   got_i420 = got_i420 || (g_str_equal (string, "I420"));
176   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
177   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
178
179   string_value = gst_value_list_get_value (format_value, 2);
180   fail_unless (string_value != NULL);
181   fail_unless (G_VALUE_HOLDS_STRING (string_value));
182   string = g_value_get_string (string_value);
183   fail_unless (string != NULL);
184   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
185   got_i420 = got_i420 || (g_str_equal (string, "I420"));
186   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
187   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
188
189   string_value = gst_value_list_get_value (format_value, 3);
190   fail_unless (string_value != NULL);
191   fail_unless (G_VALUE_HOLDS_STRING (string_value));
192   string = g_value_get_string (string_value);
193   fail_unless (string != NULL);
194   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
195   got_i420 = got_i420 || (g_str_equal (string, "I420"));
196   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
197   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
198
199   return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
200 }
201
202 GST_START_TEST (test_simplify)
203 {
204   GstStructure *s1;
205   GstCaps *caps;
206
207   caps = gst_caps_from_string (non_simple_caps_string);
208   fail_unless (caps != NULL,
209       "gst_caps_from_string (non_simple_caps_string) failed");
210
211   caps = gst_caps_simplify (caps);
212   fail_unless (caps != NULL, "gst_caps_simplify() should have worked");
213
214   /* check simplified caps, should be:
215    *
216    * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
217    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
218    *     framerate=(fraction)[ 1/100, 100 ]
219    */
220   GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
221   fail_unless (gst_caps_get_size (caps) == 1);
222   s1 = gst_caps_get_structure (caps, 0);
223   fail_unless (s1 != NULL);
224
225   fail_unless (gst_structure_has_name (s1, "video/x-raw"));
226   {
227     const GValue *framerate_value;
228     const GValue *format_value;
229     const GValue *width_value;
230     const GValue *height_value;
231     const GValue *val_fps;
232     GValue test_fps = { 0, };
233     gint min_width, max_width;
234     gint min_height, max_height;
235
236     format_value = gst_structure_get_value (s1, "format");
237     fail_unless (format_value != NULL);
238     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
239     fail_unless (gst_value_list_get_size (format_value) == 4);
240     fail_unless (check_string_list (format_value) == TRUE);
241
242     g_value_init (&test_fps, GST_TYPE_FRACTION);
243     framerate_value = gst_structure_get_value (s1, "framerate");
244     fail_unless (framerate_value != NULL);
245     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
246
247     val_fps = gst_value_get_fraction_range_min (framerate_value);
248     gst_value_set_fraction (&test_fps, 1, 100);
249     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
250
251     val_fps = gst_value_get_fraction_range_max (framerate_value);
252     gst_value_set_fraction (&test_fps, 100, 1);
253     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
254
255     g_value_unset (&test_fps);
256
257     width_value = gst_structure_get_value (s1, "width");
258     fail_unless (width_value != NULL);
259     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
260     min_width = gst_value_get_int_range_min (width_value);
261     max_width = gst_value_get_int_range_max (width_value);
262     fail_unless (min_width == 16 && max_width == 4096);
263
264     height_value = gst_structure_get_value (s1, "height");
265     fail_unless (height_value != NULL);
266     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
267     min_height = gst_value_get_int_range_min (height_value);
268     max_height = gst_value_get_int_range_max (height_value);
269     fail_unless (min_height == 16 && max_height == 4096);
270   }
271
272   gst_caps_unref (caps);
273 }
274
275 GST_END_TEST;
276
277 GST_START_TEST (test_truncate)
278 {
279   GstCaps *caps;
280
281   caps = gst_caps_from_string (non_simple_caps_string);
282   fail_unless (caps != NULL,
283       "gst_caps_from_string (non_simple_caps_string) failed");
284   fail_unless_equals_int (gst_caps_get_size (caps), 4);
285   caps = gst_caps_truncate (caps);
286   fail_unless_equals_int (gst_caps_get_size (caps), 1);
287   gst_caps_unref (caps);
288 }
289
290 GST_END_TEST;
291
292 GST_START_TEST (test_subset)
293 {
294   GstCaps *c1, *c2;
295
296   c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
297   c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
298   fail_unless (gst_caps_is_subset (c2, c1));
299   fail_if (gst_caps_is_subset (c1, c2));
300   gst_caps_unref (c1);
301   gst_caps_unref (c2);
302
303   c1 = gst_caps_from_string
304       ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
305   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
306   fail_unless (gst_caps_is_subset (c2, c1));
307   fail_if (gst_caps_is_subset (c1, c2));
308   gst_caps_unref (c1);
309   gst_caps_unref (c2);
310
311   c1 = gst_caps_from_string ("audio/x-raw, channels=(int) {1}");
312   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
313   fail_unless (gst_caps_is_subset (c2, c1));
314   fail_unless (gst_caps_is_subset (c1, c2));
315   fail_unless (gst_caps_is_equal (c1, c2));
316   gst_caps_unref (c1);
317   gst_caps_unref (c2);
318
319   c1 = gst_caps_from_string
320       ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
321   c2 = gst_caps_from_string
322       ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
323   fail_unless (gst_caps_is_subset (c1, c2));
324   fail_if (gst_caps_is_subset (c2, c1));
325   gst_caps_unref (c1);
326   gst_caps_unref (c2);
327
328   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
329   c2 = gst_caps_from_string
330       ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)nal");
331   fail_if (gst_caps_is_subset (c2, c1));
332   fail_if (gst_caps_is_subset (c1, c2));
333   fail_if (gst_caps_is_equal (c1, c2));
334   gst_caps_unref (c1);
335   gst_caps_unref (c2);
336 }
337
338 GST_END_TEST;
339
340 GST_START_TEST (test_subset_duplication)
341 {
342   GstCaps *c1, *c2;
343
344   c1 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
345   c2 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
346
347   fail_unless (gst_caps_is_subset (c1, c2));
348   fail_unless (gst_caps_is_subset (c2, c1));
349
350   gst_caps_unref (c2);
351   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE }");
352
353   fail_unless (gst_caps_is_subset (c1, c2));
354   fail_unless (gst_caps_is_subset (c2, c1));
355
356   gst_caps_unref (c2);
357   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE, F32LE }");
358
359   fail_unless (gst_caps_is_subset (c1, c2));
360   fail_unless (gst_caps_is_subset (c2, c1));
361
362   gst_caps_unref (c2);
363   c2 = gst_caps_from_string
364       ("audio/x-raw, format=(string){ F32LE, F32LE, F32LE }");
365
366   fail_unless (gst_caps_is_subset (c1, c2));
367   fail_unless (gst_caps_is_subset (c2, c1));
368
369   gst_caps_unref (c1);
370   gst_caps_unref (c2);
371 }
372
373 GST_END_TEST;
374
375 GST_START_TEST (test_merge_fundamental)
376 {
377   GstCaps *c1, *c2;
378
379   /* ANY + specific = ANY */
380   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
381   c2 = gst_caps_new_any ();
382   c2 = gst_caps_merge (c2, c1);
383   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
384   fail_unless (gst_caps_get_size (c2) == 0, NULL);
385   fail_unless (gst_caps_is_any (c2), NULL);
386   gst_caps_unref (c2);
387
388   /* specific + ANY = ANY */
389   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
390   c1 = gst_caps_new_any ();
391   c2 = gst_caps_merge (c2, c1);
392   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
393   fail_unless (gst_caps_get_size (c2) == 0, NULL);
394   fail_unless (gst_caps_is_any (c2), NULL);
395   gst_caps_unref (c2);
396
397   /* EMPTY + specific = specific */
398   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
399   c2 = gst_caps_new_empty ();
400   c2 = gst_caps_merge (c2, c1);
401   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
402   fail_unless (gst_caps_get_size (c2) == 1, NULL);
403   fail_if (gst_caps_is_empty (c2), NULL);
404   gst_caps_unref (c2);
405
406   /* specific + EMPTY = specific */
407   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
408   c1 = gst_caps_new_empty ();
409   c2 = gst_caps_merge (c2, c1);
410   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
411   fail_unless (gst_caps_get_size (c2) == 1, NULL);
412   fail_if (gst_caps_is_empty (c2), NULL);
413   gst_caps_unref (c2);
414 }
415
416 GST_END_TEST;
417
418 GST_START_TEST (test_merge_same)
419 {
420   GstCaps *c1, *c2, *test;
421
422   /* this is the same */
423   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
424   c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
425   c2 = gst_caps_merge (c2, c1);
426   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
427   fail_unless (gst_caps_get_size (c2) == 1, NULL);
428   test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
429   fail_unless (gst_caps_is_equal (c2, test));
430   gst_caps_unref (test);
431   gst_caps_unref (c2);
432
433   /* and so is this */
434   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
435   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
436   c2 = gst_caps_merge (c2, c1);
437   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
438   fail_unless (gst_caps_get_size (c2) == 1, NULL);
439   gst_caps_unref (c2);
440
441   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
442   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
443   c2 = gst_caps_merge (c2, c1);
444   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
445   fail_unless (gst_caps_get_size (c2) == 2, NULL);
446   gst_caps_unref (c2);
447
448   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
449   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
450   c2 = gst_caps_merge (c2, c1);
451   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
452   fail_unless (gst_caps_get_size (c2) == 2, NULL);
453   gst_caps_unref (c2);
454
455   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
456   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
457   c2 = gst_caps_merge (c2, c1);
458   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
459   fail_unless (gst_caps_get_size (c2) == 1, NULL);
460   gst_caps_unref (c2);
461
462   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
463   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
464   c2 = gst_caps_merge (c2, c1);
465   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
466   fail_unless (gst_caps_get_size (c2) == 2, NULL);
467   gst_caps_unref (c2);
468 }
469
470 GST_END_TEST;
471
472 GST_START_TEST (test_merge_subset)
473 {
474   GstCaps *c1, *c2, *test;
475
476   /* the 2nd is already covered */
477   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
478   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
479   c2 = gst_caps_merge (c2, c1);
480   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
481   fail_unless (gst_caps_get_size (c2) == 1, NULL);
482   test = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
483   fail_unless (gst_caps_is_equal (c2, test));
484   gst_caps_unref (c2);
485   gst_caps_unref (test);
486
487   /* here it is not */
488   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
489   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
490   c2 = gst_caps_merge (c2, c1);
491   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
492   fail_unless (gst_caps_get_size (c2) == 2, NULL);
493   test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
494   fail_unless (gst_caps_is_equal (c2, test));
495   gst_caps_unref (c2);
496   gst_caps_unref (test);
497
498   /* second one was already contained in the first one */
499   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
500   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
501   c2 = gst_caps_merge (c2, c1);
502   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
503   fail_unless (gst_caps_get_size (c2) == 1, NULL);
504   test = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
505   fail_unless (gst_caps_is_equal (c2, test));
506   gst_caps_unref (c2);
507   gst_caps_unref (test);
508
509   /* second one was already contained in the first one */
510   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
511   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
512   c2 = gst_caps_merge (c2, c1);
513   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
514   fail_unless (gst_caps_get_size (c2) == 1, NULL);
515   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
516   fail_unless (gst_caps_is_equal (c2, test));
517   gst_caps_unref (c2);
518   gst_caps_unref (test);
519
520   /* second one was already contained in the first one */
521   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
522   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
523   c2 = gst_caps_merge (c2, c1);
524   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
525   fail_unless (gst_caps_get_size (c2) == 1, NULL);
526   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
527   fail_unless (gst_caps_is_equal (c2, test));
528   gst_caps_unref (c2);
529   gst_caps_unref (test);
530
531   /* second one was already contained in the first one */
532   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
533   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
534   c2 = gst_caps_merge (c2, c1);
535   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
536   fail_unless (gst_caps_get_size (c2) == 1, NULL);
537   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
538   fail_unless (gst_caps_is_equal (c2, test));
539   gst_caps_unref (c2);
540   gst_caps_unref (test);
541
542   /* these caps cannot be merged */
543   c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
544   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
545   c2 = gst_caps_merge (c2, c1);
546   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
547   fail_unless (gst_caps_get_size (c2) == 2, NULL);
548   test =
549       gst_caps_from_string
550       ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
551   fail_unless (gst_caps_is_equal (c2, test));
552   gst_caps_unref (c2);
553   gst_caps_unref (test);
554
555   /* these caps cannot be merged */
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 =
562       gst_caps_from_string
563       ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
564   fail_unless (gst_caps_is_equal (c2, test));
565   gst_caps_unref (c2);
566   gst_caps_unref (test);
567
568   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
569   c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
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_get_size (c2) == 2, NULL);
573   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
574       "audio/x-raw,channels={1,2,3,4}");
575   fail_unless (gst_caps_is_equal (c2, test));
576   gst_caps_unref (c2);
577   gst_caps_unref (test);
578
579   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
580   c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
581   c2 = gst_caps_merge (c2, c1);
582   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
583   fail_unless (gst_caps_get_size (c2) == 2, NULL);
584   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
585       "audio/x-raw,channels={1,3}");
586   fail_unless (gst_caps_is_equal (c2, test));
587   gst_caps_unref (c2);
588   gst_caps_unref (test);
589
590   c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
591   c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
592   test = gst_caps_copy (c1);
593   c2 = gst_caps_merge (c2, c1);
594   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
595   fail_unless (gst_caps_is_subset (test, c2));
596   gst_caps_unref (test);
597   gst_caps_unref (c2);
598
599   c2 = gst_caps_from_string ("audio/x-raw");
600   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
601   c2 = gst_caps_merge (c2, c1);
602   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
603   fail_unless (gst_caps_get_size (c2) == 1, NULL);
604   test = gst_caps_from_string ("audio/x-raw");
605   fail_unless (gst_caps_is_equal (c2, test));
606   gst_caps_unref (c2);
607   gst_caps_unref (test);
608
609   c2 = gst_caps_from_string ("audio/x-raw,channels=1");
610   c1 = gst_caps_from_string ("audio/x-raw");
611   c2 = gst_caps_merge (c2, c1);
612   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
613   fail_unless (gst_caps_get_size (c2) == 2, NULL);
614   test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
615   fail_unless (gst_caps_is_equal (c2, test));
616   gst_caps_unref (c2);
617   gst_caps_unref (test);
618 }
619
620 GST_END_TEST;
621
622 GST_START_TEST (test_intersect)
623 {
624   GstStructure *s;
625   GstCaps *c1, *c2, *ci1, *ci2;
626
627   /* field not specified = any value possible, so the intersection
628    * should keep fields which are only part of one set of caps */
629   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
630   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
631
632   ci1 = gst_caps_intersect (c2, c1);
633   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
634   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
635   s = gst_caps_get_structure (ci1, 0);
636   fail_unless (gst_structure_has_name (s, "video/x-raw"));
637   fail_unless (gst_structure_get_value (s, "format") != NULL);
638   fail_unless (gst_structure_get_value (s, "width") != NULL);
639
640   /* with changed order */
641   ci2 = gst_caps_intersect (c1, c2);
642   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
643   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
644   s = gst_caps_get_structure (ci2, 0);
645   fail_unless (gst_structure_has_name (s, "video/x-raw"));
646   fail_unless (gst_structure_get_value (s, "format") != NULL);
647   fail_unless (gst_structure_get_value (s, "width") != NULL);
648
649   fail_unless (gst_caps_is_equal (ci1, ci2));
650
651   gst_caps_unref (ci1);
652   gst_caps_unref (ci2);
653
654   gst_caps_unref (c1);
655   gst_caps_unref (c2);
656
657   /* ========== */
658
659   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
660   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
661
662   ci1 = gst_caps_intersect (c2, c1);
663   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
664   fail_unless (gst_caps_is_empty (ci1), NULL);
665
666   /* with changed order */
667   ci2 = gst_caps_intersect (c1, c2);
668   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
669   fail_unless (gst_caps_is_empty (ci2), NULL);
670
671   fail_unless (gst_caps_is_equal (ci1, ci2));
672
673   gst_caps_unref (ci1);
674   gst_caps_unref (ci2);
675
676   gst_caps_unref (c1);
677   gst_caps_unref (c2);
678
679   /* ========== */
680
681   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
682   c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
683
684   ci1 = gst_caps_intersect (c2, c1);
685   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
686   fail_unless (gst_caps_is_empty (ci1), NULL);
687
688   /* with changed order */
689   ci2 = gst_caps_intersect (c1, c2);
690   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
691   fail_unless (gst_caps_is_empty (ci2), NULL);
692
693   fail_unless (gst_caps_is_equal (ci1, ci2));
694
695   gst_caps_unref (ci1);
696   gst_caps_unref (ci2);
697
698   gst_caps_unref (c1);
699   gst_caps_unref (c2);
700
701   /* ========== */
702
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,height=30");
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   fail_unless (gst_structure_get_value (s, "height") != NULL);
714
715   /* with changed order */
716   ci2 = gst_caps_intersect (c1, c2);
717   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
718   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
719   s = gst_caps_get_structure (ci2, 0);
720   fail_unless (gst_structure_has_name (s, "video/x-raw"));
721   fail_unless (gst_structure_get_value (s, "format") != NULL);
722   fail_unless (gst_structure_get_value (s, "height") != NULL);
723   fail_unless (gst_structure_get_value (s, "width") != NULL);
724
725   fail_unless (gst_caps_is_equal (ci1, ci2));
726
727   gst_caps_unref (ci1);
728   gst_caps_unref (ci2);
729
730   gst_caps_unref (c1);
731   gst_caps_unref (c2);
732 }
733
734 GST_END_TEST;
735
736 GST_START_TEST (test_intersect2)
737 {
738   GstCaps *caps1, *caps2, *icaps;
739
740   /* tests array subtraction */
741   caps1 = gst_caps_from_string ("audio/x-raw, "
742       "channel-positions=(int)<                      "
743       "{ 1, 2, 3, 4, 5, 6 },                         "
744       "{ 1, 2, 3, 4, 5, 6 },                         "
745       "{ 1, 2, 3, 4, 5, 6 },                         "
746       "{ 1, 2, 3, 4, 5, 6 },                         "
747       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
748   caps2 = gst_caps_from_string ("audio/x-raw, "
749       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
750   icaps = gst_caps_intersect (caps1, caps2);
751   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
752   fail_if (gst_caps_is_empty (icaps));
753   fail_unless (gst_caps_is_equal (icaps, caps2));
754   gst_caps_unref (caps1);
755   gst_caps_unref (caps2);
756   gst_caps_unref (icaps);
757
758   /* ===== */
759
760   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
761   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
762   icaps = gst_caps_intersect (caps1, caps2);
763   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
764   fail_if (gst_caps_is_empty (icaps));
765   fail_unless (gst_caps_is_equal (icaps, caps2));
766   gst_caps_unref (caps1);
767   gst_caps_unref (caps2);
768   gst_caps_unref (icaps);
769 }
770
771 GST_END_TEST;
772
773 GST_START_TEST (test_intersect_list_duplicate)
774 {
775   GstCaps *caps1, *caps2, *icaps;
776
777   /* make sure we don't take too long to intersect these.. */
778   caps1 = gst_caps_from_string ("video/x-raw, format=(string)YV12; "
779       "video/x-raw, format=(string)I420; video/x-raw, format=(string)YUY2; "
780       "video/x-raw, format=(string)UYVY; "
781       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx,"
782       " xRGB, xBGR, { 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 } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
787       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
788       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
789       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
790       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, { ABGR, ABGR, "
791       "{ 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       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
796       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
797       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
798       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
799       "RGB, BGR, Y41B, Y42B, YVYU, Y444 }; "
800       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, "
801       "xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
802       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
803       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
804       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
805       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
806       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
807       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
808       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
809       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, "
810       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
811       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
812       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
813       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
814       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
815       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
816       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
817       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
818       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }; "
819       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, "
820       "BGRx, xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
821       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
822       "{ RGBA, RGBA, { RGBA, RGBA } } } }, BGRA, ARGB, "
823       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
824       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
825       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }");
826
827   caps2 = gst_caps_copy (caps1);
828
829   icaps = gst_caps_intersect (caps1, caps2);
830
831   gst_caps_unref (caps1);
832   gst_caps_unref (caps2);
833   gst_caps_unref (icaps);
834 }
835
836 GST_END_TEST;
837
838 GST_START_TEST (test_intersect_zigzag)
839 {
840   GstCaps *caps1, *caps2, *icaps, *result;
841
842   /* tests if caps order is maintained */
843   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
844   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
845
846   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
847   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
848   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
849   fail_if (gst_caps_is_empty (icaps));
850   fail_unless (gst_caps_is_equal (icaps, result));
851   gst_caps_unref (icaps);
852   gst_caps_unref (result);
853
854   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
855   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
856   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
857   fail_if (gst_caps_is_empty (icaps));
858   fail_unless (gst_caps_is_equal (icaps, result));
859   gst_caps_unref (icaps);
860   gst_caps_unref (result);
861
862   gst_caps_unref (caps1);
863   gst_caps_unref (caps2);
864 }
865
866 GST_END_TEST;
867
868
869 GST_START_TEST (test_intersect_first)
870 {
871   GstCaps *caps1, *caps2, *icaps, *result;
872
873   /* tests if caps order is maintained */
874   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
875   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
876   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
877   result = gst_caps_from_string ("format/A; format/C; format/D");
878   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
879   fail_if (gst_caps_is_empty (icaps));
880   fail_unless (gst_caps_is_equal (icaps, result));
881   gst_caps_unref (caps1);
882   gst_caps_unref (caps2);
883   gst_caps_unref (icaps);
884   gst_caps_unref (result);
885 }
886
887 GST_END_TEST;
888
889
890 GST_START_TEST (test_intersect_first2)
891 {
892   GstCaps *caps1, *caps2, *icaps, *result;
893
894   /* tests if caps order is maintained */
895   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
896   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
897
898   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
899   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
900   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
901   fail_if (gst_caps_is_empty (icaps));
902   fail_unless (gst_caps_is_equal (icaps, result));
903   gst_caps_unref (icaps);
904   gst_caps_unref (result);
905
906   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
907   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
908   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
909   fail_if (gst_caps_is_empty (icaps));
910   fail_unless (gst_caps_is_equal (icaps, result));
911   gst_caps_unref (icaps);
912   gst_caps_unref (result);
913
914   gst_caps_unref (caps1);
915   gst_caps_unref (caps2);
916 }
917
918 GST_END_TEST;
919
920 GST_START_TEST (test_intersect_duplication)
921 {
922   GstCaps *c1, *c2, *test;
923
924   c1 = gst_caps_from_string
925       ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
926   c2 = gst_caps_from_string
927       ("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 ]");
928
929   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
930   fail_unless_equals_int (gst_caps_get_size (test), 1);
931   fail_unless (gst_caps_is_equal (c1, test));
932   gst_caps_unref (c1);
933   gst_caps_unref (c2);
934   gst_caps_unref (test);
935 }
936
937 GST_END_TEST;
938
939 GST_START_TEST (test_intersect_flagset)
940 {
941   GstCaps *c1, *c2, *test;
942   GType test_flagset_type;
943   GstSeekFlags test_flags, test_mask;
944   gchar *test_string;
945
946   /* Test that matching bits inside the mask intersect,
947    * and bits outside the mask don't matter */
948   c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:fffff0");
949   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
950
951   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
952   fail_unless_equals_int (gst_caps_get_size (test), 1);
953   fail_unless (gst_caps_is_equal (c1, test));
954   gst_caps_unref (c1);
955   gst_caps_unref (c2);
956   gst_caps_unref (test);
957
958   /* Test that non-matching bits in the mask don't intersect */
959   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
960   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
961
962   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
963   fail_unless (gst_caps_is_empty (test));
964   gst_caps_unref (c1);
965   gst_caps_unref (c2);
966   gst_caps_unref (test);
967
968   /* Check custom flags type serialisation and de-serialisation */
969   test_flagset_type = gst_flagset_register (GST_TYPE_SEEK_FLAGS);
970   fail_unless (g_type_is_a (test_flagset_type, GST_TYPE_FLAG_SET));
971
972   test_flags =
973       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
974       GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
975   test_mask =
976       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
977       GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
978
979   c1 = gst_caps_new_simple ("test/x-caps", "field", test_flagset_type,
980       test_flags, test_mask, NULL);
981
982   test_string = gst_caps_to_string (c1);
983   fail_if (test_string == NULL);
984
985   GST_DEBUG ("Serialised caps to %s", test_string);
986   c2 = gst_caps_from_string (test_string);
987   g_free (test_string);
988
989   fail_unless (gst_caps_is_equal (c1, c2), "Caps %s != %s",
990       gst_caps_to_string (c1), gst_caps_to_string (c2));
991
992   gst_caps_unref (c1);
993   gst_caps_unref (c2);
994 }
995
996 GST_END_TEST;
997
998 GST_START_TEST (test_union)
999 {
1000   GstCaps *c1, *c2, *test, *expect;
1001
1002   /* Test that matching bits inside the masks union OK, */
1003   c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:0ffff0");
1004   c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");
1005
1006   test = gst_caps_merge (c1, c2);
1007   test = gst_caps_simplify (test);
1008   /* c1, c2 now invalid */
1009   fail_unless_equals_int (gst_caps_get_size (test), 1);
1010   gst_caps_unref (test);
1011
1012   /* Test that non-intersecting sets of masked bits are OK */
1013   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
1014   c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:f00000");
1015   expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
1016   test = gst_caps_simplify (gst_caps_merge (c1, c2));
1017   /* c1, c2 now invalid */
1018   GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
1019       expect, test);
1020   fail_unless (gst_caps_is_equal (test, expect));
1021   gst_caps_unref (test);
1022   gst_caps_unref (expect);
1023
1024   /* Test that partially-intersecting sets of masked bits that match are OK */
1025   c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
1026   c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:ff0000");
1027   expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
1028   test = gst_caps_simplify (gst_caps_merge (c1, c2));
1029   /* c1, c2 now invalid */
1030   GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
1031       expect, test);
1032   fail_unless (gst_caps_is_equal (test, expect));
1033   gst_caps_unref (test);
1034   gst_caps_unref (expect);
1035 }
1036
1037 GST_END_TEST;
1038
1039 static gboolean
1040 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
1041 {
1042   return gst_value_is_fixed (value);
1043 }
1044
1045
1046 GST_START_TEST (test_normalize)
1047 {
1048   GstCaps *in, *norm, *out;
1049   guint i;
1050
1051   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
1052   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
1053   norm = gst_caps_normalize (in);
1054   fail_if (gst_caps_is_empty (norm));
1055   fail_unless (gst_caps_is_equal (norm, out));
1056   for (i = 0; i < gst_caps_get_size (norm); i++) {
1057     GstStructure *st = gst_caps_get_structure (norm, i);
1058     /* Make sure all fields of all structures are fixed */
1059     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1060   }
1061
1062   gst_caps_unref (out);
1063   gst_caps_unref (norm);
1064
1065   in = gst_caps_from_string
1066       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
1067   out =
1068       gst_caps_from_string
1069       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
1070       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
1071   norm = gst_caps_normalize (in);
1072   fail_if (gst_caps_is_empty (norm));
1073   fail_unless (gst_caps_is_equal (norm, out));
1074   for (i = 0; i < gst_caps_get_size (norm); i++) {
1075     GstStructure *st = gst_caps_get_structure (norm, i);
1076     /* Make sure all fields of all structures are fixed */
1077     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1078   }
1079
1080   gst_caps_unref (out);
1081   gst_caps_unref (norm);
1082
1083   in = gst_caps_from_string
1084       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
1085   out =
1086       gst_caps_from_string
1087       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
1088   norm = gst_caps_normalize (in);
1089   fail_if (gst_caps_is_empty (norm));
1090   fail_unless (gst_caps_is_equal (norm, out));
1091   for (i = 0; i < gst_caps_get_size (norm); i++) {
1092     GstStructure *st = gst_caps_get_structure (norm, i);
1093     /* Make sure all fields of all structures are fixed */
1094     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
1095   }
1096
1097   gst_caps_unref (out);
1098   gst_caps_unref (norm);
1099 }
1100
1101 GST_END_TEST;
1102
1103 GST_START_TEST (test_broken)
1104 {
1105   GstCaps *c1 = NULL;
1106
1107   /* NULL is not valid for media_type */
1108   ASSERT_CRITICAL (c1 =
1109       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
1110   fail_if (c1);
1111
1112   /* such a name is not valid, see gst_structure_validate_name() */
1113   ASSERT_CRITICAL (c1 =
1114       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
1115   fail_if (c1);
1116 }
1117
1118 GST_END_TEST;
1119
1120 GST_START_TEST (test_features)
1121 {
1122   GstCaps *c1, *c2, *c3;
1123   GstStructure *s1, *s2;
1124   GstCapsFeatures *f1, *f2;
1125   gchar *str1;
1126   static GstStaticCaps scaps =
1127       GST_STATIC_CAPS
1128       ("video/x-raw(memory:EGLImage), width=320, height=[ 240, 260 ]");
1129
1130   c1 = gst_caps_new_empty ();
1131   fail_unless (c1 != NULL);
1132   s1 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1133       GST_TYPE_INT_RANGE, 240, 260, NULL);
1134   fail_unless (s1 != NULL);
1135   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1136   fail_unless (f1 != NULL);
1137
1138   gst_caps_append_structure_full (c1, s1, f1);
1139   s2 = gst_caps_get_structure (c1, 0);
1140   fail_unless (s1 == s2);
1141   f2 = gst_caps_get_features (c1, 0);
1142   fail_unless (f1 == f2);
1143
1144   str1 = gst_caps_to_string (c1);
1145   fail_unless (str1 != NULL);
1146   c2 = gst_caps_from_string (str1);
1147   fail_unless (c2 != NULL);
1148   g_free (str1);
1149
1150   fail_unless (gst_caps_is_equal (c1, c2));
1151   fail_unless (gst_caps_is_subset (c1, c2));
1152   fail_unless (gst_caps_is_subset (c2, c1));
1153   fail_unless (gst_caps_can_intersect (c1, c2));
1154
1155   gst_caps_unref (c2);
1156
1157   c2 = gst_caps_new_empty ();
1158   fail_unless (c2 != NULL);
1159   s2 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1160       GST_TYPE_INT_RANGE, 240, 260, NULL);
1161   fail_unless (s2 != NULL);
1162   f2 = gst_caps_features_new ("memory:VASurface", "meta:VAMeta", NULL);
1163   fail_unless (f2 != NULL);
1164   gst_caps_append_structure_full (c2, s2, f2);
1165
1166   fail_if (gst_caps_is_equal (c1, c2));
1167   fail_if (gst_caps_is_subset (c1, c2));
1168   fail_if (gst_caps_is_subset (c2, c1));
1169   fail_if (gst_caps_can_intersect (c1, c2));
1170
1171   str1 = gst_caps_to_string (c2);
1172   fail_unless (str1 != NULL);
1173   c3 = gst_caps_from_string (str1);
1174   fail_unless (c3 != NULL);
1175   g_free (str1);
1176
1177   fail_unless (gst_caps_is_equal (c2, c3));
1178   fail_unless (gst_caps_is_subset (c2, c3));
1179   fail_unless (gst_caps_is_subset (c3, c2));
1180   fail_unless (gst_caps_can_intersect (c2, c3));
1181
1182   f1 = gst_caps_get_features (c3, 0);
1183   fail_unless (f1 != NULL);
1184   fail_if (f1 == f2);
1185   gst_caps_features_contains (f1, "memory:VASurface");
1186   gst_caps_features_remove (f1, "memory:VASurface");
1187   fail_if (gst_caps_is_equal (c2, c3));
1188   fail_if (gst_caps_is_subset (c2, c3));
1189   fail_if (gst_caps_is_subset (c3, c2));
1190   fail_if (gst_caps_can_intersect (c2, c3));
1191
1192   gst_caps_unref (c3);
1193   gst_caps_unref (c2);
1194
1195   c2 = gst_static_caps_get (&scaps);
1196   fail_unless (c2 != NULL);
1197   fail_unless (gst_caps_is_equal (c1, c2));
1198   fail_unless (gst_caps_is_subset (c1, c2));
1199   fail_unless (gst_caps_is_subset (c2, c1));
1200   fail_unless (gst_caps_can_intersect (c1, c2));
1201   gst_caps_unref (c2);
1202
1203   c2 = gst_caps_from_string
1204       ("video/x-raw(ANY), width=320, height=[ 240, 260 ]");
1205   fail_unless (c2 != NULL);
1206   fail_if (gst_caps_is_equal (c1, c2));
1207   fail_unless (gst_caps_is_subset (c1, c2));
1208   fail_if (gst_caps_is_subset (c2, c1));
1209   fail_unless (gst_caps_can_intersect (c1, c2));
1210
1211   c3 = gst_caps_intersect (c1, c2);
1212   fail_unless (gst_caps_is_equal (c3, c1));
1213
1214   gst_caps_unref (c3);
1215   gst_caps_unref (c2);
1216   gst_caps_unref (c1);
1217
1218   c1 = gst_caps_from_string ("video/x-raw");
1219   c2 = gst_caps_from_string ("video/x-raw");
1220
1221   f1 = gst_caps_get_features (c1, 0);
1222   gst_caps_features_add (f1, "memory:VASurface");
1223
1224   fail_unless (gst_caps_features_is_equal (f1, gst_caps_get_features (c1, 0)));
1225   fail_if (gst_caps_can_intersect (c1, c2));
1226
1227   f2 = gst_caps_get_features (c2, 0);
1228   fail_unless (gst_caps_features_is_equal
1229       (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY, f2));
1230
1231   gst_caps_unref (c2);
1232   gst_caps_unref (c1);
1233
1234   c1 = gst_caps_from_string ("video/x-raw");
1235   f1 = gst_caps_get_features (c1, 0);
1236   f2 = gst_caps_features_new ("memory:dmabuf", NULL);
1237   gst_caps_set_features (c1, 0, f2);
1238
1239   gst_caps_unref (c1);
1240 }
1241
1242 GST_END_TEST;
1243
1244 GST_START_TEST (test_special_caps)
1245 {
1246   GstCaps *caps;
1247
1248   caps = gst_caps_new_any ();
1249   fail_unless (gst_caps_is_any (caps));
1250   fail_unless (gst_caps_is_any (caps) == TRUE);
1251   fail_if (gst_caps_is_empty (caps));
1252   fail_unless (gst_caps_is_empty (caps) == FALSE);
1253   gst_caps_unref (caps);
1254
1255   caps = gst_caps_new_empty ();
1256   fail_if (gst_caps_is_any (caps));
1257   fail_unless (gst_caps_is_any (caps) == FALSE);
1258   fail_unless (gst_caps_is_empty (caps));
1259   fail_unless (gst_caps_is_empty (caps) == TRUE);
1260   gst_caps_unref (caps);
1261 }
1262
1263 GST_END_TEST;
1264
1265 static gboolean
1266 foreach_append_function (GstCapsFeatures * features, GstStructure * structure,
1267     gpointer user_data)
1268 {
1269   GstCaps *caps = user_data;
1270
1271   gst_caps_append_structure_full (caps, gst_structure_copy (structure),
1272       features ? gst_caps_features_copy (features) : NULL);
1273
1274   return TRUE;
1275 }
1276
1277 GST_START_TEST (test_foreach)
1278 {
1279   GstCaps *caps, *caps2;
1280
1281   caps =
1282       gst_caps_from_string
1283       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1284   caps2 = gst_caps_new_empty ();
1285   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1286   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1287   gst_caps_unref (caps);
1288   gst_caps_unref (caps2);
1289
1290   caps = gst_caps_new_empty ();
1291   caps2 = gst_caps_new_empty ();
1292   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1293   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1294   gst_caps_unref (caps);
1295   gst_caps_unref (caps2);
1296 }
1297
1298 GST_END_TEST;
1299
1300 static gboolean
1301 map_function (GstCapsFeatures * features, GstStructure * structure,
1302     gpointer user_data)
1303 {
1304   /* Remove caps features if there are any, otherwise add some dummy */
1305   if (gst_caps_features_contains (features, "foo:bar")) {
1306     gst_caps_features_remove (features, "foo:bar");
1307   } else {
1308     gst_caps_features_add (features, "foo:bar");
1309     gst_caps_features_remove (features, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
1310   }
1311
1312   /* Set some dummy integer in the structure */
1313   gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);
1314
1315   return TRUE;
1316 }
1317
1318 GST_START_TEST (test_map_in_place)
1319 {
1320   GstCaps *caps, *caps2;
1321
1322   caps =
1323       gst_caps_from_string
1324       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1325   caps2 =
1326       gst_caps_from_string
1327       ("video/x-raw(foo:bar), foo=(int)123, format=I420; video/x-raw, foo=(int)123; video/x-h264(foo:bar), foo=(int)123");
1328   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1329   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1330   gst_caps_unref (caps);
1331   gst_caps_unref (caps2);
1332
1333   caps = gst_caps_new_empty ();
1334   caps2 = gst_caps_new_empty ();
1335   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1336   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1337   gst_caps_unref (caps);
1338   gst_caps_unref (caps2);
1339 }
1340
1341 GST_END_TEST;
1342
1343 static gboolean
1344 filter_map_function (GstCapsFeatures * features, GstStructure * structure,
1345     gpointer user_data)
1346 {
1347   if (!gst_structure_has_name (structure, "video/x-raw"))
1348     return FALSE;
1349
1350   if (!gst_caps_features_contains (features, "foo:bar"))
1351     return FALSE;
1352
1353   /* Set some dummy integer in the structure */
1354   gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);
1355
1356   return TRUE;
1357 }
1358
1359 GST_START_TEST (test_filter_and_map_in_place)
1360 {
1361   GstCaps *caps, *caps2;
1362
1363   caps =
1364       gst_caps_from_string
1365       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1366   caps2 = gst_caps_from_string ("video/x-raw(foo:bar), foo=(int)123");
1367   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1368   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1369   gst_caps_unref (caps);
1370   gst_caps_unref (caps2);
1371
1372   caps = gst_caps_from_string ("video/x-raw, format=I420; video/x-h264");
1373   caps2 = gst_caps_new_empty ();
1374   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1375   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1376   gst_caps_unref (caps);
1377   gst_caps_unref (caps2);
1378
1379   caps = gst_caps_new_empty ();
1380   caps2 = gst_caps_new_empty ();
1381   gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
1382   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1383   gst_caps_unref (caps);
1384   gst_caps_unref (caps2);
1385 }
1386
1387 GST_END_TEST;
1388
1389 static Suite *
1390 gst_caps_suite (void)
1391 {
1392   Suite *s = suite_create ("GstCaps");
1393   TCase *tc_chain = tcase_create ("operations");
1394
1395   suite_add_tcase (s, tc_chain);
1396   tcase_add_test (tc_chain, test_from_string);
1397   tcase_add_test (tc_chain, test_double_append);
1398   tcase_add_test (tc_chain, test_mutability);
1399   tcase_add_test (tc_chain, test_static_caps);
1400   tcase_add_test (tc_chain, test_simplify);
1401   tcase_add_test (tc_chain, test_truncate);
1402   tcase_add_test (tc_chain, test_subset);
1403   tcase_add_test (tc_chain, test_subset_duplication);
1404   tcase_add_test (tc_chain, test_merge_fundamental);
1405   tcase_add_test (tc_chain, test_merge_same);
1406   tcase_add_test (tc_chain, test_merge_subset);
1407   tcase_add_test (tc_chain, test_intersect);
1408   tcase_add_test (tc_chain, test_intersect2);
1409   tcase_add_test (tc_chain, test_intersect_list_duplicate);
1410   tcase_add_test (tc_chain, test_intersect_zigzag);
1411   tcase_add_test (tc_chain, test_intersect_first);
1412   tcase_add_test (tc_chain, test_intersect_first2);
1413   tcase_add_test (tc_chain, test_intersect_duplication);
1414   tcase_add_test (tc_chain, test_intersect_flagset);
1415   tcase_add_test (tc_chain, test_union);
1416   tcase_add_test (tc_chain, test_normalize);
1417   tcase_add_test (tc_chain, test_broken);
1418   tcase_add_test (tc_chain, test_features);
1419   tcase_add_test (tc_chain, test_special_caps);
1420   tcase_add_test (tc_chain, test_foreach);
1421   tcase_add_test (tc_chain, test_map_in_place);
1422   tcase_add_test (tc_chain, test_filter_and_map_in_place);
1423
1424   return s;
1425 }
1426
1427 GST_CHECK_MAIN (gst_caps);