|
625 | 625 | end
|
626 | 626 |
|
627 | 627 | @testset "limits: overflow" begin
|
| 628 | + # Easy to reason about cases of overflow: |
628 | 629 | @test_throws OverflowError Base.checked_add(FD{Int8,2}(1), FD{Int8,2}(1))
|
629 | 630 | @test_throws OverflowError Base.checked_add(FD{Int8,2}(1), 1)
|
630 | 631 | @test_throws OverflowError Base.checked_add(FD{Int8,2}(1), FD{Int8,2}(0.4))
|
|
633 | 634 | @test_throws OverflowError Base.checked_sub(1, FD{Int8,2}(-1))
|
634 | 635 | @test_throws OverflowError Base.checked_sub(FD{Int8,2}(-1), FD{Int8,2}(0.4))
|
635 | 636 |
|
| 637 | + @test_throws OverflowError Base.checked_mul(FD{Int8,2}(1.2), FD{Int8,2}(1.2)) |
| 638 | + @test_throws OverflowError Base.checked_mul(FD{Int8,1}(12), 2) |
| 639 | + @test_throws OverflowError Base.checked_mul(FD{Int8,0}(120), 2) |
| 640 | + @test_throws OverflowError Base.checked_mul(120, FD{Int8,0}(2)) |
| 641 | + |
636 | 642 | @test_throws OverflowError Base.checked_div(FD{Int8,2}(1), FD{Int8,2}(0.5))
|
637 | 643 | @test_throws OverflowError Base.checked_div(1, FD{Int8,2}(0.5))
|
638 | 644 | @test_throws OverflowError Base.checked_div(FD{Int8,2}(1), FD{Int8,2}(0.4))
|
|
644 | 650 | @test_throws OverflowError checked_decimal_division(Int8(1), FD{Int8,2}(0.7))
|
645 | 651 | end
|
646 | 652 |
|
647 |
| - # Rounds down to 2 |
| 653 | + # Rounds down to -2 |
648 | 654 | @test_throws OverflowError Base.checked_fld(FD{Int8,2}(-1), FD{Int8,2}(0.9))
|
649 | 655 | # Rounds up to 2
|
650 | 656 | @test_throws OverflowError Base.checked_cld(FD{Int8,2}(1), FD{Int8,2}(0.9))
|
|
658 | 664 | @test_throws OverflowError Base.checked_neg(typemin(FD{Int8,2}))
|
659 | 665 | @test Base.checked_abs(typemax(FD{Int8,2})) == FD{Int8,2}(1.27)
|
660 | 666 | @test Base.checked_neg(typemax(FD{Int8,2})) == FD{Int8,2}(-1.27)
|
| 667 | + |
| 668 | + @testset "Overflow corner cases" begin |
| 669 | + @testset for I in (Int128, UInt128, Int8, UInt8), f in (0,2) |
| 670 | + T = FD{I, f} |
| 671 | + issigned(I) = signed(I) === I |
| 672 | + |
| 673 | + @test_throws OverflowError Base.checked_add(typemax(T), eps(T)) |
| 674 | + issigned(I) && @test_throws OverflowError Base.checked_add(typemin(T), -eps(T)) |
| 675 | + @test_throws OverflowError Base.checked_add(typemax(T), 1) |
| 676 | + @test_throws OverflowError Base.checked_add(1, typemax(T)) |
| 677 | + |
| 678 | + @test_throws OverflowError Base.checked_sub(typemin(T), eps(T)) |
| 679 | + issigned(I) && @test_throws OverflowError Base.checked_sub(typemax(T), -eps(T)) |
| 680 | + @test_throws OverflowError Base.checked_sub(typemin(T), 1) |
| 681 | + if issigned(I) && 2.0 <= typemax(T) |
| 682 | + @test_throws OverflowError Base.checked_sub(-2, typemax(T)) |
| 683 | + end |
| 684 | + |
| 685 | + @test_throws OverflowError Base.checked_mul(typemax(T), typemax(T)) |
| 686 | + issigned(I) && @test_throws OverflowError Base.checked_mul(typemin(T), typemax(T)) |
| 687 | + if 2.0 <= typemax(T) |
| 688 | + @test_throws OverflowError Base.checked_mul(typemax(T), 2) |
| 689 | + @test_throws OverflowError Base.checked_mul(2, typemax(T)) |
| 690 | + issigned(I) && @test_throws OverflowError Base.checked_mul(typemin(T), 2) |
| 691 | + issigned(I) && @test_throws OverflowError Base.checked_mul(2, typemin(T)) |
| 692 | + end |
| 693 | + |
| 694 | + if f > 0 |
| 695 | + @test_throws OverflowError Base.checked_div(typemax(T), eps(T)) |
| 696 | + issigned(I) && @test_throws OverflowError Base.checked_div(typemin(T), eps(T)) |
| 697 | + issigned(I) && @test_throws OverflowError Base.checked_div(typemax(T), -eps(T)) |
| 698 | + |
| 699 | + issigned(I) && @test_throws DivideError Base.checked_div(typemax(T), T(0)) |
| 700 | + issigned(I) && @test_throws DivideError Base.checked_div(typemin(T), T(0)) |
| 701 | + issigned(I) && @test_throws DivideError Base.checked_div(typemin(T), -eps(T)) |
| 702 | + end |
| 703 | + |
| 704 | + if f > 0 |
| 705 | + @test_throws OverflowError Base.checked_fld(typemax(T), eps(T)) |
| 706 | + issigned(I) && @test_throws OverflowError Base.checked_fld(typemin(T), eps(T)) |
| 707 | + issigned(I) && @test_throws OverflowError Base.checked_fld(typemax(T), -eps(T)) |
| 708 | + |
| 709 | + @test_throws OverflowError Base.checked_cld(typemax(T), eps(T)) |
| 710 | + issigned(I) && @test_throws OverflowError Base.checked_cld(typemin(T), eps(T)) |
| 711 | + issigned(I) && @test_throws OverflowError Base.checked_cld(typemax(T), -eps(T)) |
| 712 | + end |
| 713 | + |
| 714 | + issigned(I) && @test_throws OverflowError Base.checked_abs(typemin(T)) |
| 715 | + issigned(I) && @test_throws OverflowError Base.checked_neg(typemin(T)) |
| 716 | + end |
| 717 | + end |
661 | 718 | end
|
662 | 719 |
|
663 | 720 | @testset "limits of $T" for T in CONTAINER_TYPES
|
|
0 commit comments