Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precise tests #238

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion test/bigdecimal/helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# frozen_string_literal: false
require "test/unit"
require "bigdecimal"
require 'rbconfig/sizeof'
require "rbconfig/sizeof"

module TestBigDecimalBase
ROUNDING_MODE_MAP = [
[ BigDecimal::ROUND_UP, :up],
[ BigDecimal::ROUND_DOWN, :down],
[ BigDecimal::ROUND_DOWN, :truncate],
[ BigDecimal::ROUND_HALF_UP, :half_up],
[ BigDecimal::ROUND_HALF_UP, :default],
[ BigDecimal::ROUND_HALF_DOWN, :half_down],
[ BigDecimal::ROUND_HALF_EVEN, :half_even],
[ BigDecimal::ROUND_HALF_EVEN, :banker],
[ BigDecimal::ROUND_CEILING, :ceiling],
[ BigDecimal::ROUND_CEILING, :ceil],
[ BigDecimal::ROUND_FLOOR, :floor],
]

if RbConfig::SIZEOF.key?("int64_t")
SIZEOF_DECDIG = RbConfig::SIZEOF["int32_t"]
BASE = 1_000_000_000
Expand All @@ -14,6 +28,26 @@ module TestBigDecimalBase
BASE_FIG = 4
end

if defined? RbConfig::LIMITS
LIMITS = RbConfig::LIMITS
else
require "fiddle"
LONG_MAX = (1 << (Fiddle::SIZEOF_LONG*8 - 1)) - 1
LONG_MIN = [LONG_MAX + 1].pack("L!").unpack("l!")[0]
LLONG_MAX = (1 << (Fiddle::SIZEOF_LONG_LONG*8 - 1)) - 1
LLONG_MIN = [LLONG_MAX + 1].pack("Q!").unpack("q!")[0]
ULLONG_MAX = (1 << Fiddle::SIZEOF_LONG_LONG*8) - 1
LIMITS = {
"LLONG_MIN" => LLONG_MIN,
"ULLONG_MAX" => ULLONG_MAX,
"FIXNUM_MIN" => LONG_MIN / 2,
"FIXNUM_MAX" => LONG_MAX / 2,
"INT64_MIN" => -9223372036854775808,
"INT64_MAX" => 9223372036854775807,
"UINT64_MAX" => 18446744073709551615,
}.freeze
end

def setup
@mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL)
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true)
Expand Down
34 changes: 0 additions & 34 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,6 @@
class TestBigDecimal < Test::Unit::TestCase
include TestBigDecimalBase

if defined? RbConfig::LIMITS
LIMITS = RbConfig::LIMITS
else
require 'fiddle'
LONG_MAX = (1 << (Fiddle::SIZEOF_LONG*8 - 1)) - 1
LONG_MIN = [LONG_MAX + 1].pack("L!").unpack("l!")[0]
LLONG_MAX = (1 << (Fiddle::SIZEOF_LONG_LONG*8 - 1)) - 1
LLONG_MIN = [LLONG_MAX + 1].pack("Q!").unpack("q!")[0]
ULLONG_MAX = (1 << Fiddle::SIZEOF_LONG_LONG*8) - 1
LIMITS = {
"LLONG_MIN" => LLONG_MIN,
"ULLONG_MAX" => ULLONG_MAX,
"FIXNUM_MIN" => LONG_MIN / 2,
"FIXNUM_MAX" => LONG_MAX / 2,
"INT64_MIN" => -9223372036854775808,
"INT64_MAX" => 9223372036854775807,
"UINT64_MAX" => 18446744073709551615,
}.freeze
end

ROUNDING_MODE_MAP = [
[ BigDecimal::ROUND_UP, :up],
[ BigDecimal::ROUND_DOWN, :down],
[ BigDecimal::ROUND_DOWN, :truncate],
[ BigDecimal::ROUND_HALF_UP, :half_up],
[ BigDecimal::ROUND_HALF_UP, :default],
[ BigDecimal::ROUND_HALF_DOWN, :half_down],
[ BigDecimal::ROUND_HALF_EVEN, :half_even],
[ BigDecimal::ROUND_HALF_EVEN, :banker],
[ BigDecimal::ROUND_CEILING, :ceiling],
[ BigDecimal::ROUND_CEILING, :ceil],
[ BigDecimal::ROUND_FLOOR, :floor],
]

def assert_nan(x)
assert(x.nan?, "Expected #{x.inspect} to be NaN")
end
Expand Down
17 changes: 14 additions & 3 deletions test/bigdecimal/test_bigmath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ class TestBigMath < Test::Unit::TestCase
MINF = BigDecimal("-Infinity")
NAN = BigDecimal("NaN")

def test_const
assert_in_delta(Math::PI, PI(N))
assert_in_delta(Math::E, E(N))
def test_PI
eps = "1e-#{Float::DIG}".to_f
assert_in_delta(Math::PI, PI(N), eps)
assert_in_delta(BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"),
PI(100),
BigDecimal("1e-100"))
end

def test_E
eps = "1e-#{Float::DIG}".to_f
assert_in_delta(Math::E, E(N), eps)
assert_in_delta(BigDecimal("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"),
E(100),
BigDecimal("1e-100"))
end

def test_sqrt
Expand Down