diff options
Diffstat (limited to 'spec/ruby/library/bigdecimal/truncate_spec.rb')
-rw-r--r-- | spec/ruby/library/bigdecimal/truncate_spec.rb | 135 |
1 files changed, 69 insertions, 66 deletions
diff --git a/spec/ruby/library/bigdecimal/truncate_spec.rb b/spec/ruby/library/bigdecimal/truncate_spec.rb index 4ad9eb92d1..fbb5b69779 100644 --- a/spec/ruby/library/bigdecimal/truncate_spec.rb +++ b/spec/ruby/library/bigdecimal/truncate_spec.rb @@ -1,81 +1,84 @@ require_relative '../../spec_helper' -require 'bigdecimal' -describe "BigDecimal#truncate" do +ruby_version_is ""..."3.4" do + require 'bigdecimal' - before :each do - @arr = ['3.14159', '8.7', "0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1"] - @big = BigDecimal("123456.789") - @nan = BigDecimal('NaN') - @infinity = BigDecimal('Infinity') - @infinity_negative = BigDecimal('-Infinity') - end + describe "BigDecimal#truncate" do - it "returns value of type Integer." do - @arr.each do |x| - BigDecimal(x).truncate.kind_of?(Integer).should == true + before :each do + @arr = ['3.14159', '8.7', "0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1"] + @big = BigDecimal("123456.789") + @nan = BigDecimal('NaN') + @infinity = BigDecimal('Infinity') + @infinity_negative = BigDecimal('-Infinity') end - end - it "returns the integer part as a BigDecimal if no precision given" do - BigDecimal(@arr[0]).truncate.should == 3 - BigDecimal(@arr[1]).truncate.should == 8 - BigDecimal(@arr[2]).truncate.should == 3 - BigDecimal('0').truncate.should == 0 - BigDecimal('0.1').truncate.should == 0 - BigDecimal('-0.1').truncate.should == 0 - BigDecimal('1.5').truncate.should == 1 - BigDecimal('-1.5').truncate.should == -1 - BigDecimal('1E10').truncate.should == BigDecimal('1E10') - BigDecimal('-1E10').truncate.should == BigDecimal('-1E10') - BigDecimal('1.8888E10').truncate.should == BigDecimal('1.8888E10') - BigDecimal('-1E-1').truncate.should == 0 - end + it "returns value of type Integer." do + @arr.each do |x| + BigDecimal(x).truncate.kind_of?(Integer).should == true + end + end - it "returns value of given precision otherwise" do - BigDecimal('-1.55').truncate(1).should == BigDecimal('-1.5') - BigDecimal('1.55').truncate(1).should == BigDecimal('1.5') - BigDecimal(@arr[0]).truncate(2).should == BigDecimal("3.14") - BigDecimal('123.456').truncate(2).should == BigDecimal("123.45") - BigDecimal('123.456789').truncate(4).should == BigDecimal("123.4567") - BigDecimal('0.456789').truncate(10).should == BigDecimal("0.456789") - BigDecimal('-1E-1').truncate(1).should == BigDecimal('-0.1') - BigDecimal('-1E-1').truncate(2).should == BigDecimal('-0.1E0') - BigDecimal('-1E-1').truncate.should == BigDecimal('0') - BigDecimal('-1E-1').truncate(0).should == BigDecimal('0') - BigDecimal('-1E-1').truncate(-1).should == BigDecimal('0') - BigDecimal('-1E-1').truncate(-2).should == BigDecimal('0') + it "returns the integer part as a BigDecimal if no precision given" do + BigDecimal(@arr[0]).truncate.should == 3 + BigDecimal(@arr[1]).truncate.should == 8 + BigDecimal(@arr[2]).truncate.should == 3 + BigDecimal('0').truncate.should == 0 + BigDecimal('0.1').truncate.should == 0 + BigDecimal('-0.1').truncate.should == 0 + BigDecimal('1.5').truncate.should == 1 + BigDecimal('-1.5').truncate.should == -1 + BigDecimal('1E10').truncate.should == BigDecimal('1E10') + BigDecimal('-1E10').truncate.should == BigDecimal('-1E10') + BigDecimal('1.8888E10').truncate.should == BigDecimal('1.8888E10') + BigDecimal('-1E-1').truncate.should == 0 + end - BigDecimal(@arr[1]).truncate(1).should == BigDecimal("8.7") - BigDecimal(@arr[2]).truncate(100).should == BigDecimal(\ - "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679") - end + it "returns value of given precision otherwise" do + BigDecimal('-1.55').truncate(1).should == BigDecimal('-1.5') + BigDecimal('1.55').truncate(1).should == BigDecimal('1.5') + BigDecimal(@arr[0]).truncate(2).should == BigDecimal("3.14") + BigDecimal('123.456').truncate(2).should == BigDecimal("123.45") + BigDecimal('123.456789').truncate(4).should == BigDecimal("123.4567") + BigDecimal('0.456789').truncate(10).should == BigDecimal("0.456789") + BigDecimal('-1E-1').truncate(1).should == BigDecimal('-0.1') + BigDecimal('-1E-1').truncate(2).should == BigDecimal('-0.1E0') + BigDecimal('-1E-1').truncate.should == BigDecimal('0') + BigDecimal('-1E-1').truncate(0).should == BigDecimal('0') + BigDecimal('-1E-1').truncate(-1).should == BigDecimal('0') + BigDecimal('-1E-1').truncate(-2).should == BigDecimal('0') - it "sets n digits left of the decimal point to 0, if given n < 0" do - @big.truncate(-1).should == BigDecimal("123450.0") - @big.truncate(-2).should == BigDecimal("123400.0") - BigDecimal(@arr[2]).truncate(-1).should == 0 - end + BigDecimal(@arr[1]).truncate(1).should == BigDecimal("8.7") + BigDecimal(@arr[2]).truncate(100).should == BigDecimal(\ + "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679") + end - it "returns NaN if self is NaN" do - @nan.truncate(-1).should.nan? - @nan.truncate(+1).should.nan? - @nan.truncate(0).should.nan? - end + it "sets n digits left of the decimal point to 0, if given n < 0" do + @big.truncate(-1).should == BigDecimal("123450.0") + @big.truncate(-2).should == BigDecimal("123400.0") + BigDecimal(@arr[2]).truncate(-1).should == 0 + end - it "returns Infinity if self is infinite" do - @infinity.truncate(-1).should == @infinity - @infinity.truncate(+1).should == @infinity - @infinity.truncate(0).should == @infinity + it "returns NaN if self is NaN" do + @nan.truncate(-1).should.nan? + @nan.truncate(+1).should.nan? + @nan.truncate(0).should.nan? + end - @infinity_negative.truncate(-1).should == @infinity_negative - @infinity_negative.truncate(+1).should == @infinity_negative - @infinity_negative.truncate(0).should == @infinity_negative - end + it "returns Infinity if self is infinite" do + @infinity.truncate(-1).should == @infinity + @infinity.truncate(+1).should == @infinity + @infinity.truncate(0).should == @infinity - it "returns the same value if self is special value" do - -> { @nan.truncate }.should raise_error(FloatDomainError) - -> { @infinity.truncate }.should raise_error(FloatDomainError) - -> { @infinity_negative.truncate }.should raise_error(FloatDomainError) + @infinity_negative.truncate(-1).should == @infinity_negative + @infinity_negative.truncate(+1).should == @infinity_negative + @infinity_negative.truncate(0).should == @infinity_negative + end + + it "returns the same value if self is special value" do + -> { @nan.truncate }.should raise_error(FloatDomainError) + -> { @infinity.truncate }.should raise_error(FloatDomainError) + -> { @infinity_negative.truncate }.should raise_error(FloatDomainError) + end end end |