Skip to content

Commit dd57fbd

Browse files
committed
Improve test coverage
1 parent ec873b3 commit dd57fbd

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

test/mathfuncs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ for F in (:asec, :acsc, :cosh, :acosh, :acoth)
1919
end
2020
end
2121

22+
23+
x,y = rand(Float32, 2)
24+
@test widemul(BFloat16(x), BFloat16(y)) isa Float32

test/runtests.jl

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ using Test, BFloat16s, Printf, Random
22

33
@info "Testing BFloat16s" BFloat16s.llvm_storage BFloat16s.llvm_arithmetic
44

5+
@testset "basics" begin
6+
@test Base.exponent_bits(BFloat16) == 8
7+
@test Base.significand_bits(BFloat16) == 7
8+
@test precision(BFloat16) == 8
9+
10+
@test typemin(BFloat16) == -BFloat16s.InfB16
11+
@test typemax(BFloat16) == BFloat16s.InfB16
12+
end
13+
514
@testset "comparisons" begin
615
@test BFloat16(1) < BFloat16(2)
716
@test BFloat16(1f0) < BFloat16(2f0)
@@ -18,17 +27,35 @@ using Test, BFloat16s, Printf, Random
1827
@test BFloat16(2) != BFloat16(1)
1928
@test BFloat16(2f0) != BFloat16(1f0)
2029
@test BFloat16(2.0) != BFloat16(1.0)
30+
@test BFloat16(NaN) != BFloat16(1.0)
2131
@test iszero(BFloat16(0)) == true
2232
@test iszero(BFloat16(3.45)) == false
2333
end
2434

35+
@testset "trunc" begin
36+
bf_val = 5.5
37+
@testset "$Ti" for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
38+
@test trunc(Ti, bf_val) == Ti(5)
39+
end
40+
41+
@test trunc(BFloat16, Float32(π)) == BFloat16(3.140625)
42+
43+
#InexactError
44+
@test_throws InexactError trunc(Int8,BFloat16(500))
45+
end
46+
2547
@testset "conversions" begin
2648
@test Float32(BFloat16(10)) == 1f1
2749
@test Float64(BFloat16(10)) == 10.0
2850
@test Int32(BFloat16(10)) == Int32(10)
2951
@test Int64(BFloat16(10)) == Int64(10)
3052
@test BFloat16(BigFloat(1)) == BFloat16(1)
3153
@test BigFloat(BFloat16(1)) == BigFloat(1)
54+
@test Float16(BFloat16(3.140625)) == Float16(π)
55+
@test BFloat16(Float16(π)) == BFloat16(3.140625)
56+
57+
@test_throws InexactError Int8(BFloat16(500))
58+
@test_throws InexactError UInt8(BFloat16(500))
3259
end
3360

3461
@testset "abi" begin
@@ -44,6 +71,8 @@ end
4471
@test BFloat16(2) ^ BFloat16(4) == BFloat16(16)
4572
@test eps(BFloat16) == BFloat16(0.0078125)
4673
@test sqrt(BFloat16(4f0)) == BFloat16(2f0)
74+
@test rem(BFloat16(3.14), Int) == 3
75+
@test round(BFloat16(10.4), RoundToZero) == BFloat16(10.0)
4776
@test round(BFloat16(10.4), RoundUp) == BFloat16(11.0)
4877
@test round(BFloat16(10.6), RoundDown) == BFloat16(10.0)
4978
@test round(BFloat16(3.2), RoundNearest) == BFloat16(3.0)
@@ -92,6 +121,13 @@ end
92121
@test (@sprintf "%a" BFloat16(1.5)) == "0x1.8p+0"
93122
end
94123

124+
@testset "show" begin
125+
@test repr(BFloat16(Inf)) == "InfB16"
126+
@test repr(BFloat16(-Inf)) == "-InfB16"
127+
@test repr(BFloat16(NaN)) == "NaNB16"
128+
@test repr(BFloat16(2)) == "BFloat16(2.0)"
129+
end
130+
95131
@testset "random" begin
96132
x = Array{BFloat16}(undef, 10)
97133
y = Array{BFloat16}(undef, 10)
@@ -171,7 +207,7 @@ end
171207
end
172208

173209
@testset "maxintfloat" begin
174-
210+
175211
a = maxintfloat(BFloat16)
176212
@test a+1-1 == a-1 # the first +1 cannot be represented
177213
@test a-1+1 == a # but -1 can
@@ -180,7 +216,7 @@ end
180216
@testset "rand sampling" begin
181217
Random.seed!(123)
182218
mi, ma = extrema(rand(BFloat16, 1_000_000))
183-
219+
184220
# zero should be the lowest BFloat16 sampled
185221
@test mi === zero(BFloat16)
186222

@@ -189,4 +225,4 @@ end
189225
end
190226

191227
include("structure.jl")
192-
include("mathfuncs.jl")
228+
include("mathfuncs.jl")

test/structure.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,23 @@ uint(x::BFloat16) = reinterpret(UInt16, x)
1212
@testset "BFloat16 bits" begin
1313
@test uint(two) == 0x4000
1414
@test uint(half) == 0x3f00
15+
@test bitstring(two) == "0100000000000000"
16+
@test bitstring(half) == "0011111100000000"
1517
@test signbit(two) == false
1618
end
1719

1820
@testset "BFloat16 parts" begin
1921
@test exponent(whole) == 0
2022
@test significand(whole) == one(BFloat16)
21-
23+
2224
@test frexp(phi) == (BFloat16(0.80859375), 1)
2325
@test ldexp(BFloat16(0.80859375), 1) == phi
24-
26+
2527
@test exponent(invphi3) == -3
2628
@test significand(invphi3) == BFloat16(1.8828125)
27-
29+
2830
fr,xp = frexp(invphi3)
2931
@test xp == -2
3032
@test fr == BFloat16(0.94140625)
3133
@test ldexp(fr, xp) == invphi3
3234
end
33-
34-
35-
36-

0 commit comments

Comments
 (0)