Skip to content

Commit

Permalink
More broadcast tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Sep 24, 2016
1 parent 23325fe commit 0b7e17f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
17 changes: 16 additions & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ end
let f17314 = x -> x < 0 ? false : x
@test eltype(broadcast(f17314, 1:3)) === Int
@test eltype(broadcast(f17314, -1:1)) === Integer
@test eltype(broadcast(f17314, Int[])) === Any
@test eltype(broadcast(f17314, Int[])) === Union{Bool,Int}
end
let io = IOBuffer()
broadcast(x->print(io,x), 1:5) # broadcast with side effects
Expand All @@ -336,3 +336,18 @@ end
@test broadcast(+, 1.0, (0, -2.0)) == (1.0,-1.0)
@test broadcast(+, 1.0, (0, -2.0), [1]) == [2.0, 0.0]
@test broadcast(*, ["Hello"], ", ", ["World"], "!") == ["Hello, World!"]

# Ensure that even strange constructors that break `T(x)::T` work with broadcast
immutable StrangeType18623 end
StrangeType18623(x) = x
StrangeType18623(x,y) = (x,y)
@test @inferred broadcast(StrangeType18623, 1:3) == [1,2,3]
@test @inferred broadcast(StrangeType18623, 1:3, 4:6) == [(1,4),(2,5),(3,6)]

@test typeof(Int.(Number[1, 2, 3])) === typeof((x->Int(x)).(Number[1, 2, 3]))

@test @inferred broadcast(CartesianIndex, 1:2) == [CartesianIndex(1), CartesianIndex(2)]
@test @inferred broadcast(CartesianIndex, 1:2, 3:4) == [CartesianIndex(1,3), CartesianIndex(2,4)]

# Issue 18622
@test @inferred tuple.(1:3, 4:6, 7:9)::Vector{Tuple{Int,Int,Int}} == [(1,4,7), (2,5,8), (3,6,9)]
12 changes: 6 additions & 6 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2805,42 +2805,42 @@ let types = (Base.BitInteger_types..., BigInt, Bool,
Complex{Int}, Complex{UInt}, Complex32, Complex64, Complex128)
for S in types
for op in (+, -)
T = @inferred Base._promote_op(op, S)
T = @inferred Base.promote_op(op, S)
t = @inferred op(one(S))
@test T === typeof(t)
end

for R in types
for op in (+, -, *, /, ^)
T = @inferred Base._promote_op(op, S, R)
T = @inferred Base.promote_op(op, S, R)
t = @inferred op(one(S), one(R))
@test T === typeof(t)
end
end
end

@test @inferred(Base._promote_op(!, Bool)) === Bool
@test @inferred(Base.promote_op(!, Bool)) === Bool
end

let types = (Base.BitInteger_types..., BigInt, Bool,
Rational{Int}, Rational{BigInt},
Float16, Float32, Float64, BigFloat)
for S in types, T in types
for op in (<, >, <=, >=, (==))
@test @inferred(Base._promote_op(op, S, T)) === Bool
@test @inferred(Base.promote_op(op, S, T)) === Bool
end
end
end

let types = (Base.BitInteger_types..., BigInt, Bool)
for S in types
T = @inferred Base._promote_op(~, S)
T = @inferred Base.promote_op(~, S)
t = @inferred ~one(S)
@test T === typeof(t)

for R in types
for op in (&, |, <<, >>, (>>>), %, ÷)
T = @inferred Base._promote_op(op, S, R)
T = @inferred Base.promote_op(op, S, R)
t = @inferred op(one(S), one(R))
@test T === typeof(t)
end
Expand Down

0 comments on commit 0b7e17f

Please sign in to comment.