diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-07-10 04:49:24 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-07-10 04:49:24 +0000 |
commit | c67a74570dd056569763f5015e0aafa7d454fca7 (patch) | |
tree | f1dda39296b6911752cd4b6446a21f2e54ff68e1 /sample | |
parent | 3043170b14875ce53f3a951d665279b12c2c63a8 (diff) |
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@820 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rw-r--r-- | sample/biorhythm.rb | 14 | ||||
-rw-r--r-- | sample/cal.rb | 2 | ||||
-rw-r--r-- | sample/test.rb | 747 |
3 files changed, 389 insertions, 374 deletions
diff --git a/sample/biorhythm.rb b/sample/biorhythm.rb index d2cbb113ec..146e5da244 100644 --- a/sample/biorhythm.rb +++ b/sample/biorhythm.rb @@ -11,6 +11,20 @@ # # +# probably based on: +# +# Newsgroups: comp.sources.misc,de.comp.sources.os9 +# From: [email protected] (Frank Kaefer) +# Subject: v41i126: br - Biorhythm v3.0, Part01/01 +# Message-ID: <[email protected]> +# Sender: [email protected] (Kent Landfield) +# Organization: Sterling Software +# Date: Tue, 1 Feb 1994 07:06:16 GMT +# +# Posting-number: Volume 41, Issue 126 +# Archive-name: br/part01 +# Environment: basic, dos, os9 + include Math require "date.rb" require "parsearg.rb" diff --git a/sample/cal.rb b/sample/cal.rb index c15bb29fc6..f256a01a39 100644 --- a/sample/cal.rb +++ b/sample/cal.rb @@ -1,4 +1,4 @@ -#! /usr/local/bin/ruby +#! /usr/bin/env ruby # cal.rb: Written by Tadayoshi Funaba 1998-2000 # $Id: cal.rb,v 1.10 2000/05/20 02:09:47 tadf Exp $ diff --git a/sample/test.rb b/sample/test.rb index 87106330cc..1106053c10 100644 --- a/sample/test.rb +++ b/sample/test.rb @@ -1,16 +1,16 @@ -#! /usr/local/bin/ruby +#! /usr/bin/env ruby $testnum=0 $ntest=0 $failed = 0 -def check(what) +def test_check(what) printf "%s\n", what $what = what $testnum = 0 end -def ok(cond) +def test_ok(cond) $testnum+=1 $ntest+=1 if cond @@ -24,84 +24,84 @@ end # make sure conditional operators work -check "assignment" +test_check "assignment" a=[]; a[0] ||= "bar"; -ok(a[0] == "bar") +test_ok(a[0] == "bar") h={}; h["foo"] ||= "bar"; -ok(h["foo"] == "bar") +test_ok(h["foo"] == "bar") aa = 5 aa ||= 25 -ok(aa == 5) +test_ok(aa == 5) bb ||= 25 -ok(bb == 25) +test_ok(bb == 25) cc &&=33 -ok(cc == nil) +test_ok(cc == nil) cc = 5 cc &&=44 -ok(cc == 44) +test_ok(cc == 44) -check "condition" +test_check "condition" $x = '0'; -$x == $x && ok(true) -$x != $x && ok(false) -$x == $x || ok(false) -$x != $x || ok(true) +$x == $x && test_ok(true) +$x != $x && test_ok(false) +$x == $x || test_ok(false) +$x != $x || test_ok(true) # first test to see if we can run the tests. -check "if/unless"; +test_check "if/unless"; $x = 'test'; -ok(if $x == $x then true else false end) +test_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) +test_ok(!$bad) +test_ok(unless $x != $x then true else false end) -check "case" +test_check "case" case 5 when 1, 2, 3, 4, 6, 7, 8 - ok(false) + test_ok(false) when 5 - ok(true) + test_ok(true) end case 5 when 5 - ok(true) + test_ok(true) when 1..10 - ok(false) + test_ok(false) end case 5 when 1..10 - ok(true) + test_ok(true) else - ok(false) + test_ok(false) end case 5 when 5 - ok(true) + test_ok(true) else - ok(false) + test_ok(false) end case "foobar" when /^f.*r$/ - ok(true) + test_ok(true) else - ok(false) + test_ok(false) end -check "while/until"; +test_check "while/until"; tmp = open("while_tmp", "w") tmp.print "tvi925\n"; @@ -114,13 +114,13 @@ tmp.close # test break tmp = open("while_tmp", "r") -ok(tmp.kind_of?(File)) +test_ok(tmp.kind_of?(File)) while tmp.gets() break if /vt100/ end -ok(!tmp.eof? && /vt100/) +test_ok(!tmp.eof? && /vt100/) tmp.close # test next @@ -130,7 +130,7 @@ while tmp.gets() next if /vt100/; $bad = 1 if /vt100/; end -ok(!(!tmp.eof? || /vt100/ || $bad)) +test_ok(!(!tmp.eof? || /vt100/ || $bad)) tmp.close # test redo @@ -146,7 +146,7 @@ while tmp.gets() $bad = 1 if /vt100/ $bad = 1 if /VT100/ end -ok(tmp.eof? && !$bad) +test_ok(tmp.eof? && !$bad) tmp.close sum=0 @@ -157,7 +157,7 @@ for i in 1..10 redo end end -ok(sum == 220) +test_ok(sum == 220) # test interval $bad = false @@ -169,27 +169,27 @@ while tmp.gets() break end end -ok(!$bad) +test_ok(!$bad) tmp.close File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` -ok(!File.exist?("while_tmp")) +test_ok(!File.exist?("while_tmp")) i = 0 until i>4 i+=1 end -ok(i>4) +test_ok(i>4) # exception handling -check "exception"; +test_check "exception"; begin raise "this must be handled" - ok(false) + test_ok(false) rescue - ok(true) + test_ok(true) end $bad = true @@ -199,10 +199,10 @@ rescue if $bad $bad = false retry - ok(false) + test_ok(false) end end -ok(true) +test_ok(true) # exception in rescue clause $string = "this must be handled no.3" @@ -212,9 +212,9 @@ begin rescue raise $string end - ok(false) + test_ok(false) rescue - ok(true) if $! == $string + test_ok(true) if $! == $string end # exception in ensure clause @@ -224,9 +224,9 @@ begin ensure raise "exception in ensure clause" end - ok(false) + test_ok(false) rescue - ok(true) + test_ok(true) end $bad = true @@ -238,7 +238,7 @@ begin end rescue end -ok(!$bad) +test_ok(!$bad) $bad = true begin @@ -249,7 +249,7 @@ begin end rescue end -ok(!$bad) +test_ok(!$bad) $bad = true while true @@ -259,104 +259,104 @@ while true $bad = false end end -ok(!$bad) +test_ok(!$bad) -ok(catch(:foo) { +test_ok(catch(:foo) { loop do loop do throw :foo, true break end break - ok(false) # should no reach here + test_ok(false) # should no reach here end false }) -check "array" -ok([1, 2] + [3, 4] == [1, 2, 3, 4]) -ok([1, 2] * 2 == [1, 2, 1, 2]) -ok([1, 2] * ":" == "1:2") +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") -ok([1, 2].hash == [1, 2].hash) +test_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]) +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]) $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]) +test_ok($x[2] == 2) +test_ok($x[1..3] == [1, 2, 3]) +test_ok($x[1,3] == [1, 2, 3]) $x[0, 2] = 10 -ok($x[0] == 10 && $x[1] == 2) +test_ok($x[0] == 10 && $x[1] == 2) $x[0, 0] = -1 -ok($x[0] == -1 && $x[1] == 10) +test_ok($x[0] == -1 && $x[1] == 10) $x[-1, 1] = 20 -ok($x[-1] == 20 && $x.pop == 20) +test_ok($x[-1] == 20 && $x.pop == 20) # array and/or -ok(([1,2,3]&[2,4,6]) == [2]) -ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6]) +test_ok(([1,2,3]&[2,4,6]) == [2]) +test_ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6]) # compact $x = [nil, 1, nil, nil, 5, nil, nil] $x.compact! -ok($x == [1, 5]) +test_ok($x == [1, 5]) # uniq $x = [1, 1, 4, 2, 5, 4, 5, 1, 2] $x.uniq! -ok($x == [1, 4, 2, 5]) +test_ok($x == [1, 4, 2, 5]) # empty? -ok(!$x.empty?) +test_ok(!$x.empty?) $x = [] -ok($x.empty?) +test_ok($x.empty?) # sort $x = ["it", "came", "to", "pass", "that", "..."] $x = $x.sort.join(" ") -ok($x == "... came it pass that to") +test_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]) +test_ok($x == [1,2,3,5,7]) $x.sort!{|a,b| b-a} # reverse sort -ok($x == [7,5,3,2,1]) +test_ok($x == [7,5,3,2,1]) # split test -$x = "The Book of Mormon" -ok($x.split(//).reverse!.join == $x.reverse) -ok($x.reverse == $x.reverse!) -ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1") +$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") $x = "a b c d" -ok($x.split == ['a', 'b', 'c', 'd']) -ok($x.split(' ') == ['a', 'b', 'c', 'd']) -ok(defined? "a".chomp) -ok("abc".scan(/./) == ["a", "b", "c"]) -ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]]) +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"]]) # non-greedy match -ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]]) +test_ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]]) $x = [1] -ok(($x * 5).join(":") == '1:1:1:1:1') -ok(($x * 1).join(":") == '1') -ok(($x * 0).join(":") == '') +test_ok(($x * 5).join(":") == '1:1:1:1:1') +test_ok(($x * 1).join(":") == '1') +test_ok(($x * 0).join(":") == '') *$x = (1..7).to_a -ok($x.size == 7) -ok($x == [1, 2, 3, 4, 5, 6, 7]) +test_ok($x.size == 7) +test_ok($x == [1, 2, 3, 4, 5, 6, 7]) -check "hash" +test_check "hash" $x = {1=>2, 2=>4, 3=>6} $y = {1, 2, 2, 4, 3, 6} -ok($x[1] == 2) +test_ok($x[1] == 2) -ok(begin +test_ok(begin for k,v in $y raise if k*2 != v end @@ -365,37 +365,37 @@ ok(begin false 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}) +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}) $z = $y.keys.join(":") -ok($z == "1:2:3") +test_ok($z == "1:2:3") $z = $y.values.join(":") -ok($z == "2:4:6") -ok($x == $y) +test_ok($z == "2:4:6") +test_ok($x == $y) $y.shift -ok($y.length == 2) +test_ok($y.length == 2) $z = [1,2] $y[$z] = 256 -ok($y[$z] == 256) +test_ok($y[$z] == 256) -check "iterator" +test_check "iterator" -ok(!iterator?) +test_ok(!iterator?) def ttt - ok(iterator?) + test_ok(iterator?) end ttt{} # yield at top level -ok(!defined?(yield)) +test_ok(!defined?(yield)) $x = [1, 2, 3, 4] $y = [] @@ -404,7 +404,7 @@ $y = [] for i in $x $y.push i end -ok($x == $y) +test_ok($x == $y) # nested iterator def tt @@ -414,7 +414,7 @@ def tt end tt{|i| break if i == 5} -ok(i == 5) +test_ok(i == 5) def tt2(dummy) yield 1 @@ -431,7 +431,7 @@ rescue ArgumentError $x = true rescue Exception end -ok($x) +test_ok($x) # iterator break/redo/next/retry done = true @@ -439,7 +439,7 @@ loop{ break done = false # should not reach here } -ok(done) +test_ok(done) done = false $bad = false @@ -449,7 +449,7 @@ loop { next $bad = true # should not reach here } -ok(!$bad) +test_ok(!$bad) done = false $bad = false @@ -459,14 +459,14 @@ loop { redo $bad = true # should not reach here } -ok(!$bad) +test_ok(!$bad) $x = [] for i in 1 .. 7 $x.push i end -ok($x.size == 7) -ok($x == [1, 2, 3, 4, 5, 6, 7]) +test_ok($x.size == 7) +test_ok($x == [1, 2, 3, 4, 5, 6, 7]) $done = false $x = [] @@ -477,8 +477,8 @@ for i in 1 .. 7 # see how retry works in iterator loop end $x.push(i) end -ok($x.size == 10) -ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]) +test_ok($x.size == 10) +test_ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]) # append method to built-in class class Array @@ -491,7 +491,7 @@ class Array end end $x = [[1,2],[3,4],[5,6]] -ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x}) +test_ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x}) class IterTest def initialize(e); @body = e; end @@ -508,60 +508,60 @@ class IterTest end IterTest.new([0]).each0 { |x| $x = x } -ok($x == 0) +test_ok($x == 0) IterTest.new([1]).each1 { |x| $x = x } -ok($x == 1) +test_ok($x == 1) IterTest.new([2]).each2 { |x| $x = x } -ok($x == [2]) +test_ok($x == [2]) IterTest.new([3]).each3 { |x| $x = x } -ok($x == 3) +test_ok($x == 3) IterTest.new([4]).each4 { |x| $x = x } -ok($x == 4) +test_ok($x == 4) IterTest.new([5]).each5 { |x| $x = x } -ok($x == 5) +test_ok($x == 5) IterTest.new([6]).each6 { |x| $x = x } -ok($x == [6]) +test_ok($x == [6]) IterTest.new([7]).each7 { |x| $x = x } -ok($x == 7) +test_ok($x == 7) IterTest.new([8]).each8 { |x| $x = x } -ok($x == 8) +test_ok($x == 8) IterTest.new([[0]]).each0 { |x| $x = x } -ok($x == [0]) +test_ok($x == [0]) IterTest.new([[1]]).each1 { |x| $x = x } -ok($x == 1) +test_ok($x == 1) IterTest.new([[2]]).each2 { |x| $x = x } -ok($x == [2]) +test_ok($x == [2]) IterTest.new([[3]]).each3 { |x| $x = x } -ok($x == 3) +test_ok($x == 3) IterTest.new([[4]]).each4 { |x| $x = x } -ok($x == [4]) +test_ok($x == [4]) IterTest.new([[5]]).each5 { |x| $x = x } -ok($x == 5) +test_ok($x == 5) IterTest.new([[6]]).each6 { |x| $x = x } -ok($x == [6]) +test_ok($x == [6]) IterTest.new([[7]]).each7 { |x| $x = x } -ok($x == 7) +test_ok($x == 7) IterTest.new([[8]]).each8 { |x| $x = x } -ok($x == [8]) +test_ok($x == [8]) IterTest.new([[0,0]]).each0 { |x| $x = x } -ok($x == [0,0]) +test_ok($x == [0,0]) IterTest.new([[8,8]]).each8 { |x| $x = x } -ok($x == [8,8]) - -check "float" -ok(2.6.floor == 2) -ok(-2.6.floor == -3) -ok(2.6.ceil == 3) -ok(-2.6.ceil == -2) -ok(2.6.truncate == 2) -ok(-2.6.truncate == -2) -ok(2.6.round == 3) -ok(-2.4.truncate == -2) -ok((13.4 % 1 - 0.4).abs < 0.0001) - -check "bignum" +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" def fact(n) return 1 if n == 0 f = 1 @@ -573,24 +573,24 @@ def fact(n) end fact(3) $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) +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) $x = -$x -ok($x == -815915283247897734345611269596115894272000000000) -ok(2-(2**32) == -(2**32-2)) -ok(2**32 - 5 == (2**32-3)-2) +test_ok($x == -815915283247897734345611269596115894272000000000) +test_ok(2-(2**32) == -(2**32-2)) +test_ok(2**32 - 5 == (2**32-3)-2) $good = true; for i in 1000..1014 $good = false if ((1<<i) != (2**i)) end -ok($good) +test_ok($good) $good = true; n1=1<<1000 @@ -598,7 +598,7 @@ for i in 1000..1014 $good = false if ((1<<i) != n1) n1 *= 2 end -ok($good) +test_ok($good) $good = true; n2=n1 @@ -607,7 +607,7 @@ for i in 1..10 n2 = n2 >> 1 $good = false if (n1 != n2) end -ok($good) +test_ok($good) $good = true; for i in 4000..4096 @@ -616,94 +616,94 @@ for i in 4000..4096 $good = false end end -ok($good) +test_ok($good) b = 10**80 a = b * 9 + 7 -ok(7 == a.modulo(b)) -ok(-b + 7 == a.modulo(-b)) -ok(b + -7 == (-a).modulo(b)) -ok(-7 == (-a).modulo(-b)) -ok(7 == a.remainder(b)) -ok(7 == a.remainder(-b)) -ok(-7 == (-a).remainder(b)) -ok(-7 == (-a).remainder(-b)) - -check "string & char" - -ok("abcd" == "abcd") -ok("abcd" =~ "abcd") -ok("abcd" === "abcd") +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") # compile time string concatenation -ok("ab" "cd" == "abcd") -ok("#{22}aa" "cd#{44}" == "22aacd44") -ok("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566") -ok("abc" !~ /^$/) -ok("abc\n" !~ /^$/) -ok("abc" !~ /^d*$/) -ok(("abc" =~ /d*$/) == 3) -ok("" =~ /^$/) -ok("\n" =~ /^$/) -ok("a\n\n" =~ /^$/) -ok("abcabc" =~ /.*a/ && $& == "abca") -ok("abcabc" =~ /.*c/ && $& == "abcabc") -ok("abcabc" =~ /.*?a/ && $& == "a") -ok("abcabc" =~ /.*?c/ && $& == "abc") -ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb") - -ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb") -ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb") -ok(/^(ab+)+/ =~ "ababb" && $& == "ababb") -ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb") - -ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") -ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") +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") $x = <<END; ABCD ABCD END $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3} -ok($x == "AC\nAC\n") +test_ok($x == "AC\nAC\n") -ok("foobar" =~ /foo(?=(bar)|(baz))/) -ok("foobaz" =~ /foo(?=(bar)|(baz))/) +test_ok("foobar" =~ /foo(?=(bar)|(baz))/) +test_ok("foobaz" =~ /foo(?=(bar)|(baz))/) $foo = "abc" -ok("#$foo = abc" == "abc = abc") -ok("#{$foo} = abc" == "abc = abc") +test_ok("#$foo = abc" == "abc = abc") +test_ok("#{$foo} = abc" == "abc = abc") foo = "abc" -ok("#{foo} = abc" == "abc = abc") +test_ok("#{foo} = abc" == "abc = abc") -ok('-' * 5 == '-----') -ok('-' * 1 == '-') -ok('-' * 0 == '') +test_ok('-' * 5 == '-----') +test_ok('-' * 1 == '-') +test_ok('-' * 0 == '') foo = '-' -ok(foo * 5 == '-----') -ok(foo * 1 == '-') -ok(foo * 0 == '') +test_ok(foo * 5 == '-----') +test_ok(foo * 1 == '-') +test_ok(foo * 0 == '') $x = "a.gif" -ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif") -ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif") -ok($x.sub(/.*\.([^\.]+)$/, '\2') == "") -ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab") -ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>") +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>") # character constants(assumes ASCII) -ok("a"[0] == ?a) -ok(?a == ?a) -ok(?\C-a == 1) -ok(?\M-a == 225) -ok(?\M-\C-a == 129) -ok("a".upcase![0] == ?A) -ok("A".downcase![0] == ?a) -ok("abc".tr!("a-z", "A-Z") == "ABC") -ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC") -ok("abcc".squeeze!("a-z") == "abc") -ok("abcd".delete!("bc") == "ad") +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") $x = "abcdef" $y = [ ?a, ?b, ?c, ?d, ?e, ?f ] @@ -714,43 +714,43 @@ $x.each_byte {|i| break end } -ok(!$bad) +test_ok(!$bad) s = "a string" s[0..s.size]="another string" -ok(s == "another string") +test_ok(s == "another string") -check "assignment" +test_check "assignment" a = nil -ok(defined?(a)) -ok(a == nil) +test_ok(defined?(a)) +test_ok(a == nil) # multiple asignment a, b = 1, 2 -ok(a == 1 && b == 2) +test_ok(a == 1 && b == 2) a, b = b, a -ok(a == 2 && b == 1) +test_ok(a == 2 && b == 1) a, = 1,2 -ok(a == 1) +test_ok(a == 1) a, *b = 1, 2, 3 -ok(a == 1 && b == [2, 3]) +test_ok(a == 1 && b == [2, 3]) a, (b, c), d = 1, [2, 3], 4 -ok(a == 1 && b == 2 && c == 3 && d == 4) +test_ok(a == 1 && b == 2 && c == 3 && d == 4) *a = 1, 2, 3 -ok(a == [1, 2, 3]) +test_ok(a == [1, 2, 3]) *a = 4 -ok(a == [4]) +test_ok(a == [4]) *a = nil -ok(a == [nil]) +test_ok(a == [nil]) -check "call" +test_check "call" def aaa(a, b=100, *rest) res = [a, b] res += rest if rest @@ -760,31 +760,31 @@ end # not enough argument begin aaa() # need at least 1 arg - ok(false) + test_ok(false) rescue - ok(true) + test_ok(true) end begin aaa # no arg given (exception raised) - ok(false) + test_ok(false) rescue - ok(true) + test_ok(true) end -ok(aaa(1) == [1, 100]) -ok(aaa(1, 2) == [1, 2]) -ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4]) -ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4]) +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]) -check "proc" +test_check "proc" $proc = proc{|i| i} -ok($proc.call(2) == 2) -ok($proc.call(3) == 3) +test_ok($proc.call(2) == 2) +test_ok($proc.call(3) == 3) $proc = proc{|i| i*2} -ok($proc.call(2) == 4) -ok($proc.call(3) == 6) +test_ok($proc.call(2) == 4) +test_ok($proc.call(3) == 6) proc{ iii=5 # nested local variable @@ -795,23 +795,23 @@ proc{ $x = iii # nested variables shared by procs } # scope of nested variables - ok(defined?(iii)) + test_ok(defined?(iii)) }.call -ok(!defined?(iii)) # out of scope +test_ok(!defined?(iii)) # out of scope $x=0 $proc.call(5) $proc2.call -ok($x == 5) +test_ok($x == 5) if defined? Process.kill - check "signal" + test_check "signal" $x = 0 trap "SIGINT", proc{|sig| $x = 2} Process.kill "SIGINT", $$ sleep 0.1 - ok($x == 2) + test_ok($x == 2) trap "SIGINT", proc{raise "Interrupt"} @@ -822,35 +822,35 @@ if defined? Process.kill rescue x = $! end - ok(x && /Interrupt/ =~ x) + test_ok(x && /Interrupt/ =~ x) end -check "eval" -ok(eval("") == nil) +test_check "eval" +test_ok(eval("") == nil) $bad=false eval 'while false; $bad = true; print "foo\n" end' -ok(!$bad) +test_ok(!$bad) -ok(eval('TRUE')) -ok(eval('true')) -ok(!eval('NIL')) -ok(!eval('nil')) -ok(!eval('FALSE')) -ok(!eval('false')) +test_ok(eval('TRUE')) +test_ok(eval('true')) +test_ok(!eval('NIL')) +test_ok(!eval('nil')) +test_ok(!eval('FALSE')) +test_ok(!eval('false')) -$foo = 'ok(true)' +$foo = 'test_ok(true)' begin eval $foo rescue - ok(false) + test_ok(false) end -ok(eval("$foo") == 'ok(true)') -ok(eval("true") == true) +test_ok(eval("$foo") == 'test_ok(true)') +test_ok(eval("true") == true) i = 5 -ok(eval("i == 5")) -ok(eval("i") == 5) -ok(eval("defined? i")) +test_ok(eval("i == 5")) +test_ok(eval("i") == 5) +test_ok(eval("defined? i")) # eval with binding def test_ev @@ -862,56 +862,56 @@ def test_ev end $x = test_ev -ok(eval("local1", $x) == "local1") # normal local var -ok(eval("local2", $x) == "local2") # nested local var +test_ok(eval("local1", $x) == "local1") # normal local var +test_ok(eval("local2", $x) == "local2") # nested local var $bad = true begin p eval("local1") rescue NameError # must raise error $bad = false end -ok(!$bad) +test_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 +test_ok(eval("EVTEST1", $x) == 25) # constant in module +test_ok(eval("evtest2", $x) == 125) # local var in module $bad = true begin eval("EVTEST1") rescue NameError # must raise error $bad = false end -ok(!$bad) +test_ok(!$bad) x = proc{} eval "i4 = 1", x -ok(eval("i4", x) == 1) +test_ok(eval("i4", x) == 1) x = proc{proc{}}.call eval "i4 = 22", x -ok(eval("i4", x) == 22) +test_ok(eval("i4", x) == 22) $x = [] x = proc{proc{}}.call eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x -ok($x[4].call == 8) +test_ok($x[4].call == 8) x = binding eval "i = 1", x -ok(eval("i", x) == 1) +test_ok(eval("i", x) == 1) x = proc{binding}.call eval "i = 22", x -ok(eval("i", x) == 22) +test_ok(eval("i", x) == 22) $x = [] x = proc{binding}.call eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x -ok($x[4].call == 8) +test_ok($x[4].call == 8) x = proc{binding}.call eval "for i6 in 1..1; j6=i6; end", x -ok(eval("defined? i6", x)) -ok(eval("defined? j6", x)) +test_ok(eval("defined? i6", x)) +test_ok(eval("defined? j6", x)) proc { p = binding @@ -919,44 +919,44 @@ proc { foo22 = 5 proc{foo11=22}.call proc{foo22=55}.call - ok(eval("foo11", p) == eval("foo11")) - ok(eval("foo11") == 1) - ok(eval("foo22", p) == eval("foo22")) - ok(eval("foo22") == 55) + test_ok(eval("foo11", p) == eval("foo11")) + test_ok(eval("foo11") == 1) + test_ok(eval("foo22", p) == eval("foo22")) + test_ok(eval("foo22") == 55) }.call p1 = proc{i7 = 0; proc{i7}}.call -ok(p1.call == 0) +test_ok(p1.call == 0) eval "i7=5", p1 -ok(p1.call == 5) -ok(!defined?(i7)) +test_ok(p1.call == 5) +test_ok(!defined?(i7)) p1 = proc{i7 = 0; proc{i7}}.call i7 = nil -ok(p1.call == 0) +test_ok(p1.call == 0) eval "i7=1", p1 -ok(p1.call == 1) +test_ok(p1.call == 1) eval "i7=5", p1 -ok(p1.call == 5) -ok(i7 == nil) +test_ok(p1.call == 5) +test_ok(i7 == nil) -check "system" -ok(`echo foobar` == "foobar\n") -ok(`./miniruby -e 'print "foobar"'` == 'foobar') +test_check "system" +test_ok(`echo foobar` == "foobar\n") +test_ok(`./miniruby -e 'print "foobar"'` == 'foobar') tmp = open("script_tmp", "w") tmp.print "print $zzz\n"; tmp.close -ok(`./miniruby -s script_tmp -zzz` == 'true') -ok(`./miniruby -s script_tmp -zzz=555` == '555') +test_ok(`./miniruby -s script_tmp -zzz` == 'true') +test_ok(`./miniruby -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 -ok(`./miniruby script_tmp -zzz=678` == '678') +test_ok(`./miniruby script_tmp -zzz=678` == '678') tmp = open("script_tmp", "w") tmp.print "this is a leading junk\n"; @@ -966,8 +966,8 @@ tmp.print "__END__\n"; tmp.print "this is a trailing junk\n"; tmp.close -ok(`./miniruby -x script_tmp` == 'nil') -ok(`./miniruby -x script_tmp -zzz=555` == '555') +test_ok(`./miniruby -x script_tmp` == 'nil') +test_ok(`./miniruby -x script_tmp -zzz=555` == '555') tmp = open("script_tmp", "w") for i in 1..5 @@ -985,20 +985,21 @@ while tmp.gets end end tmp.close -ok(done) +test_ok(done) File.unlink "script_tmp" or `/bin/rm -f "script_tmp"` File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"` $bad = false -for script in Dir["{lib,sample}/**/*.rb"] - unless `./miniruby -c #{script}`.chomp == "Syntax OK" +for script in Dir["{lib,sample,ext}/**/*.rb"] + `./miniruby -c #{script}` + unless $? $bad = true end end -ok(!$bad) +test_ok(!$bad) -check "const" +test_check "const" TEST1 = 1 TEST2 = 2 @@ -1014,13 +1015,13 @@ end include Const -ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]) +test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]) include Const2 STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE -ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]) +test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]) -check "clone" +test_check "clone" foo = Object.new def foo.test "test" @@ -1030,23 +1031,23 @@ def bar.test2 "test2" end -ok(bar.test2 == "test2") -ok(bar.test == "test") -ok(foo.test == "test") +test_ok(bar.test2 == "test2") +test_ok(bar.test == "test") +test_ok(foo.test == "test") begin foo.test2 - ok false + test_ok false rescue NameError - ok true + test_ok true end -check "marshal" +test_check "marshal" $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)] $y = Marshal.dump($x) -ok($x == Marshal.load($y)) +test_ok($x == Marshal.load($y)) -check "pack" +test_check "pack" $format = "c2x5CCxsdils_l_a6"; # Need the expression in here to force ary[5] to be numeric. This avoids @@ -1055,86 +1056,86 @@ ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"a $x = ary.pack($format) ary2 = $x.unpack($format) -ok(ary.length == ary2.length) -ok(ary.join(':') == ary2.join(':')) -ok($x =~ /def/) +test_ok(ary.length == ary2.length) +test_ok(ary.join(':') == ary2.join(':')) +test_ok($x =~ /def/) -check "math" -ok(Math.sqrt(4) == 2) +test_check "math" +test_ok(Math.sqrt(4) == 2) include Math -ok(sqrt(4) == 2) +test_ok(sqrt(4) == 2) -check "struct" +test_check "struct" struct_test = Struct.new("Test", :foo, :bar) -ok(struct_test == Struct::Test) +test_ok(struct_test == Struct::Test) test = struct_test.new(1, 2) -ok(test.foo == 1 && test.bar == 2) -ok(test[0] == 1 && test[1] == 2) +test_ok(test.foo == 1 && test.bar == 2) +test_ok(test[0] == 1 && test[1] == 2) a, b = test.to_a -ok(a == 1 && b == 2) +test_ok(a == 1 && b == 2) test[0] = 22 -ok(test.foo == 22) +test_ok(test.foo == 22) test.bar = 47 -ok(test.bar == 47) +test_ok(test.bar == 47) -check "variable" -ok($$.instance_of?(Fixnum)) +test_check "variable" +test_ok($$.instance_of?(Fixnum)) # read-only variable begin $$ = 5 - ok false + test_ok false rescue NameError - ok true + test_ok true end foobar = "foobar" $_ = foobar -ok($_ == foobar) +test_ok($_ == foobar) -check "trace" +test_check "trace" $x = 1234 $y = 0 trace_var :$x, proc{$y = $x} $x = 40414 -ok($y == $x) +test_ok($y == $x) untrace_var :$x $x = 19660208 -ok($y != $x) +test_ok($y != $x) trace_var :$x, proc{$x *= 2} $x = 5 -ok($x == 10) +test_ok($x == 10) untrace_var :$x -check "defined?" +test_check "defined?" -ok(defined?($x)) # global variable -ok(defined?($x) == 'global-variable')# returns description +test_ok(defined?($x)) # global variable +test_ok(defined?($x) == 'global-variable')# returns description foo=5 -ok(defined?(foo)) # local variable +test_ok(defined?(foo)) # local variable -ok(defined?(Array)) # constant -ok(defined?(Object.new)) # method -ok(!defined?(Object.print)) # private method -ok(defined?(1 == 2)) # operator expression +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 def defined_test return !defined?(yield) end -ok(defined_test) # not iterator -ok(!defined_test{}) # called as iterator +test_ok(defined_test) # not iterator +test_ok(!defined_test{}) # called as iterator -check "alias" +test_check "alias" class Alias0 def foo; "foo" end end @@ -1148,11 +1149,11 @@ class Alias2<Alias1 end x = Alias2.new -ok(x.bar == "foo") -ok(x.baz == "foo+foo") +test_ok(x.bar == "foo") +test_ok(x.baz == "foo+foo") -# check for cache -ok(x.baz == "foo+foo") +# test_check for cache +test_ok(x.baz == "foo+foo") class Alias3<Alias2 def foo @@ -1166,19 +1167,19 @@ class Alias3<Alias2 end end x = Alias3.new -ok(!x.foo) -ok(x.bar) -ok(!x.quux) +test_ok(!x.foo) +test_ok(x.bar) +test_ok(!x.quux) -check "gc" +test_check "gc" begin 1.upto(10000) { tmp = [0,1,2,3,4,5,6,7,8,9] } tmp = nil - ok true + test_ok true rescue - ok false + test_ok false end if $failed > 0 |