2 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
5 * gstutils.c: Unit test for functions in gstutils
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.
23 #include <gst/check/gstcheck.h>
25 #define SPECIAL_POINTER(x) ((void*)(19283847+(x)))
27 static int n_data_probes = 0;
28 static int n_buffer_probes = 0;
29 static int n_event_probes = 0;
32 data_probe (GstPad * pad, GstMiniObject * obj, gpointer data)
35 GST_DEBUG ("data probe %d", n_data_probes);
36 g_assert (GST_IS_MINI_OBJECT (obj));
37 g_assert (data == SPECIAL_POINTER (0));
42 buffer_probe (GstPad * pad, GstBuffer * obj, gpointer data)
45 GST_DEBUG ("buffer probe %d", n_buffer_probes);
46 g_assert (GST_IS_BUFFER (obj));
47 g_assert (data == SPECIAL_POINTER (1));
52 event_probe (GstPad * pad, GstEvent * obj, gpointer data)
55 GST_DEBUG ("event probe %d", n_event_probes);
56 g_assert (GST_IS_EVENT (obj));
57 g_assert (data == SPECIAL_POINTER (2));
61 GST_START_TEST (test_buffer_probe_n_times)
63 GstElement *pipeline, *fakesrc, *fakesink;
68 pipeline = gst_element_factory_make ("pipeline", NULL);
69 fakesrc = gst_element_factory_make ("fakesrc", NULL);
70 fakesink = gst_element_factory_make ("fakesink", NULL);
72 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
74 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
75 gst_element_link (fakesrc, fakesink);
77 pad = gst_element_get_pad (fakesink, "sink");
78 gst_pad_add_data_probe (pad, G_CALLBACK (data_probe), SPECIAL_POINTER (0));
79 gst_pad_add_buffer_probe (pad, G_CALLBACK (buffer_probe),
81 gst_pad_add_event_probe (pad, G_CALLBACK (event_probe), SPECIAL_POINTER (2));
82 gst_object_unref (pad);
84 gst_element_set_state (pipeline, GST_STATE_PLAYING);
86 bus = gst_element_get_bus (pipeline);
87 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
88 gst_message_unref (message);
89 gst_object_unref (bus);
91 g_assert (n_buffer_probes == 10); /* one for every buffer */
92 g_assert (n_event_probes == 2); /* new segment and eos */
93 g_assert (n_data_probes == 12); /* duh */
95 gst_element_set_state (pipeline, GST_STATE_NULL);
96 gst_object_unref (pipeline);
98 /* make sure nothing was sent in addition to the above when shutting down */
99 g_assert (n_buffer_probes == 10); /* one for every buffer */
100 g_assert (n_event_probes == 2); /* new segment and eos */
101 g_assert (n_data_probes == 12); /* duh */
104 static int n_data_probes_once = 0;
105 static int n_buffer_probes_once = 0;
106 static int n_event_probes_once = 0;
109 data_probe_once (GstPad * pad, GstMiniObject * obj, guint * data)
111 n_data_probes_once++;
112 g_assert (GST_IS_MINI_OBJECT (obj));
114 gst_pad_remove_data_probe (pad, *data);
120 buffer_probe_once (GstPad * pad, GstBuffer * obj, guint * data)
122 n_buffer_probes_once++;
123 g_assert (GST_IS_BUFFER (obj));
125 gst_pad_remove_buffer_probe (pad, *data);
131 event_probe_once (GstPad * pad, GstEvent * obj, guint * data)
133 n_event_probes_once++;
134 g_assert (GST_IS_EVENT (obj));
136 gst_pad_remove_event_probe (pad, *data);
141 GST_START_TEST (test_buffer_probe_once)
143 GstElement *pipeline, *fakesrc, *fakesink;
149 pipeline = gst_element_factory_make ("pipeline", NULL);
150 fakesrc = gst_element_factory_make ("fakesrc", NULL);
151 fakesink = gst_element_factory_make ("fakesink", NULL);
153 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
155 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
156 gst_element_link (fakesrc, fakesink);
158 pad = gst_element_get_pad (fakesink, "sink");
159 id1 = gst_pad_add_data_probe (pad, G_CALLBACK (data_probe_once), &id1);
160 id2 = gst_pad_add_buffer_probe (pad, G_CALLBACK (buffer_probe_once), &id2);
161 id3 = gst_pad_add_event_probe (pad, G_CALLBACK (event_probe_once), &id3);
162 gst_object_unref (pad);
164 gst_element_set_state (pipeline, GST_STATE_PLAYING);
166 bus = gst_element_get_bus (pipeline);
167 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
168 gst_message_unref (message);
169 gst_object_unref (bus);
171 gst_element_set_state (pipeline, GST_STATE_NULL);
172 gst_object_unref (pipeline);
174 g_assert (n_buffer_probes_once == 1); /* can we hit it and quit? */
175 g_assert (n_event_probes_once == 1); /* i said, can we hit it and quit? */
176 g_assert (n_data_probes_once == 1); /* let's hit it and quit!!! */
179 GST_START_TEST (test_math_scale)
181 fail_if (gst_util_uint64_scale_int (1, 1, 1) != 1);
183 fail_if (gst_util_uint64_scale_int (10, 10, 1) != 100);
184 fail_if (gst_util_uint64_scale_int (10, 10, 2) != 50);
186 fail_if (gst_util_uint64_scale_int (0, 10, 2) != 0);
187 fail_if (gst_util_uint64_scale_int (0, 0, 2) != 0);
189 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 5, 1) != G_MAXUINT32 * 5LL);
190 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 10, 2) != G_MAXUINT32 * 5LL);
192 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 1, 5) != G_MAXUINT32 / 5LL);
193 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 2, 10) != G_MAXUINT32 / 5LL);
195 /* not quite overflow */
196 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, 10,
197 10) != G_MAXUINT64 - 1);
198 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, G_MAXINT32,
199 G_MAXINT32) != G_MAXUINT64 - 1);
200 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 100, G_MAXINT32,
201 G_MAXINT32) != G_MAXUINT64 - 100);
204 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, 10, 1) != G_MAXUINT64);
205 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, G_MAXINT32,
210 GST_START_TEST (test_math_scale_uint64)
212 fail_if (gst_util_uint64_scale (1, 1, 1) != 1);
214 fail_if (gst_util_uint64_scale (10, 10, 1) != 100);
215 fail_if (gst_util_uint64_scale (10, 10, 2) != 50);
217 fail_if (gst_util_uint64_scale (0, 10, 2) != 0);
218 fail_if (gst_util_uint64_scale (0, 0, 2) != 0);
220 fail_if (gst_util_uint64_scale (G_MAXUINT32, 5, 1) != G_MAXUINT32 * 5LL);
221 fail_if (gst_util_uint64_scale (G_MAXUINT32, 10, 2) != G_MAXUINT32 * 5LL);
223 fail_if (gst_util_uint64_scale (G_MAXUINT32, 1, 5) != G_MAXUINT32 / 5LL);
224 fail_if (gst_util_uint64_scale (G_MAXUINT32, 2, 10) != G_MAXUINT32 / 5LL);
226 /* not quite overflow */
227 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 10) != G_MAXUINT64 - 1);
228 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT32,
229 G_MAXUINT32) != G_MAXUINT64 - 1);
230 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 100, G_MAXUINT32,
231 G_MAXUINT32) != G_MAXUINT64 - 100);
233 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 10) != G_MAXUINT64 - 1);
234 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT64,
235 G_MAXUINT64) != G_MAXUINT64 - 1);
236 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 100, G_MAXUINT64,
237 G_MAXUINT64) != G_MAXUINT64 - 100);
240 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 1) != G_MAXUINT64);
241 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT64,
246 GST_START_TEST (test_math_scale_random)
248 guint64 val, num, denom, res;
252 rand = g_rand_new ();
258 val = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
259 num = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
260 denom = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
262 res = gst_util_uint64_scale (val, num, denom);
263 check = gst_gdouble_to_guint64 (gst_guint64_to_gdouble (val) *
264 gst_guint64_to_gdouble (num) / gst_guint64_to_gdouble (denom));
266 if (res < G_MAXUINT64 && check < G_MAXUINT64) {
272 /* some arbitrary value, really.. someone do the proper math to get
275 fail_if (diff > 20000);
284 GST_START_TEST (test_guint64_to_gdouble)
286 guint64 from[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
287 ((guint64) (1) << 63) + 1,
288 ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
290 gdouble to[] = { 0., 1., 100., 10000., 9223372036854775808.,
291 9223372036854775809., 13835058055282163712.
293 gdouble tolerance[] = { 0., 0., 0., 0., 0., 1., 1. };
298 for (i = 0; i < G_N_ELEMENTS (from); ++i) {
299 result = gst_util_guint64_to_gdouble (from[i]);
300 delta = ABS (to[i] - result);
301 fail_unless (delta <= tolerance[i],
302 "Could not convert %d: %" G_GUINT64_FORMAT
303 " -> %f, got %f instead, delta of %e with tolerance of %e",
304 i, from[i], to[i], result, delta, tolerance[i]);
310 GST_START_TEST (test_gdouble_to_guint64)
312 gdouble from[] = { 0., 1., 100., 10000., 9223372036854775808.,
313 9223372036854775809., 13835058055282163712.
315 guint64 to[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
316 ((guint64) (1) << 63) + 1,
317 ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
319 guint64 tolerance[] = { 0, 0, 0, 0, 0, 1, 1 };
324 for (i = 0; i < G_N_ELEMENTS (from); ++i) {
325 result = gst_util_gdouble_to_guint64 (from[i]);
326 delta = ABS (to[i] - result);
327 fail_unless (delta <= tolerance[i],
328 "Could not convert %f: %" G_GUINT64_FORMAT
329 " -> %d, got %d instead, delta of %e with tolerance of %e",
330 i, from[i], to[i], result, delta, tolerance[i]);
336 #ifndef GST_DISABLE_PARSE
337 GST_START_TEST (test_parse_bin_from_description)
341 const gchar *bin_desc;
342 const gchar *pad_names;
345 "identity", "identity0/sink,identity0/src"}, {
346 "identity ! identity ! identity", "identity1/sink,identity3/src"}, {
347 "identity ! fakesink", "identity4/sink"}, {
348 "fakesrc ! identity", "identity5/src"}, {
349 "fakesrc ! fakesink", ""}
353 for (i = 0; i < G_N_ELEMENTS (bin_tests); ++i) {
354 GstElement *bin, *parent;
356 GstPad *ghost_pad, *target_pad;
359 bin = gst_parse_bin_from_description (bin_tests[i].bin_desc, TRUE, &err);
361 g_error ("ERROR in gst_parse_bin_from_description (%s): %s",
362 bin_tests[i].bin_desc, err->message);
364 g_assert (bin != NULL);
366 s = g_string_new ("");
367 if ((ghost_pad = gst_element_get_pad (bin, "sink"))) {
368 g_assert (GST_IS_GHOST_PAD (ghost_pad));
370 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ghost_pad));
371 g_assert (target_pad != NULL);
372 g_assert (GST_IS_PAD (target_pad));
374 parent = gst_pad_get_parent_element (target_pad);
375 g_assert (parent != NULL);
377 g_string_append_printf (s, "%s/sink", GST_ELEMENT_NAME (parent));
379 gst_object_unref (parent);
380 gst_object_unref (target_pad);
381 gst_object_unref (ghost_pad);
384 if ((ghost_pad = gst_element_get_pad (bin, "src"))) {
385 g_assert (GST_IS_GHOST_PAD (ghost_pad));
387 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ghost_pad));
388 g_assert (target_pad != NULL);
389 g_assert (GST_IS_PAD (target_pad));
391 parent = gst_pad_get_parent_element (target_pad);
392 g_assert (parent != NULL);
395 g_string_append (s, ",");
398 g_string_append_printf (s, "%s/src", GST_ELEMENT_NAME (parent));
400 gst_object_unref (parent);
401 gst_object_unref (target_pad);
402 gst_object_unref (ghost_pad);
405 if (strcmp (s->str, bin_tests[i].pad_names) != 0) {
406 g_error ("FAILED: expted '%s', got '%s' for bin '%s'",
407 bin_tests[i].pad_names, s->str, bin_tests[i].bin_desc);
409 g_string_free (s, TRUE);
411 gst_object_unref (bin);
418 GST_START_TEST (test_element_found_tags)
420 GstElement *pipeline, *fakesrc, *fakesink;
425 pipeline = gst_element_factory_make ("pipeline", NULL);
426 fakesrc = gst_element_factory_make ("fakesrc", NULL);
427 fakesink = gst_element_factory_make ("fakesink", NULL);
428 list = gst_tag_list_new ();
430 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
432 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
433 gst_element_link (fakesrc, fakesink);
435 gst_element_set_state (pipeline, GST_STATE_PLAYING);
437 gst_element_found_tags (GST_ELEMENT (fakesrc), list);
439 bus = gst_element_get_bus (pipeline);
440 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
441 gst_message_unref (message);
442 gst_object_unref (bus);
444 /* FIXME: maybe also check if the fakesink receives the message */
446 gst_element_set_state (pipeline, GST_STATE_NULL);
447 gst_object_unref (pipeline);
452 GST_START_TEST (test_element_unlink)
454 GstElement *src, *sink;
456 src = gst_element_factory_make ("fakesrc", NULL);
457 sink = gst_element_factory_make ("fakesink", NULL);
458 fail_unless (gst_element_link (src, sink) != FALSE);
459 gst_element_unlink (src, sink);
460 gst_object_unref (src);
461 gst_object_unref (sink);
466 GST_START_TEST (test_set_value_from_string)
470 /* g_return_if_fail */
471 ASSERT_CRITICAL (gst_util_set_value_from_string (NULL, "xyz"));
473 g_value_init (&val, G_TYPE_STRING);
474 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, NULL));
475 g_value_unset (&val);
477 /* string => string */
478 g_value_init (&val, G_TYPE_STRING);
479 gst_util_set_value_from_string (&val, "Y00");
480 fail_unless (g_value_get_string (&val) != NULL);
481 fail_unless_equals_string (g_value_get_string (&val), "Y00");
482 g_value_unset (&val);
485 g_value_init (&val, G_TYPE_INT);
486 gst_util_set_value_from_string (&val, "987654321");
487 fail_unless (g_value_get_int (&val) == 987654321);
488 g_value_unset (&val);
490 g_value_init (&val, G_TYPE_INT);
491 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
492 g_value_unset (&val);
495 g_value_init (&val, G_TYPE_UINT);
496 gst_util_set_value_from_string (&val, "987654321");
497 fail_unless (g_value_get_uint (&val) == 987654321);
498 g_value_unset (&val);
500 /* CHECKME: is this really desired behaviour? (tpm) */
501 g_value_init (&val, G_TYPE_UINT);
502 gst_util_set_value_from_string (&val, "-999");
503 fail_unless (g_value_get_uint (&val) == ((guint) 0 - (guint) 999));
504 g_value_unset (&val);
506 g_value_init (&val, G_TYPE_UINT);
507 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
508 g_value_unset (&val);
511 g_value_init (&val, G_TYPE_LONG);
512 gst_util_set_value_from_string (&val, "987654321");
513 fail_unless (g_value_get_long (&val) == 987654321);
514 g_value_unset (&val);
516 g_value_init (&val, G_TYPE_LONG);
517 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
518 g_value_unset (&val);
520 /* string => ulong */
521 g_value_init (&val, G_TYPE_ULONG);
522 gst_util_set_value_from_string (&val, "987654321");
523 fail_unless (g_value_get_ulong (&val) == 987654321);
524 g_value_unset (&val);
526 /* CHECKME: is this really desired behaviour? (tpm) */
527 g_value_init (&val, G_TYPE_ULONG);
528 gst_util_set_value_from_string (&val, "-999");
529 fail_unless (g_value_get_ulong (&val) == ((gulong) 0 - (gulong) 999));
530 g_value_unset (&val);
532 g_value_init (&val, G_TYPE_ULONG);
533 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
534 g_value_unset (&val);
536 /* string => boolean */
537 g_value_init (&val, G_TYPE_BOOLEAN);
538 gst_util_set_value_from_string (&val, "true");
539 fail_unless_equals_int (g_value_get_boolean (&val), TRUE);
540 g_value_unset (&val);
542 g_value_init (&val, G_TYPE_BOOLEAN);
543 gst_util_set_value_from_string (&val, "TRUE");
544 fail_unless_equals_int (g_value_get_boolean (&val), TRUE);
545 g_value_unset (&val);
547 g_value_init (&val, G_TYPE_BOOLEAN);
548 gst_util_set_value_from_string (&val, "false");
549 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
550 g_value_unset (&val);
552 g_value_init (&val, G_TYPE_BOOLEAN);
553 gst_util_set_value_from_string (&val, "FALSE");
554 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
555 g_value_unset (&val);
557 g_value_init (&val, G_TYPE_BOOLEAN);
558 gst_util_set_value_from_string (&val, "bleh");
559 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
560 g_value_unset (&val);
563 /* string => float (yay, localisation issues involved) */
564 g_value_init (&val, G_TYPE_FLOAT);
565 gst_util_set_value_from_string (&val, "987.654");
566 fail_unless (g_value_get_float (&val) >= 987.653 &&
567 g_value_get_float (&val) <= 987.655);
568 g_value_unset (&val);
570 g_value_init (&val, G_TYPE_FLOAT);
571 gst_util_set_value_from_string (&val, "987,654");
572 fail_unless (g_value_get_float (&val) >= 987.653 &&
573 g_value_get_float (&val) <= 987.655);
574 g_value_unset (&val);
576 /* string => double (yay, localisation issues involved) */
577 g_value_init (&val, G_TYPE_DOUBLE);
578 gst_util_set_value_from_string (&val, "987.654");
579 fail_unless (g_value_get_double (&val) >= 987.653 &&
580 g_value_get_double (&val) <= 987.655);
581 g_value_unset (&val);
583 g_value_init (&val, G_TYPE_DOUBLE);
584 gst_util_set_value_from_string (&val, "987,654");
585 fail_unless (g_value_get_double (&val) >= 987.653 &&
586 g_value_get_double (&val) <= 987.655);
587 g_value_unset (&val);
594 gst_utils_suite (void)
596 Suite *s = suite_create ("GstUtils");
597 TCase *tc_chain = tcase_create ("general");
599 suite_add_tcase (s, tc_chain);
600 tcase_add_test (tc_chain, test_buffer_probe_n_times);
601 tcase_add_test (tc_chain, test_buffer_probe_once);
602 tcase_add_test (tc_chain, test_math_scale);
603 tcase_add_test (tc_chain, test_math_scale_uint64);
604 tcase_add_test (tc_chain, test_math_scale_random);
605 tcase_add_test (tc_chain, test_guint64_to_gdouble);
606 tcase_add_test (tc_chain, test_gdouble_to_guint64);
607 #ifndef GST_DISABLE_PARSE
608 tcase_add_test (tc_chain, test_parse_bin_from_description);
610 tcase_add_test (tc_chain, test_element_found_tags);
611 tcase_add_test (tc_chain, test_element_unlink);
612 tcase_add_test (tc_chain, test_set_value_from_string);
616 GST_CHECK_MAIN (gst_utils);