2000-07-10 00:49:24 -04:00
|
|
|
#! /usr/bin/env ruby
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$testnum=0
|
|
|
|
$ntest=0
|
|
|
|
$failed = 0
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
def test_check(what)
|
1998-01-16 07:13:05 -05:00
|
|
|
printf "%s\n", what
|
|
|
|
$what = what
|
|
|
|
$testnum = 0
|
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
def test_ok(cond)
|
1998-01-16 07:13:05 -05:00
|
|
|
$testnum+=1
|
|
|
|
$ntest+=1
|
|
|
|
if cond
|
|
|
|
printf "ok %d\n", $testnum
|
|
|
|
else
|
|
|
|
where = caller[0]
|
|
|
|
printf "not ok %s %d -- %s\n", $what, $testnum, where
|
|
|
|
$failed+=1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# make sure conditional operators work
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "assignment"
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
a=[]; a[0] ||= "bar";
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a[0] == "bar")
|
1999-01-19 23:59:39 -05:00
|
|
|
h={}; h["foo"] ||= "bar";
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(h["foo"] == "bar")
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
aa = 5
|
|
|
|
aa ||= 25
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(aa == 5)
|
1999-01-19 23:59:39 -05:00
|
|
|
bb ||= 25
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(bb == 25)
|
1999-01-19 23:59:39 -05:00
|
|
|
cc &&=33
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(cc == nil)
|
1999-01-19 23:59:39 -05:00
|
|
|
cc = 5
|
|
|
|
cc &&=44
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(cc == 44)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "condition"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = '0';
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
$x == $x && test_ok(true)
|
|
|
|
$x != $x && test_ok(false)
|
|
|
|
$x == $x || test_ok(false)
|
|
|
|
$x != $x || test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# first test to see if we can run the tests.
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "if/unless";
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = 'test';
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(if $x == $x then true else false end)
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
unless $x == $x
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
|
|
|
test_ok(unless $x != $x then true else false end)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "case"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case 5
|
|
|
|
when 1, 2, 3, 4, 6, 7, 8
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
when 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
case 5
|
|
|
|
when 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
when 1..10
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
case 5
|
|
|
|
when 1..10
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
case 5
|
|
|
|
when 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
case "foobar"
|
|
|
|
when /^f.*r$/
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "while/until";
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
tmp = open("while_tmp", "w")
|
|
|
|
tmp.print "tvi925\n";
|
|
|
|
tmp.print "tvi920\n";
|
|
|
|
tmp.print "vt100\n";
|
|
|
|
tmp.print "Amiga\n";
|
|
|
|
tmp.print "paper\n";
|
|
|
|
tmp.close
|
|
|
|
|
|
|
|
# test break
|
|
|
|
|
|
|
|
tmp = open("while_tmp", "r")
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(tmp.kind_of?(File))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while tmp.gets()
|
|
|
|
break if /vt100/
|
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!tmp.eof? && /vt100/)
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp.close
|
|
|
|
|
|
|
|
# test next
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp = open("while_tmp", "r")
|
|
|
|
while tmp.gets()
|
1999-01-19 23:59:39 -05:00
|
|
|
next if /vt100/;
|
|
|
|
$bad = 1 if /vt100/;
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!(!tmp.eof? || /vt100/ || $bad))
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp.close
|
|
|
|
|
|
|
|
# test redo
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp = open("while_tmp", "r")
|
|
|
|
while tmp.gets()
|
2000-01-31 22:12:21 -05:00
|
|
|
line = $_
|
|
|
|
$_ = gsub(/vt100/, 'VT100')
|
|
|
|
if $_ != line
|
1998-01-16 07:13:05 -05:00
|
|
|
gsub!('VT100', 'Vt100')
|
|
|
|
redo;
|
|
|
|
end
|
2000-03-13 02:18:45 -05:00
|
|
|
$bad = 1 if /vt100/
|
|
|
|
$bad = 1 if /VT100/
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(tmp.eof? && !$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp.close
|
|
|
|
|
|
|
|
sum=0
|
|
|
|
for i in 1..10
|
|
|
|
sum += i
|
|
|
|
i -= 1
|
|
|
|
if i > 0
|
|
|
|
redo
|
|
|
|
end
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(sum == 220)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# test interval
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp = open("while_tmp", "r")
|
|
|
|
while tmp.gets()
|
|
|
|
break unless 1..2
|
|
|
|
if /vt100/ || /Amiga/ || /paper/
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp.close
|
|
|
|
|
|
|
|
File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!File.exist?("while_tmp"))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
until i>4
|
|
|
|
i+=1
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(i>4)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
# exception handling
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "exception";
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "this must be handled"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "this must be handled no.2"
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
|
|
|
if $bad
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
retry
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# exception in rescue clause
|
|
|
|
$string = "this must be handled no.3"
|
|
|
|
begin
|
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "exception in rescue clause"
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
1999-08-13 01:45:20 -04:00
|
|
|
raise $string
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true) if $! == $string
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# exception in ensure clause
|
|
|
|
begin
|
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "this must be handled no.4"
|
1998-01-16 07:13:05 -05:00
|
|
|
ensure
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "exception in ensure clause"
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "this must be handled no.5"
|
1998-01-16 07:13:05 -05:00
|
|
|
ensure
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
begin
|
1999-08-13 01:45:20 -04:00
|
|
|
raise "this must be handled no.6"
|
1998-01-16 07:13:05 -05:00
|
|
|
ensure
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
|
|
|
while true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
break
|
|
|
|
ensure
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(catch(:foo) {
|
1999-01-19 23:59:39 -05:00
|
|
|
loop do
|
|
|
|
loop do
|
|
|
|
throw :foo, true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
break
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false) # should no reach here
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "array"
|
|
|
|
test_ok([1, 2] + [3, 4] == [1, 2, 3, 4])
|
|
|
|
test_ok([1, 2] * 2 == [1, 2, 1, 2])
|
|
|
|
test_ok([1, 2] * ":" == "1:2")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok([1, 2].hash == [1, 2].hash)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok([1,2,3] & [2,3,4] == [2,3])
|
|
|
|
test_ok([1,2,3] | [2,3,4] == [1,2,3,4])
|
|
|
|
test_ok([1,2,3] - [2,3] == [1])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = [0, 1, 2, 3, 4, 5]
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[2] == 2)
|
|
|
|
test_ok($x[1..3] == [1, 2, 3])
|
|
|
|
test_ok($x[1,3] == [1, 2, 3])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x[0, 2] = 10
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[0] == 10 && $x[1] == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x[0, 0] = -1
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[0] == -1 && $x[1] == 10)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x[-1, 1] = 20
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[-1] == 20 && $x.pop == 20)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
# array and/or
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(([1,2,3]&[2,4,6]) == [2])
|
|
|
|
test_ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
# compact
|
|
|
|
$x = [nil, 1, nil, nil, 5, nil, nil]
|
|
|
|
$x.compact!
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [1, 5])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
# uniq
|
|
|
|
$x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
|
|
|
|
$x.uniq!
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [1, 4, 2, 5])
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
# empty?
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$x.empty?)
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = []
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.empty?)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# sort
|
|
|
|
$x = ["it", "came", "to", "pass", "that", "..."]
|
|
|
|
$x = $x.sort.join(" ")
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == "... came it pass that to")
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = [2,5,3,1,7]
|
|
|
|
$x.sort!{|a,b| a<=>b} # sort with condition
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [1,2,3,5,7])
|
1998-01-16 07:13:05 -05:00
|
|
|
$x.sort!{|a,b| b-a} # reverse sort
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [7,5,3,2,1])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# split test
|
2000-07-10 00:49:24 -04:00
|
|
|
$x = "The Botest_ok of Mormon"
|
|
|
|
test_ok($x.split(//).reverse!.join == $x.reverse)
|
|
|
|
test_ok($x.reverse == $x.reverse!)
|
|
|
|
test_ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = "a b c d"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.split == ['a', 'b', 'c', 'd'])
|
|
|
|
test_ok($x.split(' ') == ['a', 'b', 'c', 'd'])
|
|
|
|
test_ok(defined? "a".chomp)
|
|
|
|
test_ok("abc".scan(/./) == ["a", "b", "c"])
|
|
|
|
test_ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]])
|
1998-01-16 07:19:22 -05:00
|
|
|
# non-greedy match
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = [1]
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(($x * 5).join(":") == '1:1:1:1:1')
|
|
|
|
test_ok(($x * 1).join(":") == '1')
|
|
|
|
test_ok(($x * 0).join(":") == '')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-18 01:09:05 -05:00
|
|
|
*$x = (1..7).to_a
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.size == 7)
|
|
|
|
test_ok($x == [1, 2, 3, 4, 5, 6, 7])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "hash"
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = {1=>2, 2=>4, 3=>6}
|
|
|
|
$y = {1, 2, 2, 4, 3, 6}
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[1] == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(begin
|
1998-01-16 07:13:05 -05:00
|
|
|
for k,v in $y
|
1999-08-13 01:45:20 -04:00
|
|
|
raise if k*2 != v
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
1998-01-16 07:19:22 -05:00
|
|
|
true
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
1998-01-16 07:19:22 -05:00
|
|
|
false
|
1998-01-16 07:13:05 -05:00
|
|
|
end)
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.length == 3)
|
|
|
|
test_ok($x.has_key?(1))
|
|
|
|
test_ok($x.has_value?(4))
|
|
|
|
test_ok($x.indexes(2,3) == [4,6])
|
|
|
|
test_ok($x == {1=>2, 2=>4, 3=>6})
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$z = $y.keys.join(":")
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($z == "1:2:3")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$z = $y.values.join(":")
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($z == "2:4:6")
|
|
|
|
test_ok($x == $y)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$y.shift
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($y.length == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$z = [1,2]
|
|
|
|
$y[$z] = 256
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($y[$z] == 256)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-31 03:37:47 -05:00
|
|
|
$x = [1,2,3]
|
|
|
|
$x[1,0] = $x
|
|
|
|
test_ok($x == [1,1,2,3,2,3])
|
|
|
|
|
|
|
|
$x = [1,2,3]
|
|
|
|
$x[-1,0] = $x
|
|
|
|
test_ok($x == [1,2,1,2,3,3])
|
|
|
|
|
|
|
|
$x = [1,2,3]
|
|
|
|
$x.concat($x)
|
|
|
|
test_ok($x == [1,2,3,1,2,3])
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "iterator"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!iterator?)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
def ttt
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(iterator?)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
ttt{}
|
|
|
|
|
|
|
|
# yield at top level
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!defined?(yield))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = [1, 2, 3, 4]
|
|
|
|
$y = []
|
|
|
|
|
|
|
|
# iterator over array
|
|
|
|
for i in $x
|
|
|
|
$y.push i
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == $y)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# nested iterator
|
|
|
|
def tt
|
|
|
|
1.upto(10) {|i|
|
|
|
|
yield i
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
tt{|i| break if i == 5}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(i == 5)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-10-15 04:52:18 -04:00
|
|
|
def tt2(dummy)
|
|
|
|
yield 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def tt3(&block)
|
|
|
|
tt2(raise(ArgumentError,""),&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
$x = false
|
|
|
|
begin
|
|
|
|
tt3{}
|
|
|
|
rescue ArgumentError
|
|
|
|
$x = true
|
|
|
|
rescue Exception
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x)
|
1999-10-15 04:52:18 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
# iterator break/redo/next/retry
|
1998-01-16 07:19:22 -05:00
|
|
|
done = true
|
1998-01-16 07:13:05 -05:00
|
|
|
loop{
|
|
|
|
break
|
1999-08-13 01:45:20 -04:00
|
|
|
done = false # should not reach here
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(done)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
done = false
|
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
loop {
|
|
|
|
break if done
|
1998-01-16 07:19:22 -05:00
|
|
|
done = true
|
1998-01-16 07:13:05 -05:00
|
|
|
next
|
1999-08-13 01:45:20 -04:00
|
|
|
$bad = true # should not reach here
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
done = false
|
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
loop {
|
|
|
|
break if done
|
1998-01-16 07:19:22 -05:00
|
|
|
done = true
|
1998-01-16 07:13:05 -05:00
|
|
|
redo
|
1999-08-13 01:45:20 -04:00
|
|
|
$bad = true # should not reach here
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = []
|
|
|
|
for i in 1 .. 7
|
|
|
|
$x.push i
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.size == 7)
|
|
|
|
test_ok($x == [1, 2, 3, 4, 5, 6, 7])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$done = false
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = []
|
|
|
|
for i in 1 .. 7 # see how retry works in iterator loop
|
|
|
|
if i == 4 and not $done
|
1998-01-16 07:19:22 -05:00
|
|
|
$done = true
|
1998-01-16 07:13:05 -05:00
|
|
|
retry
|
|
|
|
end
|
|
|
|
$x.push(i)
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.size == 10)
|
|
|
|
test_ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
# append method to built-in class
|
|
|
|
class Array
|
|
|
|
def iter_test1
|
|
|
|
collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
|
|
|
|
end
|
|
|
|
def iter_test2
|
|
|
|
a = collect{|e| [e, yield(e)]}
|
|
|
|
a.sort{|a,b|a[1]<=>b[1]}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
$x = [[1,2],[3,4],[5,6]]
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x})
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
class IterTest
|
|
|
|
def initialize(e); @body = e; end
|
|
|
|
|
|
|
|
def each0(&block); @body.each(&block); end
|
|
|
|
def each1(&block); @body.each { |*x| block.call(*x) } end
|
|
|
|
def each2(&block); @body.each { |*x| block.call(x) } end
|
|
|
|
def each3(&block); @body.each { |x| block.call(*x) } end
|
|
|
|
def each4(&block); @body.each { |x| block.call(x) } end
|
|
|
|
def each5; @body.each { |*x| yield(*x) } end
|
|
|
|
def each6; @body.each { |*x| yield(x) } end
|
|
|
|
def each7; @body.each { |x| yield(*x) } end
|
|
|
|
def each8; @body.each { |x| yield(x) } end
|
|
|
|
end
|
|
|
|
|
|
|
|
IterTest.new([0]).each0 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([1]).each1 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 1)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([2]).each2 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [2])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([3]).each3 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 3)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([4]).each4 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 4)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([5]).each5 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 5)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([6]).each6 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [6])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([7]).each7 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 7)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([8]).each8 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 8)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
IterTest.new([[0]]).each0 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [0])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[1]]).each1 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 1)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[2]]).each2 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [2])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[3]]).each3 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 3)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[4]]).each4 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [4])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[5]]).each5 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 5)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[6]]).each6 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [6])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[7]]).each7 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 7)
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[8]]).each8 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [8])
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
IterTest.new([[0,0]]).each0 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [0,0])
|
1999-01-19 23:59:39 -05:00
|
|
|
IterTest.new([[8,8]]).each8 { |x| $x = x }
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == [8,8])
|
|
|
|
|
|
|
|
test_check "float"
|
|
|
|
test_ok(2.6.floor == 2)
|
|
|
|
test_ok(-2.6.floor == -3)
|
|
|
|
test_ok(2.6.ceil == 3)
|
|
|
|
test_ok(-2.6.ceil == -2)
|
|
|
|
test_ok(2.6.truncate == 2)
|
|
|
|
test_ok(-2.6.truncate == -2)
|
|
|
|
test_ok(2.6.round == 3)
|
|
|
|
test_ok(-2.4.truncate == -2)
|
|
|
|
test_ok((13.4 % 1 - 0.4).abs < 0.0001)
|
|
|
|
|
|
|
|
test_check "bignum"
|
1998-01-16 07:13:05 -05:00
|
|
|
def fact(n)
|
|
|
|
return 1 if n == 0
|
|
|
|
f = 1
|
|
|
|
while n>0
|
|
|
|
f *= n
|
|
|
|
n -= 1
|
|
|
|
end
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
$x = fact(40)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == $x)
|
|
|
|
test_ok($x == fact(40))
|
|
|
|
test_ok($x < $x+2)
|
|
|
|
test_ok($x > $x-2)
|
|
|
|
test_ok($x == 815915283247897734345611269596115894272000000000)
|
|
|
|
test_ok($x != 815915283247897734345611269596115894272000000001)
|
|
|
|
test_ok($x+1 == 815915283247897734345611269596115894272000000001)
|
|
|
|
test_ok($x/fact(20) == 335367096786357081410764800000)
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = -$x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == -815915283247897734345611269596115894272000000000)
|
|
|
|
test_ok(2-(2**32) == -(2**32-2))
|
|
|
|
test_ok(2**32 - 5 == (2**32-3)-2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$good = true;
|
|
|
|
for i in 1000..1014
|
|
|
|
$good = false if ((1<<i) != (2**i))
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($good)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$good = true;
|
1998-01-16 07:13:05 -05:00
|
|
|
n1=1<<1000
|
1998-01-16 07:19:22 -05:00
|
|
|
for i in 1000..1014
|
|
|
|
$good = false if ((1<<i) != n1)
|
1998-01-16 07:13:05 -05:00
|
|
|
n1 *= 2
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($good)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$good = true;
|
1998-01-16 07:13:05 -05:00
|
|
|
n2=n1
|
|
|
|
for i in 1..10
|
|
|
|
n1 = n1 / 2
|
|
|
|
n2 = n2 >> 1
|
1998-01-16 07:19:22 -05:00
|
|
|
$good = false if (n1 != n2)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($good)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
$good = true;
|
|
|
|
for i in 4000..4096
|
1998-01-16 07:13:05 -05:00
|
|
|
n1 = 1 << i;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (n1**2-1) / (n1+1) != (n1-1)
|
2000-10-31 03:37:47 -05:00
|
|
|
p i
|
1999-01-19 23:59:39 -05:00
|
|
|
$good = false
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($good)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-06-23 03:05:59 -04:00
|
|
|
b = 10**80
|
|
|
|
a = b * 9 + 7
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(7 == a.modulo(b))
|
|
|
|
test_ok(-b + 7 == a.modulo(-b))
|
|
|
|
test_ok(b + -7 == (-a).modulo(b))
|
|
|
|
test_ok(-7 == (-a).modulo(-b))
|
|
|
|
test_ok(7 == a.remainder(b))
|
|
|
|
test_ok(7 == a.remainder(-b))
|
|
|
|
test_ok(-7 == (-a).remainder(b))
|
|
|
|
test_ok(-7 == (-a).remainder(-b))
|
|
|
|
|
|
|
|
test_check "string & char"
|
|
|
|
|
|
|
|
test_ok("abcd" == "abcd")
|
|
|
|
test_ok("abcd" =~ "abcd")
|
|
|
|
test_ok("abcd" === "abcd")
|
1999-10-15 04:52:18 -04:00
|
|
|
# compile time string concatenation
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("ab" "cd" == "abcd")
|
|
|
|
test_ok("#{22}aa" "cd#{44}" == "22aacd44")
|
|
|
|
test_ok("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566")
|
|
|
|
test_ok("abc" !~ /^$/)
|
|
|
|
test_ok("abc\n" !~ /^$/)
|
|
|
|
test_ok("abc" !~ /^d*$/)
|
|
|
|
test_ok(("abc" =~ /d*$/) == 3)
|
|
|
|
test_ok("" =~ /^$/)
|
|
|
|
test_ok("\n" =~ /^$/)
|
|
|
|
test_ok("a\n\n" =~ /^$/)
|
|
|
|
test_ok("abcabc" =~ /.*a/ && $& == "abca")
|
|
|
|
test_ok("abcabc" =~ /.*c/ && $& == "abcabc")
|
|
|
|
test_ok("abcabc" =~ /.*?a/ && $& == "a")
|
|
|
|
test_ok("abcabc" =~ /.*?c/ && $& == "abc")
|
|
|
|
test_ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb")
|
|
|
|
|
|
|
|
test_ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb")
|
|
|
|
test_ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb")
|
|
|
|
test_ok(/^(ab+)+/ =~ "ababb" && $& == "ababb")
|
|
|
|
test_ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb")
|
|
|
|
|
|
|
|
test_ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
|
|
|
|
test_ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
|
1999-08-13 01:45:20 -04:00
|
|
|
|
1999-10-15 04:52:18 -04:00
|
|
|
$x = <<END;
|
|
|
|
ABCD
|
|
|
|
ABCD
|
|
|
|
END
|
1999-08-13 01:45:20 -04:00
|
|
|
$x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == "AC\nAC\n")
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("foobar" =~ /foo(?=(bar)|(baz))/)
|
|
|
|
test_ok("foobaz" =~ /foo(?=(bar)|(baz))/)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$foo = "abc"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("#$foo = abc" == "abc = abc")
|
|
|
|
test_ok("#{$foo} = abc" == "abc = abc")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
foo = "abc"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("#{foo} = abc" == "abc = abc")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok('-' * 5 == '-----')
|
|
|
|
test_ok('-' * 1 == '-')
|
|
|
|
test_ok('-' * 0 == '')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
foo = '-'
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(foo * 5 == '-----')
|
|
|
|
test_ok(foo * 1 == '-')
|
|
|
|
test_ok(foo * 0 == '')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = "a.gif"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif")
|
|
|
|
test_ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif")
|
|
|
|
test_ok($x.sub(/.*\.([^\.]+)$/, '\2') == "")
|
|
|
|
test_ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab")
|
|
|
|
test_ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# character constants(assumes ASCII)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok("a"[0] == ?a)
|
|
|
|
test_ok(?a == ?a)
|
|
|
|
test_ok(?\C-a == 1)
|
|
|
|
test_ok(?\M-a == 225)
|
|
|
|
test_ok(?\M-\C-a == 129)
|
|
|
|
test_ok("a".upcase![0] == ?A)
|
|
|
|
test_ok("A".downcase![0] == ?a)
|
|
|
|
test_ok("abc".tr!("a-z", "A-Z") == "ABC")
|
|
|
|
test_ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC")
|
|
|
|
test_ok("abcc".squeeze!("a-z") == "abc")
|
|
|
|
test_ok("abcd".delete!("bc") == "ad")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = "abcdef"
|
|
|
|
$y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
$x.each_byte {|i|
|
|
|
|
if i != $y.shift
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
s = "a string"
|
|
|
|
s[0..s.size]="another string"
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(s == "another string")
|
2000-05-01 05:42:38 -04:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "assignment"
|
1998-01-16 07:13:05 -05:00
|
|
|
a = nil
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined?(a))
|
|
|
|
test_ok(a == nil)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# multiple asignment
|
|
|
|
a, b = 1, 2
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 1 && b == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
a, b = b, a
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 2 && b == 1)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
a, = 1,2
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 1)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
a, *b = 1, 2, 3
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 1 && b == [2, 3])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
a, (b, c), d = 1, [2, 3], 4
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 1 && b == 2 && c == 3 && d == 4)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
*a = 1, 2, 3
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == [1, 2, 3])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-18 01:09:05 -05:00
|
|
|
*a = 4
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == [4])
|
2000-01-18 01:09:05 -05:00
|
|
|
|
|
|
|
*a = nil
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == [nil])
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "call"
|
1998-01-16 07:13:05 -05:00
|
|
|
def aaa(a, b=100, *rest)
|
|
|
|
res = [a, b]
|
|
|
|
res += rest if rest
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
# not enough argument
|
|
|
|
begin
|
|
|
|
aaa() # need at least 1 arg
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
aaa # no arg given (exception raised)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(true)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(aaa(1) == [1, 100])
|
|
|
|
test_ok(aaa(1, 2) == [1, 2])
|
|
|
|
test_ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4])
|
|
|
|
test_ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "proc"
|
1998-01-16 07:13:05 -05:00
|
|
|
$proc = proc{|i| i}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($proc.call(2) == 2)
|
|
|
|
test_ok($proc.call(3) == 3)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$proc = proc{|i| i*2}
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($proc.call(2) == 4)
|
|
|
|
test_ok($proc.call(3) == 6)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
proc{
|
1999-01-19 23:59:39 -05:00
|
|
|
iii=5 # nested local variable
|
1998-01-16 07:13:05 -05:00
|
|
|
$proc = proc{|i|
|
|
|
|
iii = i
|
|
|
|
}
|
|
|
|
$proc2 = proc {
|
1999-01-19 23:59:39 -05:00
|
|
|
$x = iii # nested variables shared by procs
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
# scope of nested variables
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined?(iii))
|
1998-01-16 07:13:05 -05:00
|
|
|
}.call
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!defined?(iii)) # out of scope
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x=0
|
|
|
|
$proc.call(5)
|
|
|
|
$proc2.call
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 5)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if defined? Process.kill
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "signal"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
$x = 0
|
|
|
|
trap "SIGINT", proc{|sig| $x = 2}
|
|
|
|
Process.kill "SIGINT", $$
|
|
|
|
sleep 0.1
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
trap "SIGINT", proc{raise "Interrupt"}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
x = false
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
Process.kill "SIGINT", $$
|
|
|
|
sleep 0.1
|
|
|
|
rescue
|
|
|
|
x = $!
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(x && /Interrupt/ =~ x)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "eval"
|
|
|
|
test_ok(eval("") == nil)
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad=false
|
|
|
|
eval 'while false; $bad = true; print "foo\n" end'
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval('TRUE'))
|
|
|
|
test_ok(eval('true'))
|
|
|
|
test_ok(!eval('NIL'))
|
|
|
|
test_ok(!eval('nil'))
|
|
|
|
test_ok(!eval('FALSE'))
|
|
|
|
test_ok(!eval('false'))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
$foo = 'test_ok(true)'
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
eval $foo
|
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(false)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("$foo") == 'test_ok(true)')
|
|
|
|
test_ok(eval("true") == true)
|
1998-01-16 07:13:05 -05:00
|
|
|
i = 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("i == 5"))
|
|
|
|
test_ok(eval("i") == 5)
|
|
|
|
test_ok(eval("defined? i"))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# eval with binding
|
|
|
|
def test_ev
|
|
|
|
local1 = "local1"
|
|
|
|
lambda {
|
|
|
|
local2 = "local2"
|
|
|
|
return binding
|
|
|
|
}.call
|
|
|
|
end
|
|
|
|
|
|
|
|
$x = test_ev
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("local1", $x) == "local1") # normal local var
|
|
|
|
test_ok(eval("local2", $x) == "local2") # nested local var
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
p eval("local1")
|
|
|
|
rescue NameError # must raise error
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
module EvTest
|
|
|
|
EVTEST1 = 25
|
|
|
|
evtest2 = 125
|
|
|
|
$x = binding
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("EVTEST1", $x) == 25) # constant in module
|
|
|
|
test_ok(eval("evtest2", $x) == 125) # local var in module
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = true
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
eval("EVTEST1")
|
|
|
|
rescue NameError # must raise error
|
1998-01-16 07:19:22 -05:00
|
|
|
$bad = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
x = proc{}
|
|
|
|
eval "i4 = 1", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("i4", x) == 1)
|
1999-01-19 23:59:39 -05:00
|
|
|
x = proc{proc{}}.call
|
|
|
|
eval "i4 = 22", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("i4", x) == 22)
|
1999-01-19 23:59:39 -05:00
|
|
|
$x = []
|
|
|
|
x = proc{proc{}}.call
|
|
|
|
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[4].call == 8)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
x = binding
|
|
|
|
eval "i = 1", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("i", x) == 1)
|
1999-01-19 23:59:39 -05:00
|
|
|
x = proc{binding}.call
|
|
|
|
eval "i = 22", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("i", x) == 22)
|
1999-01-19 23:59:39 -05:00
|
|
|
$x = []
|
|
|
|
x = proc{binding}.call
|
|
|
|
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x[4].call == 8)
|
1999-08-13 01:45:20 -04:00
|
|
|
x = proc{binding}.call
|
|
|
|
eval "for i6 in 1..1; j6=i6; end", x
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("defined? i6", x))
|
|
|
|
test_ok(eval("defined? j6", x))
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
proc {
|
|
|
|
p = binding
|
|
|
|
eval "foo11 = 1", p
|
1999-08-13 01:45:20 -04:00
|
|
|
foo22 = 5
|
1999-01-19 23:59:39 -05:00
|
|
|
proc{foo11=22}.call
|
1999-08-13 01:45:20 -04:00
|
|
|
proc{foo22=55}.call
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(eval("foo11", p) == eval("foo11"))
|
|
|
|
test_ok(eval("foo11") == 1)
|
|
|
|
test_ok(eval("foo22", p) == eval("foo22"))
|
|
|
|
test_ok(eval("foo22") == 55)
|
1999-01-19 23:59:39 -05:00
|
|
|
}.call
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(p1.call == 0)
|
1999-08-13 01:45:20 -04:00
|
|
|
eval "i7=5", p1
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(p1.call == 5)
|
|
|
|
test_ok(!defined?(i7))
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
|
|
|
i7 = nil
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(p1.call == 0)
|
1999-08-13 01:45:20 -04:00
|
|
|
eval "i7=1", p1
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(p1.call == 1)
|
1999-08-13 01:45:20 -04:00
|
|
|
eval "i7=5", p1
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(p1.call == 5)
|
|
|
|
test_ok(i7 == nil)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "system"
|
|
|
|
test_ok(`echo foobar` == "foobar\n")
|
|
|
|
test_ok(`./miniruby -e 'print "foobar"'` == 'foobar')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
tmp = open("script_tmp", "w")
|
|
|
|
tmp.print "print $zzz\n";
|
|
|
|
tmp.close
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(`./miniruby -s script_tmp -zzz` == 'true')
|
|
|
|
test_ok(`./miniruby -s script_tmp -zzz=555` == '555')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
tmp = open("script_tmp", "w")
|
|
|
|
tmp.print "#! /usr/local/bin/ruby -s\n";
|
|
|
|
tmp.print "print $zzz\n";
|
|
|
|
tmp.close
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(`./miniruby script_tmp -zzz=678` == '678')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
tmp = open("script_tmp", "w")
|
|
|
|
tmp.print "this is a leading junk\n";
|
|
|
|
tmp.print "#! /usr/local/bin/ruby -s\n";
|
|
|
|
tmp.print "print $zzz\n";
|
|
|
|
tmp.print "__END__\n";
|
|
|
|
tmp.print "this is a trailing junk\n";
|
|
|
|
tmp.close
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(`./miniruby -x script_tmp` == 'nil')
|
|
|
|
test_ok(`./miniruby -x script_tmp -zzz=555` == '555')
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
tmp = open("script_tmp", "w")
|
|
|
|
for i in 1..5
|
|
|
|
tmp.print i, "\n"
|
|
|
|
end
|
|
|
|
tmp.close
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
`./miniruby -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
|
1998-01-16 07:19:22 -05:00
|
|
|
done = true
|
1998-01-16 07:13:05 -05:00
|
|
|
tmp = open("script_tmp", "r")
|
|
|
|
while tmp.gets
|
|
|
|
if $_.to_i % 5 != 0
|
1998-01-16 07:19:22 -05:00
|
|
|
done = false
|
1998-01-16 07:13:05 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tmp.close
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(done)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
|
|
|
|
File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
$bad = false
|
2000-07-10 00:49:24 -04:00
|
|
|
for script in Dir["{lib,sample,ext}/**/*.rb"]
|
|
|
|
`./miniruby -c #{script}`
|
|
|
|
unless $?
|
1999-01-19 23:59:39 -05:00
|
|
|
$bad = true
|
|
|
|
end
|
|
|
|
end
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!$bad)
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "const"
|
1998-01-16 07:13:05 -05:00
|
|
|
TEST1 = 1
|
|
|
|
TEST2 = 2
|
|
|
|
|
|
|
|
module Const
|
|
|
|
TEST3 = 3
|
|
|
|
TEST4 = 4
|
|
|
|
end
|
|
|
|
|
|
|
|
module Const2
|
|
|
|
TEST3 = 6
|
|
|
|
TEST4 = 8
|
|
|
|
end
|
|
|
|
|
|
|
|
include Const
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
include Const2
|
|
|
|
STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8])
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "clone"
|
1998-01-16 07:13:05 -05:00
|
|
|
foo = Object.new
|
|
|
|
def foo.test
|
|
|
|
"test"
|
|
|
|
end
|
|
|
|
bar = foo.clone
|
|
|
|
def bar.test2
|
|
|
|
"test2"
|
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(bar.test2 == "test2")
|
|
|
|
test_ok(bar.test == "test")
|
|
|
|
test_ok(foo.test == "test")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
foo.test2
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok false
|
2000-01-31 22:12:21 -05:00
|
|
|
rescue NameError
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "marshal"
|
1999-01-19 23:59:39 -05:00
|
|
|
$x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)]
|
|
|
|
$y = Marshal.dump($x)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == Marshal.load($y))
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "pack"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-30 00:24:17 -04:00
|
|
|
$format = "c2x5CCxsdils_l_a6";
|
1998-01-16 07:13:05 -05:00
|
|
|
# Need the expression in here to force ary[5] to be numeric. This avoids
|
|
|
|
# test2 failing because ary2 goes str->numeric->str and ary does not.
|
2000-05-30 00:24:17 -04:00
|
|
|
ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = ary.pack($format)
|
|
|
|
ary2 = $x.unpack($format)
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(ary.length == ary2.length)
|
|
|
|
test_ok(ary.join(':') == ary2.join(':'))
|
|
|
|
test_ok($x =~ /def/)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "math"
|
|
|
|
test_ok(Math.sqrt(4) == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
include Math
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(sqrt(4) == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "struct"
|
1998-01-16 07:13:05 -05:00
|
|
|
struct_test = Struct.new("Test", :foo, :bar)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(struct_test == Struct::Test)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
test = struct_test.new(1, 2)
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(test.foo == 1 && test.bar == 2)
|
|
|
|
test_ok(test[0] == 1 && test[1] == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-18 01:09:05 -05:00
|
|
|
a, b = test.to_a
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(a == 1 && b == 2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
test[0] = 22
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(test.foo == 22)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
test.bar = 47
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(test.bar == 47)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "variable"
|
|
|
|
test_ok($$.instance_of?(Fixnum))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
# read-only variable
|
|
|
|
begin
|
|
|
|
$$ = 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok false
|
2000-01-31 22:12:21 -05:00
|
|
|
rescue NameError
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
foobar = "foobar"
|
|
|
|
$_ = foobar
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($_ == foobar)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "trace"
|
1998-01-16 07:13:05 -05:00
|
|
|
$x = 1234
|
|
|
|
$y = 0
|
|
|
|
trace_var :$x, proc{$y = $x}
|
|
|
|
$x = 40414
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($y == $x)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
untrace_var :$x
|
|
|
|
$x = 19660208
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($y != $x)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
trace_var :$x, proc{$x *= 2}
|
|
|
|
$x = 5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok($x == 10)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
untrace_var :$x
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "defined?"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined?($x)) # global variable
|
|
|
|
test_ok(defined?($x) == 'global-variable')# returns description
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
foo=5
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined?(foo)) # local variable
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined?(Array)) # constant
|
|
|
|
test_ok(defined?(Object.new)) # method
|
|
|
|
test_ok(!defined?(Object.print)) # private method
|
|
|
|
test_ok(defined?(1 == 2)) # operator expression
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
def defined_test
|
|
|
|
return !defined?(yield)
|
|
|
|
end
|
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(defined_test) # not iterator
|
|
|
|
test_ok(!defined_test{}) # called as iterator
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "alias"
|
1998-01-16 07:13:05 -05:00
|
|
|
class Alias0
|
|
|
|
def foo; "foo" end
|
|
|
|
end
|
|
|
|
class Alias1<Alias0
|
|
|
|
alias bar foo
|
|
|
|
def foo; "foo+" + super end
|
|
|
|
end
|
|
|
|
class Alias2<Alias1
|
|
|
|
alias baz foo
|
|
|
|
undef foo
|
|
|
|
end
|
|
|
|
|
|
|
|
x = Alias2.new
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(x.bar == "foo")
|
|
|
|
test_ok(x.baz == "foo+foo")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
# test_check for cache
|
|
|
|
test_ok(x.baz == "foo+foo")
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
class Alias3<Alias2
|
|
|
|
def foo
|
|
|
|
defined? super
|
|
|
|
end
|
|
|
|
def bar
|
|
|
|
defined? super
|
|
|
|
end
|
|
|
|
def quux
|
|
|
|
defined? super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
x = Alias3.new
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok(!x.foo)
|
|
|
|
test_ok(x.bar)
|
|
|
|
test_ok(!x.quux)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-10 00:49:24 -04:00
|
|
|
test_check "gc"
|
1998-01-16 07:13:05 -05:00
|
|
|
begin
|
|
|
|
1.upto(10000) {
|
|
|
|
tmp = [0,1,2,3,4,5,6,7,8,9]
|
|
|
|
}
|
|
|
|
tmp = nil
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok true
|
1998-01-16 07:13:05 -05:00
|
|
|
rescue
|
2000-07-10 00:49:24 -04:00
|
|
|
test_ok false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if $failed > 0
|
|
|
|
printf "test: %d failed %d\n", $ntest, $failed
|
|
|
|
else
|
|
|
|
printf "end of test(test: %d)\n", $ntest
|
|
|
|
end
|