comparison LazyTensors/test/runtests.jl @ 205:70e1f3401d82 boundary_conditions

Merge. Throw away tests written by Jonatan
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 24 Jun 2019 14:43:37 +0200
parents 9d9f7b0e514b 19e579a5031f
children 7b0650021b36
comparison
equal deleted inserted replaced
204:9d9f7b0e514b 205:70e1f3401d82
55 @test (m*m*v)[6] == (:apply,m*v,6) 55 @test (m*m*v)[6] == (:apply,m*v,6)
56 @test_broken BoundsError == (m*m*v)[0] 56 @test_broken BoundsError == (m*m*v)[0]
57 @test_broken BoundsError == (m*m*v)[7] 57 @test_broken BoundsError == (m*m*v)[7]
58 end 58 end
59 59
60 @testset "LazyArray" begin
61 struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D}
62 data::T1
63 end
64 Base.size(v::DummyArray) = size(v.data)
65 Base.getindex(v::DummyArray, I...) = v.data[I...]
60 66
61 @testset "Lazy elementwise operations" begin 67 # Test lazy operations
62 struct DummyLazyArray{D} <: LazyArray{Int,D} 68 v1 = [1, 2.3, 4]
63 size::NTuple{D,Int} 69 v2 = [1., 2, 3]
70 r_add = v1 .+ v2
71 r_sub = v1 .- v2
72 r_times = v1 .* v2
73 r_div = v1 ./ v2
74 @test isa(v1 +̃ v2, LazyArray)
75 @test isa(v1 -̃ v2, LazyArray)
76 @test isa(v1 *̃ v2, LazyArray)
77 @test isa(v1 /̃ v2, LazyArray)
78 for i ∈ eachindex(v1)
79 @test (v1 +̃ v2)[i] == r_add[i]
80 @test (v1 -̃ v2)[i] == r_sub[i]
81 @test (v1 *̃ v2)[i] == r_times[i]
82 @test (v1 /̃ v2)[i] == r_div[i]
64 end 83 end
65 Base.size(a::DummyLazyArray) = a.size 84 @test_throws BoundsError (v1 +̃ v2)[4]
66 Base.getindex(a::DummyLazyArray, I...) = prod(I) 85 v2 = [1., 2, 3, 4]
86 # Test that size of arrays is asserted when not specified inbounds
87 @test_throws AssertionError v1 +̃ v2
88 # Test that no error checking is performed when specified inbounds
89 res = (v1,v2) -> (@inbounds (v1 +̃ v2)[1] == 2)
90 @test res(v1,v2)
67 91
68 92 # Test operations on LazyArray
69 a = DummyLazyArray((3,)) 93 v1 = DummyArray([1, 2.3, 4])
70 94 v2 = [1., 2, 3]
71 @test (a+a)[3] == 6 95 @test isa(v1 + v2, LazyArray)
72 @test (a-a)[3] == 0 96 @test isa(v2 + v1, LazyArray)
73 @test (a*a)[3] == 9 97 @test isa(v1 - v2, LazyArray)
74 98 @test isa(v2 - v1, LazyArray)
75 a = DummyLazyArray((4,)) 99 @test isa(v1 * v2, LazyArray)
76 b = [3,2,1,2] 100 @test isa(v2 * v1, LazyArray)
77 101 @test isa(v1 / v2, LazyArray)
78 @test (a+b)[4] == 6 102 @test isa(v2 / v1, LazyArray)
79 @test (a-b)[4] == 2 103 for i ∈ eachindex(v2)
80 @test (a*b)[4] == 8 104 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add[i]
81 105 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub[i]
82 @test (b+a)[4] == 6 106 @test (v1 * v2)[i] == (v2 * v1)[i] == r_times[i]
83 @test (b-a)[4] == -2 107 @test (v1 / v2)[i] == 1/((v2 / v1)[i]) == r_div[i]
84 @test (b*a)[4] == 8 108 end
109 @test_throws BoundsError (v1 + v2)[4]
110 v2 = [1., 2, 3, 4]
111 # Test that size of arrays is asserted when not specified inbounds
112 @test_throws AssertionError v1 + v2
113 # Test that no error checking is performed when specified inbounds
114 res = (v1,v2) -> (@inbounds (v1 + v2)[1] == 2)
115 @test res(v1,v2)
85 end 116 end