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.
27 #include <gst/check/gstcheck.h>
29 #define SPECIAL_POINTER(x) ((void*)(19283847+(x)))
31 static int n_data_probes = 0;
32 static int n_buffer_probes = 0;
33 static int n_event_probes = 0;
35 static GstPadProbeReturn
36 probe_do_nothing (GstPad * pad, GstPadProbeType type, gpointer type_data,
39 GstMiniObject *obj = type_data;
40 GST_DEBUG_OBJECT (pad, "is buffer:%d", GST_IS_BUFFER (obj));
41 return GST_PAD_PROBE_OK;
44 static GstPadProbeReturn
45 data_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
48 GstMiniObject *obj = type_data;
50 GST_DEBUG_OBJECT (pad, "data probe %d", n_data_probes);
51 g_assert (GST_IS_BUFFER (obj) || GST_IS_EVENT (obj));
52 g_assert (data == SPECIAL_POINTER (0));
53 return GST_PAD_PROBE_OK;
56 static GstPadProbeReturn
57 buffer_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
60 GstBuffer *obj = type_data;
62 GST_DEBUG_OBJECT (pad, "buffer probe %d", n_buffer_probes);
63 g_assert (GST_IS_BUFFER (obj));
64 g_assert (data == SPECIAL_POINTER (1));
65 return GST_PAD_PROBE_OK;
68 static GstPadProbeReturn
69 event_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
72 GstEvent *obj = type_data;
74 GST_DEBUG_OBJECT (pad, "event probe %d [%s]",
75 n_event_probes, GST_EVENT_TYPE_NAME (obj));
76 g_assert (GST_IS_EVENT (obj));
77 g_assert (data == SPECIAL_POINTER (2));
78 return GST_PAD_PROBE_OK;
81 GST_START_TEST (test_buffer_probe_n_times)
83 GstElement *pipeline, *fakesrc, *fakesink;
88 pipeline = gst_element_factory_make ("pipeline", NULL);
89 fakesrc = gst_element_factory_make ("fakesrc", NULL);
90 fakesink = gst_element_factory_make ("fakesink", NULL);
92 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
94 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
95 gst_element_link (fakesrc, fakesink);
97 pad = gst_element_get_static_pad (fakesink, "sink");
99 /* add the probes we need for the test */
100 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA, data_probe,
101 SPECIAL_POINTER (0), NULL);
102 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe,
103 SPECIAL_POINTER (1), NULL);
104 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT, event_probe,
105 SPECIAL_POINTER (2), NULL);
107 /* add some string probes just to test that the data is free'd
108 * properly as it should be */
109 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA, probe_do_nothing,
110 g_strdup ("data probe string"), (GDestroyNotify) g_free);
111 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, probe_do_nothing,
112 g_strdup ("buffer probe string"), (GDestroyNotify) g_free);
113 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT, probe_do_nothing,
114 g_strdup ("event probe string"), (GDestroyNotify) g_free);
116 gst_object_unref (pad);
118 gst_element_set_state (pipeline, GST_STATE_PLAYING);
120 bus = gst_element_get_bus (pipeline);
121 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
122 gst_message_unref (message);
123 gst_object_unref (bus);
125 g_assert (n_buffer_probes == 10); /* one for every buffer */
126 g_assert (n_event_probes == 3); /* new segment, latency and eos */
127 g_assert (n_data_probes == 13); /* duh */
129 gst_element_set_state (pipeline, GST_STATE_NULL);
130 gst_object_unref (pipeline);
132 /* make sure nothing was sent in addition to the above when shutting down */
133 g_assert (n_buffer_probes == 10); /* one for every buffer */
134 g_assert (n_event_probes == 3); /* new segment, latency and eos */
135 g_assert (n_data_probes == 13); /* duh */
138 static int n_data_probes_once = 0;
139 static int n_buffer_probes_once = 0;
140 static int n_event_probes_once = 0;
142 static GstPadProbeReturn
143 data_probe_once (GstPad * pad, GstPadProbeType type, GstMiniObject * obj,
146 n_data_probes_once++;
147 g_assert (GST_IS_BUFFER (obj) || GST_IS_EVENT (obj));
149 gst_pad_remove_probe (pad, *data);
151 return GST_PAD_PROBE_OK;
154 static GstPadProbeReturn
155 buffer_probe_once (GstPad * pad, GstPadProbeType type, GstBuffer * obj,
158 n_buffer_probes_once++;
159 g_assert (GST_IS_BUFFER (obj));
161 gst_pad_remove_probe (pad, *data);
163 return GST_PAD_PROBE_OK;
166 static GstPadProbeReturn
167 event_probe_once (GstPad * pad, GstPadProbeType type, GstEvent * obj,
170 n_event_probes_once++;
171 g_assert (GST_IS_EVENT (obj));
173 gst_pad_remove_probe (pad, *data);
175 return GST_PAD_PROBE_OK;
178 GST_START_TEST (test_buffer_probe_once)
180 GstElement *pipeline, *fakesrc, *fakesink;
186 pipeline = gst_element_factory_make ("pipeline", NULL);
187 fakesrc = gst_element_factory_make ("fakesrc", NULL);
188 fakesink = gst_element_factory_make ("fakesink", NULL);
190 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
192 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
193 gst_element_link (fakesrc, fakesink);
195 pad = gst_element_get_static_pad (fakesink, "sink");
197 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA,
198 (GstPadProbeCallback) data_probe_once, &id1, NULL);
200 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
201 (GstPadProbeCallback) buffer_probe_once, &id2, NULL);
203 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT,
204 (GstPadProbeCallback) event_probe_once, &id3, NULL);
205 gst_object_unref (pad);
207 gst_element_set_state (pipeline, GST_STATE_PLAYING);
209 bus = gst_element_get_bus (pipeline);
210 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
211 gst_message_unref (message);
212 gst_object_unref (bus);
214 gst_element_set_state (pipeline, GST_STATE_NULL);
215 gst_object_unref (pipeline);
217 g_assert (n_buffer_probes_once == 1); /* can we hit it and quit? */
218 g_assert (n_event_probes_once == 1); /* i said, can we hit it and quit? */
219 g_assert (n_data_probes_once == 1); /* let's hit it and quit!!! */
222 GST_START_TEST (test_math_scale)
224 fail_if (gst_util_uint64_scale_int (1, 1, 1) != 1);
226 fail_if (gst_util_uint64_scale_int (10, 10, 1) != 100);
227 fail_if (gst_util_uint64_scale_int (10, 10, 2) != 50);
229 fail_if (gst_util_uint64_scale_int (0, 10, 2) != 0);
230 fail_if (gst_util_uint64_scale_int (0, 0, 2) != 0);
232 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 5, 1) != G_MAXUINT32 * 5LL);
233 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 10, 2) != G_MAXUINT32 * 5LL);
235 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 1, 5) != G_MAXUINT32 / 5LL);
236 fail_if (gst_util_uint64_scale_int (G_MAXUINT32, 2, 10) != G_MAXUINT32 / 5LL);
238 /* not quite overflow */
239 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, 10,
240 10) != G_MAXUINT64 - 1);
241 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, G_MAXINT32,
242 G_MAXINT32) != G_MAXUINT64 - 1);
243 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 100, G_MAXINT32,
244 G_MAXINT32) != G_MAXUINT64 - 100);
247 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, 10, 1) != G_MAXUINT64);
248 fail_if (gst_util_uint64_scale_int (G_MAXUINT64 - 1, G_MAXINT32,
253 GST_START_TEST (test_math_scale_round)
255 fail_if (gst_util_uint64_scale_int_round (2, 1, 2) != 1);
256 fail_if (gst_util_uint64_scale_int_round (3, 1, 2) != 2);
257 fail_if (gst_util_uint64_scale_int_round (4, 1, 2) != 2);
259 fail_if (gst_util_uint64_scale_int_round (200, 100, 20000) != 1);
260 fail_if (gst_util_uint64_scale_int_round (299, 100, 20000) != 1);
261 fail_if (gst_util_uint64_scale_int_round (300, 100, 20000) != 2);
262 fail_if (gst_util_uint64_scale_int_round (301, 100, 20000) != 2);
263 fail_if (gst_util_uint64_scale_int_round (400, 100, 20000) != 2);
266 GST_START_TEST (test_math_scale_ceil)
268 fail_if (gst_util_uint64_scale_int_ceil (2, 1, 2) != 1);
269 fail_if (gst_util_uint64_scale_int_ceil (3, 1, 2) != 2);
270 fail_if (gst_util_uint64_scale_int_ceil (4, 1, 2) != 2);
272 fail_if (gst_util_uint64_scale_int_ceil (200, 100, 20000) != 1);
273 fail_if (gst_util_uint64_scale_int_ceil (299, 100, 20000) != 2);
274 fail_if (gst_util_uint64_scale_int_ceil (300, 100, 20000) != 2);
275 fail_if (gst_util_uint64_scale_int_ceil (301, 100, 20000) != 2);
276 fail_if (gst_util_uint64_scale_int_ceil (400, 100, 20000) != 2);
279 GST_START_TEST (test_math_scale_uint64)
281 fail_if (gst_util_uint64_scale (1, 1, 1) != 1);
283 fail_if (gst_util_uint64_scale (10, 10, 1) != 100);
284 fail_if (gst_util_uint64_scale (10, 10, 2) != 50);
286 fail_if (gst_util_uint64_scale (0, 10, 2) != 0);
287 fail_if (gst_util_uint64_scale (0, 0, 2) != 0);
289 fail_if (gst_util_uint64_scale (G_MAXUINT32, 5, 1) != G_MAXUINT32 * 5LL);
290 fail_if (gst_util_uint64_scale (G_MAXUINT32, 10, 2) != G_MAXUINT32 * 5LL);
292 fail_if (gst_util_uint64_scale (G_MAXUINT32, 1, 5) != G_MAXUINT32 / 5LL);
293 fail_if (gst_util_uint64_scale (G_MAXUINT32, 2, 10) != G_MAXUINT32 / 5LL);
295 /* not quite overflow */
296 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 10) != G_MAXUINT64 - 1);
297 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT32,
298 G_MAXUINT32) != G_MAXUINT64 - 1);
299 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 100, G_MAXUINT32,
300 G_MAXUINT32) != G_MAXUINT64 - 100);
302 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 10) != G_MAXUINT64 - 1);
303 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT64,
304 G_MAXUINT64) != G_MAXUINT64 - 1);
305 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 100, G_MAXUINT64,
306 G_MAXUINT64) != G_MAXUINT64 - 100);
309 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, 10, 1) != G_MAXUINT64);
310 fail_if (gst_util_uint64_scale (G_MAXUINT64 - 1, G_MAXUINT64,
315 GST_START_TEST (test_math_scale_random)
317 guint64 val, num, denom, res;
321 rand = g_rand_new ();
327 val = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
328 num = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
329 denom = ((guint64) g_rand_int (rand)) << 32 | g_rand_int (rand);
331 res = gst_util_uint64_scale (val, num, denom);
332 check = gst_gdouble_to_guint64 (gst_guint64_to_gdouble (val) *
333 gst_guint64_to_gdouble (num) / gst_guint64_to_gdouble (denom));
335 if (res < G_MAXUINT64 && check < G_MAXUINT64) {
341 /* some arbitrary value, really.. someone do the proper math to get
344 fail_if (diff > 20000);
353 GST_START_TEST (test_guint64_to_gdouble)
355 guint64 from[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
356 ((guint64) (1) << 63) + 1,
357 ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
359 gdouble to[] = { 0., 1., 100., 10000., 9223372036854775808.,
360 9223372036854775809., 13835058055282163712.
362 gdouble tolerance[] = { 0., 0., 0., 0., 0., 1., 1. };
367 for (i = 0; i < G_N_ELEMENTS (from); ++i) {
368 result = gst_util_guint64_to_gdouble (from[i]);
369 delta = ABS (to[i] - result);
370 fail_unless (delta <= tolerance[i],
371 "Could not convert %d: %" G_GUINT64_FORMAT
372 " -> %f, got %f instead, delta of %e with tolerance of %e",
373 i, from[i], to[i], result, delta, tolerance[i]);
379 GST_START_TEST (test_gdouble_to_guint64)
381 gdouble from[] = { 0., 1., 100., 10000., 9223372036854775808.,
382 9223372036854775809., 13835058055282163712.
384 guint64 to[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
385 ((guint64) (1) << 63) + 1,
386 ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
388 guint64 tolerance[] = { 0, 0, 0, 0, 0, 1, 1 };
393 for (i = 0; i < G_N_ELEMENTS (from); ++i) {
394 result = gst_util_gdouble_to_guint64 (from[i]);
395 delta = ABS (to[i] - result);
396 fail_unless (delta <= tolerance[i],
397 "Could not convert %f: %" G_GUINT64_FORMAT
398 " -> %d, got %d instead, delta of %e with tolerance of %e",
399 i, from[i], to[i], result, delta, tolerance[i]);
405 #ifndef GST_DISABLE_PARSE
406 GST_START_TEST (test_parse_bin_from_description)
410 const gchar *bin_desc;
411 const gchar *pad_names;
414 "identity", "identity0/sink,identity0/src"}, {
415 "identity ! identity ! identity", "identity1/sink,identity3/src"}, {
416 "identity ! fakesink", "identity4/sink"}, {
417 "fakesrc ! identity", "identity5/src"}, {
418 "fakesrc ! fakesink", ""}
422 for (i = 0; i < G_N_ELEMENTS (bin_tests); ++i) {
423 GstElement *bin, *parent;
425 GstPad *ghost_pad, *target_pad;
428 bin = gst_parse_bin_from_description (bin_tests[i].bin_desc, TRUE, &err);
430 g_error ("ERROR in gst_parse_bin_from_description (%s): %s",
431 bin_tests[i].bin_desc, err->message);
433 g_assert (bin != NULL);
435 s = g_string_new ("");
436 if ((ghost_pad = gst_element_get_static_pad (bin, "sink"))) {
437 g_assert (GST_IS_GHOST_PAD (ghost_pad));
439 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ghost_pad));
440 g_assert (target_pad != NULL);
441 g_assert (GST_IS_PAD (target_pad));
443 parent = gst_pad_get_parent_element (target_pad);
444 g_assert (parent != NULL);
446 g_string_append_printf (s, "%s/sink", GST_ELEMENT_NAME (parent));
448 gst_object_unref (parent);
449 gst_object_unref (target_pad);
450 gst_object_unref (ghost_pad);
453 if ((ghost_pad = gst_element_get_static_pad (bin, "src"))) {
454 g_assert (GST_IS_GHOST_PAD (ghost_pad));
456 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ghost_pad));
457 g_assert (target_pad != NULL);
458 g_assert (GST_IS_PAD (target_pad));
460 parent = gst_pad_get_parent_element (target_pad);
461 g_assert (parent != NULL);
464 g_string_append (s, ",");
467 g_string_append_printf (s, "%s/src", GST_ELEMENT_NAME (parent));
469 gst_object_unref (parent);
470 gst_object_unref (target_pad);
471 gst_object_unref (ghost_pad);
474 if (strcmp (s->str, bin_tests[i].pad_names) != 0) {
475 g_error ("FAILED: expected '%s', got '%s' for bin '%s'",
476 bin_tests[i].pad_names, s->str, bin_tests[i].bin_desc);
478 g_string_free (s, TRUE);
480 gst_object_unref (bin);
487 GST_START_TEST (test_element_found_tags)
489 GstElement *pipeline, *fakesrc, *fakesink;
495 pipeline = gst_element_factory_make ("pipeline", NULL);
496 fakesrc = gst_element_factory_make ("fakesrc", NULL);
497 fakesink = gst_element_factory_make ("fakesink", NULL);
498 list = gst_tag_list_new_empty ();
500 g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
502 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
503 gst_element_link (fakesrc, fakesink);
505 gst_element_set_state (pipeline, GST_STATE_PLAYING);
507 srcpad = gst_element_get_static_pad (fakesrc, "src");
508 gst_pad_push_event (srcpad, gst_event_new_tag (list));
509 gst_object_unref (srcpad);
511 bus = gst_element_get_bus (pipeline);
512 message = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
513 gst_message_unref (message);
514 gst_object_unref (bus);
516 /* FIXME: maybe also check if the fakesink receives the message */
518 gst_element_set_state (pipeline, GST_STATE_NULL);
519 gst_object_unref (pipeline);
524 GST_START_TEST (test_element_unlink)
526 GstElement *src, *sink;
528 src = gst_element_factory_make ("fakesrc", NULL);
529 sink = gst_element_factory_make ("fakesink", NULL);
530 fail_unless (gst_element_link (src, sink) != FALSE);
531 gst_element_unlink (src, sink);
532 gst_object_unref (src);
533 gst_object_unref (sink);
538 GST_START_TEST (test_set_value_from_string)
542 /* g_return_if_fail */
543 ASSERT_CRITICAL (gst_util_set_value_from_string (NULL, "xyz"));
545 g_value_init (&val, G_TYPE_STRING);
546 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, NULL));
547 g_value_unset (&val);
549 /* string => string */
550 g_value_init (&val, G_TYPE_STRING);
551 gst_util_set_value_from_string (&val, "Y00");
552 fail_unless (g_value_get_string (&val) != NULL);
553 fail_unless_equals_string (g_value_get_string (&val), "Y00");
554 g_value_unset (&val);
557 g_value_init (&val, G_TYPE_INT);
558 gst_util_set_value_from_string (&val, "987654321");
559 fail_unless (g_value_get_int (&val) == 987654321);
560 g_value_unset (&val);
562 g_value_init (&val, G_TYPE_INT);
563 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
564 g_value_unset (&val);
567 g_value_init (&val, G_TYPE_UINT);
568 gst_util_set_value_from_string (&val, "987654321");
569 fail_unless (g_value_get_uint (&val) == 987654321);
570 g_value_unset (&val);
572 /* CHECKME: is this really desired behaviour? (tpm) */
573 g_value_init (&val, G_TYPE_UINT);
574 gst_util_set_value_from_string (&val, "-999");
575 fail_unless (g_value_get_uint (&val) == ((guint) 0 - (guint) 999));
576 g_value_unset (&val);
578 g_value_init (&val, G_TYPE_UINT);
579 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
580 g_value_unset (&val);
583 g_value_init (&val, G_TYPE_LONG);
584 gst_util_set_value_from_string (&val, "987654321");
585 fail_unless (g_value_get_long (&val) == 987654321);
586 g_value_unset (&val);
588 g_value_init (&val, G_TYPE_LONG);
589 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
590 g_value_unset (&val);
592 /* string => ulong */
593 g_value_init (&val, G_TYPE_ULONG);
594 gst_util_set_value_from_string (&val, "987654321");
595 fail_unless (g_value_get_ulong (&val) == 987654321);
596 g_value_unset (&val);
598 /* CHECKME: is this really desired behaviour? (tpm) */
599 g_value_init (&val, G_TYPE_ULONG);
600 gst_util_set_value_from_string (&val, "-999");
601 fail_unless (g_value_get_ulong (&val) == ((gulong) 0 - (gulong) 999));
602 g_value_unset (&val);
604 g_value_init (&val, G_TYPE_ULONG);
605 ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
606 g_value_unset (&val);
608 /* string => boolean */
609 g_value_init (&val, G_TYPE_BOOLEAN);
610 gst_util_set_value_from_string (&val, "true");
611 fail_unless_equals_int (g_value_get_boolean (&val), TRUE);
612 g_value_unset (&val);
614 g_value_init (&val, G_TYPE_BOOLEAN);
615 gst_util_set_value_from_string (&val, "TRUE");
616 fail_unless_equals_int (g_value_get_boolean (&val), TRUE);
617 g_value_unset (&val);
619 g_value_init (&val, G_TYPE_BOOLEAN);
620 gst_util_set_value_from_string (&val, "false");
621 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
622 g_value_unset (&val);
624 g_value_init (&val, G_TYPE_BOOLEAN);
625 gst_util_set_value_from_string (&val, "FALSE");
626 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
627 g_value_unset (&val);
629 g_value_init (&val, G_TYPE_BOOLEAN);
630 gst_util_set_value_from_string (&val, "bleh");
631 fail_unless_equals_int (g_value_get_boolean (&val), FALSE);
632 g_value_unset (&val);
635 /* string => float (yay, localisation issues involved) */
636 g_value_init (&val, G_TYPE_FLOAT);
637 gst_util_set_value_from_string (&val, "987.654");
638 fail_unless (g_value_get_float (&val) >= 987.653 &&
639 g_value_get_float (&val) <= 987.655);
640 g_value_unset (&val);
642 g_value_init (&val, G_TYPE_FLOAT);
643 gst_util_set_value_from_string (&val, "987,654");
644 fail_unless (g_value_get_float (&val) >= 987.653 &&
645 g_value_get_float (&val) <= 987.655);
646 g_value_unset (&val);
648 /* string => double (yay, localisation issues involved) */
649 g_value_init (&val, G_TYPE_DOUBLE);
650 gst_util_set_value_from_string (&val, "987.654");
651 fail_unless (g_value_get_double (&val) >= 987.653 &&
652 g_value_get_double (&val) <= 987.655);
653 g_value_unset (&val);
655 g_value_init (&val, G_TYPE_DOUBLE);
656 gst_util_set_value_from_string (&val, "987,654");
657 fail_unless (g_value_get_double (&val) >= 987.653 &&
658 g_value_get_double (&val) <= 987.655);
659 g_value_unset (&val);
666 _binary_search_compare (guint32 * a, guint32 * b)
671 GST_START_TEST (test_binary_search)
675 guint32 search_element = 121 * 2;
678 for (i = 0; i < 257; i++)
679 data[i] = (i + 1) * 2;
682 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
683 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_EXACT,
684 &search_element, NULL);
685 fail_unless (match != NULL);
686 fail_unless_equals_int (match - data, 120);
689 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
690 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_BEFORE,
691 &search_element, NULL);
692 fail_unless (match != NULL);
693 fail_unless_equals_int (match - data, 120);
696 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
697 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_AFTER,
698 &search_element, NULL);
699 fail_unless (match != NULL);
700 fail_unless_equals_int (match - data, 120);
704 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
705 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_EXACT,
706 &search_element, NULL);
707 fail_unless (match == NULL);
710 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
711 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_AFTER,
712 &search_element, NULL);
713 fail_unless (match != NULL);
714 fail_unless_equals_int (match - data, 0);
717 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
718 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_BEFORE,
719 &search_element, NULL);
720 fail_unless (match == NULL);
722 search_element = 1000;
724 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
725 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_EXACT,
726 &search_element, NULL);
727 fail_unless (match == NULL);
730 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
731 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_AFTER,
732 &search_element, NULL);
733 fail_unless (match == NULL);
736 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
737 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_BEFORE,
738 &search_element, NULL);
739 fail_unless (match != NULL);
740 fail_unless_equals_int (match - data, 256);
742 search_element = 121 * 2 - 1;
744 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
745 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_EXACT,
746 &search_element, NULL);
747 fail_unless (match == NULL);
750 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
751 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_AFTER,
752 &search_element, NULL);
753 fail_unless (match != NULL);
754 fail_unless_equals_int (match - data, 120);
757 (guint32 *) gst_util_array_binary_search (data, 257, sizeof (guint32),
758 (GCompareDataFunc) _binary_search_compare, GST_SEARCH_MODE_BEFORE,
759 &search_element, NULL);
760 fail_unless (match != NULL);
761 fail_unless_equals_int (match - data, 119);
770 #include <gsl/gsl_rng.h>
774 randguint64 (gsl_rng * rng, guint64 n)
784 x.parts.a = gsl_rng_uniform_int (rng, 1 << 16);
785 x.parts.b = gsl_rng_uniform_int (rng, 1 << 16);
786 x.parts.c = gsl_rng_uniform_int (rng, 1 << 16);
787 x.parts.d = gsl_rng_uniform_int (rng, 1 << 16);
800 gmp_set_uint64 (mpz_t mp, guint64 x)
807 mpz_ui_pow_ui (two_32, 2, 32);
808 mpz_set_ui (mp, (unsigned long) ((x >> 32) & G_MAXUINT32));
809 mpz_mul (tmp, mp, two_32);
810 mpz_add_ui (mp, tmp, (unsigned long) (x & G_MAXUINT32));
816 gmp_get_uint64 (mpz_t mp)
818 mpz_t two_64, two_32, tmp;
825 mpz_ui_pow_ui (two_64, 2, 64);
826 mpz_ui_pow_ui (two_32, 2, 32);
827 if (mpz_cmp (tmp, two_64) >= 0)
831 mpz_tdiv_q (tmp, mp, two_32);
832 ret = mpz_get_ui (tmp);
834 ret |= mpz_get_ui (mp);
842 gmp_scale (guint64 x, guint64 a, guint64 b, enum round_t mode)
852 gmp_set_uint64 (mp1, x);
853 gmp_set_uint64 (mp3, a);
854 mpz_mul (mp2, mp1, mp3);
856 case ROUND_TONEAREST:
857 gmp_set_uint64 (mp1, b);
858 mpz_tdiv_q_ui (mp3, mp1, 2);
859 mpz_add (mp1, mp2, mp3);
863 gmp_set_uint64 (mp1, b);
864 mpz_sub_ui (mp3, mp1, 1);
865 mpz_add (mp1, mp2, mp3);
871 gmp_set_uint64 (mp3, b);
872 mpz_tdiv_q (mp1, mp2, mp3);
873 x = gmp_get_uint64 (mp1);
881 _gmp_test_scale (gsl_rng * rng)
883 guint64 bygst, bygmp;
884 guint64 a = randguint64 (rng, gsl_rng_uniform_int (rng,
885 2) ? G_MAXUINT64 : G_MAXUINT32);
886 guint64 b = randguint64 (rng, gsl_rng_uniform_int (rng, 2) ? G_MAXUINT64 - 1 : G_MAXUINT32 - 1) + 1; /* 0 not allowed */
887 guint64 val = randguint64 (rng, gmp_scale (G_MAXUINT64, b, a, ROUND_DOWN));
888 enum round_t mode = gsl_rng_uniform_int (rng, 3);
891 bygmp = gmp_scale (val, a, b, mode);
893 case ROUND_TONEAREST:
894 bygst = gst_util_uint64_scale_round (val, a, b);
895 func = "gst_util_uint64_scale_round";
898 bygst = gst_util_uint64_scale_ceil (val, a, b);
899 func = "gst_util_uint64_scale_ceil";
902 bygst = gst_util_uint64_scale (val, a, b);
903 func = "gst_util_uint64_scale";
906 g_assert_not_reached ();
909 fail_unless (bygst == bygmp,
910 "error: %s(): %" G_GUINT64_FORMAT " * %" G_GUINT64_FORMAT " / %"
911 G_GUINT64_FORMAT " = %" G_GUINT64_FORMAT ", correct = %" G_GUINT64_FORMAT
912 "\n", func, val, a, b, bygst, bygmp);
916 _gmp_test_scale_int (gsl_rng * rng)
918 guint64 bygst, bygmp;
919 gint32 a = randguint64 (rng, G_MAXINT32);
920 gint32 b = randguint64 (rng, G_MAXINT32 - 1) + 1; /* 0 not allowed */
921 guint64 val = randguint64 (rng, gmp_scale (G_MAXUINT64, b, a, ROUND_DOWN));
922 enum round_t mode = gsl_rng_uniform_int (rng, 3);
925 bygmp = gmp_scale (val, a, b, mode);
927 case ROUND_TONEAREST:
928 bygst = gst_util_uint64_scale_int_round (val, a, b);
929 func = "gst_util_uint64_scale_int_round";
932 bygst = gst_util_uint64_scale_int_ceil (val, a, b);
933 func = "gst_util_uint64_scale_int_ceil";
936 bygst = gst_util_uint64_scale_int (val, a, b);
937 func = "gst_util_uint64_scale_int";
940 g_assert_not_reached ();
943 fail_unless (bygst == bygmp,
944 "error: %s(): %" G_GUINT64_FORMAT " * %d / %d = %" G_GUINT64_FORMAT
945 ", correct = %" G_GUINT64_FORMAT "\n", func, val, a, b, bygst, bygmp);
948 #define GMP_TEST_RUNS 100000
950 GST_START_TEST (test_math_scale_gmp)
952 gsl_rng *rng = gsl_rng_alloc (gsl_rng_mt19937);
955 for (n = 0; n < GMP_TEST_RUNS; n++)
956 _gmp_test_scale (rng);
963 GST_START_TEST (test_math_scale_gmp_int)
965 gsl_rng *rng = gsl_rng_alloc (gsl_rng_mt19937);
968 for (n = 0; n < GMP_TEST_RUNS; n++)
969 _gmp_test_scale_int (rng);
979 GST_START_TEST (test_pad_proxy_getcaps_aggregation)
981 GstElement *tee, *sink1, *sink2;
983 GstPad *tee_src1, *tee_src2, *tee_sink, *sink1_sink, *sink2_sink;
985 tee = gst_element_factory_make ("tee", "tee");
987 sink1 = gst_element_factory_make ("fakesink", "sink1");
988 tee_src1 = gst_element_get_request_pad (tee, "src_%u");
989 sink1_sink = gst_element_get_static_pad (sink1, "sink");
990 fail_unless_equals_int (gst_pad_link (tee_src1, sink1_sink), GST_PAD_LINK_OK);
992 sink2 = gst_element_factory_make ("fakesink", "sink2");
993 tee_src2 = gst_element_get_request_pad (tee, "src_%u");
994 sink2_sink = gst_element_get_static_pad (sink2, "sink");
995 fail_unless_equals_int (gst_pad_link (tee_src2, sink2_sink), GST_PAD_LINK_OK);
997 tee_sink = gst_element_get_static_pad (tee, "sink");
999 gst_element_set_state (sink1, GST_STATE_PAUSED);
1000 gst_element_set_state (sink2, GST_STATE_PAUSED);
1001 gst_element_set_state (tee, GST_STATE_PAUSED);
1003 /* by default, ANY caps should intersect to ANY */
1004 caps = gst_pad_get_caps (tee_sink, NULL);
1005 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
1006 fail_unless (caps != NULL);
1007 fail_unless (gst_caps_is_any (caps));
1008 gst_caps_unref (caps);
1010 /* these don't intersect we should get empty caps */
1011 caps = gst_caps_new_empty_simple ("foo/bar");
1012 fail_unless (gst_pad_set_caps (sink1_sink, caps));
1013 gst_pad_use_fixed_caps (sink1_sink);
1014 gst_caps_unref (caps);
1016 caps = gst_caps_new_empty_simple ("bar/ter");
1017 fail_unless (gst_pad_set_caps (sink2_sink, caps));
1018 gst_pad_use_fixed_caps (sink2_sink);
1019 gst_caps_unref (caps);
1021 caps = gst_pad_get_caps (tee_sink, NULL);
1022 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
1023 fail_unless (caps != NULL);
1024 fail_unless (gst_caps_is_empty (caps));
1025 gst_caps_unref (caps);
1027 /* test intersection */
1028 caps = gst_caps_new_simple ("foo/bar", "barversion", G_TYPE_INT, 1, NULL);
1029 fail_unless (gst_pad_set_caps (sink2_sink, caps));
1030 gst_pad_use_fixed_caps (sink2_sink);
1031 gst_caps_unref (caps);
1033 caps = gst_pad_get_caps (tee_sink, NULL);
1034 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
1035 fail_unless (caps != NULL);
1036 fail_if (gst_caps_is_empty (caps));
1038 GstStructure *s = gst_caps_get_structure (caps, 0);
1040 fail_unless_equals_string (gst_structure_get_name (s), "foo/bar");
1041 fail_unless (gst_structure_has_field_typed (s, "barversion", G_TYPE_INT));
1043 gst_caps_unref (caps);
1045 gst_element_set_state (sink1, GST_STATE_NULL);
1046 gst_element_set_state (sink2, GST_STATE_NULL);
1047 gst_element_set_state (tee, GST_STATE_NULL);
1050 gst_element_release_request_pad (tee, tee_src1);
1051 gst_object_unref (tee_src1);
1052 gst_element_release_request_pad (tee, tee_src2);
1053 gst_object_unref (tee_src2);
1054 gst_object_unref (tee_sink);
1055 gst_object_unref (tee);
1056 gst_object_unref (sink1_sink);
1057 gst_object_unref (sink1);
1058 gst_object_unref (sink2_sink);
1059 gst_object_unref (sink2);
1064 GST_START_TEST (test_greatest_common_divisor)
1066 fail_if (gst_util_greatest_common_divisor (1, 1) != 1);
1067 fail_if (gst_util_greatest_common_divisor (2, 3) != 1);
1068 fail_if (gst_util_greatest_common_divisor (3, 5) != 1);
1069 fail_if (gst_util_greatest_common_divisor (-1, 1) != 1);
1070 fail_if (gst_util_greatest_common_divisor (-2, 3) != 1);
1071 fail_if (gst_util_greatest_common_divisor (-3, 5) != 1);
1072 fail_if (gst_util_greatest_common_divisor (-1, -1) != 1);
1073 fail_if (gst_util_greatest_common_divisor (-2, -3) != 1);
1074 fail_if (gst_util_greatest_common_divisor (-3, -5) != 1);
1075 fail_if (gst_util_greatest_common_divisor (1, -1) != 1);
1076 fail_if (gst_util_greatest_common_divisor (2, -3) != 1);
1077 fail_if (gst_util_greatest_common_divisor (3, -5) != 1);
1078 fail_if (gst_util_greatest_common_divisor (2, 2) != 2);
1079 fail_if (gst_util_greatest_common_divisor (2, 4) != 2);
1080 fail_if (gst_util_greatest_common_divisor (1001, 11) != 11);
1087 gst_utils_suite (void)
1089 Suite *s = suite_create ("GstUtils");
1090 TCase *tc_chain = tcase_create ("general");
1092 suite_add_tcase (s, tc_chain);
1093 tcase_add_test (tc_chain, test_buffer_probe_n_times);
1094 tcase_add_test (tc_chain, test_buffer_probe_once);
1095 tcase_add_test (tc_chain, test_math_scale);
1096 tcase_add_test (tc_chain, test_math_scale_round);
1097 tcase_add_test (tc_chain, test_math_scale_ceil);
1098 tcase_add_test (tc_chain, test_math_scale_uint64);
1099 tcase_add_test (tc_chain, test_math_scale_random);
1102 tcase_add_test (tc_chain, test_math_scale_gmp);
1103 tcase_add_test (tc_chain, test_math_scale_gmp_int);
1107 tcase_add_test (tc_chain, test_guint64_to_gdouble);
1108 tcase_add_test (tc_chain, test_gdouble_to_guint64);
1109 #ifndef GST_DISABLE_PARSE
1110 tcase_add_test (tc_chain, test_parse_bin_from_description);
1112 tcase_add_test (tc_chain, test_element_found_tags);
1113 tcase_add_test (tc_chain, test_element_unlink);
1114 tcase_add_test (tc_chain, test_set_value_from_string);
1115 tcase_add_test (tc_chain, test_binary_search);
1117 tcase_add_test (tc_chain, test_pad_proxy_getcaps_aggregation);
1118 tcase_add_test (tc_chain, test_greatest_common_divisor);
1122 GST_CHECK_MAIN (gst_utils);