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 3 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. If not, see http://www.gnu.org/licenses/. */
32 check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
35 int neg_x, neg_y, swap;
39 MPQ_CHECK_FORMAT (want_add);
40 MPQ_CHECK_FORMAT (want_sub);
44 for (swap = 0; swap <= 1; swap++)
46 for (neg_x = 0; neg_x <= 1; neg_x++)
48 for (neg_y = 0; neg_y <= 1; neg_y++)
51 MPQ_CHECK_FORMAT (got);
52 if (! mpq_equal (got, want_add))
54 printf ("mpq_add wrong\n");
57 mpq_trace (" got ", got);
58 mpq_trace (" want", want_add);
63 MPQ_CHECK_FORMAT (got);
64 if (! mpq_equal (got, want_sub))
66 printf ("mpq_sub wrong\n");
69 mpq_trace (" got ", got);
70 mpq_trace (" want", want_sub);
76 mpq_swap (want_add, want_sub);
80 mpq_swap (want_add, want_sub);
81 mpq_neg (want_add, want_add);
82 mpq_neg (want_sub, want_sub);
86 mpq_neg (want_sub, want_sub);
100 const char *want_sub;
104 { "0", "0", "0", "0" },
105 { "1", "0", "1", "1" },
106 { "1", "1", "2", "0" },
108 { "1/2", "1/2", "1", "0" },
109 { "5/6", "14/15", "53/30", "-1/10" },
112 mpq_t x, y, want_add, want_sub;
120 for (i = 0; i < numberof (data); i++)
122 mpq_set_str_or_abort (x, data[i].x, 0);
123 mpq_set_str_or_abort (y, data[i].y, 0);
124 mpq_set_str_or_abort (want_add, data[i].want_add, 0);
125 mpq_set_str_or_abort (want_sub, data[i].want_sub, 0);
127 check_all (x, y, want_add, want_sub);
132 mpq_clear (want_add);
133 mpq_clear (want_sub);
140 mpq_t x, y, want_add, want_sub;
142 gmp_randstate_ptr rands = RANDS;
149 for (i = 0; i < 500; i++)
151 mpz_errandomb (mpq_numref(x), rands, 512L);
152 mpz_errandomb_nonzero (mpq_denref(x), rands, 512L);
153 mpq_canonicalize (x);
155 mpz_errandomb (mpq_numref(y), rands, 512L);
156 mpz_errandomb_nonzero (mpq_denref(y), rands, 512L);
157 mpq_canonicalize (y);
159 refmpq_add (want_add, x, y);
160 refmpq_sub (want_sub, x, y);
162 check_all (x, y, want_add, want_sub);
167 mpq_clear (want_add);
168 mpq_clear (want_sub);