diff options
Diffstat (limited to 'test/ruby/test_integer_comb.rb')
| -rw-r--r-- | test/ruby/test_integer_comb.rb | 135 |
1 files changed, 80 insertions, 55 deletions
diff --git a/test/ruby/test_integer_comb.rb b/test/ruby/test_integer_comb.rb index c057deb36f..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 @@ -106,28 +107,8 @@ class TestIntegerComb < Test::Unit::TestCase ] #VS.map! {|v| 0x4000000000000000.coerce(v)[0] } - - 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 + #VS.concat VS.find_all {|v| Fixnum === v }.map {|v| 0x4000000000000000.coerce(v)[0] } + #VS.sort! {|a, b| a.abs <=> b.abs } def test_aref VS.each {|a| @@ -139,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 @@ -153,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 @@ -168,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 @@ -183,8 +164,9 @@ 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 assert_equal(a.abs * b.abs, (a * b).abs, "(#{a} * #{b}).abs") assert_equal((a-100)*(b-100)+(a-100)*100+(b-100)*100+10000, c, "#{a} * #{b}") @@ -200,11 +182,17 @@ 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(r.abs < b.abs) - assert(0 < b ? (0 <= r && r < b) : (b < r && r <= 0)) + assert_operator(r.abs, :<, b.abs) + if 0 < b + assert_operator(r, :>=, 0) + assert_operator(r, :<, b) + else + assert_operator(r, :>, b) + assert_operator(r, :<=, 0) + end assert_equal(q, a/b) assert_equal(q, a.div(b)) assert_equal(r, a%b) @@ -219,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}") @@ -235,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}") @@ -247,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 @@ -260,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 @@ -273,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}") @@ -286,12 +274,12 @@ 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}") end - 0.upto(c.size*8+10) {|nth| + 0.upto(c.bit_length+10) {|nth| assert_equal(a[nth-b], c[nth], "(#{a} << #{b})[#{nth}]") } } @@ -303,12 +291,12 @@ 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}") end - 0.upto(c.size*8+10) {|nth| + 0.upto(c.bit_length+10) {|nth| assert_equal(a[nth+b], c[nth], "(#{a} >> #{b})[#{nth}]") } } @@ -318,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") @@ -328,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") @@ -338,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 @@ -346,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}))") @@ -378,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 @@ -390,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 @@ -398,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 @@ -406,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 @@ -414,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 @@ -451,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?") } @@ -469,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| |
