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