From 06fd323f450c4989b82407698c363c7bb0272144 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 1 Nov 2016 22:34:30 +0000 Subject: 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 --- test/ruby/test_integer.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/ruby/test_integer.rb') 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) -- cgit v1.2.3