-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basics.jl
More file actions
90 lines (76 loc) · 3.26 KB
/
Copy pathtest_basics.jl
File metadata and controls
90 lines (76 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import FunctionImplementations as FI
using Test: @test, @testset
@testset "FunctionImplementations" begin
@testset "Implementation" begin
struct MyAddAlgorithm end
f = FI.Implementation(+, MyAddAlgorithm())
@test f.f ≡ +
@test f.style ≡ MyAddAlgorithm()
(::typeof(f))(x, y) = "My add"
@test f(2, 3) == "My add"
@test f.f ≡ +
@test f.style ≡ MyAddAlgorithm()
end
@testset "(s::Style)(f)" begin
# Test the shorthand for creating an Implementation by calling a Style with a
# function.
@test FI.style([1, 2, 3])(getindex) ≡
FI.Implementation(getindex, FI.DefaultArrayStyle())
end
@testset "Style" begin
# Test basic Style trait for different array types
@test FI.Style(typeof([1, 2, 3])) ≡ FI.DefaultArrayStyle()
@test FI.style([1, 2, 3]) ≡ FI.DefaultArrayStyle()
@test FI.Style(typeof([1 2; 3 4])) ≡ FI.DefaultArrayStyle()
@test FI.Style(typeof(rand(2, 3, 4))) ≡ FI.DefaultArrayStyle()
# Test custom Style definition
struct CustomStyle <: FI.Style end
struct CustomArray end
FI.Style(::Type{CustomArray}) = CustomStyle()
@test FI.Style(CustomArray) isa CustomStyle
# Test custom AbstractArrayStyle definition
struct MyArray{T, N} <: AbstractArray{T, N}
data::Array{T, N}
end
struct MyArrayStyle <: FI.AbstractArrayStyle end
FI.Style(::Type{<:MyArray}) = MyArrayStyle()
@test FI.Style(MyArray) isa MyArrayStyle
# Test style homogeneity rule (same type returns preserved)
s1 = FI.DefaultArrayStyle()
s2 = FI.DefaultArrayStyle()
@test FI.Style(s1, s2) ≡ FI.DefaultArrayStyle()
# Test UnknownStyle precedence
unknown = FI.UnknownStyle()
known = FI.DefaultArrayStyle()
@test FI.Style(known, unknown) ≡ known
@test FI.Style(unknown, unknown) ≡ unknown
# Test ArrayConflict
conflict = FI.ArrayConflict()
@test conflict isa FI.ArrayConflict
@test conflict isa FI.AbstractArrayStyle
# Test style with no arguments
@test FI.style() ≡ FI.DefaultArrayStyle()
# Test style with single argument
@test FI.style([1, 2]) ≡ FI.DefaultArrayStyle()
@test FI.style([1 2; 3 4]) ≡ FI.DefaultArrayStyle()
# Test style with two arguments
result = FI.style([1, 2], [1 2; 3 4])
@test result ≡ FI.DefaultArrayStyle()
# Test style with same dimensions
result = FI.style([1], [2])
@test result ≡ FI.DefaultArrayStyle()
# Test style with multiple arguments
result = FI.style([1], [1 2], rand(2, 3, 4))
@test result ≡ FI.DefaultArrayStyle()
# Test result_style with single argument
@test FI.result_style(FI.DefaultArrayStyle()) isa FI.DefaultArrayStyle
# Test result_style with two identical styles
s = FI.DefaultArrayStyle()
@test FI.result_style(s, s) ≡ s
# Test result_style with UnknownStyle
known = FI.DefaultArrayStyle()
unknown = FI.UnknownStyle()
@test FI.result_style(known, unknown) ≡ known
@test FI.result_style(unknown, known) ≡ known
end
end