forked from rubinius/rubinius
/
string_spec.rb
394 lines (326 loc) · 9.94 KB
/
string_spec.rb
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require File.dirname(__FILE__) + '/spec_helper'
load_extension('string')
describe "C-API String function" do
before :each do
@s = CApiStringSpecs.new
end
class ValidTostrTest
def to_str
"ruby"
end
end
class InvalidTostrTest
def to_str
[]
end
end
describe "rb_str_new" do
it "returns a new string object from a char buffer of len characters" do
@s.rb_str_new("hello", 3).should == "hel"
end
it "returns an empty string if len is 0" do
@s.rb_str_new("hello", 0).should == ""
end
end
describe "rb_str_new2" do
it "returns a new string object calling strlen on the passed C string" do
# Hardcoded to pass const char * = "hello\0invisible"
@s.rb_str_new2.should == "hello"
end
it "rb_str_new2 should raise ArgumentError if passed NULL" do
lambda { @s.rb_str_new2_with_null }.should raise_error(ArgumentError)
end
end
describe "rb_str_dup" do
it "returns a copy of the string" do
str1 = "hi"
str2 = @s.rb_str_dup str1
str1.should == str2
str1.object_id.should_not == str2.object_id
end
end
describe "rb_str_append" do
it "appends a string to another string" do
@s.rb_str_append("Hello", " Goodbye").should == "Hello Goodbye"
end
end
describe "rb_str_plus" do
it "returns a new string from concatenating two other strings" do
@s.rb_str_plus("Hello", " Goodbye").should == "Hello Goodbye"
end
end
describe "rb_str_buf_cat" do
it "concatenates a C string to a ruby string" do
@s.rb_str_buf_cat("Your house is on fire").should == "Your house is on fire?"
end
end
describe "rb_str_cat" do
it "concatenates a C string to ruby string" do
@s.rb_str_cat("Your house is on fire").should == "Your house is on fire?"
end
end
describe "rb_str_cat2" do
it "concatenates a C string to a ruby string" do
@s.rb_str_cat2("Your house is on fire").should == "Your house is on fire?"
end
end
describe "rb_str_cmp" do
it "returns 0 if two strings are identical" do
@s.rb_str_cmp("ppp", "ppp").should == 0
end
it "returns -1 if the first string is shorter than the second" do
@s.rb_str_cmp("xxx", "xxxx").should == -1
end
it "returns -1 if the first string is lexically less than the second" do
@s.rb_str_cmp("xxx", "yyy").should == -1
end
it "returns 1 if the first string is longer than the second" do
@s.rb_str_cmp("xxxx", "xxx").should == 1
end
it "returns 1 if the first string is lexically greater than the second" do
@s.rb_str_cmp("yyy", "xxx").should == 1
end
end
describe "rb_str_split" do
it "splits strings over a splitter" do
@s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"]
end
end
describe "rb_str2inum" do
it "converts a string to a number given a base" do
@s.rb_str2inum("10", 10).should == 10
@s.rb_str2inum("A", 16).should == 10
end
end
describe "rb_cstr2inum" do
it "converts a C string to a number given a base" do
@s.rb_cstr2inum("10", 10).should == 10
@s.rb_cstr2inum("10", 16).should == 16
end
end
describe "rb_str_substr" do
it "returns a substring" do
"hello".length.times do |time|
@s.rb_str_substr("hello", 0, time + 1).should == "hello"[0..time]
end
end
end
describe "rb_str_to_str" do
it "calls #to_str to coerce the value to a String" do
@s.rb_str_to_str("foo").should == "foo"
@s.rb_str_to_str(ValidTostrTest.new).should == "ruby"
end
it "raises a TypeError if coercion fails" do
lambda { @s.rb_str_to_str(0) }.should raise_error(TypeError)
lambda { @s.rb_str_to_str(InvalidTostrTest.new) }.should raise_error(TypeError)
end
end
describe "RSTRING" do
it "returns struct with a pointer to the string's contents" do
str = "xyz"
chars = []
@s.RSTRING_ptr_iterate(str) do |c|
chars << c
end
chars.should == [120, 121, 122]
end
it "allows changing the characters in the string" do
str = "abc"
@s.RSTRING_ptr_assign(str, 70)
str.should == "FFF"
end
it "allows changing the string and calling a rb_str_xxx function" do
str = "abc"
@s.RSTRING_ptr_assign_call(str)
str.should == "axcd"
end
it "allows changing the string and calling a method via rb_funcall" do
str = "abc"
@s.RSTRING_ptr_assign_funcall(str)
str.should == "axce"
end
it "returns a struct with the string's length" do
@s.RSTRING_len("dewdrops").should == 8
end
end
describe "RSTRING_PTR" do
it "returns a pointer to the string's contents" do
str = "abc"
chars = []
@s.RSTRING_PTR_iterate(str) do |c|
chars << c
end
chars.should == [97, 98, 99]
end
it "allows changing the characters in the string" do
str = "abc"
@s.RSTRING_PTR_assign(str, 65)
str.should == "AAA"
end
it "reflects changes after a rb_funcall" do
lamb = proc { |s| s.replace "NEW CONTENT" }
str = "beforebefore"
ret = @s.RSTRING_PTR_after_funcall(str, lamb)
str.should == "NEW CONTENT"
ret.should == str
end
end
describe "RSTRING_LEN" do
it "returns the size of the string" do
@s.RSTRING_LEN("gumdrops").should == 8
end
end
describe "StringValue" do
it "does not call #to_str on a String" do
str = "genuine"
str.should_not_receive(:to_str)
@s.StringValue(str)
end
it "does not call #to_s on a String" do
str = "genuine"
str.should_not_receive(:to_str)
@s.StringValue(str)
end
it "calls #to_str on non-String objects" do
str = mock("fake")
str.should_receive(:to_str).and_return("wannabe")
@s.StringValue(str)
end
it "does not call #to_s on non-String objects" do
str = mock("fake")
str.should_not_receive(:to_s)
lambda { @s.StringValue(str) }.should raise_error(TypeError)
end
end
describe "rb_str_resize" do
it "reduces the size of the string" do
str = @s.rb_str_resize("test", 2)
str.size.should == 2
@s.RSTRING_LEN(str).should == 2
str.should == "te"
end
it "updates the string's attributes visible in C code" do
@s.rb_str_resize_RSTRING_LEN("test", 2).should == 2
end
it "increases the size of the string" do
str = @s.rb_str_resize("test", 12)
str.size.should == 12
@s.RSTRING_LEN(str).should == 12
str[0, 5].should == "test\x00"
end
end
describe "rb_str_intern" do
it "returns a symbol created from the string" do
@s.rb_str_intern("symbol").should == :symbol
end
it "raises an ArgumentError if passed an empty string" do
lambda { @s.rb_str_intern("") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if the passed string contains NULL characters" do
lambda { @s.rb_str_intern("no\0no") }.should raise_error(ArgumentError)
end
end
describe "rb_str2cstr" do
before :each do
@verbose = $VERBOSE
@stderr, $stderr = $stderr, IOStub.new
end
after :each do
$stderr = @stderr
$VERBOSE = @verbose
end
it "returns a pointer to the string's content and its length" do
str, len = @s.rb_str2cstr('any str', true)
str.should == 'any str'
len.should == 7
end
it "allows changing the characters in the string" do
str = 'any str'
# Hardcoded to set "foo\0"
@s.rb_str2cstr_replace(str)
str.should == "foo\0str"
end
it "issues a warning iff passed string contains a NULL character, $VERBOSE = true and len parameter is NULL" do
$VERBOSE = false
@s.rb_str2cstr('any str', true)
$stderr.should == ''
@s.rb_str2cstr('any str', false)
$stderr.should == ''
$VERBOSE = true
@s.rb_str2cstr('any str', true)
$stderr.should == ''
@s.rb_str2cstr('any str', false)
$stderr.should == ''
$VERBOSE = false
@s.rb_str2cstr("any\0str", true)
$stderr.should == ''
@s.rb_str2cstr("any\0str", false)
$stderr.should == ''
$VERBOSE = true
@s.rb_str2cstr("any\0str", true)
$stderr.should == ''
@s.rb_str2cstr("any\0str", false)
$stderr.should =~ /string contains \\0 character/
end
end
describe "STR2CSTR" do
it "returns a pointer to the string's content" do
@s.STR2CSTR('any str').should == 'any str'
end
it "allows changing the characters in the string" do
str = 'any str'
# Hardcoded to set "foo\0"
@s.STR2CSTR_replace(str)
str.should == "foo\0str"
end
end
describe "rb_str_freeze" do
it "freezes the string" do
s = ""
@s.rb_str_freeze(s)
s.frozen?.should be_true
end
end
extended_on :rubinius do
describe "rb_str_ptr" do
it "returns struct with a pointer to the string's contents" do
str = "xyz"
chars = []
@s.rb_str_ptr_iterate(str) do |c|
chars << c
end
chars.should == [120, 121, 122]
end
it "allows changing the characters in the string" do
str = "abc"
@s.rb_str_ptr_assign(str, 70)
str.should == "FFF"
end
it "allows changing the string and calling a rb_str_xxx function" do
str = "abc"
@s.rb_str_ptr_assign_call(str)
str.should == "axcd"
end
it "allows changing the string and calling a method via rb_funcall" do
str = "abc"
@s.rb_str_ptr_assign_funcall(str)
str.should == "axce"
end
end
describe "rb_str_ptr_readonly" do
it "returns struct with a pointer to the string's contents" do
str = "xyz"
chars = []
@s.rb_str_ptr_readonly_iterate(str) do |c|
chars << c
end
chars.should == [120, 121, 122]
end
end
describe "rb_str_len" do
it "returns the string's length" do
@s.rb_str_len("dewdrops").should == 8
end
end
end
end