-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_basics.jl
97 lines (85 loc) · 2.54 KB
/
test_basics.jl
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
91
92
93
94
95
96
97
module TestBasics
using Test
using InitialValues
using InitialValues: isknown, hasinitialvalue
OPS = [*, +, |, &, min, max, Base.add_sum, Base.mul_prod]
@testset for op in OPS
@test op(Init(op), :anything) === :anything
@test op(:anything, Init(op)) === :anything
@test op(INIT, :anything) === :anything
@test op(:anything, INIT) === :anything
@test hasinitialvalue(op)
@test hasinitialvalue(typeof(op))
@test isknown(Init(op))
end
@testset "show" begin
@testset "$desired" for (op, desired) in [
(+, "Init(+)"),
(*, "Init(*)"),
(|, "Init(|)"),
(&, "Init(&)"),
(min, "Init(min)"),
(max, "Init(max)"),
]
@test repr(Init(op); context=:limit => true) == desired
@test repr(Init(op)) == "InitialValues.$desired"
@test string(Init(op)) == "InitialValues.$desired"
end
@testset "INIT" begin
@test repr(INIT; context=:limit => true) == "INIT"
@test repr(INIT) == "InitialValues.INIT"
@test string(INIT) == "InitialValues.INIT"
end
end
@testset "hasinitialvalue" begin
@test !hasinitialvalue(-)
@test !hasinitialvalue(typeof(-))
@test !hasinitialvalue(Int)
@test !hasinitialvalue(Type{Int})
end
@testset "missing" begin
@test min(Init(min), missing) === missing
@test max(Init(max), missing) === missing
end
@testset "promote" begin
for op in OPS
T = typeof(Init(op))
@test promote_type(T, Val{0}) == Union{T,Val{0}}
end
end
@testset "convert" begin
@testset "float" begin
@test float(Init(+)) === 0.0
@test float(Init(*)) === 1.0
@test float(Init(Base.add_sum)) === 0.0
@test float(Init(Base.mul_prod)) === 1.0
end
@testset "Integer" begin
@test Integer(Init(+)) === 0
@test Integer(Init(*)) === 1
@test Integer(Init(Base.add_sum)) === 0
@test Integer(Init(Base.mul_prod)) === 1
end
@testset for T in [
Int,
UInt8,
Float64,
Float32,
]
@test convert(T, Init(+))::T == 0
@test convert(T, Init(*))::T == 1
@test convert(T, Init(Base.add_sum))::T == 0
@test convert(T, Init(Base.mul_prod))::T == 1
end
@test convert(String, Init(*)) === ""
end
@testset "asmonoid" begin
absmin = asmonoid() do a, b
abs(a) < abs(b) ? a : b
end
@test absmin(Init(absmin), Inf) === Inf
@test absmin(missing, Init(absmin)) === missing
@test absmin(Init(absmin), Init(absmin)) === Init(absmin)
@test asmonoid(+) === +
end
end # module