diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-01 22:34:30 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-01 22:34:30 +0000 |
commit | 06fd323f450c4989b82407698c363c7bb0272144 (patch) | |
tree | eb5c53df995cf03c199d87b1f5e6673547d58fc6 /test/ruby | |
parent | 714f309d367473877b768d28e39354518c417d15 (diff) | |
download | ruby-06fd323f450c4989b82407698c363c7bb0272144.tar.gz |
numeric.c: bit op with non-integer
* numeric.c (rb_num_coerce_bit): enable bit operations with
coercing by non-integer object. [ruby-core:77783] [Bug #12875]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56543 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_integer.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index b52f1f7c51..6e505ec003 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -302,6 +302,42 @@ class TestInteger < Test::Unit::TestCase assert_equal(3 ^ 10, 3 ^ obj) end + module CoercionToSelf + def coerce(other) + [self.class.new(other), self] + end + end + + def test_bitwise_and_with_integer_coercion + obj = Struct.new(:value) do + include(CoercionToSelf) + def &(other) + self.value & other.value + end + end.new(10) + assert_equal(3 & 10, 3 & obj) + end + + def test_bitwise_or_with_integer_coercion + obj = Struct.new(:value) do + include(CoercionToSelf) + def |(other) + self.value | other.value + end + end.new(10) + assert_equal(3 | 10, 3 | obj) + end + + def test_bitwise_xor_with_integer_coercion + obj = Struct.new(:value) do + include(CoercionToSelf) + def ^(other) + self.value ^ other.value + end + end.new(10) + assert_equal(3 ^ 10, 3 ^ obj) + end + def test_bit_length assert_equal(13, (-2**12-1).bit_length) assert_equal(12, (-2**12).bit_length) |