Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ _bcsm(a::Number, b::Number) = a == b || b == 1
# (We may not want to define general promotion rules between, say, OneTo and Slice, but if
# we get here we know the axes are at least consistent for the purposes of broadcasting)
axistype(a::T, b::T) where T = a
axistype(a::OneTo, b::OneTo) = OneTo{Int}(a)
axistype(a, b) = UnitRange{Int}(a)

## Check that all arguments are broadcast compatible with shape
Expand Down
52 changes: 34 additions & 18 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -572,24 +572,40 @@ end
@test sum(0:0.000001:1) == 500000.5
@test sum(0:0.1:10) == 505.
end
@testset "broadcasted operations with scalars" begin
@test broadcast(-, 1:3) === -1:-1:-3
@test broadcast(-, 1:3, 2) === -1:1
@test broadcast(-, 1:3, 0.25) === 1-0.25:3-0.25
@test broadcast(+, 1:3) === 1:3
@test broadcast(+, 1:3, 2) === 3:5
@test broadcast(+, 1:3, 0.25) === 1+0.25:3+0.25
@test broadcast(+, 1:2:6, 1) === 2:2:6
@test broadcast(+, 1:2:6, 0.3) === 1+0.3:2:5+0.3
@test broadcast(-, 1:2:6, 1) === 0:2:4
@test broadcast(-, 1:2:6, 0.3) === 1-0.3:2:5-0.3
@test broadcast(-, 2, 1:3) === 1:-1:-1
end
@testset "operations between ranges and arrays" begin
@test all(([1:5;] + (5:-1:1)) .== 6)
@test all(((5:-1:1) + [1:5;]) .== 6)
@test all(([1:5;] - (1:5)) .== 0)
@test all(((1:5) - [1:5;]) .== 0)
@testset "broadcasted operations with scalars" for T in (Int, UInt, Int128)
@test broadcast(-, T(1):3, 2) === T(1)-2:1
@test broadcast(-, T(1):3, 0.25) === T(1)-0.25:3-0.25
@test broadcast(+, T(1):3) === T(1):3
@test broadcast(+, T(1):3, 2) === T(3):5
@test broadcast(+, T(1):3, 0.25) === T(1)+0.25:3+0.25
@test broadcast(+, T(1):2:6, 1) === T(2):2:6
@test broadcast(+, T(1):2:6, 0.3) === T(1)+0.3:2:5+0.3
@test broadcast(-, T(1):2:6, 1) === T(0):2:4
@test broadcast(-, T(1):2:6, 0.3) === T(1)-0.3:2:5-0.3
if T <: Unsigned
@test_broken broadcast(-, T(1):3) == -T(1):-1:-T(3)
@test_broken broadcast(-, 2, T(1):3) == T(1):-1:-T(1)
else
@test length(broadcast(-, T(1):3, 2)) === length(T(1)-2:T(3)-2)
@test broadcast(-, T(1):3) == -T(1):-1:-T(3)
@test broadcast(-, 2, T(1):3) == T(1):-1:-T(1)
end
end
@testset "operations between ranges and arrays" for T in (Int, UInt, Int128)
@test all(([T(1):5;] + (T(5):-1:1)) .=== T(6))
@test all(((T(5):-1:1) + [T(1):5;]) .=== T(6))
@test all(([T(1):5;] - (T(1):5)) .=== T(0))
@test all(((T(1):5) - [T(1):5;]) .=== T(0))
end
@testset "issue #32442: Broadcasting over views with non-`Int` indices" begin
a=rand(UInt32,20)
c=rand(UInt64,5)
@test reinterpret(UInt64,view(a,UInt64.(11:20))) .- c ==
reinterpret(UInt64,view(a,(11:20))) .- c ==
reinterpret(UInt64,view(a,(UInt64(11):UInt64(20)))) .- c ==
copy(reinterpret(UInt64,view(a,(UInt64(11):UInt64(20))))) .- c

@test view(a,(Int32(11):Int32(20))) .+ [1] == a[11:20] .+ 1
end
@testset "tricky floating-point ranges" begin
for (start, step, stop, len) in ((1, 1, 3, 3), (0, 1, 3, 4),
Expand Down