xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-fits.c (revision d11b170b9000ada93db553723522a63d5deac310)
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 http://www.gnu.org/licenses/.  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "tests.h"
26 
27 
28 /* Nothing sophisticated here, just exercise mpz_fits_*_p on a small amount
29    of data. */
30 
31 #define EXPECT_S(fun,name,answer)                                       \
32   got = fun (z);                                                        \
33   if (got != answer)                                                    \
34     {                                                                   \
35       printf ("%s (%s) got %d want %d\n", name, expr, got, answer);     \
36       printf (" z size %d\n", SIZ(z));                                  \
37       printf (" z dec "); mpz_out_str (stdout, 10, z); printf ("\n");   \
38       printf (" z hex "); mpz_out_str (stdout, 16, z); printf ("\n");   \
39       error = 1;                                                        \
40     }
41 
42 #if HAVE_STRINGIZE
43 #define EXPECT(fun,answer)  EXPECT_S(fun,#fun,answer)
44 #else
45 #define EXPECT(fun,answer)  EXPECT_S(fun,"fun",answer)
46 #endif
47 
48 int
49 main (void)
50 {
51   mpz_t       z;
52   int         got;
53   const char  *expr;
54   int         error = 0;
55 
56   tests_start ();
57   mpz_init (z);
58 
59   mpz_set_ui (z, 0L);
60   expr = "0";
61   EXPECT (mpz_fits_ulong_p, 1);
62   EXPECT (mpz_fits_uint_p, 1);
63   EXPECT (mpz_fits_ushort_p, 1);
64   EXPECT (mpz_fits_slong_p, 1);
65   EXPECT (mpz_fits_sint_p, 1);
66   EXPECT (mpz_fits_sshort_p, 1);
67 
68   mpz_set_ui (z, 1L);
69   expr = "1";
70   EXPECT (mpz_fits_ulong_p, 1);
71   EXPECT (mpz_fits_uint_p, 1);
72   EXPECT (mpz_fits_ushort_p, 1);
73   EXPECT (mpz_fits_slong_p, 1);
74   EXPECT (mpz_fits_sint_p, 1);
75   EXPECT (mpz_fits_sshort_p, 1);
76 
77   mpz_set_si (z, -1L);
78   expr = "-1";
79   EXPECT (mpz_fits_ulong_p, 0);
80   EXPECT (mpz_fits_uint_p, 0);
81   EXPECT (mpz_fits_ushort_p, 0);
82   EXPECT (mpz_fits_slong_p, 1);
83   EXPECT (mpz_fits_sint_p, 1);
84   EXPECT (mpz_fits_sshort_p, 1);
85 
86   mpz_set_ui (z, 1L);
87   mpz_mul_2exp (z, z, 5L*GMP_LIMB_BITS);
88   expr = "2^(5*BPML)";
89   EXPECT (mpz_fits_ulong_p, 0);
90   EXPECT (mpz_fits_uint_p, 0);
91   EXPECT (mpz_fits_ushort_p, 0);
92   EXPECT (mpz_fits_slong_p, 0);
93   EXPECT (mpz_fits_sint_p, 0);
94   EXPECT (mpz_fits_sshort_p, 0);
95 
96 
97   mpz_set_ui (z, (unsigned long) USHRT_MAX);
98   expr = "USHRT_MAX";
99   EXPECT (mpz_fits_ulong_p, 1);
100   EXPECT (mpz_fits_uint_p, 1);
101   EXPECT (mpz_fits_ushort_p, 1);
102 
103   mpz_set_ui (z, (unsigned long) USHRT_MAX);
104   mpz_add_ui (z, z, 1L);
105   expr = "USHRT_MAX + 1";
106   EXPECT (mpz_fits_ushort_p, 0);
107 
108 
109   mpz_set_ui (z, (unsigned long) UINT_MAX);
110   expr = "UINT_MAX";
111   EXPECT (mpz_fits_ulong_p, 1);
112   EXPECT (mpz_fits_uint_p, 1);
113 
114   mpz_set_ui (z, (unsigned long) UINT_MAX);
115   mpz_add_ui (z, z, 1L);
116   expr = "UINT_MAX + 1";
117   EXPECT (mpz_fits_uint_p, 0);
118 
119 
120   mpz_set_ui (z, ULONG_MAX);
121   expr = "ULONG_MAX";
122   EXPECT (mpz_fits_ulong_p, 1);
123 
124   mpz_set_ui (z, ULONG_MAX);
125   mpz_add_ui (z, z, 1L);
126   expr = "ULONG_MAX + 1";
127   EXPECT (mpz_fits_ulong_p, 0);
128 
129 
130   mpz_set_si (z, (long) SHRT_MAX);
131   expr = "SHRT_MAX";
132   EXPECT (mpz_fits_slong_p, 1);
133   EXPECT (mpz_fits_sint_p, 1);
134   EXPECT (mpz_fits_sshort_p, 1);
135 
136   mpz_set_si (z, (long) SHRT_MAX);
137   mpz_add_ui (z, z, 1L);
138   expr = "SHRT_MAX + 1";
139   EXPECT (mpz_fits_sshort_p, 0);
140 
141 
142   mpz_set_si (z, (long) INT_MAX);
143   expr = "INT_MAX";
144   EXPECT (mpz_fits_slong_p, 1);
145   EXPECT (mpz_fits_sint_p, 1);
146 
147   mpz_set_si (z, (long) INT_MAX);
148   mpz_add_ui (z, z, 1L);
149   expr = "INT_MAX + 1";
150   EXPECT (mpz_fits_sint_p, 0);
151 
152 
153   mpz_set_si (z, LONG_MAX);
154   expr = "LONG_MAX";
155   EXPECT (mpz_fits_slong_p, 1);
156 
157   mpz_set_si (z, LONG_MAX);
158   mpz_add_ui (z, z, 1L);
159   expr = "LONG_MAX + 1";
160   EXPECT (mpz_fits_slong_p, 0);
161 
162 
163   mpz_set_si (z, (long) SHRT_MIN);
164   expr = "SHRT_MIN";
165   EXPECT (mpz_fits_slong_p, 1);
166   EXPECT (mpz_fits_sint_p, 1);
167   EXPECT (mpz_fits_sshort_p, 1);
168 
169   mpz_set_si (z, (long) SHRT_MIN);
170   mpz_sub_ui (z, z, 1L);
171   expr = "SHRT_MIN + 1";
172   EXPECT (mpz_fits_sshort_p, 0);
173 
174 
175   mpz_set_si (z, (long) INT_MIN);
176   expr = "INT_MIN";
177   EXPECT (mpz_fits_slong_p, 1);
178   EXPECT (mpz_fits_sint_p, 1);
179 
180   mpz_set_si (z, (long) INT_MIN);
181   mpz_sub_ui (z, z, 1L);
182   expr = "INT_MIN + 1";
183   EXPECT (mpz_fits_sint_p, 0);
184 
185 
186   mpz_set_si (z, LONG_MIN);
187   expr = "LONG_MIN";
188   EXPECT (mpz_fits_slong_p, 1);
189 
190   mpz_set_si (z, LONG_MIN);
191   mpz_sub_ui (z, z, 1L);
192   expr = "LONG_MIN + 1";
193   EXPECT (mpz_fits_slong_p, 0);
194 
195 
196   if (error)
197     abort ();
198 
199   mpz_clear (z);
200   tests_end ();
201   exit (0);
202 }
203