source: trunk/third/gstreamer/testsuite/caps/fraction-multiply-and-zero.c @ 21005

Revision 21005, 4.0 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.c: test for all GstFraction operations
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 <gst/gst.h>
24#include <glib.h>
25
26static void
27check_multiplication (int num1, int den1, int num2, int den2, int num_result,
28    int den_result)
29{
30  GValue value1 = { 0 };
31  GValue value2 = { 0 };
32  GValue value3 = { 0 };
33
34  g_value_init (&value1, GST_TYPE_FRACTION);
35  g_value_init (&value2, GST_TYPE_FRACTION);
36  g_value_init (&value3, GST_TYPE_FRACTION);
37
38  gst_value_set_fraction (&value1, num1, den1);
39  gst_value_set_fraction (&value2, num2, den2);
40  g_print ("%d/%d * %d/%d = ", num1, den1, num2, den2);
41  gst_value_fraction_multiply (&value3, &value1, &value2);
42  g_print ("%d/%d (should be %d/%d)\n",
43      gst_value_get_fraction_numerator (&value3),
44      gst_value_get_fraction_denominator (&value3), num_result, den_result);
45  g_assert (gst_value_get_fraction_numerator (&value3) == num_result);
46  g_assert (gst_value_get_fraction_denominator (&value3) == den_result);
47
48  g_value_unset (&value1);
49  g_value_unset (&value2);
50  g_value_unset (&value3);
51}
52
53static void
54check_equal (int num1, int den1, int num2, int den2)
55{
56  GValue value1 = { 0 };
57  GValue value2 = { 0 };
58
59  g_value_init (&value1, GST_TYPE_FRACTION);
60  g_value_init (&value2, GST_TYPE_FRACTION);
61
62  gst_value_set_fraction (&value1, num1, den1);
63  gst_value_set_fraction (&value2, num2, den2);
64  g_print ("%d/%d == %d/%d ? ", num1, den1, num2, den2);
65  g_assert (gst_value_compare (&value1, &value2) == GST_VALUE_EQUAL);
66  g_print ("yes\n");
67
68  g_value_unset (&value1);
69  g_value_unset (&value2);
70}
71
72static void
73zero_test (void)
74{
75  GValue value1 = { 0 };
76
77  g_value_init (&value1, GST_TYPE_FRACTION);
78
79  /* fractions are initialized at 0 */
80  g_assert (gst_value_get_fraction_numerator (&value1) == 0);
81  g_assert (gst_value_get_fraction_denominator (&value1) == 1);
82
83  /* every zero value is set to 0/1 */
84  gst_value_set_fraction (&value1, 0, 235);
85  g_assert (gst_value_get_fraction_numerator (&value1) == 0);
86  g_assert (gst_value_get_fraction_denominator (&value1) == 1);
87  gst_value_set_fraction (&value1, 0, -G_MAXINT);
88  g_assert (gst_value_get_fraction_numerator (&value1) == 0);
89  g_assert (gst_value_get_fraction_denominator (&value1) == 1);
90
91  g_value_unset (&value1);
92}
93
94int
95main (int argc, char *argv[])
96{
97  GValue value1 = { 0 };
98  GValue value2 = { 0 };
99  GValue value3 = { 0 };
100
101  gst_init (&argc, &argv);
102
103  g_value_init (&value1, GST_TYPE_FRACTION);
104  g_value_init (&value2, GST_TYPE_FRACTION);
105  g_value_init (&value3, GST_TYPE_FRACTION);
106
107  /*** zeroes ***/
108
109  /* basic zero tests */
110  zero_test ();
111
112  /* check all zeroes are zeroes */
113  check_equal (0, 1, 0, 12345);
114  check_equal (0, 1, 0, -1);
115
116  /* check multiplying with zeroes results in zeroes */
117  check_multiplication (0, 1, 17, 18, 0, 1);
118  check_multiplication (0, -13, -G_MAXINT, 2736, 0, 1);
119
120  /*** large numbers ***/
121
122  /* check multiplying large numbers works */
123  check_multiplication (G_MAXINT, 1, G_MAXINT - 1, G_MAXINT, G_MAXINT - 1, 1);
124  check_multiplication (-G_MAXINT, 1, -G_MAXINT + 1, -G_MAXINT, -G_MAXINT + 1,
125      1);
126  check_multiplication (G_MAXINT / 28, 459, -28, -G_MAXINT / 459,
127      G_MAXINT / 28 * 28, G_MAXINT / 459 * 459);
128  check_multiplication (3117 * 13, -17, 3117 * 17, 13, -3117 * 3117, 1);
129
130  return 0;
131}
Note: See TracBrowser for help on using the repository browser.