-
Notifications
You must be signed in to change notification settings - Fork 4
/
infinite.jl
121 lines (99 loc) · 3.11 KB
/
infinite.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
@testset "Infinite" begin
@testset "Base" begin
@test_throws MethodError Infinite()
@inferred Infinite(true)
@inferred Infinite(false)
@test Infinite(∞) == ∞
buf = IOBuffer()
showerror(buf, InfMinusInfError())
msg = String(take!(buf))
@test msg == "∞-∞ is undefined"
end
@testset "IO" begin
inf = Infinite(false)
ninf = Infinite(true)
@test string(inf) == "∞"
@test sprint(show, inf, context=:compact=>true) == "∞"
@test sprint(show, inf) == string(inf)
@test string(ninf) == "-∞"
@test sprint(show, ninf, context=:compact=>true) == "-∞"
@test sprint(show, ninf) == string(ninf)
end
@testset "Conversion" begin
@test_throws InexactError convert(Int, ∞)
@test convert(Float64, ∞) == Inf
@test convert(Rational{Int}, ∞) == 1//0
@test_throws InexactError convert(Infinite, 2)
@test convert(Infinite, Inf) == ∞
@test convert(Infinite, 1//0) == ∞
@test convert(Infinite, ∞) == ∞
@test Float64(∞) == Inf
@test one(Infinite) == Infinity.UnknownReal
@test zero(Infinite) == Infinity.UnknownReal
@test float(Infinite) == float(Int)
@test float(Infinity.UnknownReal) == float(Int)
end
@testset "Parse" begin
@test tryparse(Infinite, "∞") === ∞
@test tryparse(Infinite, "-∞") === -∞
@test tryparse(Infinite, "1") === nothing
@test parse(Infinite, "∞") === ∞
@test parse(Infinite, "-∞") === -∞
@test_throws ArgumentError parse(Infinite, "1")
end
@testset "Comparison" begin
x = ∞
@test !isfinite(x)
@test !isfinite(-x)
@test isinf(x)
@test isinf(-x)
@test !iszero(x)
@test !iszero(-x)
@test x == x
@test x == Inf
@test Inf == x
@test x != -x
@test x != -Inf
@test Inf != -x
@test hash(x) != hash(-x)
@test -x < x
@test x > -x
@test x <= x
@test -x >= -x
@test signbit(-x)
@test !signbit(x)
@test sign(x) == 1
@test sign(-x) == -1
@test isapprox(x, x)
@test !isapprox(x, -x)
end
@testset "Arithmetic" begin
x = ∞
@test typemin(Infinite) == -x
@test typemax(Infinite) == x
@test +x == x
@test -x == -x
@test -(-x) == x
@test_throws InfMinusInfError x - x
@test x + x == x
@test -x + -x == -x
@test -x - x == -x
@test x * x == x
@test x * -x == -x
@test -x * x == -x
@test -x * -x == x
@test_throws DivideError x / x
@test abs(x) == x
@test abs(-x) == x
@test_throws DivideError x // x
@test x // 2 == 1//0
@test 2 // x == 0//1
end
@testset "Rand" begin
@test typeof(rand(Infinite)) === Infinite
Random.seed!(1)
@test rand(Infinite) == ∞
@test rand(Infinite) == ∞
@test rand(Infinite) == -∞
end
end