1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/rubyspec/library/bigdecimal/mode_spec.rb
eregon 95e8c48dd3 Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
  These files can therefore be updated like any other file in MRI.
  Instructions are provided in spec/README.
  [Feature #13156] [ruby-core:79246]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-07 12:04:49 +00:00

36 lines
1.7 KiB
Ruby

require File.expand_path('../../../spec_helper', __FILE__)
require 'bigdecimal'
describe "BigDecimal.mode" do
#the default value of BigDecimal exception constants is false
after :each do
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, false)
end
it "returns the appropriate value and continue the computation if the flag is false" do
BigDecimal("NaN").add(BigDecimal("1"),0).nan?.should == true
BigDecimal("0").add(BigDecimal("Infinity"),0).should == BigDecimal("Infinity")
BigDecimal("1").quo(BigDecimal("0")).should == BigDecimal("Infinity")
end
it "returns Infinity when too big" do
BigDecimal("1E11111111111111111111").should == BigDecimal("Infinity")
(BigDecimal("1E1000000000000000000")**10).should == BigDecimal("Infinity")
end
it "raise an exception if the flag is true" do
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true)
lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError)
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError)
BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError)
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError)
lambda { (BigDecimal("1E1000000000000000000")**10) }.should raise_error(FloatDomainError)
end
end