1 /* Test file for: 2 mpfr_fits_sint_p, mpfr_fits_slong_p, mpfr_fits_sshort_p, 3 mpfr_fits_uint_p, mpfr_fits_ulong_p, mpfr_fits_ushort_p 4 5 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 6 Contributed by the Arenaire and Cacao projects, INRIA. 7 8 This file is part of the GNU MPFR Library. 9 10 The GNU MPFR Library is free software; you can redistribute it and/or modify 11 it under the terms of the GNU Lesser General Public License as published by 12 the Free Software Foundation; either version 3 of the License, or (at your 13 option) any later version. 14 15 The GNU MPFR Library is distributed in the hope that it will be useful, but 16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 18 License for more details. 19 20 You should have received a copy of the GNU Lesser General Public License 21 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 22 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 24 25 #ifdef HAVE_CONFIG_H 26 # include "config.h" /* for a build within gmp */ 27 #endif 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <limits.h> 32 33 /* The ISO C99 standard specifies that in C++ implementations the 34 INTMAX_MAX, ... macros should only be defined if explicitly requested. */ 35 #if defined __cplusplus 36 # define __STDC_LIMIT_MACROS 37 # define __STDC_CONSTANT_MACROS 38 #endif 39 40 #if HAVE_INTTYPES_H 41 # include <inttypes.h> /* for intmax_t */ 42 #else 43 # if HAVE_STDINT_H 44 # include <stdint.h> 45 # endif 46 #endif 47 48 #include "mpfr-test.h" 49 50 #define ERROR1 { printf("Initial error for x="); mpfr_dump(x); exit(1); } 51 #define ERROR2 { printf("Error for x="); mpfr_dump(x); exit(1); } 52 53 static void check_intmax (void); 54 55 int 56 main (void) 57 { 58 mpfr_t x; 59 60 tests_start_mpfr (); 61 62 mpfr_init2 (x, 256); 63 64 /* Check NAN */ 65 mpfr_set_nan (x); 66 if (mpfr_fits_ulong_p (x, MPFR_RNDN)) 67 ERROR1; 68 if (mpfr_fits_slong_p (x, MPFR_RNDN)) 69 ERROR1; 70 if (mpfr_fits_uint_p (x, MPFR_RNDN)) 71 ERROR1; 72 if (mpfr_fits_sint_p (x, MPFR_RNDN)) 73 ERROR1; 74 if (mpfr_fits_ushort_p (x, MPFR_RNDN)) 75 ERROR1; 76 if (mpfr_fits_sshort_p (x, MPFR_RNDN)) 77 ERROR1; 78 79 /* Check INF */ 80 mpfr_set_inf (x, 1); 81 if (mpfr_fits_ulong_p (x, MPFR_RNDN)) 82 ERROR1; 83 if (mpfr_fits_slong_p (x, MPFR_RNDN)) 84 ERROR1; 85 if (mpfr_fits_uint_p (x, MPFR_RNDN)) 86 ERROR1; 87 if (mpfr_fits_sint_p (x, MPFR_RNDN)) 88 ERROR1; 89 if (mpfr_fits_ushort_p (x, MPFR_RNDN)) 90 ERROR1; 91 if (mpfr_fits_sshort_p (x, MPFR_RNDN)) 92 ERROR1; 93 94 /* Check Zero */ 95 MPFR_SET_ZERO (x); 96 if (!mpfr_fits_ulong_p (x, MPFR_RNDN)) 97 ERROR2; 98 if (!mpfr_fits_slong_p (x, MPFR_RNDN)) 99 ERROR2; 100 if (!mpfr_fits_uint_p (x, MPFR_RNDN)) 101 ERROR2; 102 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 103 ERROR2; 104 if (!mpfr_fits_ushort_p (x, MPFR_RNDN)) 105 ERROR2; 106 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 107 ERROR2; 108 109 /* Check small op */ 110 mpfr_set_str1 (x, "1@-1"); 111 if (!mpfr_fits_ulong_p (x, MPFR_RNDN)) 112 ERROR2; 113 if (!mpfr_fits_slong_p (x, MPFR_RNDN)) 114 ERROR2; 115 if (!mpfr_fits_uint_p (x, MPFR_RNDN)) 116 ERROR2; 117 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 118 ERROR2; 119 if (!mpfr_fits_ushort_p (x, MPFR_RNDN)) 120 ERROR2; 121 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 122 ERROR2; 123 124 /* Check 17 */ 125 mpfr_set_ui (x, 17, MPFR_RNDN); 126 if (!mpfr_fits_ulong_p (x, MPFR_RNDN)) 127 ERROR2; 128 if (!mpfr_fits_slong_p (x, MPFR_RNDN)) 129 ERROR2; 130 if (!mpfr_fits_uint_p (x, MPFR_RNDN)) 131 ERROR2; 132 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 133 ERROR2; 134 if (!mpfr_fits_ushort_p (x, MPFR_RNDN)) 135 ERROR2; 136 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 137 ERROR2; 138 139 /* Check all other values */ 140 mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); 141 mpfr_mul_2exp (x, x, 1, MPFR_RNDN); 142 if (mpfr_fits_ulong_p (x, MPFR_RNDN)) 143 ERROR1; 144 if (mpfr_fits_slong_p (x, MPFR_RNDN)) 145 ERROR1; 146 mpfr_mul_2exp (x, x, 40, MPFR_RNDN); 147 if (mpfr_fits_ulong_p (x, MPFR_RNDN)) 148 ERROR1; 149 if (mpfr_fits_uint_p (x, MPFR_RNDN)) 150 ERROR1; 151 if (mpfr_fits_sint_p (x, MPFR_RNDN)) 152 ERROR1; 153 if (mpfr_fits_ushort_p (x, MPFR_RNDN)) 154 ERROR1; 155 if (mpfr_fits_sshort_p (x, MPFR_RNDN)) 156 ERROR1; 157 158 mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); 159 if (!mpfr_fits_ulong_p (x, MPFR_RNDN)) 160 ERROR2; 161 mpfr_set_ui (x, LONG_MAX, MPFR_RNDN); 162 if (!mpfr_fits_slong_p (x, MPFR_RNDN)) 163 ERROR2; 164 mpfr_set_ui (x, UINT_MAX, MPFR_RNDN); 165 if (!mpfr_fits_uint_p (x, MPFR_RNDN)) 166 ERROR2; 167 mpfr_set_ui (x, INT_MAX, MPFR_RNDN); 168 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 169 ERROR2; 170 mpfr_set_ui (x, USHRT_MAX, MPFR_RNDN); 171 if (!mpfr_fits_ushort_p (x, MPFR_RNDN)) 172 ERROR2; 173 mpfr_set_ui (x, SHRT_MAX, MPFR_RNDN); 174 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 175 ERROR2; 176 177 mpfr_set_si (x, 1, MPFR_RNDN); 178 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 179 ERROR2; 180 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 181 ERROR2; 182 183 /* Check negative value */ 184 mpfr_set_si (x, -1, MPFR_RNDN); 185 if (!mpfr_fits_sint_p (x, MPFR_RNDN)) 186 ERROR2; 187 if (!mpfr_fits_sshort_p (x, MPFR_RNDN)) 188 ERROR2; 189 if (!mpfr_fits_slong_p (x, MPFR_RNDN)) 190 ERROR2; 191 if (mpfr_fits_uint_p (x, MPFR_RNDN)) 192 ERROR1; 193 if (mpfr_fits_ushort_p (x, MPFR_RNDN)) 194 ERROR1; 195 if (mpfr_fits_ulong_p (x, MPFR_RNDN)) 196 ERROR1; 197 198 mpfr_clear (x); 199 200 check_intmax (); 201 202 tests_end_mpfr (); 203 return 0; 204 } 205 206 static void check_intmax (void) 207 { 208 #ifdef _MPFR_H_HAVE_INTMAX_T 209 mpfr_t x; 210 211 mpfr_init2 (x, sizeof (uintmax_t)*CHAR_BIT); 212 213 /* Check NAN */ 214 mpfr_set_nan (x); 215 if (mpfr_fits_uintmax_p (x, MPFR_RNDN)) 216 ERROR1; 217 if (mpfr_fits_intmax_p (x, MPFR_RNDN)) 218 ERROR1; 219 220 /* Check INF */ 221 mpfr_set_inf (x, 1); 222 if (mpfr_fits_uintmax_p (x, MPFR_RNDN)) 223 ERROR1; 224 if (mpfr_fits_intmax_p (x, MPFR_RNDN)) 225 ERROR1; 226 227 /* Check Zero */ 228 MPFR_SET_ZERO (x); 229 if (!mpfr_fits_uintmax_p (x, MPFR_RNDN)) 230 ERROR2; 231 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 232 ERROR2; 233 234 /* Check small op */ 235 mpfr_set_str1 (x, "1@-1"); 236 if (!mpfr_fits_uintmax_p (x, MPFR_RNDN)) 237 ERROR2; 238 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 239 ERROR2; 240 241 /* Check 17 */ 242 mpfr_set_ui (x, 17, MPFR_RNDN); 243 if (!mpfr_fits_uintmax_p (x, MPFR_RNDN)) 244 ERROR2; 245 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 246 ERROR2; 247 248 /* Check hugest */ 249 mpfr_set_ui_2exp (x, 42, sizeof (uintmax_t) * 32, MPFR_RNDN); 250 if (mpfr_fits_uintmax_p (x, MPFR_RNDN)) 251 ERROR1; 252 if (mpfr_fits_intmax_p (x, MPFR_RNDN)) 253 ERROR1; 254 255 /* Check all other values */ 256 mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); 257 mpfr_add_ui (x, x, 1, MPFR_RNDN); 258 if (mpfr_fits_uintmax_p (x, MPFR_RNDN)) 259 ERROR1; 260 mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); 261 if (!mpfr_fits_uintmax_p (x, MPFR_RNDN)) 262 ERROR2; 263 mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN); 264 mpfr_add_ui (x, x, 1, MPFR_RNDN); 265 if (mpfr_fits_intmax_p (x, MPFR_RNDN)) 266 ERROR1; 267 mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN); 268 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 269 ERROR2; 270 mpfr_set_sj (x, MPFR_INTMAX_MIN, MPFR_RNDN); 271 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 272 ERROR2; 273 mpfr_sub_ui (x, x, 1, MPFR_RNDN); 274 if (mpfr_fits_intmax_p (x, MPFR_RNDN)) 275 ERROR1; 276 277 /* Check negative value */ 278 mpfr_set_si (x, -1, MPFR_RNDN); 279 if (!mpfr_fits_intmax_p (x, MPFR_RNDN)) 280 ERROR2; 281 if (mpfr_fits_uintmax_p (x, MPFR_RNDN)) 282 ERROR1; 283 284 mpfr_clear (x); 285 #endif 286 } 287 288