github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/cxx/t-misc.cc (about) 1 /* Test mp*_class functions. 2 3 Copyright 2002, 2003 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 21 /* Note that we don't use <climits> for LONG_MIN, but instead our own 22 definitions in gmp-impl.h. In g++ 2.95.4 (debian 3.0) under 23 -mcpu=ultrasparc, limits.h sees __sparc_v9__ defined and assumes that 24 means long is 64-bit long, but it's only 32-bits, causing fatal compile 25 errors. */ 26 27 #include "config.h" 28 29 #include <string> 30 31 #include "gmp.h" 32 #include "gmpxx.h" 33 #include "gmp-impl.h" 34 #include "tests.h" 35 36 using namespace std; 37 38 39 void 40 check_mpz (void) 41 { 42 // mpz_class::fits_sint_p 43 { 44 bool fits; 45 mpz_class z; 46 z = INT_MIN; fits = z.fits_sint_p(); ASSERT_ALWAYS (fits); 47 z--; fits = z.fits_sint_p(); ASSERT_ALWAYS (! fits); 48 z = INT_MAX; fits = z.fits_sint_p(); ASSERT_ALWAYS (fits); 49 z++; fits = z.fits_sint_p(); ASSERT_ALWAYS (! fits); 50 } 51 52 // mpz_class::fits_uint_p 53 { 54 bool fits; 55 mpz_class z; 56 z = 0; fits = z.fits_uint_p(); ASSERT_ALWAYS (fits); 57 z--; fits = z.fits_uint_p(); ASSERT_ALWAYS (! fits); 58 z = UINT_MAX; fits = z.fits_uint_p(); ASSERT_ALWAYS (fits); 59 z++; fits = z.fits_uint_p(); ASSERT_ALWAYS (! fits); 60 } 61 62 // mpz_class::fits_slong_p 63 { 64 bool fits; 65 mpz_class z; 66 z = LONG_MIN; fits = z.fits_slong_p(); ASSERT_ALWAYS (fits); 67 z--; fits = z.fits_slong_p(); ASSERT_ALWAYS (! fits); 68 z = LONG_MAX; fits = z.fits_slong_p(); ASSERT_ALWAYS (fits); 69 z++; fits = z.fits_slong_p(); ASSERT_ALWAYS (! fits); 70 } 71 72 // mpz_class::fits_ulong_p 73 { 74 bool fits; 75 mpz_class z; 76 z = 0; fits = z.fits_ulong_p(); ASSERT_ALWAYS (fits); 77 z--; fits = z.fits_ulong_p(); ASSERT_ALWAYS (! fits); 78 z = ULONG_MAX; fits = z.fits_ulong_p(); ASSERT_ALWAYS (fits); 79 z++; fits = z.fits_ulong_p(); ASSERT_ALWAYS (! fits); 80 } 81 82 // mpz_class::fits_sshort_p 83 { 84 bool fits; 85 mpz_class z; 86 z = SHRT_MIN; fits = z.fits_sshort_p(); ASSERT_ALWAYS (fits); 87 z--; fits = z.fits_sshort_p(); ASSERT_ALWAYS (! fits); 88 z = SHRT_MAX; fits = z.fits_sshort_p(); ASSERT_ALWAYS (fits); 89 z++; fits = z.fits_sshort_p(); ASSERT_ALWAYS (! fits); 90 } 91 92 // mpz_class::fits_ushort_p 93 { 94 bool fits; 95 mpz_class z; 96 z = 0; fits = z.fits_ushort_p(); ASSERT_ALWAYS (fits); 97 z--; fits = z.fits_ushort_p(); ASSERT_ALWAYS (! fits); 98 z = USHRT_MAX; fits = z.fits_ushort_p(); ASSERT_ALWAYS (fits); 99 z++; fits = z.fits_ushort_p(); ASSERT_ALWAYS (! fits); 100 } 101 102 // mpz_class::get_mpz_t 103 { 104 mpz_class z(0); 105 mpz_ptr p = z.get_mpz_t(); 106 ASSERT_ALWAYS (mpz_cmp_ui (p, 0) == 0); 107 } 108 { 109 mpz_class z(0); 110 mpz_srcptr p = z.get_mpz_t(); 111 ASSERT_ALWAYS (mpz_cmp_ui (p, 0) == 0); 112 } 113 114 // mpz_class::get_d 115 // mpz_class::get_si 116 // mpz_class::get_ui 117 { 118 mpz_class z(123); 119 { double d = z.get_d(); ASSERT_ALWAYS (d == 123.0); } 120 { long l = z.get_si(); ASSERT_ALWAYS (l == 123L); } 121 { long u = z.get_ui(); ASSERT_ALWAYS (u == 123L); } 122 } 123 { 124 mpz_class z(-123); 125 { double d = z.get_d(); ASSERT_ALWAYS (d == -123.0); } 126 { long l = z.get_si(); ASSERT_ALWAYS (l == -123L); } 127 } 128 129 // mpz_class::get_str 130 { 131 mpz_class z(123); 132 string s; 133 s = z.get_str(); ASSERT_ALWAYS (s == "123"); 134 s = z.get_str(16); ASSERT_ALWAYS (s == "7b"); 135 s = z.get_str(-16); ASSERT_ALWAYS (s == "7B"); 136 } 137 138 // mpz_class::set_str 139 { 140 mpz_class z; 141 int ret; 142 ret = z.set_str ("123", 10); ASSERT_ALWAYS (ret == 0 && z == 123); 143 ret = z.set_str ("7b", 16); ASSERT_ALWAYS (ret == 0 && z == 123); 144 ret = z.set_str ("7B", 16); ASSERT_ALWAYS (ret == 0 && z == 123); 145 ret = z.set_str ("0x7B", 0); ASSERT_ALWAYS (ret == 0 && z == 123); 146 147 ret = z.set_str (string("123"), 10); ASSERT_ALWAYS (ret == 0 && z == 123); 148 ret = z.set_str (string("7b"), 16); ASSERT_ALWAYS (ret == 0 && z == 123); 149 ret = z.set_str (string("7B"), 16); ASSERT_ALWAYS (ret == 0 && z == 123); 150 ret = z.set_str (string("0x7B"), 0); ASSERT_ALWAYS (ret == 0 && z == 123); 151 } 152 } 153 154 void 155 check_mpq (void) 156 { 157 // mpq_class::canonicalize 158 { 159 mpq_class q(12,9); 160 q.canonicalize(); 161 ASSERT_ALWAYS (q.get_num() == 4); 162 ASSERT_ALWAYS (q.get_den() == 3); 163 } 164 165 // mpq_class::get_d 166 { 167 mpq_class q(123); 168 { double d = q.get_d(); ASSERT_ALWAYS (d == 123.0); } 169 } 170 { 171 mpq_class q(-123); 172 { double d = q.get_d(); ASSERT_ALWAYS (d == -123.0); } 173 } 174 175 // mpq_class::get_mpq_t 176 { 177 mpq_class q(0); 178 mpq_ptr p = q.get_mpq_t(); 179 ASSERT_ALWAYS (mpq_cmp_ui (p, 0, 1) == 0); 180 } 181 { 182 mpq_class q(0); 183 mpq_srcptr p = q.get_mpq_t(); 184 ASSERT_ALWAYS (mpq_cmp_ui (p, 0, 1) == 0); 185 } 186 187 // mpq_class::get_num, mpq_class::get_den 188 { 189 const mpq_class q(4,5); 190 mpz_class z; 191 z = q.get_num(); ASSERT_ALWAYS (z == 4); 192 z = q.get_den(); ASSERT_ALWAYS (z == 5); 193 } 194 195 // mpq_class::get_num_mpz_t, mpq_class::get_den_mpz_t 196 { 197 mpq_class q(4,5); 198 mpz_ptr p; 199 p = q.get_num_mpz_t(); ASSERT_ALWAYS (mpz_cmp_ui (p, 4) == 0); 200 p = q.get_den_mpz_t(); ASSERT_ALWAYS (mpz_cmp_ui (p, 5) == 0); 201 } 202 { 203 const mpq_class q(4,5); 204 mpz_srcptr p; 205 p = q.get_num_mpz_t(); ASSERT_ALWAYS (mpz_cmp_ui (p, 4) == 0); 206 p = q.get_den_mpz_t(); ASSERT_ALWAYS (mpz_cmp_ui (p, 5) == 0); 207 } 208 209 // mpq_class::get_str 210 { 211 mpq_class q(17,11); 212 string s; 213 s = q.get_str(); ASSERT_ALWAYS (s == "17/11"); 214 s = q.get_str(10); ASSERT_ALWAYS (s == "17/11"); 215 s = q.get_str(16); ASSERT_ALWAYS (s == "11/b"); 216 s = q.get_str(-16); ASSERT_ALWAYS (s == "11/B"); 217 } 218 219 // mpq_class::set_str 220 { 221 mpq_class q; 222 int ret; 223 ret = q.set_str ("123", 10); ASSERT_ALWAYS (ret == 0 && q == 123); 224 ret = q.set_str ("4/5", 10); ASSERT_ALWAYS (ret == 0 && q == mpq_class(4,5)); 225 ret = q.set_str ("7b", 16); ASSERT_ALWAYS (ret == 0 && q == 123); 226 ret = q.set_str ("7B", 16); ASSERT_ALWAYS (ret == 0 && q == 123); 227 ret = q.set_str ("0x7B", 0); ASSERT_ALWAYS (ret == 0 && q == 123); 228 ret = q.set_str ("0x10/17", 0); ASSERT_ALWAYS (ret == 0 && q == mpq_class(16,17)); 229 230 ret = q.set_str (string("4/5"), 10); ASSERT_ALWAYS (ret == 0 && q == mpq_class(4,5)); 231 ret = q.set_str (string("123"), 10); ASSERT_ALWAYS (ret == 0 && q == 123); 232 ret = q.set_str (string("7b"), 16); ASSERT_ALWAYS (ret == 0 && q == 123); 233 ret = q.set_str (string("7B"), 16); ASSERT_ALWAYS (ret == 0 && q == 123); 234 ret = q.set_str (string("0x7B"), 0); ASSERT_ALWAYS (ret == 0 && q == 123); 235 ret = q.set_str (string("0x10/17"), 0); ASSERT_ALWAYS (ret == 0 && q == mpq_class(16,17)); 236 } 237 } 238 239 void 240 check_mpf (void) 241 { 242 // mpf_class::fits_sint_p 243 { 244 bool fits; 245 mpf_class f (0, 2*8*sizeof(int)); 246 f = INT_MIN; fits = f.fits_sint_p(); ASSERT_ALWAYS (fits); 247 f--; fits = f.fits_sint_p(); ASSERT_ALWAYS (! fits); 248 f = INT_MAX; fits = f.fits_sint_p(); ASSERT_ALWAYS (fits); 249 f++; fits = f.fits_sint_p(); ASSERT_ALWAYS (! fits); 250 } 251 252 // mpf_class::fits_uint_p 253 { 254 bool fits; 255 mpf_class f (0, 2*8*sizeof(int)); 256 f = 0; fits = f.fits_uint_p(); ASSERT_ALWAYS (fits); 257 f--; fits = f.fits_uint_p(); ASSERT_ALWAYS (! fits); 258 f = UINT_MAX; fits = f.fits_uint_p(); ASSERT_ALWAYS (fits); 259 f++; fits = f.fits_uint_p(); ASSERT_ALWAYS (! fits); 260 } 261 262 // mpf_class::fits_slong_p 263 { 264 bool fits; 265 mpf_class f (0, 2*8*sizeof(long)); 266 f = LONG_MIN; fits = f.fits_slong_p(); ASSERT_ALWAYS (fits); 267 f--; fits = f.fits_slong_p(); ASSERT_ALWAYS (! fits); 268 f = LONG_MAX; fits = f.fits_slong_p(); ASSERT_ALWAYS (fits); 269 f++; fits = f.fits_slong_p(); ASSERT_ALWAYS (! fits); 270 } 271 272 // mpf_class::fits_ulong_p 273 { 274 bool fits; 275 mpf_class f (0, 2*8*sizeof(long)); 276 f = 0; fits = f.fits_ulong_p(); ASSERT_ALWAYS (fits); 277 f--; fits = f.fits_ulong_p(); ASSERT_ALWAYS (! fits); 278 f = ULONG_MAX; fits = f.fits_ulong_p(); ASSERT_ALWAYS (fits); 279 f++; fits = f.fits_ulong_p(); ASSERT_ALWAYS (! fits); 280 } 281 282 // mpf_class::fits_sshort_p 283 { 284 bool fits; 285 mpf_class f (0, 2*8*sizeof(short)); 286 f = SHRT_MIN; fits = f.fits_sshort_p(); ASSERT_ALWAYS (fits); 287 f--; fits = f.fits_sshort_p(); ASSERT_ALWAYS (! fits); 288 f = SHRT_MAX; fits = f.fits_sshort_p(); ASSERT_ALWAYS (fits); 289 f++; fits = f.fits_sshort_p(); ASSERT_ALWAYS (! fits); 290 } 291 292 // mpf_class::fits_ushort_p 293 { 294 bool fits; 295 mpf_class f (0, 2*8*sizeof(short)); 296 f = 0; fits = f.fits_ushort_p(); ASSERT_ALWAYS (fits); 297 f--; fits = f.fits_ushort_p(); ASSERT_ALWAYS (! fits); 298 f = USHRT_MAX; fits = f.fits_ushort_p(); ASSERT_ALWAYS (fits); 299 f++; fits = f.fits_ushort_p(); ASSERT_ALWAYS (! fits); 300 } 301 302 // mpf_class::get_d 303 // mpf_class::get_si 304 // mpf_class::get_ui 305 { 306 mpf_class f(123); 307 { double d = f.get_d(); ASSERT_ALWAYS (d == 123.0); } 308 { long l = f.get_si(); ASSERT_ALWAYS (l == 123L); } 309 { long u = f.get_ui(); ASSERT_ALWAYS (u == 123L); } 310 } 311 { 312 mpf_class f(-123); 313 { double d = f.get_d(); ASSERT_ALWAYS (d == -123.0); } 314 { long l = f.get_si(); ASSERT_ALWAYS (l == -123L); } 315 } 316 317 // mpf_class::get_prec 318 { 319 mpf_class f; 320 ASSERT_ALWAYS (f.get_prec() == mpf_get_default_prec()); 321 } 322 323 // mpf_class::get_str 324 { 325 mpf_class f(123); 326 string s; 327 mp_exp_t e; 328 s = f.get_str(e); ASSERT_ALWAYS (s == "123" && e == 3); 329 s = f.get_str(e, 16); ASSERT_ALWAYS (s == "7b" && e == 2); 330 s = f.get_str(e, -16); ASSERT_ALWAYS (s == "7B" && e == 2); 331 s = f.get_str(e, 10, 2); ASSERT_ALWAYS (s == "12" && e == 3); 332 s = f.get_str(e, 10, 1); ASSERT_ALWAYS (s == "1" && e == 3); 333 } 334 335 // mpf_class::set_str 336 { 337 mpf_class f; 338 int ret; 339 ret = f.set_str ("123", 10); ASSERT_ALWAYS (ret == 0 && f == 123); 340 ret = f.set_str ("123e1", 10); ASSERT_ALWAYS (ret == 0 && f == 1230); 341 ret = f.set_str ("1230e-1", 10); ASSERT_ALWAYS (ret == 0 && f == 123); 342 ret = f.set_str ("7b", 16); ASSERT_ALWAYS (ret == 0 && f == 123); 343 ret = f.set_str ("7B", 16); ASSERT_ALWAYS (ret == 0 && f == 123); 344 ret = f.set_str ("7B@1", 16); ASSERT_ALWAYS (ret == 0 && f == 1968); 345 ret = f.set_str ("7B0@-1", 16); ASSERT_ALWAYS (ret == 0 && f == 123); 346 347 ret = f.set_str (string("123"), 10); ASSERT_ALWAYS (ret == 0 && f == 123); 348 ret = f.set_str (string("123e1"), 10); ASSERT_ALWAYS (ret == 0 && f == 1230); 349 ret = f.set_str (string("1230e-1"), 10); ASSERT_ALWAYS (ret == 0 && f == 123); 350 ret = f.set_str (string("7b"), 16); ASSERT_ALWAYS (ret == 0 && f == 123); 351 ret = f.set_str (string("7B"), 16); ASSERT_ALWAYS (ret == 0 && f == 123); 352 ret = f.set_str (string("7B@1"), 16); ASSERT_ALWAYS (ret == 0 && f == 1968); 353 ret = f.set_str (string("7B0@-1"), 16); ASSERT_ALWAYS (ret == 0 && f == 123); 354 } 355 356 // mpf_class::set_prec 357 { 358 mpf_class f; 359 f.set_prec (256); 360 ASSERT_ALWAYS (f.get_prec () >= 256); 361 } 362 363 // mpf_class::set_prec_raw 364 { 365 mpf_class f (0, 100 * GMP_NUMB_BITS); 366 f.set_prec_raw (5 * GMP_NUMB_BITS); 367 ASSERT_ALWAYS (f.get_prec () >= 5 * GMP_NUMB_BITS); 368 ASSERT_ALWAYS (f.get_prec () < 100 * GMP_NUMB_BITS); 369 f.set_prec_raw (100 * GMP_NUMB_BITS); 370 } 371 } 372 373 // std::numeric_limits 374 void 375 check_limits (void) 376 { 377 // Check that the content is not private. 378 ASSERT_ALWAYS ( std::numeric_limits<mpz_class>::is_integer); 379 ASSERT_ALWAYS (!std::numeric_limits<mpf_class>::is_integer); 380 381 // Check that symbols are emitted. 382 ASSERT_ALWAYS (&std::numeric_limits<mpz_class>::is_integer 383 != &std::numeric_limits<mpq_class>::is_integer); 384 } 385 386 int 387 main (void) 388 { 389 tests_start(); 390 391 check_mpz(); 392 check_mpq(); 393 check_mpf(); 394 check_limits(); 395 396 tests_end(); 397 return 0; 398 }