2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
5 * gstcaps.c: Unit test for GstCaps
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.
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.
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.
24 #include <gst/check/gstcheck.h>
25 #include <gst/gstcaps.h>
28 GST_START_TEST (test_from_string)
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);
45 fail_unless (gst_caps_is_equal (caps, caps));
46 fail_unless (gst_caps_is_equal (caps, caps2));
48 gst_caps_unref (caps);
49 gst_caps_unref (caps2);
56 GST_START_TEST (test_double_append)
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));
71 GST_START_TEST (test_mutability)
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);
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);
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);
101 GST_START_TEST (test_static_caps)
103 static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw,rate=44100");
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);
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);
121 gst_caps_unref (caps1);
122 gst_caps_unref (caps2);
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 ]";
138 check_string_list (const GValue * format_value)
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;
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"));
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"));
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"));
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"));
187 return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
190 GST_START_TEST (test_simplify)
193 gboolean did_simplify;
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");
200 did_simplify = gst_caps_do_simplify (caps);
201 fail_unless (did_simplify == TRUE,
202 "gst_caps_do_simplify() should have worked");
204 /* check simplified caps, should be:
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 ]
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);
215 fail_unless (gst_structure_has_name (s1, "video/x-raw"));
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;
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);
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));
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);
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);
245 g_value_unset (&test_fps);
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);
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);
262 gst_caps_unref (caps);
267 GST_START_TEST (test_truncate)
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);
282 GST_START_TEST (test_subset)
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));
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));
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));
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));
321 GST_START_TEST (test_merge_fundamental)
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);
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);
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);
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);
364 GST_START_TEST (test_merge_same)
366 GstCaps *c1, *c2, *test;
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);
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);
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);
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);
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);
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);
418 GST_START_TEST (test_merge_subset)
420 GstCaps *c1, *c2, *test;
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));
431 gst_caps_unref (test);
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));
442 gst_caps_unref (test);
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));
453 gst_caps_unref (test);
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));
464 gst_caps_unref (test);
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));
475 gst_caps_unref (test);
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));
486 gst_caps_unref (test);
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);
496 ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
497 fail_unless (gst_caps_is_equal (c2, test));
499 gst_caps_unref (test);
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);
509 ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
510 fail_unless (gst_caps_is_equal (c2, test));
512 gst_caps_unref (test);
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));
523 gst_caps_unref (test);
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));
534 gst_caps_unref (test);
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);
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));
553 gst_caps_unref (test);
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));
563 gst_caps_unref (test);
568 GST_START_TEST (test_intersect)
571 GstCaps *c1, *c2, *ci1, *ci2;
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");
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);
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);
595 fail_unless (gst_caps_is_equal (ci1, ci2));
597 gst_caps_unref (ci1);
598 gst_caps_unref (ci2);
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");
608 ci1 = gst_caps_intersect (c2, c1);
609 GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
610 fail_unless (gst_caps_is_empty (ci1), NULL);
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);
617 fail_unless (gst_caps_is_equal (ci1, ci2));
619 gst_caps_unref (ci1);
620 gst_caps_unref (ci2);
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");
630 ci1 = gst_caps_intersect (c2, c1);
631 GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
632 fail_unless (gst_caps_is_empty (ci1), NULL);
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);
639 fail_unless (gst_caps_is_equal (ci1, ci2));
641 gst_caps_unref (ci1);
642 gst_caps_unref (ci2);
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");
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);
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);
671 fail_unless (gst_caps_is_equal (ci1, ci2));
673 gst_caps_unref (ci1);
674 gst_caps_unref (ci2);
682 GST_START_TEST (test_intersect2)
684 GstCaps *caps1, *caps2, *icaps;
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);
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);
720 GST_START_TEST (test_intersect_zigzag)
722 GstCaps *caps1, *caps2, *icaps, *result;
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");
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);
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);
744 gst_caps_unref (caps1);
745 gst_caps_unref (caps2);
751 GST_START_TEST (test_intersect_first)
753 GstCaps *caps1, *caps2, *icaps, *result;
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);
772 GST_START_TEST (test_intersect_first2)
774 GstCaps *caps1, *caps2, *icaps, *result;
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");
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);
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);
796 gst_caps_unref (caps1);
797 gst_caps_unref (caps2);
802 GST_START_TEST (test_intersect_duplication)
804 GstCaps *c1, *c2, *test;
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 ]");
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));
816 gst_caps_unref (test);
822 _caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
824 return gst_value_is_fixed (value);
828 GST_START_TEST (test_normalize)
830 GstCaps *in, *norm, *out;
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));
845 gst_caps_unref (out);
846 gst_caps_unref (norm);
848 in = gst_caps_from_string
849 ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
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));
864 gst_caps_unref (out);
865 gst_caps_unref (norm);
867 in = gst_caps_from_string
868 ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
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));
882 gst_caps_unref (out);
883 gst_caps_unref (norm);
888 GST_START_TEST (test_broken)
892 /* NULL is not valid for media_type */
893 ASSERT_CRITICAL (c1 =
894 gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
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));
909 gst_caps_suite (void)
911 Suite *s = suite_create ("GstCaps");
912 TCase *tc_chain = tcase_create ("operations");
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);
937 GST_CHECK_MAIN (gst_caps);