add debugging category use GST_START_TEST now, so we add a debug line
[platform/upstream/gstreamer.git] / tests / check / gst / gstvalue.c
1 /* GStreamer
2  * Copyright (C) <2004> David Schleef <david at schleef dot org>
3  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * gstvalue.c: Unit tests for GstValue
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 "../gstcheck.h"
25
26
27 GST_START_TEST (test_deserialize_buffer)
28 {
29   GValue value = { 0 };
30
31   g_value_init (&value, GST_TYPE_BUFFER);
32   fail_unless (gst_value_deserialize (&value, "1234567890abcdef"));
33 }
34
35 GST_END_TEST;
36
37 GST_START_TEST (test_deserialize_gint64)
38 {
39   GValue value = { 0 };
40   const char *strings[] = {
41     "12345678901",
42     "-12345678901",
43   };
44   gint64 results[] = {
45     12345678901LL,
46     -12345678901LL,
47   };
48   int i;
49
50   g_value_init (&value, G_TYPE_INT64);
51
52   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
53     fail_unless (gst_value_deserialize (&value, strings[i]),
54         "could not deserialize %s (%d)", strings[i], i);
55     fail_unless (g_value_get_int64 (&value) == results[i],
56         "resulting value is %" G_GINT64_FORMAT ", not %" G_GINT64_FORMAT
57         ", for string %s (%d)", value, results[i], strings[i], i);
58   }
59 }
60
61 GST_END_TEST;
62
63 GST_START_TEST (test_deserialize_gint)
64 {
65   GValue value = { 0 };
66   const char *strings[] = {
67     "123456",
68     "-123456",
69     "0xFFFF",
70     "0x0000FFFF",
71     /* a positive long long, serializing to highest possible positive sint */
72     "0x7FFFFFFF",
73     /* a positive long long, serializing to lowest possible negative sint */
74     "0x80000000",
75     /* a negative long long, serializing to lowest possible negative sint */
76     "0xFFFFFFFF80000000",
77     "0xFF000000",
78     /* a positive long long serializing to -1 */
79     "0xFFFFFFFF",
80     "0xFFFFFFFF",
81     /* a negative long long serializing to -1 */
82     "0xFFFFFFFFFFFFFFFF",
83     "0xFFFFFFFFFFFFFFFF",
84     "0xEFFFFFFF",
85   };
86   gint results[] = {
87     123456,
88     -123456,
89     0xFFFF,
90     0xFFFF,
91     0x7FFFFFFF,
92     0x80000000,
93     0x80000000,
94     0xFF000000,
95     -1,
96     0xFFFFFFFF,
97     -1,
98     /* cast needs to be explicit because of unsigned -> signed */
99     (gint) 0xFFFFFFFFFFFFFFFFLL,
100     0xEFFFFFFF,
101   };
102   int i;
103
104   g_value_init (&value, G_TYPE_INT);
105
106   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
107     fail_unless (gst_value_deserialize (&value, strings[i]),
108         "could not deserialize %s (%d)", strings[i], i);
109     fail_unless (g_value_get_int (&value) == results[i],
110         "resulting value is %d, not %d, for string %s (%d)",
111         g_value_get_int (&value), results[i], strings[i], i);
112   }
113 }
114
115 GST_END_TEST;
116
117 GST_START_TEST (test_deserialize_gint_failures)
118 {
119   GValue value = { 0 };
120   const char *strings[] = {
121     "-",                        /* not a complete number */
122     "- TEST",                   /* not a complete number */
123     "0x0000000100000000",       /* lowest long long that cannot fit in 32 bits */
124     "0xF000000000000000",
125     "0xFFFFFFF000000000",
126     "0xFFFFFFFF00000000",
127     "0x10000000000000000",      /* first number too long to fit into a long long */
128     /* invent a new processor first before trying to make this one pass */
129     "0x10000000000000000000000000000000000000000000",
130   };
131   int i;
132
133   g_value_init (&value, G_TYPE_INT);
134
135   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
136     fail_if (gst_value_deserialize (&value, strings[i]),
137         "deserialized %s (%d), while it should have failed", strings[i], i);
138   }
139 }
140
141 GST_END_TEST;
142
143 GST_START_TEST (test_deserialize_guint)
144 {
145   GValue value = { 0 };
146   const char *strings[] = {
147     "123456",
148     "-123456",
149     "0xFFFF",
150     "0x0000FFFF",
151     /* a positive long long, serializing to highest possible positive sint */
152     "0x7FFFFFFF",
153     /* a positive long long, serializing to lowest possible negative sint */
154     "0x80000000",
155     "2147483648",
156     /* a negative long long, serializing to lowest possible negative sint */
157     "0xFFFFFFFF80000000",
158     /* a value typically used for rgb masks */
159     "0xFF000000",
160     /* a positive long long serializing to highest possible positive uint */
161     "0xFFFFFFFF",
162     "0xFFFFFFFF",
163     /* a negative long long serializing to highest possible positive uint */
164     "0xFFFFFFFFFFFFFFFF",
165     "0xEFFFFFFF",
166   };
167   guint results[] = {
168     123456,
169     -123456,
170     0xFFFF,
171     0xFFFF,
172     0x7FFFFFFF,
173     0x80000000,
174     (guint) 2147483648LL,
175     0x80000000,
176     0xFF000000,
177     0xFFFFFFFF,
178     G_MAXUINT,
179     (guint) 0xFFFFFFFFFFFFFFFFLL,
180     0xEFFFFFFF,
181   };
182   int i;
183
184   g_value_init (&value, G_TYPE_UINT);
185
186   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
187     fail_unless (gst_value_deserialize (&value, strings[i]),
188         "could not deserialize %s (%d)", strings[i], i);
189     fail_unless (g_value_get_uint (&value) == results[i],
190         "resulting value is %d, not %d, for string %s (%d)",
191         g_value_get_uint (&value), results[i], strings[i], i);
192   }
193 }
194
195 GST_END_TEST;
196
197 GST_START_TEST (test_deserialize_guint_failures)
198 {
199   GValue value = { 0 };
200   const char *strings[] = {
201     "-",                        /* not a complete number */
202     "- TEST",                   /* not a complete number */
203 #if 0
204 /* FIXME: these values should not be deserializable, since they overflow
205  * the target format */
206     "0x0000000100000000",       /* lowest long long that cannot fit in 32 bits */
207     "0xF000000000000000",
208     "0xFFFFFFF000000000",
209     "0xFFFFFFFF00000000",
210     "0x10000000000000000",      /* first number too long to fit into a long long */
211     /* invent a new processor first before trying to make this one pass */
212     "0x10000000000000000000000000000000000000000000",
213 #endif
214   };
215   int i;
216
217   g_value_init (&value, G_TYPE_UINT);
218
219   for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
220     fail_if (gst_value_deserialize (&value, strings[i]),
221         "deserialized %s (%d), while it should have failed", strings[i], i);
222   }
223 }
224
225 GST_END_TEST;
226
227
228 GST_START_TEST (test_string)
229 {
230   gchar *try[] = {
231     "Dude",
232     "Hi, I'm a string",
233     "tüüüt!"
234   };
235   gchar *tmp;
236   GValue v = { 0, };
237   guint i;
238
239   g_value_init (&v, G_TYPE_STRING);
240   for (i = 0; i < G_N_ELEMENTS (try); i++) {
241     g_value_set_string (&v, try[i]);
242     tmp = gst_value_serialize (&v);
243     fail_if (tmp == NULL, "couldn't serialize: %s\n", try[i]);
244     fail_unless (gst_value_deserialize (&v, tmp),
245         "couldn't deserialize: %s\n", tmp);
246     g_free (tmp);
247
248     fail_unless (g_str_equal (g_value_get_string (&v), try[i]),
249         "\nserialized  : %s\ndeserialized: %s", try[i],
250         g_value_get_string (&v));
251   }
252   g_value_unset (&v);
253 }
254
255 GST_END_TEST;
256
257 GST_START_TEST (test_deserialize_string)
258 {
259   struct
260   {
261     gchar *from;
262     gchar *to;
263   } tests[] = {
264     {
265     "", ""}, {
266     "\"\"", ""},
267         /* FAILURES */
268     {
269     "\"", NULL},                /* missing second quote */
270     {
271     "\"Hello\\ World", NULL},   /* missing second quote */
272     {
273     "\"\\", NULL},              /* quote at end, missing second quote */
274     {
275     "\"\\0", NULL},             /* missing second quote */
276     {
277     "\"\\0\"", NULL},           /* unfinished escaped character */
278     {
279     "\" \"", NULL},             /* spaces must be escaped */
280 #if 0
281         /* FIXME 0.9: this test should fail, but it doesn't */
282     {
283     "tüüt", NULL}             /* string with special chars must be escaped */
284 #endif
285   };
286   guint i;
287   GValue v = { 0, };
288   gboolean ret = TRUE;
289
290   g_value_init (&v, G_TYPE_STRING);
291   for (i = 0; i < G_N_ELEMENTS (tests); i++) {
292     if (gst_value_deserialize (&v, tests[i].from)) {
293       fail_if (tests[i].to == NULL,
294           "I got %s instead of a failure", g_value_get_string (&v));
295       fail_unless (g_str_equal (g_value_get_string (&v), tests[i].to),
296           "\nwanted: %s\ngot    : %s", tests[i].to, g_value_get_string (&v));
297     } else {
298       fail_if (tests[i].to != NULL, "failed, but wanted: %s", tests[i].to);
299       ret = FALSE;
300     }
301   }
302   g_value_unset (&v);
303 }
304
305 GST_END_TEST;
306
307 Suite *
308 gst_value_suite (void)
309 {
310   Suite *s = suite_create ("GstValue");
311   TCase *tc_chain = tcase_create ("general");
312
313   suite_add_tcase (s, tc_chain);
314   tcase_add_test (tc_chain, test_deserialize_buffer);
315   tcase_add_test (tc_chain, test_deserialize_gint);
316   tcase_add_test (tc_chain, test_deserialize_gint_failures);
317   tcase_add_test (tc_chain, test_deserialize_guint);
318   tcase_add_test (tc_chain, test_deserialize_guint_failures);
319   tcase_add_test (tc_chain, test_deserialize_gint64);
320   tcase_add_test (tc_chain, test_string);
321   tcase_add_test (tc_chain, test_deserialize_string);
322   return s;
323 }
324
325 int
326 main (int argc, char **argv)
327 {
328   int nf;
329
330   Suite *s = gst_value_suite ();
331   SRunner *sr = srunner_create (s);
332
333   gst_check_init (&argc, &argv);
334
335   srunner_run_all (sr, CK_NORMAL);
336   nf = srunner_ntests_failed (sr);
337   srunner_free (sr);
338
339   return nf;
340 }