Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #92

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# BFloat16s.jl
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[codecov-img]][codecov-url]

[codecov-img]: https://codecov.io/gh/JuliaMath/BFloat16s.jl/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/JuliaMath/BFloat16s.jl

This package defines the [BFloat16 data type](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format),
a floating-point format with 1 sign bit, 8 exponent bits and 7 mantissa bits.
Expand Down
25 changes: 25 additions & 0 deletions test/lowprecarrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@testset "lowprecarrays" begin
A = rand(Float32, 10,10)
lpA = LowPrecArray(A)

@test lpA isa LowPrecArray
@test size(A) == size(lpA)

lpA[3] = 4
@test lpA[3] == A[3]

a = rand(Float32)
ab = BFloat16s.ExpandingBFloat16(a)
b = rand(Float32)
bb = BFloat16s.ExpandingBFloat16(b)

@test ab isa BFloat16s.ExpandingBFloat16
@test ab.a isa BFloat16
@test ab * bb isa Float32

B = LowPrecArray(rand(Float32, 10,10))
C = LowPrecArray(rand(Float32, 10,10))

lpA .= B*C

end # @testset "lowprecarrays"
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
62 changes: 59 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ using Test, BFloat16s, Printf, Random

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

@testset "BFloat16s" begin

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

@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 +30,46 @@ 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 !(BFloat16(1.0) == BFloat16(NaN))
@test iszero(BFloat16(0)) == true
@test iszero(BFloat16(3.45)) == false
end

@testset "trunc" begin
bf_val = BFloat16(5.5)
@testset "$Ti" for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
if !(BFloat16s.llvm_arithmetic && sizeof(Ti) == 16) # Can't use skip argument in @test on 1.6
@test trunc(Ti, bf_val) == Ti(5)
else
@test_skip trunc(Ti, bf_val) == Ti(5)
end
end

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

#InexactError
@test_throws InexactError trunc(Int16, typemax(BFloat16))
@test_throws InexactError trunc(UInt16, typemax(BFloat16))
end

@testset "conversions" begin
@test Float32(BFloat16(10)) == 1f1
@test Float64(BFloat16(10)) == 10.0
@test Int32(BFloat16(10)) == Int32(10)
@test UInt32(BFloat16(10)) == Int32(10)
@test Int64(BFloat16(10)) == Int64(10)
@test UInt64(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 promote(BFloat16(4.5), Float64(5.0)) == (Float64(4.5), Float64(5.0))
@test promote(BFloat16(4.5), Float32(5.0)) == (Float32(4.5), Float32(5.0))

@test_throws InexactError Int16(typemax(BFloat16))
@test_throws InexactError UInt16(typemax(BFloat16))
end

@testset "abi" begin
Expand All @@ -44,6 +85,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 +135,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 @@ -134,6 +184,9 @@ end

@test x < nextfloat(x)
@test x > prevfloat(x)

@test nextfloat(x, typemax(Int)) == typemax(BFloat16)
@test prevfloat(x, typemax(Int)) == typemin(BFloat16)
end

@test isnan(nextfloat(BFloat16s.NaNB16))
Expand Down Expand Up @@ -171,7 +224,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 +233,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 +242,7 @@ end
end

include("structure.jl")
include("mathfuncs.jl")
include("mathfuncs.jl")
include("lowprecarrays.jl")

end # @testset "BFloat16s"
15 changes: 8 additions & 7 deletions test/structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ 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)


# subnormal
@test significand(reinterpret(BFloat16, 0b0000000000000001)) == 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




Loading