-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rb
More file actions
423 lines (371 loc) · 11.7 KB
/
Copy pathtest.rb
File metadata and controls
423 lines (371 loc) · 11.7 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# encoding: UTF-8
MACRUBY = defined?(MACRUBY_VERSION)
if MACRUBY
require 'new_string'
S = MRString
E = MREncoding
else
S = String
E = Encoding
end
UNICODE_ENCODINGS = [:UTF_8, :UTF_16BE, :UTF_16LE, :UTF_32BE, :UTF_32LE]
def getchar(str, i)
if MACRUBY
str.getchar(i)
else
str[i]
end
end
def chars_count(str)
if MACRUBY
str.chars_count
else
str.length
end
end
def utf16le(str)
if MACRUBY
str
else
str.encode(Encoding::UTF_16LE)
end
end
def read_data(name, enc_name)
enc_for_name = enc_name.to_s.gsub(/_/, '').downcase
file_name = File.join(File.dirname(__FILE__), "test_data/#{name}-#{enc_for_name}.txt")
data = nil
if MACRUBY
data = S.new(File.read(file_name))
else
File.open(file_name, 'r:BINARY') do |f|
data = f.read
end
end
enc = E.const_get(enc_name)
$current_encoding = enc
data.force_encoding(enc)
data
end
def called_line
begin
raise ''
rescue Exception => e
bt = e.backtrace
md = /:(\d+):/.match(bt[2])
return md ? md[1] : 0
end
end
$tests_done_count = 0
$tests_failed_count = 0
def assert_equal(wanted, got)
$tests_done_count += 1
wanted = S.new(wanted) if MACRUBY and wanted.instance_of?(NSMutableString)
got = S.new(got) if MACRUBY and got.instance_of?(NSMutableString)
if wanted != got
$tests_failed_count += 1
puts "test failed: #{wanted.inspect} != #{got.inspect} at line #{called_line} (encoding: #{$current_encoding.name})"
end
end
def assert_not_equal(not_wanted, got)
$tests_done_count += 1
wanted = S.new(wanted) if MACRUBY and wanted.instance_of?(NSMutableString)
got = S.new(got) if MACRUBY and got.instance_of?(NSMutableString)
if not_wanted == got
$tests_failed_count += 1
puts "test failed: #{not_wanted.inspect} == #{got.inspect} at line #{called_line} (encoding: #{$current_encoding.name})"
end
end
def assert_no_exception_raised
$tests_done_count += 1
begin
yield
rescue Exception
$tests_failed_count += 1
puts "test failed: exception raised at line #{called_line} (encoding: #{$current_encoding.name})"
end
end
def assert_exception_raised(exception)
$tests_done_count += 1
begin
yield
rescue exception
# we got the exception we wanted
else
$tests_failed_count += 1
puts "test failed: exception #{exception.name} not raised at line #{called_line} (encoding: #{$current_encoding.name})"
end
end
assert_equal nil, ''[0]
assert_equal nil, ''[0, -1]
assert_equal '', ''[0, 0]
assert_equal '', ''[0, 100]
assert_equal '', ''[0..100]
assert_equal '', ''[0..0]
assert_equal '', ''[0...0]
UNICODE_ENCODINGS.each do |enc|
data = read_data('ohayougozaimasu', enc)
assert_equal data, S.new(data)
assert_equal data, data.dup
assert_equal data, data.clone
assert_equal 9, data.length
assert_equal 9, data.chars_count if MACRUBY
data.length.times do |i|
c = data[i]
assert_equal data.encoding, c.encoding
assert_equal 1, c.length
assert_equal 1, c.chars_count if MACRUBY
assert_equal true, c.valid_encoding?
end
assert_equal '', data[1...1]
assert_equal '', data[9, 0]
assert_equal nil, data[10, 0]
assert_equal 1, data[0, 1].length
assert_equal 2, data[0, 2].length
if MACRUBY
assert_equal 1, data[0, 1].chars_count
assert_equal 2, data[0, 2].chars_count
end
assert_equal true, data.valid_encoding?
case enc
when :UTF_8
assert_equal 27, data.bytesize
when :UTF_16LE, :UTF_16BE
assert_equal 18, data.bytesize
when :UTF_32LE, :UTF_32BE
assert_equal 36, data.bytesize
end
double = data + data
assert_equal 0, double.index(data)
assert_equal 0, double.index(data, 0)
(1..9).each { |i| assert_equal 9, double.index(data, i) }
(10..double.length).each { |i| assert_equal nil, double.index(data, i) }
if enc == :UTF_16LE
assert_equal utf16le('お'), data[0]
assert_equal utf16le('お'), data[0, 1]
assert_equal utf16le('おは'), data[0, 2]
else
assert_not_equal utf16le('お'), data[0]
end
end
SURROGATE_UTF16_BYTES = [0xD8, 0x40, 0xDC, 0x0B]
UNICODE_ENCODINGS.each do |enc|
data = read_data('surrogate', enc)
assert_equal data, S.new(data)
assert_equal data, data.dup
assert_equal data, data.clone
if enc == :UTF_16LE or enc == :UTF_16BE
data.bytesize.times do |i|
if enc == :UTF_16LE
j = i.even? ? i+1 : i-1
else
j = i
end
assert_equal SURROGATE_UTF16_BYTES[j], data.getbyte(i)
end
end
if MACRUBY
assert_equal 2, data.length
data.length.times do |i|
if enc == :UTF_16LE or enc == :UTF_16BE
assert_no_exception_raised { data[i] }
c = data[i]
assert_not_equal nil, c
assert_equal 2, c.bytesize
assert_equal SURROGATE_UTF16_BYTES[i*2], c.getbyte(enc == :UTF_16BE ? 0 : 1)
assert_equal SURROGATE_UTF16_BYTES[i*2 + 1], c.getbyte(enc == :UTF_16BE ? 1 : 0)
assert_equal false, data[i].valid_encoding?
else
assert_exception_raised(IndexError) { data[i] }
end
end
end
assert_equal 1, chars_count(data)
assert_equal 4, getchar(data, 0).bytesize
assert_equal nil, getchar(data, 1)
assert_equal 4, data.bytesize
end
SURROGATE_WITH_INVALID_BYTES = [0x00, 0x02, 0x00, 0x0B, 0xFF, 0xFF, 0xFF, 0xFF]
[:UTF_32LE, :UTF_32BE].each do |enc|
data = read_data('surrogate_with_invalid', enc)
assert_equal data, S.new(data)
assert_equal data, data.dup
assert_equal data, data.clone
assert_equal 8, data.bytesize
data.bytesize.times do |i|
if enc == :UTF_32BE
j = i
else
case i%4
when 0
j = i + 3
when 1
j = i + 1
when 2
j = i - 1
when 3
j = i - 3
end
end
assert_equal SURROGATE_WITH_INVALID_BYTES[j], data.getbyte(i)
end
if MACRUBY
assert_equal 3, data.length
assert_exception_raised(IndexError) { data[0] }
assert_exception_raised(IndexError) { data[1] }
assert_no_exception_raised { data[2] }
end
assert_equal 2, chars_count(data)
end
UNICODE_ENCODINGS.each do |enc|
data = read_data('cut', enc)
assert_equal data, S.new(data)
assert_equal data, data.dup
assert_equal data, data.clone
assert_equal false, data.valid_encoding?
assert_equal false, data[-1].valid_encoding?
if enc == :UTF_8
assert_equal 10, data.length
assert_equal 10, data.chars_count if MACRUBY
[ [data[8], data[9]],
[data[8,1], data[9,1]],
[data[8..8], data[9..9]],
].each do |c1, c2|
assert_equal 1, c1.length
assert_equal 1, c1.bytesize
assert_equal 0xE3, c1.getbyte(0)
assert_equal 1, c2.length
assert_equal 1, c2.bytesize
assert_equal 0x81, c2.getbyte(0)
end
else
assert_equal 9, data.length
assert_equal 9, data.chars_count if MACRUBY
end
case enc
when :UTF_8
assert_equal 26, data.bytesize
when :UTF_16BE, :UTF_16LE
assert_equal 17, data.bytesize
c = data[-1]
assert_equal 1, c.bytesize
if enc == :UTF_16BE
assert_equal 0x30, c.getbyte(0)
else
assert_equal 0x59, c.getbyte(0)
end
when :UTF_32BE, :UTF_32LE
assert_equal 35, data.bytesize
end
end
[:UTF_16LE, :UTF_16BE].each do |enc|
data = read_data('inverted_surrogate_plus_surrogate', enc)
assert_equal data, S.new(data)
assert_equal data, data.dup
assert_equal data, data.clone
assert_equal 8, data.bytesize
assert_equal false, data.valid_encoding?
assert_equal 1, chars_count(data[1]+data[0])
if MACRUBY
assert_equal 4, data.length
assert_equal 2, data[0].bytesize
assert_equal 2, data[1].bytesize
assert_equal 2, data[2].bytesize
assert_equal 2, data[3].bytesize
assert_equal nil, data[4]
assert_equal nil, data[-5]
assert_equal 2, data[-4].bytesize
assert_equal 2, data[-3].bytesize
assert_equal 2, data[-2].bytesize
assert_equal 2, data[-1].bytesize
end
assert_equal 3, chars_count(data)
assert_equal 2, getchar(data, 0).bytesize
assert_equal 2, getchar(data, 1).bytesize
assert_equal 4, getchar(data, 2).bytesize
assert_equal nil, getchar(data, 3)
assert_equal nil, getchar(data, -4)
assert_equal 2, getchar(data, -3).bytesize
assert_equal 2, getchar(data, -2).bytesize
assert_equal 4, getchar(data, -1).bytesize
end
bonjour_ascii = read_data('bonjour', :ASCII)
bonjour_utf8 = read_data('bonjour', :ASCII).force_encoding(E::UTF_8)
bonjour_utf16le = read_data('bonjour', :UTF_16LE)
ohayou_utf8 = read_data('ohayougozaimasu', :UTF_8)
ohayou_utf16le = read_data('ohayougozaimasu', :UTF_16LE)
empty_utf8 = S.new.force_encoding(E::UTF_8)
empty_utf16le = S.new.force_encoding(E::UTF_16LE)
surrogate_utf16le = read_data('surrogate', :UTF_16LE)
assert_equal true, bonjour_ascii.ascii_only?
assert_equal true, bonjour_utf8.ascii_only?
assert_equal false, bonjour_utf16le.ascii_only?
assert_equal false, ohayou_utf8.ascii_only?
assert_equal false, ohayou_utf16le.ascii_only?
assert_equal E::US_ASCII, E.compatible?(bonjour_ascii, bonjour_ascii)
assert_equal E::US_ASCII, E.compatible?(bonjour_ascii, bonjour_utf8)
assert_equal E::UTF_8, E.compatible?(bonjour_utf8, bonjour_ascii)
assert_equal E::UTF_8, E.compatible?(bonjour_utf8, bonjour_utf8)
assert_equal E::UTF_8, E.compatible?(empty_utf8, empty_utf16le)
assert_equal E::UTF_8, E.compatible?(empty_utf16le, ohayou_utf8)
assert_equal E::UTF_8, E.compatible?(ohayou_utf8, empty_utf16le)
assert_equal E::UTF_16LE, E.compatible?(empty_utf8, ohayou_utf16le)
assert_equal E::UTF_16LE, E.compatible?(ohayou_utf16le, empty_utf8)
assert_equal nil, E.compatible?(bonjour_ascii, bonjour_utf16le)
assert_equal nil, E.compatible?(bonjour_utf16le, bonjour_ascii)
assert_equal nil, E.compatible?(ohayou_utf8, ohayou_utf16le)
assert_equal nil, E.compatible?(ohayou_utf16le, ohayou_utf8)
assert_equal nil, E.compatible?(ohayou_utf8, nil)
assert_equal nil, E.compatible?(ohayou_utf8, 1)
assert_equal true, empty_utf8.valid_encoding?
assert_equal true, bonjour_utf8.force_encoding(E::BINARY).ascii_only?
assert_equal false, ohayou_utf8.force_encoding(E::BINARY).ascii_only?
assert_equal S.new('ab'), S.new('a') + S.new('b')
assert_equal S.new('b'), S.new + S.new('b')
assert_equal S.new('a'), S.new('a') + S.new
assert_equal S.new, S.new + S.new
assert_exception_raised(TypeError) { S.new('a') + 2 }
ohayou_copy = ohayou_utf8.dup
assert_equal ohayou_utf8, ohayou_copy.replace(ohayou_copy)
assert_exception_raised(TypeError) { S.new.replace(2) }
s = empty_utf8.dup
s << empty_utf16le
assert_equal E::UTF_8, s.encoding
s = empty_utf8.dup
s << bonjour_utf16le
assert_equal E::UTF_16LE, s.encoding
s = S.new('a')
old_s = s.dup
s << S.new('')
assert_equal old_s, s
s << S.new('b')
assert_equal S.new('ab'), s
old_s = s.dup
s.concat(S.new('c'))
assert_equal S.new('abc'), s
assert_not_equal old_s, s
assert_equal empty_utf8, empty_utf16le
assert_equal bonjour_utf8, bonjour_ascii
assert_not_equal bonjour_utf16le, bonjour_ascii
assert_equal bonjour_ascii, (bonjour_ascii+bonjour_ascii)[1..-1][bonjour_ascii]
a = S.new('a')
assert_equal a, 'a'
assert_equal a, a['a']
assert_exception_raised(Encoding::CompatibilityError) { ohayou_utf8 + ohayou_utf16le }
assert_exception_raised(Encoding::CompatibilityError) { ohayou_utf8 << ohayou_utf16le }
assert_exception_raised(Encoding::CompatibilityError) { ohayou_utf8.index(ohayou_utf16le) }
assert_equal nil, empty_utf16le.index(ohayou_utf16le)
assert_equal 0, ohayou_utf16le.index(ohayou_utf16le)
assert_equal 0, ohayou_utf16le.index(empty_utf8)
assert_equal 6, bonjour_utf16le.index(utf16le('r'))
surrogate_ohayou_utf16 = surrogate_utf16le + ohayou_utf16le
if MACRUBY
assert_equal 2, surrogate_ohayou_utf16.index(ohayou_utf16le)
(0..2).each {|i| assert_equal 2, surrogate_ohayou_utf16.index(ohayou_utf16le, i) }
else
assert_equal 1, surrogate_ohayou_utf16.index(ohayou_utf16le)
end
if $tests_failed_count == 0
puts "everything's fine"
else
puts "#{$tests_failed_count}/#{$tests_done_count} tests failed"
end