1 /* Test mpz_fits_*_p */
2
3 /*
4 Copyright 2001 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library test suite.
7
8 The GNU MP Library test suite is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 3 of the License,
11 or (at your option) any later version.
12
13 The GNU MP Library test suite is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "gmp-impl.h"
24 #include "tests.h"
25
26
27 /* Nothing sophisticated here, just exercise mpz_fits_*_p on a small amount
28 of data. */
29
30 #define EXPECT_S(fun,name,answer) \
31 got = fun (z); \
32 if (got != answer) \
33 { \
34 printf ("%s (%s) got %d want %d\n", name, expr, got, answer); \
35 printf (" z size %d\n", SIZ(z)); \
36 printf (" z dec "); mpz_out_str (stdout, 10, z); printf ("\n"); \
37 printf (" z hex "); mpz_out_str (stdout, 16, z); printf ("\n"); \
38 error = 1; \
39 }
40
41 #define EXPECT(fun,answer) EXPECT_S(fun,#fun,answer)
42
43 int
44 main (void)
45 {
46 mpz_t z;
47 int got;
48 const char *expr;
49 int error = 0;
50
51 tests_start ();
52 mpz_init (z);
53
54 mpz_set_ui (z, 0L);
55 expr = "0";
56 EXPECT (mpz_fits_ulong_p, 1);
57 EXPECT (mpz_fits_uint_p, 1);
58 EXPECT (mpz_fits_ushort_p, 1);
59 EXPECT (mpz_fits_slong_p, 1);
60 EXPECT (mpz_fits_sint_p, 1);
61 EXPECT (mpz_fits_sshort_p, 1);
62
63 mpz_set_ui (z, 1L);
64 expr = "1";
65 EXPECT (mpz_fits_ulong_p, 1);
66 EXPECT (mpz_fits_uint_p, 1);
67 EXPECT (mpz_fits_ushort_p, 1);
68 EXPECT (mpz_fits_slong_p, 1);
69 EXPECT (mpz_fits_sint_p, 1);
70 EXPECT (mpz_fits_sshort_p, 1);
71
72 mpz_set_si (z, -1L);
73 expr = "-1";
74 EXPECT (mpz_fits_ulong_p, 0);
75 EXPECT (mpz_fits_uint_p, 0);
76 EXPECT (mpz_fits_ushort_p, 0);
77 EXPECT (mpz_fits_slong_p, 1);
78 EXPECT (mpz_fits_sint_p, 1);
79 EXPECT (mpz_fits_sshort_p, 1);
80
81 mpz_set_ui (z, 1L);
82 mpz_mul_2exp (z, z, 5L*GMP_LIMB_BITS);
83 expr = "2^(5*BPML)";
84 EXPECT (mpz_fits_ulong_p, 0);
85 EXPECT (mpz_fits_uint_p, 0);
86 EXPECT (mpz_fits_ushort_p, 0);
87 EXPECT (mpz_fits_slong_p, 0);
88 EXPECT (mpz_fits_sint_p, 0);
89 EXPECT (mpz_fits_sshort_p, 0);
90
91
92 mpz_set_ui (z, (unsigned long) USHRT_MAX);
93 expr = "USHRT_MAX";
94 EXPECT (mpz_fits_ulong_p, 1);
95 EXPECT (mpz_fits_uint_p, 1);
96 EXPECT (mpz_fits_ushort_p, 1);
97
98 mpz_set_ui (z, (unsigned long) USHRT_MAX);
99 mpz_add_ui (z, z, 1L);
100 expr = "USHRT_MAX + 1";
101 EXPECT (mpz_fits_ushort_p, 0);
102
103
104 mpz_set_ui (z, (unsigned long) UINT_MAX);
105 expr = "UINT_MAX";
106 EXPECT (mpz_fits_ulong_p, 1);
107 EXPECT (mpz_fits_uint_p, 1);
108
109 mpz_set_ui (z, (unsigned long) UINT_MAX);
110 mpz_add_ui (z, z, 1L);
111 expr = "UINT_MAX + 1";
112 EXPECT (mpz_fits_uint_p, 0);
113
114
115 mpz_set_ui (z, ULONG_MAX);
116 expr = "ULONG_MAX";
117 EXPECT (mpz_fits_ulong_p, 1);
118
119 mpz_set_ui (z, ULONG_MAX);
120 mpz_add_ui (z, z, 1L);
121 expr = "ULONG_MAX + 1";
122 EXPECT (mpz_fits_ulong_p, 0);
123
124
125 mpz_set_si (z, (long) SHRT_MAX);
126 expr = "SHRT_MAX";
127 EXPECT (mpz_fits_slong_p, 1);
128 EXPECT (mpz_fits_sint_p, 1);
129 EXPECT (mpz_fits_sshort_p, 1);
130
131 mpz_set_si (z, (long) SHRT_MAX);
132 mpz_add_ui (z, z, 1L);
133 expr = "SHRT_MAX + 1";
134 EXPECT (mpz_fits_sshort_p, 0);
135
136
137 mpz_set_si (z, (long) INT_MAX);
138 expr = "INT_MAX";
139 EXPECT (mpz_fits_slong_p, 1);
140 EXPECT (mpz_fits_sint_p, 1);
141
142 mpz_set_si (z, (long) INT_MAX);
143 mpz_add_ui (z, z, 1L);
144 expr = "INT_MAX + 1";
145 EXPECT (mpz_fits_sint_p, 0);
146
147
148 mpz_set_si (z, LONG_MAX);
149 expr = "LONG_MAX";
150 EXPECT (mpz_fits_slong_p, 1);
151
152 mpz_set_si (z, LONG_MAX);
153 mpz_add_ui (z, z, 1L);
154 expr = "LONG_MAX + 1";
155 EXPECT (mpz_fits_slong_p, 0);
156
157
158 mpz_set_si (z, (long) SHRT_MIN);
159 expr = "SHRT_MIN";
160 EXPECT (mpz_fits_slong_p, 1);
161 EXPECT (mpz_fits_sint_p, 1);
162 EXPECT (mpz_fits_sshort_p, 1);
163
164 mpz_set_si (z, (long) SHRT_MIN);
165 mpz_sub_ui (z, z, 1L);
166 expr = "SHRT_MIN + 1";
167 EXPECT (mpz_fits_sshort_p, 0);
168
169
170 mpz_set_si (z, (long) INT_MIN);
171 expr = "INT_MIN";
172 EXPECT (mpz_fits_slong_p, 1);
173 EXPECT (mpz_fits_sint_p, 1);
174
175 mpz_set_si (z, (long) INT_MIN);
176 mpz_sub_ui (z, z, 1L);
177 expr = "INT_MIN + 1";
178 EXPECT (mpz_fits_sint_p, 0);
179
180
181 mpz_set_si (z, LONG_MIN);
182 expr = "LONG_MIN";
183 EXPECT (mpz_fits_slong_p, 1);
184
185 mpz_set_si (z, LONG_MIN);
186 mpz_sub_ui (z, z, 1L);
187 expr = "LONG_MIN + 1";
188 EXPECT (mpz_fits_slong_p, 0);
189
190
191 if (error)
192 abort ();
193
194 mpz_clear (z);
195 tests_end ();
196 exit (0);
197 }