2 * Copyright (C) <2004> David Schleef <david at schleef dot org>
3 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
5 * gstvalue.c: Unit tests for GstValue
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 "../gstcheck.h"
27 GST_START_TEST (test_deserialize_buffer)
31 g_value_init (&value, GST_TYPE_BUFFER);
32 fail_unless (gst_value_deserialize (&value, "1234567890abcdef"));
37 GST_START_TEST (test_deserialize_gint64)
40 const char *strings[] = {
50 g_value_init (&value, G_TYPE_INT64);
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);
63 GST_START_TEST (test_deserialize_gint)
66 const char *strings[] = {
71 /* a positive long long, serializing to highest possible positive sint */
73 /* a positive long long, serializing to lowest possible negative sint */
75 /* a negative long long, serializing to lowest possible negative sint */
78 /* a positive long long serializing to -1 */
81 /* a negative long long serializing to -1 */
98 /* cast needs to be explicit because of unsigned -> signed */
99 (gint) 0xFFFFFFFFFFFFFFFFLL,
104 g_value_init (&value, G_TYPE_INT);
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);
117 GST_START_TEST (test_deserialize_gint_failures)
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",
133 g_value_init (&value, G_TYPE_INT);
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);
143 GST_START_TEST (test_deserialize_guint)
145 GValue value = { 0 };
146 const char *strings[] = {
151 /* a positive long long, serializing to highest possible positive sint */
153 /* a positive long long, serializing to lowest possible negative sint */
156 /* a negative long long, serializing to lowest possible negative sint */
157 "0xFFFFFFFF80000000",
158 /* a value typically used for rgb masks */
160 /* a positive long long serializing to highest possible positive uint */
163 /* a negative long long serializing to highest possible positive uint */
164 "0xFFFFFFFFFFFFFFFF",
174 (guint) 2147483648LL,
179 (guint) 0xFFFFFFFFFFFFFFFFLL,
184 g_value_init (&value, G_TYPE_UINT);
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);
197 GST_START_TEST (test_deserialize_guint_failures)
199 GValue value = { 0 };
200 const char *strings[] = {
201 "-", /* not a complete number */
202 "- TEST", /* not a complete number */
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",
217 g_value_init (&value, G_TYPE_UINT);
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);
228 GST_START_TEST (test_string)
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);
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));
257 GST_START_TEST (test_deserialize_string)
269 "\"", NULL}, /* missing second quote */
271 "\"Hello\\ World", NULL}, /* missing second quote */
273 "\"\\", NULL}, /* quote at end, missing second quote */
275 "\"\\0", NULL}, /* missing second quote */
277 "\"\\0\"", NULL}, /* unfinished escaped character */
279 "\" \"", NULL}, /* spaces must be escaped */
281 /* FIXME 0.9: this test should fail, but it doesn't */
283 "tüüt", NULL} /* string with special chars must be escaped */
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));
298 fail_if (tests[i].to != NULL, "failed, but wanted: %s", tests[i].to);
308 gst_value_suite (void)
310 Suite *s = suite_create ("GstValue");
311 TCase *tc_chain = tcase_create ("general");
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);
326 main (int argc, char **argv)
330 Suite *s = gst_value_suite ();
331 SRunner *sr = srunner_create (s);
333 gst_check_init (&argc, &argv);
335 srunner_run_all (sr, CK_NORMAL);
336 nf = srunner_ntests_failed (sr);