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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
require 'test/unit'
ConstTest = 3
class Class
alias _remove_const remove_const
public :_remove_const
end
class TestBasicInstructions < Test::Unit::TestCase
def test_immediates
assert_equal((1==1), true)
assert_equal((1==2), false)
assert_equal [][0], nil
assert_equal "sym".intern, :sym
assert_equal "sym".intern, :"sym"
assert_equal 1234 + 0, 1234
assert_equal 1234, 1_2_3_4
assert_equal 41, 0b0101001
assert_equal 420, 0644
assert_equal 18, 0x12
assert_equal 123456789012345678901234567890 + 0,
123456789012345678901234567890
assert_equal 1.234 + 0.0, 1.234
end
def test_self
assert_equal self, self
assert_equal false, (self == false) # Qfalse==0 in C
assert_equal false, (self == nil)
assert_equal false, (self == 0)
end
def test_string
expected = "str" + "ing"
assert_equal expected, 'string'
assert_equal expected, "string"
assert_equal expected, %q(string)
assert_equal expected, %Q(string)
assert_equal expected, %(string)
end
def test_dstring
assert_equal "2", "#{1+1}"
s = "OK"
assert_equal "OK", "#{s}"
assert_equal "OKx", "#{s}x"
assert_equal "xOK", "x#{s}"
assert_equal "xOKx", "x#{s}x"
end
def test_dsym
assert_equal :a3c, :"a#{1+2}c"
s = "sym"
assert_equal :sym, :"#{s}"
assert_equal :sym, :"#{"#{"#{s}"}"}"
end
def test_xstr
assert_equal 'hoge', `echo hoge`.chomp
assert_equal '3', `echo #{1 + 2}`.chomp
hoge = 'huga'
assert_equal 'huga', `echo #{hoge}`.chomp
end
def test_regexp
assert_equal /test/, /test/
assert_equal 'test', /test/.source
assert_equal 'TEST', /TEST/.source
assert_equal true, !!(/test/ =~ 'test')
assert_equal false, !!(/test/ =~ 'does not match')
re = /test/
assert_equal re, re
assert_equal 'test', re.source
assert_equal true, !!(re =~ 'test')
assert_equal false, !!(re =~ 'does not match')
assert_equal /x#{1+1}x/, /x#{1+1}x/
s = "OK"
assert_equal /x#{s}x/, /x#{s}x/
assert_equal true, !!(/x#{s}x/ =~ "xOKx")
assert_equal false, !!(/x#{s}x/ =~ "does not match")
s = "OK"
prev = nil
3.times do
assert_equal prev.object_id, (prev ||= /#{s}/o).object_id if prev
end
end
def test_array
assert_equal [], []
assert_equal 0, [].size
assert_equal [1, 2, 3], [1, 2, 3]
assert_equal [3, 7, 11], [1+2, 3+4, 5+6]
assert_equal [[1], [2], [3]], [[1], [2], [3]]
a = [1, 2, 3]
assert_equal 1, a[0]
assert_equal 2, a[1]
assert_equal 3, a[2]
assert_nil a[3]
a = %w( a b c )
assert_equal 'a', a[0]
assert_equal 'b', a[1]
assert_equal 'c', a[2]
assert_nil a[3]
end
def test_hash
assert_equal({}, {})
assert_equal({1=>2}, {1=>2})
assert_equal({1=>2, 3=>4}, {1=>2, 3=>4})
assert_equal({1=>2, 3=>4}, {3=>4, 1=>2})
# assert_equal({1=>2, 3=>4}, {1,2, 3,4}) # 1.9 doesn't support
assert_equal({"key"=>"val"}, {"key"=>"val"})
end
def test_range
assert_equal((1..3), (1..3))
assert_equal((1...3), (1...3))
assert_not_equal((1...3), (1..3))
assert_not_equal((1..3), (1...3))
assert_equal((1..3), (1..2+1))
assert_equal((1...3), (1...2+1))
assert_equal(('a'..'z'), ('a'..'z'))
end
def test_not
assert_equal true, !nil
assert_equal true, !false
assert_equal false, !true
assert_equal false, !3
assert_equal false, !(1+1)
assert_equal false, !!nil
assert_equal false, !!false
assert_equal true, !!true
assert_equal true, !!3
assert_equal true, !!(1+1)
assert_equal true, (not nil)
assert_equal true, (not false)
assert_equal false, (not true)
assert_equal false, (not 3)
assert_equal false, (not (1 + 1))
assert_equal false, (not not nil)
assert_equal false, (not not false)
assert_equal true, (not not true)
assert_equal true, (not not 3)
assert_equal true, (not not (1+1))
end
def test_local_variable
a = 7
assert_equal 7, a
assert_equal a, a
b = 17
assert_equal 7, a
assert_equal 17, b
assert_equal a, a
assert_equal b, b
assert_not_equal a, b
assert_not_equal b, a
a = b
assert_equal 17, a
assert_equal 17, b
assert_equal a, a
assert_equal b, b
assert_equal a, b
assert_equal b, a
c = 28
assert_equal 17, a
assert_equal 17, b
assert_equal 28, c
assert_equal a, a
assert_equal b, b
assert_equal a, b
assert_equal c, c
assert_not_equal a, c
assert_not_equal b, c
a = b = c
assert_equal 28, a
assert_equal 28, b
assert_equal 28, c
assert_equal a, a
assert_equal b, b
assert_equal a, b
assert_equal b, a
assert_equal a, c
assert_equal c, a
assert_equal b, c
assert_equal c, b
a = 1
b = 2
c = 3
set_lvar_in_another_method
assert_equal 1, a
assert_equal 2, b
assert_equal 3, c
end
def set_lvar_in_another_method
assert_raise(NameError) { a }
assert_raise(NameError) { b }
assert_raise(NameError) { c }
a = "NOT OK"
b = "NOT OK"
c = "NOT OK"
end
class Const
$Const = self
C = 'Const::C'
def self.c() C end
def c() C end
class A
C = 'Const::A::C'
def self.c() C end
def c() C end
CC = 'Const::A::CC'
def self.cc() CC end
def cc() CC end
class B
C = 'Const::A::B::C'
def self.c() C end
def c() C end
def self.cc() CC end
def cc() CC end
end
end
class AA < A
def self.cc() CC end
def cc() CC end
end
class AAA < AA
def self.cc() CC end
def cc() CC end
end
end
C = 0
def test_const_path
do_test_const_path
do_test_const_path
do_test_const_path
end
def do_test_const_path
assert_equal 0, C
assert_equal 0, C
assert_equal 3, ::ConstTest
assert_equal 3, ::ConstTest
assert_equal $Const, Const
assert_equal 'Const::C', Const::C
assert_equal 'Const::C', Const::C
assert_equal 'Const::A::C', Const::A::C
assert_equal 'Const::A::C', Const::A::C
assert_equal 'Const::A::B::C', Const::A::B::C
assert_equal 'Const::A::B::C', Const::A::B::C
Const::A::B._remove_const :C
assert_equal 'Const::C', Const::C
assert_equal 'Const::A::C', Const::A::C
assert_raise(NameError) { Const::A::B::C }
Const::A._remove_const :C
assert_equal 'Const::C', Const::C
assert_raise(NameError) { Const::A::C }
assert_raise(NameError) { Const::A::B::C }
Const._remove_const :C
assert_raise(NameError) { Const::C }
assert_raise(NameError) { Const::A::C }
assert_raise(NameError) { Const::A::B::C }
Const::A.const_set :C, 'Const::A::C'
assert_raise(NameError) { Const::C }
assert_equal 'Const::A::C', Const::A::C
assert_raise(NameError) { Const::A::B::C }
Const::A::B.const_set :C, 'Const::A::B::C'
assert_raise(NameError) { Const::C }
assert_equal 'Const::A::C', Const::A::C
assert_equal 'Const::A::B::C', Const::A::B::C
Const.const_set :C, 'Const::C'
assert_equal 'Const::C', Const::C
assert_equal 'Const::A::C', Const::A::C
assert_equal 'Const::A::B::C', Const::A::B::C
end
def test_const_cref
do_test_const_cref
do_test_const_cref
do_test_const_cref
end
def do_test_const_cref
assert_equal 'Const::C', Const.new.c
assert_equal 'Const::A::C', Const::A.new.c
assert_equal 'Const::A::B::C', Const::A::B.new.c
assert_equal 'Const::C', Const.c
assert_equal 'Const::A::C', Const::A.c
assert_equal 'Const::A::B::C', Const::A::B.c
Const::A::B._remove_const :C
assert_equal 'Const::C', Const.c
assert_equal 'Const::A::C', Const::A.c
assert_equal 'Const::A::C', Const::A::B.c
assert_equal 'Const::C', Const.new.c
assert_equal 'Const::A::C', Const::A.new.c
assert_equal 'Const::A::C', Const::A::B.new.c
Const::A._remove_const :C
assert_equal 'Const::C', Const.c
assert_equal 'Const::C', Const::A.c
assert_equal 'Const::C', Const::A::B.c
assert_equal 'Const::C', Const.new.c
assert_equal 'Const::C', Const::A.new.c
assert_equal 'Const::C', Const::A::B.new.c
Const::A::B.const_set :C, 'Const::A::B::C'
assert_equal 'Const::C', Const.c
assert_equal 'Const::C', Const::A.c
assert_equal 'Const::A::B::C', Const::A::B.c
assert_equal 'Const::C', Const.new.c
assert_equal 'Const::C', Const::A.new.c
assert_equal 'Const::A::B::C', Const::A::B.new.c
Const::A.const_set :C, 'Const::A::C'
assert_equal 'Const::C', Const.c
assert_equal 'Const::A::C', Const::A.c
assert_equal 'Const::A::B::C', Const::A::B.c
assert_equal 'Const::C', Const.new.c
assert_equal 'Const::A::C', Const::A.new.c
assert_equal 'Const::A::B::C', Const::A::B.new.c
ensure
# reset
Const.const_set :C, 'Const::C' unless Const.const_defined?(:C)
Const::A.const_set :C, 'Const::A::C' unless Const::A.const_defined?(:C)
Const::A::B.const_set :C, 'Const::A::B::C' unless Const::A::B.const_defined?(:C)
end
def test_const_inherit
do_test_const_inherit
do_test_const_inherit
do_test_const_inherit
end
def do_test_const_inherit
assert_equal 'Const::A::CC', Const::A.cc
assert_equal 'Const::A::CC', Const::AA.cc
assert_equal 'Const::A::CC', Const::AAA.cc
assert_equal 'Const::A::CC', Const::A.new.cc
assert_equal 'Const::A::CC', Const::AA.new.cc
assert_equal 'Const::A::CC', Const::AAA.new.cc
Const::AA.const_set :CC, 'Const::AA::CC'
assert_equal 'Const::A::CC', Const::A.cc
assert_equal 'Const::AA::CC', Const::AA.cc
assert_equal 'Const::AA::CC', Const::AAA.cc
assert_equal 'Const::A::CC', Const::A.new.cc
assert_equal 'Const::AA::CC', Const::AA.new.cc
assert_equal 'Const::AA::CC', Const::AAA.new.cc
Const::AAA.const_set :CC, 'Const::AAA::CC'
assert_equal 'Const::A::CC', Const::A.cc
assert_equal 'Const::AA::CC', Const::AA.cc
assert_equal 'Const::AAA::CC', Const::AAA.cc
assert_equal 'Const::A::CC', Const::A.new.cc
assert_equal 'Const::AA::CC', Const::AA.new.cc
assert_equal 'Const::AAA::CC', Const::AAA.new.cc
Const::AA._remove_const :CC
assert_equal 'Const::A::CC', Const::A.cc
assert_equal 'Const::A::CC', Const::AA.cc
assert_equal 'Const::AAA::CC', Const::AAA.cc
assert_equal 'Const::A::CC', Const::A.new.cc
assert_equal 'Const::A::CC', Const::AA.new.cc
assert_equal 'Const::AAA::CC', Const::AAA.new.cc
Const::AAA._remove_const :CC
assert_equal 'Const::A::CC', Const::A.cc
assert_equal 'Const::A::CC', Const::AA.cc
assert_equal 'Const::A::CC', Const::AAA.cc
assert_equal 'Const::A::CC', Const::A.new.cc
assert_equal 'Const::A::CC', Const::AA.new.cc
assert_equal 'Const::A::CC', Const::AAA.new.cc
end
def test_global_variable
$gvar1 = 1
assert_equal 1, $gvar1
$gvar1 = 2
assert_equal 2, $gvar1
$gvar2 = 77
assert_equal 2, $gvar1
assert_equal 77, $gvar2
$gvar2 = $gvar1
assert_equal 2, $gvar1
assert_equal 2, $gvar2
$gvar1 = 1
assert_equal 1, $gvar1
assert_equal 2, $gvar2
set_gvar_in_another_method
assert_equal "OK1", $gvar1
assert_equal "OK2", $gvar2
end
def set_gvar_in_another_method
assert_equal 1, $gvar1
assert_equal 2, $gvar2
$gvar1 = "OK1"
$gvar2 = "OK2"
end
class CVarA
@@cv = 'CVarA@@cv'
def self.cv() @@cv end
def self.cv=(v) @@cv = v end
class << self
def cv2() @@cv end
end
def cv() @@cv end
def cv=(v) @@cv = v end
end
class CVarB < CVarA
def self.cvB() @@cv end
def self.cvB=(v) @@cv = v end
class << self
def cvB2() @@cv end
end
def cvB() @@cv end
def cvB=(v) @@cv = v end
end
def test_class_variable
assert_equal 'CVarA@@cv', CVarA.cv
assert_equal 'CVarA@@cv', CVarA.cv2
assert_equal 'CVarA@@cv', CVarA.new.cv
CVarA.cv = 'singleton'
assert_equal 'singleton', CVarA.cv
assert_equal 'singleton', CVarA.cv2
assert_equal 'singleton', CVarA.new.cv
CVarA.new.cv = 'instance'
assert_equal 'instance', CVarA.cv
assert_equal 'instance', CVarA.cv2
assert_equal 'instance', CVarA.new.cv
CVarA.cv = 'CVarA@@cv'
CVarB.cv = 'B/singleton'
assert_equal 'B/singleton', CVarB.cv
assert_equal 'B/singleton', CVarB.cv2
assert_equal 'B/singleton', CVarB.new.cv
assert_equal 'B/singleton', CVarA.cv
assert_equal 'B/singleton', CVarA.cv2
assert_equal 'B/singleton', CVarA.new.cv
CVarB.new.cv = 'B/instance'
assert_equal 'B/instance', CVarB.cv
assert_equal 'B/instance', CVarB.cv2
assert_equal 'B/instance', CVarB.new.cv
assert_equal 'B/instance', CVarA.cv
assert_equal 'B/instance', CVarA.cv2
assert_equal 'B/instance', CVarA.new.cv
CVarA.cv = 'CVarA@@cv'
assert_equal('CVarA@@cv', CVarB.cvB)
assert_equal('CVarA@@cv', CVarB.cvB2)
assert_equal('CVarA@@cv', CVarB.new.cvB)
CVarB.cvB = 'B/singleton'
assert_equal 'B/singleton', CVarB.cvB
assert_equal 'B/singleton', CVarB.cvB2
assert_equal 'B/singleton', CVarB.new.cvB
assert_equal 'B/singleton', CVarA.cv
assert_equal 'B/singleton', CVarA.cv2
assert_equal 'B/singleton', CVarA.new.cv
CVarB.new.cvB = 'B/instance'
assert_equal 'B/instance', CVarB.cvB
assert_equal 'B/instance', CVarB.cvB2
assert_equal 'B/instance', CVarB.new.cvB
assert_equal 'B/instance', CVarA.cv
assert_equal 'B/instance', CVarA.cv2
assert_equal 'B/instance', CVarA.new.cv
CVarA.cv = 'CVarA@@cv'
CVarB.cvB = 'CVarB@@cv'
end
class OP
attr_accessor :x
end
def test_opassign
x = nil
x ||= 1
assert_equal 1, x
x &&= 2
assert_equal 2, x
x ||= 3
assert_equal 2, x
x &&= 4
assert_equal 4, x
y = OP.new
y.x = nil
y.x ||= 1
assert_equal 1, y.x
y.x &&= 2
assert_equal 2, y.x
y.x ||= 3
assert_equal 2, y.x
y.x &&= 4
assert_equal 4, y.x
z = OP.new
z.x = y
z.x.x = nil
z.x.x ||= 1
assert_equal 1, z.x.x
z.x.x &&= 2
assert_equal 2, z.x.x
z.x.x ||= 3
assert_equal 2, z.x.x
z.x.x &&= 4
assert_equal 4, z.x.x
a = []
a[0] = nil
a[0] ||= 1
assert_equal 1, a[0]
a[0] &&= 2
assert_equal 2, a[0]
a[0] ||= 3
assert_equal 2, a[0]
a[0] &&= 4
assert_equal 4, a[0]
end
def test_backref
/re/ =~ 'not match'
assert_nil $~
assert_nil $`
assert_nil $&
assert_nil $'
assert_nil $+
assert_nil $1
assert_nil $2
assert_nil $3
assert_nil $4
assert_nil $5
assert_nil $6
assert_nil $7
assert_nil $8
assert_nil $9
/(a)(b)(c)(d)(e)(f)(g)(h)(i)/ =~ 'xabcdefghiy'
assert_not_nil $~
assert_instance_of MatchData, $~
assert_equal 'abcdefghi', $~[0]
assert_equal 'a', $~[1]
assert_equal 'b', $~[2]
assert_equal 'c', $~[3]
assert_equal 'd', $~[4]
assert_equal 'e', $~[5]
assert_equal 'f', $~[6]
assert_equal 'g', $~[7]
assert_equal 'h', $~[8]
assert_equal 'i', $~[9]
assert_equal 'x', $`
assert_equal 'abcdefghi', $&
assert_equal 'y', $'
assert_equal 'i', $+
assert_equal 'a', $1
assert_equal 'b', $2
assert_equal 'c', $3
assert_equal 'd', $4
assert_equal 'e', $5
assert_equal 'f', $6
assert_equal 'g', $7
assert_equal 'h', $8
assert_equal 'i', $9
/re/ =~ 'not match'
assert_nil $~
assert_nil $`
assert_nil $&
assert_nil $'
assert_nil $+
assert_nil $1
assert_nil $2
assert_nil $3
assert_nil $4
assert_nil $5
assert_nil $6
assert_nil $7
assert_nil $8
assert_nil $9
end
def test_array_splat
a = []
assert_equal [], [*a]
assert_equal [1], [1, *a]
a = [2]
assert_equal [2], [*a]
assert_equal [1, 2], [1, *a]
a = [2, 3]
assert_equal [2, 3], [*a]
assert_equal [1, 2, 3], [1, *a]
a = nil
assert_equal [], [*a]
assert_equal [1], [1, *a]
end
end
|