File: to_s_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (145 lines) | stat: -rw-r--r-- 4,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require File.expand_path('../../../spec_helper', __FILE__)

describe "Float#to_s" do
  it "returns 'NaN' for NaN" do
    nan_value().to_s.should == 'NaN'
  end

  it "returns 'Infinity' for positive infinity" do
    infinity_value().to_s.should == 'Infinity'
  end

  it "returns '-Infinity' for negative infinity" do
    (-infinity_value()).to_s.should == '-Infinity'
  end

  it "returns '0.0' for 0.0" do
    0.0.to_s.should == "0.0"
  end

  platform_is_not :openbsd do
    it "emits '-' for -0.0" do
      -0.0.to_s.should == "-0.0"
    end
  end

  it "emits a '-' for negative values" do
    -3.14.to_s.should == "-3.14"
  end

  it "emits a trailing '.0' for a whole number" do
    50.0.to_s.should == "50.0"
  end

  it "emits a trailing '.0' for the mantissa in e format" do
    1.0e20.to_s.should == "1.0e+20"
  end

  it "uses non-e format for a positive value with fractional part having 5 significant figures" do
    0.0001.to_s.should == "0.0001"
  end

  it "uses non-e format for a negative value with fractional part having 5 significant figures" do
    -0.0001.to_s.should == "-0.0001"
  end

  it "uses e format for a positive value with fractional part having 6 significant figures" do
    0.00001.to_s.should == "1.0e-05"
  end

  it "uses e format for a negative value with fractional part having 6 significant figures" do
    -0.00001.to_s.should == "-1.0e-05"
  end

  it "uses non-e format for a positive value with whole part having 15 significant figures" do
    10000000000000.0.to_s.should == "10000000000000.0"
  end

  it "uses non-e format for a negative value with whole part having 15 significant figures" do
    -10000000000000.0.to_s.should == "-10000000000000.0"
  end

  ruby_version_is "" ... "1.9" do
    it "uses e format for a positive value with whole part having 16 significant figures" do
      100000000000000.0.to_s.should == "1.0e+14"
    end

    it "uses e format for a negative value with whole part having 16 significant figures" do
      -100000000000000.0.to_s.should == "-1.0e+14"
    end
  end

  ruby_version_is "1.9" do
    it "uses non-e format for a positive value with whole part having 16 significant figures" do
      100000000000000.0.to_s.should == "100000000000000.0"
    end

    it "uses non-e format for a negative value with whole part having 16 significant figures" do
      -100000000000000.0.to_s.should == "-100000000000000.0"
    end

    it "uses e format for a positive value with whole part having 18 significant figures" do
      10000000000000000.0.to_s.should == "1.0e+16"
    end

    it "uses e format for a negative value with whole part having 18 significant figures" do
      -10000000000000000.0.to_s.should == "-1.0e+16"
    end
  end

  ruby_version_is "1.9"..."2.0" do
    it "uses non-e format for a positive value with whole part having 17 significant figures" do
      1000000000000000.0.to_s.should == "1000000000000000.0"
    end

    it "uses non-e format for a negative value with whole part having 17 significant figures" do
      -1000000000000000.0.to_s.should == "-1000000000000000.0"
    end
  end

  ruby_version_is "2.0" do
    it "uses non-e format for a positive value with whole part having 17 significant figures" do
      1000000000000000.0.to_s.should == "1.0e+15"
    end

    it "uses non-e format for a negative value with whole part having 17 significant figures" do
      -1000000000000000.0.to_s.should == "-1.0e+15"
    end
  end

  ruby_bug "#3273", "1.8.7" do
    it "outputs the minimal, unique form necessary to recreate the value" do
      value = 0.21611564636388508
      string = "0.21611564636388508"

      value.to_s.should == string
      string.to_f.should == value
    end
  end

  it "outputs the minimal, unique form to represent the value" do
    0.56.to_s.should == "0.56"
  end
end

with_feature :encoding do
  describe "Float#to_s" do
    before :each do
      @internal = Encoding.default_internal
    end

    after :each do
      Encoding.default_internal = @internal
    end

    it "returns a String in US-ASCII encoding when Encoding.default_internal is nil" do
      Encoding.default_internal = nil
      1.23.to_s.encoding.should equal(Encoding::US_ASCII)
    end

    it "returns a String in US-ASCII encoding when Encoding.default_internal is not nil" do
      Encoding.default_internal = Encoding::IBM437
      5.47.to_s.encoding.should equal(Encoding::US_ASCII)
    end
  end
end