1 | /* Test mpz_cmp_si. |
---|
2 | |
---|
3 | Copyright 2000, 2001 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 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 | #define SGN(x) ((x) < 0 ? -1 : (x) == 0 ? 0 : 1) |
---|
29 | |
---|
30 | void |
---|
31 | check_data (void) |
---|
32 | { |
---|
33 | static const struct { |
---|
34 | const char *a, *b; |
---|
35 | int want; |
---|
36 | } data[] = { |
---|
37 | { "0", "1", -1 }, |
---|
38 | { "0", "0", 0 }, |
---|
39 | { "0", "-1", 1 }, |
---|
40 | |
---|
41 | { "1", "1", 0 }, |
---|
42 | { "1", "0", 1 }, |
---|
43 | { "1", "-1", 1 }, |
---|
44 | |
---|
45 | { "-1", "1", -1 }, |
---|
46 | { "-1", "0", -1 }, |
---|
47 | { "-1", "-1", 0 }, |
---|
48 | |
---|
49 | { "0", "-0x80000000", 1 }, |
---|
50 | { "0x80000000", "-0x80000000", 1 }, |
---|
51 | { "0x80000001", "-0x80000000", 1 }, |
---|
52 | { "-0x80000000", "-0x80000000", 0 }, |
---|
53 | { "-0x80000001", "-0x80000000", -1 }, |
---|
54 | |
---|
55 | { "0", "-0x8000000000000000", 1 }, |
---|
56 | { "0x8000000000000000", "-0x8000000000000000", 1 }, |
---|
57 | { "0x8000000000000001", "-0x8000000000000000", 1 }, |
---|
58 | { "-0x8000000000000000", "-0x8000000000000000", 0 }, |
---|
59 | { "-0x8000000000000001", "-0x8000000000000000", -1 }, |
---|
60 | }; |
---|
61 | |
---|
62 | mpz_t a, bz; |
---|
63 | long b; |
---|
64 | int got; |
---|
65 | int i; |
---|
66 | |
---|
67 | mpz_init (a); |
---|
68 | mpz_init (bz); |
---|
69 | for (i = 0; i < numberof (data); i++) |
---|
70 | { |
---|
71 | mpz_set_str_or_abort (a, data[i].a, 0); |
---|
72 | mpz_set_str_or_abort (bz, data[i].b, 0); |
---|
73 | |
---|
74 | if (mpz_fits_slong_p (bz)) |
---|
75 | { |
---|
76 | b = mpz_get_si (bz); |
---|
77 | got = mpz_cmp_si (a, b); |
---|
78 | if (SGN (got) != data[i].want) |
---|
79 | { |
---|
80 | printf ("mpz_cmp_si wrong on data[%d]\n", i); |
---|
81 | printf (" a="); mpz_out_str (stdout, 10, a); printf ("\n"); |
---|
82 | printf (" b=%ld\n", b); |
---|
83 | printf (" got=%d\n", got); |
---|
84 | printf (" want=%d\n", data[i].want); |
---|
85 | abort(); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | mpz_clear (a); |
---|
91 | mpz_clear (bz); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | int |
---|
96 | main (void) |
---|
97 | { |
---|
98 | tests_start (); |
---|
99 | |
---|
100 | check_data (); |
---|
101 | |
---|
102 | tests_end (); |
---|
103 | exit (0); |
---|
104 | } |
---|