comparison test/LazyTensors/lazy_array_test.jl @ 712:de2df1214394 feature/selectable_tests

Split testfile for LazyTensors
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 20 Feb 2021 20:59:32 +0100
parents
children 7ef605b8f132
comparison
equal deleted inserted replaced
711:df88aee35bb9 712:de2df1214394
1 using Test
2 using Sbplib.LazyTensors
3 using Sbplib.RegionIndices
4
5
6 @testset "LazyArray" begin
7 @testset "LazyConstantArray" begin
8 @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2}
9
10 lca = LazyTensors.LazyConstantArray(3.0,(3,2))
11 @test eltype(lca) == Float64
12 @test ndims(lca) == 2
13 @test size(lca) == (3,2)
14 @test lca[2] == 3.0
15 end
16 struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D}
17 data::T1
18 end
19 Base.size(v::DummyArray) = size(v.data)
20 Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...]
21
22 # Test lazy operations
23 v1 = [1, 2.3, 4]
24 v2 = [1., 2, 3]
25 s = 3.4
26 r_add_v = v1 .+ v2
27 r_sub_v = v1 .- v2
28 r_times_v = v1 .* v2
29 r_div_v = v1 ./ v2
30 r_add_s = v1 .+ s
31 r_sub_s = v1 .- s
32 r_times_s = v1 .* s
33 r_div_s = v1 ./ s
34 @test isa(v1 +̃ v2, LazyArray)
35 @test isa(v1 -̃ v2, LazyArray)
36 @test isa(v1 *̃ v2, LazyArray)
37 @test isa(v1 /̃ v2, LazyArray)
38 @test isa(v1 +̃ s, LazyArray)
39 @test isa(v1 -̃ s, LazyArray)
40 @test isa(v1 *̃ s, LazyArray)
41 @test isa(v1 /̃ s, LazyArray)
42 @test isa(s +̃ v1, LazyArray)
43 @test isa(s -̃ v1, LazyArray)
44 @test isa(s *̃ v1, LazyArray)
45 @test isa(s /̃ v1, LazyArray)
46 for i ∈ eachindex(v1)
47 @test (v1 +̃ v2)[i] == r_add_v[i]
48 @test (v1 -̃ v2)[i] == r_sub_v[i]
49 @test (v1 *̃ v2)[i] == r_times_v[i]
50 @test (v1 /̃ v2)[i] == r_div_v[i]
51 @test (v1 +̃ s)[i] == r_add_s[i]
52 @test (v1 -̃ s)[i] == r_sub_s[i]
53 @test (v1 *̃ s)[i] == r_times_s[i]
54 @test (v1 /̃ s)[i] == r_div_s[i]
55 @test (s +̃ v1)[i] == r_add_s[i]
56 @test (s -̃ v1)[i] == -r_sub_s[i]
57 @test (s *̃ v1)[i] == r_times_s[i]
58 @test (s /̃ v1)[i] == 1/r_div_s[i]
59 end
60 @test_throws BoundsError (v1 +̃ v2)[4]
61 v2 = [1., 2, 3, 4]
62 # Test that size of arrays is asserted when not specified inbounds
63 # TODO: Replace these errors with SizeMismatch
64 @test_throws DimensionMismatch v1 +̃ v2
65
66 # Test operations on LazyArray
67 v1 = DummyArray([1, 2.3, 4])
68 v2 = [1., 2, 3]
69 @test isa(v1 + v2, LazyArray)
70 @test isa(v2 + v1, LazyArray)
71 @test isa(v1 - v2, LazyArray)
72 @test isa(v2 - v1, LazyArray)
73 for i ∈ eachindex(v2)
74 @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i]
75 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i]
76 end
77 @test_throws BoundsError (v1 + v2)[4]
78 v2 = [1., 2, 3, 4]
79 # Test that size of arrays is asserted when not specified inbounds
80 # TODO: Replace these errors with SizeMismatch
81 @test_throws DimensionMismatch v1 + v2
82 end
83
84
85 @testset "LazyFunctionArray" begin
86 @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9]
87 @test LazyFunctionArray((i,j)->i*j, (3,2)) == [
88 1 2;
89 2 4;
90 3 6;
91 ]
92
93 @test size(LazyFunctionArray(i->i^2, (3,))) == (3,)
94 @test size(LazyFunctionArray((i,j)->i*j, (3,2))) == (3,2)
95
96 @inferred LazyFunctionArray(i->i^2, (3,))[2]
97
98 @test_throws BoundsError LazyFunctionArray(i->i^2, (3,))[4]
99 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2]
100 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3]
101
102 end