diff LazyTensors/test/runtests.jl @ 200:c19bfad0e836 boundary_conditions

Add tests for LazyArrays and lazy operations
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Sun, 23 Jun 2019 20:44:09 +0200
parents b5c9be7f391c
children 6b7019f2cd41
line wrap: on
line diff
--- a/LazyTensors/test/runtests.jl	Sat Jun 22 20:56:17 2019 +0200
+++ b/LazyTensors/test/runtests.jl	Sun Jun 23 20:44:09 2019 +0200
@@ -56,3 +56,53 @@
     @test_broken BoundsError == (m*m*v)[0]
     @test_broken BoundsError == (m*m*v)[7]
 end
+
+@testset "LazyArray" begin
+    struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D}
+        data::T1
+    end
+    Base.size(v::DummyArray) = size(v.data)
+    Base.getindex(v::DummyArray, I...) = v.data[I...]
+
+    # Test lazy operations
+    v1 = [1, 2.3, 4]
+    v2 = [1., 2, 3]
+    r_add = v1 .+ v2
+    r_sub = v1 .- v2
+    r_times = v1 .* v2
+    r_div = v1 ./ v2
+    @test isa(v1 +̃ v2, LazyArray)
+    @test isa(v1 -̃ v2, LazyArray)
+    @test isa(v1 *̃ v2, LazyArray)
+    @test isa(v1 /̃ v2, LazyArray)
+    for i ∈ eachindex(v1)
+        @test (v1 +̃ v2)[i] == r_add[i]
+        @test (v1 -̃ v2)[i] == r_sub[i]
+        @test (v1 *̃ v2)[i] == r_times[i]
+        @test (v1 /̃ v2)[i] == r_div[i]
+    end
+    @test_throws BoundsError (v1 +̃  v2)[4]
+
+    # Test operations on LazyArray
+    v1 = DummyArray([1, 2.3, 4])
+    @test isa(v1 + v2, LazyArray)
+    @test isa(v2 + v1, LazyArray)
+    @test isa(v1 - v2, LazyArray)
+    @test isa(v2 - v1, LazyArray)
+    @test isa(v1 * v2, LazyArray)
+    @test isa(v2 * v1, LazyArray)
+    @test_broken isa(v1 / v2, LazyArray)
+    @test_broken isa(v2 / v1, LazyArray)
+    for i ∈ eachindex(v2)
+        @test (v1 + v2)[i] == (v2 + v1)[i] == r_add[i]
+        @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub[i]
+        @test (v1 * v2)[i] == (v2 * v1)[i]  ==  r_times[i]
+        @test_broken (v1 / v2)[i] == 1/((v2 / v1)[i])  ==  r_div[i]
+    end
+    @test_throws BoundsError (v1 + v2)[4]
+    v2 = [1., 2, 3, 4]
+    # Test that size of arrays is asserted when not specified inbounds
+    @test_throws AssertionError v1 + v2
+    # Test that no error checking is performed when specified inbounds
+    @test_broken @inbounds((v1 + v2)[1] == 2)
+end