Skip to content

Commit

Permalink
[ruby/bigdecimal] Add tests for the issue GH-192
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Dec 23, 2021
1 parent ed7f4c2 commit b2a7494
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions 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)
Expand Down
14 changes: 13 additions & 1 deletion 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
Expand Down Expand Up @@ -101,6 +100,19 @@ def test_BigDecimal_bug7522
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',
Expand Down
15 changes: 15 additions & 0 deletions test/bigdecimal/test_bigdecimal_util.rb
Expand Up @@ -25,6 +25,8 @@ def test_Float_to_d_without_precision
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]'
Expand Down Expand Up @@ -60,6 +62,19 @@ def test_Float_to_d_bug13331
"[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)
Expand Down

0 comments on commit b2a7494

Please sign in to comment.