* ext/bigdecimal/bigdecimal.c (BigDecimal_save_exception_mode,

BigDecimal_save_rounding_mode, BigDecimal_save_limit): added.
* test/bigdecimal/test_bigdecimal.rb: added tests for the above
  features.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29127 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2010-08-28 08:52:14 +00:00
Родитель b6595225c1
Коммит a2bf9203e8
3 изменённых файлов: 90 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,11 @@
Sat Aug 28 17:39:33 2010 Kenta Murata <mrkn@mrkn.jp>
* ext/bigdecimal/bigdecimal.c (BigDecimal_save_exception_mode,
BigDecimal_save_rounding_mode, BigDecimal_save_limit): added.
* test/bigdecimal/test_bigdecimal.rb: added tests for the above
features.
Sat Aug 28 08:11:05 2010 Tanaka Akira <akr@fsij.org>
* ext/pathname/pathname.c (path_blockdev_p): Pathname#blockdev?

Просмотреть файл

@ -1800,6 +1800,48 @@ BigDecimal_sign(VALUE self)
return INT2FIX(s);
}
/* call-seq:
* BigDecimal.save_exception_mode { ... }
*/
static VALUE
BigDecimal_save_exception_mode(VALUE self)
{
unsigned short const exception_mode = VpGetException();
int state;
VALUE ret = rb_protect(rb_yield, Qnil, &state);
VpSetException(exception_mode);
if (state) rb_jump_tag(state);
return ret;
}
/* call-seq:
* BigDecimal.save_rounding_mode { ... }
*/
static VALUE
BigDecimal_save_rounding_mode(VALUE self)
{
unsigned short const round_mode = VpGetRoundMode();
int state;
VALUE ret = rb_protect(rb_yield, Qnil, &state);
VpSetRoundMode(round_mode);
if (state) rb_jump_tag(state);
return Qnil;
}
/* call-seq:
* BigDecimal.save_limit { ... }
*/
static VALUE
BigDecimal_save_limit(VALUE self)
{
size_t const limit = VpGetPrecLimit();
int state;
VALUE ret = rb_protect(rb_yield, Qnil, &state);
VpSetPrecLimit(limit);
if (state) rb_jump_tag(state);
return Qnil;
}
/* Document-class: BigDecimal
* BigDecimal provides arbitrary-precision floating point decimal arithmetic.
*
@ -1926,6 +1968,10 @@ Init_bigdecimal(void)
rb_define_singleton_method(rb_cBigDecimal, "_load", BigDecimal_load, 1);
rb_define_singleton_method(rb_cBigDecimal, "ver", BigDecimal_version, 0);
rb_define_singleton_method(rb_cBigDecimal, "save_exception_mode", BigDecimal_save_exception_mode, 0);
rb_define_singleton_method(rb_cBigDecimal, "save_rounding_mode", BigDecimal_save_rounding_mode, 0);
rb_define_singleton_method(rb_cBigDecimal, "save_limit", BigDecimal_save_limit, 0);
/* Constants definition */
/*

Просмотреть файл

@ -77,6 +77,42 @@ class TestBigDecimal < Test::Unit::TestCase
end
end
def test_save_exception_mode
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
mode = BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW)
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
end
assert_equal(mode, BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW))
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_FLOOR)
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_EVEN)
end
assert_equal(BigDecimal::ROUND_HALF_EVEN, BigDecimal.mode(BigDecimal::ROUND_MODE))
end
def test_save_rounding_mode
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_FLOOR)
BigDecimal.save_rounding_mode do
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_EVEN)
end
assert_equal(BigDecimal::ROUND_FLOOR, BigDecimal.mode(BigDecimal::ROUND_MODE))
end
def test_save_limit
begin
old = BigDecimal.limit
BigDecimal.limit(100)
BigDecimal.save_limit do
BigDecimal.limit(200)
end
assert_equal(100, BigDecimal.limit);
ensure
BigDecimal.limit(old)
end
end
def test_exception_nan
_test_mode(BigDecimal::EXCEPTION_NaN) { BigDecimal.new("NaN") }
end