summaryrefslogtreecommitdiff
path: root/sample/test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/test.rb')
-rw-r--r--sample/test.rb981
1 files changed, 409 insertions, 572 deletions
diff --git a/sample/test.rb b/sample/test.rb
index 7f26433181..aacddbc56a 100644
--- a/sample/test.rb
+++ b/sample/test.rb
@@ -1,6 +1,8 @@
#! /usr/local/bin/ruby
$testnum=0
+$ntest=0
+$failed = 0
def check(what)
printf "%s\n", what
@@ -8,15 +10,16 @@ def check(what)
$testnum = 0
end
-def ok
+def ok(cond)
$testnum+=1
- printf "ok %d\n", $testnum
-end
-
-def notok
- $testnum+=1
- printf "not ok %s %d\n", $what, $testnum
- $failed = TRUE
+ $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
@@ -25,50 +28,62 @@ check "condition"
$x = '0';
-$x == $x && ok
-$x != $x && notok
-$x == $x || notok
-$x != $x || ok
+$x == $x && ok(TRUE)
+$x != $x && ok(FALSE)
+$x == $x || ok(FALSE)
+$x != $x || ok(TRUE)
# first test to see if we can run the tests.
-check "if";
+check "if/unless";
$x = 'test';
-if $x == $x then ok else notok end
-if $x != $x then notok else ok end
+ok(if $x == $x then TRUE else FALSE end)
+$bad = FALSE
+unless $x == $x
+ $bad = TRUE
+end
+ok(!$bad)
+ok(unless $x != $x then TRUE else FALSE end)
check "case"
case 5
when 1, 2, 3, 4, 6, 7, 8
- notok
+ ok(FALSE)
when 5
- ok
+ ok(TRUE)
end
case 5
when 5
- ok
+ ok(TRUE)
when 1..10
- notok
+ ok(FALSE)
+end
+
+case 5
+when 1..10
+ ok(TRUE)
+else
+ ok(FALSE)
end
case 5
when 5
- ok
+ ok(TRUE)
else
- notok
+ ok(FALSE)
end
case "foobar"
when /^f.*r$/
- ok
+ ok(TRUE)
else
- notok
+ ok(FALSE)
end
-check "while";
+check "while/until";
tmp = open("while_tmp", "w")
tmp.print "tvi925\n";
@@ -81,30 +96,23 @@ tmp.close
# test break
tmp = open("while_tmp", "r")
+ok(tmp.type == "File")
while tmp.gets()
break if /vt100/
end
-if !tmp.eof && /vt100/ then
- ok
-else
- notok
-end
+ok(!tmp.eof && /vt100/)
tmp.close
-# test continue
+# test next
$bad = FALSE
tmp = open("while_tmp", "r")
while tmp.gets()
- continue if /vt100/;
+ next if /vt100/;
$bad = 1 if /vt100/;
end
-if !tmp.eof || /vt100/ || $bad
- notok
-else
- ok
-end
+ok(!(!tmp.eof || /vt100/ || $bad))
tmp.close
# test redo
@@ -118,37 +126,39 @@ while tmp.gets()
$bad = 1 if /vt100/;
$bad = 1 if /VT100/;
end
-if !tmp.eof || $bad
- notok
-else
- ok
-end
+ok(tmp.eof && !$bad)
tmp.close
# test interval
$bad = FALSE
tmp = open("while_tmp", "r")
while tmp.gets()
- break if not 1..2
+ break unless 1..2
if /vt100/ || /Amiga/ || /paper/
$bad = TRUE
- notok
break
end
end
-ok if not $bad
+ok(!$bad)
tmp.close
File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
+ok(!File.exist?("while_tmp"))
+
+i = 0
+until i>4
+ i+=1
+end
+ok(i>4)
# exception handling
check "exception";
begin
fail "this must be handled"
- notok
+ ok(FALSE)
rescue
- ok
+ ok(TRUE)
end
$bad = TRUE
@@ -158,10 +168,10 @@ rescue
if $bad
$bad = FALSE
retry
- notok
+ ok(FALSE)
end
end
-ok
+ok(TRUE)
$bad = TRUE
$string = "this must be handled no.3"
@@ -170,9 +180,9 @@ begin
rescue
ensure
$bad = FALSE
- ok
+ ok(TRUE)
end
-notok if $bad || $! != $string
+ok(FALSE) if $bad || $! != $string
# exception in rescue clause
begin
@@ -181,164 +191,163 @@ begin
rescue
fail "exception in rescue clause"
end
- notok
+ ok(FALSE)
rescue
- ok
+ ok(TRUE)
end
-check "array"
-$x = [0, 1, 2, 3, 4, 5]
-if $x[2] == 2
- ok
-else
- notok
+# exception in ensure clause
+begin
+ begin
+ fail "this must be handled no.5"
+ ensure
+ fail "exception in ensure clause"
+ end
+ ok(FALSE)
+rescue
+ ok(TRUE)
end
-if $x[1..3] == [1, 2, 3]
- ok
-else
- notok
+$bad = TRUE
+begin
+ begin
+ fail "this must be handled no.5"
+ ensure
+ $bad = FALSE
+ end
+rescue
end
+ok(!$bad)
-if $x[1,3] == [1, 2, 3]
- ok
-else
- notok
+$bad = TRUE
+begin
+ begin
+ fail "this must be handled no.5"
+ ensure
+ $bad = FALSE
+ end
+rescue
end
+ok(!$bad)
-if [1, 2] + [3, 4] == [1, 2, 3, 4]
- ok
-else
- notok
+$bad = TRUE
+while TRUE
+ begin
+ break
+ ensure
+ $bad = FALSE
+ end
end
+ok(!$bad)
+
+check "array"
+ok([1, 2] + [3, 4] == [1, 2, 3, 4])
+ok([1, 2] * 2 == [1, 2, 1, 2])
+ok([1, 2] * ":" == "1:2")
+
+ok([1, 2].hash == [1, 2].hash)
+
+ok([1,2,3] & [2,3,4] == [2,3])
+ok([1,2,3] | [2,3,4] == [1,2,3,4])
+ok([1,2,3] - [2,3] == [1])
+
+$x = [0, 1, 2, 3, 4, 5]
+ok($x[2] == 2)
+ok($x[1..3] == [1, 2, 3])
+ok($x[1,3] == [1, 2, 3])
$x[0, 2] = 10
-if $x[0] == 10 && $x[1] == 2
- ok
-else
- notok
-end
+ok($x[0] == 10 && $x[1] == 2)
$x[0, 0] = -1
-if $x[0] == -1 && $x[1] == 10
- ok
-else
- notok
-end
+ok($x[0] == -1 && $x[1] == 10)
$x[-1, 1] = 20
-if $x[-1] == 20 && $x.pop == 20
- ok
-else
- notok
-end
+ok($x[-1] == 20 && $x.pop == 20)
+# compact
+$x = [nil, 1, nil, nil, 5, nil, nil]
+$x.compact!
+ok($x == [1, 5])
+
+# empty?
+ok(!$x.empty?)
+$x = []
+ok($x.empty?)
+
+# sort
$x = ["it", "came", "to", "pass", "that", "..."]
$x = $x.sort.join(" ")
-if $x == "... came it pass that to"
- ok
-else
- notok
-end
+ok($x == "... came it pass that to")
+$x = [2,5,3,1,7]
+$x.sort!{|a,b| a<=>b} # sort with condition
+ok($x == [1,2,3,5,7])
+$x.sort!{|a,b| b-a} # reverse sort
+ok($x == [7,5,3,2,1])
# split test
-if "1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1"
- ok
-else
- notok
-end
+$x = "The Book of Mormon"
+ok($x.split(//).reverse!.join == "nomroM fo kooB ehT")
+ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
+$x = "a b c d"
+ok($x.split == ['a', 'b', 'c', 'd'])
+ok($x.split(' ') == ['a', 'b', 'c', 'd'])
$x = [1]
-if ($x * 5).join(":") == '1:1:1:1:1' then ok else notok end
-if ($x * 1).join(":") == '1' then ok else notok end
-if ($x * 0).join(":") == '' then ok else notok end
+ok(($x * 5).join(":") == '1:1:1:1:1')
+ok(($x * 1).join(":") == '1')
+ok(($x * 0).join(":") == '')
+
+*$x = 1..7
+ok($x.size == 7)
+ok($x == [1, 2, 3, 4, 5, 6, 7])
check "hash"
$x = {1=>2, 2=>4, 3=>6}
$y = {1, 2, 2, 4, 3, 6}
-if $x[1] == 2
- ok
-else
- notok
-end
+ok($x[1] == 2)
-begin
- for k,v in $y
- fail if k*2 != v
- end
- ok
-rescue
- notok
-end
-
-if $x.length == 3
- ok
-else
- notok
-end
-
-if $x.has_key?(1)
- ok
-else
- notok
-end
-
-if $x.has_value?(4)
- ok
-else
- notok
-end
+ok(begin
+ for k,v in $y
+ fail if k*2 != v
+ end
+ TRUE
+ rescue
+ FALSE
+ end)
-if $x.indexes(2,3) == [4,6]
- ok
-else
- notok
-end
+ok($x.length == 3)
+ok($x.has_key?(1))
+ok($x.has_value?(4))
+ok($x.indexes(2,3) == [4,6])
+ok($x == (1=>2, 2=>4, 3=>6))
$z = $y.keys.join(":")
-if $z == "1:2:3"
- ok
-else
- notok
-end
+ok($z == "1:2:3")
$z = $y.values.join(":")
-if $z == "2:4:6"
- ok
-else
- notok
-end
-
-if $x == $y
- ok
-else
- notok
-end
+ok($z == "2:4:6")
+ok($x == $y)
$y.shift
-if $y.length == 2
- ok
-else
- notok
-end
+ok($y.length == 2)
+
+$z = [1,2]
+$y[$z] = 256
+ok($y[$z] == 256)
check "iterator"
-if iterator? then notok else ok end
+ok(!iterator?)
def ttt
- if iterator? then ok else notok end
+ ok(iterator?)
end
ttt{}
# yield at top level
-begin
- yield
- notok
-rescue
- ok
-end
+ok(!defined?(yield))
$x = [1, 2, 3, 4]
$y = []
@@ -347,11 +356,7 @@ $y = []
for i in $x
$y.push i
end
-if $x == $y
- ok
-else
- notok
-end
+ok($x == $y)
# nested iterator
def tt
@@ -361,59 +366,54 @@ def tt
end
tt{|i| break if i == 5}
-if i == 5
- ok
+ok(i == 5)
+
+# iterator break/redo/next/retry
+unless defined? loop
+ def loop
+ while TRUE
+ yield
+ end
+ end
+ ok(FALSE)
else
- notok
+ ok(TRUE)
end
-# iterator break/redo/continue/retry
done = TRUE
loop{
break
done = FALSE
- notok
}
-ok if done
+ok(done)
-done = TRUE
+done = FALSE
$bad = FALSE
loop {
- break if not done
- done = FALSE
- continue
+ break if done
+ done = TRUE
+ next
$bad = TRUE
}
-if $bad
- notok
-else
- ok
-end
+ok(!$bad)
-done = TRUE
+done = FALSE
$bad = FALSE
loop {
- break if not done
- done = FALSE
+ break if done
+ done = TRUE
redo
$bad = TRUE
}
-if $bad
- notok
-else
- ok
-end
+ok(!$bad)
$x = []
for i in 1 .. 7
- $x.push(i)
-end
-if $x.size == 7
- ok
-else
- notok
+ $x.push i
end
-# $x == [1, 2, 3, 4, 5, 6, 7]
+ok($x.size == 7)
+ok($x == [1, 2, 3, 4, 5, 6, 7])
+
$done = FALSE
$x = []
for i in 1 .. 7 # see how retry works in iterator loop
@@ -423,102 +423,56 @@ for i in 1 .. 7 # see how retry works in iterator loop
end
$x.push(i)
end
-# $x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]
-if $x.size == 10
- ok
-else
- notok
-end
+ok($x.size == 10)
+ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7])
check "bignum"
def fact(n)
return 1 if n == 0
return n*fact(n-1)
end
-if fact(40) == 815915283247897734345611269596115894272000000000
- ok
-else
- notok
-end
-if fact(40) == 815915283247897734345611269596115894272000000001
- notok
-else
- ok
-end
+$x = fact(40)
+ok($x == $x)
+ok($x == fact(40))
+ok($x < $x+2)
+ok($x > $x-2)
+ok($x == 815915283247897734345611269596115894272000000000)
+ok($x != 815915283247897734345611269596115894272000000001)
+ok($x+1 == 815915283247897734345611269596115894272000000001)
+ok($x/fact(20) == 335367096786357081410764800000)
+$x = -$x
+ok($x == -815915283247897734345611269596115894272000000000)
+ok(2-(2**32) == -(2**32-2))
+ok(2**32 - 5 == (2**32-3)-2)
check "string & char"
-if "abcd" == "abcd"
- ok
-else
- notok
-end
-
-if "abcd" =~ "abcd"
- ok
-else
- notok
-end
+ok("abcd" == "abcd")
+ok("abcd" =~ "abcd")
+ok("abcd" === "abcd")
$foo = "abc"
-if "#$foo = abc" == "abc = abc"
- ok
-else
- notok
-end
-
-if "#{$foo} = abc" == "abc = abc"
- ok
-else
- notok
-end
+ok("#$foo = abc" == "abc = abc")
+ok("#{$foo} = abc" == "abc = abc")
foo = "abc"
-if "#{foo} = abc" == "abc = abc"
- ok
-else
- notok
-end
+ok("#{foo} = abc" == "abc = abc")
-if '-' * 5 == '-----' then ok else notok end
-if '-' * 1 == '-' then ok else notok end
-if '-' * 0 == '' then ok else notok end
+ok('-' * 5 == '-----')
+ok('-' * 1 == '-')
+ok('-' * 0 == '')
foo = '-'
-if foo * 5 == '-----' then ok else notok end
-if foo * 1 == '-' then ok else notok end
-if foo * 0 == '' then ok else notok end
+ok(foo * 5 == '-----')
+ok(foo * 1 == '-')
+ok(foo * 0 == '')
# character constants(assumes ASCII)
-if "a"[0] == ?a
- ok
-else
- notok
-end
-
-if ?a == ?a
- ok
-else
- notok
-end
-
-if ?\C-a == 1
- ok
-else
- notok
-end
-
-if ?\M-a == 225
- ok
-else
- notok
-end
-
-if ?\M-\C-a == 129
- ok
-else
- notok
-end
+ok("a"[0] == ?a)
+ok(?a == ?a)
+ok(?\C-a == 1)
+ok(?\M-a == 225)
+ok(?\M-\C-a == 129)
$x = "abcdef"
$y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
@@ -529,33 +483,28 @@ $x.each_byte {|i|
break
end
}
-if not $bad
- ok
-else
- notok
-end
+ok(!$bad)
check "asignment"
a = nil
-if a == nil
- ok
-else
- notok
-end
+ok(defined?(a))
+ok(a == nil)
+# multiple asignment
a, b = 1, 2
-if a == 1 and b == 2 then
- ok
-else
- notok
-end
+ok(a == 1 && b == 2)
+
+a, b = b, a
+ok(a == 2 && b == 1)
+
+a, = 1,2
+ok(a == 1)
a, *b = 1, 2, 3
-if a == 1 and b == [2, 3] then
- ok
-else
- notok
-end
+ok(a == 1 && b == [2, 3])
+
+*a = 1, 2, 3
+ok(a == [1, 2, 3])
check "call"
def aaa(a, b=100, *rest)
@@ -564,191 +513,167 @@ def aaa(a, b=100, *rest)
return res
end
+# not enough argument
begin
- aaa()
- notok
+ aaa() # need at least 1 arg
+ ok(FALSE)
rescue
- ok
+ ok(TRUE)
end
begin
- aaa
- notok
+ aaa # no arg given (exception raised)
+ ok(FALSE)
rescue
- ok
+ ok(TRUE)
end
begin
if aaa(1) == [1, 100]
- ok
+ ok(TRUE)
else
fail
end
rescue
- notok
+ ok(FALSE)
end
begin
if aaa(1, 2) == [1, 2]
- ok
+ ok(TRUE)
else
fail
end
rescue
- notok
+ ok(FALSE)
end
-begin
- if aaa(1, 2, 3, 4) == [1, 2, 3, 4]
- ok
- else
- fail
- end
-rescue
- notok
-end
-
-begin
- if aaa(1, *[2, 3, 4]) == [1, 2, 3, 4]
- ok
- else
- fail
- end
-rescue
- notok
-end
+ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4])
+ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4])
check "proc"
$proc = proc{|i| i}
-if $proc.call(2) == 2
- ok
-else
- notok
-end
+ok($proc.call(2) == 2)
+ok($proc.call(3) == 3)
$proc = proc{|i| i*2}
-if $proc.call(2) == 4
- ok
-else
- notok
-end
+ok($proc.call(2) == 4)
+ok($proc.call(3) == 6)
proc{
iii=5 # dynamic local variable
- $proc = proc{ |i|
+ $proc = proc{|i|
iii = i
}
$proc2 = proc {
$x = iii # dynamic variables shared by procs
}
- if defined?(iii) # dynamic variables' scope
- ok
- else
- notok
- end
+ # scope of dynamic variables
+ ok(defined?(iii))
}.call
-if defined?(iii) # out of scope
- notok
-else
- ok
-end
+ok(!defined?(iii)) # out of scope
+
$x=0
$proc.call(5)
$proc2.call
-if $x == 5
- ok
-else
- notok
-end
+ok($x == 5)
-check "signal"
-begin
- kill "SIGINT", $$
- sleep 1
- notok
-rescue
- ok
-end
+if defined? Process.kill
+ check "signal"
-$x = 0
-trap "SIGINT", proc{|sig| $x = sig;fail}
-begin
- kill "SIGINT", $$
- sleep 1
- notok
-rescue
- if $x == 2
- ok
- else
- notok
- end
-end
+ $x = 0
+ trap "SIGINT", proc{|sig| $x = sig}
+ Process.kill "SIGINT", $$
+ sleep 0.1
+ ok($x == 2)
-$x = FALSE
-trap "SIGINT", "$x = TRUE;fail"
-begin
- kill "SIGINT", $$
- sleep 1
- notok
-rescue
- if $x
- ok
- else
- notok
+ trap "SIGINT", proc{fail "Interrupt"}
+
+ x = FALSE
+ begin
+ Process.kill "SIGINT", $$
+ sleep 0.1
+ rescue
+ x = $!
end
+ ok(x =~ /Interrupt/)
+else
+ ok(FALSE)
end
check "eval"
$bad=FALSE
-eval 'while FALSE; $bad = TRUE; print "foo\n" end
-if not $bad then ok else notok end'
+eval 'while FALSE; $bad = TRUE; print "foo\n" end'
+ok(!$bad)
-$foo = 'ok'
+ok(eval('TRUE'))
+
+$foo = 'ok(TRUE)'
begin
eval $foo
rescue
- notok
+ ok(FALSE)
end
-check "system"
-if `echo foobar` == "foobar\n"
- ok
-else
- notok
+ok(eval("$foo") == 'ok(TRUE)')
+ok(eval("TRUE") == TRUE)
+i = 5
+ok(eval("i == 5"))
+ok(eval("i") == 5)
+ok(eval("defined? i"))
+
+# eval with binding
+def test_ev
+ local1 = "local1"
+ lambda {
+ local2 = "local2"
+ return binding
+ }.call
end
-if `./ruby -e 'print "foobar"'` == 'foobar'
- ok
-else
- notok
+$x = test_ev
+ok(eval("local1", $x) == "local1") # static local var
+ok(eval("local2", $x) == "local2") # dynamic local var
+$bad = TRUE
+begin
+ p eval("local1")
+rescue NameError # must raise error
+ $bad = FALSE
end
+ok(!$bad)
+
+module EvTest
+ EVTEST1 = 25
+ evtest2 = 125
+ $x = binding
+end
+ok(eval("EVTEST1", $x) == 25) # constant in module
+ok(eval("evtest2", $x) == 125) # local var in module
+$bad = TRUE
+begin
+ eval("EVTEST1")
+rescue NameError # must raise error
+ $bad = FALSE
+end
+ok(!$bad)
+
+check "system"
+ok(`echo foobar` == "foobar\n")
+ok(`./ruby -e 'print "foobar"'` == 'foobar')
tmp = open("script_tmp", "w")
tmp.print "print $zzz\n";
tmp.close
-if `./ruby -s script_tmp -zzz` == 't'
- ok
-else
- notok
-end
-
-if `./ruby -s script_tmp -zzz=555` == '555'
- ok
-else
- notok
-end
+ok(`./ruby -s script_tmp -zzz` == 'TRUE')
+ok(`./ruby -s script_tmp -zzz=555` == '555')
tmp = open("script_tmp", "w")
tmp.print "#! /usr/local/bin/ruby -s\n";
tmp.print "print $zzz\n";
tmp.close
-if `./ruby script_tmp -zzz=678` == '678'
- ok
-else
- notok
-end
+ok(`./ruby script_tmp -zzz=678` == '678')
tmp = open("script_tmp", "w")
tmp.print "this is a leading junk\n";
@@ -758,17 +683,8 @@ tmp.print "__END__\n";
tmp.print "this is a trailing junk\n";
tmp.close
-if `./ruby -x script_tmp` == 'nil'
- ok
-else
- notok
-end
-
-if `./ruby -x script_tmp -zzz=555` == '555'
- ok
-else
- notok
-end
+ok(`./ruby -x script_tmp` == 'nil')
+ok(`./ruby -x script_tmp -zzz=555` == '555')
tmp = open("script_tmp", "w")
for i in 1..5
@@ -780,13 +696,14 @@ tmp.close
done = TRUE
tmp = open("script_tmp", "r")
while tmp.gets
+ print "c: ", $_
if $_.to_i % 5 != 0
done = FALSE
- notok
break
end
end
-ok if done
+tmp.close
+ok(done)
File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
@@ -807,19 +724,11 @@ end
include Const
-if [TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]
- ok
-else
- notok
-end
+ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4])
include Const2
-
-if [TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]
- ok
-else
- notok
-end
+STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE
+ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8])
check "clone"
foo = Object.new
@@ -831,200 +740,124 @@ def bar.test2
"test2"
end
-if bar.test2 == "test2"
- ok
-else
- notok
-end
-
-if bar.test == "test"
- ok
-else
- notok
-end
-
-if foo.test == "test"
- ok
-else
- notok
-end
+ok(bar.test2 == "test2")
+ok(bar.test == "test")
+ok(foo.test == "test")
begin
foo.test2
- notok
+ ok FALSE
rescue
- ok
+ ok TRUE
end
check "pack"
$format = "c2x5CCxsdila6";
# Need the expression in here to force ary[5] to be numeric. This avoids
-# test2 failing because ary2 goes str->numeric->str and ary doesn't.
+# test2 failing because ary2 goes str->numeric->str and ary does not.
ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef"]
$x = ary.pack($format)
ary2 = $x.unpack($format)
-if ary.length == ary2.length then ok else notok end
-
-if ary.join(':') == ary2.join(':') then ok else notok end
-
-if $x =~ /def/ then ok else notok end
+ok(ary.length == ary2.length)
+ok(ary.join(':') == ary2.join(':'))
+ok($x =~ /def/)
check "math"
-if Math.sqrt(4) == 2
- ok
-else
- notok
-end
+ok(Math.sqrt(4) == 2)
include Math
-if sqrt(4) == 2
- ok
-else
- notok
-end
+ok(sqrt(4) == 2)
check "struct"
struct_test = Struct.new("Test", :foo, :bar)
-if struct_test == Struct::Test
- ok
-else
- notok
-end
+ok(struct_test == Struct::Test)
+
test = struct_test.new(1, 2)
-if test.foo == 1 && test.bar == 2
- ok
-else
- notok
-end
-if test[0] == 1 && test[1] == 2
- ok
-else
- notok
-end
+ok(test.foo == 1 && test.bar == 2)
+ok(test[0] == 1 && test[1] == 2)
+
a, b = test
-if a == 1 && b == 2
- ok
-else
- notok
-end
+ok(a == 1 && b == 2)
+
test[0] = 22
-if test.foo == 22
- ok
-else
- notok
-end
+ok(test.foo == 22)
+
test.bar = 47
-if test.bar == 47
- ok
-else
- notok
-end
+ok(test.bar == 47)
check "variable"
-if $$.is_instance_of? Fixnum
- ok
-else
- notok
-end
+ok($$.instance_of?(Fixnum))
+# read-only variable
begin
$$ = 5
- notok
+ ok FALSE
rescue
- ok
+ ok TRUE
end
foobar = "foobar"
$_ = foobar
-if $_ == foobar
- ok
-else
- notok
-end
+ok($_ == foobar)
check "trace"
$x = 1234
$y = 0
trace_var :$x, proc{$y = $x}
$x = 40414
-if $y == $x
- ok
-else
- notok
-end
+ok($y == $x)
untrace_var :$x
$x = 19660208
-if $y != $x
- ok
-else
- notok
-end
+ok($y != $x)
trace_var :$x, proc{$x *= 2}
$x = 5
-if $x == 10
- ok
-else
- notok
-end
+ok($x == 10)
+
untrace_var :$x
check "defined?"
-if defined? $x
- ok
-else
- notok
-end
+
+ok(defined?($x)) # global variable
+ok(defined?($x) == 'global-variable')# returns description
foo=5
-if defined? foo
- ok
-else
- notok
-end
+ok(defined?(foo)) # local variable
-if defined? Array
- ok
-else
- notok
-end
+ok(defined?(Array)) # constant
+ok(defined?(Object.new)) # method
+ok(!defined?(Object.print)) # private method
+ok(defined?(1 == 2)) # operator expression
-if defined? Object.new
- ok
-else
- notok
+def defined_test
+ return !defined?(yield)
end
-if defined? 1 == 2
- ok
-else
- notok
-end
+ok(defined_test) # not iterator
+ok(!defined_test{}) # called as iterator
-if defined? fail
- ok
-else
- notok
+check "alias"
+class Alias0
+ def foo; "foo" end
end
-
-def defined_test
- return defined?(yield)
+class Alias1<Alias0
+ alias bar foo
+ def foo; "foo+" + super end
end
-
-if defined_test
- notok
-else
- ok
+class Alias2<Alias1
+ alias baz foo
+ undef foo
end
-if defined_test{}
- ok
-else
- notok
-end
+x = Alias2.new
+ok(x.bar == "foo")
+ok(x.baz == "foo+foo")
+
+# check for cache
+ok(x.baz == "foo+foo")
check "gc"
begin
@@ -1032,9 +865,13 @@ begin
tmp = [0,1,2,3,4,5,6,7,8,9]
}
tmp = nil
- ok
+ ok TRUE
rescue
- notok
+ ok FALSE
end
-print "end of test\n" if not $failed
+if $failed > 0
+ printf "test: %d failed %d\n", $ntest, $failed
+else
+ printf "end of test(test: %d)\n", $ntest
+end