diff options
Diffstat (limited to 'test/ruby/test_integer_comb.rb')
| -rw-r--r-- | test/ruby/test_integer_comb.rb | 118 |
1 files changed, 67 insertions, 51 deletions
diff --git a/test/ruby/test_integer_comb.rb b/test/ruby/test_integer_comb.rb index 9018518334..150f45cfd7 100644 --- a/test/ruby/test_integer_comb.rb +++ b/test/ruby/test_integer_comb.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require 'test/unit' class TestIntegerComb < Test::Unit::TestCase @@ -109,28 +110,6 @@ class TestIntegerComb < Test::Unit::TestCase #VS.concat VS.find_all {|v| Fixnum === v }.map {|v| 0x4000000000000000.coerce(v)[0] } #VS.sort! {|a, b| a.abs <=> b.abs } - min = -1 - min *= 2 while min.class == Fixnum - FIXNUM_MIN = min/2 - max = 1 - max *= 2 while (max-1).class == Fixnum - FIXNUM_MAX = max/2-1 - - def test_fixnum_range - assert_instance_of(Bignum, FIXNUM_MIN-1) - assert_instance_of(Fixnum, FIXNUM_MIN) - assert_instance_of(Fixnum, FIXNUM_MAX) - assert_instance_of(Bignum, FIXNUM_MAX+1) - end - - def check_class(n) - if FIXNUM_MIN <= n && n <= FIXNUM_MAX - assert_instance_of(Fixnum, n) - else - assert_instance_of(Bignum, n) - end - end - def test_aref VS.each {|a| 100.times {|i| @@ -141,7 +120,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|b| c = nil assert_nothing_raised("(#{a})[#{b}]") { c = a[b] } - check_class(c) + assert_kind_of(Integer, c) if b < 0 assert_equal(0, c, "(#{a})[#{b}]") else @@ -155,7 +134,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a + b - check_class(c) + assert_kind_of(Integer, c) assert_equal(b + a, c, "#{a} + #{b}") assert_equal(a, c - b, "(#{a} + #{b}) - #{b}") assert_equal(a-~b-1, c, "#{a} + #{b}") # Hacker's Delight @@ -170,7 +149,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a - b - check_class(c) + assert_kind_of(Integer, c) assert_equal(a, c + b, "(#{a} - #{b}) + #{b}") assert_equal(-b, c - a, "(#{a} - #{b}) - #{a}") assert_equal(a+~b+1, c, "#{a} - #{b}") # Hacker's Delight @@ -185,7 +164,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a * b - check_class(c) + assert_kind_of(Integer, c) assert_equal(b * a, c, "#{a} * #{b}") assert_equal(b.send(:*, a), c, "#{a} * #{b}") assert_equal(b, c / a, "(#{a} * #{b}) / #{a}") if a != 0 @@ -203,8 +182,8 @@ class TestIntegerComb < Test::Unit::TestCase assert_raise(ZeroDivisionError) { a.divmod(b) } else q, r = a.divmod(b) - check_class(q) - check_class(r) + assert_kind_of(Integer, q) + assert_kind_of(Integer, r) assert_equal(a, b*q+r) assert_operator(r.abs, :<, b.abs) if 0 < b @@ -228,7 +207,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| small_values.each {|b| c = a ** b - check_class(c) + assert_kind_of(Integer, c) d = 1 b.times { d *= a } assert_equal(d, c, "(#{a}) ** #{b}") @@ -244,7 +223,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_not VS.each {|a| b = ~a - check_class(b) + assert_kind_of(Integer, b) assert_equal(-1 ^ a, b, "~#{a}") assert_equal(-a-1, b, "~#{a}") # Hacker's Delight assert_equal(0, a & b, "#{a} & ~#{a}") @@ -256,7 +235,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a | b - check_class(c) + assert_kind_of(Integer, c) assert_equal(b | a, c, "#{a} | #{b}") assert_equal(a + b - (a&b), c, "#{a} | #{b}") assert_equal((a & ~b) + b, c, "#{a} | #{b}") # Hacker's Delight @@ -269,7 +248,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a & b - check_class(c) + assert_kind_of(Integer, c) assert_equal(b & a, c, "#{a} & #{b}") assert_equal(a + b - (a|b), c, "#{a} & #{b}") assert_equal((~a | b) - ~a, c, "#{a} & #{b}") # Hacker's Delight @@ -282,7 +261,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| VS.each {|b| c = a ^ b - check_class(c) + assert_kind_of(Integer, c) assert_equal(b ^ a, c, "#{a} ^ #{b}") assert_equal((a|b)-(a&b), c, "#{a} ^ #{b}") # Hacker's Delight assert_equal(b, c ^ a, "(#{a} ^ #{b}) ^ #{a}") @@ -295,7 +274,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| small_values.each {|b| c = a << b - check_class(c) + assert_kind_of(Integer, c) if 0 <= b assert_equal(a, c >> b, "(#{a} << #{b}) >> #{b}") assert_equal(a * 2**b, c, "#{a} << #{b}") @@ -312,7 +291,7 @@ class TestIntegerComb < Test::Unit::TestCase VS.each {|a| small_values.each {|b| c = a >> b - check_class(c) + assert_kind_of(Integer, c) if b <= 0 assert_equal(a, c << b, "(#{a} >> #{b}) << #{b}") assert_equal(a * 2**(-b), c, "#{a} >> #{b}") @@ -327,7 +306,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_succ VS.each {|a| b = a.succ - check_class(b) + assert_kind_of(Integer, b) assert_equal(a+1, b, "(#{a}).succ") assert_equal(a, b.pred, "(#{a}).succ.pred") assert_equal(a, b-1, "(#{a}).succ - 1") @@ -337,7 +316,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_pred VS.each {|a| b = a.pred - check_class(b) + assert_kind_of(Integer, b) assert_equal(a-1, b, "(#{a}).pred") assert_equal(a, b.succ, "(#{a}).pred.succ") assert_equal(a, b + 1, "(#{a}).pred + 1") @@ -347,7 +326,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_unary_plus VS.each {|a| b = +a - check_class(b) + assert_kind_of(Integer, b) assert_equal(a, b, "+(#{a})") } end @@ -355,7 +334,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_unary_minus VS.each {|a| b = -a - check_class(b) + assert_kind_of(Integer, b) assert_equal(0-a, b, "-(#{a})") assert_equal(~a+1, b, "-(#{a})") assert_equal(0, a+b, "#{a}+(-(#{a}))") @@ -387,7 +366,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_abs VS.each {|a| b = a.abs - check_class(b) + assert_kind_of(Integer, b) if a < 0 assert_equal(-a, b, "(#{a}).abs") else @@ -399,7 +378,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_ceil VS.each {|a| b = a.ceil - check_class(b) + assert_kind_of(Integer, b) assert_equal(a, b, "(#{a}).ceil") } end @@ -407,7 +386,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_floor VS.each {|a| b = a.floor - check_class(b) + assert_kind_of(Integer, b) assert_equal(a, b, "(#{a}).floor") } end @@ -415,7 +394,7 @@ class TestIntegerComb < Test::Unit::TestCase def test_round VS.each {|a| b = a.round - check_class(b) + assert_kind_of(Integer, b) assert_equal(a, b, "(#{a}).round") } end @@ -423,25 +402,38 @@ class TestIntegerComb < Test::Unit::TestCase def test_truncate VS.each {|a| b = a.truncate - check_class(b) + assert_kind_of(Integer, b) assert_equal(a, b, "(#{a}).truncate") } end def test_remainder + coerce = EnvUtil.labeled_class("CoerceNum") do + def initialize(num) + @num = num + end + def coerce(other) + [other, @num] + end + def inspect + "#{self.class.name}(#@num)" + end + alias to_s inspect + end + VS.each {|a| - VS.each {|b| - if b == 0 + (VS + VS.map {|b| [coerce.new(b), b]}).each {|b, i = b| + if i == 0 assert_raise(ZeroDivisionError) { a.divmod(b) } else - r = a.remainder(b) - check_class(r) + r = assert_nothing_raised(ArgumentError, "#{a}.remainder(#{b})") {a.remainder(b)} + assert_kind_of(Integer, r) if a < 0 - assert_operator(-b.abs, :<, r, "#{a}.remainder(#{b})") + assert_operator(-i.abs, :<, r, "#{a}.remainder(#{b})") assert_operator(0, :>=, r, "#{a}.remainder(#{b})") elsif 0 < a assert_operator(0, :<=, r, "#{a}.remainder(#{b})") - assert_operator(b.abs, :>, r, "#{a}.remainder(#{b})") + assert_operator(i.abs, :>, r, "#{a}.remainder(#{b})") else assert_equal(0, r, "#{a}.remainder(#{b})") end @@ -460,7 +452,7 @@ class TestIntegerComb < Test::Unit::TestCase else assert_equal(false, z, "(#{a}).zero?") assert_equal(a, n, "(#{a}).nonzero?") - check_class(n) + assert_kind_of(Integer, n) end assert(z ^ n, "(#{a}).zero? ^ (#{a}).nonzero?") } @@ -478,6 +470,30 @@ class TestIntegerComb < Test::Unit::TestCase } end + def test_allbits_p + VS.each {|a| + VS.each {|b| + assert_equal((a & b) == b, a.allbits?(b), "(#{a}).allbits?(#{b}") + } + } + end + + def test_anybits_p + VS.each {|a| + VS.each {|b| + assert_equal((a & b) != 0, a.anybits?(b), "(#{a}).anybits?(#{b}") + } + } + end + + def test_nobits_p + VS.each {|a| + VS.each {|b| + assert_equal((a & b) == 0, a.nobits?(b), "(#{a}).nobits?(#{b}") + } + } + end + def test_to_s 2.upto(36) {|radix| VS.each {|a| |
