Exhaustive tests for struct.c, sprintf.c and math.c

e$B1sF#$H?=$7$^$9!#e(B

test_struct.rb e$B$He(B test_sprintf.rb e$B$He(B test_math.rb
e$B$K$b%F%9%H$rDI2Ce(B
e$B$7$^$7$?!#e(Bgcov e$B$G$N%+%P%l%C%8$,0J2<$N$h$&$K>e$,$j$^$9!#e(B

struct.c : 55.87% -> 94.60%
sprintf.c : 70.45% -> 97.50%
math.c : 28.36% -> 98.51%

e$B$3$l$b%3%_%C%H$7$F$h$m$7$$$G$7$g$&$+!#$h$1$l$P$=$N$&$AB>$G$b$d$m$&$He(B
e$B;W$$$^$9!#e(B

Index: test/ruby/test_struct.rb

— test/ruby/test_struct.rb (revision 15173)
+++ test/ruby/test_struct.rb (working copy)
@@ -49,4 +49,162 @@
}
}
end
+

  • def test_inherit

  • klass = Struct.new(:a)

  • klass2 = Class.new(klass)

  • o = klass2.new(1)

  • assert_equal(1, o.a)

  • end

  • def test_members

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal([:a], klass.members)

  • assert_equal([:a], o.members)

  • end

  • def test_ref

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal(1, o[:a])

  • assert_raise(NameError) { o[:b] }

  • end

  • def test_modify

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_raise(SecurityError) do

  •  Thread.new do
    
  •    $SAFE = 4
    
  •    o.a = 2
    
  •  end.value
    
  • end

  • end

  • def test_set

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • o[:a] = 2

  • assert_equal(2, o[:a])

  • assert_raise(NameError) { o[:b] = 3 }

  • end

  • def test_struct_new

  • Struct.instance_eval { const_set(:Foo, nil) }

  • assert_raise(NameError) { Struct.new(“foo”) }

  • assert_nothing_raised { Struct.new(“Foo”) }

  • Struct.instance_eval { remove_const(:Foo) }

  • assert_nothing_raised { Struct.new(:a) { } }

  • assert_raise(RuntimeError) { Struct.new(:a) { raise } }

  • assert_equal([:utime, :stime, :cutime, :cstime],
    Process.times.members)

  • end

  • def test_initialize

  • klass = Struct.new(:a)

  • assert_raise(ArgumentError) { klass.new(1, 2) }

  • end

  • def test_each

  • klass = Struct.new(:a, :b)

  • o = klass.new(1, 2)

  • assert_equal([1, 2], o.each.to_a)

  • end

  • def test_each_pair

  • klass = Struct.new(:a, :b)

  • o = klass.new(1, 2)

  • assert_equal([[:a, 1], [:b, 2]], o.each_pair.to_a)

  • end

  • def test_inspect

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal("#", o.inspect)

  • o.a = o

  • assert(o.inspect =~ /^#<struct a=#<struct #<.*?>:…>>$/)

  • Struct.new(“Foo”, :a)

  • o = Struct::Foo.new(1)

  • assert_equal("#", o.inspect)

  • Struct.instance_eval { remove_const(:Foo) }

  • klass = Struct.new(:a, :b)

  • o = klass.new(1, 2)

  • assert_equal("#", o.inspect)

  • klass = Struct.new(:@a)

  • o = klass.new(1)

  • assert_equal("#<struct :@a=1>", o.inspect)

  • end

  • def test_init_copy

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal(o, o.dup)

  • end

  • def test_aref

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal(1, o[0])

  • assert_raise(IndexError) { o[-2] }

  • assert_raise(IndexError) { o[1] }

  • end

  • def test_aset

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • o[0] = 2

  • assert_equal(2, o[:a])

  • assert_raise(IndexError) { o[-2] = 3 }

  • assert_raise(IndexError) { o[1] = 3 }

  • end

  • def test_values_at

  • klass = Struct.new(:a, :b, :c, :d, :e, :f)

  • o = klass.new(1, 2, 3, 4, 5, 6)

  • assert_equal([2, 4, 6], o.values_at(1, 3, 5))

  • assert_equal([2, 3, 4, 3, 4, 5], o.values_at(1…3, 2…5))

  • end

  • def test_select

  • klass = Struct.new(:a, :b, :c, :d, :e, :f)

  • o = klass.new(1, 2, 3, 4, 5, 6)

  • assert_equal([1, 3, 5], o.select {|v| v % 2 != 0 })

  • assert_raise(ArgumentError) { o.select(1) }

  • end

  • def test_equal

  • klass1 = Struct.new(:a)

  • klass2 = Struct.new(:a, :b)

  • o1 = klass1.new(1)

  • o2 = klass1.new(1)

  • o3 = klass2.new(1)

  • assert(o1.==(o2))

  • assert(o1 != o3)

  • end

  • def test_hash

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert(o.hash.is_a?(Fixnum))

  • end

  • def test_eql

  • klass1 = Struct.new(:a)

  • klass2 = Struct.new(:a, :b)

  • o1 = klass1.new(1)

  • o2 = klass1.new(1)

  • o3 = klass2.new(1)

  • assert(o1.eql?(o2))

  • assert(!(o1.eql?(o3)))

  • end

  • def test_size

  • klass = Struct.new(:a)

  • o = klass.new(1)

  • assert_equal(1, o.size)

  • end
    end
    Index: test/ruby/test_sprintf.rb
    ===================================================================
    — test/ruby/test_sprintf.rb (revision 15173)
    +++ test/ruby/test_sprintf.rb (working copy)
    @@ -173,10 +173,88 @@
    assert_equal("%\0x hello", sprintf("%\0x hello"),
    “[ruby-core:11571]”)

    assert_raise(ArgumentError, “[ruby-core:11573]”)
    {sprintf("%.25555555555555555555555555555555555555s", “hello”)}

  • assert_raise(ArgumentError) { sprintf("%\1", 1) }

  • assert_raise(ArgumentError) { sprintf("%!", 1) }

  • assert_raise(ArgumentError) { sprintf("%1$1$d", 1) }

  • assert_raise(ArgumentError) { sprintf("%0%") }

  • assert_nothing_raised { sprintf("", 1) }
    end

def test_float
assert_equal(“36893488147419111424”,
sprintf("%20.0f", 36893488147419107329.0))
end
+

  • BSIZ = 120
  • def test_skip
  • assert_equal(" " * BSIZ + “1”, sprintf(" " * BSIZ + “%d”, 1))
  • end
  • def test_char
  • assert_equal(“a”, sprintf("%c", 97))
  • assert_equal(“a”, sprintf("%c", ?a))
  • assert_raise(ArgumentError) { sprintf("%c", sprintf("%c%c", ?a,
    ?a)) }
  • assert_equal(" " * (BSIZ - 1) + “a”, sprintf(" " * (BSIZ - 1) +
    “%c”, ?a))
  • assert_equal(" " * (BSIZ - 1) + “a”, sprintf(" " * (BSIZ - 1) +
    “%-1c”, ?a))
  • assert_equal(" " * BSIZ + “a”, sprintf("%#{ BSIZ + 1 }c", ?a))
  • assert_equal(“a” + " " * BSIZ, sprintf("%-#{ BSIZ + 1 }c", ?a))
  • end
  • def test_string
  • assert_equal(“foo”, sprintf("%s", “foo”))
  • assert_equal(“fo”, sprintf("%.2s", “foo”))
  • assert_equal(" " * BSIZ, sprintf("%s", " " * BSIZ))
  • assert_equal(" " * (BSIZ - 1) + “foo”, sprintf("%#{ BSIZ - 1 + 3
    }s", “foo”))
  • assert_equal(" " * BSIZ + “foo”, sprintf("%#{ BSIZ + 3 }s", “foo”))
  • assert_equal(“foo” + " " * BSIZ, sprintf("%-#{ BSIZ + 3 }s",
    “foo”))
  • end
  • def test_integer
  • assert_equal(“01”, sprintf("%#o", 1))
  • assert_equal(“0x1”, sprintf("%#x", 1))
  • assert_equal(“0X1”, sprintf("%#X", 1))
  • assert_equal(“0b1”, sprintf("%#b", 1))
  • assert_equal(“0B1”, sprintf("%#B", 1))
  • assert_equal(“1”, sprintf("%d", 1.0))
  • assert_equal(“4294967296”, sprintf("%d", (2**32).to_f))
  • assert_equal(“1”, sprintf("%d", “1”))
  • o = Object.new; def o.to_int; 1; end
  • assert_equal(“1”, sprintf("%d", o))
  • assert_equal("+1", sprintf("%+d", 1))
  • assert_equal(" 1", sprintf("% d", 1))
  • assert_equal("…f", sprintf("%x", -1))
  • assert_equal("…7", sprintf("%o", -1))
  • one = (2**32).coerce(1).first
  • mone = (2**32).coerce(-1).first
  • assert_equal("+1", sprintf("%+d", one))
  • assert_equal(" 1", sprintf("% d", one))
  • assert_equal("…f", sprintf("%x", mone))
  • assert_equal("…7", sprintf("%o", mone))
  • assert_equal(" " * BSIZ + “1”, sprintf("%#{ BSIZ + 1 }d", one))
  • assert_equal(" " * (BSIZ - 1) + “1”, sprintf(" " * (BSIZ - 1) +
    “%d”, 1))
  • end
  • def test_float2
  • inf = 1.0 / 0.0
  • assert_equal(" " * BSIZ + “Inf”, sprintf("%#{ BSIZ + 3 }.1f", inf))
  • assert_equal("+Inf", sprintf("%±f", inf))
  • assert_equal(" " * BSIZ + “1.0”, sprintf("%#{ BSIZ + 3 }.1f", 1.0))
  • end
  • class
    T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  • end
  • def test_star
  • assert_equal("-1 “, sprintf(”%*d", -3, -1))
  • end
  • def test_escape
  • assert_equal("%" * BSIZ, sprintf("%%" * BSIZ))
  • end
  • def test_rb_sprintf

assert(T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect
=~
/^#TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+$/)

  • end
    end
    Index: test/ruby/test_math.rb
    ===================================================================
    — test/ruby/test_math.rb (revision 15173)
    +++ test/ruby/test_math.rb (working copy)
    @@ -1,12 +1,173 @@
    require ‘test/unit’

class TestMath < Test::Unit::TestCase

  • def test_math
  • assert_equal(2, Math.sqrt(4))
  • def check(a, b)
  • assert(a - b < Float::EPSILON * 3)
  • end
  • self.class.class_eval {
  •  include Math
    
  • }
  • assert_equal(2, sqrt(4))
  • def test_atan2
  • check(Math.atan2(0, 1), 0)
  • check(Math.atan2(1, 1), Math::PI / 4)
  • check(Math.atan2(1, 0), Math::PI / 2)
    end
  • def test_cos
  • check(Math.cos(0 * Math::PI / 4), 1.0)
  • check(Math.cos(1 * Math::PI / 4), 1.0 / Math.sqrt(2))
  • check(Math.cos(2 * Math::PI / 4), 0.0)
  • check(Math.cos(4 * Math::PI / 4), -1.0)
  • check(Math.cos(6 * Math::PI / 4), 0.0)
  • end
  • def test_sin
  • check(Math.sin(0 * Math::PI / 4), 0.0)
  • check(Math.sin(1 * Math::PI / 4), 1.0 / Math.sqrt(2))
  • check(Math.sin(2 * Math::PI / 4), 1.0)
  • check(Math.sin(4 * Math::PI / 4), 0.0)
  • check(Math.sin(6 * Math::PI / 4), -1.0)
  • end
  • def test_tan
  • check(Math.tan(0 * Math::PI / 4), 0.0)
  • check(Math.tan(1 * Math::PI / 4), 1.0)
  • assert(Math.tan(2 * Math::PI / 4).abs > 1024)
  • check(Math.tan(4 * Math::PI / 4), 0.0)
  • assert(Math.tan(6 * Math::PI / 4).abs > 1024)
  • end
  • def test_acos
  • check(0 * Math::PI / 4, Math.acos( 1.0))
  • check(1 * Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
  • check(2 * Math::PI / 4, Math.acos( 0.0))
  • check(4 * Math::PI / 4, Math.acos(-1.0))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.acos(2.0) }
  • end
  • def test_asin
  • check( 0 * Math::PI / 4, Math.asin( 0.0))
  • check( 1 * Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
  • check( 2 * Math::PI / 4, Math.asin( 1.0))
  • check(-2 * Math::PI / 4, Math.asin(-1.0))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.asin(2.0) }
  • end
  • def test_atan
  • check( 0 * Math::PI / 4, Math.atan( 0.0))
  • check( 1 * Math::PI / 4, Math.atan( 1.0))
  • check( 2 * Math::PI / 4, Math.atan(1.0 / 0.0))
  • check(-1 * Math::PI / 4, Math.atan(-1.0))
  • end
  • def test_cosh
  • check(1, Math.cosh(0))
  • check((Math::E ** 1 + Math::E ** -1) / 2, Math.cosh(1))
  • check((Math::E ** 2 + Math::E ** -2) / 2, Math.cosh(2))
  • end
  • def test_sinh
  • check(0, Math.sinh(0))
  • check((Math::E ** 1 - Math::E ** -1) / 2, Math.sinh(1))
  • check((Math::E ** 2 - Math::E ** -2) / 2, Math.sinh(2))
  • end
  • def test_tanh
  • check(Math.sinh(0) / Math.cosh(0), Math.tanh(0))
  • check(Math.sinh(1) / Math.cosh(1), Math.tanh(1))
  • check(Math.sinh(2) / Math.cosh(2), Math.tanh(2))
  • end
  • def test_acosh
  • check(Math.acosh(1), 0)
  • check(Math.acosh((Math::E ** 1 + Math::E ** -1) / 2), 1)
  • check(Math.acosh((Math::E ** 2 + Math::E ** -2) / 2), 2)
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.acosh(0) }
  • end
  • def test_asinh
  • check(Math.asinh(0), 0)
  • check(Math.asinh((Math::E ** 1 - Math::E ** -1) / 2), 1)
  • check(Math.asinh((Math::E ** 2 - Math::E ** -2) / 2), 2)
  • end
  • def test_atanh
  • check(Math.atanh(Math.sinh(0) / Math.cosh(0)), 0)
  • check(Math.atanh(Math.sinh(1) / Math.cosh(1)), 1)
  • check(Math.atanh(Math.sinh(2) / Math.cosh(2)), 2)
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.atanh(-1) }
  • end
  • def test_exp
  • check(1, Math.exp(0))
  • check(Math.sqrt(Math::E), Math.exp(0.5))
  • check(Math::E, Math.exp(1))
  • check(Math::E ** 2, Math.exp(2))
  • end
  • def test_log
  • check(0, Math.log(1))
  • check(1, Math.log(Math::E))
  • check(0, Math.log(1, 10))
  • check(1, Math.log(10, 10))
  • check(2, Math.log(100, 10))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(0) }
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(-1) }
  • end
  • def test_log2
  • check(0, Math.log2(1))
  • check(1, Math.log2(2))
  • check(2, Math.log2(4))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(0) }
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(-1) }
  • end
  • def test_log10
  • check(0, Math.log10(1))
  • check(1, Math.log10(10))
  • check(2, Math.log10(100))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(0) }
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(-1) }
  • end
  • def test_sqrt
  • check(0, Math.sqrt(0))
  • check(1, Math.sqrt(1))
  • check(2, Math.sqrt(4))
  • assert_raise(Errno::EDOM, Errno::ERANGE) { Math.sqrt(-1) }
  • end
  • def test_frexp
  • check(0.0, Math.frexp(0.0).first)
  • assert_equal(0, Math.frexp(0).last)
  • check(0.5, Math.frexp(0.5).first)
  • assert_equal(0, Math.frexp(0.5).last)
  • check(0.5, Math.frexp(1.0).first)
  • assert_equal(1, Math.frexp(1.0).last)
  • check(0.5, Math.frexp(2.0).first)
  • assert_equal(2, Math.frexp(2.0).last)
  • check(0.75, Math.frexp(3.0).first)
  • assert_equal(2, Math.frexp(3.0).last)
  • end
  • def test_ldexp
  • check(0.0, Math.ldexp(0.0, 0.0))
  • check(0.5, Math.ldexp(0.5, 0.0))
  • check(1.0, Math.ldexp(0.5, 1.0))
  • check(2.0, Math.ldexp(0.5, 2.0))
  • check(3.0, Math.ldexp(0.75, 2.0))
  • end
  • def test_hypot
  • check(5, Math.hypot(3, 4))
  • end
  • def test_erf
  • check(0, Math.erf(0))
  • check(1, Math.erf(1.0 / 0.0))
  • end
  • def test_erfc
  • check(1, Math.erfc(0))
  • check(0, Math.erfc(1.0 / 0.0))
  • end
    end

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33280] exhaustive tests for struct.c,
sprintf.c and math.c”
on Wed, 23 Jan 2008 00:03:52 +0900, “Yusuke ENDOH” [email protected]
writes:

|test_struct.rb e$B$He(B test_sprintf.rb e$B$He(B test_math.rb e$B$K$b%F%9%H$rDI2Ce(B
|e$B$7$^$7$?!#e(Bgcov e$B$G$N%+%P%l%C%8$,0J2<$N$h$&$K>e$,$j$^$9!#e(B
|
|struct.c : 55.87% → 94.60%
|sprintf.c : 70.45% → 97.50%
|math.c : 28.36% → 98.51%
|
|e$B$3$l$b%3%_%C%H$7$F$h$m$7$$$G$7$g$&$+!#$h$1$l$P$=$N$&$AB>$G$b$d$m$&$He(B
|e$B;W$$$^$9!#e(B

e$B$=$&$G$9$M!#$=$NB>$N%F%9%H$b1sN8$J$/DI2C$7$F$/$@$5$$!#$?$@!"e(B
e$B%F%9%H;~4V$,0[MM$KD9$/$J$i$J$$DxEY$G:Q$`$H=u$+$j$^$9!#e(B