diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-04-28 19:50:06 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-04-28 19:50:06 +0000 |
commit | 4fbb9aa3cb6c31ec128bfb31f59efa66d66adba4 (patch) | |
tree | 84a654b260261fe172f2584f60b3ba93e59f841d /spec/ruby/library/bigdecimal | |
parent | b864bd05bff2a61d55b08deb92e969f9fa55e07c (diff) |
Update to ruby/spec@6f38a82
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/bigdecimal')
-rw-r--r-- | spec/ruby/library/bigdecimal/BigDecimal_spec.rb | 109 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/divmod_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/inspect_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/limit_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/new_spec.rb | 109 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/shared/eql.rb | 4 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/shared/modulo.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/to_r_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/to_s_spec.rb | 24 |
9 files changed, 141 insertions, 131 deletions
diff --git a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb new file mode 100644 index 0000000000..d278512737 --- /dev/null +++ b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb @@ -0,0 +1,109 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "Kernel#BigDecimal" do + + it "creates a new object of class BigDecimal" do + BigDecimal("3.14159").should be_kind_of(BigDecimal) + (0..9).each {|i| + BigDecimal("1#{i}").should == 10 + i + BigDecimal("-1#{i}").should == -10 - i + BigDecimal("1E#{i}").should == 10**i + BigDecimal("1000000E-#{i}").should == 10**(6-i).to_f + # ^ to_f to avoid Rational type + } + (1..9).each {|i| + BigDecimal("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i + BigDecimal("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i + } + end + + it "accepts significant digits >= given precision" do + BigDecimal("3.1415923", 10).precs[1].should >= 10 + end + + it "determines precision from initial value" do + pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043" + BigDecimal(pi_string).precs[1].should >= pi_string.size-1 + end + + it "ignores leading whitespace" do + BigDecimal(" \t\n \r1234").should == BigDecimal("1234") + BigDecimal(" \t\n \rNaN \n").nan?.should == true + BigDecimal(" \t\n \rInfinity \n").infinite?.should == 1 + BigDecimal(" \t\n \r-Infinity \n").infinite?.should == -1 + end + + it "ignores trailing garbage" do + BigDecimal("123E45ruby").should == BigDecimal("123E45") + BigDecimal("123x45").should == BigDecimal("123") + BigDecimal("123.4%E5").should == BigDecimal("123.4") + BigDecimal("1E2E3E4E5E").should == BigDecimal("100") + end + + ruby_version_is ""..."2.4" do + it "treats invalid strings as 0.0" do + BigDecimal("ruby").should == BigDecimal("0.0") + BigDecimal(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal("0.0") + end + end + + ruby_version_is "2.4" do + it "raises ArgumentError for invalid strings" do + lambda { BigDecimal("ruby") }.should raise_error(ArgumentError) + lambda { BigDecimal(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError) + end + end + + it "allows omitting the integer part" do + BigDecimal(".123").should == BigDecimal("0.123") + end + + it "allows for underscores in all parts" do + reference = BigDecimal("12345.67E89") + + BigDecimal("12_345.67E89").should == reference + BigDecimal("1_2_3_4_5_._6____7_E89").should == reference + BigDecimal("12345_.67E_8__9_").should == reference + end + + it "accepts NaN and [+-]Infinity" do + BigDecimal("NaN").nan?.should == true + + pos_inf = BigDecimal("Infinity") + pos_inf.finite?.should == false + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal("-Infinity") + neg_inf.finite?.should == false + neg_inf.should < 0 + end + + it "allows for [eEdD] as exponent separator" do + reference = BigDecimal("12345.67E89") + + BigDecimal("12345.67e89").should == reference + BigDecimal("12345.67E89").should == reference + BigDecimal("12345.67d89").should == reference + BigDecimal("12345.67D89").should == reference + end + + it "allows for varying signs" do + reference = BigDecimal("123.456E1") + + BigDecimal("+123.456E1").should == reference + BigDecimal("-123.456E1").should == -reference + BigDecimal("123.456E+1").should == reference + BigDecimal("12345.6E-1").should == reference + BigDecimal("+123.456E+1").should == reference + BigDecimal("+12345.6E-1").should == reference + BigDecimal("-123.456E+1").should == -reference + BigDecimal("-12345.6E-1").should == -reference + end + + it 'raises ArgumentError when Float is used without precision' do + lambda { BigDecimal(1.0) }.should raise_error(ArgumentError) + end + +end diff --git a/spec/ruby/library/bigdecimal/divmod_spec.rb b/spec/ruby/library/bigdecimal/divmod_spec.rb index a93e0836bf..22b66c2d48 100644 --- a/spec/ruby/library/bigdecimal/divmod_spec.rb +++ b/spec/ruby/library/bigdecimal/divmod_spec.rb @@ -36,7 +36,7 @@ describe "BigDecimal#mod_part_of_divmod" do it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod it "raises ZeroDivisionError if other is zero" do - bd5667 = BigDecimal.new("5667.19") + bd5667 = BigDecimal("5667.19") lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError) lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError) @@ -96,8 +96,8 @@ describe "BigDecimal#divmod" do it "can be reversed with * and +" do # Example taken from BigDecimal documentation - a = BigDecimal.new("42") - b = BigDecimal.new("9") + a = BigDecimal("42") + b = BigDecimal("9") q, m = a.divmod(b) c = q * b + m a.should == c diff --git a/spec/ruby/library/bigdecimal/inspect_spec.rb b/spec/ruby/library/bigdecimal/inspect_spec.rb index 0873e8bef6..7e1a8297e2 100644 --- a/spec/ruby/library/bigdecimal/inspect_spec.rb +++ b/spec/ruby/library/bigdecimal/inspect_spec.rb @@ -4,7 +4,7 @@ require 'bigdecimal' describe "BigDecimal#inspect" do before :each do - @bigdec = BigDecimal.new("1234.5678") + @bigdec = BigDecimal("1234.5678") end it "returns String" do diff --git a/spec/ruby/library/bigdecimal/limit_spec.rb b/spec/ruby/library/bigdecimal/limit_spec.rb index 7fd395dd50..75cbc8b55c 100644 --- a/spec/ruby/library/bigdecimal/limit_spec.rb +++ b/spec/ruby/library/bigdecimal/limit_spec.rb @@ -42,4 +42,14 @@ describe "BigDecimal.limit" do BigDecimal('0.888').div(BigDecimal('3'), 2).should == BigDecimal('0.30') end end + + it "picks the global precision when limit 0 specified" do + BigDecimalSpecs.with_limit(3) do + BigDecimal('0.8888').add(BigDecimal('0'), 0).should == BigDecimal('0.889') + BigDecimal('0.8888').sub(BigDecimal('0'), 0).should == BigDecimal('0.889') + BigDecimal('0.888').mult(BigDecimal('3'), 0).should == BigDecimal('2.66') + BigDecimal('0.8888').div(BigDecimal('3'), 0).should == BigDecimal('0.296') + end + end + end diff --git a/spec/ruby/library/bigdecimal/new_spec.rb b/spec/ruby/library/bigdecimal/new_spec.rb deleted file mode 100644 index 64f2a7f468..0000000000 --- a/spec/ruby/library/bigdecimal/new_spec.rb +++ /dev/null @@ -1,109 +0,0 @@ -require_relative '../../spec_helper' -require 'bigdecimal' - -describe "BigDecimal.new" do - - it "creates a new object of class BigDecimal" do - BigDecimal.new("3.14159").should be_kind_of(BigDecimal) - (0..9).each {|i| - BigDecimal.new("1#{i}").should == 10 + i - BigDecimal.new("-1#{i}").should == -10 - i - BigDecimal.new("1E#{i}").should == 10**i - BigDecimal.new("1000000E-#{i}").should == 10**(6-i).to_f - # ^ to_f to avoid Rational type - } - (1..9).each {|i| - BigDecimal.new("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i - BigDecimal.new("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i - } - end - - it "accepts significant digits >= given precision" do - BigDecimal.new("3.1415923", 10).precs[1].should >= 10 - end - - it "determines precision from initial value" do - pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043" - BigDecimal.new(pi_string).precs[1].should >= pi_string.size-1 - end - - it "ignores leading whitespace" do - BigDecimal.new(" \t\n \r1234").should == BigDecimal.new("1234") - BigDecimal.new(" \t\n \rNaN \n").nan?.should == true - BigDecimal.new(" \t\n \rInfinity \n").infinite?.should == 1 - BigDecimal.new(" \t\n \r-Infinity \n").infinite?.should == -1 - end - - it "ignores trailing garbage" do - BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45") - BigDecimal.new("123x45").should == BigDecimal.new("123") - BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4") - BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100") - end - - ruby_version_is ""..."2.4" do - it "treats invalid strings as 0.0" do - BigDecimal.new("ruby").should == BigDecimal.new("0.0") - BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal.new("0.0") - end - end - - ruby_version_is "2.4" do - it "raises ArgumentError for invalid strings" do - lambda { BigDecimal.new("ruby") }.should raise_error(ArgumentError) - lambda { BigDecimal.new(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError) - end - end - - it "allows omitting the integer part" do - BigDecimal.new(".123").should == BigDecimal.new("0.123") - end - - it "allows for underscores in all parts" do - reference = BigDecimal.new("12345.67E89") - - BigDecimal.new("12_345.67E89").should == reference - BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference - BigDecimal.new("12345_.67E_8__9_").should == reference - end - - it "accepts NaN and [+-]Infinity" do - BigDecimal.new("NaN").nan?.should == true - - pos_inf = BigDecimal.new("Infinity") - pos_inf.finite?.should == false - pos_inf.should > 0 - pos_inf.should == BigDecimal.new("+Infinity") - - neg_inf = BigDecimal.new("-Infinity") - neg_inf.finite?.should == false - neg_inf.should < 0 - end - - it "allows for [eEdD] as exponent separator" do - reference = BigDecimal.new("12345.67E89") - - BigDecimal.new("12345.67e89").should == reference - BigDecimal.new("12345.67E89").should == reference - BigDecimal.new("12345.67d89").should == reference - BigDecimal.new("12345.67D89").should == reference - end - - it "allows for varying signs" do - reference = BigDecimal.new("123.456E1") - - BigDecimal.new("+123.456E1").should == reference - BigDecimal.new("-123.456E1").should == -reference - BigDecimal.new("123.456E+1").should == reference - BigDecimal.new("12345.6E-1").should == reference - BigDecimal.new("+123.456E+1").should == reference - BigDecimal.new("+12345.6E-1").should == reference - BigDecimal.new("-123.456E+1").should == -reference - BigDecimal.new("-12345.6E-1").should == -reference - end - - it 'raises ArgumentError when Float is used without precision' do - lambda { BigDecimal(1.0) }.should raise_error(ArgumentError) - end - -end diff --git a/spec/ruby/library/bigdecimal/shared/eql.rb b/spec/ruby/library/bigdecimal/shared/eql.rb index eaad272e9a..8e3e388bab 100644 --- a/spec/ruby/library/bigdecimal/shared/eql.rb +++ b/spec/ruby/library/bigdecimal/shared/eql.rb @@ -2,8 +2,8 @@ require 'bigdecimal' describe :bigdecimal_eql, shared: true do before :each do - @bg6543_21 = BigDecimal.new("6543.21") - @bg5667_19 = BigDecimal.new("5667.19") + @bg6543_21 = BigDecimal("6543.21") + @bg5667_19 = BigDecimal("5667.19") @a = BigDecimal("1.0000000000000000000000000000000000000000005") @b = BigDecimal("1.00000000000000000000000000000000000000000005") @bigint = BigDecimal("1000.0") diff --git a/spec/ruby/library/bigdecimal/shared/modulo.rb b/spec/ruby/library/bigdecimal/shared/modulo.rb index 78ebe8360d..c9691cbf37 100644 --- a/spec/ruby/library/bigdecimal/shared/modulo.rb +++ b/spec/ruby/library/bigdecimal/shared/modulo.rb @@ -18,8 +18,8 @@ describe :bigdecimal_modulo, shared: true do end it "returns self modulo other" do - bd6543 = BigDecimal.new("6543.21") - bd5667 = BigDecimal.new("5667.19") + bd6543 = BigDecimal("6543.21") + bd5667 = BigDecimal("5667.19") a = BigDecimal("1.0000000000000000000000000000000000000000005") b = BigDecimal("1.00000000000000000000000000000000000000000005") @@ -107,7 +107,7 @@ end describe :bigdecimal_modulo_zerodivisionerror, shared: true do it "raises ZeroDivisionError if other is zero" do - bd5667 = BigDecimal.new("5667.19") + bd5667 = BigDecimal("5667.19") lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError) lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError) diff --git a/spec/ruby/library/bigdecimal/to_r_spec.rb b/spec/ruby/library/bigdecimal/to_r_spec.rb index e2956782bd..91d2b33993 100644 --- a/spec/ruby/library/bigdecimal/to_r_spec.rb +++ b/spec/ruby/library/bigdecimal/to_r_spec.rb @@ -8,7 +8,7 @@ describe "BigDecimal#to_r" do end it "returns a Rational with bignum values" do - r = BigDecimal.new("3.141592653589793238462643").to_r + r = BigDecimal("3.141592653589793238462643").to_r r.numerator.should eql(3141592653589793238462643) r.denominator.should eql(1000000000000000000000000) end diff --git a/spec/ruby/library/bigdecimal/to_s_spec.rb b/spec/ruby/library/bigdecimal/to_s_spec.rb index 86716c93fe..86ae1e61f6 100644 --- a/spec/ruby/library/bigdecimal/to_s_spec.rb +++ b/spec/ruby/library/bigdecimal/to_s_spec.rb @@ -34,11 +34,11 @@ describe "BigDecimal#to_s" do @bigdec.to_s(3).should =~ re str1 = '-123.45678 90123 45678 9' - BigDecimal.new("-123.45678901234567890").to_s('5F').should == str1 + BigDecimal("-123.45678901234567890").to_s('5F').should == str1 # trailing zeroes removed - BigDecimal.new("1.00000000000").to_s('1F').should == "1.0" + BigDecimal("1.00000000000").to_s('1F').should == "1.0" # 0 is treated as no spaces - BigDecimal.new("1.2345").to_s('0F').should == "1.2345" + BigDecimal("1.2345").to_s('0F').should == "1.2345" end it "can return a leading space for values > 0" do @@ -47,15 +47,15 @@ describe "BigDecimal#to_s" do end it "removes trailing spaces in floating point notation" do - BigDecimal.new('-123.45678901234567890').to_s('F').should == "-123.4567890123456789" - BigDecimal.new('1.2500').to_s('F').should == "1.25" - BigDecimal.new('0000.00000').to_s('F').should == "0.0" - BigDecimal.new('-00.000010000').to_s('F').should == "-0.00001" - BigDecimal.new("5.00000E-2").to_s("F").should == "0.05" + BigDecimal('-123.45678901234567890').to_s('F').should == "-123.4567890123456789" + BigDecimal('1.2500').to_s('F').should == "1.25" + BigDecimal('0000.00000').to_s('F').should == "0.0" + BigDecimal('-00.000010000').to_s('F').should == "-0.00001" + BigDecimal("5.00000E-2").to_s("F").should == "0.05" - BigDecimal.new("500000").to_s("F").should == "500000.0" - BigDecimal.new("5E2").to_s("F").should == "500.0" - BigDecimal.new("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0" + BigDecimal("500000").to_s("F").should == "500000.0" + BigDecimal("5E2").to_s("F").should == "500.0" + BigDecimal("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0" end it "can use engineering notation" do @@ -66,7 +66,7 @@ describe "BigDecimal#to_s" do @bigdec.to_s("F").should == @bigdec_str @bigneg.to_s("F").should == @bigneg_str str2 = "+123.45678901 23456789" - BigDecimal.new('123.45678901234567890').to_s('+8F').should == str2 + BigDecimal('123.45678901234567890').to_s('+8F').should == str2 end end |