1 /* Test mpz_cmp and mpz_cmpabs.
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 /* Nothing sophisticated here, just exercise some combinations of sizes and
27 signs. */
28
29
30 void
check_one(mpz_ptr x,mpz_ptr y,int want_cmp,int want_cmpabs)31 check_one (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
32 {
33 int got;
34
35 got = mpz_cmp (x, y);
36 if (( got < 0) != (want_cmp < 0)
37 || (got == 0) != (want_cmp == 0)
38 || (got > 0) != (want_cmp > 0))
39 {
40 printf ("mpz_cmp got %d want %d\n", got, want_cmp);
41 mpz_trace ("x", x);
42 mpz_trace ("y", y);
43 abort ();
44 }
45
46 got = mpz_cmpabs (x, y);
47 if (( got < 0) != (want_cmpabs < 0)
48 || (got == 0) != (want_cmpabs == 0)
49 || (got > 0) != (want_cmpabs > 0))
50 {
51 printf ("mpz_cmpabs got %d want %d\n", got, want_cmpabs);
52 mpz_trace ("x", x);
53 mpz_trace ("y", y);
54 abort ();
55 }
56 }
57
58
59 void
check_all(mpz_ptr x,mpz_ptr y,int want_cmp,int want_cmpabs)60 check_all (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
61 {
62 check_one (x, y, want_cmp, want_cmpabs);
63 check_one (y, x, -want_cmp, -want_cmpabs);
64
65 mpz_neg (x, x);
66 mpz_neg (y, y);
67 want_cmp = -want_cmp;
68
69 check_one (x, y, want_cmp, want_cmpabs);
70 check_one (y, x, -want_cmp, -want_cmpabs);
71 }
72
73
74 #define SET1(z,size, n) \
75 SIZ(z) = size; PTR(z)[0] = n
76
77 #define SET2(z,size, n1,n0) \
78 SIZ(z) = size; PTR(z)[1] = n1; PTR(z)[0] = n0
79
80 #define SET4(z,size, n3,n2,n1,n0) \
81 SIZ(z) = size; PTR(z)[3] = n3; PTR(z)[2] = n2; PTR(z)[1] = n1; PTR(z)[0] = n0
82
83 void
check_various(void)84 check_various (void)
85 {
86 mpz_t x, y;
87
88 mpz_init (x);
89 mpz_init (y);
90
91 mpz_realloc (x, (mp_size_t) 20);
92 mpz_realloc (y, (mp_size_t) 20);
93
94 /* 0 cmp 0, junk in low limbs */
95 SET1 (x,0, 123);
96 SET1 (y,0, 456);
97 check_all (x, y, 0, 0);
98
99
100 /* 123 cmp 0 */
101 SET1 (x,1, 123);
102 SET1 (y,0, 456);
103 check_all (x, y, 1, 1);
104
105 /* 123:456 cmp 0 */
106 SET2 (x,2, 456,123);
107 SET1 (y,0, 9999);
108 check_all (x, y, 1, 1);
109
110
111 /* 123 cmp 123 */
112 SET1(x,1, 123);
113 SET1(y,1, 123);
114 check_all (x, y, 0, 0);
115
116 /* -123 cmp 123 */
117 SET1(x,-1, 123);
118 SET1(y,1, 123);
119 check_all (x, y, -1, 0);
120
121
122 /* 123 cmp 456 */
123 SET1(x,1, 123);
124 SET1(y,1, 456);
125 check_all (x, y, -1, -1);
126
127 /* -123 cmp 456 */
128 SET1(x,-1, 123);
129 SET1(y,1, 456);
130 check_all (x, y, -1, -1);
131
132 /* 123 cmp -456 */
133 SET1(x,1, 123);
134 SET1(y,-1, 456);
135 check_all (x, y, 1, -1);
136
137
138 /* 1:0 cmp 1:0 */
139 SET2 (x,2, 1,0);
140 SET2 (y,2, 1,0);
141 check_all (x, y, 0, 0);
142
143 /* -1:0 cmp 1:0 */
144 SET2 (x,-2, 1,0);
145 SET2 (y,2, 1,0);
146 check_all (x, y, -1, 0);
147
148
149 /* 2:0 cmp 1:0 */
150 SET2 (x,2, 2,0);
151 SET2 (y,2, 1,0);
152 check_all (x, y, 1, 1);
153
154
155 /* 4:3:2:1 cmp 2:1 */
156 SET4 (x,4, 4,3,2,1);
157 SET2 (y,2, 2,1);
158 check_all (x, y, 1, 1);
159
160 /* -4:3:2:1 cmp 2:1 */
161 SET4 (x,-4, 4,3,2,1);
162 SET2 (y,2, 2,1);
163 check_all (x, y, -1, 1);
164
165
166 mpz_clear (x);
167 mpz_clear (y);
168 }
169
170
171 int
main(void)172 main (void)
173 {
174 tests_start ();
175 mp_trace_base = -16;
176
177 check_various ();
178
179 tests_end ();
180 exit (0);
181 }
182