1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* numeric.c (bit_coerce): A Fixnum and a Bignum are only permitted for

bitwise arithmetic with a Fixnum.  #1792
* test/ruby/test_fixnum.rb: add tests for the above change.
* bignum.c (bit_coerce): A Fixnum and a Bignum are only permitted for
 bitwise arithmetic with a Bignum.  #1792
* test/ruby/test_bignum.rb: add tests for the above change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33108 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2011-08-29 01:07:15 +00:00
parent 0d530b2392
commit 640420f704
5 changed files with 104 additions and 7 deletions

View file

@ -1,3 +1,15 @@
Sun Aug 29 09:58:00 2011 Kenta Murata <mrkn@mrkn.jp>
* numeric.c (bit_coerce): A Fixnum and a Bignum are only permitted for
bitwise arithmetic with a Fixnum. #1792
* test/ruby/test_fixnum.rb: add tests for the above change.
* bignum.c (bit_coerce): A Fixnum and a Bignum are only permitted for
bitwise arithmetic with a Bignum. #1792
* test/ruby/test_bignum.rb: add tests for the above change.
Sun Aug 28 15:38:17 2011 CHIKANAGA Tomoyuki <nagachika00@gmail.com> Sun Aug 28 15:38:17 2011 CHIKANAGA Tomoyuki <nagachika00@gmail.com>
* ext/date/date_parse.c (date_zone_to_diff): keep a temporary string * ext/date/date_parse.c (date_zone_to_diff): keep a temporary string

View file

@ -3108,9 +3108,9 @@ static inline VALUE
bit_coerce(VALUE x) bit_coerce(VALUE x)
{ {
while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) { while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) {
if (TYPE(x) == T_FLOAT) { rb_raise(rb_eTypeError,
rb_raise(rb_eTypeError, "can't convert Float into Integer"); "can't convert %s into Integer for bitwise arithmetic",
} rb_obj_classname(x));
x = rb_to_int(x); x = rb_to_int(x);
} }
return x; return x;

View file

@ -2917,10 +2917,9 @@ static VALUE
bit_coerce(VALUE x) bit_coerce(VALUE x)
{ {
while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) { while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) {
if (TYPE(x) == T_FLOAT) { rb_raise(rb_eTypeError,
rb_raise(rb_eTypeError, "can't convert Float into Integer"); "can't convert %s into Integer for bitwise arithmetic",
} rb_obj_classname(x));
x = rb_to_int(x);
} }
return x; return x;
} }

View file

@ -118,6 +118,8 @@ class TestBignum < Test::Unit::TestCase
T32P = T32 - 1 # 4294967295 T32P = T32 - 1 # 4294967295
T64 = 2**64 # 18446744073709551616 T64 = 2**64 # 18446744073709551616
T64P = T64 - 1 # 18446744073709551615 T64P = T64 - 1 # 18446744073709551615
T1024 = 2**1024
T1024P = T1024 - 1
def test_big_2comp def test_big_2comp
assert_equal("-4294967296", (~T32P).to_s) assert_equal("-4294967296", (~T32P).to_s)
@ -321,6 +323,48 @@ class TestBignum < Test::Unit::TestCase
assert_equal(T64 + T32, T32 ^ T64) assert_equal(T64 + T32, T32 ^ T64)
end end
class DummyNumeric < Numeric
def to_int
1
end
end
def test_and_with_float
assert_raise(TypeError) { T1024 & 1.5 }
end
def test_and_with_rational
assert_raise(TypeError, "#1792") { T1024 & Rational(3, 2) }
end
def test_and_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { T1024 & DummyNumeric.new }
end
def test_or_with_float
assert_raise(TypeError) { T1024 | 1.5 }
end
def test_or_with_rational
assert_raise(TypeError, "#1792") { T1024 | Rational(3, 2) }
end
def test_or_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { T1024 | DummyNumeric.new }
end
def test_xor_with_float
assert_raise(TypeError) { T1024 ^ 1.5 }
end
def test_xor_with_rational
assert_raise(TypeError, "#1792") { T1024 ^ Rational(3, 2) }
end
def test_xor_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { T1024 ^ DummyNumeric.new }
end
def test_shift2 def test_shift2
assert_equal(2**33, (2**32) << 1) assert_equal(2**33, (2**32) << 1)
assert_equal(2**31, (2**32) << -1) assert_equal(2**31, (2**32) << -1)

View file

@ -229,4 +229,46 @@ class TestFixnum < Test::Unit::TestCase
assert(!(1.send(:<=, 0.0))) assert(!(1.send(:<=, 0.0)))
assert_raise(ArgumentError) { 1.send(:<=, nil) } assert_raise(ArgumentError) { 1.send(:<=, nil) }
end end
class DummyNumeric < Numeric
def to_int
1
end
end
def test_and_with_float
assert_raise(TypeError) { 1 & 1.5 }
end
def test_and_with_rational
assert_raise(TypeError, "#1792") { 1 & Rational(3, 2) }
end
def test_and_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { 1 & DummyNumeric.new }
end
def test_or_with_float
assert_raise(TypeError) { 1 | 1.5 }
end
def test_or_with_rational
assert_raise(TypeError, "#1792") { 1 | Rational(3, 2) }
end
def test_or_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { 1 | DummyNumeric.new }
end
def test_xor_with_float
assert_raise(TypeError) { 1 ^ 1.5 }
end
def test_xor_with_rational
assert_raise(TypeError, "#1792") { 1 ^ Rational(3, 2) }
end
def test_xor_with_nonintegral_numeric
assert_raise(TypeError, "#1792") { 1 ^ DummyNumeric.new }
end
end end