diff --git a/test/bigdecimal/helper.rb b/test/bigdecimal/helper.rb index 22b05f09ae..46721fb9a8 100644 --- a/test/bigdecimal/helper.rb +++ b/test/bigdecimal/helper.rb @@ -1,8 +1,19 @@ # frozen_string_literal: false require "test/unit" require "bigdecimal" +require 'rbconfig/sizeof' module TestBigDecimalBase + if RbConfig::SIZEOF.key?("int64_t") + SIZEOF_DECDIG = RbConfig::SIZEOF["int32_t"] + BASE = 1_000_000_000 + BASE_FIG = 9 + else + SIZEOF_DECDIG = RbConfig::SIZEOF["int16_t"] + BASE = 1000 + BASE_FIG = 4 + end + def setup @mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL) BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true) diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index d7b245b0b8..1e6c59535f 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1,7 +1,6 @@ # frozen_string_literal: false require_relative "helper" require 'bigdecimal/math' -require 'rbconfig/sizeof' class TestBigDecimal < Test::Unit::TestCase include TestBigDecimalBase @@ -101,6 +100,19 @@ class TestBigDecimal < Test::Unit::TestCase assert_not_same(bd, BigDecimal(bd, 1, exception: false)) end + def test_BigDecimal_issue_192 + # https://github.com/ruby/bigdecimal/issues/192 + # https://github.com/rails/rails/pull/42125 + if BASE_FIG == 9 + int = 1_000_000_000_12345_0000 + big = BigDecimal("0.100000000012345e19") + else # BASE_FIG == 4 + int = 1_0000_12_00 + big = BigDecimal("0.1000012e9") + end + assert_equal(BigDecimal(int), big, "[ruby/bigdecimal#192]") + end + def test_BigDecimal_with_invalid_string [ '', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d', diff --git a/test/bigdecimal/test_bigdecimal_util.rb b/test/bigdecimal/test_bigdecimal_util.rb index ffd4c5f679..2f27163ebf 100644 --- a/test/bigdecimal/test_bigdecimal_util.rb +++ b/test/bigdecimal/test_bigdecimal_util.rb @@ -25,6 +25,8 @@ class TestBigDecimalUtil < Test::Unit::TestCase assert_equal(9.05, 9.05.to_d.to_f) assert_equal("9.05", 9.05.to_d.to_s('F')) + assert_equal("65.6", 65.6.to_d.to_s("F")) + assert_equal(Math::PI, Math::PI.to_d.to_f) bug9214 = '[ruby-core:58858]' @@ -60,6 +62,19 @@ class TestBigDecimalUtil < Test::Unit::TestCase "[ruby-core:80234] [Bug #13331]") end + def test_Float_to_d_issue_192 + # https://github.com/ruby/bigdecimal/issues/192 + # https://github.com/rails/rails/pull/42125 + if BASE_FIG == 9 + flo = 1_000_000_000.12345 + big = BigDecimal("0.100000000012345e10") + else # BASE_FIG == 4 + flo = 1_0000.12 + big = BigDecimal("0.1000012e5") + end + assert_equal(flo.to_d, big, "[ruby/bigdecimal#192]") + end + def test_Rational_to_d digits = 100 delta = 1.0/10**(digits)