caps: Add gst_caps_foreach() and gst_caps_map_in_place()
[platform/upstream/gstreamer.git] / tests / check / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * gstcaps.c: Unit test for GstCaps
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23
24 #include <gst/check/gstcheck.h>
25 #include <gst/gstcaps.h>
26 #include "capslist.h"
27
28 GST_START_TEST (test_from_string)
29 {
30   GstCaps *caps;
31   GstCaps *caps2;
32   gchar *to_str;
33   int i;
34
35   for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
36     caps = gst_caps_from_string (caps_list[i]);
37     fail_if (caps == NULL,
38         "Could not create caps from string %s\n", caps_list[i]);
39     to_str = gst_caps_to_string (caps);
40     fail_if (to_str == NULL,
41         "Could not convert caps back to string %s\n", caps_list[i]);
42     caps2 = gst_caps_from_string (caps_list[i]);
43     fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);
44
45     fail_unless (gst_caps_is_equal (caps, caps));
46     fail_unless (gst_caps_is_equal (caps, caps2));
47
48     gst_caps_unref (caps);
49     gst_caps_unref (caps2);
50     g_free (to_str);
51   }
52 }
53
54 GST_END_TEST;
55
56 GST_START_TEST (test_double_append)
57 {
58   GstStructure *s1;
59   GstCaps *c1;
60
61   c1 = gst_caps_new_any ();
62   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
63   gst_caps_append_structure (c1, s1);
64   ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
65
66   gst_caps_unref (c1);
67 }
68
69 GST_END_TEST;
70
71 GST_START_TEST (test_mutability)
72 {
73   GstStructure *s1;
74   GstCaps *c1;
75   gint ret;
76
77   c1 = gst_caps_new_any ();
78   s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
79   gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
80   gst_caps_append_structure (c1, s1);
81   gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
82   gst_caps_ref (c1);
83   ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
84   fail_unless (gst_structure_get_int (s1, "rate", &ret));
85   fail_unless (ret == 22500);
86   ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
87   fail_unless (gst_structure_get_int (s1, "rate", &ret));
88   fail_unless (ret == 22500);
89   gst_caps_unref (c1);
90   gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
91   fail_unless (gst_structure_get_int (s1, "rate", &ret));
92   fail_unless (ret == 11250);
93   gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
94   fail_unless (gst_structure_get_int (s1, "rate", &ret));
95   fail_unless (ret == 1);
96   gst_caps_unref (c1);
97 }
98
99 GST_END_TEST;
100
101 GST_START_TEST (test_static_caps)
102 {
103   static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw,rate=44100");
104   GstCaps *caps1;
105   GstCaps *caps2;
106   static GstStaticCaps sany = GST_STATIC_CAPS_ANY;
107   static GstStaticCaps snone = GST_STATIC_CAPS_NONE;
108
109   /* caps creation */
110   caps1 = gst_static_caps_get (&scaps);
111   fail_unless (caps1 != NULL);
112   /* 1 refcount core, one from us */
113   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
114
115   /* caps should be the same */
116   caps2 = gst_static_caps_get (&scaps);
117   fail_unless (caps2 != NULL);
118   /* 1 refcount core, two from us */
119   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
120   /* caps must be equal */
121   fail_unless (caps1 == caps2);
122
123   gst_caps_unref (caps1);
124   gst_caps_unref (caps2);
125
126   caps1 = gst_static_caps_get (&sany);
127   fail_unless (gst_caps_is_equal (caps1, GST_CAPS_ANY));
128   caps2 = gst_static_caps_get (&snone);
129   fail_unless (gst_caps_is_equal (caps2, GST_CAPS_NONE));
130   fail_if (gst_caps_is_equal (caps1, caps2));
131   gst_caps_unref (caps1);
132   gst_caps_unref (caps2);
133 }
134
135 GST_END_TEST;
136
137 static const gchar non_simple_caps_string[] =
138     "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
139     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
140     "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
141     "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
142     "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
143     "height=(int)[ 16, 4096 ]; video/x-raw, "
144     "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
145     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
146
147 static gboolean
148 check_string_list (const GValue * format_value)
149 {
150   const GValue *string_value;
151   gboolean got_rgb8 = FALSE;
152   gboolean got_yv12 = FALSE;
153   gboolean got_i420 = FALSE;
154   gboolean got_yuy2 = FALSE;
155   const gchar *string;
156
157   string_value = gst_value_list_get_value (format_value, 0);
158   fail_unless (string_value != NULL);
159   fail_unless (G_VALUE_HOLDS_STRING (string_value));
160   string = g_value_get_string (string_value);
161   fail_unless (string != NULL);
162   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
163   got_i420 = got_i420 || (g_str_equal (string, "I420"));
164   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
165   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
166
167   string_value = gst_value_list_get_value (format_value, 1);
168   fail_unless (string_value != NULL);
169   fail_unless (G_VALUE_HOLDS_STRING (string_value));
170   string = g_value_get_string (string_value);
171   fail_unless (string != NULL);
172   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
173   got_i420 = got_i420 || (g_str_equal (string, "I420"));
174   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
175   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
176
177   string_value = gst_value_list_get_value (format_value, 2);
178   fail_unless (string_value != NULL);
179   fail_unless (G_VALUE_HOLDS_STRING (string_value));
180   string = g_value_get_string (string_value);
181   fail_unless (string != NULL);
182   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
183   got_i420 = got_i420 || (g_str_equal (string, "I420"));
184   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
185   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
186
187   string_value = gst_value_list_get_value (format_value, 3);
188   fail_unless (string_value != NULL);
189   fail_unless (G_VALUE_HOLDS_STRING (string_value));
190   string = g_value_get_string (string_value);
191   fail_unless (string != NULL);
192   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
193   got_i420 = got_i420 || (g_str_equal (string, "I420"));
194   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
195   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
196
197   return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
198 }
199
200 GST_START_TEST (test_simplify)
201 {
202   GstStructure *s1;
203   GstCaps *caps;
204
205   caps = gst_caps_from_string (non_simple_caps_string);
206   fail_unless (caps != NULL,
207       "gst_caps_from_string (non_simple_caps_string) failed");
208
209   caps = gst_caps_simplify (caps);
210   fail_unless (caps != NULL, "gst_caps_simplify() should have worked");
211
212   /* check simplified caps, should be:
213    *
214    * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
215    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
216    *     framerate=(fraction)[ 1/100, 100 ]
217    */
218   GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
219   fail_unless (gst_caps_get_size (caps) == 1);
220   s1 = gst_caps_get_structure (caps, 0);
221   fail_unless (s1 != NULL);
222
223   fail_unless (gst_structure_has_name (s1, "video/x-raw"));
224   {
225     const GValue *framerate_value;
226     const GValue *format_value;
227     const GValue *width_value;
228     const GValue *height_value;
229     const GValue *val_fps;
230     GValue test_fps = { 0, };
231     gint min_width, max_width;
232     gint min_height, max_height;
233
234     format_value = gst_structure_get_value (s1, "format");
235     fail_unless (format_value != NULL);
236     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
237     fail_unless (gst_value_list_get_size (format_value) == 4);
238     fail_unless (check_string_list (format_value) == TRUE);
239
240     g_value_init (&test_fps, GST_TYPE_FRACTION);
241     framerate_value = gst_structure_get_value (s1, "framerate");
242     fail_unless (framerate_value != NULL);
243     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
244
245     val_fps = gst_value_get_fraction_range_min (framerate_value);
246     gst_value_set_fraction (&test_fps, 1, 100);
247     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
248
249     val_fps = gst_value_get_fraction_range_max (framerate_value);
250     gst_value_set_fraction (&test_fps, 100, 1);
251     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
252
253     g_value_unset (&test_fps);
254
255     width_value = gst_structure_get_value (s1, "width");
256     fail_unless (width_value != NULL);
257     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
258     min_width = gst_value_get_int_range_min (width_value);
259     max_width = gst_value_get_int_range_max (width_value);
260     fail_unless (min_width == 16 && max_width == 4096);
261
262     height_value = gst_structure_get_value (s1, "height");
263     fail_unless (height_value != NULL);
264     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
265     min_height = gst_value_get_int_range_min (height_value);
266     max_height = gst_value_get_int_range_max (height_value);
267     fail_unless (min_height == 16 && max_height == 4096);
268   }
269
270   gst_caps_unref (caps);
271 }
272
273 GST_END_TEST;
274
275 GST_START_TEST (test_truncate)
276 {
277   GstCaps *caps;
278
279   caps = gst_caps_from_string (non_simple_caps_string);
280   fail_unless (caps != NULL,
281       "gst_caps_from_string (non_simple_caps_string) failed");
282   fail_unless_equals_int (gst_caps_get_size (caps), 4);
283   caps = gst_caps_truncate (caps);
284   fail_unless_equals_int (gst_caps_get_size (caps), 1);
285   gst_caps_unref (caps);
286 }
287
288 GST_END_TEST;
289
290 GST_START_TEST (test_subset)
291 {
292   GstCaps *c1, *c2;
293
294   c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
295   c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
296   fail_unless (gst_caps_is_subset (c2, c1));
297   fail_if (gst_caps_is_subset (c1, c2));
298   gst_caps_unref (c1);
299   gst_caps_unref (c2);
300
301   c1 = gst_caps_from_string
302       ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
303   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
304   fail_unless (gst_caps_is_subset (c2, c1));
305   fail_if (gst_caps_is_subset (c1, c2));
306   gst_caps_unref (c1);
307   gst_caps_unref (c2);
308
309   c1 = gst_caps_from_string ("audio/x-raw, channels=(int) {1}");
310   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
311   fail_unless (gst_caps_is_subset (c2, c1));
312   fail_unless (gst_caps_is_subset (c1, c2));
313   fail_unless (gst_caps_is_equal (c1, c2));
314   gst_caps_unref (c1);
315   gst_caps_unref (c2);
316
317   c1 = gst_caps_from_string
318       ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
319   c2 = gst_caps_from_string
320       ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
321   fail_unless (gst_caps_is_subset (c1, c2));
322   fail_if (gst_caps_is_subset (c2, c1));
323   gst_caps_unref (c1);
324   gst_caps_unref (c2);
325
326   c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
327   c2 = gst_caps_from_string
328       ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)nal");
329   fail_if (gst_caps_is_subset (c2, c1));
330   fail_if (gst_caps_is_subset (c1, c2));
331   fail_if (gst_caps_is_equal (c1, c2));
332   gst_caps_unref (c1);
333   gst_caps_unref (c2);
334 }
335
336 GST_END_TEST;
337
338 GST_START_TEST (test_subset_duplication)
339 {
340   GstCaps *c1, *c2;
341
342   c1 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
343   c2 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
344
345   fail_unless (gst_caps_is_subset (c1, c2));
346   fail_unless (gst_caps_is_subset (c2, c1));
347
348   gst_caps_unref (c2);
349   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE }");
350
351   fail_unless (gst_caps_is_subset (c1, c2));
352   fail_unless (gst_caps_is_subset (c2, c1));
353
354   gst_caps_unref (c2);
355   c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE, F32LE }");
356
357   fail_unless (gst_caps_is_subset (c1, c2));
358   fail_unless (gst_caps_is_subset (c2, c1));
359
360   gst_caps_unref (c2);
361   c2 = gst_caps_from_string
362       ("audio/x-raw, format=(string){ F32LE, F32LE, F32LE }");
363
364   fail_unless (gst_caps_is_subset (c1, c2));
365   fail_unless (gst_caps_is_subset (c2, c1));
366
367   gst_caps_unref (c1);
368   gst_caps_unref (c2);
369 }
370
371 GST_END_TEST;
372
373 GST_START_TEST (test_merge_fundamental)
374 {
375   GstCaps *c1, *c2;
376
377   /* ANY + specific = ANY */
378   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
379   c2 = gst_caps_new_any ();
380   c2 = gst_caps_merge (c2, c1);
381   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
382   fail_unless (gst_caps_get_size (c2) == 0, NULL);
383   fail_unless (gst_caps_is_any (c2), NULL);
384   gst_caps_unref (c2);
385
386   /* specific + ANY = ANY */
387   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
388   c1 = gst_caps_new_any ();
389   c2 = gst_caps_merge (c2, c1);
390   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
391   fail_unless (gst_caps_get_size (c2) == 0, NULL);
392   fail_unless (gst_caps_is_any (c2), NULL);
393   gst_caps_unref (c2);
394
395   /* EMPTY + specific = specific */
396   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
397   c2 = gst_caps_new_empty ();
398   c2 = gst_caps_merge (c2, c1);
399   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
400   fail_unless (gst_caps_get_size (c2) == 1, NULL);
401   fail_if (gst_caps_is_empty (c2), NULL);
402   gst_caps_unref (c2);
403
404   /* specific + EMPTY = specific */
405   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
406   c1 = gst_caps_new_empty ();
407   c2 = gst_caps_merge (c2, c1);
408   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
409   fail_unless (gst_caps_get_size (c2) == 1, NULL);
410   fail_if (gst_caps_is_empty (c2), NULL);
411   gst_caps_unref (c2);
412 }
413
414 GST_END_TEST;
415
416 GST_START_TEST (test_merge_same)
417 {
418   GstCaps *c1, *c2, *test;
419
420   /* this is the same */
421   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
422   c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
423   c2 = gst_caps_merge (c2, c1);
424   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
425   fail_unless (gst_caps_get_size (c2) == 1, NULL);
426   test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
427   fail_unless (gst_caps_is_equal (c2, test));
428   gst_caps_unref (test);
429   gst_caps_unref (c2);
430
431   /* and so is this */
432   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
433   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
434   c2 = gst_caps_merge (c2, c1);
435   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
436   fail_unless (gst_caps_get_size (c2) == 1, NULL);
437   gst_caps_unref (c2);
438
439   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
440   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
441   c2 = gst_caps_merge (c2, c1);
442   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
443   fail_unless (gst_caps_get_size (c2) == 2, NULL);
444   gst_caps_unref (c2);
445
446   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
447   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
448   c2 = gst_caps_merge (c2, c1);
449   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
450   fail_unless (gst_caps_get_size (c2) == 2, NULL);
451   gst_caps_unref (c2);
452
453   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
454   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
455   c2 = gst_caps_merge (c2, c1);
456   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
457   fail_unless (gst_caps_get_size (c2) == 1, NULL);
458   gst_caps_unref (c2);
459
460   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
461   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
462   c2 = gst_caps_merge (c2, c1);
463   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
464   fail_unless (gst_caps_get_size (c2) == 2, NULL);
465   gst_caps_unref (c2);
466 }
467
468 GST_END_TEST;
469
470 GST_START_TEST (test_merge_subset)
471 {
472   GstCaps *c1, *c2, *test;
473
474   /* the 2nd is already covered */
475   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
476   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
477   c2 = gst_caps_merge (c2, c1);
478   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
479   fail_unless (gst_caps_get_size (c2) == 1, NULL);
480   test = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
481   fail_unless (gst_caps_is_equal (c2, test));
482   gst_caps_unref (c2);
483   gst_caps_unref (test);
484
485   /* here it is not */
486   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
487   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
488   c2 = gst_caps_merge (c2, c1);
489   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
490   fail_unless (gst_caps_get_size (c2) == 2, NULL);
491   test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
492   fail_unless (gst_caps_is_equal (c2, test));
493   gst_caps_unref (c2);
494   gst_caps_unref (test);
495
496   /* second one was already contained in the first one */
497   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
498   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
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,channels=[1,3]");
503   fail_unless (gst_caps_is_equal (c2, test));
504   gst_caps_unref (c2);
505   gst_caps_unref (test);
506
507   /* second one was already contained in the first one */
508   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
509   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
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   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
514   fail_unless (gst_caps_is_equal (c2, test));
515   gst_caps_unref (c2);
516   gst_caps_unref (test);
517
518   /* second one was already contained in the first one */
519   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
520   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
521   c2 = gst_caps_merge (c2, c1);
522   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
523   fail_unless (gst_caps_get_size (c2) == 1, NULL);
524   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
525   fail_unless (gst_caps_is_equal (c2, test));
526   gst_caps_unref (c2);
527   gst_caps_unref (test);
528
529   /* second one was already contained in the first one */
530   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
531   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
532   c2 = gst_caps_merge (c2, c1);
533   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
534   fail_unless (gst_caps_get_size (c2) == 1, NULL);
535   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
536   fail_unless (gst_caps_is_equal (c2, test));
537   gst_caps_unref (c2);
538   gst_caps_unref (test);
539
540   /* these caps cannot be merged */
541   c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
542   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
543   c2 = gst_caps_merge (c2, c1);
544   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
545   fail_unless (gst_caps_get_size (c2) == 2, NULL);
546   test =
547       gst_caps_from_string
548       ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
549   fail_unless (gst_caps_is_equal (c2, test));
550   gst_caps_unref (c2);
551   gst_caps_unref (test);
552
553   /* these caps cannot be merged */
554   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
555   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
556   c2 = gst_caps_merge (c2, c1);
557   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
558   fail_unless (gst_caps_get_size (c2) == 2, NULL);
559   test =
560       gst_caps_from_string
561       ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
562   fail_unless (gst_caps_is_equal (c2, test));
563   gst_caps_unref (c2);
564   gst_caps_unref (test);
565
566   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
567   c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
568   c2 = gst_caps_merge (c2, c1);
569   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
570   fail_unless (gst_caps_get_size (c2) == 2, NULL);
571   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
572       "audio/x-raw,channels={1,2,3,4}");
573   fail_unless (gst_caps_is_equal (c2, test));
574   gst_caps_unref (c2);
575   gst_caps_unref (test);
576
577   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
578   c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
579   c2 = gst_caps_merge (c2, c1);
580   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
581   fail_unless (gst_caps_get_size (c2) == 2, NULL);
582   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
583       "audio/x-raw,channels={1,3}");
584   fail_unless (gst_caps_is_equal (c2, test));
585   gst_caps_unref (c2);
586   gst_caps_unref (test);
587
588   c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
589   c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
590   test = gst_caps_copy (c1);
591   c2 = gst_caps_merge (c2, c1);
592   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
593   fail_unless (gst_caps_is_subset (test, c2));
594   gst_caps_unref (test);
595   gst_caps_unref (c2);
596
597   c2 = gst_caps_from_string ("audio/x-raw");
598   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
599   c2 = gst_caps_merge (c2, c1);
600   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
601   fail_unless (gst_caps_get_size (c2) == 1, NULL);
602   test = gst_caps_from_string ("audio/x-raw");
603   fail_unless (gst_caps_is_equal (c2, test));
604   gst_caps_unref (c2);
605   gst_caps_unref (test);
606
607   c2 = gst_caps_from_string ("audio/x-raw,channels=1");
608   c1 = gst_caps_from_string ("audio/x-raw");
609   c2 = gst_caps_merge (c2, c1);
610   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
611   fail_unless (gst_caps_get_size (c2) == 2, NULL);
612   test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
613   fail_unless (gst_caps_is_equal (c2, test));
614   gst_caps_unref (c2);
615   gst_caps_unref (test);
616 }
617
618 GST_END_TEST;
619
620 GST_START_TEST (test_intersect)
621 {
622   GstStructure *s;
623   GstCaps *c1, *c2, *ci1, *ci2;
624
625   /* field not specified = any value possible, so the intersection
626    * should keep fields which are only part of one set of caps */
627   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
628   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
629
630   ci1 = gst_caps_intersect (c2, c1);
631   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
632   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
633   s = gst_caps_get_structure (ci1, 0);
634   fail_unless (gst_structure_has_name (s, "video/x-raw"));
635   fail_unless (gst_structure_get_value (s, "format") != NULL);
636   fail_unless (gst_structure_get_value (s, "width") != NULL);
637
638   /* with changed order */
639   ci2 = gst_caps_intersect (c1, c2);
640   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
641   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
642   s = gst_caps_get_structure (ci2, 0);
643   fail_unless (gst_structure_has_name (s, "video/x-raw"));
644   fail_unless (gst_structure_get_value (s, "format") != NULL);
645   fail_unless (gst_structure_get_value (s, "width") != NULL);
646
647   fail_unless (gst_caps_is_equal (ci1, ci2));
648
649   gst_caps_unref (ci1);
650   gst_caps_unref (ci2);
651
652   gst_caps_unref (c1);
653   gst_caps_unref (c2);
654
655   /* ========== */
656
657   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
658   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
659
660   ci1 = gst_caps_intersect (c2, c1);
661   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
662   fail_unless (gst_caps_is_empty (ci1), NULL);
663
664   /* with changed order */
665   ci2 = gst_caps_intersect (c1, c2);
666   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
667   fail_unless (gst_caps_is_empty (ci2), NULL);
668
669   fail_unless (gst_caps_is_equal (ci1, ci2));
670
671   gst_caps_unref (ci1);
672   gst_caps_unref (ci2);
673
674   gst_caps_unref (c1);
675   gst_caps_unref (c2);
676
677   /* ========== */
678
679   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
680   c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
681
682   ci1 = gst_caps_intersect (c2, c1);
683   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
684   fail_unless (gst_caps_is_empty (ci1), NULL);
685
686   /* with changed order */
687   ci2 = gst_caps_intersect (c1, c2);
688   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
689   fail_unless (gst_caps_is_empty (ci2), NULL);
690
691   fail_unless (gst_caps_is_equal (ci1, ci2));
692
693   gst_caps_unref (ci1);
694   gst_caps_unref (ci2);
695
696   gst_caps_unref (c1);
697   gst_caps_unref (c2);
698
699   /* ========== */
700
701   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
702   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,height=30");
703
704   ci1 = gst_caps_intersect (c2, c1);
705   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
706   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
707   s = gst_caps_get_structure (ci1, 0);
708   fail_unless (gst_structure_has_name (s, "video/x-raw"));
709   fail_unless (gst_structure_get_value (s, "format") != NULL);
710   fail_unless (gst_structure_get_value (s, "width") != NULL);
711   fail_unless (gst_structure_get_value (s, "height") != NULL);
712
713   /* with changed order */
714   ci2 = gst_caps_intersect (c1, c2);
715   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
716   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
717   s = gst_caps_get_structure (ci2, 0);
718   fail_unless (gst_structure_has_name (s, "video/x-raw"));
719   fail_unless (gst_structure_get_value (s, "format") != NULL);
720   fail_unless (gst_structure_get_value (s, "height") != 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 GST_END_TEST;
733
734 GST_START_TEST (test_intersect2)
735 {
736   GstCaps *caps1, *caps2, *icaps;
737
738   /* tests array subtraction */
739   caps1 = gst_caps_from_string ("audio/x-raw, "
740       "channel-positions=(int)<                      "
741       "{ 1, 2, 3, 4, 5, 6 },                         "
742       "{ 1, 2, 3, 4, 5, 6 },                         "
743       "{ 1, 2, 3, 4, 5, 6 },                         "
744       "{ 1, 2, 3, 4, 5, 6 },                         "
745       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
746   caps2 = gst_caps_from_string ("audio/x-raw, "
747       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
748   icaps = gst_caps_intersect (caps1, caps2);
749   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
750   fail_if (gst_caps_is_empty (icaps));
751   fail_unless (gst_caps_is_equal (icaps, caps2));
752   gst_caps_unref (caps1);
753   gst_caps_unref (caps2);
754   gst_caps_unref (icaps);
755
756   /* ===== */
757
758   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
759   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
760   icaps = gst_caps_intersect (caps1, caps2);
761   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
762   fail_if (gst_caps_is_empty (icaps));
763   fail_unless (gst_caps_is_equal (icaps, caps2));
764   gst_caps_unref (caps1);
765   gst_caps_unref (caps2);
766   gst_caps_unref (icaps);
767 }
768
769 GST_END_TEST;
770
771 GST_START_TEST (test_intersect_list_duplicate)
772 {
773   GstCaps *caps1, *caps2, *icaps;
774
775   /* make sure we don't take too long to intersect these.. */
776   caps1 = gst_caps_from_string ("video/x-raw, format=(string)YV12; "
777       "video/x-raw, format=(string)I420; video/x-raw, format=(string)YUY2; "
778       "video/x-raw, format=(string)UYVY; "
779       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx,"
780       " xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
781       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
782       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
783       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
784       "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
785       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
786       "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
787       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
788       "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, { ABGR, ABGR, "
789       "{ ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
790       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
791       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
792       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
793       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
794       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
795       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
796       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
797       "RGB, BGR, Y41B, Y42B, YVYU, Y444 }; "
798       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, "
799       "xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
800       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
801       "{ RGBA, RGBA, { RGBA, RGBA } } }, { 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 } } } } } }, BGRA, ARGB, "
808       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
809       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
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       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }; "
817       "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, "
818       "BGRx, xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
819       "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
820       "{ RGBA, RGBA, { RGBA, RGBA } } } }, BGRA, ARGB, "
821       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
822       "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
823       "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }");
824
825   caps2 = gst_caps_copy (caps1);
826
827   icaps = gst_caps_intersect (caps1, caps2);
828
829   gst_caps_unref (caps1);
830   gst_caps_unref (caps2);
831   gst_caps_unref (icaps);
832 }
833
834 GST_END_TEST;
835
836 GST_START_TEST (test_intersect_zigzag)
837 {
838   GstCaps *caps1, *caps2, *icaps, *result;
839
840   /* tests if caps order is maintained */
841   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
842   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
843
844   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
845   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
846   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
847   fail_if (gst_caps_is_empty (icaps));
848   fail_unless (gst_caps_is_equal (icaps, result));
849   gst_caps_unref (icaps);
850   gst_caps_unref (result);
851
852   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
853   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
854   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
855   fail_if (gst_caps_is_empty (icaps));
856   fail_unless (gst_caps_is_equal (icaps, result));
857   gst_caps_unref (icaps);
858   gst_caps_unref (result);
859
860   gst_caps_unref (caps1);
861   gst_caps_unref (caps2);
862 }
863
864 GST_END_TEST;
865
866
867 GST_START_TEST (test_intersect_first)
868 {
869   GstCaps *caps1, *caps2, *icaps, *result;
870
871   /* tests if caps order is maintained */
872   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
873   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
874   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
875   result = gst_caps_from_string ("format/A; format/C; format/D");
876   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
877   fail_if (gst_caps_is_empty (icaps));
878   fail_unless (gst_caps_is_equal (icaps, result));
879   gst_caps_unref (caps1);
880   gst_caps_unref (caps2);
881   gst_caps_unref (icaps);
882   gst_caps_unref (result);
883 }
884
885 GST_END_TEST;
886
887
888 GST_START_TEST (test_intersect_first2)
889 {
890   GstCaps *caps1, *caps2, *icaps, *result;
891
892   /* tests if caps order is maintained */
893   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
894   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
895
896   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
897   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
898   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
899   fail_if (gst_caps_is_empty (icaps));
900   fail_unless (gst_caps_is_equal (icaps, result));
901   gst_caps_unref (icaps);
902   gst_caps_unref (result);
903
904   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
905   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
906   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
907   fail_if (gst_caps_is_empty (icaps));
908   fail_unless (gst_caps_is_equal (icaps, result));
909   gst_caps_unref (icaps);
910   gst_caps_unref (result);
911
912   gst_caps_unref (caps1);
913   gst_caps_unref (caps2);
914 }
915
916 GST_END_TEST;
917
918 GST_START_TEST (test_intersect_duplication)
919 {
920   GstCaps *c1, *c2, *test;
921
922   c1 = gst_caps_from_string
923       ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
924   c2 = gst_caps_from_string
925       ("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 ]");
926
927   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
928   fail_unless_equals_int (gst_caps_get_size (test), 1);
929   fail_unless (gst_caps_is_equal (c1, test));
930   gst_caps_unref (c1);
931   gst_caps_unref (c2);
932   gst_caps_unref (test);
933 }
934
935 GST_END_TEST;
936
937 static gboolean
938 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
939 {
940   return gst_value_is_fixed (value);
941 }
942
943
944 GST_START_TEST (test_normalize)
945 {
946   GstCaps *in, *norm, *out;
947   guint i;
948
949   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
950   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
951   norm = gst_caps_normalize (in);
952   fail_if (gst_caps_is_empty (norm));
953   fail_unless (gst_caps_is_equal (norm, out));
954   for (i = 0; i < gst_caps_get_size (norm); i++) {
955     GstStructure *st = gst_caps_get_structure (norm, i);
956     /* Make sure all fields of all structures are fixed */
957     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
958   }
959
960   gst_caps_unref (out);
961   gst_caps_unref (norm);
962
963   in = gst_caps_from_string
964       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
965   out =
966       gst_caps_from_string
967       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
968       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
969   norm = gst_caps_normalize (in);
970   fail_if (gst_caps_is_empty (norm));
971   fail_unless (gst_caps_is_equal (norm, out));
972   for (i = 0; i < gst_caps_get_size (norm); i++) {
973     GstStructure *st = gst_caps_get_structure (norm, i);
974     /* Make sure all fields of all structures are fixed */
975     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
976   }
977
978   gst_caps_unref (out);
979   gst_caps_unref (norm);
980
981   in = gst_caps_from_string
982       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
983   out =
984       gst_caps_from_string
985       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
986   norm = gst_caps_normalize (in);
987   fail_if (gst_caps_is_empty (norm));
988   fail_unless (gst_caps_is_equal (norm, out));
989   for (i = 0; i < gst_caps_get_size (norm); i++) {
990     GstStructure *st = gst_caps_get_structure (norm, i);
991     /* Make sure all fields of all structures are fixed */
992     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
993   }
994
995   gst_caps_unref (out);
996   gst_caps_unref (norm);
997 }
998
999 GST_END_TEST;
1000
1001 GST_START_TEST (test_broken)
1002 {
1003   GstCaps *c1;
1004
1005   /* NULL is not valid for media_type */
1006   ASSERT_CRITICAL (c1 =
1007       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
1008   fail_if (c1);
1009
1010 #ifndef G_DISABLE_CHECKS
1011   /* such a name is not valid, see gst_structure_validate_name() */
1012   ASSERT_CRITICAL (c1 =
1013       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
1014   fail_if (c1);
1015 #endif
1016 }
1017
1018 GST_END_TEST;
1019
1020 GST_START_TEST (test_features)
1021 {
1022   GstCaps *c1, *c2, *c3;
1023   GstStructure *s1, *s2;
1024   GstCapsFeatures *f1, *f2;
1025   gchar *str1;
1026   static GstStaticCaps scaps =
1027       GST_STATIC_CAPS
1028       ("video/x-raw(memory:EGLImage), width=320, height=[ 240, 260 ]");
1029
1030   c1 = gst_caps_new_empty ();
1031   fail_unless (c1 != NULL);
1032   s1 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1033       GST_TYPE_INT_RANGE, 240, 260, NULL);
1034   fail_unless (s1 != NULL);
1035   f1 = gst_caps_features_new ("memory:EGLImage", NULL);
1036   fail_unless (f1 != NULL);
1037
1038   gst_caps_append_structure_full (c1, s1, f1);
1039   s2 = gst_caps_get_structure (c1, 0);
1040   fail_unless (s1 == s2);
1041   f2 = gst_caps_get_features (c1, 0);
1042   fail_unless (f1 == f2);
1043
1044   str1 = gst_caps_to_string (c1);
1045   fail_unless (str1 != NULL);
1046   c2 = gst_caps_from_string (str1);
1047   fail_unless (c2 != NULL);
1048   g_free (str1);
1049
1050   fail_unless (gst_caps_is_equal (c1, c2));
1051   fail_unless (gst_caps_is_subset (c1, c2));
1052   fail_unless (gst_caps_is_subset (c2, c1));
1053   fail_unless (gst_caps_can_intersect (c1, c2));
1054
1055   gst_caps_unref (c2);
1056
1057   c2 = gst_caps_new_empty ();
1058   fail_unless (c2 != NULL);
1059   s2 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
1060       GST_TYPE_INT_RANGE, 240, 260, NULL);
1061   fail_unless (s2 != NULL);
1062   f2 = gst_caps_features_new ("memory:VASurface", "meta:VAMeta", NULL);
1063   fail_unless (f2 != NULL);
1064   gst_caps_append_structure_full (c2, s2, f2);
1065
1066   fail_if (gst_caps_is_equal (c1, c2));
1067   fail_if (gst_caps_is_subset (c1, c2));
1068   fail_if (gst_caps_is_subset (c2, c1));
1069   fail_if (gst_caps_can_intersect (c1, c2));
1070
1071   str1 = gst_caps_to_string (c2);
1072   fail_unless (str1 != NULL);
1073   c3 = gst_caps_from_string (str1);
1074   fail_unless (c3 != NULL);
1075   g_free (str1);
1076
1077   fail_unless (gst_caps_is_equal (c2, c3));
1078   fail_unless (gst_caps_is_subset (c2, c3));
1079   fail_unless (gst_caps_is_subset (c3, c2));
1080   fail_unless (gst_caps_can_intersect (c2, c3));
1081
1082   f1 = gst_caps_get_features (c3, 0);
1083   fail_unless (f1 != NULL);
1084   fail_if (f1 == f2);
1085   gst_caps_features_contains (f1, "memory:VASurface");
1086   gst_caps_features_remove (f1, "memory:VASurface");
1087   fail_if (gst_caps_is_equal (c2, c3));
1088   fail_if (gst_caps_is_subset (c2, c3));
1089   fail_if (gst_caps_is_subset (c3, c2));
1090   fail_if (gst_caps_can_intersect (c2, c3));
1091
1092   gst_caps_unref (c3);
1093   gst_caps_unref (c2);
1094
1095   c2 = gst_static_caps_get (&scaps);
1096   fail_unless (c2 != NULL);
1097   fail_unless (gst_caps_is_equal (c1, c2));
1098   fail_unless (gst_caps_is_subset (c1, c2));
1099   fail_unless (gst_caps_is_subset (c2, c1));
1100   fail_unless (gst_caps_can_intersect (c1, c2));
1101   gst_caps_unref (c2);
1102
1103   c2 = gst_caps_from_string
1104       ("video/x-raw(ANY), width=320, height=[ 240, 260 ]");
1105   fail_unless (c2 != NULL);
1106   fail_if (gst_caps_is_equal (c1, c2));
1107   fail_unless (gst_caps_is_subset (c1, c2));
1108   fail_if (gst_caps_is_subset (c2, c1));
1109   fail_unless (gst_caps_can_intersect (c1, c2));
1110
1111   c3 = gst_caps_intersect (c1, c2);
1112   fail_unless (gst_caps_is_equal (c3, c1));
1113
1114   gst_caps_unref (c3);
1115   gst_caps_unref (c2);
1116   gst_caps_unref (c1);
1117
1118   c1 = gst_caps_from_string ("video/x-raw");
1119   c2 = gst_caps_from_string ("video/x-raw");
1120
1121   f1 = gst_caps_get_features (c1, 0);
1122   gst_caps_features_add (f1, "memory:VASurface");
1123
1124   fail_unless (gst_caps_features_is_equal (f1, gst_caps_get_features (c1, 0)));
1125   fail_if (gst_caps_can_intersect (c1, c2));
1126
1127   f2 = gst_caps_get_features (c2, 0);
1128   fail_unless (gst_caps_features_is_equal
1129       (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY, f2));
1130
1131   gst_caps_unref (c2);
1132   gst_caps_unref (c1);
1133
1134   c1 = gst_caps_from_string ("video/x-raw");
1135   f1 = gst_caps_get_features (c1, 0);
1136   f2 = gst_caps_features_new ("memory:dmabuf", NULL);
1137   gst_caps_set_features (c1, 0, f2);
1138
1139   gst_caps_unref (c1);
1140 }
1141
1142 GST_END_TEST;
1143
1144 GST_START_TEST (test_special_caps)
1145 {
1146   GstCaps *caps;
1147
1148   caps = gst_caps_new_any ();
1149   fail_unless (gst_caps_is_any (caps));
1150   fail_unless (gst_caps_is_any (caps) == TRUE);
1151   fail_if (gst_caps_is_empty (caps));
1152   fail_unless (gst_caps_is_empty (caps) == FALSE);
1153   gst_caps_unref (caps);
1154
1155   caps = gst_caps_new_empty ();
1156   fail_if (gst_caps_is_any (caps));
1157   fail_unless (gst_caps_is_any (caps) == FALSE);
1158   fail_unless (gst_caps_is_empty (caps));
1159   fail_unless (gst_caps_is_empty (caps) == TRUE);
1160   gst_caps_unref (caps);
1161 }
1162
1163 GST_END_TEST;
1164
1165 static gboolean
1166 foreach_append_function (GstCapsFeatures * features, GstStructure * structure,
1167     gpointer user_data)
1168 {
1169   GstCaps *caps = user_data;
1170
1171   gst_caps_append_structure_full (caps, gst_structure_copy (structure),
1172       features ? gst_caps_features_copy (features) : NULL);
1173
1174   return TRUE;
1175 }
1176
1177 GST_START_TEST (test_foreach)
1178 {
1179   GstCaps *caps, *caps2;
1180
1181   caps =
1182       gst_caps_from_string
1183       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1184   caps2 = gst_caps_new_empty ();
1185   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1186   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1187   gst_caps_unref (caps);
1188   gst_caps_unref (caps2);
1189
1190   caps = gst_caps_new_empty ();
1191   caps2 = gst_caps_new_empty ();
1192   fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
1193   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1194   gst_caps_unref (caps);
1195   gst_caps_unref (caps2);
1196 }
1197
1198 GST_END_TEST;
1199
1200 static gboolean
1201 map_function (GstCapsFeatures * features, GstStructure * structure,
1202     gpointer user_data)
1203 {
1204   /* Remove caps features if there are any, otherwise add some dummy */
1205   if (gst_caps_features_contains (features, "foo:bar")) {
1206     gst_caps_features_remove (features, "foo:bar");
1207   } else {
1208     gst_caps_features_add (features, "foo:bar");
1209     gst_caps_features_remove (features, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
1210   }
1211
1212   /* Set some dummy integer in the structure */
1213   gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);
1214
1215   return TRUE;
1216 }
1217
1218 GST_START_TEST (test_map_in_place)
1219 {
1220   GstCaps *caps, *caps2;
1221
1222   caps =
1223       gst_caps_from_string
1224       ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
1225   caps2 =
1226       gst_caps_from_string
1227       ("video/x-raw(foo:bar), foo=(int)123, format=I420; video/x-raw, foo=(int)123; video/x-h264(foo:bar), foo=(int)123");
1228   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1229   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1230   gst_caps_unref (caps);
1231   gst_caps_unref (caps2);
1232
1233   caps = gst_caps_new_empty ();
1234   caps2 = gst_caps_new_empty ();
1235   fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
1236   fail_unless (gst_caps_is_strictly_equal (caps, caps2));
1237   gst_caps_unref (caps);
1238   gst_caps_unref (caps2);
1239 }
1240
1241 GST_END_TEST;
1242
1243 static Suite *
1244 gst_caps_suite (void)
1245 {
1246   Suite *s = suite_create ("GstCaps");
1247   TCase *tc_chain = tcase_create ("operations");
1248
1249   suite_add_tcase (s, tc_chain);
1250   tcase_add_test (tc_chain, test_from_string);
1251   tcase_add_test (tc_chain, test_double_append);
1252   tcase_add_test (tc_chain, test_mutability);
1253   tcase_add_test (tc_chain, test_static_caps);
1254   tcase_add_test (tc_chain, test_simplify);
1255   tcase_add_test (tc_chain, test_truncate);
1256   tcase_add_test (tc_chain, test_subset);
1257   tcase_add_test (tc_chain, test_subset_duplication);
1258   tcase_add_test (tc_chain, test_merge_fundamental);
1259   tcase_add_test (tc_chain, test_merge_same);
1260   tcase_add_test (tc_chain, test_merge_subset);
1261   tcase_add_test (tc_chain, test_intersect);
1262   tcase_add_test (tc_chain, test_intersect2);
1263   tcase_add_test (tc_chain, test_intersect_list_duplicate);
1264   tcase_add_test (tc_chain, test_intersect_zigzag);
1265   tcase_add_test (tc_chain, test_intersect_first);
1266   tcase_add_test (tc_chain, test_intersect_first2);
1267   tcase_add_test (tc_chain, test_intersect_duplication);
1268   tcase_add_test (tc_chain, test_normalize);
1269   tcase_add_test (tc_chain, test_broken);
1270   tcase_add_test (tc_chain, test_features);
1271   tcase_add_test (tc_chain, test_special_caps);
1272   tcase_add_test (tc_chain, test_foreach);
1273   tcase_add_test (tc_chain, test_map_in_place);
1274
1275   return s;
1276 }
1277
1278 GST_CHECK_MAIN (gst_caps);