summaryrefslogtreecommitdiff
path: root/tests/mpz/t-cmp.c
blob: 14c4aab2adc8dce1a1286df8e2c30226efa319d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Test mpz_cmp and mpz_cmpabs.

Copyright 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"


/* Nothing sophisticated here, just exercise some combinations of sizes and
   signs.  */


void
check_one (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
{
  int  got;

  got = mpz_cmp (x, y);
  if ((   got <  0) != (want_cmp <  0)
      || (got == 0) != (want_cmp == 0)
      || (got >  0) != (want_cmp >  0))
    {
      printf ("mpz_cmp got %d want %d\n", got, want_cmp);
      mpz_trace ("x", x);
      mpz_trace ("y", y);
      abort ();
    }

  got = mpz_cmpabs (x, y);
  if ((   got <  0) != (want_cmpabs <  0)
      || (got == 0) != (want_cmpabs == 0)
      || (got >  0) != (want_cmpabs >  0))
    {
      printf ("mpz_cmpabs got %d want %d\n", got, want_cmpabs);
      mpz_trace ("x", x);
      mpz_trace ("y", y);
      abort ();
    }
}


void
check_all (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs)
{
  check_one (x, y,  want_cmp,  want_cmpabs);
  check_one (y, x, -want_cmp, -want_cmpabs);

  mpz_neg (x, x);
  mpz_neg (y, y);
  want_cmp = -want_cmp;

  check_one (x, y,  want_cmp,  want_cmpabs);
  check_one (y, x, -want_cmp, -want_cmpabs);
}


#define SET1(z,size, n) \
  SIZ(z) = size; PTR(z)[0] = n

#define SET2(z,size, n1,n0) \
  SIZ(z) = size; PTR(z)[1] = n1; PTR(z)[0] = n0

#define SET4(z,size, n3,n2,n1,n0) \
  SIZ(z) = size; PTR(z)[3] = n3; PTR(z)[2] = n2; PTR(z)[1] = n1; PTR(z)[0] = n0

void
check_various (void)
{
  mpz_t  x, y;

  mpz_init (x);
  mpz_init (y);

  mpz_realloc (x, (mp_size_t) 20);
  mpz_realloc (y, (mp_size_t) 20);

  /* 0 cmp 0, junk in low limbs */
  SET1 (x,0, 123);
  SET1 (y,0, 456);
  check_all (x, y, 0, 0);


  /* 123 cmp 0 */
  SET1 (x,1, 123);
  SET1 (y,0, 456);
  check_all (x, y, 1, 1);

  /* 123:456 cmp 0 */
  SET2 (x,2, 456,123);
  SET1 (y,0, 9999);
  check_all (x, y, 1, 1);


  /* 123 cmp 123 */
  SET1(x,1, 123);
  SET1(y,1, 123);
  check_all (x, y, 0, 0);

  /* -123 cmp 123 */
  SET1(x,-1, 123);
  SET1(y,1,  123);
  check_all (x, y, -1, 0);


  /* 123 cmp 456 */
  SET1(x,1, 123);
  SET1(y,1, 456);
  check_all (x, y, -1, -1);

  /* -123 cmp 456 */
  SET1(x,-1, 123);
  SET1(y,1,  456);
  check_all (x, y, -1, -1);

  /* 123 cmp -456 */
  SET1(x,1,  123);
  SET1(y,-1, 456);
  check_all (x, y, 1, -1);


  /* 1:0 cmp 1:0 */
  SET2 (x,2, 1,0);
  SET2 (y,2, 1,0);
  check_all (x, y, 0, 0);

  /* -1:0 cmp 1:0 */
  SET2 (x,-2, 1,0);
  SET2 (y,2,  1,0);
  check_all (x, y, -1, 0);


  /* 2:0 cmp 1:0 */
  SET2 (x,2, 2,0);
  SET2 (y,2, 1,0);
  check_all (x, y, 1, 1);


  /* 4:3:2:1 cmp 2:1 */
  SET4 (x,4, 4,3,2,1);
  SET2 (y,2, 2,1);
  check_all (x, y, 1, 1);

  /* -4:3:2:1 cmp 2:1 */
  SET4 (x,-4, 4,3,2,1);
  SET2 (y,2,  2,1);
  check_all (x, y, -1, 1);


  mpz_clear (x);
  mpz_clear (y);
}


int
main (void)
{
  tests_start ();
  mp_trace_base = -16;

  check_various ();

  tests_end ();
  exit (0);
}