source: trunk/third/gmp/tests/mpz/t-cmp.c @ 18191

Revision 18191, 3.7 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test mpz_cmp and mpz_cmpabs.
2
3Copyright 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include "gmp.h"
25#include "gmp-impl.h"
26#include "tests.h"
27
28
29/* Nothing sophisticated here, just exercise some combinations of sizes and
30   signs.  */
31
32
33void
34check_one (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
35{
36  int  got;
37
38  got = mpz_cmp (x, y);
39  if ((   got <  0) != (want_cmp <  0)
40      || (got == 0) != (want_cmp == 0)
41      || (got >  0) != (want_cmp >  0))
42    {
43      printf ("mpz_cmp got %d want %d\n", got, want_cmp);
44      mpz_trace ("x", x);
45      mpz_trace ("y", y);
46      abort ();
47    }
48
49  got = mpz_cmpabs (x, y);
50  if ((   got <  0) != (want_cmpabs <  0)
51      || (got == 0) != (want_cmpabs == 0)
52      || (got >  0) != (want_cmpabs >  0))
53    {
54      printf ("mpz_cmpabs got %d want %d\n", got, want_cmpabs);
55      mpz_trace ("x", x);
56      mpz_trace ("y", y);
57      abort ();
58    }
59}
60
61
62void
63check_all (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
64{
65  check_one (x, y,  want_cmp,  want_cmpabs);
66  check_one (y, x, -want_cmp, -want_cmpabs);
67
68  mpz_neg (x, x);
69  mpz_neg (y, y);
70  want_cmp = -want_cmp;
71
72  check_one (x, y,  want_cmp,  want_cmpabs);
73  check_one (y, x, -want_cmp, -want_cmpabs);
74}
75
76
77#define SET1(z,size, n) \
78  SIZ(z) = size; PTR(z)[0] = n
79
80#define SET2(z,size, n1,n0) \
81  SIZ(z) = size; PTR(z)[1] = n1; PTR(z)[0] = n0
82
83#define SET4(z,size, n3,n2,n1,n0) \
84  SIZ(z) = size; PTR(z)[3] = n3; PTR(z)[2] = n2; PTR(z)[1] = n1; PTR(z)[0] = n0
85
86void
87check_various (void)
88{
89  mpz_t  x, y;
90
91  mpz_init (x);
92  mpz_init (y);
93
94  mpz_realloc (x, (mp_size_t) 20);
95  mpz_realloc (y, (mp_size_t) 20);
96
97  /* 0 cmp 0, junk in low limbs */
98  SET1 (x,0, 123);
99  SET1 (y,0, 456);
100  check_all (x, y, 0, 0);
101
102
103  /* 123 cmp 0 */
104  SET1 (x,1, 123);
105  SET1 (y,0, 456);
106  check_all (x, y, 1, 1);
107
108  /* 123:456 cmp 0 */
109  SET2 (x,2, 456,123);
110  SET1 (y,0, 9999);
111  check_all (x, y, 1, 1);
112
113
114  /* 123 cmp 123 */
115  SET1(x,1, 123);
116  SET1(y,1, 123);
117  check_all (x, y, 0, 0);
118
119  /* -123 cmp 123 */
120  SET1(x,-1, 123);
121  SET1(y,1,  123);
122  check_all (x, y, -1, 0);
123
124
125  /* 123 cmp 456 */
126  SET1(x,1, 123);
127  SET1(y,1, 456);
128  check_all (x, y, -1, -1);
129
130  /* -123 cmp 456 */
131  SET1(x,-1, 123);
132  SET1(y,1,  456);
133  check_all (x, y, -1, -1);
134
135  /* 123 cmp -456 */
136  SET1(x,1,  123);
137  SET1(y,-1, 456);
138  check_all (x, y, 1, -1);
139
140
141  /* 1:0 cmp 1:0 */
142  SET2 (x,2, 1,0);
143  SET2 (y,2, 1,0);
144  check_all (x, y, 0, 0);
145
146  /* -1:0 cmp 1:0 */
147  SET2 (x,-2, 1,0);
148  SET2 (y,2,  1,0);
149  check_all (x, y, -1, 0);
150
151
152  /* 2:0 cmp 1:0 */
153  SET2 (x,2, 2,0);
154  SET2 (y,2, 1,0);
155  check_all (x, y, 1, 1);
156
157
158  /* 4:3:2:1 cmp 2:1 */
159  SET4 (x,4, 4,3,2,1);
160  SET2 (y,2, 2,1);
161  check_all (x, y, 1, 1);
162
163  /* -4:3:2:1 cmp 2:1 */
164  SET4 (x,-4, 4,3,2,1);
165  SET2 (y,2,  2,1);
166  check_all (x, y, -1, 1);
167
168
169  mpz_clear (x);
170  mpz_clear (y);
171}
172
173
174int
175main (void)
176{
177  tests_start ();
178  mp_trace_base = -16;
179
180  check_various ();
181
182  tests_end ();
183  exit (0);
184}
Note: See TracBrowser for help on using the repository browser.