|
693 | 693 | @test @inferred(vcat(t3)) == t3 |
694 | 694 | @inferred vcat(t3, t3) |
695 | 695 | @inferred vcat(t3, collect(t3)) |
| 696 | + a = StructArray(y = Union{Missing, Int}[missing]) |
| 697 | + b = StructArray(y = [3]) |
| 698 | + c = StructArray(y = Union{Missing, Int}[4]) |
| 699 | + vcatted = vcat(a, b, c) |
| 700 | + @test eltype(vcatted) === NamedTuple{(:y,), Tuple{Union{Missing, Int}}} |
| 701 | + reduced_vcat = reduce(vcat, [a, b, c]) |
| 702 | + @test eltype(reduced_vcat) === eltype(vcatted) |
| 703 | + @test isequal(reduced_vcat, vcatted) |
| 704 | + @test reduced_vcat.y isa Vector{Union{Missing, Int}} |
| 705 | + hcatted = hcat(reshape(a, 1, 1), reshape(b, 1, 1), reshape(c, 1, 1)) |
| 706 | + @test eltype(hcatted) === NamedTuple{(:y,), Tuple{Union{Missing, Int}}} |
| 707 | + reduced_hcat = reduce(hcat, [reshape(a, 1, 1), reshape(b, 1, 1), reshape(c, 1, 1)]) |
| 708 | + @test eltype(reduced_hcat) === eltype(hcatted) |
| 709 | + @test isequal(reduced_hcat, hcatted) |
| 710 | + @test reduced_hcat.y isa Matrix{Union{Missing, Int}} |
| 711 | + |
| 712 | + struct CatTestType{A, B} |
| 713 | + a::A |
| 714 | + b::B |
| 715 | + end |
| 716 | + custom_a = StructArray{CatTestType{Int, Missing}}((a = [1], b = Missing[missing])) |
| 717 | + custom_b = StructArray{CatTestType{Int, Int}}((a = [2], b = [3])) |
| 718 | + custom_vcat = vcat(custom_a, custom_b, custom_a) |
| 719 | + @test custom_vcat == CatTestType{Int}[CatTestType(1, missing), CatTestType(2, 3), CatTestType(1, missing)] |
| 720 | + @test custom_vcat.b isa Vector{Union{Missing, Int}} |
| 721 | + reduced_custom_vcat = reduce(vcat, [custom_a, custom_b, custom_a]) |
| 722 | + @test isequal(reduced_custom_vcat, custom_vcat) |
| 723 | + @test eltype(reduced_custom_vcat) === eltype(custom_vcat) === CatTestType{Int} |
| 724 | + @test reduced_custom_vcat.b isa Vector{Union{Missing, Int}} |
| 725 | + |
| 726 | + # error behavior is consistent between reduce(vcat) and vcat(), and is generally reasonable |
| 727 | + mismatched_names_a = StructArray(a = [1], b = [2]) |
| 728 | + mismatched_names_b = StructArray(x = [3], y = [4]) |
| 729 | + @test_throws ArgumentError vcat(mismatched_names_a, mismatched_names_b) |
| 730 | + @test_throws ArgumentError reduce(vcat, [mismatched_names_a, mismatched_names_b]) |
| 731 | + mixed_rowtype_a = StructArray(re = [1.0], im = [2.0]) |
| 732 | + mixed_rowtype_b = StructArray(ComplexF64[3 + 4im]) |
| 733 | + @test_throws ArgumentError vcat(mixed_rowtype_a, mixed_rowtype_b) |
| 734 | + @test_throws ArgumentError reduce(vcat, [mixed_rowtype_a, mixed_rowtype_b]) |
| 735 | + different_names_a = StructArray(a = [1]) |
| 736 | + different_names_b = StructArray(x = [2], y = [3], z = [4]) |
| 737 | + @test_throws ArgumentError vcat(different_names_a, different_names_b) |
| 738 | + @test_throws ArgumentError reduce(vcat, [different_names_a, different_names_b]) |
| 739 | + different_lengths_a = StructArray(([1], [2], [3])) |
| 740 | + different_lengths_b = StructArray(([4], [5])) |
| 741 | + @test_throws ArgumentError reduce(vcat, [different_lengths_a, different_lengths_b]) |
| 742 | + @test_throws ArgumentError reduce(hcat, [reshape(different_lengths_a, 1, 1), reshape(different_lengths_b, 1, 1)]) |
| 743 | + |
696 | 744 | # Check that `cat(dims=1)` doesn't commit type piracy (#254) |
697 | 745 | # We only test that this works, the return value is immaterial |
698 | 746 | @test cat(dims=1) == vcat() |
|
0 commit comments