Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Mar 5, 2025
1 parent ec873b3 commit dd57fbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions test/mathfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ for F in (:asec, :acsc, :cosh, :acosh, :acoth)
end
end


x,y = rand(Float32, 2)
@test widemul(BFloat16(x), BFloat16(y)) isa Float32
42 changes: 39 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ using Test, BFloat16s, Printf, Random

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

@testset "basics" begin
@test Base.exponent_bits(BFloat16) == 8
@test Base.significand_bits(BFloat16) == 7
@test precision(BFloat16) == 8

@test typemin(BFloat16) == -BFloat16s.InfB16
@test typemax(BFloat16) == BFloat16s.InfB16
end

@testset "comparisons" begin
@test BFloat16(1) < BFloat16(2)
@test BFloat16(1f0) < BFloat16(2f0)
Expand All @@ -18,17 +27,35 @@ using Test, BFloat16s, Printf, Random
@test BFloat16(2) != BFloat16(1)
@test BFloat16(2f0) != BFloat16(1f0)
@test BFloat16(2.0) != BFloat16(1.0)
@test BFloat16(NaN) != BFloat16(1.0)
@test iszero(BFloat16(0)) == true
@test iszero(BFloat16(3.45)) == false
end

@testset "trunc" begin
bf_val = 5.5
@testset "$Ti" for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
@test trunc(Ti, bf_val) == Ti(5)
end

@test trunc(BFloat16, Float32(π)) == BFloat16(3.140625)

#InexactError
@test_throws InexactError trunc(Int8,BFloat16(500))
end

@testset "conversions" begin
@test Float32(BFloat16(10)) == 1f1
@test Float64(BFloat16(10)) == 10.0
@test Int32(BFloat16(10)) == Int32(10)
@test Int64(BFloat16(10)) == Int64(10)
@test BFloat16(BigFloat(1)) == BFloat16(1)
@test BigFloat(BFloat16(1)) == BigFloat(1)
@test Float16(BFloat16(3.140625)) == Float16(π)
@test BFloat16(Float16(π)) == BFloat16(3.140625)

@test_throws InexactError Int8(BFloat16(500))
@test_throws InexactError UInt8(BFloat16(500))
end

@testset "abi" begin
Expand All @@ -44,6 +71,8 @@ end
@test BFloat16(2) ^ BFloat16(4) == BFloat16(16)
@test eps(BFloat16) == BFloat16(0.0078125)
@test sqrt(BFloat16(4f0)) == BFloat16(2f0)
@test rem(BFloat16(3.14), Int) == 3
@test round(BFloat16(10.4), RoundToZero) == BFloat16(10.0)
@test round(BFloat16(10.4), RoundUp) == BFloat16(11.0)
@test round(BFloat16(10.6), RoundDown) == BFloat16(10.0)
@test round(BFloat16(3.2), RoundNearest) == BFloat16(3.0)
Expand Down Expand Up @@ -92,6 +121,13 @@ end
@test (@sprintf "%a" BFloat16(1.5)) == "0x1.8p+0"
end

@testset "show" begin
@test repr(BFloat16(Inf)) == "InfB16"
@test repr(BFloat16(-Inf)) == "-InfB16"
@test repr(BFloat16(NaN)) == "NaNB16"
@test repr(BFloat16(2)) == "BFloat16(2.0)"
end

@testset "random" begin
x = Array{BFloat16}(undef, 10)
y = Array{BFloat16}(undef, 10)
Expand Down Expand Up @@ -171,7 +207,7 @@ end
end

@testset "maxintfloat" begin

a = maxintfloat(BFloat16)
@test a+1-1 == a-1 # the first +1 cannot be represented
@test a-1+1 == a # but -1 can
Expand All @@ -180,7 +216,7 @@ end
@testset "rand sampling" begin
Random.seed!(123)
mi, ma = extrema(rand(BFloat16, 1_000_000))

# zero should be the lowest BFloat16 sampled
@test mi === zero(BFloat16)

Expand All @@ -189,4 +225,4 @@ end
end

include("structure.jl")
include("mathfuncs.jl")
include("mathfuncs.jl")
12 changes: 5 additions & 7 deletions test/structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ uint(x::BFloat16) = reinterpret(UInt16, x)
@testset "BFloat16 bits" begin
@test uint(two) == 0x4000
@test uint(half) == 0x3f00
@test bitstring(two) == "0100000000000000"
@test bitstring(half) == "0011111100000000"
@test signbit(two) == false
end

@testset "BFloat16 parts" begin
@test exponent(whole) == 0
@test significand(whole) == one(BFloat16)

@test frexp(phi) == (BFloat16(0.80859375), 1)
@test ldexp(BFloat16(0.80859375), 1) == phi

@test exponent(invphi3) == -3
@test significand(invphi3) == BFloat16(1.8828125)

fr,xp = frexp(invphi3)
@test xp == -2
@test fr == BFloat16(0.94140625)
@test ldexp(fr, xp) == invphi3
end




0 comments on commit dd57fbd

Please sign in to comment.