t-constr.cc revision 1.1.1.5
1/* Test mp*_class constructors. 2 3Copyright 2001-2003 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library test suite. 6 7The GNU MP Library test suite is free software; you can redistribute it 8and/or modify it under the terms of the GNU General Public License as 9published by the Free Software Foundation; either version 3 of the License, 10or (at your option) any later version. 11 12The GNU MP Library test suite is distributed in the hope that it will be 13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15Public License for more details. 16 17You should have received a copy of the GNU General Public License along with 18the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 19 20#include "config.h" 21 22#include <iostream> 23#include <string> 24 25#include "gmpxx.h" 26#include "gmp-impl.h" 27#include "tests.h" 28 29using namespace std; 30 31 32void 33check_mpz (void) 34{ 35 // mpz_class() 36 { 37 mpz_class a; ASSERT_ALWAYS(a == 0); 38 } 39 40 // mpz_class(const mpz_class &) 41 // see below 42 43 // template <class T, class U> mpz_class(const __gmp_expr<T, U> &) 44 // not tested here, see t-unary.cc, t-binary.cc 45 46 // mpz_class(signed char) 47 { 48 signed char a = -127; 49 mpz_class b(a); ASSERT_ALWAYS(b == -127); 50 } 51 52 // mpz_class(unsigned char) 53 { 54 unsigned char a = 255; 55 mpz_class b(a); ASSERT_ALWAYS(b == 255); 56 } 57 58 // either signed or unsigned char, machine dependent 59 { 60 mpz_class a('A'); ASSERT_ALWAYS(a == 65); 61 } 62 { 63 mpz_class a('z'); ASSERT_ALWAYS(a == 122); 64 } 65 66 // mpz_class(signed int) 67 { 68 signed int a = 0; 69 mpz_class b(a); ASSERT_ALWAYS(b == 0); 70 } 71 { 72 signed int a = -123; 73 mpz_class b(a); ASSERT_ALWAYS(b == -123); 74 } 75 { 76 signed int a = 4567; 77 mpz_class b(a); ASSERT_ALWAYS(b == 4567); 78 } 79 80 // mpz_class(unsigned int) 81 { 82 unsigned int a = 890; 83 mpz_class b(a); ASSERT_ALWAYS(b == 890); 84 } 85 86 // mpz_class(signed short int) 87 { 88 signed short int a = -12345; 89 mpz_class b(a); ASSERT_ALWAYS(b == -12345); 90 } 91 92 // mpz_class(unsigned short int) 93 { 94 unsigned short int a = 54321u; 95 mpz_class b(a); ASSERT_ALWAYS(b == 54321u); 96 } 97 98 // mpz_class(signed long int) 99 { 100 signed long int a = -1234567890L; 101 mpz_class b(a); ASSERT_ALWAYS(b == -1234567890L); 102 } 103 104 // mpz_class(unsigned long int) 105 { 106 unsigned long int a = 1UL << 30; 107 mpz_class b(a); ASSERT_ALWAYS(b == 1073741824L); 108 } 109 110 // mpz_class(float) 111 { 112 float a = 123.45; 113 mpz_class b(a); ASSERT_ALWAYS(b == 123); 114 } 115 116 // mpz_class(double) 117 { 118 double a = 3.141592653589793238; 119 mpz_class b(a); ASSERT_ALWAYS(b == 3); 120 } 121 122 // mpz_class(long double) 123 // currently not implemented 124 125 // mpz_class(const char *) 126 { 127 const char *a = "1234567890"; 128 mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L); 129 } 130 131 // mpz_class(const char *, int) 132 { 133 const char *a = "FFFF"; 134 int base = 16; 135 mpz_class b(a, base); ASSERT_ALWAYS(b == 65535u); 136 } 137 138 // mpz_class(const std::string &) 139 { 140 string a("1234567890"); 141 mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L); 142 } 143 144 // mpz_class(const std::string &, int) 145 { 146 string a("7777"); 147 int base = 8; 148 mpz_class b(a, base); ASSERT_ALWAYS(b == 4095); 149 } 150 151 // mpz_class(const char *) with invalid 152 { 153 try { 154 const char *a = "ABC"; 155 mpz_class b(a); 156 ASSERT_ALWAYS (0); /* should not be reached */ 157 } catch (invalid_argument&) { 158 } 159 } 160 161 // mpz_class(const char *, int) with invalid 162 { 163 try { 164 const char *a = "GHI"; 165 int base = 16; 166 mpz_class b(a, base); 167 ASSERT_ALWAYS (0); /* should not be reached */ 168 } catch (invalid_argument&) { 169 } 170 } 171 172 // mpz_class(const std::string &) with invalid 173 { 174 try { 175 string a("abc"); 176 mpz_class b(a); 177 ASSERT_ALWAYS (0); /* should not be reached */ 178 } catch (invalid_argument&) { 179 } 180 } 181 182 // mpz_class(const std::string &, int) with invalid 183 { 184 try { 185 string a("ZZZ"); 186 int base = 8; 187 mpz_class b(a, base); 188 ASSERT_ALWAYS (0); /* should not be reached */ 189 } catch (invalid_argument&) { 190 } 191 } 192 193 // mpz_class(mpz_srcptr) 194 { 195 mpz_t a; 196 mpz_init_set_ui(a, 100); 197 mpz_class b(a); ASSERT_ALWAYS(b == 100); 198 mpz_clear(a); 199 } 200 201 // mpz_class(const mpz_class &) 202 { 203 mpz_class a(12345); // tested above, assume it works 204 mpz_class b(a); ASSERT_ALWAYS(b == 12345); 205 } 206 207 // no constructor for bool, but it gets casted to int 208 { 209 bool a = true; 210 mpz_class b(a); ASSERT_ALWAYS(b == 1); 211 } 212 { 213 bool a = false; 214 mpz_class b(a); ASSERT_ALWAYS(b == 0); 215 } 216} 217 218void 219check_mpq (void) 220{ 221 // mpq_class() 222 { 223 mpq_class a; ASSERT_ALWAYS(a == 0); 224 } 225 226 // mpq_class(const mpq_class &) 227 // see below 228 229 // template <class T, class U> mpq_class(const __gmp_expr<T, U> &) 230 // not tested here, see t-unary.cc, t-binary.cc 231 232 // mpq_class(signed char) 233 { 234 signed char a = -127; 235 mpq_class b(a); ASSERT_ALWAYS(b == -127); 236 } 237 238 // mpq_class(unsigned char) 239 { 240 unsigned char a = 255; 241 mpq_class b(a); ASSERT_ALWAYS(b == 255); 242 } 243 244 // either signed or unsigned char, machine dependent 245 { 246 mpq_class a('A'); ASSERT_ALWAYS(a == 65); 247 } 248 { 249 mpq_class a('z'); ASSERT_ALWAYS(a == 122); 250 } 251 252 // mpq_class(signed int) 253 { 254 signed int a = 0; 255 mpq_class b(a); ASSERT_ALWAYS(b == 0); 256 } 257 { 258 signed int a = -123; 259 mpq_class b(a); ASSERT_ALWAYS(b == -123); 260 } 261 { 262 signed int a = 4567; 263 mpq_class b(a); ASSERT_ALWAYS(b == 4567); 264 } 265 266 // mpq_class(unsigned int) 267 { 268 unsigned int a = 890; 269 mpq_class b(a); ASSERT_ALWAYS(b == 890); 270 } 271 272 // mpq_class(signed short int) 273 { 274 signed short int a = -12345; 275 mpq_class b(a); ASSERT_ALWAYS(b == -12345); 276 } 277 278 // mpq_class(unsigned short int) 279 { 280 unsigned short int a = 54321u; 281 mpq_class b(a); ASSERT_ALWAYS(b == 54321u); 282 } 283 284 // mpq_class(signed long int) 285 { 286 signed long int a = -1234567890L; 287 mpq_class b(a); ASSERT_ALWAYS(b == -1234567890L); 288 } 289 290 // mpq_class(unsigned long int) 291 { 292 unsigned long int a = 1UL << 30; 293 mpq_class b(a); ASSERT_ALWAYS(b == 1073741824L); 294 } 295 296 // mpq_class(float) 297 { 298 float a = 0.625; 299 mpq_class b(a); ASSERT_ALWAYS(b == 0.625); 300 } 301 302 // mpq_class(double) 303 { 304 double a = 1.25; 305 mpq_class b(a); ASSERT_ALWAYS(b == 1.25); 306 } 307 308 // mpq_class(long double) 309 // currently not implemented 310 311 // mpq_class(const char *) 312 { 313 const char *a = "1234567890"; 314 mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L); 315 } 316 317 // mpq_class(const char *, int) 318 { 319 const char *a = "FFFF"; 320 int base = 16; 321 mpq_class b(a, base); ASSERT_ALWAYS(b == 65535u); 322 mpq_class c(0, 1); ASSERT_ALWAYS(c == 0); 323 } 324 325 // mpq_class(const std::string &) 326 { 327 string a("1234567890"); 328 mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L); 329 } 330 331 // mpq_class(const std::string &, int) 332 { 333 string a("7777"); 334 int base = 8; 335 mpq_class b(a, base); ASSERT_ALWAYS(b == 4095); 336 } 337 338 // mpq_class(const char *) with invalid 339 { 340 try { 341 const char *a = "abc"; 342 mpq_class b(a); 343 ASSERT_ALWAYS (0); /* should not be reached */ 344 } catch (invalid_argument&) { 345 } 346 } 347 348 // mpq_class(const char *, int) with invalid 349 { 350 try { 351 const char *a = "ZZZ"; 352 int base = 16; 353 mpq_class b (a, base); 354 ASSERT_ALWAYS (0); /* should not be reached */ 355 } catch (invalid_argument&) { 356 } 357 } 358 359 // mpq_class(const std::string &) with invalid 360 { 361 try { 362 string a("abc"); 363 mpq_class b(a); 364 ASSERT_ALWAYS (0); /* should not be reached */ 365 } catch (invalid_argument&) { 366 } 367 } 368 369 // mpq_class(const std::string &, int) with invalid 370 { 371 try { 372 string a("ZZZ"); 373 int base = 8; 374 mpq_class b (a, base); 375 ASSERT_ALWAYS (0); /* should not be reached */ 376 } catch (invalid_argument&) { 377 } 378 } 379 380 // mpq_class(mpq_srcptr) 381 { 382 mpq_t a; 383 mpq_init(a); 384 mpq_set_ui(a, 100, 1); 385 mpq_class b(a); ASSERT_ALWAYS(b == 100); 386 mpq_clear(a); 387 } 388 389 // mpq_class(const mpz_class &, const mpz_class &) 390 { 391 mpz_class a(123), b(4); // tested above, assume it works 392 mpq_class c(a, b); ASSERT_ALWAYS(c == 30.75); 393 } 394 { 395 mpz_class a(-1), b(2); // tested above, assume it works 396 mpq_class c(a, b); ASSERT_ALWAYS(c == -0.5); 397 } 398 { 399 mpz_class a(5), b(4); // tested above, assume it works 400 mpq_class c(a, b); ASSERT_ALWAYS(c == 1.25); 401 } 402 403 // mpq_class(const mpz_class &) 404 { 405 mpq_class a(12345); // tested above, assume it works 406 mpq_class b(a); ASSERT_ALWAYS(b == 12345); 407 } 408 409 // no constructor for bool, but it gets casted to int 410 { 411 bool a = true; 412 mpq_class b(a); ASSERT_ALWAYS(b == 1); 413 } 414 { 415 bool a = false; 416 mpq_class b(a); ASSERT_ALWAYS(b == 0); 417 } 418} 419 420void 421check_mpf (void) 422{ 423 // mpf_class() 424 { 425 mpf_class a; ASSERT_ALWAYS(a == 0); 426 } 427 428 // mpf_class(const mpf_class &) 429 // mpf_class(const mpf_class &, unsigned long int) 430 // see below 431 432 // template <class T, class U> mpf_class(const __gmp_expr<T, U> &) 433 // template <class T, class U> mpf_class(const __gmp_expr<T, U> &, 434 // unsigned long int) 435 // not tested here, see t-unary.cc, t-binary.cc 436 437 // mpf_class(signed char) 438 { 439 signed char a = -127; 440 mpf_class b(a); ASSERT_ALWAYS(b == -127); 441 } 442 443 // mpf_class(signed char, unsigned long int) 444 { 445 signed char a = -1; 446 int prec = 64; 447 mpf_class b(a, prec); ASSERT_ALWAYS(b == -1); 448 } 449 450 // mpf_class(unsigned char) 451 { 452 unsigned char a = 255; 453 mpf_class b(a); ASSERT_ALWAYS(b == 255); 454 } 455 456 // mpf_class(unsigned char, unsigned long int) 457 { 458 unsigned char a = 128; 459 int prec = 128; 460 mpf_class b(a, prec); ASSERT_ALWAYS(b == 128); 461 } 462 463 // either signed or unsigned char, machine dependent 464 { 465 mpf_class a('A'); ASSERT_ALWAYS(a == 65); 466 } 467 { 468 int prec = 256; 469 mpf_class a('z', prec); ASSERT_ALWAYS(a == 122); 470 } 471 472 // mpf_class(signed int) 473 { 474 signed int a = 0; 475 mpf_class b(a); ASSERT_ALWAYS(b == 0); 476 } 477 { 478 signed int a = -123; 479 mpf_class b(a); ASSERT_ALWAYS(b == -123); 480 } 481 { 482 signed int a = 4567; 483 mpf_class b(a); ASSERT_ALWAYS(b == 4567); 484 } 485 486 // mpf_class(signed int, unsigned long int) 487 { 488 signed int a = -123; 489 int prec = 64; 490 mpf_class b(a, prec); ASSERT_ALWAYS(b == -123); 491 } 492 493 // mpf_class(unsigned int) 494 { 495 unsigned int a = 890; 496 mpf_class b(a); ASSERT_ALWAYS(b == 890); 497 } 498 499 // mpf_class(unsigned int, unsigned long int) 500 { 501 unsigned int a = 890; 502 int prec = 128; 503 mpf_class b(a, prec); ASSERT_ALWAYS(b == 890); 504 } 505 506 // mpf_class(signed short int) 507 { 508 signed short int a = -12345; 509 mpf_class b(a); ASSERT_ALWAYS(b == -12345); 510 } 511 512 // mpf_class(signed short int, unsigned long int) 513 { 514 signed short int a = 6789; 515 int prec = 256; 516 mpf_class b(a, prec); ASSERT_ALWAYS(b == 6789); 517 } 518 519 // mpf_class(unsigned short int) 520 { 521 unsigned short int a = 54321u; 522 mpf_class b(a); ASSERT_ALWAYS(b == 54321u); 523 } 524 525 // mpf_class(unsigned short int, unsigned long int) 526 { 527 unsigned short int a = 54321u; 528 int prec = 64; 529 mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321u); 530 } 531 532 // mpf_class(signed long int) 533 { 534 signed long int a = -1234567890L; 535 mpf_class b(a); ASSERT_ALWAYS(b == -1234567890L); 536 } 537 538 // mpf_class(signed long int, unsigned long int) 539 { 540 signed long int a = -1234567890L; 541 int prec = 128; 542 mpf_class b(a, prec); ASSERT_ALWAYS(b == -1234567890L); 543 } 544 545 // mpf_class(unsigned long int) 546 { 547 unsigned long int a = 3456789012UL; 548 mpf_class b(a); ASSERT_ALWAYS(b == 3456789012UL); 549 } 550 551 // mpf_class(unsigned long int, unsigned long int) 552 { 553 unsigned long int a = 3456789012UL; 554 int prec = 256; 555 mpf_class b(a, prec); ASSERT_ALWAYS(b == 3456789012UL); 556 } 557 558 // mpf_class(float) 559 { 560 float a = 1234.5; 561 mpf_class b(a); ASSERT_ALWAYS(b == 1234.5); 562 } 563 564 // mpf_class(float, unsigned long int) 565 { 566 float a = 1234.5; 567 int prec = 64; 568 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234.5); 569 } 570 571 // mpf_class(double) 572 { 573 double a = 12345.0; 574 mpf_class b(a); ASSERT_ALWAYS(b == 12345); 575 } 576 { 577 double a = 1.2345e+4; 578 mpf_class b(a); ASSERT_ALWAYS(b == 12345); 579 } 580 { 581 double a = 312.5e-2; 582 mpf_class b(a); ASSERT_ALWAYS(b == 3.125); 583 } 584 585 // mpf_class(double, unsigned long int) 586 { 587 double a = 5.4321e+4; 588 int prec = 128; 589 mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321L); 590 } 591 592 // mpf_class(long double) 593 // mpf_class(long double, unsigned long int) 594 // currently not implemented 595 596 // mpf_class(const char *) 597 { 598 const char *a = "1234567890"; 599 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L); 600 } 601 602 // mpf_class(const char *, unsigned long int, int = 0) 603 { 604 const char *a = "1234567890"; 605 int prec = 256; 606 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L); 607 } 608 { 609 const char *a = "777777"; 610 int prec = 64, base = 8; 611 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L); 612 } 613 614 // mpf_class(const std::string &) 615 { 616 string a("1234567890"); 617 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L); 618 } 619 620 // mpf_class(const std::string &, unsigned long int, int = 0) 621 { 622 string a("1234567890"); 623 int prec = 128; 624 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L); 625 } 626 { 627 string a("FFFF"); 628 int prec = 256, base = 16; 629 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u); 630 } 631 632 // mpf_class(const char *) with invalid 633 { 634 try { 635 const char *a = "abc"; 636 mpf_class b(a); 637 ASSERT_ALWAYS (0); /* should not be reached */ 638 } catch (invalid_argument&) { 639 } 640 } 641 642 // mpf_class(const char *, unsigned long int, int = 0) with invalid 643 { 644 try { 645 const char *a = "def"; 646 int prec = 256; 647 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L); 648 ASSERT_ALWAYS (0); /* should not be reached */ 649 } catch (invalid_argument&) { 650 } 651 } 652 { 653 try { 654 const char *a = "ghi"; 655 int prec = 64, base = 8; 656 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L); 657 ASSERT_ALWAYS (0); /* should not be reached */ 658 } catch (invalid_argument&) { 659 } 660 } 661 662 // mpf_class(const std::string &) with invalid 663 { 664 try { 665 string a("abc"); 666 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L); 667 ASSERT_ALWAYS (0); /* should not be reached */ 668 } catch (invalid_argument&) { 669 } 670 } 671 672 // mpf_class(const std::string &, unsigned long int, int = 0) with invalid 673 { 674 try { 675 string a("def"); 676 int prec = 128; 677 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L); 678 ASSERT_ALWAYS (0); /* should not be reached */ 679 } catch (invalid_argument&) { 680 } 681 } 682 { 683 try { 684 string a("ghi"); 685 int prec = 256, base = 16; 686 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u); 687 ASSERT_ALWAYS (0); /* should not be reached */ 688 } catch (invalid_argument&) { 689 } 690 } 691 692 // mpf_class(mpf_srcptr) 693 { 694 mpf_t a; 695 mpf_init_set_ui(a, 100); 696 mpf_class b(a); ASSERT_ALWAYS(b == 100); 697 mpf_clear(a); 698 } 699 700 // mpf_class(mpf_srcptr, unsigned long int) 701 { 702 mpf_t a; 703 int prec = 64; 704 mpf_init_set_ui(a, 100); 705 mpf_class b(a, prec); ASSERT_ALWAYS(b == 100); 706 mpf_clear(a); 707 } 708 709 // mpf_class(const mpf_class &) 710 { 711 mpf_class a(12345); // tested above, assume it works 712 mpf_class b(a); ASSERT_ALWAYS(b == 12345); 713 } 714 715 // mpf_class(const mpf_class &, unsigned long int) 716 { 717 mpf_class a(12345); // tested above, assume it works 718 int prec = 64; 719 mpf_class b(a, prec); ASSERT_ALWAYS(b == 12345); 720 } 721 722 // no constructors for bool, but it gets casted to int 723 { 724 bool a = true; 725 mpf_class b(a); ASSERT_ALWAYS(b == 1); 726 } 727 { 728 bool a = false; 729 mpf_class b(a); ASSERT_ALWAYS(b == 0); 730 } 731 { 732 bool a = true; 733 int prec = 128; 734 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1); 735 } 736 { 737 bool a = false; 738 int prec = 256; 739 mpf_class b(a, prec); ASSERT_ALWAYS(b == 0); 740 } 741} 742 743 744int 745main (void) 746{ 747 tests_start(); 748 749 check_mpz(); 750 check_mpq(); 751 check_mpf(); 752 753 tests_end(); 754 return 0; 755} 756