xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpq/t-equal.c (revision dd75ac5b443e967e26b4d18cc8cd5eb98512bfbf)
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 https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 
26 void
27 check_one (mpq_srcptr x, mpq_srcptr y, int want)
28 {
29   int  got;
30 
31   MPQ_CHECK_FORMAT (x);
32   MPQ_CHECK_FORMAT (y);
33 
34   got = mpq_equal (x, y);
35   if ((got != 0) != (want != 0))
36     {
37       printf ("mpq_equal got %d want %d\n", got, want);
38       mpq_trace ("x", x);
39       mpq_trace ("y", y);
40       abort ();
41     }
42 }
43 
44 
45 void
46 check_all (mpq_ptr x, mpq_ptr y, int want)
47 {
48   check_one (x, y, want);
49   check_one (y, x, want);
50 
51   mpq_neg (x, x);
52   mpq_neg (y, y);
53 
54   check_one (x, y, want);
55   check_one (y, x, want);
56 }
57 
58 
59 #define SET4Z(z, size,l3,l2,l1,l0) \
60   SIZ(z) = size; PTR(z)[3] = l3; PTR(z)[2] = l2; PTR(z)[1] = l1; PTR(z)[0] = l0
61 
62 #define SET4(q, nsize,n3,n2,n1,n0, dsize,d3,d2,d1,d0)   \
63   SET4Z (mpq_numref(q), nsize,n3,n2,n1,n0);             \
64   SET4Z (mpq_denref(q), dsize,d3,d2,d1,d0)
65 
66 
67 /* Exercise various combinations of same and slightly different values. */
68 
69 void
70 check_various (void)
71 {
72   mpq_t  x, y;
73 
74   mpq_init (x);
75   mpq_init (y);
76 
77   mpz_realloc (mpq_numref(x), (mp_size_t) 20);
78   mpz_realloc (mpq_denref(x), (mp_size_t) 20);
79   mpz_realloc (mpq_numref(y), (mp_size_t) 20);
80   mpz_realloc (mpq_denref(y), (mp_size_t) 20);
81 
82   /* 0 == 0 */
83   SET4 (x, 0,13,12,11,10, 1,23,22,21,1);
84   SET4 (y, 0,33,32,31,30, 1,43,42,41,1);
85   check_all (x, y, 1);
86 
87   /* 83/99 == 83/99 */
88   SET4 (x, 1,13,12,11,83, 1,23,22,21,99);
89   SET4 (y, 1,33,32,31,83, 1,43,42,41,99);
90   check_all (x, y, 1);
91 
92   /* 1:2:3:4/5:6:7 == 1:2:3:4/5:6:7 */
93   SET4 (x, 4,1,2,3,4, 3,88,5,6,7);
94   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
95   check_all (x, y, 1);
96 
97   /* various individual changes making != */
98   SET4 (x, 4,1,2,3,667, 3,88,5,6,7);
99   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
100   check_all (x, y, 0);
101   SET4 (x, 4,1,2,666,4, 3,88,5,6,7);
102   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
103   check_all (x, y, 0);
104   SET4 (x, 4,1,666,3,4, 3,88,5,6,7);
105   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
106   check_all (x, y, 0);
107 #if GMP_NUMB_BITS != 62
108   SET4 (x, 4,667,2,3,4, 3,88,5,6,7);
109   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
110   check_all (x, y, 0);
111 #endif
112   SET4 (x, 4,1,2,3,4, 3,88,5,6,667);
113   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
114   check_all (x, y, 0);
115   SET4 (x, 4,1,2,3,4, 3,88,5,667,7);
116   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
117   check_all (x, y, 0);
118   SET4 (x, 4,1,2,3,4, 3,88,666,6,7);
119   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
120   check_all (x, y, 0);
121   SET4 (x, -4,1,2,3,4, 3,88,5,6,7);
122   SET4 (y,  4,1,2,3,4, 3,99,5,6,7);
123   check_all (x, y, 0);
124   SET4 (x, 1,1,2,3,4, 3,88,5,6,7);
125   SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
126   check_all (x, y, 0);
127   SET4 (x, 4,1,2,3,4, 3,88,5,6,7);
128   SET4 (y, 4,1,2,3,4, 2,99,5,6,7);
129   check_all (x, y, 0);
130 
131   mpq_clear (x);
132   mpq_clear (y);
133 }
134 
135 
136 int
137 main (void)
138 {
139   tests_start ();
140   mp_trace_base = -16;
141 
142   check_various ();
143 
144   tests_end ();
145   exit (0);
146 }
147