gstreamer/tests/old/testsuite/caps/fraction-convert.c
Wim Taymans 36b41f4666 Added transform functions between double and fraction.
Original commit message from CVS:
* gst/gstvalue.c: (gst_value_transform_double_fraction),
(gst_value_transform_fraction_double), (_gst_value_initialize):
* testsuite/caps/Makefile.am:
* testsuite/caps/fraction-convert.c: (check_from_double_convert),
(check_from_fraction_convert), (transform_test), (main):
Added transform functions between double and fraction.
Added testcase to verify transforms
2004-07-27 16:45:30 +00:00

115 lines
3.7 KiB
C

/* GStreamer
*
* fraction-convert.c: test for GstFraction transform
*
* 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 <math.h>
#include <gst/gst.h>
#include <glib.h>
static void
check_from_double_convert (gdouble value, gint nom, gint denom, gdouble prec)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
gdouble check;
g_value_init (&value1, G_TYPE_DOUBLE);
g_value_init (&value2, GST_TYPE_FRACTION);
g_value_set_double (&value1, value);
g_value_transform (&value1, &value2);
g_print ("%s = %s\n",
gst_value_serialize (&value1), gst_value_serialize (&value2));
g_assert (gst_value_get_fraction_numerator (&value2) == nom);
g_assert (gst_value_get_fraction_denominator (&value2) == denom);
g_value_transform (&value2, &value1);
g_print ("%s = %s\n",
gst_value_serialize (&value2), gst_value_serialize (&value1));
check = g_value_get_double (&value1);
g_assert (fabs (value - check) <= prec);
}
static void
check_from_fraction_convert (gint nom, gint denom, gdouble prec)
{
GValue value1 = { 0 };
GValue value2 = { 0 };
gdouble value;
g_value_init (&value1, GST_TYPE_FRACTION);
g_value_init (&value2, G_TYPE_DOUBLE);
gst_value_set_fraction (&value1, nom, denom);
g_value_transform (&value1, &value2);
value = g_value_get_double (&value2);
g_assert (fabs (value - ((gdouble) nom) / denom) < prec);
g_print ("%s = %s, %2.50lf as double\n",
gst_value_serialize (&value1), gst_value_serialize (&value2), value);
g_value_transform (&value2, &value1);
g_print ("%s = %s\n",
gst_value_serialize (&value2), gst_value_serialize (&value1));
value = g_value_get_double (&value2);
g_assert (gst_value_get_fraction_numerator (&value1) == nom);
g_assert (gst_value_get_fraction_denominator (&value1) == denom);
g_value_unset (&value2);
g_value_unset (&value1);
}
static void
transform_test (void)
{
check_from_fraction_convert (30000, 1001, 1.0e-12);
check_from_fraction_convert (1, G_MAXINT, 1.0e-12);
check_from_fraction_convert (G_MAXINT, 1, 1.0e-12);
check_from_double_convert (0.0, 0, 1, 1.0e-12);
check_from_double_convert (1.0, 1, 1, 1.0e-12);
check_from_double_convert (-1.0, -1, 1, 1.0e-12);
check_from_double_convert (M_PI, 1881244168, 598818617, 1.0e-12);
check_from_double_convert (-M_PI, -1881244168, 598818617, 1.0e-12);
check_from_double_convert (G_MAXDOUBLE, G_MAXINT, 1, G_MAXDOUBLE);
check_from_double_convert (G_MINDOUBLE, 0, 1, G_MAXDOUBLE);
check_from_double_convert (-G_MAXDOUBLE, -G_MAXINT, 1, G_MAXDOUBLE);
check_from_double_convert (-G_MINDOUBLE, 0, 1, G_MAXDOUBLE);
check_from_double_convert (((gdouble) G_MAXINT) + 1, G_MAXINT, 1,
G_MAXDOUBLE);
check_from_double_convert (((gdouble) G_MININT) - 1, G_MININT + 1, 1,
G_MAXDOUBLE);
check_from_double_convert (G_MAXINT - 1, G_MAXINT - 1, 1, 0);
check_from_double_convert (G_MININT + 1, G_MININT + 1, 1, 0);
}
int
main (int argc, char *argv[])
{
gst_init (&argc, &argv);
transform_test ();
return 0;
}