2 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4 * gststructure.c: Unit tests for GstStructure
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include <gst/gststructure.h>
24 #include <gst/check/gstcheck.h>
27 GST_START_TEST (test_from_string_int)
29 const char *strings[] = {
30 "video/x-raw-rgb, width = (int) 123456",
31 "video/x-raw-rgb, stride = (int) -123456",
32 "video/x-raw-rgb, red_mask = (int) 0xFFFF",
33 "video/x-raw-rgb, red_mask = (int) 0x0000FFFF",
34 "video/x-raw-rgb, red_mask = (int) 0x7FFFFFFF",
35 "video/x-raw-rgb, red_mask = (int) 0x80000000",
36 "video/x-raw-rgb, red_mask = (int) 0xFF000000",
38 * gst-launch ... ! "video/x-raw-rgb, red_mask=(int)0xFF000000" ! ... */
39 "video/x-raw-rgb,\\ red_mask=(int)0xFF000000",
51 GstStructure *structure;
54 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
61 structure = gst_structure_from_string (s, NULL);
62 fail_if (structure == NULL, "Could not get structure from string %s", s);
63 name = gst_structure_nth_field_name (structure, 0);
64 fail_unless (gst_structure_get_int (structure, name, &value));
65 fail_unless (value == results[i],
66 "Value %d is not the expected result %d for string %s",
67 value, results[i], s);
70 gst_structure_free (structure);
76 /* Test type conversions from string */
77 GST_START_TEST (test_from_string)
79 GstStructure *structure;
83 s = "test-string,value=1";
84 structure = gst_structure_from_string (s, NULL);
85 fail_if (structure == NULL, "Could not get structure from string %s", s);
86 fail_unless ((val = gst_structure_get_value (structure, "value")) != NULL);
87 fail_unless (G_VALUE_HOLDS_INT (val));
88 gst_structure_free (structure);
90 s = "test-string,value=1.0";
91 structure = gst_structure_from_string (s, NULL);
92 fail_if (structure == NULL, "Could not get structure from string %s", s);
93 fail_unless ((val = gst_structure_get_value (structure, "value")) != NULL);
94 fail_unless (G_VALUE_HOLDS_DOUBLE (val));
95 gst_structure_free (structure);
97 s = "test-string,value=1/1";
98 structure = gst_structure_from_string (s, NULL);
99 fail_if (structure == NULL, "Could not get structure from string %s", s);
100 fail_unless ((val = gst_structure_get_value (structure, "value")) != NULL);
101 fail_unless (GST_VALUE_HOLDS_FRACTION (val));
102 gst_structure_free (structure);
104 s = "test-string,value=bar";
105 structure = gst_structure_from_string (s, NULL);
106 fail_if (structure == NULL, "Could not get structure from string %s", s);
107 fail_unless ((val = gst_structure_get_value (structure, "value")) != NULL);
108 fail_unless (G_VALUE_HOLDS_STRING (val));
109 gst_structure_free (structure);
111 s = "test-string,value=true";
112 structure = gst_structure_from_string (s, NULL);
113 fail_if (structure == NULL, "Could not get structure from string %s", s);
114 fail_unless ((val = gst_structure_get_value (structure, "value")) != NULL);
115 fail_unless (G_VALUE_HOLDS_BOOLEAN (val));
116 fail_unless_equals_int (g_value_get_boolean (val), TRUE);
117 gst_structure_free (structure);
119 /* This should still work for now (FIXME: 0.11) */
120 s = "0.10:decoder-video/mpeg, abc=(boolean)false";
121 structure = gst_structure_from_string (s, NULL);
122 fail_if (structure == NULL, "Could not get structure from string %s", s);
123 gst_structure_free (structure);
125 /* make sure we bail out correctly in case of an error or if parsing fails */
126 s = "***foo***, abc=(boolean)false";
127 structure = gst_structure_from_string (s, NULL);
128 fail_unless (structure == NULL);
134 GST_START_TEST (test_to_string)
138 ASSERT_CRITICAL (st1 = gst_structure_new ("Foo\nwith-newline", NULL));
139 fail_unless (st1 == NULL);
141 /* FIXME 0.11: re-enable this */
143 ASSERT_CRITICAL (st1 = gst_structure_new ("Foo with whitespace", NULL));
144 fail_unless (st1 == NULL);
145 ASSERT_CRITICAL (st1 = gst_structure_new ("1st", NULL));
146 fail_unless (st1 == NULL);
148 st1 = gst_structure_new ("Foo with whitespace is still allowed", NULL);
149 fail_unless (st1 != NULL);
150 gst_structure_free (st1);
152 /* structure names starting with a number are also still allowed */
153 st1 = gst_structure_new ("1st", NULL);
154 fail_unless (st1 != NULL);
155 gst_structure_free (st1);
162 GST_START_TEST (test_to_from_string)
164 GstCaps *caps1, *caps2;
165 GstStructure *st1, *st2;
166 gchar *str, *res1, *res2;
168 /* test escaping/unescaping */
169 st1 = gst_structure_new ("FooBar-123/0_1", "num", G_TYPE_INT, 9173,
170 "string", G_TYPE_STRING, "Something Like Face/Off", NULL);
171 str = gst_structure_to_string (st1);
172 st2 = gst_structure_from_string (str, NULL);
175 fail_unless (st2 != NULL);
177 /* need to put stuctures into caps to compare */
178 caps1 = gst_caps_new_empty ();
179 gst_caps_append_structure (caps1, st1);
180 caps2 = gst_caps_new_empty ();
181 gst_caps_append_structure (caps2, st2);
182 res1 = gst_caps_to_string (caps1);
183 res2 = gst_caps_to_string (caps2);
184 fail_unless (gst_caps_is_equal (caps1, caps2),
185 "Structures did not match:\n\tStructure 1: %s\n\tStructure 2: %s\n",
187 gst_caps_unref (caps1);
188 gst_caps_unref (caps2);
195 GST_START_TEST (test_complete_structure)
197 GstStructure *structure;
200 s = "GstEventSeek, rate=(double)1, format=(GstFormat)GST_FORMAT_TIME, flags=(GstSeekFlags)GST_SEEK_FLAGS_NONE, cur_type=(GstSeekType)GST_SEEK_TYPE_SET, cur=(gint64)1000000000, stop_type=(GstSeekType)GST_SEEK_TYPE_NONE, stop=(gint64)0";
201 structure = gst_structure_from_string (s, NULL);
202 fail_if (structure == NULL, "Could not get structure from string %s", s);
203 /* FIXME: TODO: add checks for correct serialization of members ? */
204 gst_structure_free (structure);
209 GST_START_TEST (test_structure_new)
216 GstClockTime clocktime;
219 s = gst_structure_new ("name",
220 "key", G_TYPE_STRING, "value",
221 "bool", G_TYPE_BOOLEAN, TRUE,
222 "fraction", GST_TYPE_FRACTION, 1, 5,
223 "clocktime", GST_TYPE_CLOCK_TIME, GST_CLOCK_TIME_NONE,
224 "fourcc", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('f', 'o', 'u', 'r'), NULL);
226 fail_unless (gst_structure_get_field_type (s, "unknown") == G_TYPE_INVALID);
227 /* test setting a different name */
228 gst_structure_set_name (s, "newname");
229 fail_unless (strcmp (gst_structure_get_string (s, "key"), "value") == 0);
230 fail_unless (gst_structure_has_field (s, "key"));
231 fail_unless_equals_int (gst_structure_n_fields (s), 5);
232 /* test removing a field */
233 gst_structure_remove_field (s, "key");
234 fail_if (gst_structure_get_string (s, "key"));
235 fail_if (gst_structure_has_field (s, "key"));
236 fail_unless_equals_int (gst_structure_n_fields (s), 4);
238 fail_unless (gst_structure_get_boolean (s, "bool", &bool));
241 fail_unless (gst_structure_get_fraction (s, "fraction", &num, &den));
242 fail_unless_equals_int (num, 1);
243 fail_unless_equals_int (den, 5);
245 fail_unless (gst_structure_get_clock_time (s, "clocktime", &clocktime));
246 fail_unless_equals_uint64 (clocktime, GST_CLOCK_TIME_NONE);
248 fail_unless (gst_structure_get_fourcc (s, "fourcc", &fourcc));
250 gst_structure_free (s);
252 domain = g_quark_from_static_string ("test");
253 e = g_error_new (domain, 0, "a test error");
254 s = gst_structure_new ("name", "key", GST_TYPE_G_ERROR, e, NULL);
256 gst_structure_free (s);
258 /* This should still work for now (FIXME 0.11) */
259 gst_structure_free (gst_structure_new ("0.10:decoder-video/mpeg", NULL));
261 /* make sure we bail out correctly in case of an error or if parsing fails */
262 ASSERT_CRITICAL (s = gst_structure_new ("^joo\nba\ndoo^",
263 "abc", G_TYPE_BOOLEAN, FALSE, NULL));
264 fail_unless (s == NULL);
269 GST_START_TEST (test_fixate)
273 s = gst_structure_new ("name",
274 "int", G_TYPE_INT, 5,
275 "intrange", GST_TYPE_INT_RANGE, 5, 10,
276 "intrange2", GST_TYPE_INT_RANGE, 5, 10, NULL);
278 fail_if (gst_structure_fixate_field_nearest_int (s, "int", 5));
279 fail_unless (gst_structure_fixate_field_nearest_int (s, "intrange", 5));
280 fail_if (gst_structure_fixate_field_nearest_int (s, "intrange", 5));
281 fail_unless (gst_structure_fixate_field_nearest_int (s, "intrange2", 15));
282 fail_if (gst_structure_fixate_field_nearest_int (s, "intrange2", 15));
283 gst_structure_free (s);
288 GST_START_TEST (test_fixate_frac_list)
296 g_value_init (&list, GST_TYPE_LIST);
297 g_value_init (&frac, GST_TYPE_FRACTION);
299 gst_value_set_fraction (&frac, 30, 1);
300 gst_value_list_append_value (&list, &frac);
301 gst_value_set_fraction (&frac, 15, 1);
302 gst_value_list_append_value (&list, &frac);
303 gst_value_set_fraction (&frac, 10, 1);
304 gst_value_list_append_value (&list, &frac);
306 s = gst_structure_new ("name", NULL);
307 gst_structure_set_value (s, "frac", &list);
308 g_value_unset (&frac);
309 g_value_unset (&list);
311 str = gst_structure_to_string (s);
312 GST_DEBUG ("list %s", str);
315 /* fixate to the nearest fraction, this should give 15/1 */
316 fail_unless (gst_structure_fixate_field_nearest_fraction (s, "frac", 14, 1));
318 fail_unless (gst_structure_get_fraction (s, "frac", &num, &denom));
319 fail_unless (num == 15);
320 fail_unless (denom == 1);
322 gst_structure_free (s);
327 GST_START_TEST (test_structure_nested)
329 GstStructure *sp, *sc1, *sc2;
333 gst_structure_new ("Camera", "XResolution", G_TYPE_INT, 72, "YResolution",
334 G_TYPE_INT, 73, NULL);
335 fail_unless (sc1 != NULL);
338 gst_structure_new ("Image-Data", "Orientation", G_TYPE_STRING, "top-left",
340 fail_unless (sc2 != NULL);
342 sp = gst_structure_new ("Exif", "Camera", GST_TYPE_STRUCTURE, sc1,
343 "Image Data", GST_TYPE_STRUCTURE, sc2, NULL);
344 fail_unless (sp != NULL);
346 fail_unless (gst_structure_has_field_typed (sp, "Camera",
347 GST_TYPE_STRUCTURE));
349 str = gst_structure_to_string (sp);
350 fail_unless (str != NULL);
352 fail_unless (g_str_equal (str,
354 ", Camera=(structure)Camera, XResolution=(int)72, YResolution=(int)73;"
355 ", Image Data=(structure)Image-Data, Orientation=(string)top-left;;"));
360 gst_structure_free (sc1);
361 gst_structure_free (sc2);
362 gst_structure_free (sp);
369 gst_structure_suite (void)
371 Suite *s = suite_create ("GstStructure");
372 TCase *tc_chain = tcase_create ("general");
374 suite_add_tcase (s, tc_chain);
375 tcase_add_test (tc_chain, test_from_string_int);
376 tcase_add_test (tc_chain, test_from_string);
377 tcase_add_test (tc_chain, test_to_string);
378 tcase_add_test (tc_chain, test_to_from_string);
379 tcase_add_test (tc_chain, test_complete_structure);
380 tcase_add_test (tc_chain, test_structure_new);
381 tcase_add_test (tc_chain, test_fixate);
382 tcase_add_test (tc_chain, test_fixate_frac_list);
383 tcase_add_test (tc_chain, test_structure_nested);
387 GST_CHECK_MAIN (gst_structure);