Skip to content

Commit 837bc9c

Browse files
committed
Rearrange tests for Normed conversions
1 parent acccff6 commit 837bc9c

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

test/normed.jl

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,54 +89,61 @@ end
8989
x = N0f8(0.5)
9090
@test convert(N0f8, x) === x
9191

92-
@test convert(N0f8, 1.1/typemax(UInt8)) == eps(N0f8)
93-
@test convert(N6f10, 1.1/typemax(UInt16)*64) == eps(N6f10)
94-
@test convert(N4f12, 1.1/typemax(UInt16)*16) == eps(N4f12)
95-
@test convert(N2f14, 1.1/typemax(UInt16)*4) == eps(N2f14)
96-
@test convert(N0f16, 1.1/typemax(UInt16)) == eps(N0f16)
97-
@test convert(Normed{UInt32,16}, 1.1/typemax(UInt32)*2^16) == eps(Normed{UInt32,16})
98-
@test convert(Normed{UInt64,3}, 1.1/typemax(UInt64)*UInt64(2)^61) == eps(Normed{UInt64,3})
99-
@test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) == eps(Normed{UInt128,7})
100-
101-
@test convert(N0f8, 1.1f0/typemax(UInt8)) == eps(N0f8)
102-
103-
@test convert(N0f8, 1//255) === eps(N0f8)
104-
@test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
105-
@test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
106-
@test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8}))
92+
@test convert(N0f8, 1.1/typemax(UInt8)) === eps(N0f8)
93+
@test convert(N0f8, 1.1f0/typemax(UInt8)) === eps(N0f8)
94+
@test convert(N6f10, 1.1/typemax(UInt16)*64) === eps(N6f10)
95+
@test convert(N4f12, 1.1/typemax(UInt16)*16) === eps(N4f12)
96+
@test convert(N2f14, 1.1/typemax(UInt16)*4) === eps(N2f14)
97+
@test convert(N0f16, 1.1/typemax(UInt16)) === eps(N0f16)
98+
@test convert(N16f16, 1.1/typemax(UInt32)*2^16) === eps(N16f16)
99+
@test convert(N61f3, 1.1/typemax(UInt64)*UInt64(2)^61) === eps(N61f3)
100+
@test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) === eps(Normed{UInt128,7})
107101

108102
@test convert(N0f8, Base.TwicePrecision(1.0)) === 1N0f8
109103

110-
@test convert(Float64, eps(N0f8)) == 1/typemax(UInt8)
111-
@test convert(Float32, eps(N0f8)) == 1.0f0/typemax(UInt8)
112-
@test convert(BigFloat, eps(N0f8)) == BigFloat(1)/typemax(UInt8)
113-
# TODO: migrate to separate testsets
114-
for T in target(Normed)
115-
@test convert(Bool, zero(T)) == false
116-
@test convert(Bool, one(T)) == true
117-
eps(T) != 1 && @test_throws InexactError convert(Bool, convert(T, 0.2))
118-
@test convert(Int, one(T)) == 1
119-
@test convert(Integer, one(T)) == 1
120-
@test convert(Rational, one(T)) == 1
121-
end
122104
@test convert(N0f16, one(N0f8)) === one(N0f16)
123-
@test convert(N0f16, N0f8(0.5)).i === 0x8080
124-
@test convert(Normed{UInt16,7}, Normed{UInt8,7}(0.504)) === Normed{UInt16,7}(0.504)
105+
@test convert(N0f16, N0f8(0.5)) === reinterpret(N0f16, 0x8080)
106+
@test convert(N9f7, N1f7(0.504)) === N9f7(0.504)
125107

126108
# avoiding overflow with Float16
127109
@test N0f16(Float16(1.0)) === N0f16(1.0)
128110
@test Float16(1.0) % N0f16 === N0f16(1.0)
129111
end
130112

113+
@testset "bool conversions" begin
114+
@testset "$N to/from Bool" for N in target(Normed)
115+
@test convert(Bool, zero(N)) === false
116+
@test convert(Bool, oneunit(N)) === true
117+
eps(N) < 1 && @test_throws InexactError convert(Bool, convert(N, 0.2))
118+
@test convert(N, true) === oneunit(N)
119+
@test convert(N, false) === zero(N)
120+
end
121+
@test Bool(1N0f8) === true
122+
end
123+
131124
@testset "integer conversions" begin
125+
@testset "$N to/from integer" for N in target(Normed)
126+
@test convert(Int, oneunit(N)) === 1
127+
@test convert(Integer, oneunit(N)) === oneunit(rawtype(N))
128+
@test convert(N, 1) === oneunit(N)
129+
@test convert(N, 0x0) === zero(N)
130+
end
132131
@test convert(UInt, 1N1f7) === UInt(1)
133-
@test convert(Integer, 1N1f7) === 0x01
134-
@test convert(Int, 1N1f7) === 1
135132
@test_throws InexactError convert(Integer, 0.5N1f7)
136133
@test_throws InexactError convert(Int8, 256N8f8)
137134
end
138135

139136
@testset "rational conversions" begin
137+
@testset "$N to/from rational" for N in target(Normed)
138+
@test convert(Rational, oneunit(N)) == 1//1
139+
@test convert(Rational{Int}, zero(N)) === 0//1
140+
@test convert(N, 1//1) === oneunit(N)
141+
end
142+
@test convert(N0f8, 1//255) === eps(N0f8)
143+
@test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
144+
@test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
145+
@test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8}))
146+
140147
@test convert(Rational, 0.5N0f8) === Rational{UInt8}(0x80//0xff)
141148
@test convert(Rational, 0.5N4f12) === Rational{UInt16}(0x800//0xfff)
142149
@test convert(Rational{Int}, 0.5N0f8) === Rational{Int}(0x80//0xff)
@@ -149,6 +156,7 @@ end
149156

150157
@testset "BigFloat conversions" begin
151158
@test convert(BigFloat, 0.5N0f8)::BigFloat == 128 / big"255"
159+
@test convert(BigFloat, eps(N0f8))::BigFloat == 1 / big"255"
152160

153161
@test big(N7f1) === BigFloat # !== BigInt
154162
@test big(0.5N4f4)::BigFloat == 8 / big"15"

0 commit comments

Comments
 (0)