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