And correct even more valid sparse warnings.
[platform/upstream/gstreamer.git] / tests / check / gst / gststructure.c
1 /* GStreamer
2  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gststructure.c: Unit tests for GstStructure
5  *
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.
10  *
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.
15  *
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.
20  */
21
22
23 #include <gst/gststructure.h>
24 #include <gst/check/gstcheck.h>
25
26
27 GST_START_TEST (test_from_string_int)
28 {
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",
37     /* result from
38      * gst-launch ... ! "video/x-raw-rgb, red_mask=(int)0xFF000000" ! ... */
39     "video/x-raw-rgb,\\ red_mask=(int)0xFF000000",
40   };
41   gint results[] = {
42     123456,
43     -123456,
44     0xFFFF,
45     0xFFFF,
46     0x7FFFFFFF,
47     (gint) 0x80000000,
48     (gint) 0xFF000000,
49     (gint) 0xFF000000,
50   };
51   GstStructure *structure;
52   int i;
53
54   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
55     const char *s;
56     const gchar *name;
57     gint value;
58
59     s = strings[i];
60
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);
68
69     /* cleanup */
70     gst_structure_free (structure);
71   }
72 }
73
74 GST_END_TEST;
75
76 /* Test type conversions from string */
77 GST_START_TEST (test_from_string)
78 {
79   GstStructure *structure;
80   const gchar *s;
81   const GValue *val;
82
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);
89
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);
96
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);
103
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);
110
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);
118
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);
124
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);
129 }
130
131 GST_END_TEST;
132
133
134 GST_START_TEST (test_to_string)
135 {
136   GstStructure *st1;
137
138   ASSERT_CRITICAL (st1 = gst_structure_new ("Foo\nwith-newline", NULL));
139   fail_unless (st1 == NULL);
140
141   /* FIXME 0.11: re-enable this */
142 #if 0
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);
147 #else
148   st1 = gst_structure_new ("Foo with whitespace is still allowed", NULL);
149   fail_unless (st1 != NULL);
150   gst_structure_free (st1);
151
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);
156 #endif
157 }
158
159 GST_END_TEST;
160
161
162 GST_START_TEST (test_to_from_string)
163 {
164   GstCaps *caps1, *caps2;
165   GstStructure *st1, *st2;
166   gchar *str, *res1, *res2;
167
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);
173   g_free (str);
174
175   fail_unless (st2 != NULL);
176
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",
186       res1, res2);
187   gst_caps_unref (caps1);
188   gst_caps_unref (caps2);
189   g_free (res1);
190   g_free (res2);
191 }
192
193 GST_END_TEST;
194
195 GST_START_TEST (test_complete_structure)
196 {
197   GstStructure *structure;
198   const gchar *s;
199
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);
205 }
206
207 GST_END_TEST;
208
209 GST_START_TEST (test_structure_new)
210 {
211   GstStructure *s;
212   GError *e;
213   GQuark domain;
214   gboolean bool;
215   gint num, den;
216   GstClockTime clocktime;
217   guint32 fourcc;
218
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);
225
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);
237
238   fail_unless (gst_structure_get_boolean (s, "bool", &bool));
239   fail_unless (bool);
240
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);
244
245   fail_unless (gst_structure_get_clock_time (s, "clocktime", &clocktime));
246   fail_unless_equals_uint64 (clocktime, GST_CLOCK_TIME_NONE);
247
248   fail_unless (gst_structure_get_fourcc (s, "fourcc", &fourcc));
249
250   gst_structure_free (s);
251
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);
255   g_error_free (e);
256   gst_structure_free (s);
257
258   /* This should still work for now (FIXME 0.11) */
259   gst_structure_free (gst_structure_new ("0.10:decoder-video/mpeg", NULL));
260
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);
265 }
266
267 GST_END_TEST;
268
269 GST_START_TEST (test_fixate)
270 {
271   GstStructure *s;
272
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);
277
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);
284 }
285
286 GST_END_TEST;
287
288 GST_START_TEST (test_fixate_frac_list)
289 {
290   GstStructure *s;
291   GValue list = { 0 };
292   GValue frac = { 0 };
293   gchar *str;
294   gint num, denom;
295
296   g_value_init (&list, GST_TYPE_LIST);
297   g_value_init (&frac, GST_TYPE_FRACTION);
298
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);
305
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);
310
311   str = gst_structure_to_string (s);
312   GST_DEBUG ("list %s", str);
313   g_free (str);
314
315   /* fixate to the nearest fraction, this should give 15/1 */
316   fail_unless (gst_structure_fixate_field_nearest_fraction (s, "frac", 14, 1));
317
318   fail_unless (gst_structure_get_fraction (s, "frac", &num, &denom));
319   fail_unless (num == 15);
320   fail_unless (denom == 1);
321
322   gst_structure_free (s);
323 }
324
325 GST_END_TEST;
326
327 GST_START_TEST (test_structure_nested)
328 {
329   GstStructure *sp, *sc1, *sc2;
330   gchar *str;
331
332   sc1 =
333       gst_structure_new ("Camera", "XResolution", G_TYPE_INT, 72, "YResolution",
334       G_TYPE_INT, 73, NULL);
335   fail_unless (sc1 != NULL);
336
337   sc2 =
338       gst_structure_new ("Image-Data", "Orientation", G_TYPE_STRING, "top-left",
339       NULL);
340   fail_unless (sc2 != NULL);
341
342   sp = gst_structure_new ("Exif", "Camera", GST_TYPE_STRUCTURE, sc1,
343       "Image Data", GST_TYPE_STRUCTURE, sc2, NULL);
344   fail_unless (sp != NULL);
345
346   fail_unless (gst_structure_has_field_typed (sp, "Camera",
347           GST_TYPE_STRUCTURE));
348
349   str = gst_structure_to_string (sp);
350   fail_unless (str != NULL);
351
352   fail_unless (g_str_equal (str,
353           "Exif"
354           ", Camera=(structure)Camera, XResolution=(int)72, YResolution=(int)73;"
355           ", Image Data=(structure)Image-Data, Orientation=(string)top-left;;"));
356
357   g_free (str);
358   str = NULL;
359
360   gst_structure_free (sc1);
361   gst_structure_free (sc2);
362   gst_structure_free (sp);
363
364 }
365
366 GST_END_TEST;
367
368 static Suite *
369 gst_structure_suite (void)
370 {
371   Suite *s = suite_create ("GstStructure");
372   TCase *tc_chain = tcase_create ("general");
373
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);
384   return s;
385 }
386
387 GST_CHECK_MAIN (gst_structure);