Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / tests / check / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * gstcaps.c: Unit test for GstCaps
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #include <gst/check/gstcheck.h>
25 #include <gst/gstcaps.h>
26 #include "capslist.h"
27
28 GST_START_TEST (test_from_string)
29 {
30   GstCaps *caps;
31   GstCaps *caps2;
32   gchar *to_str;
33   int i;
34
35   for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
36     caps = gst_caps_from_string (caps_list[i]);
37     fail_if (caps == NULL,
38         "Could not create caps from string %s\n", caps_list[i]);
39     to_str = gst_caps_to_string (caps);
40     fail_if (to_str == NULL,
41         "Could not convert caps back to string %s\n", caps_list[i]);
42     caps2 = gst_caps_from_string (caps_list[i]);
43     fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);
44
45     fail_unless (gst_caps_is_equal (caps, caps));
46     fail_unless (gst_caps_is_equal (caps, caps2));
47
48     gst_caps_unref (caps);
49     gst_caps_unref (caps2);
50     g_free (to_str);
51   }
52 }
53
54 GST_END_TEST;
55
56 GST_START_TEST (test_double_append)
57 {
58   GstStructure *s1;
59   GstCaps *c1;
60
61   c1 = gst_caps_new_any ();
62   s1 = gst_structure_from_string ("audio/x-raw,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
107   /* caps creation */
108   caps1 = gst_static_caps_get (&scaps);
109   fail_unless (caps1 != NULL);
110   /* 1 refcount core, one from us */
111   fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
112
113   /* caps should be the same */
114   caps2 = gst_static_caps_get (&scaps);
115   fail_unless (caps2 != NULL);
116   /* 1 refcount core, two from us */
117   fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
118   /* caps must be equal */
119   fail_unless (caps1 == caps2);
120
121   gst_caps_unref (caps1);
122   gst_caps_unref (caps2);
123 }
124
125 GST_END_TEST;
126
127 static const gchar non_simple_caps_string[] =
128     "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
129     "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
130     "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
131     "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
132     "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
133     "height=(int)[ 16, 4096 ]; video/x-raw, "
134     "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
135     "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
136
137 static gboolean
138 check_string_list (const GValue * format_value)
139 {
140   const GValue *string_value;
141   gboolean got_rgb8 = FALSE;
142   gboolean got_yv12 = FALSE;
143   gboolean got_i420 = FALSE;
144   gboolean got_yuy2 = FALSE;
145   const gchar *string;
146
147   string_value = gst_value_list_get_value (format_value, 0);
148   fail_unless (string_value != NULL);
149   fail_unless (G_VALUE_HOLDS_STRING (string_value));
150   string = g_value_get_string (string_value);
151   fail_unless (string != NULL);
152   got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
153   got_i420 = got_i420 || (g_str_equal (string, "I420"));
154   got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
155   got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));
156
157   string_value = gst_value_list_get_value (format_value, 1);
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, 2);
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, 3);
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   return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
188 }
189
190 GST_START_TEST (test_simplify)
191 {
192   GstStructure *s1;
193   gboolean did_simplify;
194   GstCaps *caps;
195
196   caps = gst_caps_from_string (non_simple_caps_string);
197   fail_unless (caps != NULL,
198       "gst_caps_from_string (non_simple_caps_string) failed");
199
200   did_simplify = gst_caps_do_simplify (caps);
201   fail_unless (did_simplify == TRUE,
202       "gst_caps_do_simplify() should have worked");
203
204   /* check simplified caps, should be:
205    *
206    * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
207    *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
208    *     framerate=(fraction)[ 1/100, 100 ]
209    */
210   GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
211   fail_unless (gst_caps_get_size (caps) == 1);
212   s1 = gst_caps_get_structure (caps, 0);
213   fail_unless (s1 != NULL);
214
215   fail_unless (gst_structure_has_name (s1, "video/x-raw"));
216   {
217     const GValue *framerate_value;
218     const GValue *format_value;
219     const GValue *width_value;
220     const GValue *height_value;
221     const GValue *val_fps;
222     GValue test_fps = { 0, };
223     gint min_width, max_width;
224     gint min_height, max_height;
225
226     format_value = gst_structure_get_value (s1, "format");
227     fail_unless (format_value != NULL);
228     fail_unless (GST_VALUE_HOLDS_LIST (format_value));
229     fail_unless (gst_value_list_get_size (format_value) == 4);
230     fail_unless (check_string_list (format_value) == TRUE);
231
232     g_value_init (&test_fps, GST_TYPE_FRACTION);
233     framerate_value = gst_structure_get_value (s1, "framerate");
234     fail_unless (framerate_value != NULL);
235     fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
236
237     val_fps = gst_value_get_fraction_range_min (framerate_value);
238     gst_value_set_fraction (&test_fps, 1, 100);
239     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
240
241     val_fps = gst_value_get_fraction_range_max (framerate_value);
242     gst_value_set_fraction (&test_fps, 100, 1);
243     fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
244
245     g_value_unset (&test_fps);
246
247     width_value = gst_structure_get_value (s1, "width");
248     fail_unless (width_value != NULL);
249     fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
250     min_width = gst_value_get_int_range_min (width_value);
251     max_width = gst_value_get_int_range_max (width_value);
252     fail_unless (min_width == 16 && max_width == 4096);
253
254     height_value = gst_structure_get_value (s1, "height");
255     fail_unless (height_value != NULL);
256     fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
257     min_height = gst_value_get_int_range_min (height_value);
258     max_height = gst_value_get_int_range_max (height_value);
259     fail_unless (min_height == 16 && max_height == 4096);
260   }
261
262   gst_caps_unref (caps);
263 }
264
265 GST_END_TEST;
266
267 GST_START_TEST (test_truncate)
268 {
269   GstCaps *caps;
270
271   caps = gst_caps_from_string (non_simple_caps_string);
272   fail_unless (caps != NULL,
273       "gst_caps_from_string (non_simple_caps_string) failed");
274   fail_unless_equals_int (gst_caps_get_size (caps), 4);
275   gst_caps_truncate (caps);
276   fail_unless_equals_int (gst_caps_get_size (caps), 1);
277   gst_caps_unref (caps);
278 }
279
280 GST_END_TEST;
281
282 GST_START_TEST (test_subset)
283 {
284   GstCaps *c1, *c2;
285
286   c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
287   c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
288   fail_unless (gst_caps_is_subset (c2, c1));
289   fail_if (gst_caps_is_subset (c1, c2));
290   gst_caps_unref (c1);
291   gst_caps_unref (c2);
292
293   c1 = gst_caps_from_string
294       ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
295   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
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 ("audio/x-raw, channels=(int) {1}");
302   c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
303   fail_unless (gst_caps_is_subset (c2, c1));
304   fail_unless (gst_caps_is_subset (c1, c2));
305   fail_unless (gst_caps_is_equal (c1, c2));
306   gst_caps_unref (c1);
307   gst_caps_unref (c2);
308
309   c1 = gst_caps_from_string
310       ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
311   c2 = gst_caps_from_string
312       ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
313   fail_unless (gst_caps_is_subset (c1, c2));
314   fail_if (gst_caps_is_subset (c2, c1));
315   gst_caps_unref (c1);
316   gst_caps_unref (c2);
317 }
318
319 GST_END_TEST;
320
321 GST_START_TEST (test_merge_fundamental)
322 {
323   GstCaps *c1, *c2;
324
325   /* ANY + specific = ANY */
326   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
327   c2 = gst_caps_new_any ();
328   gst_caps_merge (c2, c1);
329   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
330   fail_unless (gst_caps_get_size (c2) == 0, NULL);
331   fail_unless (gst_caps_is_any (c2), NULL);
332   gst_caps_unref (c2);
333
334   /* specific + ANY = ANY */
335   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
336   c1 = gst_caps_new_any ();
337   gst_caps_merge (c2, c1);
338   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
339   fail_unless (gst_caps_get_size (c2) == 0, NULL);
340   fail_unless (gst_caps_is_any (c2), NULL);
341   gst_caps_unref (c2);
342
343   /* EMPTY + specific = specific */
344   c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
345   c2 = gst_caps_new_empty ();
346   gst_caps_merge (c2, c1);
347   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
348   fail_unless (gst_caps_get_size (c2) == 1, NULL);
349   fail_if (gst_caps_is_empty (c2), NULL);
350   gst_caps_unref (c2);
351
352   /* specific + EMPTY = specific */
353   c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
354   c1 = gst_caps_new_empty ();
355   gst_caps_merge (c2, c1);
356   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
357   fail_unless (gst_caps_get_size (c2) == 1, NULL);
358   fail_if (gst_caps_is_empty (c2), NULL);
359   gst_caps_unref (c2);
360 }
361
362 GST_END_TEST;
363
364 GST_START_TEST (test_merge_same)
365 {
366   GstCaps *c1, *c2, *test;
367
368   /* this is the same */
369   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
370   c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
371   gst_caps_merge (c2, c1);
372   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
373   fail_unless (gst_caps_get_size (c2) == 1, NULL);
374   test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
375   fail_unless (gst_caps_is_equal (c2, test));
376   gst_caps_unref (test);
377   gst_caps_unref (c2);
378
379   /* and so is this */
380   c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
381   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
382   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) == 1, NULL);
385   gst_caps_unref (c2);
386
387   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
388   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
389   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) == 2, NULL);
392   gst_caps_unref (c2);
393
394   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
395   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
396   gst_caps_merge (c2, c1);
397   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
398   fail_unless (gst_caps_get_size (c2) == 2, NULL);
399   gst_caps_unref (c2);
400
401   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
402   c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
403   gst_caps_merge (c2, c1);
404   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
405   fail_unless (gst_caps_get_size (c2) == 1, NULL);
406   gst_caps_unref (c2);
407
408   c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
409   c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
410   gst_caps_merge (c2, c1);
411   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
412   fail_unless (gst_caps_get_size (c2) == 2, NULL);
413   gst_caps_unref (c2);
414 }
415
416 GST_END_TEST;
417
418 GST_START_TEST (test_merge_subset)
419 {
420   GstCaps *c1, *c2, *test;
421
422   /* the 2nd is already covered */
423   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
424   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
425   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,channels=[1,2]");
429   fail_unless (gst_caps_is_equal (c2, test));
430   gst_caps_unref (c2);
431   gst_caps_unref (test);
432
433   /* here it is not */
434   c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
435   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
436   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) == 2, NULL);
439   test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
440   fail_unless (gst_caps_is_equal (c2, test));
441   gst_caps_unref (c2);
442   gst_caps_unref (test);
443
444   /* second one was already contained in the first one */
445   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
446   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
447   gst_caps_merge (c2, c1);
448   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
449   fail_unless (gst_caps_get_size (c2) == 1, NULL);
450   test = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
451   fail_unless (gst_caps_is_equal (c2, test));
452   gst_caps_unref (c2);
453   gst_caps_unref (test);
454
455   /* second one was already contained in the first one */
456   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
457   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
458   gst_caps_merge (c2, c1);
459   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
460   fail_unless (gst_caps_get_size (c2) == 1, NULL);
461   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
462   fail_unless (gst_caps_is_equal (c2, test));
463   gst_caps_unref (c2);
464   gst_caps_unref (test);
465
466   /* second one was already contained in the first one */
467   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
468   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
469   gst_caps_merge (c2, c1);
470   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
471   fail_unless (gst_caps_get_size (c2) == 1, NULL);
472   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
473   fail_unless (gst_caps_is_equal (c2, test));
474   gst_caps_unref (c2);
475   gst_caps_unref (test);
476
477   /* second one was already contained in the first one */
478   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
479   c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
480   gst_caps_merge (c2, c1);
481   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
482   fail_unless (gst_caps_get_size (c2) == 1, NULL);
483   test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
484   fail_unless (gst_caps_is_equal (c2, test));
485   gst_caps_unref (c2);
486   gst_caps_unref (test);
487
488   /* these caps cannot be merged */
489   c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
490   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
491   gst_caps_merge (c2, c1);
492   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
493   fail_unless (gst_caps_get_size (c2) == 2, NULL);
494   test =
495       gst_caps_from_string
496       ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
497   fail_unless (gst_caps_is_equal (c2, test));
498   gst_caps_unref (c2);
499   gst_caps_unref (test);
500
501   /* these caps cannot be merged */
502   c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
503   c1 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
504   gst_caps_merge (c2, c1);
505   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
506   fail_unless (gst_caps_get_size (c2) == 2, NULL);
507   test =
508       gst_caps_from_string
509       ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
510   fail_unless (gst_caps_is_equal (c2, test));
511   gst_caps_unref (c2);
512   gst_caps_unref (test);
513
514   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
515   c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
516   gst_caps_merge (c2, c1);
517   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
518   fail_unless (gst_caps_get_size (c2) == 2, NULL);
519   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
520       "audio/x-raw,channels={1,2,3,4}");
521   fail_unless (gst_caps_is_equal (c2, test));
522   gst_caps_unref (c2);
523   gst_caps_unref (test);
524
525   c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
526   c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
527   gst_caps_merge (c2, c1);
528   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
529   fail_unless (gst_caps_get_size (c2) == 2, NULL);
530   test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
531       "audio/x-raw,channels={1,3}");
532   fail_unless (gst_caps_is_equal (c2, test));
533   gst_caps_unref (c2);
534   gst_caps_unref (test);
535
536   c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
537   c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
538   test = gst_caps_copy (c1);
539   gst_caps_merge (c2, c1);
540   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
541   fail_unless (gst_caps_is_subset (test, c2));
542   gst_caps_unref (test);
543   gst_caps_unref (c2);
544
545   c2 = gst_caps_from_string ("audio/x-raw");
546   c1 = gst_caps_from_string ("audio/x-raw,channels=1");
547   gst_caps_merge (c2, c1);
548   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
549   fail_unless (gst_caps_get_size (c2) == 1, NULL);
550   test = gst_caps_from_string ("audio/x-raw");
551   fail_unless (gst_caps_is_equal (c2, test));
552   gst_caps_unref (c2);
553   gst_caps_unref (test);
554
555   c2 = gst_caps_from_string ("audio/x-raw,channels=1");
556   c1 = gst_caps_from_string ("audio/x-raw");
557   gst_caps_merge (c2, c1);
558   GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
559   fail_unless (gst_caps_get_size (c2) == 2, NULL);
560   test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
561   fail_unless (gst_caps_is_equal (c2, test));
562   gst_caps_unref (c2);
563   gst_caps_unref (test);
564 }
565
566 GST_END_TEST;
567
568 GST_START_TEST (test_intersect)
569 {
570   GstStructure *s;
571   GstCaps *c1, *c2, *ci1, *ci2;
572
573   /* field not specified = any value possible, so the intersection
574    * should keep fields which are only part of one set of caps */
575   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
576   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
577
578   ci1 = gst_caps_intersect (c2, c1);
579   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
580   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
581   s = gst_caps_get_structure (ci1, 0);
582   fail_unless (gst_structure_has_name (s, "video/x-raw"));
583   fail_unless (gst_structure_get_value (s, "format") != NULL);
584   fail_unless (gst_structure_get_value (s, "width") != NULL);
585
586   /* with changed order */
587   ci2 = gst_caps_intersect (c1, c2);
588   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
589   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
590   s = gst_caps_get_structure (ci2, 0);
591   fail_unless (gst_structure_has_name (s, "video/x-raw"));
592   fail_unless (gst_structure_get_value (s, "format") != NULL);
593   fail_unless (gst_structure_get_value (s, "width") != NULL);
594
595   fail_unless (gst_caps_is_equal (ci1, ci2));
596
597   gst_caps_unref (ci1);
598   gst_caps_unref (ci2);
599
600   gst_caps_unref (c1);
601   gst_caps_unref (c2);
602
603   /* ========== */
604
605   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
606   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
607
608   ci1 = gst_caps_intersect (c2, c1);
609   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
610   fail_unless (gst_caps_is_empty (ci1), NULL);
611
612   /* with changed order */
613   ci2 = gst_caps_intersect (c1, c2);
614   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
615   fail_unless (gst_caps_is_empty (ci2), NULL);
616
617   fail_unless (gst_caps_is_equal (ci1, ci2));
618
619   gst_caps_unref (ci1);
620   gst_caps_unref (ci2);
621
622   gst_caps_unref (c1);
623   gst_caps_unref (c2);
624
625   /* ========== */
626
627   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
628   c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
629
630   ci1 = gst_caps_intersect (c2, c1);
631   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
632   fail_unless (gst_caps_is_empty (ci1), NULL);
633
634   /* with changed order */
635   ci2 = gst_caps_intersect (c1, c2);
636   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
637   fail_unless (gst_caps_is_empty (ci2), NULL);
638
639   fail_unless (gst_caps_is_equal (ci1, ci2));
640
641   gst_caps_unref (ci1);
642   gst_caps_unref (ci2);
643
644   gst_caps_unref (c1);
645   gst_caps_unref (c2);
646
647   /* ========== */
648
649   c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
650   c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,height=30");
651
652   ci1 = gst_caps_intersect (c2, c1);
653   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
654   fail_unless (gst_caps_get_size (ci1) == 1, NULL);
655   s = gst_caps_get_structure (ci1, 0);
656   fail_unless (gst_structure_has_name (s, "video/x-raw"));
657   fail_unless (gst_structure_get_value (s, "format") != NULL);
658   fail_unless (gst_structure_get_value (s, "width") != NULL);
659   fail_unless (gst_structure_get_value (s, "height") != NULL);
660
661   /* with changed order */
662   ci2 = gst_caps_intersect (c1, c2);
663   GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
664   fail_unless (gst_caps_get_size (ci2) == 1, NULL);
665   s = gst_caps_get_structure (ci2, 0);
666   fail_unless (gst_structure_has_name (s, "video/x-raw"));
667   fail_unless (gst_structure_get_value (s, "format") != NULL);
668   fail_unless (gst_structure_get_value (s, "height") != NULL);
669   fail_unless (gst_structure_get_value (s, "width") != 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 GST_END_TEST;
681
682 GST_START_TEST (test_intersect2)
683 {
684   GstCaps *caps1, *caps2, *icaps;
685
686   /* tests array subtraction */
687   caps1 = gst_caps_from_string ("audio/x-raw, "
688       "channel-positions=(int)<                      "
689       "{ 1, 2, 3, 4, 5, 6 },                         "
690       "{ 1, 2, 3, 4, 5, 6 },                         "
691       "{ 1, 2, 3, 4, 5, 6 },                         "
692       "{ 1, 2, 3, 4, 5, 6 },                         "
693       "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
694   caps2 = gst_caps_from_string ("audio/x-raw, "
695       "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
696   icaps = gst_caps_intersect (caps1, caps2);
697   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
698   fail_if (gst_caps_is_empty (icaps));
699   fail_unless (gst_caps_is_equal (icaps, caps2));
700   gst_caps_unref (caps1);
701   gst_caps_unref (caps2);
702   gst_caps_unref (icaps);
703
704   /* ===== */
705
706   caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
707   caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
708   icaps = gst_caps_intersect (caps1, caps2);
709   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
710   fail_if (gst_caps_is_empty (icaps));
711   fail_unless (gst_caps_is_equal (icaps, caps2));
712   gst_caps_unref (caps1);
713   gst_caps_unref (caps2);
714   gst_caps_unref (icaps);
715 }
716
717 GST_END_TEST;
718
719
720 GST_START_TEST (test_intersect_zigzag)
721 {
722   GstCaps *caps1, *caps2, *icaps, *result;
723
724   /* tests if caps order is maintained */
725   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
726   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
727
728   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
729   result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
730   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
731   fail_if (gst_caps_is_empty (icaps));
732   fail_unless (gst_caps_is_equal (icaps, result));
733   gst_caps_unref (icaps);
734   gst_caps_unref (result);
735
736   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
737   result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
738   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
739   fail_if (gst_caps_is_empty (icaps));
740   fail_unless (gst_caps_is_equal (icaps, result));
741   gst_caps_unref (icaps);
742   gst_caps_unref (result);
743
744   gst_caps_unref (caps1);
745   gst_caps_unref (caps2);
746 }
747
748 GST_END_TEST;
749
750
751 GST_START_TEST (test_intersect_first)
752 {
753   GstCaps *caps1, *caps2, *icaps, *result;
754
755   /* tests if caps order is maintained */
756   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
757   caps2 = gst_caps_from_string ("format/C; format/D; format/A");
758   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
759   result = gst_caps_from_string ("format/A; format/C; format/D");
760   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
761   fail_if (gst_caps_is_empty (icaps));
762   fail_unless (gst_caps_is_equal (icaps, result));
763   gst_caps_unref (caps1);
764   gst_caps_unref (caps2);
765   gst_caps_unref (icaps);
766   gst_caps_unref (result);
767 }
768
769 GST_END_TEST;
770
771
772 GST_START_TEST (test_intersect_first2)
773 {
774   GstCaps *caps1, *caps2, *icaps, *result;
775
776   /* tests if caps order is maintained */
777   caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
778   caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");
779
780   icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
781   result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
782   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
783   fail_if (gst_caps_is_empty (icaps));
784   fail_unless (gst_caps_is_equal (icaps, result));
785   gst_caps_unref (icaps);
786   gst_caps_unref (result);
787
788   icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
789   result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
790   GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
791   fail_if (gst_caps_is_empty (icaps));
792   fail_unless (gst_caps_is_equal (icaps, result));
793   gst_caps_unref (icaps);
794   gst_caps_unref (result);
795
796   gst_caps_unref (caps1);
797   gst_caps_unref (caps2);
798 }
799
800 GST_END_TEST;
801
802 GST_START_TEST (test_intersect_duplication)
803 {
804   GstCaps *c1, *c2, *test;
805
806   c1 = gst_caps_from_string
807       ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
808   c2 = gst_caps_from_string
809       ("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 ]");
810
811   test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
812   fail_unless_equals_int (gst_caps_get_size (test), 1);
813   fail_unless (gst_caps_is_equal (c1, test));
814   gst_caps_unref (c1);
815   gst_caps_unref (c2);
816   gst_caps_unref (test);
817 }
818
819 GST_END_TEST;
820
821 static gboolean
822 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
823 {
824   return gst_value_is_fixed (value);
825 }
826
827
828 GST_START_TEST (test_normalize)
829 {
830   GstCaps *in, *norm, *out;
831   guint i;
832
833   in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
834   out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
835   norm = gst_caps_normalize (in);
836   fail_if (gst_caps_is_empty (norm));
837   fail_unless (gst_caps_is_equal (norm, out));
838   for (i = 0; i < gst_caps_get_size (norm); i++) {
839     GstStructure *st = gst_caps_get_structure (norm, i);
840     /* Make sure all fields of all structures are fixed */
841     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
842   }
843
844   gst_caps_unref (in);
845   gst_caps_unref (out);
846   gst_caps_unref (norm);
847
848   in = gst_caps_from_string
849       ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
850   out =
851       gst_caps_from_string
852       ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
853       "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
854   norm = gst_caps_normalize (in);
855   fail_if (gst_caps_is_empty (norm));
856   fail_unless (gst_caps_is_equal (norm, out));
857   for (i = 0; i < gst_caps_get_size (norm); i++) {
858     GstStructure *st = gst_caps_get_structure (norm, i);
859     /* Make sure all fields of all structures are fixed */
860     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
861   }
862
863   gst_caps_unref (in);
864   gst_caps_unref (out);
865   gst_caps_unref (norm);
866
867   in = gst_caps_from_string
868       ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
869   out =
870       gst_caps_from_string
871       ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
872   norm = gst_caps_normalize (in);
873   fail_if (gst_caps_is_empty (norm));
874   fail_unless (gst_caps_is_equal (norm, out));
875   for (i = 0; i < gst_caps_get_size (norm); i++) {
876     GstStructure *st = gst_caps_get_structure (norm, i);
877     /* Make sure all fields of all structures are fixed */
878     fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
879   }
880
881   gst_caps_unref (in);
882   gst_caps_unref (out);
883   gst_caps_unref (norm);
884 }
885
886 GST_END_TEST;
887
888 GST_START_TEST (test_broken)
889 {
890   GstCaps *c1;
891
892   /* NULL is not valid for media_type */
893   ASSERT_CRITICAL (c1 =
894       gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
895   fail_if (c1);
896
897 #ifndef G_DISABLE_CHECKS
898   /* such a name is not valid, see gst_structure_validate_name() */
899   ASSERT_CRITICAL (c1 =
900       gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
901   fail_if (c1);
902 #endif
903 }
904
905 GST_END_TEST;
906
907
908 static Suite *
909 gst_caps_suite (void)
910 {
911   Suite *s = suite_create ("GstCaps");
912   TCase *tc_chain = tcase_create ("operations");
913
914   suite_add_tcase (s, tc_chain);
915   tcase_add_test (tc_chain, test_from_string);
916   tcase_add_test (tc_chain, test_double_append);
917   tcase_add_test (tc_chain, test_mutability);
918   tcase_add_test (tc_chain, test_static_caps);
919   tcase_add_test (tc_chain, test_simplify);
920   tcase_add_test (tc_chain, test_truncate);
921   tcase_add_test (tc_chain, test_subset);
922   tcase_add_test (tc_chain, test_merge_fundamental);
923   tcase_add_test (tc_chain, test_merge_same);
924   tcase_add_test (tc_chain, test_merge_subset);
925   tcase_add_test (tc_chain, test_intersect);
926   tcase_add_test (tc_chain, test_intersect2);
927   tcase_add_test (tc_chain, test_intersect_zigzag);
928   tcase_add_test (tc_chain, test_intersect_first);
929   tcase_add_test (tc_chain, test_intersect_first2);
930   tcase_add_test (tc_chain, test_intersect_duplication);
931   tcase_add_test (tc_chain, test_normalize);
932   tcase_add_test (tc_chain, test_broken);
933
934   return s;
935 }
936
937 GST_CHECK_MAIN (gst_caps);