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 <gst/check/gstcheck.h>
27 GST_START_TEST (test_deserialize_buffer)
32 g_value_init (&value, GST_TYPE_BUFFER);
33 fail_unless (gst_value_deserialize (&value, "1234567890abcdef"));
34 /* does not increase the refcount */
35 buf = GST_BUFFER (gst_value_get_mini_object (&value));
36 ASSERT_MINI_OBJECT_REFCOUNT (buf, "buffer", 1);
38 /* does not increase the refcount */
39 buf = gst_value_get_buffer (&value);
40 ASSERT_MINI_OBJECT_REFCOUNT (buf, "buffer", 1);
43 g_value_unset (&value);
48 /* create and serialize a buffer */
49 GST_START_TEST (test_serialize_buffer)
54 static const char *buf_data = "1234567890abcdef";
57 len = strlen (buf_data);
58 buf = gst_buffer_new_and_alloc (len);
59 memcpy (GST_BUFFER_DATA (buf), buf_data, len);
60 ASSERT_MINI_OBJECT_REFCOUNT (buf, "buffer", 1);
62 /* and assign buffer to mini object */
63 g_value_init (&value, GST_TYPE_BUFFER);
64 gst_value_take_buffer (&value, buf);
65 ASSERT_MINI_OBJECT_REFCOUNT (buf, "buffer", 1);
67 /* now serialize it */
68 serialized = gst_value_serialize (&value);
69 GST_DEBUG ("serialized buffer to %s", serialized);
70 fail_unless (serialized != NULL);
72 /* refcount should not change */
73 ASSERT_MINI_OBJECT_REFCOUNT (buf, "buffer", 1);
77 g_value_unset (&value);
79 /* take NULL buffer */
80 g_value_init (&value, GST_TYPE_BUFFER);
81 GST_DEBUG ("setting NULL buffer");
82 gst_value_take_buffer (&value, NULL);
84 /* now serialize it */
85 GST_DEBUG ("serializing NULL buffer");
86 serialized = gst_value_serialize (&value);
87 /* should return NULL */
88 fail_unless (serialized == NULL);
91 g_value_unset (&value);
96 GST_START_TEST (test_deserialize_gint64)
99 const char *strings[] = {
109 g_value_init (&value, G_TYPE_INT64);
111 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
112 fail_unless (gst_value_deserialize (&value, strings[i]),
113 "could not deserialize %s (%d)", strings[i], i);
114 fail_unless (g_value_get_int64 (&value) == results[i],
115 "resulting value is %" G_GINT64_FORMAT ", not %" G_GINT64_FORMAT
116 ", for string %s (%d)", g_value_get_int64 (&value),
117 results[i], strings[i], i);
123 GST_START_TEST (test_deserialize_gstfraction)
125 GValue value = { 0 };
126 const char *strings[] = {
130 gint64 result_numers[] = {
134 gint64 result_denoms[] = {
141 g_value_init (&value, GST_TYPE_FRACTION);
142 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
143 fail_unless (gst_value_deserialize (&value, strings[i]),
144 "could not deserialize %s (%d)", strings[i], i);
145 fail_unless (gst_value_get_fraction_numerator (&value) == result_numers[i],
146 "resulting numerator value is %d, not %d"
147 ", for string %s (%d)", gst_value_get_fraction_numerator (&value),
148 result_numers[i], strings[i], i);
149 fail_unless (gst_value_get_fraction_denominator (&value) ==
150 result_denoms[i], "resulting denominator value is %d, not %d"
151 ", for string %s (%d)", gst_value_get_fraction_denominator (&value),
152 result_denoms[i], strings[i], i);
158 GST_START_TEST (test_deserialize_gint)
160 GValue value = { 0 };
161 const char *strings[] = {
166 /* a positive long long, serializing to highest possible positive sint */
168 /* a positive long long, serializing to lowest possible negative sint */
170 /* a negative long long, serializing to lowest possible negative sint */
171 "0xFFFFFFFF80000000",
173 /* a positive long long serializing to -1 */
176 /* a negative long long serializing to -1 */
177 "0xFFFFFFFFFFFFFFFF",
178 "0xFFFFFFFFFFFFFFFF",
181 /* some casts need to be explicit because of unsigned -> signed */
194 (gint) 0xFFFFFFFFFFFFFFFFLL,
199 g_value_init (&value, G_TYPE_INT);
201 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
202 fail_unless (gst_value_deserialize (&value, strings[i]),
203 "could not deserialize %s (%d)", strings[i], i);
204 fail_unless (g_value_get_int (&value) == results[i],
205 "resulting value is %d, not %d, for string %s (%d)",
206 g_value_get_int (&value), results[i], strings[i], i);
212 GST_START_TEST (test_deserialize_gint_failures)
214 GValue value = { 0 };
215 const char *strings[] = {
216 "-", /* not a complete number */
217 "- TEST", /* not a complete number */
218 "0x0000000100000000", /* lowest long long that cannot fit in 32 bits */
219 "0xF000000000000000",
220 "0xFFFFFFF000000000",
221 "0xFFFFFFFF00000000",
222 "0x10000000000000000", /* first number too long to fit into a long long */
223 /* invent a new processor first before trying to make this one pass */
224 "0x10000000000000000000000000000000000000000000",
228 g_value_init (&value, G_TYPE_INT);
230 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
231 fail_if (gst_value_deserialize (&value, strings[i]),
232 "deserialized %s (%d), while it should have failed", strings[i], i);
238 GST_START_TEST (test_deserialize_guint)
240 GValue value = { 0 };
241 const char *strings[] = {
246 /* a positive long long, serializing to highest possible positive sint */
248 /* a positive long long, serializing to lowest possible negative sint */
251 /* a negative long long, serializing to lowest possible negative sint */
252 "0xFFFFFFFF80000000",
253 /* a value typically used for rgb masks */
255 /* a positive long long serializing to highest possible positive uint */
258 /* a negative long long serializing to highest possible positive uint */
259 "0xFFFFFFFFFFFFFFFF",
269 (guint) 2147483648LL,
274 (guint) 0xFFFFFFFFFFFFFFFFLL,
279 g_value_init (&value, G_TYPE_UINT);
281 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
282 fail_unless (gst_value_deserialize (&value, strings[i]),
283 "could not deserialize %s (%d)", strings[i], i);
284 fail_unless (g_value_get_uint (&value) == results[i],
285 "resulting value is %d, not %d, for string %s (%d)",
286 g_value_get_uint (&value), results[i], strings[i], i);
292 GST_START_TEST (test_deserialize_guint_failures)
294 GValue value = { 0 };
295 const char *strings[] = {
296 "-", /* not a complete number */
297 "- TEST", /* not a complete number */
299 /* FIXME: these values should not be deserializable, since they overflow
300 * the target format */
301 "0x0000000100000000", /* lowest long long that cannot fit in 32 bits */
302 "0xF000000000000000",
303 "0xFFFFFFF000000000",
304 "0xFFFFFFFF00000000",
305 "0x10000000000000000", /* first number too long to fit into a long long */
306 /* invent a new processor first before trying to make this one pass */
307 "0x10000000000000000000000000000000000000000000",
312 g_value_init (&value, G_TYPE_UINT);
314 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
315 fail_if (gst_value_deserialize (&value, strings[i]),
316 "deserialized %s (%d), while it should have failed", strings[i], i);
322 GST_START_TEST (test_serialize_flags)
324 GValue value = { 0 };
326 GstSeekFlags flags[] = {
330 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
332 const char *results[] = {
333 "GST_SEEK_FLAG_NONE",
334 "GST_SEEK_FLAG_NONE",
335 "GST_SEEK_FLAG_FLUSH",
336 "GST_SEEK_FLAG_FLUSH+GST_SEEK_FLAG_ACCURATE",
340 g_value_init (&value, GST_TYPE_SEEK_FLAGS);
342 for (i = 0; i < G_N_ELEMENTS (flags); ++i) {
343 g_value_set_flags (&value, flags[i]);
344 string = gst_value_serialize (&value);
345 fail_if (string == NULL, "could not serialize flags %d", i);
346 fail_unless (strcmp (string, results[i]) == 0,
347 "resulting value is %s, not %s, for flags #%d", string, results[i], i);
355 GST_START_TEST (test_deserialize_flags)
357 GValue value = { 0 };
358 const char *strings[] = {
361 "GST_SEEK_FLAG_NONE",
362 "GST_SEEK_FLAG_FLUSH",
363 "GST_SEEK_FLAG_FLUSH+GST_SEEK_FLAG_ACCURATE",
365 GstSeekFlags results[] = {
370 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
374 g_value_init (&value, GST_TYPE_SEEK_FLAGS);
376 for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
377 fail_unless (gst_value_deserialize (&value, strings[i]),
378 "could not deserialize %s (%d)", strings[i], i);
379 fail_unless (g_value_get_flags (&value) == results[i],
380 "resulting value is %d, not %d, for string %s (%d)",
381 g_value_get_flags (&value), results[i], strings[i], i);
387 GST_START_TEST (test_string)
398 g_value_init (&v, G_TYPE_STRING);
399 for (i = 0; i < G_N_ELEMENTS (try); i++) {
400 g_value_set_string (&v, try[i]);
401 tmp = gst_value_serialize (&v);
402 fail_if (tmp == NULL, "couldn't serialize: %s\n", try[i]);
403 fail_unless (gst_value_deserialize (&v, tmp),
404 "couldn't deserialize: %s\n", tmp);
407 fail_unless (g_str_equal (g_value_get_string (&v), try[i]),
408 "\nserialized : %s\ndeserialized: %s", try[i],
409 g_value_get_string (&v));
411 /* NULL strings should not be serializable */
412 g_value_set_string (&v, NULL);
413 fail_unless (gst_value_serialize (&v) == NULL);
419 GST_START_TEST (test_deserialize_string)
427 "", ""}, /* empty strings */
429 "\"\"", ""}, /* FAILURES */
431 "\"", NULL}, /* missing second quote */
433 "\"Hello\\ World", NULL}, /* missing second quote */
435 "\"\\", NULL}, /* quote at end, missing second quote */
437 "\"\\0", NULL}, /* missing second quote */
439 "\"\\0\"", NULL}, /* unfinished escaped character */
441 "\" \"", NULL}, /* spaces must be escaped */
443 /* FIXME 0.9: this test should fail, but it doesn't */
445 "tüüt", NULL} /* string with special chars must be escaped */
452 g_value_init (&v, G_TYPE_STRING);
453 for (i = 0; i < G_N_ELEMENTS (tests); i++) {
454 if (gst_value_deserialize (&v, tests[i].from)) {
455 fail_if (tests[i].to == NULL,
456 "I got %s instead of a failure", g_value_get_string (&v));
457 fail_unless (g_str_equal (g_value_get_string (&v), tests[i].to),
458 "\nwanted: %s\ngot : %s", tests[i].to, g_value_get_string (&v));
460 fail_if (tests[i].to != NULL, "failed, but wanted: %s", tests[i].to);
469 GST_START_TEST (test_value_compare)
471 GValue value1 = { 0 };
472 GValue value2 = { 0 };
475 g_value_init (&value1, G_TYPE_INT);
476 g_value_set_int (&value1, 10);
477 g_value_init (&value2, G_TYPE_INT);
478 g_value_set_int (&value2, 20);
479 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
480 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
481 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
482 g_value_unset (&value1);
483 g_value_unset (&value2);
485 g_value_init (&value1, G_TYPE_DOUBLE);
486 g_value_set_double (&value1, 10);
487 g_value_init (&value2, G_TYPE_DOUBLE);
488 g_value_set_double (&value2, 20);
489 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
490 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
491 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
492 g_value_unset (&value1);
493 g_value_unset (&value2);
495 g_value_init (&value1, G_TYPE_STRING);
496 g_value_set_string (&value1, "a");
497 g_value_init (&value2, G_TYPE_STRING);
498 g_value_set_string (&value2, "b");
499 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
500 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
501 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
502 g_value_unset (&value1);
503 g_value_unset (&value2);
505 g_value_init (&value1, GST_TYPE_FOURCC);
506 gst_value_set_fourcc (&value1, GST_MAKE_FOURCC ('a', 'b', 'c', 'd'));
507 g_value_init (&value2, GST_TYPE_FOURCC);
508 gst_value_set_fourcc (&value2, GST_MAKE_FOURCC ('1', '2', '3', '4'));
509 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_UNORDERED);
510 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
511 g_value_unset (&value1);
512 g_value_unset (&value2);
514 /* comparing 2/3 with 3/4 */
515 g_value_init (&value1, GST_TYPE_FRACTION);
516 gst_value_set_fraction (&value1, 2, 3);
517 g_value_init (&value2, GST_TYPE_FRACTION);
518 gst_value_set_fraction (&value2, 3, 4);
519 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
520 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
521 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
522 g_value_unset (&value1);
523 g_value_unset (&value2);
525 /* comparing -4/5 with 2/-3 */
526 g_value_init (&value1, GST_TYPE_FRACTION);
527 gst_value_set_fraction (&value1, -4, 5);
528 g_value_init (&value2, GST_TYPE_FRACTION);
529 gst_value_set_fraction (&value2, 2, -3);
530 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
531 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
532 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
533 g_value_unset (&value1);
534 g_value_unset (&value2);
536 /* comparing 10/100 with 200/2000 */
537 g_value_init (&value1, GST_TYPE_FRACTION);
538 gst_value_set_fraction (&value1, 10, 100);
539 g_value_init (&value2, GST_TYPE_FRACTION);
540 gst_value_set_fraction (&value2, 200, 2000);
541 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL);
542 g_value_unset (&value1);
543 g_value_unset (&value2);
545 /* comparing -4/5 with 2/-3 */
546 g_value_init (&value1, GST_TYPE_FRACTION);
547 gst_value_set_fraction (&value1, -4, 5);
548 g_value_init (&value2, GST_TYPE_FRACTION);
549 gst_value_set_fraction (&value2, 2, -3);
550 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_LESS_THAN);
551 fail_unless (gst_value_compare (&value2, &value1) == GST_VALUE_GREATER_THAN);
552 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL);
553 g_value_unset (&value1);
554 g_value_unset (&value2);
556 /* Check that lists are equal regardless of order */
557 g_value_init (&value1, GST_TYPE_LIST);
558 g_value_init (&tmp, G_TYPE_INT);
559 g_value_set_int (&tmp, 1);
560 gst_value_list_append_value (&value1, &tmp);
561 g_value_set_int (&tmp, 2);
562 gst_value_list_append_value (&value1, &tmp);
563 g_value_set_int (&tmp, 3);
564 gst_value_list_append_value (&value1, &tmp);
565 g_value_set_int (&tmp, 4);
566 gst_value_list_append_value (&value1, &tmp);
568 g_value_init (&value2, GST_TYPE_LIST);
569 g_value_set_int (&tmp, 4);
570 gst_value_list_append_value (&value2, &tmp);
571 g_value_set_int (&tmp, 3);
572 gst_value_list_append_value (&value2, &tmp);
573 g_value_set_int (&tmp, 2);
574 gst_value_list_append_value (&value2, &tmp);
575 g_value_set_int (&tmp, 1);
576 gst_value_list_append_value (&value2, &tmp);
578 fail_unless (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL,
579 "value lists with different order were not equal when they should be");
580 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL,
581 "value lists with same order were not equal when they should be");
582 fail_unless (gst_value_compare (&value2, &value2) == GST_VALUE_EQUAL,
583 "value lists with same order were not equal when they should be");
585 /* Carry over the lists to this next check: */
586 /* Lists with different sizes are unequal */
587 g_value_set_int (&tmp, 1);
588 gst_value_list_append_value (&value2, &tmp);
590 fail_if (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL,
591 "Value lists with different size were equal when they shouldn't be");
593 /* Carry over the lists to this next check: */
594 /* Lists with same size but list1 contains one more element not in list2 */
595 g_value_set_int (&tmp, 5);
596 gst_value_list_append_value (&value1, &tmp);
598 fail_if (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL,
599 "Value lists with different elements were equal when they shouldn't be");
600 fail_if (gst_value_compare (&value2, &value1) == GST_VALUE_EQUAL,
601 "Value lists with different elements were equal when they shouldn't be");
603 g_value_unset (&value1);
604 g_value_unset (&value2);
605 g_value_unset (&tmp);
607 /* Arrays are only equal when in the same order */
608 g_value_init (&value1, GST_TYPE_ARRAY);
609 g_value_init (&tmp, G_TYPE_INT);
610 g_value_set_int (&tmp, 1);
611 gst_value_array_append_value (&value1, &tmp);
612 g_value_set_int (&tmp, 2);
613 gst_value_array_append_value (&value1, &tmp);
614 g_value_set_int (&tmp, 3);
615 gst_value_array_append_value (&value1, &tmp);
616 g_value_set_int (&tmp, 4);
617 gst_value_array_append_value (&value1, &tmp);
619 g_value_init (&value2, GST_TYPE_ARRAY);
620 g_value_set_int (&tmp, 4);
621 gst_value_array_append_value (&value2, &tmp);
622 g_value_set_int (&tmp, 3);
623 gst_value_array_append_value (&value2, &tmp);
624 g_value_set_int (&tmp, 2);
625 gst_value_array_append_value (&value2, &tmp);
626 g_value_set_int (&tmp, 1);
627 gst_value_array_append_value (&value2, &tmp);
629 fail_if (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL,
630 "Value arrays with different order were equal when they shouldn't be");
631 fail_unless (gst_value_compare (&value1, &value1) == GST_VALUE_EQUAL,
632 "Identical value arrays were not equal when they should be");
633 fail_unless (gst_value_compare (&value2, &value2) == GST_VALUE_EQUAL,
634 "Identical value arrays were not equal when they should be");
636 /* Carry over the arrays to this next check: */
637 /* Arrays with different sizes are unequal */
638 g_value_unset (&value2);
639 g_value_init (&value2, GST_TYPE_ARRAY);
640 g_value_copy (&value1, &value2);
642 g_value_set_int (&tmp, 1);
643 gst_value_array_append_value (&value2, &tmp);
645 fail_if (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL,
646 "Value arrays with different size were equal when they shouldn't be");
647 /* order should not matter */
648 fail_if (gst_value_compare (&value2, &value1) == GST_VALUE_EQUAL,
649 "Value arrays with different size were equal when they shouldn't be");
651 g_value_unset (&value1);
652 g_value_unset (&value2);
653 g_value_unset (&tmp);
658 GST_START_TEST (test_value_intersect)
666 g_value_init (&src1, G_TYPE_INT);
667 g_value_set_int (&src1, 10);
668 g_value_init (&src2, G_TYPE_INT);
669 g_value_set_int (&src2, 20);
670 ret = gst_value_intersect (&dest, &src1, &src2);
671 fail_unless (ret == FALSE);
672 g_value_unset (&src1);
673 g_value_unset (&src2);
675 g_value_init (&src1, GST_TYPE_FOURCC);
676 gst_value_set_fourcc (&src1, GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'));
677 g_value_init (&src2, GST_TYPE_LIST);
678 g_value_init (&item, GST_TYPE_FOURCC);
679 gst_value_set_fourcc (&item, GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'));
680 gst_value_list_append_value (&src2, &item);
681 gst_value_set_fourcc (&item, GST_MAKE_FOURCC ('I', '4', '2', '0'));
682 gst_value_list_append_value (&src2, &item);
683 gst_value_set_fourcc (&item, GST_MAKE_FOURCC ('A', 'B', 'C', 'D'));
684 gst_value_list_append_value (&src2, &item);
686 fail_unless (gst_value_intersect (&dest, &src1, &src2));
687 fail_unless (GST_VALUE_HOLDS_FOURCC (&dest));
688 fail_unless (gst_value_get_fourcc (&dest) ==
689 GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'));
691 g_value_unset (&src1);
692 g_value_unset (&src2);
698 GST_START_TEST (test_value_subtract_int)
708 g_value_init (&src1, G_TYPE_INT);
709 g_value_set_int (&src1, 10);
710 g_value_init (&src2, G_TYPE_INT);
711 g_value_set_int (&src2, 20);
712 /* subtract as in sets, result is 10 */
713 ret = gst_value_subtract (&dest, &src1, &src2);
714 fail_unless (ret == TRUE);
715 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
716 g_value_unset (&dest);
718 /* same values, yields empty set */
719 ret = gst_value_subtract (&dest, &src1, &src1);
720 fail_unless (ret == FALSE);
721 g_value_unset (&src1);
722 g_value_unset (&src2);
727 /* would yield an empty set */
728 g_value_init (&src1, G_TYPE_INT);
729 g_value_set_int (&src1, 10);
730 g_value_init (&src2, GST_TYPE_INT_RANGE);
731 gst_value_set_int_range (&src2, 0, 20);
732 ret = gst_value_subtract (&dest, &src1, &src2);
733 fail_unless (ret == FALSE);
735 /* and the other way around, should create a list of two ranges. */
736 ret = gst_value_subtract (&dest, &src2, &src1);
737 fail_unless (ret == TRUE);
738 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
739 tmp = gst_value_list_get_value (&dest, 0);
740 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
741 fail_unless (gst_value_get_int_range_min (tmp) == 0);
742 fail_unless (gst_value_get_int_range_max (tmp) == 9);
743 tmp = gst_value_list_get_value (&dest, 1);
744 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
745 fail_unless (gst_value_get_int_range_min (tmp) == 11);
746 fail_unless (gst_value_get_int_range_max (tmp) == 20);
747 g_value_unset (&dest);
748 g_value_unset (&src1);
749 g_value_unset (&src2);
751 /* border case 1, empty set */
752 g_value_init (&src1, G_TYPE_INT);
753 g_value_set_int (&src1, 10);
754 g_value_init (&src2, GST_TYPE_INT_RANGE);
755 gst_value_set_int_range (&src2, 10, 20);
756 ret = gst_value_subtract (&dest, &src1, &src2);
757 fail_unless (ret == FALSE);
759 /* and the other way around, should create a new range. */
760 ret = gst_value_subtract (&dest, &src2, &src1);
761 fail_unless (ret == TRUE);
762 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
763 fail_unless (gst_value_get_int_range_min (&dest) == 11);
764 fail_unless (gst_value_get_int_range_max (&dest) == 20);
765 g_value_unset (&dest);
766 g_value_unset (&src1);
767 g_value_unset (&src2);
769 /* border case 2, empty set */
770 g_value_init (&src1, G_TYPE_INT);
771 g_value_set_int (&src1, 20);
772 g_value_init (&src2, GST_TYPE_INT_RANGE);
773 gst_value_set_int_range (&src2, 10, 20);
774 ret = gst_value_subtract (&dest, &src1, &src2);
775 fail_unless (ret == FALSE);
777 /* and the other way around, should create a new range. */
778 ret = gst_value_subtract (&dest, &src2, &src1);
779 fail_unless (ret == TRUE);
780 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
781 fail_unless (gst_value_get_int_range_min (&dest) == 10);
782 fail_unless (gst_value_get_int_range_max (&dest) == 19);
783 g_value_unset (&dest);
784 g_value_unset (&src1);
785 g_value_unset (&src2);
787 /* case 3, valid set */
788 g_value_init (&src1, G_TYPE_INT);
789 g_value_set_int (&src1, 0);
790 g_value_init (&src2, GST_TYPE_INT_RANGE);
791 gst_value_set_int_range (&src2, 10, 20);
792 ret = gst_value_subtract (&dest, &src1, &src2);
793 fail_unless (ret == TRUE);
794 fail_unless (G_VALUE_HOLDS_INT (&dest) == TRUE);
795 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
796 g_value_unset (&dest);
798 /* and the other way around, should keep the range. */
799 ret = gst_value_subtract (&dest, &src2, &src1);
800 fail_unless (ret == TRUE);
801 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
802 fail_unless (gst_value_get_int_range_min (&dest) == 10);
803 fail_unless (gst_value_get_int_range_max (&dest) == 20);
804 g_value_unset (&dest);
805 g_value_unset (&src1);
806 g_value_unset (&src2);
808 /* int_range <-> int_range
811 /* same range, empty set */
812 g_value_init (&src1, GST_TYPE_INT_RANGE);
813 gst_value_set_int_range (&src1, 10, 20);
814 g_value_init (&src2, GST_TYPE_INT_RANGE);
815 gst_value_set_int_range (&src2, 10, 20);
816 ret = gst_value_subtract (&dest, &src1, &src2);
817 fail_unless (ret == FALSE);
818 ret = gst_value_subtract (&dest, &src2, &src1);
819 fail_unless (ret == FALSE);
820 g_value_unset (&src1);
821 g_value_unset (&src2);
823 /* non overlapping ranges */
824 g_value_init (&src1, GST_TYPE_INT_RANGE);
825 gst_value_set_int_range (&src1, 10, 20);
826 g_value_init (&src2, GST_TYPE_INT_RANGE);
827 gst_value_set_int_range (&src2, 30, 40);
828 ret = gst_value_subtract (&dest, &src1, &src2);
829 fail_unless (ret == TRUE);
830 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
831 fail_unless (gst_value_get_int_range_min (&dest) == 10);
832 fail_unless (gst_value_get_int_range_max (&dest) == 20);
833 g_value_unset (&dest);
835 ret = gst_value_subtract (&dest, &src2, &src1);
836 fail_unless (ret == TRUE);
837 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
838 fail_unless (gst_value_get_int_range_min (&dest) == 30);
839 fail_unless (gst_value_get_int_range_max (&dest) == 40);
840 g_value_unset (&dest);
841 g_value_unset (&src1);
842 g_value_unset (&src2);
844 /* completely overlapping ranges */
845 g_value_init (&src1, GST_TYPE_INT_RANGE);
846 gst_value_set_int_range (&src1, 10, 20);
847 g_value_init (&src2, GST_TYPE_INT_RANGE);
848 gst_value_set_int_range (&src2, 10, 30);
849 ret = gst_value_subtract (&dest, &src1, &src2);
850 fail_unless (ret == FALSE);
852 ret = gst_value_subtract (&dest, &src2, &src1);
853 fail_unless (ret == TRUE);
854 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
855 fail_unless (gst_value_get_int_range_min (&dest) == 21);
856 fail_unless (gst_value_get_int_range_max (&dest) == 30);
857 g_value_unset (&dest);
858 g_value_unset (&src1);
859 g_value_unset (&src2);
861 /* partially overlapping ranges */
862 g_value_init (&src1, GST_TYPE_INT_RANGE);
863 gst_value_set_int_range (&src1, 10, 20);
864 g_value_init (&src2, GST_TYPE_INT_RANGE);
865 gst_value_set_int_range (&src2, 15, 30);
866 ret = gst_value_subtract (&dest, &src1, &src2);
867 fail_unless (ret == TRUE);
868 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
869 fail_unless (gst_value_get_int_range_min (&dest) == 10);
870 fail_unless (gst_value_get_int_range_max (&dest) == 14);
871 g_value_unset (&dest);
873 ret = gst_value_subtract (&dest, &src2, &src1);
874 fail_unless (ret == TRUE);
875 fail_unless (GST_VALUE_HOLDS_INT_RANGE (&dest) == TRUE);
876 fail_unless (gst_value_get_int_range_min (&dest) == 21);
877 fail_unless (gst_value_get_int_range_max (&dest) == 30);
878 g_value_unset (&dest);
879 g_value_unset (&src1);
880 g_value_unset (&src2);
882 /* create a hole { int_range, int_range } */
883 g_value_init (&src1, GST_TYPE_INT_RANGE);
884 gst_value_set_int_range (&src1, 10, 30);
885 g_value_init (&src2, GST_TYPE_INT_RANGE);
886 gst_value_set_int_range (&src2, 15, 20);
887 ret = gst_value_subtract (&dest, &src1, &src2);
888 fail_unless (ret == TRUE);
889 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
890 tmp = gst_value_list_get_value (&dest, 0);
891 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
892 fail_unless (gst_value_get_int_range_min (tmp) == 10);
893 fail_unless (gst_value_get_int_range_max (tmp) == 14);
894 tmp = gst_value_list_get_value (&dest, 1);
895 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
896 fail_unless (gst_value_get_int_range_min (tmp) == 21);
897 fail_unless (gst_value_get_int_range_max (tmp) == 30);
898 g_value_unset (&dest);
900 ret = gst_value_subtract (&dest, &src2, &src1);
901 fail_unless (ret == FALSE);
902 g_value_unset (&src1);
903 g_value_unset (&src2);
905 /* create a hole, { int, int } */
906 g_value_init (&src1, GST_TYPE_INT_RANGE);
907 gst_value_set_int_range (&src1, 10, 30);
908 g_value_init (&src2, GST_TYPE_INT_RANGE);
909 gst_value_set_int_range (&src2, 11, 29);
910 ret = gst_value_subtract (&dest, &src1, &src2);
911 fail_unless (ret == TRUE);
912 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
913 tmp = gst_value_list_get_value (&dest, 0);
914 fail_unless (G_VALUE_HOLDS_INT (tmp) == TRUE);
915 fail_unless (g_value_get_int (tmp) == 10);
916 tmp = gst_value_list_get_value (&dest, 1);
917 fail_unless (G_VALUE_HOLDS_INT (tmp) == TRUE);
918 fail_unless (g_value_get_int (tmp) == 30);
919 g_value_unset (&dest);
921 ret = gst_value_subtract (&dest, &src2, &src1);
922 fail_unless (ret == FALSE);
923 g_value_unset (&src1);
924 g_value_unset (&src2);
926 /* create a hole, { int, int_range } */
927 g_value_init (&src1, GST_TYPE_INT_RANGE);
928 gst_value_set_int_range (&src1, 10, 30);
929 g_value_init (&src2, GST_TYPE_INT_RANGE);
930 gst_value_set_int_range (&src2, 11, 28);
931 ret = gst_value_subtract (&dest, &src1, &src2);
932 fail_unless (ret == TRUE);
933 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
934 tmp = gst_value_list_get_value (&dest, 0);
935 fail_unless (G_VALUE_HOLDS_INT (tmp) == TRUE);
936 fail_unless (g_value_get_int (tmp) == 10);
937 tmp = gst_value_list_get_value (&dest, 1);
938 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
939 fail_unless (gst_value_get_int_range_min (tmp) == 29);
940 fail_unless (gst_value_get_int_range_max (tmp) == 30);
941 g_value_unset (&dest);
943 ret = gst_value_subtract (&dest, &src2, &src1);
944 fail_unless (ret == FALSE);
945 g_value_unset (&src1);
946 g_value_unset (&src2);
948 /* create a hole, { int_range, int } */
949 g_value_init (&src1, GST_TYPE_INT_RANGE);
950 gst_value_set_int_range (&src1, 10, 30);
951 g_value_init (&src2, GST_TYPE_INT_RANGE);
952 gst_value_set_int_range (&src2, 12, 29);
953 ret = gst_value_subtract (&dest, &src1, &src2);
954 fail_unless (ret == TRUE);
955 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
956 tmp = gst_value_list_get_value (&dest, 0);
957 fail_unless (GST_VALUE_HOLDS_INT_RANGE (tmp) == TRUE);
958 fail_unless (gst_value_get_int_range_min (tmp) == 10);
959 fail_unless (gst_value_get_int_range_max (tmp) == 11);
960 tmp = gst_value_list_get_value (&dest, 1);
961 fail_unless (G_VALUE_HOLDS_INT (tmp) == TRUE);
962 fail_unless (g_value_get_int (tmp) == 30);
963 g_value_unset (&dest);
965 ret = gst_value_subtract (&dest, &src2, &src1);
966 fail_unless (ret == FALSE);
967 g_value_unset (&src1);
968 g_value_unset (&src2);
973 GST_START_TEST (test_value_subtract_double)
983 g_value_init (&src1, G_TYPE_DOUBLE);
984 g_value_set_double (&src1, 10.0);
985 g_value_init (&src2, G_TYPE_DOUBLE);
986 g_value_set_double (&src2, 20.0);
987 /* subtract as in sets, result is 10 */
988 ret = gst_value_subtract (&dest, &src1, &src2);
989 fail_unless (ret == TRUE);
990 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
991 g_value_unset (&dest);
993 /* same values, yields empty set */
994 ret = gst_value_subtract (&dest, &src1, &src1);
995 fail_unless (ret == FALSE);
996 g_value_unset (&src1);
997 g_value_unset (&src2);
999 /* double <-> double_range
1002 /* would yield an empty set */
1003 g_value_init (&src1, G_TYPE_DOUBLE);
1004 g_value_set_double (&src1, 10.0);
1005 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1006 gst_value_set_double_range (&src2, 0.0, 20.0);
1007 ret = gst_value_subtract (&dest, &src1, &src2);
1008 fail_unless (ret == FALSE);
1010 /* and the other way around, we cannot create open ranges
1011 * so the result is the range again */
1012 ret = gst_value_subtract (&dest, &src2, &src1);
1013 fail_unless (ret == TRUE);
1014 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1015 fail_unless (gst_value_get_double_range_min (&dest) == 0.0);
1016 fail_unless (gst_value_get_double_range_max (&dest) == 20.0);
1017 g_value_unset (&dest);
1018 g_value_unset (&src1);
1019 g_value_unset (&src2);
1021 /* border case 1, empty set */
1022 g_value_init (&src1, G_TYPE_DOUBLE);
1023 g_value_set_double (&src1, 10.0);
1024 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1025 gst_value_set_double_range (&src2, 10.0, 20.0);
1026 ret = gst_value_subtract (&dest, &src1, &src2);
1027 fail_unless (ret == FALSE);
1029 /* and the other way around, should keep same range as
1030 * we don't have open ranges. */
1031 ret = gst_value_subtract (&dest, &src2, &src1);
1032 fail_unless (ret == TRUE);
1033 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1034 fail_unless (gst_value_get_double_range_min (&dest) == 10.0);
1035 fail_unless (gst_value_get_double_range_max (&dest) == 20.0);
1036 g_value_unset (&dest);
1037 g_value_unset (&src1);
1038 g_value_unset (&src2);
1040 /* border case 2, empty set */
1041 g_value_init (&src1, G_TYPE_DOUBLE);
1042 g_value_set_double (&src1, 20.0);
1043 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1044 gst_value_set_double_range (&src2, 10.0, 20.0);
1045 ret = gst_value_subtract (&dest, &src1, &src2);
1046 fail_unless (ret == FALSE);
1048 /* and the other way around, should keep same range as
1049 * we don't have open ranges. */
1050 ret = gst_value_subtract (&dest, &src2, &src1);
1051 fail_unless (ret == TRUE);
1052 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1053 fail_unless (gst_value_get_double_range_min (&dest) == 10.0);
1054 fail_unless (gst_value_get_double_range_max (&dest) == 20.0);
1055 g_value_unset (&dest);
1056 g_value_unset (&src1);
1057 g_value_unset (&src2);
1059 /* case 3, valid set */
1060 g_value_init (&src1, G_TYPE_DOUBLE);
1061 g_value_set_double (&src1, 0.0);
1062 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1063 gst_value_set_double_range (&src2, 10.0, 20.0);
1064 ret = gst_value_subtract (&dest, &src1, &src2);
1065 fail_unless (ret == TRUE);
1066 fail_unless (G_VALUE_HOLDS_DOUBLE (&dest) == TRUE);
1067 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
1068 g_value_unset (&dest);
1070 /* and the other way around, should keep the range. */
1071 ret = gst_value_subtract (&dest, &src2, &src1);
1072 fail_unless (ret == TRUE);
1073 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1074 fail_unless (gst_value_get_double_range_min (&dest) == 10.0);
1075 fail_unless (gst_value_get_double_range_max (&dest) == 20.0);
1076 g_value_unset (&dest);
1077 g_value_unset (&src1);
1078 g_value_unset (&src2);
1080 /* double_range <-> double_range
1083 /* same range, empty set */
1084 g_value_init (&src1, GST_TYPE_DOUBLE_RANGE);
1085 gst_value_set_double_range (&src1, 10.0, 20.0);
1086 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1087 gst_value_set_double_range (&src2, 10.0, 20.0);
1088 ret = gst_value_subtract (&dest, &src1, &src2);
1089 fail_unless (ret == FALSE);
1090 ret = gst_value_subtract (&dest, &src2, &src1);
1091 fail_unless (ret == FALSE);
1092 g_value_unset (&src1);
1093 g_value_unset (&src2);
1095 /* non overlapping ranges */
1096 g_value_init (&src1, GST_TYPE_DOUBLE_RANGE);
1097 gst_value_set_double_range (&src1, 10.0, 20.0);
1098 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1099 gst_value_set_double_range (&src2, 30.0, 40.0);
1100 ret = gst_value_subtract (&dest, &src1, &src2);
1101 fail_unless (ret == TRUE);
1102 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1103 fail_unless (gst_value_get_double_range_min (&dest) == 10.0);
1104 fail_unless (gst_value_get_double_range_max (&dest) == 20.0);
1105 g_value_unset (&dest);
1107 ret = gst_value_subtract (&dest, &src2, &src1);
1108 fail_unless (ret == TRUE);
1109 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1110 fail_unless (gst_value_get_double_range_min (&dest) == 30.0);
1111 fail_unless (gst_value_get_double_range_max (&dest) == 40.0);
1112 g_value_unset (&dest);
1113 g_value_unset (&src1);
1114 g_value_unset (&src2);
1116 /* completely overlapping ranges */
1117 g_value_init (&src1, GST_TYPE_DOUBLE_RANGE);
1118 gst_value_set_double_range (&src1, 10.0, 20.0);
1119 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1120 gst_value_set_double_range (&src2, 10.0, 30.0);
1121 ret = gst_value_subtract (&dest, &src1, &src2);
1122 fail_unless (ret == FALSE);
1124 ret = gst_value_subtract (&dest, &src2, &src1);
1125 fail_unless (ret == TRUE);
1126 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1127 fail_unless (gst_value_get_double_range_min (&dest) == 20.0);
1128 fail_unless (gst_value_get_double_range_max (&dest) == 30.0);
1129 g_value_unset (&dest);
1130 g_value_unset (&src1);
1131 g_value_unset (&src2);
1133 /* partially overlapping ranges */
1134 g_value_init (&src1, GST_TYPE_DOUBLE_RANGE);
1135 gst_value_set_double_range (&src1, 10.0, 20.0);
1136 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1137 gst_value_set_double_range (&src2, 15.0, 30.0);
1138 ret = gst_value_subtract (&dest, &src1, &src2);
1139 fail_unless (ret == TRUE);
1140 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1141 fail_unless (gst_value_get_double_range_min (&dest) == 10.0);
1142 fail_unless (gst_value_get_double_range_max (&dest) == 15.0);
1143 g_value_unset (&dest);
1145 ret = gst_value_subtract (&dest, &src2, &src1);
1146 fail_unless (ret == TRUE);
1147 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (&dest) == TRUE);
1148 fail_unless (gst_value_get_double_range_min (&dest) == 20.0);
1149 fail_unless (gst_value_get_double_range_max (&dest) == 30.0);
1150 g_value_unset (&dest);
1151 g_value_unset (&src1);
1152 g_value_unset (&src2);
1154 /* create a hole { double_range, double_range } */
1155 g_value_init (&src1, GST_TYPE_DOUBLE_RANGE);
1156 gst_value_set_double_range (&src1, 10.0, 30.0);
1157 g_value_init (&src2, GST_TYPE_DOUBLE_RANGE);
1158 gst_value_set_double_range (&src2, 15.0, 20.0);
1159 ret = gst_value_subtract (&dest, &src1, &src2);
1160 fail_unless (ret == TRUE);
1161 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
1162 tmp = gst_value_list_get_value (&dest, 0);
1163 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (tmp) == TRUE);
1164 fail_unless (gst_value_get_double_range_min (tmp) == 10.0);
1165 fail_unless (gst_value_get_double_range_max (tmp) == 15.0);
1166 tmp = gst_value_list_get_value (&dest, 1);
1167 fail_unless (GST_VALUE_HOLDS_DOUBLE_RANGE (tmp) == TRUE);
1168 fail_unless (gst_value_get_double_range_min (tmp) == 20.0);
1169 fail_unless (gst_value_get_double_range_max (tmp) == 30.0);
1170 g_value_unset (&dest);
1172 ret = gst_value_subtract (&dest, &src2, &src1);
1173 fail_unless (ret == FALSE);
1174 g_value_unset (&src1);
1175 g_value_unset (&src2);
1180 /* Test arithmetic subtraction of fractions */
1181 GST_START_TEST (test_value_subtract_fraction)
1183 GValue result = { 0 };
1184 GValue src1 = { 0 };
1185 GValue src2 = { 0 };
1187 /* Subtract 1/4 from 1/2 */
1188 g_value_init (&src1, GST_TYPE_FRACTION);
1189 g_value_init (&src2, GST_TYPE_FRACTION);
1190 g_value_init (&result, GST_TYPE_FRACTION);
1191 gst_value_set_fraction (&src1, 1, 2);
1192 gst_value_set_fraction (&src2, 1, 4);
1193 fail_unless (gst_value_fraction_subtract (&result, &src1, &src2) == TRUE);
1194 fail_unless (gst_value_get_fraction_numerator (&result) == 1);
1195 fail_unless (gst_value_get_fraction_denominator (&result) == 4);
1197 g_value_unset (&src1);
1198 g_value_unset (&src2);
1199 g_value_unset (&result);
1201 /* Subtract 1/12 from 7/8 */
1202 g_value_init (&src1, GST_TYPE_FRACTION);
1203 g_value_init (&src2, GST_TYPE_FRACTION);
1204 g_value_init (&result, GST_TYPE_FRACTION);
1205 gst_value_set_fraction (&src1, 7, 8);
1206 gst_value_set_fraction (&src2, 1, 12);
1207 fail_unless (gst_value_fraction_subtract (&result, &src1, &src2) == TRUE);
1208 fail_unless (gst_value_get_fraction_numerator (&result) == 19);
1209 fail_unless (gst_value_get_fraction_denominator (&result) == 24);
1211 g_value_unset (&src1);
1212 g_value_unset (&src2);
1213 g_value_unset (&result);
1215 /* Subtract 12/13 from 4/3 */
1216 g_value_init (&src1, GST_TYPE_FRACTION);
1217 g_value_init (&src2, GST_TYPE_FRACTION);
1218 g_value_init (&result, GST_TYPE_FRACTION);
1219 gst_value_set_fraction (&src1, 4, 3);
1220 gst_value_set_fraction (&src2, 12, 13);
1221 fail_unless (gst_value_fraction_subtract (&result, &src1, &src2) == TRUE);
1222 fail_unless (gst_value_get_fraction_numerator (&result) == 16);
1223 fail_unless (gst_value_get_fraction_denominator (&result) == 39);
1225 g_value_unset (&src1);
1226 g_value_unset (&src2);
1227 g_value_unset (&result);
1229 /* Subtract 1/12 from 7/8 */
1234 /* Test set subtraction operations on fraction ranges */
1235 GST_START_TEST (test_value_subtract_fraction_range)
1237 GValue dest = { 0 };
1238 GValue src1 = { 0 };
1239 GValue src2 = { 0 };
1244 /* Value for tests */
1245 g_value_init (&cmp, GST_TYPE_FRACTION);
1247 /* fraction <-> fraction
1249 g_value_init (&src1, GST_TYPE_FRACTION);
1250 gst_value_set_fraction (&src1, 10, 1);
1251 g_value_init (&src2, GST_TYPE_FRACTION);
1252 gst_value_set_fraction (&src2, 20, 1);
1253 gst_value_set_fraction (&src1, 10, 1);
1255 /* subtract as in sets, result is 10 */
1256 ret = gst_value_subtract (&dest, &src1, &src2);
1257 fail_unless (ret == TRUE);
1258 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
1259 g_value_unset (&dest);
1261 /* same values, yields empty set */
1262 ret = gst_value_subtract (&dest, &src1, &src1);
1263 fail_unless (ret == FALSE);
1264 g_value_unset (&src1);
1265 g_value_unset (&src2);
1267 /* fraction <-> fraction_range
1270 /* would yield an empty set */
1271 g_value_init (&src1, GST_TYPE_FRACTION);
1272 gst_value_set_fraction (&src1, 10, 1);
1273 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1274 gst_value_set_fraction_range_full (&src2, 0, 1, 20, 1);
1275 ret = gst_value_subtract (&dest, &src1, &src2);
1276 fail_unless (ret == FALSE);
1278 /* and the other way around, we cannot create open ranges
1279 * so the result is the range again */
1280 ret = gst_value_subtract (&dest, &src2, &src1);
1281 fail_unless (ret == TRUE);
1282 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1283 gst_value_set_fraction (&cmp, 0, 1);
1284 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1285 &cmp) == GST_VALUE_EQUAL);
1286 gst_value_set_fraction (&cmp, 20, 1);
1287 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1288 &cmp) == GST_VALUE_EQUAL);
1289 g_value_unset (&dest);
1290 g_value_unset (&src1);
1291 g_value_unset (&src2);
1293 /* border case 1, empty set */
1294 g_value_init (&src1, GST_TYPE_FRACTION);
1295 gst_value_set_fraction (&src1, 10, 1);
1296 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1297 gst_value_set_fraction_range_full (&src2, 10, 1, 20, 1);
1298 ret = gst_value_subtract (&dest, &src1, &src2);
1299 fail_unless (ret == FALSE);
1301 /* and the other way around, should keep same range as
1302 * we don't have open ranges. */
1303 ret = gst_value_subtract (&dest, &src2, &src1);
1304 fail_unless (ret == TRUE);
1305 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1306 gst_value_set_fraction (&cmp, 10, 1);
1307 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1308 &cmp) == GST_VALUE_EQUAL);
1309 gst_value_set_fraction (&cmp, 20, 1);
1310 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1311 &cmp) == GST_VALUE_EQUAL);
1312 g_value_unset (&dest);
1313 g_value_unset (&src1);
1314 g_value_unset (&src2);
1316 /* case 2, valid set */
1317 g_value_init (&src1, GST_TYPE_FRACTION);
1318 gst_value_set_fraction (&src1, 0, 1);
1319 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1320 gst_value_set_fraction_range_full (&src2, 10, 1, 20, 1);
1321 ret = gst_value_subtract (&dest, &src1, &src2);
1322 fail_unless (ret == TRUE);
1323 fail_unless (GST_VALUE_HOLDS_FRACTION (&dest) == TRUE);
1324 fail_unless (gst_value_compare (&dest, &src1) == GST_VALUE_EQUAL);
1325 g_value_unset (&dest);
1327 /* and the other way around, should keep the range. */
1328 ret = gst_value_subtract (&dest, &src2, &src1);
1329 fail_unless (ret == TRUE);
1330 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1331 fail_unless (gst_value_compare (&dest, &src2) == GST_VALUE_EQUAL);
1332 g_value_unset (&dest);
1333 g_value_unset (&src1);
1334 g_value_unset (&src2);
1336 /* fraction_range <-> fraction_range
1339 /* same range, empty set */
1340 g_value_init (&src1, GST_TYPE_FRACTION_RANGE);
1341 gst_value_set_fraction_range_full (&src1, 10, 2, 20, 2);
1342 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1343 gst_value_set_fraction_range_full (&src2, 10, 2, 20, 2);
1344 ret = gst_value_subtract (&dest, &src1, &src2);
1345 fail_unless (ret == FALSE);
1346 ret = gst_value_subtract (&dest, &src2, &src1);
1347 fail_unless (ret == FALSE);
1348 g_value_unset (&src1);
1349 g_value_unset (&src2);
1351 /* non overlapping ranges */
1352 g_value_init (&src1, GST_TYPE_FRACTION_RANGE);
1353 gst_value_set_fraction_range_full (&src1, 10, 2, 10, 1);
1354 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1355 gst_value_set_fraction_range_full (&src2, 30, 2, 40, 2);
1356 ret = gst_value_subtract (&dest, &src1, &src2);
1357 fail_unless (ret == TRUE);
1358 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1359 gst_value_set_fraction (&cmp, 5, 1);
1360 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1361 &cmp) == GST_VALUE_EQUAL);
1362 gst_value_set_fraction (&cmp, 10, 1);
1363 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1364 &cmp) == GST_VALUE_EQUAL);
1366 g_value_unset (&dest);
1368 ret = gst_value_subtract (&dest, &src2, &src1);
1369 fail_unless (ret == TRUE);
1370 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1371 gst_value_set_fraction (&cmp, 15, 1);
1372 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1373 &cmp) == GST_VALUE_EQUAL);
1374 gst_value_set_fraction (&cmp, 20, 1);
1375 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1376 &cmp) == GST_VALUE_EQUAL);
1377 g_value_unset (&dest);
1378 g_value_unset (&src1);
1379 g_value_unset (&src2);
1381 /* completely overlapping ranges */
1382 g_value_init (&src1, GST_TYPE_FRACTION_RANGE);
1383 gst_value_set_fraction_range_full (&src1, 10, 1, 20, 1);
1384 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1385 gst_value_set_fraction_range_full (&src2, 10, 1, 30, 1);
1386 ret = gst_value_subtract (&dest, &src1, &src2);
1387 fail_unless (ret == FALSE);
1389 ret = gst_value_subtract (&dest, &src2, &src1);
1390 fail_unless (ret == TRUE);
1391 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1392 gst_value_set_fraction (&cmp, 20, 1);
1393 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1394 &cmp) == GST_VALUE_EQUAL);
1395 gst_value_set_fraction (&cmp, 30, 1);
1396 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1397 &cmp) == GST_VALUE_EQUAL);
1398 g_value_unset (&dest);
1399 g_value_unset (&src1);
1400 g_value_unset (&src2);
1402 /* partially overlapping ranges */
1403 g_value_init (&src1, GST_TYPE_FRACTION_RANGE);
1404 gst_value_set_fraction_range_full (&src1, 10, 1, 20, 1);
1405 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1406 gst_value_set_fraction_range_full (&src2, 15, 1, 30, 1);
1407 ret = gst_value_subtract (&dest, &src1, &src2);
1408 fail_unless (ret == TRUE);
1409 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1410 gst_value_set_fraction (&cmp, 10, 1);
1411 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1412 &cmp) == GST_VALUE_EQUAL);
1413 gst_value_set_fraction (&cmp, 15, 1);
1414 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1415 &cmp) == GST_VALUE_EQUAL);
1416 g_value_unset (&dest);
1419 ret = gst_value_subtract (&dest, &src2, &src1);
1420 fail_unless (ret == TRUE);
1421 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (&dest) == TRUE);
1422 gst_value_set_fraction (&cmp, 20, 1);
1423 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (&dest),
1424 &cmp) == GST_VALUE_EQUAL);
1425 gst_value_set_fraction (&cmp, 30, 1);
1426 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (&dest),
1427 &cmp) == GST_VALUE_EQUAL);
1428 g_value_unset (&dest);
1429 g_value_unset (&src1);
1430 g_value_unset (&src2);
1432 /* create a hole { double_range, double_range } */
1433 g_value_init (&src1, GST_TYPE_FRACTION_RANGE);
1434 gst_value_set_fraction_range_full (&src1, 10, 1, 30, 1);
1435 g_value_init (&src2, GST_TYPE_FRACTION_RANGE);
1436 gst_value_set_fraction_range_full (&src2, 15, 1, 20, 1);
1437 ret = gst_value_subtract (&dest, &src1, &src2);
1438 fail_unless (ret == TRUE);
1439 fail_unless (GST_VALUE_HOLDS_LIST (&dest) == TRUE);
1440 /* 1st list entry */
1441 tmp = gst_value_list_get_value (&dest, 0);
1442 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (tmp) == TRUE);
1443 gst_value_set_fraction (&cmp, 10, 1);
1444 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (tmp),
1445 &cmp) == GST_VALUE_EQUAL);
1446 gst_value_set_fraction (&cmp, 15, 1);
1447 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (tmp),
1448 &cmp) == GST_VALUE_EQUAL);
1449 /* 2nd list entry */
1450 tmp = gst_value_list_get_value (&dest, 1);
1451 fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (tmp) == TRUE);
1452 gst_value_set_fraction (&cmp, 20, 1);
1453 fail_unless (gst_value_compare (gst_value_get_fraction_range_min (tmp),
1454 &cmp) == GST_VALUE_EQUAL);
1455 gst_value_set_fraction (&cmp, 30, 1);
1456 fail_unless (gst_value_compare (gst_value_get_fraction_range_max (tmp),
1457 &cmp) == GST_VALUE_EQUAL);
1458 g_value_unset (&dest);
1460 ret = gst_value_subtract (&dest, &src2, &src1);
1461 fail_unless (ret == FALSE);
1462 g_value_unset (&src1);
1463 g_value_unset (&src2);
1465 g_value_unset (&cmp);
1470 GST_START_TEST (test_date)
1473 GDate *date, *date2;
1476 date = g_date_new_dmy (22, 9, 2005);
1478 s = gst_structure_new ("media/x-type", "SOME_DATE_TAG", GST_TYPE_DATE,
1481 fail_unless (gst_structure_has_field_typed (s, "SOME_DATE_TAG",
1483 fail_unless (gst_structure_get_date (s, "SOME_DATE_TAG", &date2));
1484 fail_unless (date2 != NULL);
1485 fail_unless (g_date_valid (date2));
1486 fail_unless (g_date_compare (date, date2) == 0);
1489 g_date_free (date2);
1493 str = gst_structure_to_string (s);
1494 gst_structure_free (s);
1497 fail_unless (g_str_equal (str,
1498 "media/x-type, SOME_DATE_TAG=(GstDate)2005-09-22;"));
1500 s = gst_structure_from_string (str, NULL);
1504 fail_unless (s != NULL);
1505 fail_unless (gst_structure_has_name (s, "media/x-type"));
1506 fail_unless (gst_structure_has_field_typed (s, "SOME_DATE_TAG",
1508 fail_unless (gst_structure_get_date (s, "SOME_DATE_TAG", &date));
1509 fail_unless (date != NULL);
1510 fail_unless (g_date_valid (date));
1511 fail_unless (g_date_get_day (date) == 22);
1512 fail_unless (g_date_get_month (date) == 9);
1513 fail_unless (g_date_get_year (date) == 2005);
1517 str = gst_structure_to_string (s);
1518 gst_structure_free (s);
1521 fail_unless (g_str_equal (str,
1522 "media/x-type, SOME_DATE_TAG=(GstDate)2005-09-22;"));
1529 GST_START_TEST (test_fraction_range)
1531 GValue range = { 0, };
1532 GValue start = { 0, }, end = {
1534 GValue src = { 0, }, dest = {
1536 GValue range2 = { 0, };
1538 g_value_init (&range, GST_TYPE_FRACTION_RANGE);
1539 g_value_init (&range2, GST_TYPE_FRACTION_RANGE);
1540 g_value_init (&start, GST_TYPE_FRACTION);
1541 g_value_init (&end, GST_TYPE_FRACTION);
1542 g_value_init (&src, GST_TYPE_FRACTION);
1544 gst_value_set_fraction (&src, 1, 2);
1546 /* Check that a intersection of fraction & range = fraction */
1547 gst_value_set_fraction (&start, 1, 4);
1548 gst_value_set_fraction (&end, 2, 3);
1549 gst_value_set_fraction_range (&range, &start, &end);
1551 fail_unless (gst_value_intersect (&dest, &src, &range) == TRUE);
1552 fail_unless (G_VALUE_TYPE (&dest) == GST_TYPE_FRACTION);
1553 fail_unless (gst_value_compare (&dest, &src) == GST_VALUE_EQUAL);
1555 /* Check that a intersection selects the overlapping range */
1556 gst_value_set_fraction (&start, 1, 3);
1557 gst_value_set_fraction (&end, 2, 3);
1558 gst_value_set_fraction_range (&range2, &start, &end);
1559 g_value_unset (&dest);
1560 fail_unless (gst_value_intersect (&dest, &range, &range2) == TRUE);
1561 fail_unless (G_VALUE_TYPE (&dest) == GST_TYPE_FRACTION_RANGE);
1563 gst_value_set_fraction_range (&range2, &start, &end);
1564 fail_unless (gst_value_compare (&dest, &range2) == GST_VALUE_EQUAL);
1566 /* Check that non intersection ranges don't intersect */
1567 gst_value_set_fraction (&start, 4, 2);
1568 gst_value_set_fraction (&end, 5, 2);
1569 gst_value_set_fraction_range (&range2, &start, &end);
1570 g_value_unset (&dest);
1571 fail_unless (gst_value_intersect (&dest, &range, &range2) == FALSE);
1573 g_value_unset (&start);
1574 g_value_unset (&end);
1575 g_value_unset (&range);
1576 g_value_unset (&range2);
1577 g_value_unset (&src);
1582 GST_START_TEST (test_serialize_deserialize_format_enum)
1584 GstStructure *s, *s2;
1585 GstFormat foobar_fmt;
1586 gchar *str, *str2, *end = NULL;
1588 /* make sure custom formats are serialised properly as well */
1589 foobar_fmt = gst_format_register ("foobar", "GST_FORMAT_FOOBAR");
1590 fail_unless (foobar_fmt != GST_FORMAT_UNDEFINED);
1592 s = gst_structure_new ("foo/bar", "format1", GST_TYPE_FORMAT,
1593 GST_FORMAT_BYTES, "format2", GST_TYPE_FORMAT, GST_FORMAT_TIME,
1594 "format3", GST_TYPE_FORMAT, GST_FORMAT_DEFAULT, "format4",
1595 GST_TYPE_FORMAT, foobar_fmt, NULL);
1597 str = gst_structure_to_string (s);
1598 GST_LOG ("Got structure string '%s'", GST_STR_NULL (str));
1599 fail_unless (str != NULL);
1600 fail_unless (strstr (str, "TIME") != NULL);
1601 fail_unless (strstr (str, "BYTE") != NULL);
1602 fail_unless (strstr (str, "DEFAULT") != NULL);
1603 fail_unless (strstr (str, "FOOBAR") != NULL);
1605 s2 = gst_structure_from_string (str, &end);
1606 fail_unless (s2 != NULL);
1608 str2 = gst_structure_to_string (s2);
1609 fail_unless (str2 != NULL);
1611 fail_unless (g_str_equal (str, str2));
1615 gst_structure_free (s);
1616 gst_structure_free (s2);
1622 gst_value_suite (void)
1624 Suite *s = suite_create ("GstValue");
1625 TCase *tc_chain = tcase_create ("general");
1627 suite_add_tcase (s, tc_chain);
1628 tcase_add_test (tc_chain, test_deserialize_buffer);
1629 tcase_add_test (tc_chain, test_serialize_buffer);
1630 tcase_add_test (tc_chain, test_deserialize_gint);
1631 tcase_add_test (tc_chain, test_deserialize_gint_failures);
1632 tcase_add_test (tc_chain, test_deserialize_guint);
1633 tcase_add_test (tc_chain, test_deserialize_guint_failures);
1634 tcase_add_test (tc_chain, test_deserialize_gint64);
1635 tcase_add_test (tc_chain, test_deserialize_gstfraction);
1636 tcase_add_test (tc_chain, test_serialize_flags);
1637 tcase_add_test (tc_chain, test_deserialize_flags);
1638 tcase_add_test (tc_chain, test_serialize_deserialize_format_enum);
1639 tcase_add_test (tc_chain, test_string);
1640 tcase_add_test (tc_chain, test_deserialize_string);
1641 tcase_add_test (tc_chain, test_value_compare);
1642 tcase_add_test (tc_chain, test_value_intersect);
1643 tcase_add_test (tc_chain, test_value_subtract_int);
1644 tcase_add_test (tc_chain, test_value_subtract_double);
1645 tcase_add_test (tc_chain, test_value_subtract_fraction);
1646 tcase_add_test (tc_chain, test_value_subtract_fraction_range);
1647 tcase_add_test (tc_chain, test_date);
1648 tcase_add_test (tc_chain, test_fraction_range);
1653 GST_CHECK_MAIN (gst_value);