diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/bigdecimal/to_s_spec.rb | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
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
Diffstat (limited to 'spec/rubyspec/library/bigdecimal/to_s_spec.rb')
-rw-r--r-- | spec/rubyspec/library/bigdecimal/to_s_spec.rb | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/spec/rubyspec/library/bigdecimal/to_s_spec.rb b/spec/rubyspec/library/bigdecimal/to_s_spec.rb new file mode 100644 index 0000000000..b97311c800 --- /dev/null +++ b/spec/rubyspec/library/bigdecimal/to_s_spec.rb @@ -0,0 +1,73 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'bigdecimal' + +describe "BigDecimal#to_s" do + + before :each do + @bigdec_str = "3.14159265358979323846264338327950288419716939937" + @bigneg_str = "-3.1415926535897932384626433832795028841971693993" + @bigdec = BigDecimal(@bigdec_str) + @bigneg = BigDecimal(@bigneg_str) + end + + it "return type is of class String" do + @bigdec.to_s.kind_of?(String).should == true + @bigneg.to_s.kind_of?(String).should == true + end + + it "the default format looks like 0.xxxxEnn" do + @bigdec.to_s.should =~ /^0\.[0-9]*E[0-9]*$/i + end + + it "takes an optional argument" do + lambda {@bigdec.to_s("F")}.should_not raise_error() + end + + it "starts with + if + is supplied and value is positive" do + @bigdec.to_s("+").should =~ /^\+.*/ + @bigneg.to_s("+").should_not =~ /^\+.*/ + end + + it "inserts a space every n chars, if integer n is supplied" do + re =\ + /\A0\.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1\z/i + @bigdec.to_s(3).should =~ re + + str1 = '-123.45678 90123 45678 9' + BigDecimal.new("-123.45678901234567890").to_s('5F').should == str1 + # trailing zeroes removed + BigDecimal.new("1.00000000000").to_s('1F').should == "1.0" + # 0 is treated as no spaces + BigDecimal.new("1.2345").to_s('0F').should == "1.2345" + end + + it "can return a leading space for values > 0" do + @bigdec.to_s(" F").should =~ /\ .*/ + @bigneg.to_s(" F").should_not =~ /\ .*/ + 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.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" + end + + it "can use engineering notation" do + @bigdec.to_s("E").should =~ /^0\.[0-9]*E[0-9]*$/i + end + + it "can use conventional floating point notation" 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 + end + +end + |