1
/* Test mpq_add and mpq_sub.
3
Copyright 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
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.
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.
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. */
34
check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
37
int neg_x, neg_y, swap;
41
MPQ_CHECK_FORMAT (want_add);
42
MPQ_CHECK_FORMAT (want_sub);
46
for (swap = 0; swap <= 1; swap++)
48
for (neg_x = 0; neg_x <= 1; neg_x++)
50
for (neg_y = 0; neg_y <= 1; neg_y++)
53
MPQ_CHECK_FORMAT (got);
54
if (! mpq_equal (got, want_add))
56
printf ("mpq_add wrong\n");
59
mpq_trace (" got ", got);
60
mpq_trace (" want", want_add);
65
MPQ_CHECK_FORMAT (got);
66
if (! mpq_equal (got, want_sub))
68
printf ("mpq_sub wrong\n");
71
mpq_trace (" got ", got);
72
mpq_trace (" want", want_sub);
78
mpq_swap (want_add, want_sub);
82
mpq_swap (want_add, want_sub);
83
mpq_neg (want_add, want_add);
84
mpq_neg (want_sub, want_sub);
88
mpq_neg (want_sub, want_sub);
101
const char *want_add;
102
const char *want_sub;
106
{ "0", "0", "0", "0" },
107
{ "1", "0", "1", "1" },
108
{ "1", "1", "2", "0" },
110
{ "1/2", "1/2", "1", "0" },
111
{ "5/6", "14/15", "53/30", "-1/10" },
114
mpq_t x, y, want_add, want_sub;
122
for (i = 0; i < numberof (data); i++)
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);
129
check_all (x, y, want_add, want_sub);
134
mpq_clear (want_add);
135
mpq_clear (want_sub);
142
mpq_t x, y, want_add, want_sub;
144
gmp_randstate_ptr rands = RANDS;
151
for (i = 0; i < 500; i++)
153
mpz_errandomb (mpq_numref(x), rands, 512L);
154
mpz_errandomb_nonzero (mpq_denref(x), rands, 512L);
155
mpq_canonicalize (x);
157
mpz_errandomb (mpq_numref(y), rands, 512L);
158
mpz_errandomb_nonzero (mpq_denref(y), rands, 512L);
159
mpq_canonicalize (y);
161
refmpq_add (want_add, x, y);
162
refmpq_sub (want_sub, x, y);
164
check_all (x, y, want_add, want_sub);
169
mpq_clear (want_add);
170
mpq_clear (want_sub);