1/* Test mpz_cmp_si. 2 3Copyright 2000, 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#define SGN(x) ((x) < 0 ? -1 : (x) == 0 ? 0 : 1) 27 28void 29check_data (void) 30{ 31 static const struct { 32 const char *a, *b; 33 int want; 34 } data[] = { 35 { "0", "1", -1 }, 36 { "0", "0", 0 }, 37 { "0", "-1", 1 }, 38 39 { "1", "1", 0 }, 40 { "1", "0", 1 }, 41 { "1", "-1", 1 }, 42 43 { "-1", "1", -1 }, 44 { "-1", "0", -1 }, 45 { "-1", "-1", 0 }, 46 47 { "0", "-0x80000000", 1 }, 48 { "0x80000000", "-0x80000000", 1 }, 49 { "0x80000001", "-0x80000000", 1 }, 50 { "-0x80000000", "-0x80000000", 0 }, 51 { "-0x80000001", "-0x80000000", -1 }, 52 53 { "0", "-0x8000000000000000", 1 }, 54 { "0x8000000000000000", "-0x8000000000000000", 1 }, 55 { "0x8000000000000001", "-0x8000000000000000", 1 }, 56 { "-0x8000000000000000", "-0x8000000000000000", 0 }, 57 { "-0x8000000000000001", "-0x8000000000000000", -1 }, 58 }; 59 60 mpz_t a, bz; 61 long b; 62 int got; 63 int i; 64 65 mpz_init (a); 66 mpz_init (bz); 67 for (i = 0; i < numberof (data); i++) 68 { 69 mpz_set_str_or_abort (a, data[i].a, 0); 70 mpz_set_str_or_abort (bz, data[i].b, 0); 71 72 if (mpz_fits_slong_p (bz)) 73 { 74 b = mpz_get_si (bz); 75 got = mpz_cmp_si (a, b); 76 if (SGN (got) != data[i].want) 77 { 78 printf ("mpz_cmp_si wrong on data[%d]\n", i); 79 printf (" a="); mpz_out_str (stdout, 10, a); printf ("\n"); 80 printf (" b=%ld\n", b); 81 printf (" got=%d\n", got); 82 printf (" want=%d\n", data[i].want); 83 abort(); 84 } 85 } 86 } 87 88 mpz_clear (a); 89 mpz_clear (bz); 90} 91 92 93int 94main (void) 95{ 96 tests_start (); 97 98 check_data (); 99 100 tests_end (); 101 exit (0); 102} 103