|
177 | 177 | str = String(take!(io))
|
178 | 178 | @test occursin("x1 + x2", str)
|
179 | 179 | end
|
| 180 | + |
| 181 | +@testset "eltype (issue #32)" begin |
| 182 | + # Tests fix for |
| 183 | + # https://github.com/JuliaArrays/MappedArrays.jl/issues/32#issuecomment-682985419 |
| 184 | + T = Union{Missing, Float32} |
| 185 | + @test eltype(of_eltype(T, [missing, 3])) == T |
| 186 | + @test eltype(of_eltype(T, [3, missing])) == T |
| 187 | + @test eltype(of_eltype(Union{Missing, Float64}, [1, 2])) == Float64 |
| 188 | + |
| 189 | + @test eltype(mappedarray(identity, [1, missing])) == Union{Missing, Int} |
| 190 | + @test eltype(mappedarray(identity, [missing, 1])) == Union{Missing, Int} |
| 191 | + |
| 192 | + # ReadonlyMappedArray and MappedArray |
| 193 | + _zero(x) = x > 0 ? x : 0 |
| 194 | + @test eltype(mappedarray(_zero, [1, 1.0])) == Union{Float64,Int} |
| 195 | + @test eltype(mappedarray(_zero, [1.0, 1])) == Union{Float64,Int} |
| 196 | + @test eltype(mappedarray(_zero, [1, 1])) == Int |
| 197 | + |
| 198 | + @test eltype(mappedarray(_zero, identity, [1, 1.0])) == Union{Float64,Int} |
| 199 | + @test eltype(mappedarray(_zero, identity, [1.0, 1])) == Union{Float64,Int} |
| 200 | + @test eltype(mappedarray(_zero, identity, [1, 1])) == Int |
| 201 | + |
| 202 | + # MultiMappedArray and ReadonlyMultiMappedArray |
| 203 | + _sum(x, y) = _zero(x) + _zero(y) |
| 204 | + inferred_type = VERSION >= v"1.6.0-RC1" ? Union{Missing, Float64, Int64} : Any |
| 205 | + @test eltype(mappedarray(_sum, [1, 1.0], [1.0, missing])) == inferred_type |
| 206 | + @test eltype(mappedarray(_sum, [1, 1], [2, 2])) == Int |
| 207 | + @test eltype(mappedarray(_sum, identity, [1, 1.0], [1.0, missing])) == inferred_type |
| 208 | + @test eltype(mappedarray(_sum, identity, [1, 1], [2, 2])) == Int |
| 209 | + |
| 210 | + _maybe_int(x) = x > 0 ? x : Int(x) |
| 211 | + @test eltype(mappedarray(_maybe_int, Float64, [1.0, 1, -1, -1.0])) == Union{Float64, Int64} |
| 212 | + @test eltype(mappedarray(_maybe_int, Float64, [1.0, -1.0])) == Union{Float64, Int64} |
| 213 | + @test eltype(mappedarray(_maybe_int, Float64, [1, -1])) == Int64 |
| 214 | + @test eltype(mappedarray(Float64, _maybe_int, [1.0, 1, -1, -1.0])) == Float64 |
| 215 | + @test eltype(mappedarray(Float64, _maybe_int, [1, -1])) == Float64 |
| 216 | +end |
0 commit comments