From a756488c6d3a9a0d0fab76cd4c67e655f00c399b Mon Sep 17 00:00:00 2001 From: mrkn Date: Sat, 22 Dec 2012 15:06:22 +0000 Subject: * include/ruby/intern.h: add the prototype declaration of rb_num_coerce_bit. * numeric.c (rb_num_coerce_bit): the new coerce function for bitwise binary operation. * bignum.c (rb_big_and): use coerce to convert the argument, which isn't a Fixnum nor a Bignum, to the corresponding Integer object so that bitwise operations can support Integer-mimic objects. [Bug #1792] [ruby-core:39491] * bignum.c (rb_big_or): ditto. * bignum.c (rb_big_xor): ditto. * numeric.c (bit_coerce): ditto. * numeric.c (fix_and): ditto. * numeric.c (fix_or): ditto. * numeric.c (fix_xor): ditto. * test/ruby/test_integer.rb: add tests for the above changes. * test/ruby/test_bignum.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38560 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_bignum.rb | 36 ++++++++++++++++++++++++++++++++++++ test/ruby/test_integer.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb index 1ff23e2a00..4af3748344 100644 --- a/test/ruby/test_bignum.rb +++ b/test/ruby/test_bignum.rb @@ -654,4 +654,40 @@ class TestBignum < Test::Unit::TestCase def test_frozen assert_equal(true, (2**100).frozen?) end + + def test_bitwise_and_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { T1024 & obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(T1024 & 10, T1024 & obj) + end + + def test_bitwise_or_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { T1024 | obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(T1024 | 10, T1024 | obj) + end + + def test_bitwise_xor_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { T1024 ^ obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(T1024 ^ 10, T1024 ^ obj) + end end diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 3f4add7f15..655f8bb1a5 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -205,4 +205,40 @@ class TestInteger < Test::Unit::TestCase assert_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1)) assert_equal(Bignum, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1).class) end + + def test_bitwise_and_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { 3 & obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(3 & 10, 3 & obj) + end + + def test_bitwise_or_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { 3 | obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(3 | 10, 3 | obj) + end + + def test_bitwise_xor_with_integer_mimic_object + def (obj = Object.new).to_int + 10 + end + assert_raise(TypeError, '[ruby-core:39491]') { 3 ^ obj } + + def obj.coerce(other) + [other, 10] + end + assert_equal(3 ^ 10, 3 ^ obj) + end end -- cgit v1.2.3