| ... | ... | @@ -3,6 +3,7 @@ const builtin = @import("builtin"); |
| 3 | 3 | const debug = std.debug; |
| 4 | 4 | const math = std.math; |
| 5 | 5 | const mem = std.mem; |
| 6 | const testing = std.testing; |
| 6 | 7 | const Allocator = mem.Allocator; |
| 7 | 8 | const ArrayList = std.ArrayList; |
| 8 | 9 | |
| ... | ... | @@ -425,6 +426,7 @@ var al = debug.global_allocator; |
| 425 | 426 | const SignedDoubleLimb = @IntType(true, DoubleLimb.bit_count); |
| 426 | 427 | |
| 427 | 428 | fn gcd(rma: *Int, x: Int, y: Int) !void { |
| 429 | rma.assertWritable(); |
| 428 | 430 | var r = rma; |
| 429 | 431 | var aliased = rma.limbs.ptr == x.limbs.ptr or rma.limbs.ptr == y.limbs.ptr; |
| 430 | 432 | |
| ... | ... | @@ -463,19 +465,23 @@ fn FixedIntFromSignedDoubleLimb(A: SignedDoubleLimb, storage: []Limb) Int { |
| 463 | 465 | // |
| 464 | 466 | // r = gcd(x, y) where x, y > 0 |
| 465 | 467 | fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void { |
| 466 | | debug.assert(xa.positive and ya.positive); |
| 467 | | debug.assert(xa.cmp(ya) >= 0); |
| 468 | | |
| 469 | 468 | var x = try xa.clone(); |
| 469 | x.abs(); |
| 470 | 470 | defer x.deinit(); |
| 471 | 471 | |
| 472 | 472 | var y = try ya.clone(); |
| 473 | y.abs(); |
| 473 | 474 | defer y.deinit(); |
| 474 | 475 | |
| 476 | if (x.cmp(y) < 0) { |
| 477 | x.swap(&y); |
| 478 | } |
| 479 | |
| 475 | 480 | var T = try Int.init(r.allocator.?); |
| 476 | 481 | defer T.deinit(); |
| 477 | 482 | |
| 478 | 483 | while (y.len > 1) { |
| 484 | debug.assert(x.positive and y.positive); |
| 479 | 485 | debug.assert(x.len >= y.len); |
| 480 | 486 | |
| 481 | 487 | // chop the leading zeros of the limbs and normalize |
| ... | ... | @@ -540,7 +546,13 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void { |
| 540 | 546 | try r.add(x, r.*); |
| 541 | 547 | |
| 542 | 548 | x.swap(&T); |
| 549 | x.abs(); |
| 543 | 550 | y.swap(r); |
| 551 | y.abs(); |
| 552 | |
| 553 | if (x.cmp(y) < 0) { |
| 554 | x.swap(&y); |
| 555 | } |
| 544 | 556 | } |
| 545 | 557 | } |
| 546 | 558 | |
| ... | ... | @@ -563,7 +575,7 @@ test "big.rational gcd non-one small" { |
| 563 | 575 | |
| 564 | 576 | try gcd(&r, a, b); |
| 565 | 577 | |
| 566 | | debug.assert((try r.to(u32)) == 1); |
| 578 | testing.expect((try r.to(u32)) == 1); |
| 567 | 579 | } |
| 568 | 580 | |
| 569 | 581 | test "big.rational gcd non-one small" { |
| ... | ... | @@ -573,7 +585,7 @@ test "big.rational gcd non-one small" { |
| 573 | 585 | |
| 574 | 586 | try gcd(&r, a, b); |
| 575 | 587 | |
| 576 | | debug.assert((try r.to(u32)) == 38); |
| 588 | testing.expect((try r.to(u32)) == 38); |
| 577 | 589 | } |
| 578 | 590 | |
| 579 | 591 | test "big.rational gcd non-one large" { |
| ... | ... | @@ -583,7 +595,7 @@ test "big.rational gcd non-one large" { |
| 583 | 595 | |
| 584 | 596 | try gcd(&r, a, b); |
| 585 | 597 | |
| 586 | | debug.assert((try r.to(u32)) == 4369); |
| 598 | testing.expect((try r.to(u32)) == 4369); |
| 587 | 599 | } |
| 588 | 600 | |
| 589 | 601 | test "big.rational gcd large multi-limb result" { |
| ... | ... | @@ -593,11 +605,11 @@ test "big.rational gcd large multi-limb result" { |
| 593 | 605 | |
| 594 | 606 | try gcd(&r, a, b); |
| 595 | 607 | |
| 596 | | debug.assert((try r.to(u256)) == 0xf000000ff00000fff0000ffff000fffff00ffffff1); |
| 608 | testing.expect((try r.to(u256)) == 0xf000000ff00000fff0000ffff000fffff00ffffff1); |
| 597 | 609 | } |
| 598 | 610 | |
| 599 | 611 | fn extractLowBits(a: Int, comptime T: type) T { |
| 600 | | debug.assert(@typeId(T) == builtin.TypeId.Int); |
| 612 | testing.expect(@typeId(T) == builtin.TypeId.Int); |
| 601 | 613 | |
| 602 | 614 | if (T.bit_count <= Limb.bit_count) { |
| 603 | 615 | return @truncate(T, a.limbs[0]); |
| ... | ... | @@ -619,71 +631,71 @@ test "big.rational extractLowBits" { |
| 619 | 631 | var a = try Int.initSet(al, 0x11112222333344441234567887654321); |
| 620 | 632 | |
| 621 | 633 | const a1 = extractLowBits(a, u8); |
| 622 | | debug.assert(a1 == 0x21); |
| 634 | testing.expect(a1 == 0x21); |
| 623 | 635 | |
| 624 | 636 | const a2 = extractLowBits(a, u16); |
| 625 | | debug.assert(a2 == 0x4321); |
| 637 | testing.expect(a2 == 0x4321); |
| 626 | 638 | |
| 627 | 639 | const a3 = extractLowBits(a, u32); |
| 628 | | debug.assert(a3 == 0x87654321); |
| 640 | testing.expect(a3 == 0x87654321); |
| 629 | 641 | |
| 630 | 642 | const a4 = extractLowBits(a, u64); |
| 631 | | debug.assert(a4 == 0x1234567887654321); |
| 643 | testing.expect(a4 == 0x1234567887654321); |
| 632 | 644 | |
| 633 | 645 | const a5 = extractLowBits(a, u128); |
| 634 | | debug.assert(a5 == 0x11112222333344441234567887654321); |
| 646 | testing.expect(a5 == 0x11112222333344441234567887654321); |
| 635 | 647 | } |
| 636 | 648 | |
| 637 | 649 | test "big.rational set" { |
| 638 | 650 | var a = try Rational.init(al); |
| 639 | 651 | |
| 640 | 652 | try a.setInt(5); |
| 641 | | debug.assert((try a.p.to(u32)) == 5); |
| 642 | | debug.assert((try a.q.to(u32)) == 1); |
| 653 | testing.expect((try a.p.to(u32)) == 5); |
| 654 | testing.expect((try a.q.to(u32)) == 1); |
| 643 | 655 | |
| 644 | 656 | try a.setRatio(7, 3); |
| 645 | | debug.assert((try a.p.to(u32)) == 7); |
| 646 | | debug.assert((try a.q.to(u32)) == 3); |
| 657 | testing.expect((try a.p.to(u32)) == 7); |
| 658 | testing.expect((try a.q.to(u32)) == 3); |
| 647 | 659 | |
| 648 | 660 | try a.setRatio(9, 3); |
| 649 | | debug.assert((try a.p.to(i32)) == 3); |
| 650 | | debug.assert((try a.q.to(i32)) == 1); |
| 661 | testing.expect((try a.p.to(i32)) == 3); |
| 662 | testing.expect((try a.q.to(i32)) == 1); |
| 651 | 663 | |
| 652 | 664 | try a.setRatio(-9, 3); |
| 653 | | debug.assert((try a.p.to(i32)) == -3); |
| 654 | | debug.assert((try a.q.to(i32)) == 1); |
| 665 | testing.expect((try a.p.to(i32)) == -3); |
| 666 | testing.expect((try a.q.to(i32)) == 1); |
| 655 | 667 | |
| 656 | 668 | try a.setRatio(9, -3); |
| 657 | | debug.assert((try a.p.to(i32)) == -3); |
| 658 | | debug.assert((try a.q.to(i32)) == 1); |
| 669 | testing.expect((try a.p.to(i32)) == -3); |
| 670 | testing.expect((try a.q.to(i32)) == 1); |
| 659 | 671 | |
| 660 | 672 | try a.setRatio(-9, -3); |
| 661 | | debug.assert((try a.p.to(i32)) == 3); |
| 662 | | debug.assert((try a.q.to(i32)) == 1); |
| 673 | testing.expect((try a.p.to(i32)) == 3); |
| 674 | testing.expect((try a.q.to(i32)) == 1); |
| 663 | 675 | } |
| 664 | 676 | |
| 665 | 677 | test "big.rational setFloat" { |
| 666 | 678 | var a = try Rational.init(al); |
| 667 | 679 | |
| 668 | 680 | try a.setFloat(f64, 2.5); |
| 669 | | debug.assert((try a.p.to(i32)) == 5); |
| 670 | | debug.assert((try a.q.to(i32)) == 2); |
| 681 | testing.expect((try a.p.to(i32)) == 5); |
| 682 | testing.expect((try a.q.to(i32)) == 2); |
| 671 | 683 | |
| 672 | 684 | try a.setFloat(f32, -2.5); |
| 673 | | debug.assert((try a.p.to(i32)) == -5); |
| 674 | | debug.assert((try a.q.to(i32)) == 2); |
| 685 | testing.expect((try a.p.to(i32)) == -5); |
| 686 | testing.expect((try a.q.to(i32)) == 2); |
| 675 | 687 | |
| 676 | 688 | try a.setFloat(f32, 3.141593); |
| 677 | 689 | |
| 678 | 690 | // = 3.14159297943115234375 |
| 679 | | debug.assert((try a.p.to(u32)) == 3294199); |
| 680 | | debug.assert((try a.q.to(u32)) == 1048576); |
| 691 | testing.expect((try a.p.to(u32)) == 3294199); |
| 692 | testing.expect((try a.q.to(u32)) == 1048576); |
| 681 | 693 | |
| 682 | 694 | try a.setFloat(f64, 72.141593120712409172417410926841290461290467124); |
| 683 | 695 | |
| 684 | 696 | // = 72.1415931207124145885245525278151035308837890625 |
| 685 | | debug.assert((try a.p.to(u128)) == 5076513310880537); |
| 686 | | debug.assert((try a.q.to(u128)) == 70368744177664); |
| 697 | testing.expect((try a.p.to(u128)) == 5076513310880537); |
| 698 | testing.expect((try a.q.to(u128)) == 70368744177664); |
| 687 | 699 | } |
| 688 | 700 | |
| 689 | 701 | test "big.rational setFloatString" { |
| ... | ... | @@ -692,8 +704,8 @@ test "big.rational setFloatString" { |
| 692 | 704 | try a.setFloatString("72.14159312071241458852455252781510353"); |
| 693 | 705 | |
| 694 | 706 | // = 72.1415931207124145885245525278151035308837890625 |
| 695 | | debug.assert((try a.p.to(u128)) == 7214159312071241458852455252781510353); |
| 696 | | debug.assert((try a.q.to(u128)) == 100000000000000000000000000000000000); |
| 707 | testing.expect((try a.p.to(u128)) == 7214159312071241458852455252781510353); |
| 708 | testing.expect((try a.q.to(u128)) == 100000000000000000000000000000000000); |
| 697 | 709 | } |
| 698 | 710 | |
| 699 | 711 | test "big.rational toFloat" { |
| ... | ... | @@ -701,11 +713,11 @@ test "big.rational toFloat" { |
| 701 | 713 | |
| 702 | 714 | // = 3.14159297943115234375 |
| 703 | 715 | try a.setRatio(3294199, 1048576); |
| 704 | | debug.assert((try a.toFloat(f64)) == 3.14159297943115234375); |
| 716 | testing.expect((try a.toFloat(f64)) == 3.14159297943115234375); |
| 705 | 717 | |
| 706 | 718 | // = 72.1415931207124145885245525278151035308837890625 |
| 707 | 719 | try a.setRatio(5076513310880537, 70368744177664); |
| 708 | | debug.assert((try a.toFloat(f64)) == 72.141593120712409172417410926841290461290467124); |
| 720 | testing.expect((try a.toFloat(f64)) == 72.141593120712409172417410926841290461290467124); |
| 709 | 721 | } |
| 710 | 722 | |
| 711 | 723 | test "big.rational set/to Float round-trip" { |
| ... | ... | @@ -720,7 +732,7 @@ test "big.rational set/to Float round-trip" { |
| 720 | 732 | while (i < 512) : (i += 1) { |
| 721 | 733 | const r = prng.random.float(f64); |
| 722 | 734 | try a.setFloat(f64, r); |
| 723 | | debug.assert((try a.toFloat(f64)) == r); |
| 735 | testing.expect((try a.toFloat(f64)) == r); |
| 724 | 736 | } |
| 725 | 737 | } |
| 726 | 738 | |
| ... | ... | @@ -730,54 +742,54 @@ test "big.rational copy" { |
| 730 | 742 | const b = try Int.initSet(al, 5); |
| 731 | 743 | |
| 732 | 744 | try a.copyInt(b); |
| 733 | | debug.assert((try a.p.to(u32)) == 5); |
| 734 | | debug.assert((try a.q.to(u32)) == 1); |
| 745 | testing.expect((try a.p.to(u32)) == 5); |
| 746 | testing.expect((try a.q.to(u32)) == 1); |
| 735 | 747 | |
| 736 | 748 | const c = try Int.initSet(al, 7); |
| 737 | 749 | const d = try Int.initSet(al, 3); |
| 738 | 750 | |
| 739 | 751 | try a.copyRatio(c, d); |
| 740 | | debug.assert((try a.p.to(u32)) == 7); |
| 741 | | debug.assert((try a.q.to(u32)) == 3); |
| 752 | testing.expect((try a.p.to(u32)) == 7); |
| 753 | testing.expect((try a.q.to(u32)) == 3); |
| 742 | 754 | |
| 743 | 755 | const e = try Int.initSet(al, 9); |
| 744 | 756 | const f = try Int.initSet(al, 3); |
| 745 | 757 | |
| 746 | 758 | try a.copyRatio(e, f); |
| 747 | | debug.assert((try a.p.to(u32)) == 3); |
| 748 | | debug.assert((try a.q.to(u32)) == 1); |
| 759 | testing.expect((try a.p.to(u32)) == 3); |
| 760 | testing.expect((try a.q.to(u32)) == 1); |
| 749 | 761 | } |
| 750 | 762 | |
| 751 | 763 | test "big.rational negate" { |
| 752 | 764 | var a = try Rational.init(al); |
| 753 | 765 | |
| 754 | 766 | try a.setInt(-50); |
| 755 | | debug.assert((try a.p.to(i32)) == -50); |
| 756 | | debug.assert((try a.q.to(i32)) == 1); |
| 767 | testing.expect((try a.p.to(i32)) == -50); |
| 768 | testing.expect((try a.q.to(i32)) == 1); |
| 757 | 769 | |
| 758 | 770 | a.negate(); |
| 759 | | debug.assert((try a.p.to(i32)) == 50); |
| 760 | | debug.assert((try a.q.to(i32)) == 1); |
| 771 | testing.expect((try a.p.to(i32)) == 50); |
| 772 | testing.expect((try a.q.to(i32)) == 1); |
| 761 | 773 | |
| 762 | 774 | a.negate(); |
| 763 | | debug.assert((try a.p.to(i32)) == -50); |
| 764 | | debug.assert((try a.q.to(i32)) == 1); |
| 775 | testing.expect((try a.p.to(i32)) == -50); |
| 776 | testing.expect((try a.q.to(i32)) == 1); |
| 765 | 777 | } |
| 766 | 778 | |
| 767 | 779 | test "big.rational abs" { |
| 768 | 780 | var a = try Rational.init(al); |
| 769 | 781 | |
| 770 | 782 | try a.setInt(-50); |
| 771 | | debug.assert((try a.p.to(i32)) == -50); |
| 772 | | debug.assert((try a.q.to(i32)) == 1); |
| 783 | testing.expect((try a.p.to(i32)) == -50); |
| 784 | testing.expect((try a.q.to(i32)) == 1); |
| 773 | 785 | |
| 774 | 786 | a.abs(); |
| 775 | | debug.assert((try a.p.to(i32)) == 50); |
| 776 | | debug.assert((try a.q.to(i32)) == 1); |
| 787 | testing.expect((try a.p.to(i32)) == 50); |
| 788 | testing.expect((try a.q.to(i32)) == 1); |
| 777 | 789 | |
| 778 | 790 | a.abs(); |
| 779 | | debug.assert((try a.p.to(i32)) == 50); |
| 780 | | debug.assert((try a.q.to(i32)) == 1); |
| 791 | testing.expect((try a.p.to(i32)) == 50); |
| 792 | testing.expect((try a.q.to(i32)) == 1); |
| 781 | 793 | } |
| 782 | 794 | |
| 783 | 795 | test "big.rational swap" { |
| ... | ... | @@ -787,19 +799,19 @@ test "big.rational swap" { |
| 787 | 799 | try a.setRatio(50, 23); |
| 788 | 800 | try b.setRatio(17, 3); |
| 789 | 801 | |
| 790 | | debug.assert((try a.p.to(u32)) == 50); |
| 791 | | debug.assert((try a.q.to(u32)) == 23); |
| 802 | testing.expect((try a.p.to(u32)) == 50); |
| 803 | testing.expect((try a.q.to(u32)) == 23); |
| 792 | 804 | |
| 793 | | debug.assert((try b.p.to(u32)) == 17); |
| 794 | | debug.assert((try b.q.to(u32)) == 3); |
| 805 | testing.expect((try b.p.to(u32)) == 17); |
| 806 | testing.expect((try b.q.to(u32)) == 3); |
| 795 | 807 | |
| 796 | 808 | a.swap(&b); |
| 797 | 809 | |
| 798 | | debug.assert((try a.p.to(u32)) == 17); |
| 799 | | debug.assert((try a.q.to(u32)) == 3); |
| 810 | testing.expect((try a.p.to(u32)) == 17); |
| 811 | testing.expect((try a.q.to(u32)) == 3); |
| 800 | 812 | |
| 801 | | debug.assert((try b.p.to(u32)) == 50); |
| 802 | | debug.assert((try b.q.to(u32)) == 23); |
| 813 | testing.expect((try b.p.to(u32)) == 50); |
| 814 | testing.expect((try b.q.to(u32)) == 23); |
| 803 | 815 | } |
| 804 | 816 | |
| 805 | 817 | test "big.rational cmp" { |
| ... | ... | @@ -808,11 +820,11 @@ test "big.rational cmp" { |
| 808 | 820 | |
| 809 | 821 | try a.setRatio(500, 231); |
| 810 | 822 | try b.setRatio(18903, 8584); |
| 811 | | debug.assert((try a.cmp(b)) < 0); |
| 823 | testing.expect((try a.cmp(b)) < 0); |
| 812 | 824 | |
| 813 | 825 | try a.setRatio(890, 10); |
| 814 | 826 | try b.setRatio(89, 1); |
| 815 | | debug.assert((try a.cmp(b)) == 0); |
| 827 | testing.expect((try a.cmp(b)) == 0); |
| 816 | 828 | } |
| 817 | 829 | |
| 818 | 830 | test "big.rational add single-limb" { |
| ... | ... | @@ -821,11 +833,11 @@ test "big.rational add single-limb" { |
| 821 | 833 | |
| 822 | 834 | try a.setRatio(500, 231); |
| 823 | 835 | try b.setRatio(18903, 8584); |
| 824 | | debug.assert((try a.cmp(b)) < 0); |
| 836 | testing.expect((try a.cmp(b)) < 0); |
| 825 | 837 | |
| 826 | 838 | try a.setRatio(890, 10); |
| 827 | 839 | try b.setRatio(89, 1); |
| 828 | | debug.assert((try a.cmp(b)) == 0); |
| 840 | testing.expect((try a.cmp(b)) == 0); |
| 829 | 841 | } |
| 830 | 842 | |
| 831 | 843 | test "big.rational add" { |
| ... | ... | @@ -838,7 +850,7 @@ test "big.rational add" { |
| 838 | 850 | try a.add(a, b); |
| 839 | 851 | |
| 840 | 852 | try r.setRatio(984786924199, 290395044174); |
| 841 | | debug.assert((try a.cmp(r)) == 0); |
| 853 | testing.expect((try a.cmp(r)) == 0); |
| 842 | 854 | } |
| 843 | 855 | |
| 844 | 856 | test "big.rational sub" { |
| ... | ... | @@ -851,7 +863,7 @@ test "big.rational sub" { |
| 851 | 863 | try a.sub(a, b); |
| 852 | 864 | |
| 853 | 865 | try r.setRatio(979040510045, 290395044174); |
| 854 | | debug.assert((try a.cmp(r)) == 0); |
| 866 | testing.expect((try a.cmp(r)) == 0); |
| 855 | 867 | } |
| 856 | 868 | |
| 857 | 869 | test "big.rational mul" { |
| ... | ... | @@ -864,7 +876,7 @@ test "big.rational mul" { |
| 864 | 876 | try a.mul(a, b); |
| 865 | 877 | |
| 866 | 878 | try r.setRatio(571481443, 17082061422); |
| 867 | | debug.assert((try a.cmp(r)) == 0); |
| 879 | testing.expect((try a.cmp(r)) == 0); |
| 868 | 880 | } |
| 869 | 881 | |
| 870 | 882 | test "big.rational div" { |
| ... | ... | @@ -877,7 +889,7 @@ test "big.rational div" { |
| 877 | 889 | try a.div(a, b); |
| 878 | 890 | |
| 879 | 891 | try r.setRatio(75531824394, 221015929); |
| 880 | | debug.assert((try a.cmp(r)) == 0); |
| 892 | testing.expect((try a.cmp(r)) == 0); |
| 881 | 893 | } |
| 882 | 894 | |
| 883 | 895 | test "big.rational div" { |
| ... | ... | @@ -888,11 +900,11 @@ test "big.rational div" { |
| 888 | 900 | a.invert(); |
| 889 | 901 | |
| 890 | 902 | try r.setRatio(23341, 78923); |
| 891 | | debug.assert((try a.cmp(r)) == 0); |
| 903 | testing.expect((try a.cmp(r)) == 0); |
| 892 | 904 | |
| 893 | 905 | try a.setRatio(-78923, 23341); |
| 894 | 906 | a.invert(); |
| 895 | 907 | |
| 896 | 908 | try r.setRatio(-23341, 78923); |
| 897 | | debug.assert((try a.cmp(r)) == 0); |
| 909 | testing.expect((try a.cmp(r)) == 0); |
| 898 | 910 | } |