[ruby/bigdecimal] Add tests for the issue GH-192

https://github.com/ruby/bigdecimal/commit/e864828b47
This commit is contained in:
Kenta Murata 2021-10-27 17:31:45 +09:00
parent ed7f4c24d7
commit b2a74948b6
No known key found for this signature in database
GPG Key ID: CEFE8AFB6081B062
3 changed files with 39 additions and 1 deletions

View File

@ -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)

View File

@ -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',

View File

@ -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)