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>
27 GST_START_TEST (test_from_string)
34 for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
35 caps = gst_caps_from_string (caps_list[i]);
36 fail_if (caps == NULL,
37 "Could not create caps from string %s\n", caps_list[i]);
38 to_str = gst_caps_to_string (caps);
39 fail_if (to_str == NULL,
40 "Could not convert caps back to string %s\n", caps_list[i]);
41 caps2 = gst_caps_from_string (caps_list[i]);
42 fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);
44 fail_unless (gst_caps_is_equal (caps, caps));
45 fail_unless (gst_caps_is_equal (caps, caps2));
47 gst_caps_unref (caps);
48 gst_caps_unref (caps2);
55 GST_START_TEST (test_buffer)
60 buffer = gst_buffer_new_and_alloc (1000);
61 c1 = gst_caps_new_simple ("audio/x-raw-int",
62 "buffer", GST_TYPE_BUFFER, buffer, NULL);
64 GST_DEBUG ("caps: %" GST_PTR_FORMAT, c1);
65 gst_buffer_unref (buffer);
67 buffer = gst_buffer_new_and_alloc (1000);
68 gst_buffer_set_caps (buffer, c1); /* doesn't give away our c1 ref */
71 gst_buffer_unref (buffer); /* Should now drop both references */
76 GST_START_TEST (test_double_append)
81 c1 = gst_caps_new_any ();
82 s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
83 gst_caps_append_structure (c1, s1);
84 ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
91 GST_START_TEST (test_mutability)
97 c1 = gst_caps_new_any ();
98 s1 = gst_structure_from_string ("audio/x-raw-int,rate=44100", NULL);
99 gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
100 gst_caps_append_structure (c1, s1);
101 gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
103 ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
104 fail_unless (gst_structure_get_int (s1, "rate", &ret));
105 fail_unless (ret == 22500);
106 ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
107 fail_unless (gst_structure_get_int (s1, "rate", &ret));
108 fail_unless (ret == 22500);
110 gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
111 fail_unless (gst_structure_get_int (s1, "rate", &ret));
112 fail_unless (ret == 11250);
113 gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
114 fail_unless (gst_structure_get_int (s1, "rate", &ret));
115 fail_unless (ret == 1);
121 GST_START_TEST (test_static_caps)
123 static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw-int,rate=44100");
128 caps1 = gst_static_caps_get (&scaps);
129 fail_unless (caps1 != NULL);
130 /* 1 refcount core, one from us */
131 fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);
133 /* caps should be the same */
134 caps2 = gst_static_caps_get (&scaps);
135 fail_unless (caps2 != NULL);
136 /* 1 refcount core, two from us */
137 fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
138 /* caps must be equal */
139 fail_unless (caps1 == caps2);
141 gst_caps_unref (caps1);
142 gst_caps_unref (caps2);
147 static const gchar non_simple_caps_string[] =
148 "video/x-raw-yuv, format=(fourcc)I420, framerate=(fraction)[ 1/100, 100 ], "
149 "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
150 "format=(fourcc)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
151 "height=(int)[ 16, 4096 ]; video/x-raw-rgb, bpp=(int)8, depth=(int)8, "
152 "endianness=(int)1234, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
153 "height=(int)[ 16, 4096 ]; video/x-raw-yuv, "
154 "format=(fourcc){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
155 "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
158 check_fourcc_list (const GValue * format_value)
160 const GValue *fourcc_value;
161 gboolean got_yv12 = FALSE;
162 gboolean got_i420 = FALSE;
163 gboolean got_yuy2 = FALSE;
166 fourcc_value = gst_value_list_get_value (format_value, 0);
167 fail_unless (fourcc_value != NULL);
168 fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
169 fourcc = gst_value_get_fourcc (fourcc_value);
170 fail_unless (fourcc != 0);
171 got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
172 got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
173 got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
175 fourcc_value = gst_value_list_get_value (format_value, 1);
176 fail_unless (fourcc_value != NULL);
177 fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
178 fourcc = gst_value_get_fourcc (fourcc_value);
179 fail_unless (fourcc != 0);
180 got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
181 got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
182 got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
184 fourcc_value = gst_value_list_get_value (format_value, 2);
185 fail_unless (fourcc_value != NULL);
186 fail_unless (GST_VALUE_HOLDS_FOURCC (fourcc_value));
187 fourcc = gst_value_get_fourcc (fourcc_value);
188 fail_unless (fourcc != 0);
189 got_i420 = got_i420 || (fourcc == GST_STR_FOURCC ("I420"));
190 got_yuy2 = got_yuy2 || (fourcc == GST_STR_FOURCC ("YUY2"));
191 got_yv12 = got_yv12 || (fourcc == GST_STR_FOURCC ("YV12"));
193 return (got_i420 && got_yuy2 && got_yv12);
196 GST_START_TEST (test_simplify)
198 GstStructure *s1, *s2;
199 gboolean did_simplify;
202 caps = gst_caps_from_string (non_simple_caps_string);
203 fail_unless (caps != NULL,
204 "gst_caps_from_string (non_simple_caps_string) failed");
206 did_simplify = gst_caps_do_simplify (caps);
207 fail_unless (did_simplify == TRUE,
208 "gst_caps_do_simplify() should have worked");
210 /* check simplified caps, should be:
212 * video/x-raw-rgb, bpp=(int)8, depth=(int)8, endianness=(int)1234,
213 * framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ],
214 * height=(int)[ 16, 4096 ];
215 * video/x-raw-yuv, format=(fourcc){ YV12, YUY2, I420 },
216 * width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
217 * framerate=(fraction)[ 1/100, 100 ]
219 fail_unless (gst_caps_get_size (caps) == 2);
220 s1 = gst_caps_get_structure (caps, 0);
221 s2 = gst_caps_get_structure (caps, 1);
222 fail_unless (s1 != NULL);
223 fail_unless (s2 != NULL);
225 if (!gst_structure_has_name (s1, "video/x-raw-rgb")) {
233 fail_unless (gst_structure_has_name (s1, "video/x-raw-rgb"));
235 const GValue *framerate_value;
236 const GValue *width_value;
237 const GValue *height_value;
238 const GValue *val_fps;
239 GValue test_fps = { 0, };
240 gint bpp, depth, endianness;
241 gint min_width, max_width;
242 gint min_height, max_height;
244 fail_unless (gst_structure_get_int (s1, "bpp", &bpp));
245 fail_unless (bpp == 8);
247 fail_unless (gst_structure_get_int (s1, "depth", &depth));
248 fail_unless (depth == 8);
250 fail_unless (gst_structure_get_int (s1, "endianness", &endianness));
251 fail_unless (endianness == G_LITTLE_ENDIAN);
253 g_value_init (&test_fps, GST_TYPE_FRACTION);
254 framerate_value = gst_structure_get_value (s1, "framerate");
255 fail_unless (framerate_value != NULL);
256 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
258 val_fps = gst_value_get_fraction_range_min (framerate_value);
259 gst_value_set_fraction (&test_fps, 1, 100);
260 fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
262 val_fps = gst_value_get_fraction_range_max (framerate_value);
263 gst_value_set_fraction (&test_fps, 100, 1);
264 fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
266 g_value_unset (&test_fps);
268 width_value = gst_structure_get_value (s1, "width");
269 fail_unless (width_value != NULL);
270 fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
271 min_width = gst_value_get_int_range_min (width_value);
272 max_width = gst_value_get_int_range_max (width_value);
273 fail_unless (min_width == 16 && max_width == 4096);
275 height_value = gst_structure_get_value (s1, "height");
276 fail_unless (height_value != NULL);
277 fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
278 min_height = gst_value_get_int_range_min (height_value);
279 max_height = gst_value_get_int_range_max (height_value);
280 fail_unless (min_height == 16 && max_height == 4096);
283 fail_unless (gst_structure_has_name (s2, "video/x-raw-yuv"));
285 const GValue *framerate_value;
286 const GValue *format_value;
287 const GValue *width_value;
288 const GValue *height_value;
289 const GValue *val_fps;
290 GValue test_fps = { 0, };
291 gint min_width, max_width;
292 gint min_height, max_height;
294 format_value = gst_structure_get_value (s2, "format");
295 fail_unless (format_value != NULL);
296 fail_unless (GST_VALUE_HOLDS_LIST (format_value));
297 fail_unless (gst_value_list_get_size (format_value) == 3);
298 fail_unless (check_fourcc_list (format_value) == TRUE);
300 g_value_init (&test_fps, GST_TYPE_FRACTION);
301 framerate_value = gst_structure_get_value (s2, "framerate");
302 fail_unless (framerate_value != NULL);
303 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));
305 val_fps = gst_value_get_fraction_range_min (framerate_value);
306 gst_value_set_fraction (&test_fps, 1, 100);
307 fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
309 val_fps = gst_value_get_fraction_range_max (framerate_value);
310 gst_value_set_fraction (&test_fps, 100, 1);
311 fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);
313 g_value_unset (&test_fps);
315 width_value = gst_structure_get_value (s2, "width");
316 fail_unless (width_value != NULL);
317 fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
318 min_width = gst_value_get_int_range_min (width_value);
319 max_width = gst_value_get_int_range_max (width_value);
320 fail_unless (min_width == 16 && max_width == 4096);
322 height_value = gst_structure_get_value (s2, "height");
323 fail_unless (height_value != NULL);
324 fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
325 min_height = gst_value_get_int_range_min (height_value);
326 max_height = gst_value_get_int_range_max (height_value);
327 fail_unless (min_height == 16 && max_height == 4096);
330 gst_caps_unref (caps);
336 gst_caps_suite (void)
338 Suite *s = suite_create ("GstCaps");
339 TCase *tc_chain = tcase_create ("mutability");
341 suite_add_tcase (s, tc_chain);
342 tcase_add_test (tc_chain, test_from_string);
343 tcase_add_test (tc_chain, test_double_append);
344 tcase_add_test (tc_chain, test_mutability);
345 tcase_add_test (tc_chain, test_buffer);
346 tcase_add_test (tc_chain, test_static_caps);
347 tcase_add_test (tc_chain, test_simplify);
352 GST_CHECK_MAIN (gst_caps);