1 /* Test mpf_fits_*_p 2 3 Copyright 2001, 2002, 2013 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 26 /* Nothing sophisticated here, just exercise mpf_fits_*_p on a small amount 27 of data. */ 28 29 #define EXPECT_S(fun,name,answer) \ 30 got = fun (f); \ 31 if (got != answer) \ 32 { \ 33 printf ("%s (%s) got %d want %d\n", name, expr, got, answer); \ 34 printf (" f size %d exp %ld\n", SIZ(f), EXP(f)); \ 35 printf (" f dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); \ 36 printf (" f hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); \ 37 error = 1; \ 38 } 39 40 #define EXPECT(fun,answer) EXPECT_S(fun,#fun,answer) 41 42 int 43 main (void) 44 { 45 mpf_t f, f0p5; 46 int got; 47 const char *expr; 48 int error = 0; 49 50 tests_start (); 51 mpf_init2 (f, 200L); 52 mpf_init2 (f0p5, 200L); 53 54 /* 0.5 */ 55 mpf_set_ui (f0p5, 1L); 56 mpf_div_2exp (f0p5, f0p5, 1L); 57 58 mpf_set_ui (f, 0L); 59 expr = "0"; 60 EXPECT (mpf_fits_ulong_p, 1); 61 EXPECT (mpf_fits_uint_p, 1); 62 EXPECT (mpf_fits_ushort_p, 1); 63 EXPECT (mpf_fits_slong_p, 1); 64 EXPECT (mpf_fits_sint_p, 1); 65 EXPECT (mpf_fits_sshort_p, 1); 66 67 mpf_set_ui (f, 1L); 68 expr = "1"; 69 EXPECT (mpf_fits_ulong_p, 1); 70 EXPECT (mpf_fits_uint_p, 1); 71 EXPECT (mpf_fits_ushort_p, 1); 72 EXPECT (mpf_fits_slong_p, 1); 73 EXPECT (mpf_fits_sint_p, 1); 74 EXPECT (mpf_fits_sshort_p, 1); 75 76 mpf_set_si (f, -1L); 77 expr = "-1"; 78 EXPECT (mpf_fits_ulong_p, 0); 79 EXPECT (mpf_fits_uint_p, 0); 80 EXPECT (mpf_fits_ushort_p, 0); 81 EXPECT (mpf_fits_slong_p, 1); 82 EXPECT (mpf_fits_sint_p, 1); 83 EXPECT (mpf_fits_sshort_p, 1); 84 85 86 mpf_set_ui (f, (unsigned long) USHRT_MAX); 87 expr = "USHRT_MAX"; 88 EXPECT (mpf_fits_ulong_p, 1); 89 EXPECT (mpf_fits_uint_p, 1); 90 EXPECT (mpf_fits_ushort_p, 1); 91 92 mpf_set_ui (f, (unsigned long) USHRT_MAX); 93 mpf_add (f, f, f0p5); 94 expr = "USHRT_MAX + 0.5"; 95 EXPECT (mpf_fits_ulong_p, 1); 96 EXPECT (mpf_fits_uint_p, 1); 97 EXPECT (mpf_fits_ushort_p, 1); 98 99 mpf_set_ui (f, (unsigned long) USHRT_MAX); 100 mpf_add_ui (f, f, 1L); 101 expr = "USHRT_MAX + 1"; 102 EXPECT (mpf_fits_ushort_p, 0); 103 104 105 mpf_set_ui (f, (unsigned long) UINT_MAX); 106 expr = "UINT_MAX"; 107 EXPECT (mpf_fits_ulong_p, 1); 108 EXPECT (mpf_fits_uint_p, 1); 109 110 mpf_set_ui (f, (unsigned long) UINT_MAX); 111 mpf_add (f, f, f0p5); 112 expr = "UINT_MAX + 0.5"; 113 EXPECT (mpf_fits_ulong_p, 1); 114 EXPECT (mpf_fits_uint_p, 1); 115 116 mpf_set_ui (f, (unsigned long) UINT_MAX); 117 mpf_add_ui (f, f, 1L); 118 expr = "UINT_MAX + 1"; 119 EXPECT (mpf_fits_uint_p, 0); 120 121 122 mpf_set_ui (f, ULONG_MAX); 123 expr = "ULONG_MAX"; 124 EXPECT (mpf_fits_ulong_p, 1); 125 126 mpf_set_ui (f, ULONG_MAX); 127 mpf_add (f, f, f0p5); 128 expr = "ULONG_MAX + 0.5"; 129 EXPECT (mpf_fits_ulong_p, 1); 130 131 mpf_set_ui (f, ULONG_MAX); 132 mpf_add_ui (f, f, 1L); 133 expr = "ULONG_MAX + 1"; 134 EXPECT (mpf_fits_ulong_p, 0); 135 136 137 mpf_set_si (f, (long) SHRT_MAX); 138 expr = "SHRT_MAX"; 139 EXPECT (mpf_fits_slong_p, 1); 140 EXPECT (mpf_fits_sint_p, 1); 141 EXPECT (mpf_fits_sshort_p, 1); 142 143 mpf_set_si (f, (long) SHRT_MAX); 144 expr = "SHRT_MAX + 0.5"; 145 mpf_add (f, f, f0p5); 146 EXPECT (mpf_fits_slong_p, 1); 147 EXPECT (mpf_fits_sint_p, 1); 148 EXPECT (mpf_fits_sshort_p, 1); 149 150 mpf_set_si (f, (long) SHRT_MAX); 151 mpf_add_ui (f, f, 1L); 152 expr = "SHRT_MAX + 1"; 153 EXPECT (mpf_fits_sshort_p, 0); 154 155 156 mpf_set_si (f, (long) INT_MAX); 157 expr = "INT_MAX"; 158 EXPECT (mpf_fits_slong_p, 1); 159 EXPECT (mpf_fits_sint_p, 1); 160 161 mpf_set_si (f, (long) INT_MAX); 162 mpf_add (f, f, f0p5); 163 expr = "INT_MAX + 0.5"; 164 EXPECT (mpf_fits_slong_p, 1); 165 EXPECT (mpf_fits_sint_p, 1); 166 167 mpf_set_si (f, (long) INT_MAX); 168 mpf_add_ui (f, f, 1L); 169 expr = "INT_MAX + 1"; 170 EXPECT (mpf_fits_sint_p, 0); 171 172 173 mpf_set_si (f, LONG_MAX); 174 expr = "LONG_MAX"; 175 EXPECT (mpf_fits_slong_p, 1); 176 177 mpf_set_si (f, LONG_MAX); 178 mpf_add (f, f, f0p5); 179 expr = "LONG_MAX + 0.5"; 180 EXPECT (mpf_fits_slong_p, 1); 181 182 mpf_set_si (f, LONG_MAX); 183 mpf_add_ui (f, f, 1L); 184 expr = "LONG_MAX + 1"; 185 EXPECT (mpf_fits_slong_p, 0); 186 187 188 mpf_set_si (f, (long) SHRT_MIN); 189 expr = "SHRT_MIN"; 190 EXPECT (mpf_fits_slong_p, 1); 191 EXPECT (mpf_fits_sint_p, 1); 192 EXPECT (mpf_fits_sshort_p, 1); 193 194 mpf_set_si (f, (long) SHRT_MIN); 195 mpf_sub (f, f, f0p5); 196 expr = "SHRT_MIN - 0.5"; 197 EXPECT (mpf_fits_slong_p, 1); 198 EXPECT (mpf_fits_sint_p, 1); 199 EXPECT (mpf_fits_sshort_p, 1); 200 201 mpf_set_si (f, (long) SHRT_MIN); 202 mpf_sub_ui (f, f, 1L); 203 expr = "SHRT_MIN - 1"; 204 EXPECT (mpf_fits_sshort_p, 0); 205 206 207 mpf_set_si (f, (long) INT_MIN); 208 expr = "INT_MIN"; 209 EXPECT (mpf_fits_slong_p, 1); 210 EXPECT (mpf_fits_sint_p, 1); 211 212 mpf_set_si (f, (long) INT_MIN); 213 mpf_sub (f, f, f0p5); 214 expr = "INT_MIN - 0.5"; 215 EXPECT (mpf_fits_slong_p, 1); 216 EXPECT (mpf_fits_sint_p, 1); 217 218 mpf_set_si (f, (long) INT_MIN); 219 mpf_sub_ui (f, f, 1L); 220 expr = "INT_MIN - 1"; 221 EXPECT (mpf_fits_sint_p, 0); 222 223 224 mpf_set_si (f, LONG_MIN); 225 expr = "LONG_MIN"; 226 EXPECT (mpf_fits_slong_p, 1); 227 228 mpf_set_si (f, LONG_MIN); 229 mpf_sub (f, f, f0p5); 230 expr = "LONG_MIN - 0.5"; 231 EXPECT (mpf_fits_slong_p, 1); 232 233 mpf_set_si (f, LONG_MIN); 234 mpf_sub_ui (f, f, 1L); 235 expr = "LONG_MIN - 1"; 236 EXPECT (mpf_fits_slong_p, 0); 237 238 239 mpf_set_str_or_abort (f, "0.5", 10); 240 expr = "0.5"; 241 EXPECT (mpf_fits_ulong_p, 1); 242 EXPECT (mpf_fits_uint_p, 1); 243 EXPECT (mpf_fits_ushort_p, 1); 244 EXPECT (mpf_fits_slong_p, 1); 245 EXPECT (mpf_fits_sint_p, 1); 246 EXPECT (mpf_fits_sshort_p, 1); 247 248 mpf_set_str_or_abort (f, "-0.5", 10); 249 expr = "-0.5"; 250 EXPECT (mpf_fits_ulong_p, 1); 251 EXPECT (mpf_fits_uint_p, 1); 252 EXPECT (mpf_fits_ushort_p, 1); 253 EXPECT (mpf_fits_slong_p, 1); 254 EXPECT (mpf_fits_sint_p, 1); 255 EXPECT (mpf_fits_sshort_p, 1); 256 257 mpf_set_str_or_abort (f, "-1.5", 10); 258 expr = "-1.5"; 259 EXPECT (mpf_fits_ulong_p, 0); 260 EXPECT (mpf_fits_uint_p, 0); 261 EXPECT (mpf_fits_ushort_p, 0); 262 EXPECT (mpf_fits_slong_p, 1); 263 EXPECT (mpf_fits_sint_p, 1); 264 EXPECT (mpf_fits_sshort_p, 1); 265 266 267 mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16); 268 expr = "1.000000000000000000000000000000000001 base 16"; 269 EXPECT (mpf_fits_ulong_p, 1); 270 EXPECT (mpf_fits_uint_p, 1); 271 EXPECT (mpf_fits_ushort_p, 1); 272 EXPECT (mpf_fits_slong_p, 1); 273 EXPECT (mpf_fits_sint_p, 1); 274 EXPECT (mpf_fits_sshort_p, 1); 275 276 mpf_set_str_or_abort (f, "1@1000", 16); 277 expr = "1@1000 base 16"; 278 EXPECT (mpf_fits_ulong_p, 0); 279 EXPECT (mpf_fits_uint_p, 0); 280 EXPECT (mpf_fits_ushort_p, 0); 281 EXPECT (mpf_fits_slong_p, 0); 282 EXPECT (mpf_fits_sint_p, 0); 283 EXPECT (mpf_fits_sshort_p, 0); 284 285 286 mpf_set_ui (f, 1L); 287 mpf_mul_2exp (f, f, BITS_PER_ULONG + 1); 288 mpf_sub_ui (f, f, 1L); 289 expr = "2^(BITS_PER_ULONG+1) - 1"; 290 EXPECT (mpf_fits_ulong_p, 0); 291 EXPECT (mpf_fits_uint_p, 0); 292 EXPECT (mpf_fits_ushort_p, 0); 293 EXPECT (mpf_fits_slong_p, 0); 294 EXPECT (mpf_fits_sint_p, 0); 295 EXPECT (mpf_fits_sshort_p, 0); 296 297 mpf_set_ui (f, 1L); 298 mpf_mul_2exp (f, f, BITS_PER_ULONG + 1); 299 mpf_ui_sub (f, 1L, f); 300 expr = "- (2^(BITS_PER_ULONG+1) - 1)"; 301 EXPECT (mpf_fits_ulong_p, 0); 302 EXPECT (mpf_fits_uint_p, 0); 303 EXPECT (mpf_fits_ushort_p, 0); 304 EXPECT (mpf_fits_slong_p, 0); 305 EXPECT (mpf_fits_sint_p, 0); 306 EXPECT (mpf_fits_sshort_p, 0); 307 308 mpf_set_ui (f, 1L); 309 mpf_mul_2exp (f, f, BITS_PER_ULONG + 5); 310 mpf_sub_ui (f, f, 1L); 311 expr = "2^(BITS_PER_ULONG+5) - 1"; 312 EXPECT (mpf_fits_ulong_p, 0); 313 EXPECT (mpf_fits_uint_p, 0); 314 EXPECT (mpf_fits_ushort_p, 0); 315 EXPECT (mpf_fits_slong_p, 0); 316 EXPECT (mpf_fits_sint_p, 0); 317 EXPECT (mpf_fits_sshort_p, 0); 318 319 320 if (error) 321 abort (); 322 323 mpf_clear (f); 324 mpf_clear (f0p5); 325 tests_end (); 326 exit (0); 327 } 328