Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 98cab87bbe32
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8c2b5f587df4
Choose a head ref
  • 4 commits
  • 3 files changed
  • 1 contributor

Commits on Jun 4, 2015

  1. Copy the full SHA
    f8a17c1 View commit details
  2. Copy the full SHA
    a11eb7a View commit details
  3. Copy the full SHA
    0ab74b7 View commit details
  4. Copy the full SHA
    8c2b5f5 View commit details
Showing with 277 additions and 257 deletions.
  1. +200 −219 core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
  2. +69 −38 test/test_big_decimal.rb
  3. +8 −0 test/test_higher_javasupport.rb
419 changes: 200 additions & 219 deletions core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java

Large diffs are not rendered by default.

107 changes: 69 additions & 38 deletions test/test_big_decimal.rb
Original file line number Diff line number Diff line change
@@ -15,12 +15,12 @@ def test_no_singleton_methods_on_bigdecimal
assert_raise(TypeError) { class << num ; def amethod ; end ; end }
assert_raise(TypeError) { def num.amethod ; end }
end

def test_can_instantiate_big_decimal
assert_nothing_raised {BigDecimal.new("4")}
assert_nothing_raised {BigDecimal.new("3.14159")}
end

def test_can_implicitly_instantiate_big_decimal
# JRUBY-153 issues
assert_nothing_raised {BigDecimal("4")}
@@ -43,21 +43,61 @@ def test_alphabetic_args_return_zero
'Big Decimal objects instanitiated with a value that starts
with a letter should have a value of 0.0' )
end

class X
def to_str; "3.14159" end
end

def test_can_accept_arbitrary_objects_as_arguments
# as log as the object has a #to_str method...
x = X.new
assert_nothing_raised { BigDecimal.new(x) }
assert_nothing_raised { BigDecimal(x) }
end


def test_cmp
begin
BigDecimal.new('10') < "foo"
rescue ArgumentError => e
assert_equal 'comparison of BigDecimal with String failed', e.message
else
fail 'expected cmp to fail'
end

begin
BigDecimal.new('10') >= nil
rescue ArgumentError => e
assert_equal 'comparison of BigDecimal with nil failed', e.message
else
fail 'expected cmp to fail'
end
end

class MyNum
def *(other)
33
end

def /(other)
99
end

def coerce(other)
[MyNum.new, self]
end
end

def test_coerce_div_mul
require 'bigdecimal/util'

assert_equal 33, BigDecimal.new('10') * MyNum.new
assert_equal 99, 10.0 / MyNum.new
assert_equal 99, 10.0.to_d / MyNum.new
end

require "bigdecimal/newton"
include Newton

class Function
def initialize()
@zero = BigDecimal::new("0.0")
@@ -80,7 +120,7 @@ def values(x) # <= defines functions solved
f
end
end

def test_newton_extension
f = BigDecimal::limit(100)
f = Function.new
@@ -90,10 +130,10 @@ def test_newton_extension
BigDecimal('0.1000000000262923315461642086010446338567975310185638386446002778855192224707966221794469725479649528E1')]
assert_equal expected, x
end

require "bigdecimal/math.rb"
include BigMath

def test_math_extension
expected = BigDecimal('0.31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066453462141417033006060218E1')
# this test fails under C Ruby
@@ -104,7 +144,7 @@ def test_math_extension
one = BigDecimal("1")
two = BigDecimal("2")
three = BigDecimal("3")

assert_equal one * 1, one
assert_equal one / 1, one
assert_equal one + 1, two
@@ -114,25 +154,25 @@ def test_math_extension
assert_equal one, three % two
assert_equal BigDecimal("0.2"), BigDecimal("2.2") % two
assert_equal BigDecimal("0.003"), BigDecimal("15.993") % BigDecimal("15.99")

assert_equal 1*one, one
assert_equal 1/one, one
assert_equal 1+one, BigDecimal("2")
assert_equal 1-one, BigDecimal("0")

assert_equal one * 1.0, 1.0
assert_equal one / 1.0, 1.0
assert_equal one + 1.0, 2.0
assert_equal one - 1.0, 0.0

assert_equal 1.0*one, 1.0
assert_equal 1.0/one, 1.0
assert_equal 1.0+one, 2.0
assert_equal 1.0-one, 0.0

assert_equal("1.0", BigDecimal.new('1.0').to_s('F'))
assert_equal("0.0", BigDecimal.new('0.0').to_s)

assert_equal(BigDecimal("2"), BigDecimal("1.5").round)
assert_equal(BigDecimal("15"), BigDecimal("15").round)
assert_equal(BigDecimal("20"), BigDecimal("15").round(-1))
@@ -141,7 +181,7 @@ def test_math_extension
assert_equal(BigDecimal("10"), BigDecimal("15").round(-1, BigDecimal::ROUND_HALF_DOWN))
assert_equal(BigDecimal("20"), BigDecimal("25").round(-1, BigDecimal::ROUND_HALF_EVEN))
assert_equal(BigDecimal("15.99"), BigDecimal("15.993").round(2))

