From c7de69f0af052c6004ece6f961a64f7dfc84069d Mon Sep 17 00:00:00 2001 From: gogotanaka Date: Sat, 31 Jan 2015 16:14:21 -0800 Subject: [PATCH 2/2] Add tests for math.c in the context of overriding Integer#to_f To: https://bugs.ruby-lang.org/ --- test/ruby/test_math.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index a067aae..edc7edf 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -290,4 +290,41 @@ class TestMath < Test::Unit::TestCase assert_raise(Math::DomainError) { Math.lgamma(-Float::INFINITY) } end + + def test_override_fixnum_to_f + Fixnum.class_eval do + alias _to_f to_f + def to_f + (self + 1)._to_f + end + end + + check(Math.cos(1._to_f), Math.cos(0)) + check(Math.exp(1._to_f), Math.exp(0)) + check(Math.log(1._to_f), Math.log(0)) + + Fixnum.class_eval do + def to_f + _to_f + end + end + end + + def test_override_bignum_to_f + Bignum.class_eval do + alias _to_f to_f + def to_f + (self << 1)._to_f + end + end + + check(Math.cos((1 << 63)._to_f), Math.cos(1 << 62)) + check(Math.log((1 << 63)._to_f), Math.log(1 << 62)) + + Bignum.class_eval do + def to_f + _to_f + end + end + end end -- gogotanaka