summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/to_i_spec.rb
blob: a37be477783636bf6e282b8dae08423f78f2956a (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "String#to_i" do
  it "returns 0 for strings with leading underscores" do
    "_123".to_i.should == 0
  end

  it "ignores underscores in between the digits" do
    "1_2_3asdf".to_i.should == 123
  end

  it "ignores leading whitespaces" do
    [ " 123", "     123", "\r\n\r\n123", "\t\t123",
      "\r\n\t\n123", " \t\n\r\t 123"].each do |str|
      str.to_i.should == 123
    end
  end

  it "ignores subsequent invalid characters" do
    "123asdf".to_i.should == 123
    "123#123".to_i.should == 123
    "123 456".to_i.should == 123
  end

  it "returns 0 if self is no valid integer-representation" do
    [ "++2", "+-2", "--2" ].each do |str|
      str.to_i.should == 0
    end
  end

  it "accepts '+' at the beginning of a String" do
    "+0d56".to_i.should == 56
  end

  it "interprets leading characters as a number in the given base" do
    "100110010010".to_i(2).should == 0b100110010010
    "100110201001".to_i(3).should == 186409
    "103110201001".to_i(4).should == 5064769
    "103110241001".to_i(5).should == 55165126
    "153110241001".to_i(6).should == 697341529
    "153160241001".to_i(7).should == 3521513430
    "153160241701".to_i(8).should == 14390739905
    "853160241701".to_i(9).should == 269716550518
    "853160241791".to_i(10).should == 853160241791

    "F00D_BE_1337".to_i(16).should == 0xF00D_BE_1337
    "-hello_world".to_i(32).should == -18306744
    "abcXYZ".to_i(36).should == 623741435

    ("z" * 24).to_i(36).should == 22452257707354557240087211123792674815

    "5e10".to_i.should == 5
  end

  it "auto-detects base 8 via leading 0 when base = 0" do
    "01778".to_i(0).should == 0177
    "-01778".to_i(0).should == -0177
  end

  it "auto-detects base 2 via 0b when base = 0" do
    "0b112".to_i(0).should == 0b11
    "-0b112".to_i(0).should == -0b11
  end

  it "auto-detects base 10 via 0d when base = 0" do
    "0d19A".to_i(0).should == 19
    "-0d19A".to_i(0).should == -19
  end

  it "auto-detects base 8 via 0o when base = 0" do
    "0o178".to_i(0).should == 0o17
    "-0o178".to_i(0).should == -0o17
  end

  it "auto-detects base 16 via 0x when base = 0" do
    "0xFAZ".to_i(0).should == 0xFA
    "-0xFAZ".to_i(0).should == -0xFA
  end

  it "auto-detects base 10 with no base specifier when base = 0" do
    "1234567890ABC".to_i(0).should == 1234567890
    "-1234567890ABC".to_i(0).should == -1234567890
  end

  it "doesn't handle foreign base specifiers when base is > 0" do
    [2, 3, 4, 8, 10].each do |base|
      "0111".to_i(base).should == "111".to_i(base)

      "0b11".to_i(base).should == (base ==  2 ? 0b11 : 0)
      "0d11".to_i(base).should == (base == 10 ? 0d11 : 0)
      "0o11".to_i(base).should == (base ==  8 ? 0o11 : 0)
      "0xFA".to_i(base).should == 0
    end

    "0xD00D".to_i(16).should == 0xD00D

    "0b11".to_i(16).should == 0xb11
    "0d11".to_i(16).should == 0xd11
    "0o11".to_i(25).should == 15026
    "0x11".to_i(34).should == 38183

    "0B11".to_i(16).should == 0xb11
    "0D11".to_i(16).should == 0xd11
    "0O11".to_i(25).should == 15026
    "0X11".to_i(34).should == 38183
  end

  it "tries to convert the base to an integer using to_int" do
    obj = mock('8')
    obj.should_receive(:to_int).and_return(8)

    "777".to_i(obj).should == 0777
  end

  it "requires that the sign if any appears before the base specifier" do
    "0b-1".to_i( 2).should == 0
    "0d-1".to_i(10).should == 0
    "0o-1".to_i( 8).should == 0
    "0x-1".to_i(16).should == 0

    "0b-1".to_i(2).should == 0
    "0o-1".to_i(8).should == 0
    "0d-1".to_i(10).should == 0
    "0x-1".to_i(16).should == 0
  end

  it "raises an ArgumentError for illegal bases (1, < 0 or > 36)" do
    -> { "".to_i(1)  }.should raise_error(ArgumentError)
    -> { "".to_i(-1) }.should raise_error(ArgumentError)
    -> { "".to_i(37) }.should raise_error(ArgumentError)
  end

  it "returns a Fixnum for long strings with trailing spaces" do
    "0                             ".to_i.should == 0
    "0                             ".to_i.should be_an_instance_of(Fixnum)

    "10                             ".to_i.should == 10
    "10                             ".to_i.should be_an_instance_of(Fixnum)

    "-10                            ".to_i.should == -10
    "-10                            ".to_i.should be_an_instance_of(Fixnum)
  end

  it "returns a Fixnum for long strings with leading spaces" do
    "                             0".to_i.should == 0
    "                             0".to_i.should be_an_instance_of(Fixnum)

    "                             10".to_i.should == 10
    "                             10".to_i.should be_an_instance_of(Fixnum)

    "                            -10".to_i.should == -10
    "                            -10".to_i.should be_an_instance_of(Fixnum)
  end

  it "returns the correct Bignum for long strings" do
    "245789127594125924165923648312749312749327482".to_i.should == 245789127594125924165923648312749312749327482
    "-245789127594125924165923648312749312749327482".to_i.should == -245789127594125924165923648312749312749327482
  end
end

describe "String#to_i with bases" do
  it "parses a String in base 2" do
    str = "10" * 50
    str.to_i(2).to_s(2).should == str
  end

  it "parses a String in base 3" do
    str = "120" * 33
    str.to_i(3).to_s(3).should == str
  end

  it "parses a String in base 4" do
    str = "1230" * 25
    str.to_i(4).to_s(4).should == str
  end

  it "parses a String in base 5" do
    str = "12340" * 20
    str.to_i(5).to_s(5).should == str
  end

  it "parses a String in base 6" do
    str = "123450" * 16
    str.to_i(6).to_s(6).should == str
  end

  it "parses a String in base 7" do
    str = "1234560" * 14
    str.to_i(7).to_s(7).should == str
  end

  it "parses a String in base 8" do
    str = "12345670" * 12
    str.to_i(8).to_s(8).should == str
  end

  it "parses a String in base 9" do
    str = "123456780" * 11
    str.to_i(9).to_s(9).should == str
  end

  it "parses a String in base 10" do
    str = "1234567890" * 10
    str.to_i(10).to_s(10).should == str
  end

  it "parses a String in base 11" do
    str = "1234567890a" * 9
    str.to_i(11).to_s(11).should == str
  end

  it "parses a String in base 12" do
    str = "1234567890ab" * 8
    str.to_i(12).to_s(12).should == str
  end

  it "parses a String in base 13" do
    str = "1234567890abc" * 7
    str.to_i(13).to_s(13).should == str
  end

  it "parses a String in base 14" do
    str = "1234567890abcd" * 7
    str.to_i(14).to_s(14).should == str
  end

  it "parses a String in base 15" do
    str = "1234567890abcde" * 6
    str.to_i(15).to_s(15).should == str
  end

  it "parses a String in base 16" do
    str = "1234567890abcdef" * 6
    str.to_i(16).to_s(16).should == str
  end

  it "parses a String in base 17" do
    str = "1234567890abcdefg" * 5
    str.to_i(17).to_s(17).should == str
  end

  it "parses a String in base 18" do
    str = "1234567890abcdefgh" * 5
    str.to_i(18).to_s(18).should == str
  end

  it "parses a String in base 19" do
    str = "1234567890abcdefghi" * 5
    str.to_i(19).to_s(19).should == str
  end

  it "parses a String in base 20" do
    str = "1234567890abcdefghij" * 5
    str.to_i(20).to_s(20).should == str
  end

  it "parses a String in base 21" do
    str = "1234567890abcdefghijk" * 4
    str.to_i(21).to_s(21).should == str
  end

  it "parses a String in base 22" do
    str = "1234567890abcdefghijkl" * 4
    str.to_i(22).to_s(22).should == str
  end

  it "parses a String in base 23" do
    str = "1234567890abcdefghijklm" * 4
    str.to_i(23).to_s(23).should == str
  end

  it "parses a String in base 24" do
    str = "1234567890abcdefghijklmn" * 4
    str.to_i(24).to_s(24).should == str
  end

  it "parses a String in base 25" do
    str = "1234567890abcdefghijklmno" * 4
    str.to_i(25).to_s(25).should == str
  end

  it "parses a String in base 26" do
    str = "1234567890abcdefghijklmnop" * 3
    str.to_i(26).to_s(26).should == str
  end

  it "parses a String in base 27" do
    str = "1234567890abcdefghijklmnopq" * 3
    str.to_i(27).to_s(27).should == str
  end

  it "parses a String in base 28" do
    str = "1234567890abcdefghijklmnopqr" * 3
    str.to_i(28).to_s(28).should == str
  end

  it "parses a String in base 29" do
    str = "1234567890abcdefghijklmnopqrs" * 3
    str.to_i(29).to_s(29).should == str
  end

  it "parses a String in base 30" do
    str = "1234567890abcdefghijklmnopqrst" * 3
    str.to_i(30).to_s(30).should == str
  end

  it "parses a String in base 31" do
    str = "1234567890abcdefghijklmnopqrstu" * 3
    str.to_i(31).to_s(31).should == str
  end

  it "parses a String in base 32" do
    str = "1234567890abcdefghijklmnopqrstuv" * 3
    str.to_i(32).to_s(32).should == str
  end

  it "parses a String in base 33" do
    str = "1234567890abcdefghijklmnopqrstuvw" * 3
    str.to_i(33).to_s(33).should == str
  end

  it "parses a String in base 34" do
    str = "1234567890abcdefghijklmnopqrstuvwx" * 2
    str.to_i(34).to_s(34).should == str
  end

  it "parses a String in base 35" do
    str = "1234567890abcdefghijklmnopqrstuvwxy" * 2
    str.to_i(35).to_s(35).should == str
  end

  it "parses a String in base 36" do
    str = "1234567890abcdefghijklmnopqrstuvwxyz" * 2
    str.to_i(36).to_s(36).should == str
  end
end