fix request pad
[platform/upstream/gstreamer.git] / tests / check / gst / gstutils.c
1 /* GStreamer
2  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4  *
5  * gstutils.c: Unit test for functions in gstutils
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #define SPECIAL_POINTER(x) ((void*)(19283847+(x)))
30
31 static int n_data_probes = 0;
32 static int n_buffer_probes = 0;
33 static int n_event_probes = 0;
34
35 static GstPadProbeReturn
36 probe_do_nothing (GstPad * pad, GstPadProbeType type, gpointer type_data,
37     gpointer data)
38 {
39   GstMiniObject *obj = type_data;
40   GST_DEBUG_OBJECT (pad, "is buffer:%d", GST_IS_BUFFER (obj));
41   return GST_PAD_PROBE_OK;
42 }
43
44 static GstPadProbeReturn
45 data_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
46     gpointer data)
47 {
48   GstMiniObject *obj = type_data;
49   n_data_probes++;
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;
54 }
55
56 static GstPadProbeReturn
57 buffer_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
58     gpointer data)
59 {
60   GstBuffer *obj = type_data;
61   n_buffer_probes++;
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;
66 }
67
68 static GstPadProbeReturn
69 event_probe (GstPad * pad, GstPadProbeType type, gpointer type_data,
70     gpointer data)
71 {
72   GstEvent *obj = type_data;
73   n_event_probes++;
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;
79 }
80
81 GST_START_TEST (test_buffer_probe_n_times)
82 {
83   GstElement *pipeline, *fakesrc, *fakesink;
84   GstBus *bus;
85   GstMessage *message;
86   GstPad *pad;
87
88   pipeline = gst_element_factory_make ("pipeline", NULL);
89   fakesrc = gst_element_factory_make ("fakesrc", NULL);
90   fakesink = gst_element_factory_make ("fakesink", NULL);
91
92   g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
93
94   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
95   gst_element_link (fakesrc, fakesink);
96
97   pad = gst_element_get_static_pad (fakesink, "sink");
98
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);
106
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);
115
116   gst_object_unref (pad);
117
118   gst_element_set_state (pipeline, GST_STATE_PLAYING);
119
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);
124
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 */
128
129   gst_element_set_state (pipeline, GST_STATE_NULL);
130   gst_object_unref (pipeline);
131
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 */
136 } GST_END_TEST;
137
138 static int n_data_probes_once = 0;
139 static int n_buffer_probes_once = 0;
140 static int n_event_probes_once = 0;
141
142 static GstPadProbeReturn
143 data_probe_once (GstPad * pad, GstPadProbeType type, GstMiniObject * obj,
144     guint * data)
145 {
146   n_data_probes_once++;
147   g_assert (GST_IS_BUFFER (obj) || GST_IS_EVENT (obj));
148
149   gst_pad_remove_probe (pad, *data);
150
151   return GST_PAD_PROBE_OK;
152 }
153
154 static GstPadProbeReturn
155 buffer_probe_once (GstPad * pad, GstPadProbeType type, GstBuffer * obj,
156     guint * data)
157 {
158   n_buffer_probes_once++;
159   g_assert (GST_IS_BUFFER (obj));
160
161   gst_pad_remove_probe (pad, *data);
162
163   return GST_PAD_PROBE_OK;
164 }
165
166 static GstPadProbeReturn
167 event_probe_once (GstPad * pad, GstPadProbeType type, GstEvent * obj,
168     guint * data)
169 {
170   n_event_probes_once++;
171   g_assert (GST_IS_EVENT (obj));
172
173   gst_pad_remove_probe (pad, *data);
174
175   return GST_PAD_PROBE_OK;
176 }
177
178 GST_START_TEST (test_buffer_probe_once)
179 {
180   GstElement *pipeline, *fakesrc, *fakesink;
181   GstBus *bus;
182   GstMessage *message;
183   GstPad *pad;
184   guint id1, id2, id3;
185
186   pipeline = gst_element_factory_make ("pipeline", NULL);
187   fakesrc = gst_element_factory_make ("fakesrc", NULL);
188   fakesink = gst_element_factory_make ("fakesink", NULL);
189
190   g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
191
192   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
193   gst_element_link (fakesrc, fakesink);
194
195   pad = gst_element_get_static_pad (fakesink, "sink");
196   id1 =
197       gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA,
198       (GstPadProbeCallback) data_probe_once, &id1, NULL);
199   id2 =
200       gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
201       (GstPadProbeCallback) buffer_probe_once, &id2, NULL);
202   id3 =
203       gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT,
204       (GstPadProbeCallback) event_probe_once, &id3, NULL);
205   gst_object_unref (pad);
206
207   gst_element_set_state (pipeline, GST_STATE_PLAYING);
208
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);
213
214   gst_element_set_state (pipeline, GST_STATE_NULL);
215   gst_object_unref (pipeline);
216
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!!! */
220 } GST_END_TEST;
221
222 GST_START_TEST (test_math_scale)
223 {
224   fail_if (gst_util_uint64_scale_int (1, 1, 1) != 1);
225
226   fail_if (gst_util_uint64_scale_int (10, 10, 1) != 100);
227   fail_if (gst_util_uint64_scale_int (10, 10, 2) != 50);
228
229   fail_if (gst_util_uint64_scale_int (0, 10, 2) != 0);
230   fail_if (gst_util_uint64_scale_int (0, 0, 2) != 0);
231
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);
234
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);
237
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);
245
246   /* overflow */
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,
249           1) != G_MAXUINT64);
250
251 } GST_END_TEST;
252
253 GST_START_TEST (test_math_scale_round)
254 {
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);
258
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);
264 } GST_END_TEST;
265
266 GST_START_TEST (test_math_scale_ceil)
267 {
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);
271
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);
277 } GST_END_TEST;
278
279 GST_START_TEST (test_math_scale_uint64)
280 {
281   fail_if (gst_util_uint64_scale (1, 1, 1) != 1);
282
283   fail_if (gst_util_uint64_scale (10, 10, 1) != 100);
284   fail_if (gst_util_uint64_scale (10, 10, 2) != 50);
285
286   fail_if (gst_util_uint64_scale (0, 10, 2) != 0);
287   fail_if (gst_util_uint64_scale (0, 0, 2) != 0);
288
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);
291
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);
294
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);
301
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);
307
308   /* overflow */
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,
311           1) != G_MAXUINT64);
312
313 } GST_END_TEST;
314
315 GST_START_TEST (test_math_scale_random)
316 {
317   guint64 val, num, denom, res;
318   GRand *rand;
319   gint i;
320
321   rand = g_rand_new ();
322
323   i = 100000;
324   while (i--) {
325     guint64 check, diff;
326
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);
330
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));
334
335     if (res < G_MAXUINT64 && check < G_MAXUINT64) {
336       if (res > check)
337         diff = res - check;
338       else
339         diff = check - res;
340
341       /* some arbitrary value, really.. someone do the proper math to get
342        * the upper bound */
343       if (diff > 20000)
344         fail_if (diff > 20000);
345     }
346   }
347   g_rand_free (rand);
348
349 }
350
351 GST_END_TEST;
352
353 GST_START_TEST (test_guint64_to_gdouble)
354 {
355   guint64 from[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
356     ((guint64) (1) << 63) + 1,
357     ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
358   };
359   gdouble to[] = { 0., 1., 100., 10000., 9223372036854775808.,
360     9223372036854775809., 13835058055282163712.
361   };
362   gdouble tolerance[] = { 0., 0., 0., 0., 0., 1., 1. };
363   gint i;
364   gdouble result;
365   gdouble delta;
366
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]);
374   }
375 }
376
377 GST_END_TEST;
378
379 GST_START_TEST (test_gdouble_to_guint64)
380 {
381   gdouble from[] = { 0., 1., 100., 10000., 9223372036854775808.,
382     9223372036854775809., 13835058055282163712.
383   };
384   guint64 to[] = { 0, 1, 100, 10000, (guint64) (1) << 63,
385     ((guint64) (1) << 63) + 1,
386     ((guint64) (1) << 63) + (G_GINT64_CONSTANT (1) << 62)
387   };
388   guint64 tolerance[] = { 0, 0, 0, 0, 0, 1, 1 };
389   gint i;
390   gdouble result;
391   guint64 delta;
392
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]);
400   }
401 }
402
403 GST_END_TEST;
404
405 #ifndef GST_DISABLE_PARSE
406 GST_START_TEST (test_parse_bin_from_description)
407 {
408   struct
409   {
410     const gchar *bin_desc;
411     const gchar *pad_names;
412   } bin_tests[] = {
413     {
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", ""}
419   };
420   gint i;
421
422   for (i = 0; i < G_N_ELEMENTS (bin_tests); ++i) {
423     GstElement *bin, *parent;
424     GString *s;
425     GstPad *ghost_pad, *target_pad;
426     GError *err = NULL;
427
428     bin = gst_parse_bin_from_description (bin_tests[i].bin_desc, TRUE, &err);
429     if (err) {
430       g_error ("ERROR in gst_parse_bin_from_description (%s): %s",
431           bin_tests[i].bin_desc, err->message);
432     }
433     g_assert (bin != NULL);
434
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));
438
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));
442
443       parent = gst_pad_get_parent_element (target_pad);
444       g_assert (parent != NULL);
445
446       g_string_append_printf (s, "%s/sink", GST_ELEMENT_NAME (parent));
447
448       gst_object_unref (parent);
449       gst_object_unref (target_pad);
450       gst_object_unref (ghost_pad);
451     }
452
453     if ((ghost_pad = gst_element_get_static_pad (bin, "src"))) {
454       g_assert (GST_IS_GHOST_PAD (ghost_pad));
455
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));
459
460       parent = gst_pad_get_parent_element (target_pad);
461       g_assert (parent != NULL);
462
463       if (s->len > 0) {
464         g_string_append (s, ",");
465       }
466
467       g_string_append_printf (s, "%s/src", GST_ELEMENT_NAME (parent));
468
469       gst_object_unref (parent);
470       gst_object_unref (target_pad);
471       gst_object_unref (ghost_pad);
472     }
473
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);
477     }
478     g_string_free (s, TRUE);
479
480     gst_object_unref (bin);
481   }
482 }
483
484 GST_END_TEST;
485 #endif
486
487 GST_START_TEST (test_element_found_tags)
488 {
489   GstElement *pipeline, *fakesrc, *fakesink;
490   GstTagList *list;
491   GstBus *bus;
492   GstMessage *message;
493   GstPad *srcpad;
494
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 ();
499
500   g_object_set (fakesrc, "num-buffers", (int) 10, NULL);
501
502   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
503   gst_element_link (fakesrc, fakesink);
504
505   gst_element_set_state (pipeline, GST_STATE_PLAYING);
506
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);
510
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);
515
516   /* FIXME: maybe also check if the fakesink receives the message */
517
518   gst_element_set_state (pipeline, GST_STATE_NULL);
519   gst_object_unref (pipeline);
520 }
521
522 GST_END_TEST;
523
524 GST_START_TEST (test_element_unlink)
525 {
526   GstElement *src, *sink;
527
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);
534 }
535
536 GST_END_TEST;
537
538 GST_START_TEST (test_set_value_from_string)
539 {
540   GValue val = { 0, };
541
542   /* g_return_if_fail */
543   ASSERT_CRITICAL (gst_util_set_value_from_string (NULL, "xyz"));
544
545   g_value_init (&val, G_TYPE_STRING);
546   ASSERT_CRITICAL (gst_util_set_value_from_string (&val, NULL));
547   g_value_unset (&val);
548
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);
555
556   /* string => int */
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);
561
562   g_value_init (&val, G_TYPE_INT);
563   ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
564   g_value_unset (&val);
565
566   /* string => uint */
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);
571
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);
577
578   g_value_init (&val, G_TYPE_UINT);
579   ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
580   g_value_unset (&val);
581
582   /* string => long */
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);
587
588   g_value_init (&val, G_TYPE_LONG);
589   ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
590   g_value_unset (&val);
591
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);
597
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);
603
604   g_value_init (&val, G_TYPE_ULONG);
605   ASSERT_CRITICAL (gst_util_set_value_from_string (&val, "xyz"));
606   g_value_unset (&val);
607
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);
613
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);
618
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);
623
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);
628
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);
633
634 #if 0
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);
641
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);
647
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);
654
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);
660 #endif
661 }
662
663 GST_END_TEST;
664
665 static gint
666 _binary_search_compare (guint32 * a, guint32 * b)
667 {
668   return *a - *b;
669 }
670
671 GST_START_TEST (test_binary_search)
672 {
673   guint32 data[257];
674   guint32 *match;
675   guint32 search_element = 121 * 2;
676   guint i;
677
678   for (i = 0; i < 257; i++)
679     data[i] = (i + 1) * 2;
680
681   match =
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);
687
688   match =
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);
694
695   match =
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);
701
702   search_element = 0;
703   match =
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);
708
709   match =
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);
715
716   match =
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);
721
722   search_element = 1000;
723   match =
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);
728
729   match =
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);
734
735   match =
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);
741
742   search_element = 121 * 2 - 1;
743   match =
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);
748
749   match =
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);
755
756   match =
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);
762
763 }
764
765 GST_END_TEST;
766
767 #ifdef HAVE_GSL
768 #ifdef HAVE_GMP
769
770 #include <gsl/gsl_rng.h>
771 #include <gmp.h>
772
773 static guint64
774 randguint64 (gsl_rng * rng, guint64 n)
775 {
776   union
777   {
778     guint64 x;
779     struct
780     {
781       guint16 a, b, c, d;
782     } parts;
783   } x;
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);
788   return x.x % n;
789 }
790
791
792 enum round_t
793 {
794   ROUND_TONEAREST = 0,
795   ROUND_UP,
796   ROUND_DOWN
797 };
798
799 static void
800 gmp_set_uint64 (mpz_t mp, guint64 x)
801 {
802   mpz_t two_32, tmp;
803
804   mpz_init (two_32);
805   mpz_init (tmp);
806
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));
811   mpz_clear (two_32);
812   mpz_clear (tmp);
813 }
814
815 static guint64
816 gmp_get_uint64 (mpz_t mp)
817 {
818   mpz_t two_64, two_32, tmp;
819   guint64 ret;
820
821   mpz_init (two_64);
822   mpz_init (two_32);
823   mpz_init (tmp);
824
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)
828     return G_MAXUINT64;
829   mpz_clear (two_64);
830
831   mpz_tdiv_q (tmp, mp, two_32);
832   ret = mpz_get_ui (tmp);
833   ret <<= 32;
834   ret |= mpz_get_ui (mp);
835   mpz_clear (two_32);
836   mpz_clear (tmp);
837
838   return ret;
839 }
840
841 static guint64
842 gmp_scale (guint64 x, guint64 a, guint64 b, enum round_t mode)
843 {
844   mpz_t mp1, mp2, mp3;
845   if (!b)
846     /* overflow */
847     return G_MAXUINT64;
848   mpz_init (mp1);
849   mpz_init (mp2);
850   mpz_init (mp3);
851
852   gmp_set_uint64 (mp1, x);
853   gmp_set_uint64 (mp3, a);
854   mpz_mul (mp2, mp1, mp3);
855   switch (mode) {
856     case ROUND_TONEAREST:
857       gmp_set_uint64 (mp1, b);
858       mpz_tdiv_q_ui (mp3, mp1, 2);
859       mpz_add (mp1, mp2, mp3);
860       mpz_set (mp2, mp1);
861       break;
862     case ROUND_UP:
863       gmp_set_uint64 (mp1, b);
864       mpz_sub_ui (mp3, mp1, 1);
865       mpz_add (mp1, mp2, mp3);
866       mpz_set (mp2, mp1);
867       break;
868     case ROUND_DOWN:
869       break;
870   }
871   gmp_set_uint64 (mp3, b);
872   mpz_tdiv_q (mp1, mp2, mp3);
873   x = gmp_get_uint64 (mp1);
874   mpz_clear (mp1);
875   mpz_clear (mp2);
876   mpz_clear (mp3);
877   return x;
878 }
879
880 static void
881 _gmp_test_scale (gsl_rng * rng)
882 {
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);
889   const char *func;
890
891   bygmp = gmp_scale (val, a, b, mode);
892   switch (mode) {
893     case ROUND_TONEAREST:
894       bygst = gst_util_uint64_scale_round (val, a, b);
895       func = "gst_util_uint64_scale_round";
896       break;
897     case ROUND_UP:
898       bygst = gst_util_uint64_scale_ceil (val, a, b);
899       func = "gst_util_uint64_scale_ceil";
900       break;
901     case ROUND_DOWN:
902       bygst = gst_util_uint64_scale (val, a, b);
903       func = "gst_util_uint64_scale";
904       break;
905     default:
906       g_assert_not_reached ();
907       break;
908   }
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);
913 }
914
915 static void
916 _gmp_test_scale_int (gsl_rng * rng)
917 {
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);
923   const char *func;
924
925   bygmp = gmp_scale (val, a, b, mode);
926   switch (mode) {
927     case ROUND_TONEAREST:
928       bygst = gst_util_uint64_scale_int_round (val, a, b);
929       func = "gst_util_uint64_scale_int_round";
930       break;
931     case ROUND_UP:
932       bygst = gst_util_uint64_scale_int_ceil (val, a, b);
933       func = "gst_util_uint64_scale_int_ceil";
934       break;
935     case ROUND_DOWN:
936       bygst = gst_util_uint64_scale_int (val, a, b);
937       func = "gst_util_uint64_scale_int";
938       break;
939     default:
940       g_assert_not_reached ();
941       break;
942   }
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);
946 }
947
948 #define GMP_TEST_RUNS 100000
949
950 GST_START_TEST (test_math_scale_gmp)
951 {
952   gsl_rng *rng = gsl_rng_alloc (gsl_rng_mt19937);
953   gint n;
954
955   for (n = 0; n < GMP_TEST_RUNS; n++)
956     _gmp_test_scale (rng);
957
958   gsl_rng_free (rng);
959 }
960
961 GST_END_TEST;
962
963 GST_START_TEST (test_math_scale_gmp_int)
964 {
965   gsl_rng *rng = gsl_rng_alloc (gsl_rng_mt19937);
966   gint n;
967
968   for (n = 0; n < GMP_TEST_RUNS; n++)
969     _gmp_test_scale_int (rng);
970
971   gsl_rng_free (rng);
972 }
973
974 GST_END_TEST;
975
976 #endif
977 #endif
978
979 GST_START_TEST (test_pad_proxy_getcaps_aggregation)
980 {
981   GstElement *tee, *sink1, *sink2;
982   GstCaps *caps;
983   GstPad *tee_src1, *tee_src2, *tee_sink, *sink1_sink, *sink2_sink;
984
985   tee = gst_element_factory_make ("tee", "tee");
986
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);
991
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);
996
997   tee_sink = gst_element_get_static_pad (tee, "sink");
998
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);
1002
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);
1009
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);
1015
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);
1020
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);
1026
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);
1032
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));
1037   {
1038     GstStructure *s = gst_caps_get_structure (caps, 0);
1039
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));
1042   }
1043   gst_caps_unref (caps);
1044
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);
1048
1049   /* clean up */
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);
1060 }
1061
1062 GST_END_TEST;
1063
1064 GST_START_TEST (test_greatest_common_divisor)
1065 {
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);
1081
1082 }
1083
1084 GST_END_TEST;
1085
1086 static Suite *
1087 gst_utils_suite (void)
1088 {
1089   Suite *s = suite_create ("GstUtils");
1090   TCase *tc_chain = tcase_create ("general");
1091
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);
1100 #ifdef HAVE_GSL
1101 #ifdef HAVE_GMP
1102   tcase_add_test (tc_chain, test_math_scale_gmp);
1103   tcase_add_test (tc_chain, test_math_scale_gmp_int);
1104 #endif
1105 #endif
1106
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);
1111 #endif
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);
1116
1117   tcase_add_test (tc_chain, test_pad_proxy_getcaps_aggregation);
1118   tcase_add_test (tc_chain, test_greatest_common_divisor);
1119   return s;
1120 }
1121
1122 GST_CHECK_MAIN (gst_utils);