From aaf5d9c03cd41818edb34a154ac570b7fe61687c Mon Sep 17 00:00:00 2001 From: nahi Date: Thu, 4 Sep 2003 16:18:59 +0000 Subject: [PATCH] * test/ruby: tests for ruby itself. * test/ruby/test_*.rb: split sample/test.rb into 28 test/unit testcases. some tests could not be translates... search '!!' mark to see it. * test/csv/test_csv.rb: should require 'csv', not '../lib/csv'. test runner should set load path correctly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 + test/csv/test_csv.rb | 2 +- test/ruby/test_alias.rb | 42 ++++ test/ruby/test_array.rb | 103 ++++++++ test/ruby/test_assignment.rb | 469 +++++++++++++++++++++++++++++++++++ test/ruby/test_bignum.rb | 100 ++++++++ test/ruby/test_call.rb | 32 +++ test/ruby/test_case.rb | 42 ++++ test/ruby/test_clone.rb | 35 +++ test/ruby/test_condition.rb | 18 ++ test/ruby/test_const.rb | 35 +++ test/ruby/test_defined.rb | 42 ++++ test/ruby/test_eval.rb | 124 +++++++++ test/ruby/test_exception.rb | 96 +++++++ test/ruby/test_float.rb | 45 ++++ test/ruby/test_hash.rb | 76 ++++++ test/ruby/test_ifunless.rb | 16 ++ test/ruby/test_iterator.rb | 383 ++++++++++++++++++++++++++++ test/ruby/test_marshal.rb | 32 +++ test/ruby/test_math.rb | 14 ++ test/ruby/test_pack.rb | 21 ++ test/ruby/test_path.rb | 48 ++++ test/ruby/test_proc.rb | 45 ++++ test/ruby/test_signal.rb | 26 ++ test/ruby/test_stringchar.rb | 116 +++++++++ test/ruby/test_struct.rb | 23 ++ test/ruby/test_system.rb | 77 ++++++ test/ruby/test_trace.rb | 23 ++ test/ruby/test_variable.rb | 58 +++++ test/ruby/test_whileuntil.rb | 82 ++++++ 30 files changed, 2234 insertions(+), 1 deletion(-) create mode 100644 test/ruby/test_alias.rb create mode 100644 test/ruby/test_array.rb create mode 100644 test/ruby/test_assignment.rb create mode 100644 test/ruby/test_bignum.rb create mode 100644 test/ruby/test_call.rb create mode 100644 test/ruby/test_case.rb create mode 100644 test/ruby/test_clone.rb create mode 100644 test/ruby/test_condition.rb create mode 100644 test/ruby/test_const.rb create mode 100644 test/ruby/test_defined.rb create mode 100644 test/ruby/test_eval.rb create mode 100644 test/ruby/test_exception.rb create mode 100644 test/ruby/test_float.rb create mode 100644 test/ruby/test_hash.rb create mode 100644 test/ruby/test_ifunless.rb create mode 100644 test/ruby/test_iterator.rb create mode 100644 test/ruby/test_marshal.rb create mode 100644 test/ruby/test_math.rb create mode 100644 test/ruby/test_pack.rb create mode 100644 test/ruby/test_path.rb create mode 100644 test/ruby/test_proc.rb create mode 100644 test/ruby/test_signal.rb create mode 100644 test/ruby/test_stringchar.rb create mode 100644 test/ruby/test_struct.rb create mode 100644 test/ruby/test_system.rb create mode 100644 test/ruby/test_trace.rb create mode 100644 test/ruby/test_variable.rb create mode 100644 test/ruby/test_whileuntil.rb diff --git a/ChangeLog b/ChangeLog index d94465ecbb..d34b303cc6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Wed Sep 5 01:10:11 2003 NAKAMURA, Hiroshi + + * test/ruby: tests for ruby itself. + + * test/ruby/test_*.rb: split sample/test.rb into 28 test/unit testcases. + some tests could not be translates... search '!!' mark to see it. + + * test/csv/test_csv.rb: should require 'csv', not '../lib/csv'. test + runner should set load path correctly. + Wed Sep 5 01:03:59 2003 NAKAMURA, Hiroshi * test/csv/test_csv.rb: close opened files for CSV::IOBuf explicitly. diff --git a/test/csv/test_csv.rb b/test/csv/test_csv.rb index f7dff34550..f1ba35a66f 100644 --- a/test/csv/test_csv.rb +++ b/test/csv/test_csv.rb @@ -2,7 +2,7 @@ require 'test/unit' require 'tempfile' require 'fileutils' -require '../lib/csv' +require 'csv' class CSV class StreamBuf diff --git a/test/ruby/test_alias.rb b/test/ruby/test_alias.rb new file mode 100644 index 0000000000..0e94e14608 --- /dev/null +++ b/test/ruby/test_alias.rb @@ -0,0 +1,42 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestAlias < Test::Unit::TestCase + class Alias0 + def foo; "foo" end + end + class Alias1b} # sort with condition + assert($x == [1,2,3,5,7]) + $x.sort!{|a,b| b-a} # reverse sort + assert($x == [7,5,3,2,1]) + end + + def test_split + $x = "The Boassert of Mormon" + assert($x.split(//).reverse!.join == $x.reverse) + assert($x.reverse == $x.reverse!) + assert("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1") + $x = "a b c d" + assert($x.split == ['a', 'b', 'c', 'd']) + assert($x.split(' ') == ['a', 'b', 'c', 'd']) + end + + def test_misc + assert(defined? "a".chomp) + assert("abc".scan(/./) == ["a", "b", "c"]) + assert("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]]) + # non-greedy match + assert("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]]) + + $x = [1] + assert(($x * 5).join(":") == '1:1:1:1:1') + assert(($x * 1).join(":") == '1') + assert(($x * 0).join(":") == '') + + *$x = *(1..7).to_a + assert($x.size == 7) + assert($x == [1, 2, 3, 4, 5, 6, 7]) + + $x = [1,2,3] + $x[1,0] = $x + assert($x == [1,1,2,3,2,3]) + + $x = [1,2,3] + $x[-1,0] = $x + assert($x == [1,2,1,2,3,3]) + + $x = [1,2,3] + $x.concat($x) + assert($x == [1,2,3,1,2,3]) + end +end diff --git a/test/ruby/test_assignment.rb b/test/ruby/test_assignment.rb new file mode 100644 index 0000000000..faaca4775d --- /dev/null +++ b/test/ruby/test_assignment.rb @@ -0,0 +1,469 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestAssignment < Test::Unit::TestCase + def test_assign + a=[]; a[0] ||= "bar"; + assert(a[0] == "bar") + h={}; h["foo"] ||= "bar"; + assert(h["foo"] == "bar") + + aa = 5 + aa ||= 25 + assert(aa == 5) + bb ||= 25 + assert(bb == 25) + cc &&=33 + assert(cc == nil) + cc = 5 + cc &&=44 + assert(cc == 44) + + a = nil; assert(a == nil) + a = 1; assert(a == 1) + a = []; assert(a == []) + a = [1]; assert(a == [1]) + a = [nil]; assert(a == [nil]) + a = [[]]; assert(a == [[]]) + a = [1,2]; assert(a == [1,2]) + a = [*[]]; assert(a == []) + a = [*[1]]; assert(a == [1]) + a = [*[1,2]]; assert(a == [1,2]) + + a = *nil; assert(a == nil) + a = *1; assert(a == 1) + a = *[]; assert(a == nil) + a = *[1]; assert(a == 1) + a = *[nil]; assert(a == nil) + a = *[[]]; assert(a == []) + a = *[1,2]; assert(a == [1,2]) + a = *[*[]]; assert(a == nil) + a = *[*[1]]; assert(a == 1) + a = *[*[1,2]]; assert(a == [1,2]) + + *a = nil; assert(a == [nil]) + *a = 1; assert(a == [1]) + *a = []; assert(a == [[]]) + *a = [1]; assert(a == [[1]]) + *a = [nil]; assert(a == [[nil]]) + *a = [[]]; assert(a == [[[]]]) + *a = [1,2]; assert(a == [[1,2]]) + *a = [*[]]; assert(a == [[]]) + *a = [*[1]]; assert(a == [[1]]) + *a = [*[1,2]]; assert(a == [[1,2]]) + + *a = *nil; assert(a == [nil]) + *a = *1; assert(a == [1]) + *a = *[]; assert(a == []) + *a = *[1]; assert(a == [1]) + *a = *[nil]; assert(a == [nil]) + *a = *[[]]; assert(a == [[]]) + *a = *[1,2]; assert(a == [1,2]) + *a = *[*[]]; assert(a == []) + *a = *[*[1]]; assert(a == [1]) + *a = *[*[1,2]]; assert(a == [1,2]) + + a,b,*c = nil; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = 1; assert([a,b,c] == [1,nil,[]]) + a,b,*c = []; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = [1]; assert([a,b,c] == [1,nil,[]]) + a,b,*c = [nil]; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = [[]]; assert([a,b,c] == [[],nil,[]]) + a,b,*c = [1,2]; assert([a,b,c] == [1,2,[]]) + a,b,*c = [*[]]; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = [*[1]]; assert([a,b,c] == [1,nil,[]]) + a,b,*c = [*[1,2]]; assert([a,b,c] == [1,2,[]]) + + a,b,*c = *nil; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = *1; assert([a,b,c] == [1,nil,[]]) + a,b,*c = *[]; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = *[1]; assert([a,b,c] == [1,nil,[]]) + a,b,*c = *[nil]; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = *[[]]; assert([a,b,c] == [[],nil,[]]) + a,b,*c = *[1,2]; assert([a,b,c] == [1,2,[]]) + a,b,*c = *[*[]]; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = *[*[1]]; assert([a,b,c] == [1,nil,[]]) + a,b,*c = *[*[1,2]]; assert([a,b,c] == [1,2,[]]) + end + + def test_yield + def f; yield nil; end; f {|a| assert(a == nil)} + def f; yield 1; end; f {|a| assert(a == 1)} + def f; yield []; end; f {|a| assert(a == [])} + def f; yield [1]; end; f {|a| assert(a == [1])} + def f; yield [nil]; end; f {|a| assert(a == [nil])} + def f; yield [[]]; end; f {|a| assert(a == [[]])} + def f; yield [*[]]; end; f {|a| assert(a == [])} + def f; yield [*[1]]; end; f {|a| assert(a == [1])} + def f; yield [*[1,2]]; end; f {|a| assert(a == [1,2])} + + def f; yield *nil; end; f {|a| assert(a == nil)} + def f; yield *1; end; f {|a| assert(a == 1)} + def f; yield *[1]; end; f {|a| assert(a == 1)} + def f; yield *[nil]; end; f {|a| assert(a == nil)} + def f; yield *[[]]; end; f {|a| assert(a == [])} + def f; yield *[*[1]]; end; f {|a| assert(a == 1)} + + def f; yield; end; f {|*a| assert(a == [])} + def f; yield nil; end; f {|*a| assert(a == [nil])} + def f; yield 1; end; f {|*a| assert(a == [1])} + def f; yield []; end; f {|*a| assert(a == [[]])} + def f; yield [1]; end; f {|*a| assert(a == [[1]])} + def f; yield [nil]; end; f {|*a| assert(a == [[nil]])} + def f; yield [[]]; end; f {|*a| assert(a == [[[]]])} + def f; yield [1,2]; end; f {|*a| assert(a == [[1,2]])} + def f; yield [*[]]; end; f {|*a| assert(a == [[]])} + def f; yield [*[1]]; end; f {|*a| assert(a == [[1]])} + def f; yield [*[1,2]]; end; f {|*a| assert(a == [[1,2]])} + + def f; yield *nil; end; f {|*a| assert(a == [nil])} + def f; yield *1; end; f {|*a| assert(a == [1])} + def f; yield *[]; end; f {|*a| assert(a == [])} + def f; yield *[1]; end; f {|*a| assert(a == [1])} + def f; yield *[nil]; end; f {|*a| assert(a == [nil])} + def f; yield *[[]]; end; f {|*a| assert(a == [[]])} + def f; yield *[*[]]; end; f {|*a| assert(a == [])} + def f; yield *[*[1]]; end; f {|*a| assert(a == [1])} + def f; yield *[*[1,2]]; end; f {|*a| assert(a == [1,2])} + + def f; yield; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield nil; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield 1; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield []; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield [1]; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield [nil]; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield [[]]; end; f {|a,b,*c| assert([a,b,c] == [[],nil,[]])} + def f; yield [*[]]; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield [*[1]]; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield [*[1,2]]; end; f {|a,b,*c| assert([a,b,c] == [1,2,[]])} + + def f; yield *nil; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield *1; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield *[]; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield *[1]; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield *[nil]; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield *[[]]; end; f {|a,b,*c| assert([a,b,c] == [[],nil,[]])} + def f; yield *[*[]]; end; f {|a,b,*c| assert([a,b,c] == [nil,nil,[]])} + def f; yield *[*[1]]; end; f {|a,b,*c| assert([a,b,c] == [1,nil,[]])} + def f; yield *[*[1,2]]; end; f {|a,b,*c| assert([a,b,c] == [1,2,[]])} + end + + def test_return + def r; return; end; a = r(); assert(a == nil) + def r; return nil; end; a = r(); assert(a == nil) + def r; return 1; end; a = r(); assert(a == 1) + def r; return []; end; a = r(); assert(a == []) + def r; return [1]; end; a = r(); assert(a == [1]) + def r; return [nil]; end; a = r(); assert(a == [nil]) + def r; return [[]]; end; a = r(); assert(a == [[]]) + def r; return [*[]]; end; a = r(); assert(a == []) + def r; return [*[1]]; end; a = r(); assert(a == [1]) + def r; return [*[1,2]]; end; a = r(); assert(a == [1,2]) + + def r; return *nil; end; a = r(); assert(a == nil) + def r; return *1; end; a = r(); assert(a == 1) + def r; return *[]; end; a = r(); assert(a == nil) + def r; return *[1]; end; a = r(); assert(a == 1) + def r; return *[nil]; end; a = r(); assert(a == nil) + def r; return *[[]]; end; a = r(); assert(a == []) + def r; return *[*[]]; end; a = r(); assert(a == nil) + def r; return *[*[1]]; end; a = r(); assert(a == 1) + def r; return *[*[1,2]]; end; a = r(); assert(a == [1,2]) + + def r; return *nil; end; a = *r(); assert(a == nil) + def r; return *1; end; a = *r(); assert(a == 1) + def r; return *[]; end; a = *r(); assert(a == nil) + def r; return *[1]; end; a = *r(); assert(a == 1) + def r; return *[nil]; end; a = *r(); assert(a == nil) + def r; return *[[]]; end; a = *r(); assert(a == nil) + def r; return *[*[]]; end; a = *r(); assert(a == nil) + def r; return *[*[1]]; end; a = *r(); assert(a == 1) + def r; return *[*[1,2]]; end; a = *r(); assert(a == [1,2]) + + def r; return; end; *a = r(); assert(a == [nil]) + def r; return nil; end; *a = r(); assert(a == [nil]) + def r; return 1; end; *a = r(); assert(a == [1]) + def r; return []; end; *a = r(); assert(a == [[]]) + def r; return [1]; end; *a = r(); assert(a == [[1]]) + def r; return [nil]; end; *a = r(); assert(a == [[nil]]) + def r; return [[]]; end; *a = r(); assert(a == [[[]]]) + def r; return [1,2]; end; *a = r(); assert(a == [[1,2]]) + def r; return [*[]]; end; *a = r(); assert(a == [[]]) + def r; return [*[1]]; end; *a = r(); assert(a == [[1]]) + def r; return [*[1,2]]; end; *a = r(); assert(a == [[1,2]]) + + def r; return *nil; end; *a = r(); assert(a == [nil]) + def r; return *1; end; *a = r(); assert(a == [1]) + def r; return *[]; end; *a = r(); assert(a == [nil]) + def r; return *[1]; end; *a = r(); assert(a == [1]) + def r; return *[nil]; end; *a = r(); assert(a == [nil]) + def r; return *[[]]; end; *a = r(); assert(a == [[]]) + def r; return *[1,2]; end; *a = r(); assert(a == [[1,2]]) + def r; return *[*[]]; end; *a = r(); assert(a == [nil]) + def r; return *[*[1]]; end; *a = r(); assert(a == [1]) + def r; return *[*[1,2]]; end; *a = r(); assert(a == [[1,2]]) + + def r; return *nil; end; *a = *r(); assert(a == [nil]) + def r; return *1; end; *a = *r(); assert(a == [1]) + def r; return *[]; end; *a = *r(); assert(a == [nil]) + def r; return *[1]; end; *a = *r(); assert(a == [1]) + def r; return *[nil]; end; *a = *r(); assert(a == [nil]) + def r; return *[[]]; end; *a = *r(); assert(a == []) + def r; return *[1,2]; end; *a = *r(); assert(a == [1,2]) + def r; return *[*[]]; end; *a = *r(); assert(a == [nil]) + def r; return *[*[1]]; end; *a = *r(); assert(a == [1]) + def r; return *[*[1,2]]; end; *a = *r(); assert(a == [1,2]) + + def r; return; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return nil; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return 1; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return []; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return [1]; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return [nil]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return [[]]; end; a,b,*c = r(); assert([a,b,c] == [[],nil,[]]) + def r; return [1,2]; end; a,b,*c = r(); assert([a,b,c] == [1,2,[]]) + def r; return [*[]]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return [*[1]]; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return [*[1,2]]; end; a,b,*c = r(); assert([a,b,c] == [1,2,[]]) + + def r; return *nil; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return *1; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return *[]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return *[1]; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return *[nil]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return *[[]]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return *[1,2]; end; a,b,*c = r(); assert([a,b,c] == [1,2,[]]) + def r; return *[*[]]; end; a,b,*c = r(); assert([a,b,c] == [nil,nil,[]]) + def r; return *[*[1]]; end; a,b,*c = r(); assert([a,b,c] == [1,nil,[]]) + def r; return *[*[1,2]]; end; a,b,*c = r(); assert([a,b,c] == [1,2,[]]) + end + + def test_lambda + f = lambda {|r,| assert([] == r)} + f.call([], *[]) + + f = lambda {|r,*l| assert([] == r); assert([1] == l)} + f.call([], *[1]) + + f = lambda{|x| x} + assert(f.call(42) == 42) + assert(f.call([42]) == [42]) + assert(f.call([[42]]) == [[42]]) + assert(f.call([42,55]) == [42,55]) + + f = lambda{|x,| x} + assert(f.call(42) == 42) + assert(f.call([42]) == [42]) + assert(f.call([[42]]) == [[42]]) + assert(f.call([42,55]) == [42,55]) + + f = lambda{|*x| x} + assert(f.call(42) == [42]) + assert(f.call([42]) == [[42]]) + assert(f.call([[42]]) == [[[42]]]) + assert(f.call([42,55]) == [[42,55]]) + assert(f.call(42,55) == [42,55]) + end + + def test_multi + a,=*[1] + assert(a == 1) + a,=*[[1]] + assert(a == [1]) + a,=*[[[1]]] + assert(a == [[1]]) + + x, (y, z) = 1, 2, 3 + assert([1,2,nil] == [x,y,z]) + x, (y, z) = 1, [2,3] + assert([1,2,3] == [x,y,z]) + x, (y, z) = 1, [2] + assert([1,2,nil] == [x,y,z]) + end + + def test_break + a = loop do break; end; assert(a == nil) + a = loop do break nil; end; assert(a == nil) + a = loop do break 1; end; assert(a == 1) + a = loop do break []; end; assert(a == []) + a = loop do break [1]; end; assert(a == [1]) + a = loop do break [nil]; end; assert(a == [nil]) + a = loop do break [[]]; end; assert(a == [[]]) + a = loop do break [*[]]; end; assert(a == []) + a = loop do break [*[1]]; end; assert(a == [1]) + a = loop do break [*[1,2]]; end; assert(a == [1,2]) + + a = loop do break *nil; end; assert(a == nil) + a = loop do break *1; end; assert(a == 1) + a = loop do break *[]; end; assert(a == nil) + a = loop do break *[1]; end; assert(a == 1) + a = loop do break *[nil]; end; assert(a == nil) + a = loop do break *[[]]; end; assert(a == []) + a = loop do break *[*[]]; end; assert(a == nil) + a = loop do break *[*[1]]; end; assert(a == 1) + a = loop do break *[*[1,2]]; end; assert(a == [1,2]) + + *a = loop do break; end; assert(a == [nil]) + *a = loop do break nil; end; assert(a == [nil]) + *a = loop do break 1; end; assert(a == [1]) + *a = loop do break []; end; assert(a == [[]]) + *a = loop do break [1]; end; assert(a == [[1]]) + *a = loop do break [nil]; end; assert(a == [[nil]]) + *a = loop do break [[]]; end; assert(a == [[[]]]) + *a = loop do break [1,2]; end; assert(a == [[1,2]]) + *a = loop do break [*[]]; end; assert(a == [[]]) + *a = loop do break [*[1]]; end; assert(a == [[1]]) + *a = loop do break [*[1,2]]; end; assert(a == [[1,2]]) + + *a = loop do break *nil; end; assert(a == [nil]) + *a = loop do break *1; end; assert(a == [1]) + *a = loop do break *[]; end; assert(a == [nil]) + *a = loop do break *[1]; end; assert(a == [1]) + *a = loop do break *[nil]; end; assert(a == [nil]) + *a = loop do break *[[]]; end; assert(a == [[]]) + *a = loop do break *[1,2]; end; assert(a == [[1,2]]) + *a = loop do break *[*[]]; end; assert(a == [nil]) + *a = loop do break *[*[1]]; end; assert(a == [1]) + *a = loop do break *[*[1,2]]; end; assert(a == [[1,2]]) + + *a = *loop do break *nil; end; assert(a == [nil]) + *a = *loop do break *1; end; assert(a == [1]) + *a = *loop do break *[]; end; assert(a == [nil]) + *a = *loop do break *[1]; end; assert(a == [1]) + *a = *loop do break *[nil]; end; assert(a == [nil]) + *a = *loop do break *[[]]; end; assert(a == []) + *a = *loop do break *[1,2]; end; assert(a == [1,2]) + *a = *loop do break *[*[]]; end; assert(a == [nil]) + *a = *loop do break *[*[1]]; end; assert(a == [1]) + *a = *loop do break *[*[1,2]]; end; assert(a == [1,2]) + + a,b,*c = loop do break; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break nil; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break 1; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break []; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break [1]; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break [nil]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break [[]]; end; assert([a,b,c] == [[],nil,[]]) + a,b,*c = loop do break [1,2]; end; assert([a,b,c] == [1,2,[]]) + a,b,*c = loop do break [*[]]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break [*[1]]; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break [*[1,2]]; end; assert([a,b,c] == [1,2,[]]) + + a,b,*c = loop do break *nil; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break *1; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break *[]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break *[1]; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break *[nil]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break *[[]]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break *[1,2]; end; assert([a,b,c] == [1,2,[]]) + a,b,*c = loop do break *[*[]]; end; assert([a,b,c] == [nil,nil,[]]) + a,b,*c = loop do break *[*[1]]; end; assert([a,b,c] == [1,nil,[]]) + a,b,*c = loop do break *[*[1,2]]; end; assert([a,b,c] == [1,2,[]]) + end + + def test_next + def r(val); a = yield(); assert(a == val, 2); end + r(nil){next} + r(nil){next nil} + r(1){next 1} + r([]){next []} + r([1]){next [1]} + r([nil]){next [nil]} + r([[]]){next [[]]} + r([]){next [*[]]} + r([1]){next [*[1]]} + r([1,2]){next [*[1,2]]} + + r(nil){next *nil} + r(1){next *1} + r(nil){next *[]} + r(1){next *[1]} + r(nil){next *[nil]} + r([]){next *[[]]} + r(nil){next *[*[]]} + r(1){next *[*[1]]} + r([1,2]){next *[*[1,2]]} + + def r(val); *a = yield(); assert(a == val, 2); end + r([nil]){next} + r([nil]){next nil} + r([1]){next 1} + r([[]]){next []} + r([[1]]){next [1]} + r([[nil]]){next [nil]} + r([[[]]]){next [[]]} + r([[1,2]]){next [1,2]} + r([[]]){next [*[]]} + r([[1]]){next [*[1]]} + r([[1,2]]){next [*[1,2]]} + + def r(val); *a = *yield(); assert(a == val, 2); end + r([nil]){next *nil} + r([1]){next *1} + r([nil]){next *[]} + r([1]){next *[1]} + r([nil]){next *[nil]} + r([]){next *[[]]} + r([1,2]){next *[1,2]} + r([nil]){next *[*[]]} + r([1]){next *[*[1]]} + r([1,2]){next *[*[1,2]]} + + def r(val); a,b,*c = yield(); assert([a,b,c] == val, 2); end + r([nil,nil,[]]){next} + r([nil,nil,[]]){next nil} + r([1,nil,[]]){next 1} + r([nil,nil,[]]){next []} + r([1,nil,[]]){next [1]} + r([nil,nil,[]]){next [nil]} + r([[],nil,[]]){next [[]]} + r([1,2,[]]){next [1,2]} + r([nil,nil,[]]){next [*[]]} + r([1,nil,[]]){next [*[1]]} + r([1,2,[]]){next [*[1,2]]} + + def r(val); a,b,*c = *yield(); assert([a,b,c] == val, 2); end + r([nil,nil,[]]){next *nil} + r([1,nil,[]]){next *1} + r([nil,nil,[]]){next *[]} + r([1,nil,[]]){next *[1]} + r([nil,nil,[]]){next *[nil]} + r([nil,nil,[]]){next *[[]]} + r([1,2,[]]){next *[1,2]} + r([nil,nil,[]]){next *[*[]]} + r([1,nil,[]]){next *[*[1]]} + r([1,2,[]]){next *[*[1,2]]} + end + + def test_assign2 + a = nil + assert(defined?(a)) + assert(a == nil) + + # multiple asignment + a, b = 1, 2 + assert(a == 1 && b == 2) + + a, b = b, a + assert(a == 2 && b == 1) + + a, = 1,2 + assert(a == 1) + + a, *b = 1, 2, 3 + assert(a == 1 && b == [2, 3]) + + a, (b, c), d = 1, [2, 3], 4 + assert(a == 1 && b == 2 && c == 3 && d == 4) + + *a = 1, 2, 3 + assert(a == [1, 2, 3]) + + *a = 4 + assert(a == [4]) + + *a = nil + assert(a == [nil]) + end +end diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb new file mode 100644 index 0000000000..c0cab6d11d --- /dev/null +++ b/test/ruby/test_bignum.rb @@ -0,0 +1,100 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestBignum < Test::Unit::TestCase + def fact(n) + return 1 if n == 0 + f = 1 + while n>0 + f *= n + n -= 1 + end + return f + end + + def test_bignum + $x = fact(40) + assert($x == $x) + assert($x == fact(40)) + assert($x < $x+2) + assert($x > $x-2) + assert($x == 815915283247897734345611269596115894272000000000) + assert($x != 815915283247897734345611269596115894272000000001) + assert($x+1 == 815915283247897734345611269596115894272000000001) + assert($x/fact(20) == 335367096786357081410764800000) + $x = -$x + assert($x == -815915283247897734345611269596115894272000000000) + assert(2-(2**32) == -(2**32-2)) + assert(2**32 - 5 == (2**32-3)-2) + + $good = true; + for i in 1000..1014 + $good = false if ((1<> 1 + $good = false if (n1 != n2) + end + assert($good) + + $good = true; + for i in 4000..4096 + n1 = 1 << i; + if (n1**2-1) / (n1+1) != (n1-1) + p i + $good = false + end + end + assert($good) + end + + def test_calc + b = 10**80 + a = b * 9 + 7 + assert(7 == a.modulo(b)) + assert(-b + 7 == a.modulo(-b)) + assert(b + -7 == (-a).modulo(b)) + assert(-7 == (-a).modulo(-b)) + assert(7 == a.remainder(b)) + assert(7 == a.remainder(-b)) + assert(-7 == (-a).remainder(b)) + assert(-7 == (-a).remainder(-b)) + + assert(10**40+10**20 == 10000000000000000000100000000000000000000) + assert(10**40/10**20 == 100000000000000000000) + + a = 677330545177305025495135714080 + b = 14269972710765292560 + assert(a % b == 0) + assert(-a % b == 0) + end + + def test_shift + def shift_test(a) + b = a / (2 ** 32) + c = a >> 32 + assert(b == c) + + b = a * (2 ** 32) + c = a << 32 + assert(b == c) + end + + shift_test(-4518325415524767873) + shift_test(-0xfffffffffffffffff) + end +end diff --git a/test/ruby/test_call.rb b/test/ruby/test_call.rb new file mode 100644 index 0000000000..f47164b438 --- /dev/null +++ b/test/ruby/test_call.rb @@ -0,0 +1,32 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestCall < Test::Unit::TestCase + def aaa(a, b=100, *rest) + res = [a, b] + res += rest if rest + return res + end + + def test_call + begin + aaa() # need at least 1 arg + assert(false) + rescue + assert(true) + end + + begin + aaa # no arg given (exception raised) + assert(false) + rescue + assert(true) + end + + assert(aaa(1) == [1, 100]) + assert(aaa(1, 2) == [1, 2]) + assert(aaa(1, 2, 3, 4) == [1, 2, 3, 4]) + assert(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4]) + end +end diff --git a/test/ruby/test_case.rb b/test/ruby/test_case.rb new file mode 100644 index 0000000000..325d123dc9 --- /dev/null +++ b/test/ruby/test_case.rb @@ -0,0 +1,42 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestCase < Test::Unit::TestCase + def test_case + case 5 + when 1, 2, 3, 4, 6, 7, 8 + assert(false) + when 5 + assert(true) + end + + case 5 + when 5 + assert(true) + when 1..10 + assert(false) + end + + case 5 + when 1..10 + assert(true) + else + assert(false) + end + + case 5 + when 5 + assert(true) + else + assert(false) + end + + case "foobar" + when /^f.*r$/ + assert(true) + else + assert(false) + end + end +end diff --git a/test/ruby/test_clone.rb b/test/ruby/test_clone.rb new file mode 100644 index 0000000000..ad146709ad --- /dev/null +++ b/test/ruby/test_clone.rb @@ -0,0 +1,35 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestClone < Test::Unit::TestCase + module M001; end + module M002; end + module M003; include M002; end + module M002; include M001; end + module M003; include M002; end + + def test_clone + foo = Object.new + def foo.test + "test" + end + bar = foo.clone + def bar.test2 + "test2" + end + + assert(bar.test2 == "test2") + assert(bar.test == "test") + assert(foo.test == "test") + + begin + foo.test2 + assert false + rescue NoMethodError + assert true + end + + assert(M003.ancestors == [M003, M002, M001]) + end +end diff --git a/test/ruby/test_condition.rb b/test/ruby/test_condition.rb new file mode 100644 index 0000000000..303521f95f --- /dev/null +++ b/test/ruby/test_condition.rb @@ -0,0 +1,18 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestCondition < Test::Unit::TestCase + + # [should] first test to see if we can run the tests. + + def test_condition + $x = '0'; + + $x == $x && assert(true) + $x != $x && assert(false) + $x == $x || assert(false) + $x != $x || assert(true) + + end +end diff --git a/test/ruby/test_const.rb b/test/ruby/test_const.rb new file mode 100644 index 0000000000..179271aef1 --- /dev/null +++ b/test/ruby/test_const.rb @@ -0,0 +1,35 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestConst < Test::Unit::TestCase + TEST1 = 1 + TEST2 = 2 + + module Const + TEST3 = 3 + TEST4 = 4 + end + + module Const2 + TEST3 = 6 + TEST4 = 8 + end + + def test_const + self.class.class_eval { + include Const + } + assert([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]) + + self.class.class_eval { + include Const2 + } + STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE + assert([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]) + + assert((String <=> Object) == -1) + assert((Object <=> String) == 1) + assert((Array <=> String) == nil) + end +end diff --git a/test/ruby/test_defined.rb b/test/ruby/test_defined.rb new file mode 100644 index 0000000000..f53c69f791 --- /dev/null +++ b/test/ruby/test_defined.rb @@ -0,0 +1,42 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestDefined < Test::Unit::TestCase + class Foo + def foo + p :foo + end + protected :foo + def bar(f) + yield(defined?(self.foo)) + yield(defined?(f.foo)) + end + end + + def defined_test + return !defined?(yield) + end + + def test_defined + $x = nil + + assert(defined?($x)) # global variable + assert(defined?($x) == 'global-variable')# returns description + + foo=5 + assert(defined?(foo)) # local variable + + assert(defined?(::Array)) # constant !! Array -> ::Array + assert(defined?(Object.new)) # method + assert(!defined?(Object.print)) # private method + assert(defined?(1 == 2)) # operator expression + + f = Foo.new + assert(defined?(f.foo) == nil) + f.bar(f) { |v| assert(v) } + + assert(defined_test) # not iterator + assert(!defined_test{}) # called as iterator + end +end diff --git a/test/ruby/test_eval.rb b/test/ruby/test_eval.rb new file mode 100644 index 0000000000..8de359b8dd --- /dev/null +++ b/test/ruby/test_eval.rb @@ -0,0 +1,124 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestEval < Test::Unit::TestCase + # eval with binding + def test_ev + local1 = "local1" + lambda { + local2 = "local2" + return binding + }.call + end + + def test_eval + assert(eval("") == nil) + $bad=false + eval 'while false; $bad = true; print "foo\n" end' + assert(!$bad) + + assert(eval('TRUE')) + assert(eval('true')) + assert(!eval('NIL')) + assert(!eval('nil')) + assert(!eval('FALSE')) + assert(!eval('false')) + + $foo = 'assert(true)' + begin + eval $foo + rescue + assert(false) + end + + assert(eval("$foo") == 'assert(true)') + assert(eval("true") == true) + i = 5 + assert(eval("i == 5")) + assert(eval("i") == 5) + assert(eval("defined? i")) + + $x = test_ev + assert(eval("local1", $x) == "local1") # normal local var + assert(eval("local2", $x) == "local2") # nested local var + $bad = true + begin + p eval("local1") + rescue NameError # must raise error + $bad = false + end + assert(!$bad) + + # !! use class_eval to avoid nested definition + self.class.class_eval %q( + module EvTest + EVTEST1 = 25 + evtest2 = 125 + $x = binding + end + ) + assert(eval("EVTEST1", $x) == 25) # constant in module + assert(eval("evtest2", $x) == 125) # local var in module + $bad = true + begin + eval("EVTEST1") + rescue NameError # must raise error + $bad = false + end + assert(!$bad) + + x = proc{} + eval "i4 = 1", x + assert(eval("i4", x) == 1) + x = proc{proc{}}.call + eval "i4 = 22", x + assert(eval("i4", x) == 22) + $x = [] + x = proc{proc{}}.call + eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x + assert($x[4].call == 8) + + x = binding + eval "i = 1", x + assert(eval("i", x) == 1) + x = proc{binding}.call + eval "i = 22", x + assert(eval("i", x) == 22) + $x = [] + x = proc{binding}.call + eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x + assert($x[4].call == 8) + x = proc{binding}.call + eval "for i6 in 1..1; j6=i6; end", x + assert(eval("defined? i6", x)) + assert(eval("defined? j6", x)) + + proc { + p = binding + eval "foo11 = 1", p + foo22 = 5 + proc{foo11=22}.call + proc{foo22=55}.call + assert(eval("foo11", p) == eval("foo11")) + assert(eval("foo11") == 1) + assert(eval("foo22", p) == eval("foo22")) + assert(eval("foo22") == 55) + }.call + + p1 = proc{i7 = 0; proc{i7}}.call + assert(p1.call == 0) + eval "i7=5", p1 + assert(p1.call == 5) + assert(!defined?(i7)) + + p1 = proc{i7 = 0; proc{i7}}.call + i7 = nil + assert(p1.call == 0) + eval "i7=1", p1 + assert(p1.call == 1) + eval "i7=5", p1 + assert(p1.call == 5) + assert(i7 == nil) + end +end diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb new file mode 100644 index 0000000000..e22b3b37bb --- /dev/null +++ b/test/ruby/test_exception.rb @@ -0,0 +1,96 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestException < Test::Unit::TestCase + def test_exception + begin + raise "this must be handled" + assert(false) + rescue + assert(true) + end + + $bad = true + begin + raise "this must be handled no.2" + rescue + if $bad + $bad = false + retry + assert(false) + end + end + assert(true) + + # exception in rescue clause + $string = "this must be handled no.3" + begin + begin + raise "exception in rescue clause" + rescue + raise $string + end + assert(false) + rescue + assert(true) if $! == $string + end + + # exception in ensure clause + begin + begin + raise "this must be handled no.4" + ensure + raise "exception in ensure clause" + end + assert(false) + rescue + assert(true) + end + + $bad = true + begin + begin + raise "this must be handled no.5" + ensure + $bad = false + end + rescue + end + assert(!$bad) + + $bad = true + begin + begin + raise "this must be handled no.6" + ensure + $bad = false + end + rescue + end + assert(!$bad) + + $bad = true + while true + begin + break + ensure + $bad = false + end + end + assert(!$bad) + + assert(catch(:foo) { + loop do + loop do + throw :foo, true + break + end + break + assert(false) # should no reach here + end + false + }) + + end +end diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb new file mode 100644 index 0000000000..1541fa71ea --- /dev/null +++ b/test/ruby/test_float.rb @@ -0,0 +1,45 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestFloat < Test::Unit::TestCase + def test_float + assert(2.6.floor == 2) + assert((-2.6).floor == -3) + assert(2.6.ceil == 3) + assert((-2.6).ceil == -2) + assert(2.6.truncate == 2) + assert((-2.6).truncate == -2) + assert(2.6.round == 3) + assert((-2.4).truncate == -2) + assert((13.4 % 1 - 0.4).abs < 0.0001) + nan = 0.0/0 + def nan.test(v) + extend Test::Unit::Assertions + assert(self != v) + assert((self < v) == false) + assert((self > v) == false) + assert((self <= v) == false) + assert((self >= v) == false) + end + nan.test(nan) + nan.test(0) + nan.test(1) + nan.test(-1) + nan.test(1000) + nan.test(-1000) + nan.test(1_000_000_000_000) + nan.test(-1_000_000_000_000) + nan.test(100.0); + nan.test(-100.0); + nan.test(0.001); + nan.test(-0.001); + nan.test(1.0/0); + nan.test(-1.0/0); + + #s = "3.7517675036461267e+17" + #assert(s == sprintf("%.16e", s.to_f)) + f = 3.7517675036461267e+17 + assert(f == sprintf("%.16e", f).to_f) + end +end diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb new file mode 100644 index 0000000000..d759c7cd4b --- /dev/null +++ b/test/ruby/test_hash.rb @@ -0,0 +1,76 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestHash < Test::Unit::TestCase + def test_hash + $x = {1=>2, 2=>4, 3=>6} + $y = {1, 2, 2, 4, 3, 6} + + assert($x[1] == 2) + + assert(begin + for k,v in $y + raise if k*2 != v + end + true + rescue + false + end) + + assert($x.length == 3) + assert($x.has_key?(1)) + assert($x.has_value?(4)) + assert($x.values_at(2,3) == [4,6]) + assert($x == {1=>2, 2=>4, 3=>6}) + + $z = $y.keys.join(":") + assert($z == "1:2:3") + + $z = $y.values.join(":") + assert($z == "2:4:6") + assert($x == $y) + + $y.shift + assert($y.length == 2) + + $z = [1,2] + $y[$z] = 256 + assert($y[$z] == 256) + + $x = Hash.new(0) + $x[1] = 1 + assert($x[1] == 1) + assert($x[2] == 0) + + $x = Hash.new([]) + assert($x[22] == []) + assert($x[22].equal?($x[22])) + + $x = Hash.new{[]} + assert($x[22] == []) + assert(!$x[22].equal?($x[22])) + + $x = Hash.new{|h,k| $z = k; h[k] = k*2} + $z = 0 + assert($x[22] == 44) + assert($z == 22) + $z = 0 + assert($x[22] == 44) + assert($z == 0) + $x.default = 5 + assert($x[23] == 5) + + $x = Hash.new + def $x.default(k) + $z = k + self[k] = k*2 + end + $z = 0 + assert($x[22] == 44) + assert($z == 22) + $z = 0 + assert($x[22] == 44) + assert($z == 0) + end +end diff --git a/test/ruby/test_ifunless.rb b/test/ruby/test_ifunless.rb new file mode 100644 index 0000000000..7910f81b52 --- /dev/null +++ b/test/ruby/test_ifunless.rb @@ -0,0 +1,16 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestIfunless < Test::Unit::TestCase + def test_if_unless + $x = 'test'; + assert(if $x == $x then true else false end) + $bad = false + unless $x == $x + $bad = true + end + assert(!$bad) + assert(unless $x != $x then true else false end) + end +end diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb new file mode 100644 index 0000000000..3d7e6cbcd2 --- /dev/null +++ b/test/ruby/test_iterator.rb @@ -0,0 +1,383 @@ +require 'test/unit' + +$KCODE = 'none' + +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 + +class TestIterator < Test::Unit::TestCase + def ttt + assert(iterator?) + end + + def test_iterator + assert(!iterator?) + + ttt{} + + # yield at top level !! here's not toplevel + assert(!defined?(yield)) + end + + def test_array + $x = [1, 2, 3, 4] + $y = [] + + # iterator over array + for i in $x + $y.push i + end + assert($x == $y) + end + + def tt + 1.upto(10) {|i| + yield i + } + end + + def tt2(dummy) + yield 1 + end + + def tt3(&block) + tt2(raise(ArgumentError,""),&block) + end + + def test_nested_iterator + i = 0 + tt{|i| break if i == 5} + assert(i == 5) + + $x = false + begin + tt3{} + rescue ArgumentError + $x = true + rescue Exception + end + assert($x) + end + + # iterator break/redo/next/retry + def test_break + done = true + loop{ + break + done = false # should not reach here + } + assert(done) + + done = false + $bad = false + loop { + break if done + done = true + next + $bad = true # should not reach here + } + assert(!$bad) + + done = false + $bad = false + loop { + break if done + done = true + redo + $bad = true # should not reach here + } + assert(!$bad) + + $x = [] + for i in 1 .. 7 + $x.push i + end + assert($x.size == 7) + assert($x == [1, 2, 3, 4, 5, 6, 7]) + + $done = false + $x = [] + for i in 1 .. 7 # see how retry works in iterator loop + if i == 4 and not $done + $done = true + retry + end + $x.push(i) + end + assert($x.size == 10) + assert($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]) + end + + def test_append_method_to_built_in_class + $x = [[1,2],[3,4],[5,6]] + assert($x.iter_test1{|x|x} == $x.iter_test2{|x|x}) + end + + 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 + + def f(a) + a + end + end + + def test_itertest + assert(IterTest.new(nil).method(:f).to_proc.call([1]) == [1]) + m = /\w+/.match("abc") + assert(IterTest.new(nil).method(:f).to_proc.call([m]) == [m]) + + IterTest.new([0]).each0 {|x| assert(x == 0)} + IterTest.new([1]).each1 {|x| assert(x == 1)} + IterTest.new([2]).each2 {|x| assert(x == [2])} + IterTest.new([3]).each3 {|x| assert(x == 3)} + IterTest.new([4]).each4 {|x| assert(x == 4)} + IterTest.new([5]).each5 {|x| assert(x == 5)} + IterTest.new([6]).each6 {|x| assert(x == [6])} + IterTest.new([7]).each7 {|x| assert(x == 7)} + IterTest.new([8]).each8 {|x| assert(x == 8)} + + IterTest.new([[0]]).each0 {|x| assert(x == [0])} + IterTest.new([[1]]).each1 {|x| assert(x == [1])} + IterTest.new([[2]]).each2 {|x| assert(x == [[2]])} + IterTest.new([[3]]).each3 {|x| assert(x == 3)} + IterTest.new([[4]]).each4 {|x| assert(x == [4])} + IterTest.new([[5]]).each5 {|x| assert(x == [5])} + IterTest.new([[6]]).each6 {|x| assert(x == [[6]])} + IterTest.new([[7]]).each7 {|x| assert(x == 7)} + IterTest.new([[8]]).each8 {|x| assert(x == [8])} + + IterTest.new([[0,0]]).each0 {|x| assert(x == [0,0])} + IterTest.new([[8,8]]).each8 {|x| assert(x == [8,8])} + end + + def m(var) + assert(var) + end + + def m1 + m(block_given?) + end + + def m2 + m(block_given?,&proc{}) + end + + def test_foo + m1{p 'test'} + m2{p 'test'} + end + + class C + include Enumerable + def initialize + @a = [1,2,3] + end + def each(&block) + @a.each(&block) + end + end + + def test_collect + assert(C.new.collect{|n| n} == [1,2,3]) + end + + def test_proc + assert(Proc == lambda{}.class) + assert(Proc == Proc.new{}.class) + lambda{|a|assert(a==1)}.call(1) + end + + def block_test(klass, &block) + assert(klass === block) + end + + def test_block + block_test(NilClass) + block_test(Proc){} + end + + def argument_test(state, proc, *args) + x = state + begin + proc.call(*args) + rescue ArgumentError + x = !x + end + assert(x,2) + end + + def test_argument + argument_test(true, lambda{||}) + argument_test(false, lambda{||}, 1) + argument_test(true, lambda{|a,|}, 1) + argument_test(false, lambda{|a,|}) + argument_test(false, lambda{|a,|}, 1,2) + end + + def get_block(&block) + block + end + + def test_get_block + assert(Proc == get_block{}.class) + argument_test(true, get_block{||}) + argument_test(true, get_block{||}, 1) + argument_test(true, get_block{|a,|}, 1) + argument_test(true, get_block{|a,|}) + argument_test(true, get_block{|a,|}, 1,2) + + argument_test(true, get_block(&lambda{||})) + argument_test(false, get_block(&lambda{||}),1) + argument_test(true, get_block(&lambda{|a,|}),1) + argument_test(false, get_block(&lambda{|a,|}),1,2) + + block = get_block{11} + assert(block.class == Proc) + assert(block.to_proc.class == Proc) + assert(block.clone.call == 11) + assert(get_block(&block).class == Proc) + + lambda = lambda{44} + assert(lambda.class == Proc) + assert(lambda.to_proc.class == Proc) + assert(lambda.clone.call == 44) + assert(get_block(&lambda).class == Proc) + + assert(Proc.new{|a,| a}.call(1,2,3) == 1) + argument_test(true, Proc.new{|a,|}, 1,2) + end + + def return1_test # !! test_return1 -> return1_test + Proc.new { + return 55 + }.call + 5 + end + + def test_return1 + assert(return1_test() == 55) + end + + def return2_test # !! test_return2 -> return2_test + lambda { + return 55 + }.call + 5 + end + + def test_return2 + assert(return2_test() == 60) + end + + def proc_call(&b) + b.call + end + def proc_yield() + yield + end + def proc_return1 + proc_call{return 42}+1 + end + + def test_proc_return1 + assert(proc_return1() == 42) + end + + def proc_return2 + proc_yield{return 42}+1 + end + + def test_proc_return2 + assert(proc_return2() == 42) + end + + def ljump_test(state, proc, *args) + x = state + begin + proc.call(*args) + rescue LocalJumpError + x = !x + end + assert(x,2) + end + + def test_ljump + block = get_block{11} + lambda = lambda{44} + # ljump_test(false, get_block{break}) # !! This line terminates testrunner... please sombody fix it. + ljump_test(true, lambda{break}) + + assert(block.arity == -1) + assert(lambda.arity == -1) + assert(lambda{||}.arity == 0) + assert(lambda{|a|}.arity == 1) + assert(lambda{|a,|}.arity == 1) + assert(lambda{|a,b|}.arity == 2) + end + + def marity_test(m) + method = method(m) + assert(method.arity == method.to_proc.arity) + end + + def test_marity + marity_test(:assert) + marity_test(:marity_test) + marity_test(:p) + + lambda(&method(:assert)).call(true) + lambda(&get_block{|a,n| assert(a,n)}).call(true, 2) + end + + class ITER_TEST1 + def a + block_given? + end + end + + class ITER_TEST2 < ITER_TEST1 + include Test::Unit::Assertions + def a + assert(super) + super + end + end + + def test_iter_test2 + assert(ITER_TEST2.new.a {}) + end + + class ITER_TEST3 + def foo x + return yield if block_given? + x + end + end + + class ITER_TEST4 < ITER_TEST3 + include Test::Unit::Assertions + def foo x + assert(super == yield) + assert(super(x, &nil) == x) + end + end + + def test_iter4 + ITER_TEST4.new.foo(44){55} + end +end diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb new file mode 100644 index 0000000000..5564fd1e7b --- /dev/null +++ b/test/ruby/test_marshal.rb @@ -0,0 +1,32 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestMarshal < Test::Unit::TestCase + def fact(n) + return 1 if n == 0 + f = 1 + while n>0 + f *= n + n -= 1 + end + return f + end + + StrClone=String.clone; + + def test_marshal + $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)] + $y = Marshal.dump($x) + assert($x == Marshal.load($y)) + + assert(Marshal.load(Marshal.dump(StrClone.new("abc"))).class == StrClone) + + [[1,2,3,4], [81, 2, 118, 3146]].each { |w,x,y,z| + a = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f)) + ma = Marshal.dump(a) + b = Marshal.load(ma) + assert(a == b) + } + end +end diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb new file mode 100644 index 0000000000..15537159a3 --- /dev/null +++ b/test/ruby/test_math.rb @@ -0,0 +1,14 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestMath < Test::Unit::TestCase + def test_math + assert(Math.sqrt(4) == 2) + + self.class.class_eval { + include Math + } + assert(sqrt(4) == 2) + end +end diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb new file mode 100644 index 0000000000..15b774f6ca --- /dev/null +++ b/test/ruby/test_pack.rb @@ -0,0 +1,21 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestPack < Test::Unit::TestCase + def test_pack + $format = "c2x5CCxsdils_l_a6"; + # 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. + ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"] + $x = ary.pack($format) + ary2 = $x.unpack($format) + + assert(ary.length == ary2.length) + assert(ary.join(':') == ary2.join(':')) + assert($x =~ /def/) + + $x = [-1073741825] + assert($x.pack("q").unpack("q") == $x) + end +end diff --git a/test/ruby/test_path.rb b/test/ruby/test_path.rb new file mode 100644 index 0000000000..d13ec66d30 --- /dev/null +++ b/test/ruby/test_path.rb @@ -0,0 +1,48 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestPath < Test::Unit::TestCase + def test_path + assert(File.basename("a") == "a") + assert(File.basename("a/b") == "b") + assert(File.basename("a/b/") == "b") + assert(File.basename("/") == "/") + assert(File.basename("//") == "/") + assert(File.basename("///") == "/") + assert(File.basename("a/b////") == "b") + assert(File.basename("a.rb", ".rb") == "a") + assert(File.basename("a.rb///", ".rb") == "a") + assert(File.basename("a.rb///", ".*") == "a") + assert(File.basename("a.rb///", ".c") == "a.rb") + assert(File.dirname("a") == ".") + assert(File.dirname("/") == "/") + assert(File.dirname("/a") == "/") + assert(File.dirname("a/b") == "a") + assert(File.dirname("a/b/c") == "a/b") + assert(File.dirname("/a/b/c") == "/a/b") + assert(File.dirname("/a/b/") == "/a") + assert(File.dirname("/a/b///") == "/a") + case Dir.pwd + when %r'\A\w:' + assert(/\A\w:\/\z/ =~ File.expand_path(".", "/")) + assert(/\A\w:\/a\z/ =~ File.expand_path("a", "/")) + dosish = true + when %r'\A//' + assert(%r'\A//[^/]+/[^/]+\z' =~ File.expand_path(".", "/")) + assert(%r'\A//[^/]+/[^/]+/a\z' =~ File.expand_path(".", "/")) + dosish = true + else + assert(File.expand_path(".", "/") == "/") + assert(File.expand_path("sub", "/") == "/sub") + end + if dosish + assert(File.expand_path("/", "//machine/share/sub") == "//machine/share") + assert(File.expand_path("/dir", "//machine/share/sub") == "//machine/share/dir") + assert(File.expand_path("/", "z:/sub") == "z:/") + assert(File.expand_path("/dir", "z:/sub") == "z:/dir") + end + assert(File.expand_path(".", "//") == "//") + assert(File.expand_path("sub", "//") == "//sub") + end +end diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb new file mode 100644 index 0000000000..0cf470ff00 --- /dev/null +++ b/test/ruby/test_proc.rb @@ -0,0 +1,45 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestProc < Test::Unit::TestCase + def test_proc + $proc = proc{|i| i} + assert($proc.call(2) == 2) + assert($proc.call(3) == 3) + + $proc = proc{|i| i*2} + assert($proc.call(2) == 4) + assert($proc.call(3) == 6) + + proc{ + iii=5 # nested local variable + $proc = proc{|i| + iii = i + } + $proc2 = proc { + $x = iii # nested variables shared by procs + } + # scope of nested variables + assert(defined?(iii)) + }.call + assert(!defined?(iii)) # out of scope + + loop{iii=5; assert(eval("defined? iii")); break} + loop { + iii = 10 + def dyna_var_check + loop { + assert(!defined?(iii)) + break + } + end + dyna_var_check + break + } + $x=0 + $proc.call(5) + $proc2.call + assert($x == 5) + end +end diff --git a/test/ruby/test_signal.rb b/test/ruby/test_signal.rb new file mode 100644 index 0000000000..d9376b47ea --- /dev/null +++ b/test/ruby/test_signal.rb @@ -0,0 +1,26 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestSignal < Test::Unit::TestCase + def test_signal + if defined? Process.kill + $x = 0 + trap "SIGINT", proc{|sig| $x = 2} + Process.kill "SIGINT", $$ + sleep 0.1 + assert($x == 2) + + trap "SIGINT", proc{raise "Interrupt"} + + x = false + begin + Process.kill "SIGINT", $$ + sleep 0.1 + rescue + x = $! + end + assert(x && /Interrupt/ =~ x.message) + end + end +end diff --git a/test/ruby/test_stringchar.rb b/test/ruby/test_stringchar.rb new file mode 100644 index 0000000000..498300f379 --- /dev/null +++ b/test/ruby/test_stringchar.rb @@ -0,0 +1,116 @@ +require 'test/unit' + +$KCODE = 'none' + +class TestStringchar < Test::Unit::TestCase + def test_stringchar + assert("abcd" == "abcd") + assert("abcd" =~ /abcd/) + assert("abcd" === "abcd") + # compile time string concatenation + assert("ab" "cd" == "abcd") + assert("#{22}aa" "cd#{44}" == "22aacd44") + assert("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566") + assert("abc" !~ /^$/) + assert("abc\n" !~ /^$/) + assert("abc" !~ /^d*$/) + assert(("abc" =~ /d*$/) == 3) + assert("" =~ /^$/) + assert("\n" =~ /^$/) + assert("a\n\n" =~ /^$/) + assert("abcabc" =~ /.*a/ && $& == "abca") + assert("abcabc" =~ /.*c/ && $& == "abcabc") + assert("abcabc" =~ /.*?a/ && $& == "a") + assert("abcabc" =~ /.*?c/ && $& == "abc") + assert(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb") + + assert(/^(ab+)+b/ =~ "ababb" && $& == "ababb") + assert(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb") + assert(/^(ab+)+/ =~ "ababb" && $& == "ababb") + assert(/^(?:ab+)+/ =~ "ababb" && $& == "ababb") + + assert(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") + assert(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") + + $x = <') == "") + + # character constants(assumes ASCII) + assert("a"[0] == ?a) + assert(?a == ?a) + assert(?\C-a == 1) + assert(?\M-a == 225) + assert(?\M-\C-a == 129) + assert("a".upcase![0] == ?A) + assert("A".downcase![0] == ?a) + assert("abc".tr!("a-z", "A-Z") == "ABC") + assert("aabbcccc".tr_s!("a-z", "A-Z") == "ABC") + assert("abcc".squeeze!("a-z") == "abc") + assert("abcd".delete!("bc") == "ad") + + $x = "abcdef" + $y = [ ?a, ?b, ?c, ?d, ?e, ?f ] + $bad = false + $x.each_byte {|i| + if i != $y.shift + $bad = true + break + end + } + assert(!$bad) + + s = "a string" + s[0..s.size]="another string" + assert(s == "another string") + + s = < 0 + redo + end + end + assert(sum == 220) + + $bad = false + tmp = open("while_tmp", "r") + while line = tmp.gets() + break if 3 + case line + when /vt100/, /Amiga/, /paper/ + $bad = true + end + end + assert(!$bad) + tmp.close + + File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` + assert(!File.exist?("while_tmp")) + end + + def test_until + i = 0 + until i>4 + i+=1 + end + assert(i>4) + end +end