gst/gstvalue.c: use ints and return ints, fractions only use ints, too, so this avoids accidently casting multiplicat...

Original commit message from CVS:
* gst/gstvalue.c: (gst_greatest_common_divisor):
use ints and return ints, fractions only use ints, too, so this
avoids accidently casting multiplications to unsigned
(gst_value_lcopy_fraction): it's ints, not uint32
(gst_value_set_fraction): disallow minint, multiplying and negation
are broken with it
(gst_value_fraction_multiply): fix to make large numbers work and get
rid of the assumption that the multiplication of two ints fits an
int64 - dunno if that's true for all systems
* testsuite/caps/Makefile.am:
* testsuite/caps/fraction-multiply-and-zero.c:
(check_multiplication), (check_equal), (zero_test), (main):
add tests for all the stuff above
* testsuite/caps/value_compare.c: (test1):
fix comment
* tests/.cvsignore:
* testsuite/caps/.cvsignore:
* testsuite/debug/.cvsignore:
* testsuite/dlopen/.cvsignore:
* testsuite/states/.cvsignore:
get up to date
This commit is contained in:
Benjamin Otte 2004-07-16 01:16:53 +00:00
parent 895f68b86b
commit ae7fb01363
17 changed files with 328 additions and 27 deletions

View file

@ -1,3 +1,27 @@
2004-07-16 Benjamin Otte <otte@gnome.org>
* gst/gstvalue.c: (gst_greatest_common_divisor):
use ints and return ints, fractions only use ints, too, so this
avoids accidently casting multiplications to unsigned
(gst_value_lcopy_fraction): it's ints, not uint32
(gst_value_set_fraction): disallow minint, multiplying and negation
are broken with it
(gst_value_fraction_multiply): fix to make large numbers work and get
rid of the assumption that the multiplication of two ints fits an
int64 - dunno if that's true for all systems
* testsuite/caps/Makefile.am:
* testsuite/caps/fraction-multiply-and-zero.c:
(check_multiplication), (check_equal), (zero_test), (main):
add tests for all the stuff above
* testsuite/caps/value_compare.c: (test1):
fix comment
* tests/.cvsignore:
* testsuite/caps/.cvsignore:
* testsuite/debug/.cvsignore:
* testsuite/dlopen/.cvsignore:
* testsuite/states/.cvsignore:
get up to date
2004-07-16 Zaheer Abbas Merali <zaheerabbas at merali dot org>
* docs/manual/bins-api.xml:

View file

