Skip to content

Commit 8586afc

Browse files
committed
Rearrange tests for Normed conversions
1 parent 9adacd5 commit 8586afc

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
@@ -97,54 +97,61 @@ end
9797
x = N0f8(0.5)
9898
@test convert(N0f8, x) === x
9999

100-
@test convert(N0f8, 1.1/typemax(UInt8)) == eps(N0f8)
101-
@test convert(N6f10, 1.1/typemax(UInt16)*64) == eps(N6f10)
102-
@test convert(N4f12, 1.1/typemax(UInt16)*16) == eps(N4f12)
103-
@test convert(N2f14, 1.1/typemax(UInt16)*4) == eps(N2f14)
104-
@test convert(N0f16, 1.1/typemax(UInt16)) == eps(N0f16)
105-
@test convert(Normed{UInt32,16}, 1.1/typemax(UInt32)*2^16) == eps(Normed{UInt32,16})
106-
@test convert(Normed{UInt64,3}, 1.1/typemax(UInt64)*UInt64(2)^61) == eps(Normed{UInt64,3})
107-
@test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) == eps(Normed{UInt128,7})
108-
109-
@test convert(N0f8, 1.1f0/typemax(UInt8)) == eps(N0f8)
110-
111-
@test convert(N0f8, 1//255) === eps(N0f8)
112-
@test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
113-
@test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
114-
@test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8}))
100+
@test convert(N0f8, 1.1/typemax(UInt8)) === eps(N0f8)
101+
@test convert(N0f8, 1.1f0/typemax(UInt8)) === eps(N0f8)
102+
@test convert(N6f10, 1.1/typemax(UInt16)*64) === eps(N6f10)
103+
@test convert(N4f12, 1.1/typemax(UInt16)*16) === eps(N4f12)
104+
@test convert(N2f14, 1.1/typemax(UInt16)*4) === eps(N2f14)
105+
@test convert(N0f16, 1.1/typemax(UInt16)) === eps(N0f16)
106+
@test convert(N16f16, 1.1/typemax(UInt32)*2^16) === eps(N16f16)
107+
@test convert(N61f3, 1.1/typemax(UInt64)*UInt64(2)^61) === eps(N61f3)
108+
@test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) === eps(Normed{UInt128,7})
115109

116110
@test convert(N0f8, Base.TwicePrecision(1.0)) === 1N0f8
117111

118-
@test convert(Float64, eps(N0f8)) == 1/typemax(UInt8)
119-
@test convert(Float32, eps(N0f8)) == 1.0f0/typemax(UInt8)
120-
@test convert(BigFloat, eps(N0f8)) == BigFloat(1)/typemax(UInt8)
121-
# TODO: migrate to separate testsets
122-
for T in target(Normed)
123-
@test convert(Bool, zero(T)) == false
124-
@test convert(Bool, one(T)) == true
125-
eps(T) != 1 && @test_throws InexactError convert(Bool, convert(T, 0.2))
126-
@test convert(Int, one(T)) == 1
127-
@test convert(Integer, one(T)) == 1
128-
@test convert(Rational, one(T)) == 1
129-
end
130112
@test convert(N0f16, one(N0f8)) === one(N0f16)
131-
@test convert(N0f16, N0f8(0.5)).i === 0x8080
132-
@test convert(Normed{UInt16,7}, Normed{UInt8,7}(0.504)) === Normed{UInt16,7}(0.504)
113+
@test convert(N0f16, N0f8(0.5)) === reinterpret(N0f16, 0x8080)
114+
@test convert(N9f7, N1f7(0.504)) === N9f7(0.504)
133115

134116
# avoiding overflow with Float16
135117
@test N0f16(Float16(1.0)) === N0f16(1.0)
136118
@test Float16(1.0) % N0f16 === N0f16(1.0)
137119
end
138120

121+
@testset "bool conversions" begin
122+
@testset "$N to/from Bool" for N in target(Normed)
123+
@test convert(Bool, zero(N)) === false
124+
@test convert(Bool, oneunit(N)) === true
125+
eps(N) < 1 && @test_throws InexactError convert(Bool, convert(N, 0.2))
126+
@test convert(N, true) === oneunit(N)
127+
@test convert(N, false) === zero(N)
128+
end
129+
@test Bool(1N0f8) === true
130+
end
131+
139132
@testset "integer conversions" begin
133+
@testset "$N to/from integer" for N in target(Normed)
134+
@test convert(Int, oneunit(N)) === 1
135+
@test convert(Integer, oneunit(N)) === oneunit(rawtype(N))
136+
@test convert(N, 1) === oneunit(N)
137+
@test convert(N, 0x0) === zero(N)
138+
end
140139
@test convert(UInt, 1N1f7) === UInt(1)
141-
@test convert(Integer, 1N1f7) === 0x01
142-
@test convert(Int, 1N1f7) === 1
143140
@test_throws InexactError convert(Integer, 0.5N1f7)
144141
@test_throws InexactError convert(Int8, 256N8f8)
145142
end
146143

147144
@testset "rational conversions" begin
145+
@testset "$N to/from rational" for N in target(Normed)
146+
@test convert(Rational, oneunit(N)) == 1//1
147+
@test convert(Rational{Int}, zero(N)) === 0//1
148+
@test convert(N, 1//1) === oneunit(N)
149+
end
150+
@test convert(N0f8, 1//255) === eps(N0f8)
151+
@test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
152+
@test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
153+
@test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8}))
154+
148155
@test convert(Rational, 0.5N0f8) === Rational{UInt8}(0x80//0xff)
149156
@test convert(Rational, 0.5N4f12) === Rational{UInt16}(0x800//0xfff)
150157
@test convert(Rational{Int}, 0.5N0f8) === Rational{Int}(0x80//0xff)
@@ -157,6 +164,7 @@ end
157164

158165
@testset "BigFloat conversions" begin
159166
@test convert(BigFloat, 0.5N0f8)::BigFloat == 128 / big"255"
167+
@test convert(BigFloat, eps(N0f8))::BigFloat == 1 / big"255"
160168

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

0 commit comments

Comments
 (0)