Mercurial > repos > public > sbplib_julia
comparison 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 |
comparison
equal
deleted
inserted
replaced
199:a22b419bbdf0 | 200:c19bfad0e836 |
---|---|
54 @test (m*m*v)[3] == (:apply,m*v,3) | 54 @test (m*m*v)[3] == (:apply,m*v,3) |
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 | |
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...] | |
66 | |
67 # Test lazy operations | |
68 v1 = [1, 2.3, 4] | |
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] | |
83 end | |
84 @test_throws BoundsError (v1 +̃ v2)[4] | |
85 | |
86 # Test operations on LazyArray | |
87 v1 = DummyArray([1, 2.3, 4]) | |
88 @test isa(v1 + v2, LazyArray) | |
89 @test isa(v2 + v1, LazyArray) | |
90 @test isa(v1 - v2, LazyArray) | |
91 @test isa(v2 - v1, LazyArray) | |
92 @test isa(v1 * v2, LazyArray) | |
93 @test isa(v2 * v1, LazyArray) | |
94 @test_broken isa(v1 / v2, LazyArray) | |
95 @test_broken isa(v2 / v1, LazyArray) | |
96 for i ∈ eachindex(v2) | |
97 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add[i] | |
98 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub[i] | |
99 @test (v1 * v2)[i] == (v2 * v1)[i] == r_times[i] | |
100 @test_broken (v1 / v2)[i] == 1/((v2 / v1)[i]) == r_div[i] | |
101 end | |
102 @test_throws BoundsError (v1 + v2)[4] | |
103 v2 = [1., 2, 3, 4] | |
104 # Test that size of arrays is asserted when not specified inbounds | |
105 @test_throws AssertionError v1 + v2 | |
106 # Test that no error checking is performed when specified inbounds | |
107 @test_broken @inbounds((v1 + v2)[1] == 2) | |
108 end |