@ -2298,8 +2298,8 @@ gst_type_is_fixed (GType type)
/* Finds the greatest common divisor.
* Returns 1 if none other found.
* This is Euclid's algorithm. */
static guint
gst_greatest_common_divisor (gint64 a, gint64 b)
static gint
gst_greatest_common_divisor (gint a, gint b)
{
while (b != 0) {
int temp = a;
@ -2308,9 +2308,7 @@ gst_greatest_common_divisor (gint64 a, gint64 b)
b = temp % b;
}
g_return_val_if_fail (a < G_MAXINT, 1);
g_return_val_if_fail (a > G_MININT, 1);
return ABS ((gint) a);
return ABS (a);
}
static void
@ -2341,8 +2339,8 @@ static gchar *
gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
guint32 *numerator = collect_values[0].v_pointer;
guint32 *denominator = collect_values[1].v_pointer;
gint *numerator = collect_values[0].v_pointer;
gint *denominator = collect_values[1].v_pointer;
if (!numerator)
return g_strdup_printf ("numerator for `%s' passed as NULL",
@ -2370,9 +2368,12 @@ gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
void
gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
{
gint gcd = 0;
g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
g_return_if_fail (denominator != 0);
gint gcd = 0;
g_return_if_fail (denominator >= -G_MAXINT);
g_return_if_fail (numerator >= -G_MAXINT);
/* normalize sign */
if (denominator < 0) {
@ -2437,10 +2438,7 @@ gboolean
gst_value_fraction_multiply (GValue * product, const GValue * factor1,
const GValue * factor2)
{
gint64 n, d;
guint gcd;
gint n1, n2, d1, d2;
gint gcd, n1, n2, d1, d2;
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
@ -2450,19 +2448,17 @@ gst_value_fraction_multiply (GValue * product, const GValue * factor1,
d1 = factor1->data[1].v_int;
d2 = factor2->data[1].v_int;
n = n1 * n2;
d = d1 * d2;
gcd = gst_greatest_common_divisor (n1, d2);
n1 /= gcd;
d2 /= gcd;
gcd = gst_greatest_common_divisor (n2, d1);
n2 /= gcd;
d1 /= gcd;
gcd = gst_greatest_common_divisor (n, d);
n /= gcd;
d /= gcd;
g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
g_return_val_if_fail (n < G_MAXINT, FALSE);
g_return_val_if_fail (n > G_MININT, FALSE);
g_return_val_if_fail (d < G_MAXINT, FALSE);
g_return_val_if_fail (d > G_MININT, FALSE);
gst_value_set_fraction (product, n, d);
gst_value_set_fraction (product, n1 * n2, d1 * d2);
return TRUE;
}

1
tests/.gitignore vendored
View file

@ -38,6 +38,7 @@ faketest
events
lat
mass_elements
spidey_bench
*.bb
*.bbg
*.da

View file

@ -13,12 +13,16 @@ app_fixate
audioscale
caps
compatibility
erathostenes
deserialize
eratosthenes
filtercaps
fixed
fraction-multiply-and-zero
intersect2
intersection
normalisation
random
renegotiate
union
simplify
sets

View file

@ -10,6 +10,7 @@ tests_pass = \
union \
string-conversions \
fixed \
fraction-multiply-and-zero \
intersect2 \
caps \
value_compare \

View file

@ -0,0 +1,131 @@
/* GStreamer
*
* fraction.c: test for all GstFraction operations
*
* Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gst/gst.h>
#include <glib.h>
static void
check_multiplication (int num1, int den1, int num2, int den2, int num_result,
int den_result)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
GValue value3 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
g_value_init (&value3, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, num1, den1);
gst_value_set_fraction (&value2, num2, den2);
g_print ("%d/%d * %d/%d = ", num1, den1, num2, den2);
gst_value_fraction_multiply (&value3, &value1, &value2);
g_print ("%d/%d (should be %d/%d)\n",
gst_value_get_fraction_numerator (&value3),
gst_value_get_fraction_denominator (&value3), num_result, den_result);
g_assert (gst_value_get_fraction_numerator (&value3) == num_result);
g_assert (gst_value_get_fraction_denominator (&value3) == den_result);
g_value_unset (&value1);
g_value_unset (&value2);
g_value_unset (&value3);
}
static void
check_equal (int num1, int den1, int num2, int den2)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, num1, den1);
gst_value_set_fraction (&value2, num2, den2);
g_print ("%d/%d == %d/%d ? ", num1, den1, num2, den2);
g_assert (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL);
g_print ("yes\n");
g_value_unset (&value1);
g_value_unset (&value2);
}
static void
zero_test (void)
{
GValue value1 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
/* fractions are initialized at 0 */
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
/* every zero value is set to 0/1 */
gst_value_set_fraction (&value1, 0, 235);
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
gst_value_set_fraction (&value1, 0, -G_MAXINT);
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
g_value_unset (&value1);
}
int
main (int argc, char *argv[])
{
GValue value1 = { 0 };
GValue value2 = { 0 };
GValue value3 = { 0 };
gst_init (&argc, &argv);
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
g_value_init (&value3, GST_TYPE_FRACTION);
/*** zeroes ***/
/* basic zero tests */
zero_test ();
/* check all zeroes are zeroes */
check_equal (0, 1, 0, 12345);
check_equal (0, 1, 0, -1);
/* check multiplying with zeroes results in zeroes */
check_multiplication (0, 1, 17, 18, 0, 1);
check_multiplication (0, -13, -G_MAXINT, 2736, 0, 1);
/*** large numbers ***/
/* check multiplying large numbers works */
check_multiplication (G_MAXINT, 1, G_MAXINT - 1, G_MAXINT, G_MAXINT - 1, 1);
check_multiplication (-G_MAXINT, 1, -G_MAXINT + 1, -G_MAXINT, -G_MAXINT + 1,
1);
check_multiplication (G_MAXINT / 28, 459, -28, -G_MAXINT / 459,
G_MAXINT / 28 * 28, G_MAXINT / 459 * 459);
check_multiplication (3117 * 13, -17, 3117 * 17, 13, -3117 * 3117, 1);
return 0;
}

View file

@ -72,7 +72,7 @@ test1 (void)
g_value_unset (&value1);
g_value_unset (&value2);
/* comparing 10/100 with 20/2000 */
/* comparing 10/100 with 200/2000 */
g_value_init (&value1, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, 10, 100);
g_value_init (&value2, GST_TYPE_FRACTION);

View file

@ -1,4 +1,5 @@
category
commandline
global
output
printf_extension

1
tests/old/testsuite/dlopen/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
dlopen_gst

View file

@ -5,4 +5,6 @@ Makefile.in
*.la
.deps
.libs
locked
parent

View file

@ -13,12 +13,16 @@ app_fixate
audioscale
caps
compatibility
erathostenes
deserialize
eratosthenes
filtercaps
fixed
fraction-multiply-and-zero
intersect2
intersection
normalisation
random
renegotiate
union
simplify
sets

View file

@ -10,6 +10,7 @@ tests_pass = \
union \
string-conversions \
fixed \
fraction-multiply-and-zero \
intersect2 \
caps \
value_compare \

View file

@ -0,0 +1,131 @@
/* GStreamer
*
* fraction.c: test for all GstFraction operations
*
* Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gst/gst.h>
#include <glib.h>
static void
check_multiplication (int num1, int den1, int num2, int den2, int num_result,
int den_result)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
GValue value3 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
g_value_init (&value3, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, num1, den1);
gst_value_set_fraction (&value2, num2, den2);
g_print ("%d/%d * %d/%d = ", num1, den1, num2, den2);
gst_value_fraction_multiply (&value3, &value1, &value2);
g_print ("%d/%d (should be %d/%d)\n",
gst_value_get_fraction_numerator (&value3),
gst_value_get_fraction_denominator (&value3), num_result, den_result);
g_assert (gst_value_get_fraction_numerator (&value3) == num_result);
g_assert (gst_value_get_fraction_denominator (&value3) == den_result);
g_value_unset (&value1);
g_value_unset (&value2);
g_value_unset (&value3);
}
static void
check_equal (int num1, int den1, int num2, int den2)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, num1, den1);
gst_value_set_fraction (&value2, num2, den2);
g_print ("%d/%d == %d/%d ? ", num1, den1, num2, den2);
g_assert (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL);
g_print ("yes\n");
g_value_unset (&value1);
g_value_unset (&value2);
}
static void
zero_test (void)
{
GValue value1 = { 0 };
g_value_init (&value1, GST_TYPE_FRACTION);
/* fractions are initialized at 0 */
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
/* every zero value is set to 0/1 */
gst_value_set_fraction (&value1, 0, 235);
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
gst_value_set_fraction (&value1, 0, -G_MAXINT);
g_assert (gst_value_get_fraction_numerator (&value1) == 0);
g_assert (gst_value_get_fraction_denominator (&value1) == 1);
g_value_unset (&value1);
}
int
main (int argc, char *argv[])
{
GValue value1 = { 0 };
GValue value2 = { 0 };
GValue value3 = { 0 };
gst_init (&argc, &argv);
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, GST_TYPE_FRACTION);
g_value_init (&value3, GST_TYPE_FRACTION);
/*** zeroes ***/
/* basic zero tests */
zero_test ();
/* check all zeroes are zeroes */
check_equal (0, 1, 0, 12345);
check_equal (0, 1, 0, -1);
/* check multiplying with zeroes results in zeroes */
check_multiplication (0, 1, 17, 18, 0, 1);
check_multiplication (0, -13, -G_MAXINT, 2736, 0, 1);
/*** large numbers ***/
/* check multiplying large numbers works */
check_multiplication (G_MAXINT, 1, G_MAXINT - 1, G_MAXINT, G_MAXINT - 1, 1);
check_multiplication (-G_MAXINT, 1, -G_MAXINT + 1, -G_MAXINT, -G_MAXINT + 1,
1);
check_multiplication (G_MAXINT / 28, 459, -28, -G_MAXINT / 459,
G_MAXINT / 28 * 28, G_MAXINT / 459 * 459);
check_multiplication (3117 * 13, -17, 3117 * 17, 13, -3117 * 3117, 1);
return 0;
}

View file

@ -72,7 +72,7 @@ test1 (void)
g_value_unset (&value1);
g_value_unset (&value2);
/* comparing 10/100 with 20/2000 */
/* comparing 10/100 with 200/2000 */
g_value_init (&value1, GST_TYPE_FRACTION);
gst_value_set_fraction (&value1, 10, 100);
g_value_init (&value2, GST_TYPE_FRACTION);

View file

@ -1,4 +1,5 @@
category
commandline
global
output
printf_extension

1
testsuite/dlopen/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
dlopen_gst

View file

@ -5,4 +5,6 @@ Makefile.in
*.la
.deps
.libs
locked
parent