Mercurial > repos > public > sbplib_julia
comparison LazyTensors/test/runtests.jl @ 231:fbabfd4e8f20
Merge in boundary_conditions
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 26 Jun 2019 15:07:47 +0200 |
parents | 2aa33d0eef90 |
children | a20bb4fac23d |
comparison
equal
deleted
inserted
replaced
144:ce56727e4232 | 231:fbabfd4e8f20 |
---|---|
1 using Test | |
2 using LazyTensors | |
3 | |
4 @testset "Generic Mapping methods" begin | |
5 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end | |
6 LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply | |
7 @test range_dim(DummyMapping{Int,2,3}()) == 2 | |
8 @test domain_dim(DummyMapping{Int,2,3}()) == 3 | |
9 @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0) == :apply | |
10 end | |
11 | |
12 @testset "Generic Operator methods" begin | |
13 struct DummyOperator{T,D} <: TensorOperator{T,D} end | |
14 @test range_size(DummyOperator{Int,2}(), (3,5)) == (3,5) | |
15 @test domain_size(DummyOperator{Float64, 3}(), (3,3,1)) == (3,3,1) | |
16 end | |
17 | |
18 @testset "Mapping transpose" begin | |
19 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end | |
20 | |
21 LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply | |
22 LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose | |
23 | |
24 LazyTensors.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = :range_size | |
25 LazyTensors.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = :domain_size | |
26 | |
27 m = DummyMapping{Float64,2,3}() | |
28 @test m'' == m | |
29 @test apply(m',zeros(Float64,(0,0)),0) == :apply_transpose | |
30 @test apply(m'',zeros(Float64,(0,0,0)),0) == :apply | |
31 @test apply_transpose(m', zeros(Float64,(0,0,0)),0) == :apply | |
32 | |
33 @test range_size(m', (0,0)) == :domain_size | |
34 @test domain_size(m', (0,0,0)) == :range_size | |
35 end | |
36 | |
37 @testset "TensorApplication" begin | |
38 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end | |
39 | |
40 LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = (:apply,v,i) | |
41 LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose | |
42 | |
43 LazyTensors.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = 2 .* domain_size | |
44 LazyTensors.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = range_size.÷2 | |
45 | |
46 | |
47 m = DummyMapping{Int, 1, 1}() | |
48 v = [0,1,2] | |
49 @test m*v isa AbstractVector{Int} | |
50 @test size(m*v) == 2 .*size(v) | |
51 @test (m*v)[0] == (:apply,v,0) | |
52 @test m*m*v isa AbstractVector{Int} | |
53 @test (m*m*v)[1] == (:apply,m*v,1) | |
54 @test (m*m*v)[3] == (:apply,m*v,3) | |
55 @test (m*m*v)[6] == (:apply,m*v,6) | |
56 @test_broken BoundsError == (m*m*v)[0] | |
57 @test_broken BoundsError == (m*m*v)[7] | |
58 end | |
59 | |
60 @testset "TensorMapping binary operations" begin | |
61 struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} | |
62 λ::T | |
63 end | |
64 | |
65 LazyTensors.apply(m::ScalarMapping{T,R,D}, v, i) where {T,R,D} = m.λ*v[i] | |
66 LazyTensors.range_size(m::ScalarMapping, domain_size) = domain_size | |
67 LazyTensors.domain_size(m::ScalarMapping, range_sizes) = range_sizes | |
68 | |
69 A = ScalarMapping{Float64,1,1}(2.0) | |
70 B = ScalarMapping{Float64,1,1}(3.0) | |
71 | |
72 v = [1.1,1.2,1.3] | |
73 | |
74 for i ∈ eachindex(v) | |
75 @test ((A+B)*v)[i] == 2*v[i] + 3*v[i] | |
76 end | |
77 | |
78 for i ∈ eachindex(v) | |
79 @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] | |
80 end | |
81 | |
82 @test range_size(A+B, (3,)) == range_size(A, (3,)) == range_size(B,(3,)) | |
83 @test domain_size(A+B, (3,)) == domain_size(A, (3,)) == domain_size(B,(3,)) | |
84 end | |
85 | |
86 @testset "LazyArray" begin | |
87 struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} | |
88 data::T1 | |
89 end | |
90 Base.size(v::DummyArray) = size(v.data) | |
91 Base.getindex(v::DummyArray, I...) = v.data[I...] | |
92 | |
93 # Test lazy operations | |
94 v1 = [1, 2.3, 4] | |
95 v2 = [1., 2, 3] | |
96 r_add = v1 .+ v2 | |
97 r_sub = v1 .- v2 | |
98 r_times = v1 .* v2 | |
99 r_div = v1 ./ v2 | |
100 @test isa(v1 +̃ v2, LazyArray) | |
101 @test isa(v1 -̃ v2, LazyArray) | |
102 @test isa(v1 *̃ v2, LazyArray) | |
103 @test isa(v1 /̃ v2, LazyArray) | |
104 for i ∈ eachindex(v1) | |
105 @test (v1 +̃ v2)[i] == r_add[i] | |
106 @test (v1 -̃ v2)[i] == r_sub[i] | |
107 @test (v1 *̃ v2)[i] == r_times[i] | |
108 @test (v1 /̃ v2)[i] == r_div[i] | |
109 end | |
110 @test_throws BoundsError (v1 +̃ v2)[4] | |
111 v2 = [1., 2, 3, 4] | |
112 # Test that size of arrays is asserted when not specified inbounds | |
113 @test_throws DimensionMismatch v1 +̃ v2 | |
114 | |
115 # Test operations on LazyArray | |
116 v1 = DummyArray([1, 2.3, 4]) | |
117 v2 = [1., 2, 3] | |
118 @test isa(v1 + v2, LazyArray) | |
119 @test isa(v2 + v1, LazyArray) | |
120 @test isa(v1 - v2, LazyArray) | |
121 @test isa(v2 - v1, LazyArray) | |
122 for i ∈ eachindex(v2) | |
123 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add[i] | |
124 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub[i] | |
125 end | |
126 @test_throws BoundsError (v1 + v2)[4] | |
127 v2 = [1., 2, 3, 4] | |
128 # Test that size of arrays is asserted when not specified inbounds | |
129 @test_throws DimensionMismatch v1 + v2 | |
130 end |