|
| 1 | +# frozen_string_literal: false |
| 2 | +require_relative 'helper' |
| 3 | +require 'bigdecimal/math' |
| 4 | + |
| 5 | +class TestJRuby < Test::Unit::TestCase |
| 6 | + # JRuby uses its own native BigDecimal implementation |
| 7 | + # but uses the same BigMath module as CRuby. |
| 8 | + # These are test to ensure BigMath works correctly with JRuby's BigDecimal. |
| 9 | + # Also run on CRuby to ensure compatibility. |
| 10 | + |
| 11 | + N = 20 |
| 12 | + |
| 13 | + def test_sqrt |
| 14 | + sqrt2 = BigDecimal(2).sqrt(N) |
| 15 | + assert_in_delta(Math.sqrt(2), sqrt2) |
| 16 | + assert_in_delta(2, sqrt2 * sqrt2) |
| 17 | + end |
| 18 | + |
| 19 | + def test_exp |
| 20 | + assert_in_delta(Math.exp(2), BigMath.exp(BigDecimal(2), N)) |
| 21 | + assert_in_delta(Math.exp(2), BigMath.exp(2, N)) |
| 22 | + assert_in_delta(Math.exp(2.5), BigMath.exp(2.5, N)) |
| 23 | + assert_in_delta(Math.exp(2.5), BigMath.exp(2.5r, N)) |
| 24 | + end |
| 25 | + |
| 26 | + def test_log |
| 27 | + assert_in_delta(Math.log(2), BigMath.log(BigDecimal(2), N)) |
| 28 | + assert_in_delta(Math.log(2), BigMath.log(2, N)) |
| 29 | + assert_in_delta(Math.log(2.5), BigMath.log(2.5, N)) |
| 30 | + assert_in_delta(Math.log(2.5), BigMath.log(2.5r, N)) |
| 31 | + end |
| 32 | + |
| 33 | + def test_power |
| 34 | + x = BigDecimal(2) |
| 35 | + expected = 2 ** 2.5 |
| 36 | + assert_in_delta(expected, x ** BigDecimal('2.5')) |
| 37 | + assert_in_delta(expected, x.sqrt(N) ** 5) |
| 38 | + # assert_in_delta(expected, x ** 2.5) |
| 39 | + assert_in_delta(expected, x ** 2.5r) |
| 40 | + assert_in_delta(expected, x.power(BigDecimal('2.5'), N)) |
| 41 | + # assert_in_delta(expected, x.power(2.5, N)) |
| 42 | + assert_in_delta(expected, x.sqrt(N).power(5, N)) |
| 43 | + assert_in_delta(expected, x.power(2.5r, N)) |
| 44 | + end |
| 45 | + |
| 46 | + def test_bigmath |
| 47 | + assert_in_delta(Math.sqrt(2), BigMath.sqrt(BigDecimal(2), N)) |
| 48 | + assert_in_delta(Math.sin(1), BigMath.sin(BigDecimal(1), N)) |
| 49 | + assert_in_delta(Math.cos(1), BigMath.cos(BigDecimal(1), N)) |
| 50 | + assert_in_delta(Math.atan(1), BigMath.atan(BigDecimal(1), N)) |
| 51 | + assert_in_delta(Math::PI, BigMath.PI(N)) |
| 52 | + assert_in_delta(Math::E, BigMath.E(N)) |
| 53 | + end |
| 54 | +end |
0 commit comments