assert_equal(BigDecimal("1"), BigDecimal("1.8").round(0, BigDecimal::ROUND_DOWN))
assert_equal(BigDecimal("2"), BigDecimal("1.2").round(0, BigDecimal::ROUND_UP))
assert_equal(BigDecimal("-1"), BigDecimal("-1.5").round(0, BigDecimal::ROUND_CEILING))
@@ -151,7 +191,7 @@ def test_math_extension
assert_equal(BigDecimal("2"), BigDecimal("1.5").round(0, BigDecimal::ROUND_HALF_EVEN))
assert_equal(BigDecimal("2"), BigDecimal("2.5").round(0, BigDecimal::ROUND_HALF_EVEN))
end

def test_big_decimal_power
n = BigDecimal("10")
assert_equal(n.power(0), BigDecimal("1"))
@@ -166,48 +206,48 @@ def test_big_decimal_mode
assert BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW)
assert BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW,true)
assert BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW,false)

# Reject invalid arguments to #mode
assert_raises(TypeError) { BigDecimal.mode(true) } # first argument must be a Fixnum
assert_raises(ArgumentError) { BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, 1) } # second argument must be [true|false]
assert_raises(TypeError) { BigDecimal.mode(512) } # first argument must be == 256, or return non-zero when AND-ed with 255

# exception mode defaults to 0
assert_equal 0, BigDecimal.mode(1) # value of first argument doesn't matter when retrieving the current exception mode, as long as it's a Fixnum <= 255

# set and clear a single exception mode
assert_equal BigDecimal::EXCEPTION_INFINITY, BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
assert_equal 0, BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
assert_equal BigDecimal::EXCEPTION_NaN, BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true)
assert_equal 0, BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)

# set a composition of exception modes separately, make sure the final result is the composited value
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true)
assert_equal BigDecimal::EXCEPTION_INFINITY | BigDecimal::EXCEPTION_NaN, BigDecimal.mode(1)

# reset the exception mode to 0 for the following tests
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)

# set a composition of exception modes with one call and retrieve it using the retrieval idiom
# note: this is to check compatibility with MRI, which currently sets only the last mode
# it checks for
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY | BigDecimal::EXCEPTION_NaN, true)
assert_equal BigDecimal::EXCEPTION_NaN, BigDecimal.mode(1)

# rounding mode defaults to BigDecimal::ROUND_HALF_UP
assert_equal BigDecimal::ROUND_HALF_UP, BigDecimal.mode(BigDecimal::ROUND_MODE)

# make sure each setting complete replaces any previous setting
[BigDecimal::ROUND_UP, BigDecimal::ROUND_DOWN, BigDecimal::ROUND_CEILING, BigDecimal::ROUND_FLOOR,
BigDecimal::ROUND_HALF_UP, BigDecimal::ROUND_HALF_DOWN, BigDecimal::ROUND_HALF_EVEN].each do |mode|
assert_equal mode, BigDecimal.mode(BigDecimal::ROUND_MODE, mode)
end

# reset rounding mode to 0 for following tests
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP)

assert_raises(TypeError) { BigDecimal.mode(BigDecimal::ROUND_MODE, true) } # second argument must be a Fixnum
assert_raises(ArgumentError) { BigDecimal.mode(BigDecimal::ROUND_MODE, 8) } # any Fixnum >= 8 should trigger this error, as the valid rounding modes are currently [0..6]
end
@@ -218,7 +258,7 @@ def test_marshaling
bd_serialized = Marshal.dump(bd)
assert_equal f, Marshal.restore(bd_serialized).to_f
end

#JRUBY-2272
def test_marshal_regression
assert_equal BigDecimal('0.0'), Marshal.load(Marshal.dump(BigDecimal.new('0.0')))
@@ -234,18 +274,9 @@ def test_large_bigdecimal_to_f
assert neg_inf < 0
assert BigDecimal.new("5E-69999999").to_f < Float::EPSILON
end

#JRUBY-3818
def test_decimal_format
require 'java'
format = java.text.DecimalFormat.new("#,##0.00")
locale_separator = java.text.DecimalFormatSymbols.new().getDecimalSeparator()
value = java.math.BigDecimal.new("10")
assert_equal "10" + locale_separator.chr + "00", format.format(value)
end

#JRUBY-5190
def test_large_precisions
def test_large_precisions
a = BigDecimal("1").div(BigDecimal("3"), 307)
b = BigDecimal("1").div(BigDecimal("3"), 308)
assert_equal a.to_f, b.to_f
8 changes: 8 additions & 0 deletions test/test_higher_javasupport.rb
Original file line number Diff line number Diff line change
@@ -702,6 +702,14 @@ def test_big_decimal_interaction
assert_equal(BigDecimal, BigDecimal.new("1.23").add(BigDecimal.new("2.34")).class)
end

#JRUBY-3818
def test_decimal_format
format = java.text.DecimalFormat.new("#,##0.00")
locale_separator = java.text.DecimalFormatSymbols.new().getDecimalSeparator()
value = java.math.BigDecimal.new("10")
assert_equal "10" + locale_separator.chr + "00", format.format(value)
end

def test_direct_package_access
a = java.util.ArrayList.new
assert_equal(0, a.size)