DEFINITIONS
This source file includes following functions.
1 #! /usr/bin/env ruby
2
3 $testnum=0
4 $ntest=0
5 $failed = 0
6
7 def test_check(what)
8 printf "%s\n", what
9 $what = what
10 $testnum = 0
11 end
12
13 def test_ok(cond)
14 $testnum+=1
15 $ntest+=1
16 if cond
17 printf "ok %d\n", $testnum
18 else
19 where = caller[0]
20 printf "not ok %s %d -- %s\n", $what, $testnum, where
21 $failed+=1
22 end
23 end
24
25 # make sure conditional operators work
26
27 test_check "assignment"
28
29 a=[]; a[0] ||= "bar";
30 test_ok(a[0] == "bar")
31 h={}; h["foo"] ||= "bar";
32 test_ok(h["foo"] == "bar")
33
34 aa = 5
35 aa ||= 25
36 test_ok(aa == 5)
37 bb ||= 25
38 test_ok(bb == 25)
39 cc &&=33
40 test_ok(cc == nil)
41 cc = 5
42 cc &&=44
43 test_ok(cc == 44)
44
45 a = nil; test_ok(a == nil)
46 a = 1; test_ok(a == 1)
47 a = []; test_ok(a == [])
48 a = [1]; test_ok(a == [1])
49 a = [nil]; test_ok(a == [nil])
50 a = [[]]; test_ok(a == [[]])
51 a = [*[]]; test_ok(a == [])
52 a = [*[1]]; test_ok(a == [1])
53 a = [*[1,2]]; test_ok(a == [1,2])
54
55 a = *nil; test_ok(a == nil)
56 a = *1; test_ok(a == 1)
57 a = *[]; test_ok(a == nil)
58 a = *[1]; test_ok(a == 1)
59 a = *[nil]; test_ok(a == nil)
60 a = *[[]]; test_ok(a == [])
61 a = *[*[]]; test_ok(a == nil)
62 a = *[*[1]]; test_ok(a == 1)
63 a = *[*[1,2]]; test_ok(a == [1,2])
64
65 *a = nil; test_ok(a == [])
66 *a = 1; test_ok(a == [1])
67 *a = []; test_ok(a == [])
68 *a = [1]; test_ok(a == [1])
69 *a = [nil]; test_ok(a == [nil])
70 *a = [[]]; test_ok(a == [[]])
71 *a = [*[]]; test_ok(a == [])
72 *a = [*[1]]; test_ok(a == [1])
73 *a = [*[1,2]]; test_ok(a == [1,2])
74
75 *a = *nil; test_ok(a == [])
76 *a = *1; test_ok(a == [1])
77 *a = *[]; test_ok(a == [])
78 *a = *[1]; test_ok(a == [1])
79 *a = *[nil]; test_ok(a == [])
80 *a = *[[]]; test_ok(a == [])
81 *a = *[*[]]; test_ok(a == [])
82 *a = *[*[1]]; test_ok(a == [1])
83 *a = *[*[1,2]]; test_ok(a == [1,2])
84
85 a,b,*c = nil; test_ok([a,b,c] == [nil, nil, []])
86 a,b,*c = 1; test_ok([a,b,c] == [1, nil, []])
87 a,b,*c = []; test_ok([a,b,c] == [nil, nil, []])
88 a,b,*c = [1]; test_ok([a,b,c] == [1, nil, []])
89 a,b,*c = [nil]; test_ok([a,b,c] == [nil, nil, []])
90 a,b,*c = [[]]; test_ok([a,b,c] == [[], nil, []])
91 a,b,*c = [*[]]; test_ok([a,b,c] == [nil, nil, []])
92 a,b,*c = [*[1]]; test_ok([a,b,c] == [1, nil, []])
93 a,b,*c = [*[1,2]]; test_ok([a,b,c] == [1, 2, []])
94
95 a,b,*c = *nil; test_ok([a,b,c] == [nil, nil, []])
96 a,b,*c = *1; test_ok([a,b,c] == [1, nil, []])
97 a,b,*c = *[]; test_ok([a,b,c] == [nil, nil, []])
98 a,b,*c = *[1]; test_ok([a,b,c] == [1, nil, []])
99 a,b,*c = *[nil]; test_ok([a,b,c] == [nil, nil, []])
100 a,b,*c = *[[]]; test_ok([a,b,c] == [nil, nil, []])
101 a,b,*c = *[*[]]; test_ok([a,b,c] == [nil, nil, []])
102 a,b,*c = *[*[1]]; test_ok([a,b,c] == [1, nil, []])
103 a,b,*c = *[*[1,2]]; test_ok([a,b,c] == [1, 2, []])
104
105 def f; yield nil; end; f {|a| test_ok(a == nil)}
106 def f; yield 1; end; f {|a| test_ok(a == 1)}
107 def f; yield []; end; f {|a| test_ok(a == [])}
108 def f; yield [1]; end; f {|a| test_ok(a == [1])}
109 def f; yield [nil]; end; f {|a| test_ok(a == [nil])}
110 def f; yield [[]]; end; f {|a| test_ok(a == [[]])}
111 def f; yield [*[]]; end; f {|a| test_ok(a == [])}
112 def f; yield [*[1]]; end; f {|a| test_ok(a == [1])}
113 def f; yield [*[1,2]]; end; f {|a| test_ok(a == [1,2])}
114
115 def f; yield *nil; end; f {|a| test_ok(a == nil)}
116 def f; yield *1; end; f {|a| test_ok(a == 1)}
117 def f; yield *[]; end; f {|a| test_ok(a == nil)}
118 def f; yield *[1]; end; f {|a| test_ok(a == 1)}
119 def f; yield *[nil]; end; f {|a| test_ok(a == nil)}
120 def f; yield *[[]]; end; f {|a| test_ok(a == [])}
121 def f; yield *[*[]]; end; f {|a| test_ok(a == nil)}
122 def f; yield *[*[1]]; end; f {|a| test_ok(a == 1)}
123 def f; yield *[*[1,2]]; end; f {|a| test_ok(a == [1,2])}
124
125 def f; yield nil; end; f {|*a| test_ok(a == [])}
126 def f; yield 1; end; f {|*a| test_ok(a == [1])}
127 def f; yield []; end; f {|*a| test_ok(a == [])}
128 def f; yield [1]; end; f {|*a| test_ok(a == [1])}
129 def f; yield [nil]; end; f {|*a| test_ok(a == [nil])}
130 def f; yield [[]]; end; f {|*a| test_ok(a == [[]])}
131 def f; yield [*[]]; end; f {|*a| test_ok(a == [])}
132 def f; yield [*[1]]; end; f {|*a| test_ok(a == [1])}
133 def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [1,2])}
134
135 def f; yield *nil; end; f {|*a| test_ok(a == [])}
136 def f; yield *1; end; f {|*a| test_ok(a == [1])}
137 def f; yield *[]; end; f {|*a| test_ok(a == [])}
138 def f; yield *[1]; end; f {|*a| test_ok(a == [1])}
139 def f; yield *[nil]; end; f {|*a| test_ok(a == [])}
140 def f; yield *[[]]; end; f {|*a| test_ok(a == [])}
141 def f; yield *[*[]]; end; f {|*a| test_ok(a == [])}
142 def f; yield *[*[1]]; end; f {|*a| test_ok(a == [1])}
143 def f; yield *[*[1,2]]; end; f {|*a| test_ok(a == [1,2])}
144
145 def f; yield nil; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
146 def f; yield 1; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
147 def f; yield []; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
148 def f; yield [1]; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
149 def f; yield [nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
150 def f; yield [[]]; end; f {|a,b,*c| test_ok([a,b,c] == [[], nil, []])}
151 def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
152 def f; yield [*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
153 def f; yield [*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1, 2, []])}
154
155 def f; yield *nil; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
156 def f; yield *1; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
157 def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
158 def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
159 def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
160 def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
161 def f; yield *[*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil, nil, []])}
162 def f; yield *[*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1, nil, []])}
163 def f; yield *[*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1, 2, []])}
164
165 test_check "condition"
166
167 $x = '0';
168
169 $x == $x && test_ok(true)
170 $x != $x && test_ok(false)
171 $x == $x || test_ok(false)
172 $x != $x || test_ok(true)
173
174 # first test to see if we can run the tests.
175
176 test_check "if/unless";
177
178 $x = 'test';
179 test_ok(if $x == $x then true else false end)
180 $bad = false
181 unless $x == $x
182 $bad = true
183 end
184 test_ok(!$bad)
185 test_ok(unless $x != $x then true else false end)
186
187 test_check "case"
188
189 case 5
190 when 1, 2, 3, 4, 6, 7, 8
191 test_ok(false)
192 when 5
193 test_ok(true)
194 end
195
196 case 5
197 when 5
198 test_ok(true)
199 when 1..10
200 test_ok(false)
201 end
202
203 case 5
204 when 1..10
205 test_ok(true)
206 else
207 test_ok(false)
208 end
209
210 case 5
211 when 5
212 test_ok(true)
213 else
214 test_ok(false)
215 end
216
217 case "foobar"
218 when /^f.*r$/
219 test_ok(true)
220 else
221 test_ok(false)
222 end
223
224 test_check "while/until";
225
226 tmp = open("while_tmp", "w")
227 tmp.print "tvi925\n";
228 tmp.print "tvi920\n";
229 tmp.print "vt100\n";
230 tmp.print "Amiga\n";
231 tmp.print "paper\n";
232 tmp.close
233
234 # test break
235
236 tmp = open("while_tmp", "r")
237 test_ok(tmp.kind_of?(File))
238
239 while line = tmp.gets()
240 break if /vt100/ =~ line
241 end
242
243 test_ok(!tmp.eof? && /vt100/ =~ line)
244 tmp.close
245
246 # test next
247 $bad = false
248 tmp = open("while_tmp", "r")
249 while line = tmp.gets()
250 next if /vt100/ =~ line
251 $bad = 1 if /vt100/ =~ line
252 end
253 test_ok(!(!tmp.eof? || /vt100/ =~ line || $bad))
254 tmp.close
255
256 # test redo
257 $bad = false
258 tmp = open("while_tmp", "r")
259 while tmp.gets()
260 line = $_
261 gsub(/vt100/, 'VT100')
262 if $_ != line
263 $_.gsub!('VT100', 'Vt100')
264 redo
265 end
266 $bad = 1 if /vt100/ =~ $_
267 $bad = 1 if /VT100/ =~ $_
268 end
269 test_ok(tmp.eof? && !$bad)
270 tmp.close
271
272 sum=0
273 for i in 1..10
274 sum += i
275 i -= 1
276 if i > 0
277 redo
278 end
279 end
280 test_ok(sum == 220)
281
282 # test interval
283 $bad = false
284 tmp = open("while_tmp", "r")
285 while line = tmp.gets()
286 break if 3
287 case line
288 when /vt100/, /Amiga/, /paper/
289 $bad = true
290 end
291 end
292 test_ok(!$bad)
293 tmp.close
294
295 File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
296 test_ok(!File.exist?("while_tmp"))
297
298 i = 0
299 until i>4
300 i+=1
301 end
302 test_ok(i>4)
303
304
305 # exception handling
306 test_check "exception";
307
308 begin
309 raise "this must be handled"
310 test_ok(false)
311 rescue
312 test_ok(true)
313 end
314
315 $bad = true
316 begin
317 raise "this must be handled no.2"
318 rescue
319 if $bad
320 $bad = false
321 retry
322 test_ok(false)
323 end
324 end
325 test_ok(true)
326
327 # exception in rescue clause
328 $string = "this must be handled no.3"
329 begin
330 begin
331 raise "exception in rescue clause"
332 rescue
333 raise $string
334 end
335 test_ok(false)
336 rescue
337 test_ok(true) if $! == $string
338 end
339
340 # exception in ensure clause
341 begin
342 begin
343 raise "this must be handled no.4"
344 ensure
345 raise "exception in ensure clause"
346 end
347 test_ok(false)
348 rescue
349 test_ok(true)
350 end
351
352 $bad = true
353 begin
354 begin
355 raise "this must be handled no.5"
356 ensure
357 $bad = false
358 end
359 rescue
360 end
361 test_ok(!$bad)
362
363 $bad = true
364 begin
365 begin
366 raise "this must be handled no.6"
367 ensure
368 $bad = false
369 end
370 rescue
371 end
372 test_ok(!$bad)
373
374 $bad = true
375 while true
376 begin
377 break
378 ensure
379 $bad = false
380 end
381 end
382 test_ok(!$bad)
383
384 test_ok(catch(:foo) {
385 loop do
386 loop do
387 throw :foo, true
388 break
389 end
390 break
391 test_ok(false) # should no reach here
392 end
393 false
394 })
395
396 test_check "array"
397 test_ok([1, 2] + [3, 4] == [1, 2, 3, 4])
398 test_ok([1, 2] * 2 == [1, 2, 1, 2])
399 test_ok([1, 2] * ":" == "1:2")
400
401 test_ok([1, 2].hash == [1, 2].hash)
402
403 test_ok([1,2,3] & [2,3,4] == [2,3])
404 test_ok([1,2,3] | [2,3,4] == [1,2,3,4])
405 test_ok([1,2,3] - [2,3] == [1])
406
407 $x = [0, 1, 2, 3, 4, 5]
408 test_ok($x[2] == 2)
409 test_ok($x[1..3] == [1, 2, 3])
410 test_ok($x[1,3] == [1, 2, 3])
411
412 $x[0, 2] = 10
413 test_ok($x[0] == 10 && $x[1] == 2)
414
415 $x[0, 0] = -1
416 test_ok($x[0] == -1 && $x[1] == 10)
417
418 $x[-1, 1] = 20
419 test_ok($x[-1] == 20 && $x.pop == 20)
420
421 # array and/or
422 test_ok(([1,2,3]&[2,4,6]) == [2])
423 test_ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
424
425 # compact
426 $x = [nil, 1, nil, nil, 5, nil, nil]
427 $x.compact!
428 test_ok($x == [1, 5])
429
430 # uniq
431 $x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
432 $x.uniq!
433 test_ok($x == [1, 4, 2, 5])
434
435 # empty?
436 test_ok(!$x.empty?)
437 $x = []
438 test_ok($x.empty?)
439
440 # sort
441 $x = ["it", "came", "to", "pass", "that", "..."]
442 $x = $x.sort.join(" ")
443 test_ok($x == "... came it pass that to")
444 $x = [2,5,3,1,7]
445 $x.sort!{|a,b| a<=>b} # sort with condition
446 test_ok($x == [1,2,3,5,7])
447 $x.sort!{|a,b| b-a} # reverse sort
448 test_ok($x == [7,5,3,2,1])
449
450 # split test
451 $x = "The Botest_ok of Mormon"
452 test_ok($x.split(
453 test_ok($x.reverse == $x.reverse!)
454 test_ok("1 byte string".split(
455 $x = "a b c d"
456 test_ok($x.split == ['a', 'b', 'c', 'd'])
457 test_ok($x.split(' ') == ['a', 'b', 'c', 'd'])
458 test_ok(defined? "a".chomp)
459 test_ok("abc".scan(/./) == ["a", "b", "c"])
460 test_ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]])
461 # non-greedy match
462 test_ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]])
463
464 $x = [1]
465 test_ok(($x * 5).join(":") == '1:1:1:1:1')
466 test_ok(($x * 1).join(":") == '1')
467 test_ok(($x * 0).join(":") == '')
468
469 *$x = (1..7).to_a
470 test_ok($x.size == 7)
471 test_ok($x == [1, 2, 3, 4, 5, 6, 7])
472
473 $x = [1,2,3]
474 $x[1,0] = $x
475 test_ok($x == [1,1,2,3,2,3])
476
477 $x = [1,2,3]
478 $x[-1,0] = $x
479 test_ok($x == [1,2,1,2,3,3])
480
481 $x = [1,2,3]
482 $x.concat($x)
483 test_ok($x == [1,2,3,1,2,3])
484
485 test_check "hash"
486 $x = {1=>2, 2=>4, 3=>6}
487 $y = {1, 2, 2, 4, 3, 6}
488
489 test_ok($x[1] == 2)
490
491 test_ok(begin
492 for k,v in $y
493 raise if k*2 != v
494 end
495 true
496 rescue
497 false
498 end)
499
500 test_ok($x.length == 3)
501 test_ok($x.has_key?(1))
502 test_ok($x.has_value?(4))
503 test_ok($x.select(2,3) == [4,6])
504 test_ok($x == {1=>2, 2=>4, 3=>6})
505
506 $z = $y.keys.join(":")
507 test_ok($z == "1:2:3")
508
509 $z = $y.values.join(":")
510 test_ok($z == "2:4:6")
511 test_ok($x == $y)
512
513 $y.shift
514 test_ok($y.length == 2)
515
516 $z = [1,2]
517 $y[$z] = 256
518 test_ok($y[$z] == 256)
519
520 $x = Hash.new(0)
521 $x[1] = 1
522 test_ok($x[1] == 1)
523 test_ok($x[2] == 0)
524
525 $x = Hash.new([])
526 test_ok($x[22] == [])
527 test_ok($x[22].equal?($x[22]))
528
529 $x = Hash.new{[]}
530 test_ok($x[22] == [])
531 test_ok(!$x[22].equal?($x[22]))
532
533 $x = Hash.new{|h,k| $z = k; h[k] = k*2}
534 $z = 0
535 test_ok($x[22] == 44)
536 test_ok($z == 22)
537 $z = 0
538 test_ok($x[22] == 44)
539 test_ok($z == 0)
540 $x.default = 5
541 test_ok($x[23] == 5)
542
543 $x = Hash.new
544 def $x.default(k)
545 $z = k
546 self[k] = k*2
547 end
548 $z = 0
549 test_ok($x[22] == 44)
550 test_ok($z == 22)
551 $z = 0
552 test_ok($x[22] == 44)
553 test_ok($z == 0)
554
555 test_check "iterator"
556
557 test_ok(!iterator?)
558
559 def ttt
560 test_ok(iterator?)
561 end
562 ttt{}
563
564 # yield at top level
565 test_ok(!defined?(yield))
566
567 $x = [1, 2, 3, 4]
568 $y = []
569
570 # iterator over array
571 for i in $x
572 $y.push i
573 end
574 test_ok($x == $y)
575
576 # nested iterator
577 def tt
578 1.upto(10) {|i|
579 yield i
580 }
581 end
582
583 tt{|i| break if i == 5}
584 test_ok(i == 5)
585
586 def tt2(dummy)
587 yield 1
588 end
589
590 def tt3(&block)
591 tt2(raise(ArgumentError,""),&block)
592 end
593
594 $x = false
595 begin
596 tt3{}
597 rescue ArgumentError
598 $x = true
599 rescue Exception
600 end
601 test_ok($x)
602
603 # iterator break/redo/next/retry
604 done = true
605 loop{
606 break
607 done = false # should not reach here
608 }
609 test_ok(done)
610
611 done = false
612 $bad = false
613 loop {
614 break if done
615 done = true
616 next
617 $bad = true # should not reach here
618 }
619 test_ok(!$bad)
620
621 done = false
622 $bad = false
623 loop {
624 break if done
625 done = true
626 redo
627 $bad = true # should not reach here
628 }
629 test_ok(!$bad)
630
631 $x = []
632 for i in 1 .. 7
633 $x.push i
634 end
635 test_ok($x.size == 7)
636 test_ok($x == [1, 2, 3, 4, 5, 6, 7])
637
638 $done = false
639 $x = []
640 for i in 1 .. 7 # see how retry works in iterator loop
641 if i == 4 and not $done
642 $done = true
643 retry
644 end
645 $x.push(i)
646 end
647 test_ok($x.size == 10)
648 test_ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7])
649
650 # append method to built-in class
651 class Array
652 def iter_test1
653 collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
654 end
655 def iter_test2
656 a = collect{|e| [e, yield(e)]}
657 a.sort{|a,b|a[1]<=>b[1]}
658 end
659 end
660 $x = [[1,2],[3,4],[5,6]]
661 test_ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x})
662
663 class IterTest
664 def initialize(e); @body = e; end
665
666 def each0(&block); @body.each(&block); end
667 def each1(&block); @body.each { |*x| block.call(*x) } end
668 def each2(&block); @body.each { |*x| block.call(x) } end
669 def each3(&block); @body.each { |x| block.call(*x) } end
670 def each4(&block); @body.each { |x| block.call(x) } end
671 def each5; @body.each { |*x| yield(*x) } end
672 def each6; @body.each { |*x| yield(x) } end
673 def each7; @body.each { |x| yield(*x) } end
674 def each8; @body.each { |x| yield(x) } end
675
676 def f(a)
677 test_ok(a == [1])
678 end
679 end
680 IterTest.new(nil).method(:f).to_proc.call([1])
681
682 IterTest.new([0]).each0 { |x| $x = x }
683 test_ok($x == 0)
684 IterTest.new([1]).each1 { |x| $x = x }
685 test_ok($x == 1)
686 IterTest.new([2]).each2 { |x| $x = x }
687 test_ok($x == [2])
688 IterTest.new([3]).each3 { |x| $x = x }
689 test_ok($x == 3)
690 IterTest.new([4]).each4 { |x| $x = x }
691 test_ok($x == 4)
692 IterTest.new([5]).each5 { |x| $x = x }
693 test_ok($x == 5)
694 IterTest.new([6]).each6 { |x| $x = x }
695 test_ok($x == [6])
696 IterTest.new([7]).each7 { |x| $x = x }
697 test_ok($x == 7)
698 IterTest.new([8]).each8 { |x| $x = x }
699 test_ok($x == 8)
700
701 IterTest.new([[0]]).each0 { |x| $x = x }
702 test_ok($x == [0])
703 IterTest.new([[1]]).each1 { |x| $x = x }
704 test_ok($x == 1)
705 IterTest.new([[2]]).each2 { |x| $x = x }
706 test_ok($x == [2])
707 IterTest.new([[3]]).each3 { |x| $x = x }
708 test_ok($x == 3)
709 IterTest.new([[4]]).each4 { |x| $x = x }
710 test_ok($x == [4])
711 IterTest.new([[5]]).each5 { |x| $x = x }
712 test_ok($x == 5)
713 IterTest.new([[6]]).each6 { |x| $x = x }
714 test_ok($x == [6])
715 IterTest.new([[7]]).each7 { |x| $x = x }
716 test_ok($x == 7)
717 IterTest.new([[8]]).each8 { |x| $x = x }
718 test_ok($x == [8])
719
720 IterTest.new([[0,0]]).each0 { |x| $x = x }
721 test_ok($x == [0,0])
722 IterTest.new([[8,8]]).each8 { |x| $x = x }
723 test_ok($x == [8,8])
724
725 test_check "float"
726 test_ok(2.6.floor == 2)
727 test_ok(-2.6.floor == -3)
728 test_ok(2.6.ceil == 3)
729 test_ok(-2.6.ceil == -2)
730 test_ok(2.6.truncate == 2)
731 test_ok(-2.6.truncate == -2)
732 test_ok(2.6.round == 3)
733 test_ok(-2.4.truncate == -2)
734 test_ok((13.4 % 1 - 0.4).abs < 0.0001)
735
736 test_check "bignum"
737 def fact(n)
738 return 1 if n == 0
739 f = 1
740 while n>0
741 f *= n
742 n -= 1
743 end
744 return f
745 end
746 $x = fact(40)
747 test_ok($x == $x)
748 test_ok($x == fact(40))
749 test_ok($x < $x+2)
750 test_ok($x > $x-2)
751 test_ok($x == 815915283247897734345611269596115894272000000000)
752 test_ok($x != 815915283247897734345611269596115894272000000001)
753 test_ok($x+1 == 815915283247897734345611269596115894272000000001)
754 test_ok($x/fact(20) == 335367096786357081410764800000)
755 $x = -$x
756 test_ok($x == -815915283247897734345611269596115894272000000000)
757 test_ok(2-(2**32) == -(2**32-2))
758 test_ok(2**32 - 5 == (2**32-3)-2)
759
760 $good = true;
761 for i in 1000..1014
762 $good = false if ((1<<i) != (2**i))
763 end
764 test_ok($good)
765
766 $good = true;
767 n1=1<<1000
768 for i in 1000..1014
769 $good = false if ((1<<i) != n1)
770 n1 *= 2
771 end
772 test_ok($good)
773
774 $good = true;
775 n2=n1
776 for i in 1..10
777 n1 = n1 / 2
778 n2 = n2 >> 1
779 $good = false if (n1 != n2)
780 end
781 test_ok($good)
782
783 $good = true;
784 for i in 4000..4096
785 n1 = 1 << i;
786 if (n1**2-1) / (n1+1) != (n1-1)
787 p i
788 $good = false
789 end
790 end
791 test_ok($good)
792
793 b = 10**80
794 a = b * 9 + 7
795 test_ok(7 == a.modulo(b))
796 test_ok(-b + 7 == a.modulo(-b))
797 test_ok(b + -7 == (-a).modulo(b))
798 test_ok(-7 == (-a).modulo(-b))
799 test_ok(7 == a.remainder(b))
800 test_ok(7 == a.remainder(-b))
801 test_ok(-7 == (-a).remainder(b))
802 test_ok(-7 == (-a).remainder(-b))
803
804 test_ok(10**40+10**20 == 10000000000000000000100000000000000000000)
805 test_ok(10**40/10**20 == 100000000000000000000)
806
807 a = 677330545177305025495135714080
808 b = 14269972710765292560
809 test_ok(a % b == 0)
810 test_ok(-a % b == 0)
811
812 def shift_test(a)
813 b = a / (2 ** 32)
814 c = a >> 32
815 test_ok(b == c)
816
817 b = a * (2 ** 32)
818 c = a << 32
819 test_ok(b == c)
820 end
821
822 shift_test(-4518325415524767873)
823 shift_test(-0xfffffffffffffffff)
824
825 test_check "string & char"
826
827 test_ok("abcd" == "abcd")
828 test_ok("abcd" =~ "abcd")
829 test_ok("abcd" === "abcd")
830 # compile time string concatenation
831 test_ok("ab" "cd" == "abcd")
832 test_ok("#{22}aa" "cd#{44}" == "22aacd44")
833 test_ok("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566")
834 test_ok("abc" !~ /^$/)
835 test_ok("abc\n" !~ /^$/)
836 test_ok("abc" !~ /^d*$/)
837 test_ok(("abc" =~ /d*$/) == 3)
838 test_ok("" =~ /^$/)
839 test_ok("\n" =~ /^$/)
840 test_ok("a\n\n" =~ /^$/)
841 test_ok("abcabc" =~ /.*a/ && $& == "abca")
842 test_ok("abcabc" =~ /.*c/ && $& == "abcabc")
843 test_ok("abcabc" =~ /.*?a/ && $& == "a")
844 test_ok("abcabc" =~ /.*?c/ && $& == "abc")
845 test_ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb")
846
847 test_ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb")
848 test_ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb")
849 test_ok(/^(ab+)+/ =~ "ababb" && $& == "ababb")
850 test_ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb")
851
852 test_ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
853 test_ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
854
855 $x = <<END;
856 ABCD
857 ABCD
858 END
859 $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3}
860 test_ok($x == "AC\nAC\n")
861
862 test_ok("foobar" =~ /foo(?=(bar)|(baz))/)
863 test_ok("foobaz" =~ /foo(?=(bar)|(baz))/)
864
865 $foo = "abc"
866 test_ok("#$foo = abc" == "abc = abc")
867 test_ok("#{$foo} = abc" == "abc = abc")
868
869 foo = "abc"
870 test_ok("#{foo} = abc" == "abc = abc")
871
872 test_ok('-' * 5 == '-----')
873 test_ok('-' * 1 == '-')
874 test_ok('-' * 0 == '')
875
876 foo = '-'
877 test_ok(foo * 5 == '-----')
878 test_ok(foo * 1 == '-')
879 test_ok(foo * 0 == '')
880
881 $x = "a.gif"
882 test_ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif")
883 test_ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif")
884 test_ok($x.sub(/.*\.([^\.]+)$/, '\2') == "")
885 test_ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab")
886 test_ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>")
887
888 # character constants(assumes ASCII)
889 test_ok("a"[0] == ?a)
890 test_ok(?a == ?a)
891 test_ok(?\C-a == 1)
892 test_ok(?\M-a == 225)
893 test_ok(?\M-\C-a == 129)
894 test_ok("a".upcase![0] == ?A)
895 test_ok("A".downcase![0] == ?a)
896 test_ok("abc".tr!("a-z", "A-Z") == "ABC")
897 test_ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC")
898 test_ok("abcc".squeeze!("a-z") == "abc")
899 test_ok("abcd".delete!("bc") == "ad")
900
901 $x = "abcdef"
902 $y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
903 $bad = false
904 $x.each_byte {|i|
905 if i != $y.shift
906 $bad = true
907 break
908 end
909 }
910 test_ok(!$bad)
911
912 s = "a string"
913 s[0..s.size]="another string"
914 test_ok(s == "another string")
915
916 s = <<EOS
917 #{
918 [1,2,3].join(",")
919 }
920 EOS
921 test_ok(s == "1,2,3\n")
922
923 test_check "assignment"
924 a = nil
925 test_ok(defined?(a))
926 test_ok(a == nil)
927
928 # multiple asignment
929 a, b = 1, 2
930 test_ok(a == 1 && b == 2)
931
932 a, b = b, a
933 test_ok(a == 2 && b == 1)
934
935 a, = 1,2
936 test_ok(a == 1)
937
938 a, *b = 1, 2, 3
939 test_ok(a == 1 && b == [2, 3])
940
941 a, (b, c), d = 1, [2, 3], 4
942 test_ok(a == 1 && b == 2 && c == 3 && d == 4)
943
944 *a = 1, 2, 3
945 test_ok(a == [1, 2, 3])
946
947 *a = 4
948 test_ok(a == [4])
949
950 *a = nil
951 test_ok(a == [])
952
953 test_check "call"
954 def aaa(a, b=100, *rest)
955 res = [a, b]
956 res += rest if rest
957 return res
958 end
959
960 # not enough argument
961 begin
962 aaa() # need at least 1 arg
963 test_ok(false)
964 rescue
965 test_ok(true)
966 end
967
968 begin
969 aaa # no arg given (exception raised)
970 test_ok(false)
971 rescue
972 test_ok(true)
973 end
974
975 test_ok(aaa(1) == [1, 100])
976 test_ok(aaa(1, 2) == [1, 2])
977 test_ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4])
978 test_ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4])
979
980 test_check "proc"
981 $proc = proc{|i| i}
982 test_ok($proc.call(2) == 2)
983 test_ok($proc.call(3) == 3)
984
985 $proc = proc{|i| i*2}
986 test_ok($proc.call(2) == 4)
987 test_ok($proc.call(3) == 6)
988
989 proc{
990 iii=5 # nested local variable
991 $proc = proc{|i|
992 iii = i
993 }
994 $proc2 = proc {
995 $x = iii # nested variables shared by procs
996 }
997 # scope of nested variables
998 test_ok(defined?(iii))
999 }.call
1000 test_ok(!defined?(iii)) # out of scope
1001
1002 loop{iii=5; test_ok(eval("defined? iii")); break}
1003 loop {
1004 iii = 10
1005 def dyna_var_check
1006 loop {
1007 test_ok(!defined?(iii))
1008 break
1009 }
1010 end
1011 dyna_var_check
1012 break
1013 }
1014 $x=0
1015 $proc.call(5)
1016 $proc2.call
1017 test_ok($x == 5)
1018
1019 if defined? Process.kill
1020 test_check "signal"
1021
1022 $x = 0
1023 trap "SIGINT", proc{|sig| $x = 2}
1024 Process.kill "SIGINT", $$
1025 sleep 0.1
1026 test_ok($x == 2)
1027
1028 trap "SIGINT", proc{raise "Interrupt"}
1029
1030 x = false
1031 begin
1032 Process.kill "SIGINT", $$
1033 sleep 0.1
1034 rescue
1035 x = $!
1036 end
1037 test_ok(x && /Interrupt/ =~ x)
1038 end
1039
1040 test_check "eval"
1041 test_ok(eval("") == nil)
1042 $bad=false
1043 eval 'while false; $bad = true; print "foo\n" end'
1044 test_ok(!$bad)
1045
1046 test_ok(eval('TRUE'))
1047 test_ok(eval('true'))
1048 test_ok(!eval('NIL'))
1049 test_ok(!eval('nil'))
1050 test_ok(!eval('FALSE'))
1051 test_ok(!eval('false'))
1052
1053 $foo = 'test_ok(true)'
1054 begin
1055 eval $foo
1056 rescue
1057 test_ok(false)
1058 end
1059
1060 test_ok(eval("$foo") == 'test_ok(true)')
1061 test_ok(eval("true") == true)
1062 i = 5
1063 test_ok(eval("i == 5"))
1064 test_ok(eval("i") == 5)
1065 test_ok(eval("defined? i"))
1066
1067 # eval with binding
1068 def test_ev
1069 local1 = "local1"
1070 lambda {
1071 local2 = "local2"
1072 return binding
1073 }.call
1074 end
1075
1076 $x = test_ev
1077 test_ok(eval("local1", $x) == "local1") # normal local var
1078 test_ok(eval("local2", $x) == "local2") # nested local var
1079 $bad = true
1080 begin
1081 p eval("local1")
1082 rescue NameError # must raise error
1083 $bad = false
1084 end
1085 test_ok(!$bad)
1086
1087 module EvTest
1088 EVTEST1 = 25
1089 evtest2 = 125
1090 $x = binding
1091 end
1092 test_ok(eval("EVTEST1", $x) == 25) # constant in module
1093 test_ok(eval("evtest2", $x) == 125) # local var in module
1094 $bad = true
1095 begin
1096 eval("EVTEST1")
1097 rescue NameError # must raise error
1098 $bad = false
1099 end
1100 test_ok(!$bad)
1101
1102 x = proc{}
1103 eval "i4 = 1", x
1104 test_ok(eval("i4", x) == 1)
1105 x = proc{proc{}}.call
1106 eval "i4 = 22", x
1107 test_ok(eval("i4", x) == 22)
1108 $x = []
1109 x = proc{proc{}}.call
1110 eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
1111 test_ok($x[4].call == 8)
1112
1113 x = binding
1114 eval "i = 1", x
1115 test_ok(eval("i", x) == 1)
1116 x = proc{binding}.call
1117 eval "i = 22", x
1118 test_ok(eval("i", x) == 22)
1119 $x = []
1120 x = proc{binding}.call
1121 eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
1122 test_ok($x[4].call == 8)
1123 x = proc{binding}.call
1124 eval "for i6 in 1..1; j6=i6; end", x
1125 test_ok(eval("defined? i6", x))
1126 test_ok(eval("defined? j6", x))
1127
1128 proc {
1129 p = binding
1130 eval "foo11 = 1", p
1131 foo22 = 5
1132 proc{foo11=22}.call
1133 proc{foo22=55}.call
1134 test_ok(eval("foo11", p) == eval("foo11"))
1135 test_ok(eval("foo11") == 1)
1136 test_ok(eval("foo22", p) == eval("foo22"))
1137 test_ok(eval("foo22") == 55)
1138 }.call
1139
1140 p1 = proc{i7 = 0; proc{i7}}.call
1141 test_ok(p1.call == 0)
1142 eval "i7=5", p1
1143 test_ok(p1.call == 5)
1144 test_ok(!defined?(i7))
1145
1146 p1 = proc{i7 = 0; proc{i7}}.call
1147 i7 = nil
1148 test_ok(p1.call == 0)
1149 eval "i7=1", p1
1150 test_ok(p1.call == 1)
1151 eval "i7=5", p1
1152 test_ok(p1.call == 5)
1153 test_ok(i7 == nil)
1154
1155 test_check "system"
1156 test_ok(`echo foobar` == "foobar\n")
1157 test_ok(`./miniruby -e 'print "foobar"'` == 'foobar')
1158
1159 tmp = open("script_tmp", "w")
1160 tmp.print "print $zzz\n";
1161 tmp.close
1162
1163 test_ok(`./miniruby -s script_tmp -zzz` == 'true')
1164 test_ok(`./miniruby -s script_tmp -zzz=555` == '555')
1165
1166 tmp = open("script_tmp", "w")
1167 tmp.print "#! /usr/local/bin/ruby -s\n";
1168 tmp.print "print $zzz\n";
1169 tmp.close
1170
1171 test_ok(`./miniruby script_tmp -zzz=678` == '678')
1172
1173 tmp = open("script_tmp", "w")
1174 tmp.print "this is a leading junk\n";
1175 tmp.print "#! /usr/local/bin/ruby -s\n";
1176 tmp.print "print $zzz\n";
1177 tmp.print "__END__\n";
1178 tmp.print "this is a trailing junk\n";
1179 tmp.close
1180
1181 test_ok(`./miniruby -x script_tmp` == 'nil')
1182 test_ok(`./miniruby -x script_tmp -zzz=555` == '555')
1183
1184 tmp = open("script_tmp", "w")
1185 for i in 1..5
1186 tmp.print i, "\n"
1187 end
1188 tmp.close
1189
1190 `./miniruby -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
1191 done = true
1192 tmp = open("script_tmp", "r")
1193 while tmp.gets
1194 if $_.to_i % 5 != 0
1195 done = false
1196 break
1197 end
1198 end
1199 tmp.close
1200 test_ok(done)
1201
1202 File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
1203 File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
1204
1205 $bad = false
1206 if (dir = File.dirname(File.dirname($0))) == '.'
1207 dir = ""
1208 else
1209 dir << "/"
1210 end
1211
1212 def valid_syntax?(code, fname)
1213 eval("BEGIN {return true}\n#{code}", nil, fname, 0)
1214 rescue Exception
1215 puts $!.message
1216 false
1217 end
1218
1219 for script in Dir["#{dir}{lib,sample,ext}/**/*.rb"]
1220 unless valid_syntax? IO::read(script), script
1221 $bad = true
1222 end
1223 end
1224 test_ok(!$bad)
1225
1226 test_check "const"
1227 TEST1 = 1
1228 TEST2 = 2
1229
1230 module Const
1231 TEST3 = 3
1232 TEST4 = 4
1233 end
1234
1235 module Const2
1236 TEST3 = 6
1237 TEST4 = 8
1238 end
1239
1240 include Const
1241
1242 test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4])
1243
1244 include Const2
1245 STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE
1246 test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8])
1247
1248 test_check "clone"
1249 foo = Object.new
1250 def foo.test
1251 "test"
1252 end
1253 bar = foo.clone
1254 def bar.test2
1255 "test2"
1256 end
1257
1258 test_ok(bar.test2 == "test2")
1259 test_ok(bar.test == "test")
1260 test_ok(foo.test == "test")
1261
1262 begin
1263 foo.test2
1264 test_ok false
1265 rescue NoMethodError
1266 test_ok true
1267 end
1268
1269 module M001; end
1270 module M002; end
1271 module M003; include M002; end
1272 module M002; include M001; end
1273 module M003; include M002; end
1274
1275 test_ok(M003.ancestors == [M003, M002, M001])
1276
1277 test_check "marshal"
1278 $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)]
1279 $y = Marshal.dump($x)
1280 test_ok($x == Marshal.load($y))
1281
1282 StrClone=String.clone;
1283 test_ok(Marshal.load(Marshal.dump(StrClone.new("abc"))).type == StrClone)
1284
1285 test_check "pack"
1286
1287 $format = "c2x5CCxsdils_l_a6";
1288 # Need the expression in here to force ary[5] to be numeric. This avoids
1289 # test2 failing because ary2 goes str->numeric->str and ary does not.
1290 ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
1291 $x = ary.pack($format)
1292 ary2 = $x.unpack($format)
1293
1294 test_ok(ary.length == ary2.length)
1295 test_ok(ary.join(':') == ary2.join(':'))
1296 test_ok($x =~ /def/)
1297
1298 test_check "math"
1299 test_ok(Math.sqrt(4) == 2)
1300
1301 include Math
1302 test_ok(sqrt(4) == 2)
1303
1304 test_check "struct"
1305 struct_test = Struct.new("Test", :foo, :bar)
1306 test_ok(struct_test == Struct::Test)
1307
1308 test = struct_test.new(1, 2)
1309 test_ok(test.foo == 1 && test.bar == 2)
1310 test_ok(test[0] == 1 && test[1] == 2)
1311
1312 a, b = test.to_a
1313 test_ok(a == 1 && b == 2)
1314
1315 test[0] = 22
1316 test_ok(test.foo == 22)
1317
1318 test.bar = 47
1319 test_ok(test.bar == 47)
1320
1321 test_check "variable"
1322 test_ok($$.instance_of?(Fixnum))
1323
1324 # read-only variable
1325 begin
1326 $$ = 5
1327 test_ok false
1328 rescue NameError
1329 test_ok true
1330 end
1331
1332 foobar = "foobar"
1333 $_ = foobar
1334 test_ok($_ == foobar)
1335
1336 class Gods
1337 @@rule = "Uranus"
1338 def ruler0
1339 @@rule
1340 end
1341
1342 def self.ruler1 # <= per method definition style
1343 @@rule
1344 end
1345 class << self # <= multiple method definition style
1346 def ruler2
1347 @@rule
1348 end
1349 end
1350 end
1351
1352 module Olympians
1353 @@rule ="Zeus"
1354 def ruler3
1355 @@rule
1356 end
1357 end
1358
1359 class Titans < Gods
1360 @@rule = "Cronus"
1361 include Olympians # OK to cause warning (intentional)
1362 end
1363
1364 test_ok(Gods.new.ruler0 == "Cronus")
1365 test_ok(Gods.ruler1 == "Cronus")
1366 test_ok(Gods.ruler2 == "Cronus")
1367 test_ok(Titans.ruler1 == "Cronus")
1368 test_ok(Titans.ruler2 == "Cronus")
1369 atlas = Titans.new
1370 test_ok(atlas.ruler0 == "Cronus")
1371 test_ok(atlas.ruler3 == "Zeus")
1372
1373 test_check "trace"
1374 $x = 1234
1375 $y = 0
1376 trace_var :$x, proc{$y = $x}
1377 $x = 40414
1378 test_ok($y == $x)
1379
1380 untrace_var :$x
1381 $x = 19660208
1382 test_ok($y != $x)
1383
1384 trace_var :$x, proc{$x *= 2}
1385 $x = 5
1386 test_ok($x == 10)
1387
1388 untrace_var :$x
1389
1390 test_check "defined?"
1391
1392 test_ok(defined?($x)) # global variable
1393 test_ok(defined?($x) == 'global-variable')# returns description
1394
1395 foo=5
1396 test_ok(defined?(foo)) # local variable
1397
1398 test_ok(defined?(Array)) # constant
1399 test_ok(defined?(Object.new)) # method
1400 test_ok(!defined?(Object.print))# private method
1401 test_ok(defined?(1 == 2)) # operator expression
1402
1403 class Foo
1404 def foo
1405 p :foo
1406 end
1407 protected :foo
1408 def bar(f)
1409 test_ok(defined?(self.foo))
1410 test_ok(defined?(f.foo))
1411 end
1412 end
1413 f = Foo.new
1414 test_ok(defined?(f.foo) == nil)
1415 f.bar(f)
1416
1417 def defined_test
1418 return !defined?(yield)
1419 end
1420
1421 test_ok(defined_test) # not iterator
1422 test_ok(!defined_test{}) # called as iterator
1423
1424 test_check "alias"
1425 class Alias0
1426 def foo; "foo" end
1427 end
1428 class Alias1<Alias0
1429 alias bar foo
1430 def foo; "foo+" + super end
1431 end
1432 class Alias2<Alias1
1433 alias baz foo
1434 undef foo
1435 end
1436
1437 x = Alias2.new
1438 test_ok(x.bar == "foo")
1439 test_ok(x.baz == "foo+foo")
1440
1441 # test_check for cache
1442 test_ok(x.baz == "foo+foo")
1443
1444 class Alias3<Alias2
1445 def foo
1446 defined? super
1447 end
1448 def bar
1449 defined? super
1450 end
1451 def quux
1452 defined? super
1453 end
1454 end
1455 x = Alias3.new
1456 test_ok(!x.foo)
1457 test_ok(x.bar)
1458 test_ok(!x.quux)
1459
1460 test_check "gc"
1461 begin
1462 1.upto(10000) {
1463 tmp = [0,1,2,3,4,5,6,7,8,9]
1464 }
1465 tmp = nil
1466 test_ok true
1467 rescue
1468 test_ok false
1469 end
1470 class S
1471 def initialize(a)
1472 @a = a
1473 end
1474 end
1475 l=nil
1476 100000.times {
1477 l = S.new(l)
1478 }
1479 GC.start
1480 test_ok true # reach here or dumps core
1481 l = []
1482 100000.times {
1483 l.push([l])
1484 }
1485 GC.start
1486 test_ok true # reach here or dumps core
1487
1488 if $failed > 0
1489 printf "test: %d failed %d\n", $ntest, $failed
1490 else
1491 printf "end of test(test: %d)\n", $ntest
1492 end