|
600 | 600 | @test strip(String(read(cmd))) == "baz bar"
|
601 | 601 | end
|
602 | 602 |
|
| 603 | +# https://github.com/JuliaLang/julia/pull/37559 |
| 604 | +@testset "reinterpred(reshape, ...)" begin |
| 605 | + # simplified from PR |
| 606 | + Ar = Int64[1 3; 2 4] |
| 607 | + @test @inferred(ndims(reinterpret(reshape, Complex{Int64}, Ar))) == 1 |
| 608 | + @test @inferred(axes(reinterpret(reshape, Complex{Int64}, Ar))) === (Base.OneTo(2),) |
| 609 | + @test @inferred(size(reinterpret(reshape, Complex{Int64}, Ar))) == (2,) |
| 610 | + |
| 611 | + _B = Complex{Int64}[5+6im, 7+8im, 9+10im] |
| 612 | + @test @inferred(ndims(reinterpret(reshape, Int64, _B))) == 2 |
| 613 | + @test @inferred(axes(reinterpret(reshape, Int64, _B))) === (Base.OneTo(2), Base.OneTo(3)) |
| 614 | + @test @inferred(size(reinterpret(reshape, Int64, _B))) == (2, 3) |
| 615 | + @test @inferred(ndims(reinterpret(reshape, Int128, _B))) == 1 |
| 616 | + @test @inferred(axes(reinterpret(reshape, Int128, _B))) === (Base.OneTo(3),) |
| 617 | + @test @inferred(size(reinterpret(reshape, Int128, _B))) == (3,) |
| 618 | + |
| 619 | + A = Int64[1, 2, 3, 4] |
| 620 | + Av = [Int32[1,2], Int32[3,4]] |
| 621 | + |
| 622 | + @test_throws ArgumentError reinterpret(Vector{Int64}, A) # ("cannot reinterpret `Int64` as `Vector{Int64}`, type `Vector{Int64}` is not a bits type") |
| 623 | + @test_throws ArgumentError reinterpret(Int32, Av) # ("cannot reinterpret `Vector{Int32}` as `Int32`, type `Vector{Int32}` is not a bits type") |
| 624 | + @test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int64` array to `Int32` which is of a different size") reinterpret(Int32, reshape([Int64(0)])) |
| 625 | + @test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int32` array to `Int64` which is of a different size") reinterpret(Int64, reshape([Int32(0)])) |
| 626 | + @test_throws ArgumentError reinterpret(Tuple{Int,Int}, [1,2,3,4,5]) # ("""cannot reinterpret an `$Int` array to `Tuple{$Int, $Int}` whose first dimension has size `5`. |
| 627 | + # The resulting array would have non-integral first dimension. |
| 628 | + # """) |
| 629 | + @test_throws ArgumentError("`reinterpret(reshape, Complex{Int64}, a)` where `eltype(a)` is Int64 requires that `axes(a, 1)` (got Base.OneTo(4)) be equal to 1:2 (from the ratio of element sizes)") reinterpret(reshape, Complex{Int64}, A) |
| 630 | + @test_throws ArgumentError("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got 24) and `sizeof(eltype(a))` (got 16) be an integer multiple of the other") reinterpret(reshape, NTuple{3, Int64}, _B) |
| 631 | + @test_throws ArgumentError reinterpret(reshape, Vector{Int64}, Ar) # ("cannot reinterpret `Int64` as `Vector{Int64}`, type `Vector{Int64}` is not a bits type") |
| 632 | + @test_throws ArgumentError("cannot reinterpret a zero-dimensional `UInt8` array to `UInt16` which is of a larger size") reinterpret(reshape, UInt16, reshape([0x01])) |
| 633 | + |
| 634 | + # getindex |
| 635 | + _A = A |
| 636 | + @test reinterpret(Complex{Int64}, _A) == [1 + 2im, 3 + 4im] |
| 637 | + @test reinterpret(Float64, _A) == reinterpret.(Float64, A) |
| 638 | + @test reinterpret(reshape, Float64, _A) == reinterpret.(Float64, A) |
| 639 | + |
| 640 | + Ars = Ar |
| 641 | + @test reinterpret(reshape, Complex{Int64}, Ar) == [1 + 2im, 3 + 4im] |
| 642 | + @test reinterpret(reshape, Float64, Ar) == reinterpret.(Float64, Ars) |
| 643 | + |
| 644 | + # setindex |
| 645 | + A3 = collect(reshape(1:18, 2, 3, 3)) |
| 646 | + A3r = reinterpret(reshape, Complex{Int}, A3) |
| 647 | + @test A3r[4] === A3r[1,2] === A3r[CartesianIndex(1, 2)] === 7+8im |
| 648 | + A3r[2,3] = -8-15im |
| 649 | + @test A3[1,2,3] == -8 |
| 650 | + @test A3[2,2,3] == -15 |
| 651 | + A3r[4] = 100+200im |
| 652 | + @test A3[1,1,2] == 100 |
| 653 | + @test A3[2,1,2] == 200 |
| 654 | + A3r[CartesianIndex(1,2)] = 300+400im |
| 655 | + @test A3[1,1,2] == 300 |
| 656 | + @test A3[2,1,2] == 400 |
| 657 | + |
| 658 | + # Test 0-dimensional Arrays |
| 659 | + A = zeros(UInt32) |
| 660 | + B = reinterpret(Int32,A) |
| 661 | + Brs = reinterpret(reshape,Int32,A) |
| 662 | + @test size(B) == size(Brs) == () |
| 663 | + @test axes(B) == axes(Brs) == () |
| 664 | + B[] = Int32(5) |
| 665 | + @test B[] === Int32(5) |
| 666 | + @test Brs[] === Int32(5) |
| 667 | + @test A[] === UInt32(5) |
| 668 | + |
| 669 | + # reductions |
| 670 | + a = [(1,2,3), (4,5,6)] |
| 671 | + ars = reinterpret(reshape, Int, a) |
| 672 | + @test sum(ars) == 21 |
| 673 | + @test sum(ars; dims=1) == [6 15] |
| 674 | + @test sum(ars; dims=2) == reshape([5,7,9], (3, 1)) |
| 675 | + @test sum(ars; dims=(1,2)) == reshape([21], (1, 1)) |
| 676 | + # also test large sizes for the pairwise algorithm |
| 677 | + a = [(k,k+1,k+2) for k = 1:3:4000] |
| 678 | + ars = reinterpret(reshape, Int, a) |
| 679 | + @test sum(ars) == 8010003 |
| 680 | +end |
| 681 | + |
603 | 682 | include("iterators.jl")
|
604 | 683 |
|
605 | 684 | nothing
|
0 commit comments