Skip to content

Commit e864828

Browse files
committed
Add tests for the issue GH-192
1 parent f528a00 commit e864828

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

test/bigdecimal/helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
# frozen_string_literal: false
22
require "test/unit"
33
require "bigdecimal"
4+
require 'rbconfig/sizeof'
45

56
module TestBigDecimalBase
7+
if RbConfig::SIZEOF.key?("int64_t")
8+
SIZEOF_DECDIG = RbConfig::SIZEOF["int32_t"]
9+
BASE = 1_000_000_000
10+
BASE_FIG = 9
11+
else
12+
SIZEOF_DECDIG = RbConfig::SIZEOF["int16_t"]
13+
BASE = 1000
14+
BASE_FIG = 4
15+
end
16+
617
def setup
718
@mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL)
819
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true)

test/bigdecimal/test_bigdecimal.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: false
22
require_relative "helper"
33
require 'bigdecimal/math'
4-
require 'rbconfig/sizeof'
54

65
class TestBigDecimal < Test::Unit::TestCase
76
include TestBigDecimalBase
@@ -101,6 +100,19 @@ def test_BigDecimal_bug7522
101100
assert_not_same(bd, BigDecimal(bd, 1, exception: false))
102101
end
103102

103+
def test_BigDecimal_issue_192
104+
# https://github.com/ruby/bigdecimal/issues/192
105+
# https://github.com/rails/rails/pull/42125
106+
if BASE_FIG == 9
107+
int = 1_000_000_000_12345_0000
108+
big = BigDecimal("0.100000000012345e19")
109+
else # BASE_FIG == 4
110+
int = 1_0000_12_00
111+
big = BigDecimal("0.1000012e9")
112+
end
113+
assert_equal(BigDecimal(int), big, "[ruby/bigdecimal#192]")
114+
end
115+
104116
def test_BigDecimal_with_invalid_string
105117
[
106118
'', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d',

test/bigdecimal/test_bigdecimal_util.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_Float_to_d_without_precision
2525
assert_equal(9.05, 9.05.to_d.to_f)
2626
assert_equal("9.05", 9.05.to_d.to_s('F'))
2727

28+
assert_equal("65.6", 65.6.to_d.to_s("F"))
29+
2830
assert_equal(Math::PI, Math::PI.to_d.to_f)
2931

3032
bug9214 = '[ruby-core:58858]'
@@ -60,6 +62,19 @@ def test_Float_to_d_bug13331
6062
"[ruby-core:80234] [Bug #13331]")
6163
end
6264

65+
def test_Float_to_d_issue_192
66+
# https://github.com/ruby/bigdecimal/issues/192
67+
# https://github.com/rails/rails/pull/42125
68+
if BASE_FIG == 9
69+
flo = 1_000_000_000.12345
70+
big = BigDecimal("0.100000000012345e10")
71+
else # BASE_FIG == 4
72+
flo = 1_0000.12
73+
big = BigDecimal("0.1000012e5")
74+
end
75+
assert_equal(flo.to_d, big, "[ruby/bigdecimal#192]")
76+
end
77+
6378
def test_Rational_to_d
6479
digits = 100
6580
delta = 1.0/10**(digits)

0 commit comments

Comments
 (0)