Skip to content

Commit

Permalink
Merge pull request #12318 from JuliaLang/ksh/floattests
Browse files Browse the repository at this point in the history
More tests for float functions and some small tests elsewhere
  • Loading branch information
kshyatt committed Jul 27, 2015
2 parents f2df2a0 + 5b771d5 commit a3f0605
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function choosetests(choices = [])
"nullable", "meta", "profile", "libgit2", "docs", "markdown",
"base64", "serialize", "functors", "misc",
"enums", "cmdlineargs", "i18n", "workspace", "libdl", "int",
"intset"
"intset","floatfuncs"
]

if Base.USE_GPL_LIBS
Expand Down
54 changes: 54 additions & 0 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

using Base.Test

# test the basic floating point functions

# flipsign

for elty in (Float32,Float64)
x = convert(elty,-2.0)
x = flipsign(x,-1.0)
@test flipsign(x,-1.0) == convert(elty,-2.0)
end

#maxintfloat

@test maxintfloat(Float16) == Float16(2048f0)
for elty in (Float16,Float32,Float64)
@test maxintfloat(rand(elty)) == maxintfloat(elty)
end
@test maxintfloat() == maxintfloat(Float64)

#num2hex
for elty in (Float16,Float32,Float64)
x = rand(elty)
@test_approx_eq hex2num(num2hex(x)) x
end

#round
for elty in (Float32,Float64)
x = rand(elty)
A = fill(x,(10,10))
@test round(A,RoundToZero) == fill(trunc(x),(10,10))
@test round(A,RoundUp) == fill(ceil(x),(10,10))
@test round(A,RoundDown) == fill(floor(x),(10,10))
A = fill(x,(10,10,10))
@test round(A,RoundToZero) == fill(trunc(x),(10,10,10))
@test round(A,RoundUp) == fill(ceil(x),(10,10,10))
@test round(A,RoundDown) == fill(floor(x),(10,10,10))
for elty2 in (Int32,Int64)
A = fill(x,(10,))
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,))
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,))
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,))
A = fill(x,(10,10))
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10))
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10))
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10))
A = fill(x,(10,10,10))
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10,10))
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10,10))
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10,10))
end
end
3 changes: 3 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@test reduce((x,y)->"($x+$y)", 9:11) == "((9+10)+11)"
@test reduce(max, [8 6 7 5 3 0 9]) == 9
@test reduce(+, 1000, 1:5) == (1000 + 1 + 2 + 3 + 4 + 5)
@test reduce(+,1) == 1

@test mapreduce(-, +, [-10 -9 -3]) == ((10 + 9) + 3)
@test mapreduce((x)->x[1:3], (x,y)->"($x+$y)", ["abcd", "efgh", "01234"]) == "((abc+efg)+012)"
Expand Down Expand Up @@ -117,6 +118,8 @@ end
@test prod(1:big(16)) == big(20922789888000)
@test prod(big(typemax(Int64)):big(typemax(Int64))+16) == parse(BigInt,"25300281663413827620486300433089141956148633919452440329174083959168114253708467653081909888307573358090001734956158476311046124934597861626299416732205795533726326734482449215730132757595422510465791525610410023802664753402501982524443370512346073948799084936298007821432734720004795146875180123558814648586972474376192000")

@test_throws ErrorException prod(bitunpack(trues(10)))

# check type-stability
prod2(itr) = invoke(prod, Tuple{Any}, itr)
@test prod(Int[]) === prod2(Int[]) === 1
Expand Down
7 changes: 7 additions & 0 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,10 @@ for T in [Float32,Float64]
@test isinf(pu) || pu - pd == eps(pz)
end
end

#fenv
@test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundNearest)) == RoundNearest
@test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundToZero)) == RoundToZero
@test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundUp)) == RoundUp
@test Base.Rounding.from_fenv(Base.Rounding.to_fenv(RoundDown)) == RoundDown
@test_throws ArgumentError Base.Rounding.from_fenv(-99)
3 changes: 2 additions & 1 deletion test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ end
@test isapprox(.1+.1+.1, .3)
@test !isapprox(.1+.1+.1, .4)

@test_throws ErrorException Test.test_approx_eq(ones(10),ones(11),1e-8,"a","b")
@test_throws ErrorException Test.test_approx_eq(ones(10),zeros(10),1e-8,"a","b")

# Test @test_approx_eq_eps
# TODO

0 comments on commit a3f0605

Please sign in to comment.