[MetaRuby] Test::Unit testsuite for Float
pat eyler
pat.eyler at gmail.com
Fri Oct 28 18:59:24 PDT 2005
Here's my first whack at cleaning up tests for metaruby. I grabbed
the rubytests TestFloat.rb and converted it to use Test::Unit, added
some assertions to existing tests (to cover things that seemed
missing), and added new tests to cover untested methods. I didn't
know what to do about singleton_add_method, anyone want to beat me
with a clue stick?
Anyway, here's the code for review.
require 'test/unit'
class TestFloat < Test::Unit::TestCase
def assert_flequal(exp, actual, msg=nil)
assert_in_delta(exp, actual, exp == 0.0 ? 1e-7 : exp.abs/1e7, msg)
end
BIG = 3.1415926e43
SML = 3.18309891613572e-44 # = 1/BIG
###########################################################################
def test_UMINUS
[ -99.0, -1.0, 0.0, 0.5, +1.0 , +99.0].each { |n|
assert_flequal(0.0, -n + n) }
end
def test_CMP # '<=>'
assert_flequal(0, BIG <=> BIG)
assert_flequal(0, SML <=> SML)
assert_flequal(0, 1.0 <=> 1.0)
assert_flequal(1, BIG <=> SML)
assert_flequal(1, BIG <=> 0.0)
assert_flequal(1, BIG <=> 1.0)
assert_flequal(1, SML <=> 0.0)
assert_flequal(-1, SML <=> BIG)
assert_flequal(-1, SML <=> 0.1)
assert_flequal(-1, SML <=> 1)
# and negatives:
assert_flequal(1, 1.0 <=> -1.0)
assert_flequal(-1, -1.0 <=> 1.0)
end
def test_DIV # '/'
assert_flequal(0.0, 0.0/3.0)
assert_flequal(1.0, 3.0/3.0)
assert_flequal(1.5, 4.5/3.0)
assert_flequal(0.333333333, 1.0/3.0)
assert_flequal(0.0, -0.0/3.0)
assert_flequal(-1.0, -3.0/3.0)
assert_flequal(-1.5, -4.5/3.0)
assert_flequal(-0.333333333, -1.0/3.0)
assert_flequal(0.0, 0.0/-3.0)
assert_flequal(-1.0, 3.0/-3.0)
assert_flequal(-1.5, 4.5/-3.0)
assert_flequal(-0.333333333, 1.0/-3.0)
assert_flequal(0.0, -0.0/-3.0)
assert_flequal(1.0, -3.0/-3.0)
assert_flequal(1.5, -4.5/-3.0)
assert_flequal(0.333333333, -1.0/-3.0)
assert_flequal(0.333333333, -1.0/-3)
end
def test_div
assert_equal(0, 0.0.div(3.0))
assert_equal(1, 3.0.div(3.0))
assert_equal(1, 4.5.div(3.0))
assert_equal(0, 1.0.div(3.0))
assert_equal(0, -0.0.div(3.0))
assert_equal(-1, -3.0.div(3.0))
assert_equal(-1, -4.5.div(3.0))
assert_equal(-0, -1.0.div(3.0))
assert_equal(0, 0.0.div(-3.0))
assert_equal(-1, 3.0.div(-3.0))
assert_equal(-1, 4.5.div(-3.0))
assert_equal(0, 1.0.div(-3.0))
assert_equal(0, -0.0.div(-3.0))
assert_equal(1, -3.0.div(-3.0))
assert_equal(1, -4.5.div(-3.0))
assert_equal(0, -1.0.div(-3.0))
assert_equal(0, -1.0.div(-3))
end
def test_quo
assert_flequal(0.0, 0.0.quo(3.0))
assert_flequal(1.0, 3.0.quo(3.0))
assert_flequal(1.5, 4.5.quo(3.0))
assert_flequal(0.333333333, 1.0.quo(3.0))
assert_flequal(0.0, -0.0.quo(3.0))
assert_flequal(-1.0, -3.0.quo(3.0))
assert_flequal(-1.5, -4.5.quo(3.0))
assert_flequal(-0.33333333, -1.0.quo(3.0))
assert_flequal(0.0, 0.0.quo(-3.0))
assert_flequal(-1.0, 3.0.quo(-3.0))
assert_flequal(-1.5, 4.5.quo(-3.0))
assert_flequal(-0.33333333, 1.0.quo(-3.0))
assert_flequal(0.0, -0.0.quo(-3.0))
assert_flequal(1.0, -3.0.quo(-3.0))
assert_flequal(1.5, -4.5.quo(-3.0))
assert_flequal(0.33333333, -1.0.quo(-3.0))
assert_flequal(0.33333333, -1.0.quo(-3))
end
def test_nan?
a = 1.0/0.0
b = 0.0/0.0
assert_equal(false, a.nan?)
assert_equal(true, b.nan?)
assert_equal(false, 99.0.nan?)
end
def test_nonzero?
assert(1.0.nonzero?)
assert(SML.nonzero?)
assert(BIG.nonzero?)
assert(-1.0.nonzero?)
assert( ! 0.0.nonzero?)
end
def test_infinite?
a = 1.0/0.0
b = -1.0/0.0
assert_equal(1, a.infinite?)
assert_equal(-1, b.infinite?)
assert_nil(99.0.infinite?)
end
def test_finite?
a = 1.0/0.0
b = 0.0/0.0
assert_equal(false, a.finite?)
assert_equal(false, b.finite?)
assert_equal(true, 99.0.finite?)
end
def test_EQUAL # '=='
assert(0.0 == 0.0)
assert(BIG == BIG)
assert(SML == SML)
assert(-1.0 == -1.0)
assert(!(0.0 == 1e-6))
end
def test_GE # '>='
assert(0.0 >= 0.0)
assert(1.0 >= 0.0)
assert(BIG >= 0.0)
assert(BIG >= BIG)
assert(SML >= 0.0)
assert(SML >= SML)
assert(0.0 >= -1.0)
assert(1.0 >= -1.0)
assert(-1.0 >= -2.0)
assert_equal(false, (0.0 >= 1.0))
assert_equal(false, (0.0 >= BIG))
assert_equal(false, (SML >= BIG))
end
def test_GT # '>'
assert(1.0 > 0.0)
assert(BIG > 0.0)
assert(SML > 0.0)
assert(0.0 > -1.0)
assert(1.0 > -1.0)
assert(-1.0 > -2.0)
assert(!(0.0 > 0.0))
assert(!(0.0 > 1.0))
assert(!(0.0 > BIG))
assert(!(SML > BIG))
end
def test_LE # '<='
assert(0.0 <= 0.0)
assert(0.0 <= 1.0)
assert(0.0 <= BIG)
assert(BIG <= BIG)
assert(0.0 <= SML)
assert(SML <= SML)
assert(-1.0 <= 0.0)
assert(-2.0 <= -1.0)
assert(!(1.0 <= 0.0))
assert(!(BIG <= 0.0))
assert(!(SML <= 0.0))
end
def test_LT # '<'
assert(0.0 < 1.0)
assert(0.0 < BIG)
assert(0.0 < SML)
assert(-1.0 < 0.0)
assert(-2.0 < -1.0)
assert(!(0.0 < 0.0))
assert(!(1.0 < 0.0))
assert(!(BIG < 0.0))
assert(!(SML < 0.0))
end
def test_MINUS # '-'
assert_flequal(0.0, 0.0-0.0)
assert_flequal(0.0, 1.0-1.0)
assert_flequal(0.0, -1.0 - (-1.0))
assert_flequal(-2.0, -1.0 - (1.0))
assert_flequal(2.0, 1.0 - (-1.0))
assert_flequal(77, 100.0 - 20.0 - 3.0)
end
def test_UMINUS # -@
assert_flequal((0.0 - 1.0), -1.0)
assert_flequal(0,0, -0.0)
assert_flequal((0.0 - (-1.0)), 1.0)
assert_flequal((0.0 - SML), -SML)
assert_flequal((0.0 - BIG), -BIG)
end
def test_MOD # '%'
assert_flequal(0.0, 0.0%123.0)
assert_flequal(1.0, 13.0%4.0)
assert_flequal(-3.0, 13.0%(-4.0))
assert_flequal(3.0, (-13.0)%4.0)
assert_flequal(-1.0, (-13.0)%(-4.0))
assert_flequal(1.5, 13.5%4.0)
assert_flequal(-2.5, 13.5%(-4.0))
assert_flequal(2.5, (-13.5)%4.0)
assert_flequal(-1.5, (-13.5)%(-4.0))
assert_flequal(0.4, 13.4 % 1)
end
def test_modulo
assert_flequal(1.0, 13.0.modulo(3.0))
assert_flequal(2.5, (-13.5).modulo(4.0))
assert_flequal(-1.5, (-13.5).modulo(-4.0))
end
def test_MUL # '*'
assert_flequal(0.0, 0.0*BIG)
assert_flequal(BIG, 1.0*BIG)
assert_flequal(SML, 1.0*SML)
assert_flequal(1.0, BIG*SML)
assert_flequal(-1.0, -1.0 * 1.0)
assert_flequal( 1.0, -1.0 * -1.0)
end
def test_PLUS # '+'
assert_flequal(0.0, 0.0 + 0.0)
assert_flequal(1.0, 0.0 + 1.0)
assert_flequal(1.0, 1.0 + 0.0)
assert_flequal(0.0, 1.0 + (-1.0))
assert_flequal(-1.0, 0.0 + (-1.0))
assert_flequal(-2.0, -1.0 + (-1.0))
assert_flequal(0.0, -1.0 + (1.0))
assert_flequal(3.0, 0.0 + 3)
end
def test_UPLUS # +@
assert_flequal(1.0, +1.0)
assert_flequal(0.0, +0.0)
assert_flequal(-1.0, +(-1.0))
assert_flequal(SML, +SML)
assert_flequal(BIG, +BIG)
end
def test_POW # '**'
assert_flequal(1e40, 10**40)
assert_flequal(1e-40, 10**-40)
assert_not_nil((10.0**10.0**10.0).infinite?)
assert_flequal(0.0, 10.0**-(1000.0))
assert_flequal(1.4142135623731, 2.0**0.5)
end
def test_abs
assert_flequal(1.0, 1.0.abs)
assert_flequal(1.0, (-1.0).abs)
end
def test_between?
assert(3.0.between?(1.0,5.0))
assert(-3.0.between?(-5.0,-1.0))
assert( ! 3.0.between?(5.0,1.0))
end
def test_ceil
assert_flequal(2.0, (1.2).ceil)
assert_flequal(2.0, (2.0).ceil)
assert_flequal(-1.0, (-1.2).ceil)
assert_flequal(-2.0, (-2.0).ceil)
assert_flequal(3, 2.6.ceil)
assert_flequal(-2, -2.6.ceil)
end
def test_divmod
vals = {
[ 13.0, 4.0 ] => [3.0, 1.0],
[ 13.0, -4.0 ] => [-4.0, -3.0],
[ -13.0, 4.0 ] => [-4.0, 3.0],
[ -13.0, -4.0 ] => [3.0, -1.0],
}
vals.each { |ip, op|
res = ip[0].divmod(ip[1])
assert_flequal(op[0], res[0], "#{ip.join('.divmod ')}")
assert_flequal(op[1], res[1], "#{ip.join('.divmod ')}")
}
end
def test_floor
assert_flequal(1.0, (1.2).floor)
assert_flequal(2.0, (2.0).floor)
assert_flequal(-2.0, (-1.2).floor)
assert_flequal(2, 2.6.floor)
assert_flequal(-3, -2.6.floor)
end
def test_prec
assert_flequal(2.0, 2.prec(Float))
assert_instance_of(Float, 2.prec(Float))
assert_equal(2, 2.0.prec(Fixnum))
assert_instance_of(Fixnum, 2.prec(Bignum))
assert_instance_of(Bignum, BIG.prec(Bignum))
assert_flequal(BIG, BIG.prec(Bignum).prec(Float))
end
def test_prec_f
assert_instance_of(Float, 2.prec_f)
assert_flequal(2.0, 2.prec_f)
end
def test_prec_i
assert_instance_of(Fixnum, SML.prec_i)
assert_instance_of(Bignum, BIG.prec_i)
end
def test_remainder
assert_flequal(0.0, 0.0.remainder(123.0))
assert_flequal(1.0, 13.0.remainder(4.0))
assert_flequal(1.0, 13.0.remainder(-4.0))
assert_flequal(-1.0, (-13.0).remainder(4.0))
assert_flequal(-1.0, (-13.0).remainder(-4.0))
assert_flequal(1.5, 13.5.remainder(4.0))
assert_flequal(1.5, 13.5.remainder(-4.0))
assert_flequal(-1.5, (-13.5).remainder(4.0))
assert_flequal(-1.5, (-13.5).remainder(-4.0))
end
def test_round
assert_instance_of(Fixnum, 1.5.round)
assert_instance_of(Bignum, 1.5e20.round)
assert_equal(0, 0.0.round)
assert_equal(1, 0.5.round)
assert_equal(1, 1.0.round)
assert_equal(1, 1.49.round)
assert_equal(2, 1.5.round)
assert_equal(-1, -1.49.round)
assert_equal(-2, -1.5.round)
assert_equal(3, 2.6.round)
end
def test_singleton_method_added
raise NotImplementedError, "Need to implement test_singleton_method_added"
end
def test_step
hold = 0.0
1.0.step(5.0, 1.0) {|n|
assert_flequal(hold, n - 1.0)
hold = n
}
end
def test_to_f
a = BIG.to_f
assert_equal(a, BIG)
assert_equal(a.__id__, BIG.__id__)
end
def test_to_i
assert_instance_of(Fixnum, 1.23.to_i)
assert_instance_of(Bignum, BIG.to_i)
assert_equal(1, 1.4.to_i)
assert_equal(1, 1.9.to_i)
assert_equal(-1, -1.4.to_i)
assert_equal(-1, -1.9.to_i)
end
def test_to_int
assert_instance_of(Fixnum, 1.23.to_int)
assert_instance_of(Bignum, BIG.to_int)
assert_equal(1, 1.4.to_int)
assert_equal(1, 1.9.to_int)
assert_equal(-1, -1.4.to_int)
assert_equal(-1, -1.9.to_int)
end
def test_to_s
assert_equal("Infinity", (1.0/0.0).to_s)
assert_equal("-Infinity", (-1.0/0.0).to_s)
assert_equal("NaN", (0.0/0.0).to_s)
assert_equal("1.23456", 1.23456.to_s)
assert_equal("-1.23456", -1.23456.to_s)
assert_flequal(1.23e+45, Float(1.23e+45.to_s))
assert_flequal(1.23e-45, Float(1.23e-45.to_s))
end
def test_truncate
assert_equal(2, 2.6.truncate)
assert_equal(-2, -2.6.truncate)
assert_equal(-2, -2.4.truncate)
end
def test_zero?
assert(0.0.zero?)
assert(!0.1.zero?)
end
def test_s_induced_from
assert_flequal(1.0, Float.induced_from(1))
assert_flequal(1.0, Float.induced_from(1.0))
end
end
--
thanks,
-pate
-------------------------
More information about the MetaRuby
mailing list