xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpq/t-equal.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* Test mpq_equal.
2 
3 Copyright 2001 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp.h"
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 
27 void
28 check_one (mpq_srcptr x, mpq_srcptr y, int want)
29 {
30   int  got;
31 
32   MPQ_CHECK_FORMAT (x);
33   MPQ_CHECK_FORMAT (y);
34 
35   got = mpq_equal (x, y);
36   if ((got != 0) != (want != 0))
37     {
38       printf ("mpq_equal got %d want %d\n", got, want);
39       mpq_trace ("x", x);
40       mpq_trace ("y", y);
41       abort ();
42     }
43 }
44 
45 
46 void
47 check_all (mpq_ptr x, mpq_ptr y, int want)
48 {
49   check_one (x, y, want);
50   check_one (y, x, want);
51 
52   mpq_neg (x, x);
53   mpq_neg (y, y);
54 
55   check_one (x, y, want);
56   check_one (y, x, want);
57 }
58 
59 
60 #define SET4Z(z, size,l3,l2,l1,l0) \
61   SIZ(z) = size; PTR(z)[3] = l3; PTR(z)[2] = l2; PTR(z)[1] = l1; PTR(z)[0] = l0
62 
63 #define SET4(q, nsize,n3,n2,n1,n0, dsize,d3,d2,d1,d0)   \
64   SET4Z (mpq_numref(q), nsize,n3,n2,n1,n0);             \
65   SET4Z (mpq_denref(q), dsize,d3,d2,d1,d0)
66 
67 
68 /* Exercise various combinations of same and slightly different values. */
69 
70 void
71 check_various (void)
72 {
73   mpq_t  x, y;
74 
75   mpq_init (x);
76   mpq_init (y);
77 
78   mpz_realloc (mpq_numref(x), (mp_size_t) 20);
79   mpz_realloc (mpq_denref(x), (mp_size_t) 20);
80   mpz_realloc (mpq_numref(y), (mp_size_t) 20);
81   mpz_realloc (mpq_denref(y), (mp_size_t) 20);
82 
83   /* 0 == 0 */
84   SET4 (x, 0,13,12,11,10, 1,23,22,21,1);
85   SET4 (y, 0,33,32,31,30, 1,43,42,41,1);
86   check_all (x, y, 1);
87 
88   /* 83/99 == 83/99 */
89   SET4 (x, 1,13,12,11,83, 1,23,22,21,99);
90   SET4 (y, 1,33,32,31,83, 1,43,42,41,99);
91   check_all (x, y, 1);
92 
93   /* 1:2:3:4/5:6:7 == 1:2:3:4/5:6:7 */
94   SET4 (x, 4,1,2,3,4, 3,88,5,6,7);
95   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
96   check_all (x, y, 1);
97 
98   /* various individual changes making != */
99   SET4 (x, 4,1,2,3,667, 3,88,5,6,7);
100   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
101   check_all (x, y, 0);
102   SET4 (x, 4,1,2,666,4, 3,88,5,6,7);
103   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
104   check_all (x, y, 0);
105   SET4 (x, 4,1,666,3,4, 3,88,5,6,7);
106   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
107   check_all (x, y, 0);
108 #if GMP_NUMB_BITS != 62
109   SET4 (x, 4,667,2,3,4, 3,88,5,6,7);
110   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
111   check_all (x, y, 0);
112 #endif
113   SET4 (x, 4,1,2,3,4, 3,88,5,6,667);
114   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
115   check_all (x, y, 0);
116   SET4 (x, 4,1,2,3,4, 3,88,5,667,7);
117   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
118   check_all (x, y, 0);
119   SET4 (x, 4,1,2,3,4, 3,88,666,6,7);
120   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
121   check_all (x, y, 0);
122   SET4 (x, -4,1,2,3,4, 3,88,5,6,7);
123   SET4 (y,  4,1,2,3,4, 3,99,5,6,7);
124   check_all (x, y, 0);
125   SET4 (x, 1,1,2,3,4, 3,88,5,6,7);
126   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
127   check_all (x, y, 0);
128   SET4 (x, 4,1,2,3,4, 3,88,5,6,7);
129   SET4 (y, 4,1,2,3,4, 2,99,5,6,7);
130   check_all (x, y, 0);
131 
132   mpq_clear (x);
133   mpq_clear (y);
134 }
135 
136 
137 int
138 main (void)
139 {
140   tests_start ();
141   mp_trace_base = -16;
142 
143   check_various ();
144 
145   tests_end ();
146   exit (0);
147 }
148