[前][次][番号順一覧][スレッド一覧]

ruby-changes:70471

From: Kenta <ko1@a...>
Date: Fri, 24 Dec 2021 02:30:04 +0900 (JST)
Subject: [ruby-changes:70471] b2a74948b6 (master): [ruby/bigdecimal] Add tests for the issue GH-192

https://git.ruby-lang.org/ruby.git/commit/?id=b2a74948b6

From b2a74948b6fe60727235b80ab56b4c701c315aa3 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Wed, 27 Oct 2021 17:31:45 +0900
Subject: [ruby/bigdecimal] Add tests for the issue GH-192

https://github.com/ruby/bigdecimal/commit/e864828b47
---
 test/bigdecimal/helper.rb               | 11 +++++++++++
 test/bigdecimal/test_bigdecimal.rb      | 14 +++++++++++++-
 test/bigdecimal/test_bigdecimal_util.rb | 15 +++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/test/bigdecimal/helper.rb b/test/bigdecimal/helper.rb
index 22b05f09ae4..46721fb9a81 100644
--- a/test/bigdecimal/helper.rb
+++ b/test/bigdecimal/helper.rb
@@ -1,8 +1,19 @@ https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/helper.rb#L1
 # 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 d7b245b0b81..1e6c59535fe 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -1,7 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1
 # 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 https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L100
     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 ffd4c5f679d..2f27163ebfa 100644
--- a/test/bigdecimal/test_bigdecimal_util.rb
+++ b/test/bigdecimal/test_bigdecimal_util.rb
@@ -25,6 +25,8 @@ class TestBigDecimalUtil < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal_util.rb#L25
     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 https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal_util.rb#L62
                  "[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)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]