source: trunk/third/gstreamer/testsuite/caps/fraction-convert.c @ 21005

Revision 21005, 4.4 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21004, which included commits to RCS files with non-trunk default branches.
Line 
1/* GStreamer
2 *
3 * fraction-convert.c: test for GstFraction transform
4 *
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
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#include <math.h>
24#include <gst/gst.h>
25#include <glib.h>
26
27static void
28check_from_double_convert (gdouble value, gint num, gint denom, gdouble prec)
29{
30  GValue value1 = { 0 };
31  GValue value2 = { 0 };
32  gdouble check;
33  gint res_num, res_denom;
34
35  g_value_init (&value1, G_TYPE_DOUBLE);
36  g_value_init (&value2, GST_TYPE_FRACTION);
37
38  g_value_set_double (&value1, value);
39  g_value_transform (&value1, &value2);
40  g_print ("%s = %s ? (expected: %d/%d )\n",
41      gst_value_serialize (&value1), gst_value_serialize (&value2), num, denom);
42
43  res_num = gst_value_get_fraction_numerator (&value2);
44  res_denom = gst_value_get_fraction_denominator (&value2);
45  if (res_num == num && res_denom == denom) {
46    g_print ("best conversion.\n");
47  } else {
48    if (fabs (value - res_num / (gdouble) res_denom) <= prec) {
49      g_print ("acceptable suboptimal conversion.\n");
50    } else {
51      g_print ("unacceptable suboptimal conversion.\n");
52      g_assert_not_reached ();
53    }
54  }
55  g_value_transform (&value2, &value1);
56  g_print ("%s = %s\n",
57      gst_value_serialize (&value2), gst_value_serialize (&value1));
58  check = g_value_get_double (&value1);
59  g_assert (fabs (value - check) <= prec);
60}
61
62static void
63check_from_fraction_convert (gint num, gint denom, gdouble prec)
64{
65  GValue value1 = { 0 };
66  GValue value2 = { 0 };
67  gdouble value;
68  gint res_num, res_denom;
69
70  g_value_init (&value1, GST_TYPE_FRACTION);
71  g_value_init (&value2, G_TYPE_DOUBLE);
72
73  gst_value_set_fraction (&value1, num, denom);
74  g_value_transform (&value1, &value2);
75
76  value = g_value_get_double (&value2);
77  g_assert (fabs (value - ((gdouble) num) / denom) < prec);
78
79  g_print ("%s = %s, %2.50lf as double\n",
80      gst_value_serialize (&value1), gst_value_serialize (&value2), value);
81
82  g_value_transform (&value2, &value1);
83  g_print ("%s = %s ? (expected: %d/%d )\n",
84      gst_value_serialize (&value2), gst_value_serialize (&value1), num, denom);
85  value = g_value_get_double (&value2);
86
87  res_num = gst_value_get_fraction_numerator (&value1);
88  res_denom = gst_value_get_fraction_denominator (&value1);
89  if (res_num == num && res_denom == denom) {
90    g_print ("best conversion.\n");
91  } else {
92    if (fabs (value - res_num / (gdouble) res_denom) <= prec) {
93      g_print ("acceptable suboptimal conversion.\n");
94    } else {
95      g_print ("unacceptable suboptimal conversion.\n");
96      g_assert_not_reached ();
97    }
98  }
99
100  g_value_unset (&value2);
101  g_value_unset (&value1);
102}
103
104static void
105transform_test (void)
106{
107  check_from_fraction_convert (30000, 1001, 1.0e-9);
108  check_from_fraction_convert (1, G_MAXINT, 1.0e-9);
109  check_from_fraction_convert (G_MAXINT, 1, 1.0e-9);
110
111  check_from_double_convert (0.0, 0, 1, 1.0e-9);
112  check_from_double_convert (1.0, 1, 1, 1.0e-9);
113  check_from_double_convert (-1.0, -1, 1, 1.0e-9);
114  check_from_double_convert (M_PI, 1881244168, 598818617, 1.0e-9);
115  check_from_double_convert (-M_PI, -1881244168, 598818617, 1.0e-9);
116
117  check_from_double_convert (G_MAXDOUBLE, G_MAXINT, 1, G_MAXDOUBLE);
118  check_from_double_convert (G_MINDOUBLE, 0, 1, G_MAXDOUBLE);
119  check_from_double_convert (-G_MAXDOUBLE, -G_MAXINT, 1, G_MAXDOUBLE);
120  check_from_double_convert (-G_MINDOUBLE, 0, 1, G_MAXDOUBLE);
121
122  check_from_double_convert (((gdouble) G_MAXINT) + 1, G_MAXINT, 1,
123      G_MAXDOUBLE);
124  check_from_double_convert (((gdouble) G_MININT) - 1, G_MININT + 1, 1,
125      G_MAXDOUBLE);
126
127  check_from_double_convert (G_MAXINT - 1, G_MAXINT - 1, 1, 0);
128  check_from_double_convert (G_MININT + 1, G_MININT + 1, 1, 0);
129}
130
131int
132main (int argc, char *argv[])
133{
134  gst_init (&argc, &argv);
135
136  transform_test ();
137
138  return 0;
139}
Note: See TracBrowser for help on using the repository browser.