Annotation of OpenXM_contrib/gmp/tests/mpq/t-aors.c, Revision 1.1.1.1
1.1 ohara 1: /* Test mpq_add and mpq_sub.
2:
3: Copyright 2001 Free Software Foundation, Inc.
4:
5: This file is part of the GNU MP Library.
6:
7: The GNU MP Library is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or (at your
10: option) any later version.
11:
12: The GNU MP Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15: License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include "config.h"
23:
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27:
28: #include "gmp.h"
29: #include "gmp-impl.h"
30: #include "tests.h"
31:
32:
33: void
34: check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
35: {
36: mpq_t got;
37: int neg_x, neg_y, swap;
38:
39: mpq_init (got);
40:
41: MPQ_CHECK_FORMAT (want_add);
42: MPQ_CHECK_FORMAT (want_sub);
43: MPQ_CHECK_FORMAT (x);
44: MPQ_CHECK_FORMAT (y);
45:
46: for (swap = 0; swap <= 1; swap++)
47: {
48: for (neg_x = 0; neg_x <= 1; neg_x++)
49: {
50: for (neg_y = 0; neg_y <= 1; neg_y++)
51: {
52: mpq_add (got, x, y);
53: MPQ_CHECK_FORMAT (got);
54: if (! mpq_equal (got, want_add))
55: {
56: printf ("mpq_add wrong\n");
57: mpq_trace (" x ", x);
58: mpq_trace (" y ", y);
59: mpq_trace (" got ", got);
60: mpq_trace (" want", want_add);
61: abort ();
62: }
63:
64: mpq_sub (got, x, y);
65: MPQ_CHECK_FORMAT (got);
66: if (! mpq_equal (got, want_sub))
67: {
68: printf ("mpq_sub wrong\n");
69: mpq_trace (" x ", x);
70: mpq_trace (" y ", y);
71: mpq_trace (" got ", got);
72: mpq_trace (" want", want_sub);
73: abort ();
74: }
75:
76:
77: mpq_neg (y, y);
78: mpq_swap (want_add, want_sub);
79: }
80:
81: mpq_neg (x, x);
82: mpq_swap (want_add, want_sub);
83: mpq_neg (want_add, want_add);
84: mpq_neg (want_sub, want_sub);
85: }
86:
87: mpq_swap (x, y);
88: mpq_neg (want_sub, want_sub);
89: }
90:
91: mpq_clear (got);
92: }
93:
94:
95: void
96: check_data (void)
97: {
98: static const struct {
99: const char *x;
100: const char *y;
101: const char *want_add;
102: const char *want_sub;
103:
104: } data[] = {
105:
106: { "0", "0", "0", "0" },
107: { "1", "0", "1", "1" },
108: { "1", "1", "2", "0" },
109:
110: { "1/2", "1/2", "1", "0" },
111: { "5/6", "14/15", "53/30", "-1/10" },
112: };
113:
114: mpq_t x, y, want_add, want_sub;
115: int i;
116:
117: mpq_init (x);
118: mpq_init (y);
119: mpq_init (want_add);
120: mpq_init (want_sub);
121:
122: for (i = 0; i < numberof (data); i++)
123: {
124: mpq_set_str_or_abort (x, data[i].x, 0);
125: mpq_set_str_or_abort (y, data[i].y, 0);
126: mpq_set_str_or_abort (want_add, data[i].want_add, 0);
127: mpq_set_str_or_abort (want_sub, data[i].want_sub, 0);
128:
129: check_all (x, y, want_add, want_sub);
130: }
131:
132: mpq_clear (x);
133: mpq_clear (y);
134: mpq_clear (want_add);
135: mpq_clear (want_sub);
136: }
137:
138:
139: void
140: check_rand (void)
141: {
142: mpq_t x, y, want_add, want_sub;
143: int i;
144: gmp_randstate_ptr rands = RANDS;
145:
146: mpq_init (x);
147: mpq_init (y);
148: mpq_init (want_add);
149: mpq_init (want_sub);
150:
151: for (i = 0; i < 500; i++)
152: {
153: mpz_errandomb (mpq_numref(x), rands, 512L);
154: mpz_errandomb_nonzero (mpq_denref(x), rands, 512L);
155: mpq_canonicalize (x);
156:
157: mpz_errandomb (mpq_numref(y), rands, 512L);
158: mpz_errandomb_nonzero (mpq_denref(y), rands, 512L);
159: mpq_canonicalize (y);
160:
161: refmpq_add (want_add, x, y);
162: refmpq_sub (want_sub, x, y);
163:
164: check_all (x, y, want_add, want_sub);
165: }
166:
167: mpq_clear (x);
168: mpq_clear (y);
169: mpq_clear (want_add);
170: mpq_clear (want_sub);
171: }
172:
173:
174: int
175: main (void)
176: {
177: tests_start ();
178:
179: check_data ();
180: check_rand ();
181:
182: tests_end ();
183:
184: exit (0);
185: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>