* 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
This commit is contained in:
nahi 2003-09-04 16:18:59 +00:00
Родитель 822a72b8ee
Коммит aaf5d9c03c
30 изменённых файлов: 2234 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Wed Sep 5 01:10:11 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* 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 <nahi@ruby-lang.org>
* test/csv/test_csv.rb: close opened files for CSV::IOBuf explicitly.

Просмотреть файл

@ -2,7 +2,7 @@ require 'test/unit'
require 'tempfile'
require 'fileutils'
require '../lib/csv'
require 'csv'
class CSV
class StreamBuf

42
test/ruby/test_alias.rb Normal file
Просмотреть файл

@ -0,0 +1,42 @@
require 'test/unit'
$KCODE = 'none'
class TestAlias < Test::Unit::TestCase
class Alias0
def foo; "foo" end
end
class Alias1<Alias0
alias bar foo
def foo; "foo+" + super end
end
class Alias2<Alias1
alias baz foo
undef foo
end
class Alias3<Alias2
def foo
defined? super
end
def bar
defined? super
end
def quux
defined? super
end
end
def test_alias
x = Alias2.new
assert(x.bar == "foo")
assert(x.baz == "foo+foo")
# test_check for cache
assert(x.baz == "foo+foo")
x = Alias3.new
assert(!x.foo)
assert(x.bar)
assert(!x.quux)
end
end

103
test/ruby/test_array.rb Normal file
Просмотреть файл

@ -0,0 +1,103 @@
require 'test/unit'
$KCODE = 'none'
class TestArray < Test::Unit::TestCase
def test_array
assert([1, 2] + [3, 4] == [1, 2, 3, 4])
assert([1, 2] * 2 == [1, 2, 1, 2])
assert([1, 2] * ":" == "1:2")
assert([1, 2].hash == [1, 2].hash)
assert([1,2,3] & [2,3,4] == [2,3])
assert([1,2,3] | [2,3,4] == [1,2,3,4])
assert([1,2,3] - [2,3] == [1])
$x = [0, 1, 2, 3, 4, 5]
assert($x[2] == 2)
assert($x[1..3] == [1, 2, 3])
assert($x[1,3] == [1, 2, 3])
$x[0, 2] = 10
assert($x[0] == 10 && $x[1] == 2)
$x[0, 0] = -1
assert($x[0] == -1 && $x[1] == 10)
$x[-1, 1] = 20
assert($x[-1] == 20 && $x.pop == 20)
end
def test_array_andor
assert(([1,2,3]&[2,4,6]) == [2])
assert(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
end
def test_compact
$x = [nil, 1, nil, nil, 5, nil, nil]
$x.compact!
assert($x == [1, 5])
end
def test_uniq
$x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
$x.uniq!
assert($x == [1, 4, 2, 5])
# empty?
assert(!$x.empty?)
$x = []
assert($x.empty?)
end
def test_sort
$x = ["it", "came", "to", "pass", "that", "..."]
$x = $x.sort.join(" ")
assert($x == "... came it pass that to")
$x = [2,5,3,1,7]
$x.sort!{|a,b| a<=>b} # 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

Просмотреть файл

@ -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

100
test/ruby/test_bignum.rb Normal file
Просмотреть файл

@ -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<<i) != (2**i))
end
assert($good)
$good = true;
n1=1<<1000
for i in 1000..1014
$good = false if ((1<<i) != n1)
n1 *= 2
end
assert($good)
$good = true;
n2=n1
for i in 1..10
n1 = n1 / 2
n2 = n2 >> 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

32
test/ruby/test_call.rb Normal file
Просмотреть файл

@ -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

42
test/ruby/test_case.rb Normal file
Просмотреть файл

@ -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

35
test/ruby/test_clone.rb Normal file
Просмотреть файл

@ -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

Просмотреть файл

@ -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

35
test/ruby/test_const.rb Normal file
Просмотреть файл

@ -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

42
test/ruby/test_defined.rb Normal file
Просмотреть файл

@ -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

124
test/ruby/test_eval.rb Normal file
Просмотреть файл

@ -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

Просмотреть файл

@ -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

45
test/ruby/test_float.rb Normal file
Просмотреть файл

@ -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

76
test/ruby/test_hash.rb Normal file
Просмотреть файл

@ -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

Просмотреть файл

@ -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

383
test/ruby/test_iterator.rb Normal file
Просмотреть файл

@ -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

32
test/ruby/test_marshal.rb Normal file
Просмотреть файл

@ -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

14
test/ruby/test_math.rb Normal file
Просмотреть файл

@ -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

21
test/ruby/test_pack.rb Normal file
Просмотреть файл

@ -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

48
test/ruby/test_path.rb Normal file
Просмотреть файл

@ -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

45
test/ruby/test_proc.rb Normal file
Просмотреть файл

@ -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

26
test/ruby/test_signal.rb Normal file
Просмотреть файл

@ -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

Просмотреть файл

@ -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 = <<END;
ABCD
ABCD
END
$x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3}
assert($x == "AC\nAC\n")
assert("foobar" =~ /foo(?=(bar)|(baz))/)
assert("foobaz" =~ /foo(?=(bar)|(baz))/)
$foo = "abc"
assert("#$foo = abc" == "abc = abc")
assert("#{$foo} = abc" == "abc = abc")
foo = "abc"
assert("#{foo} = abc" == "abc = abc")
assert('-' * 5 == '-----')
assert('-' * 1 == '-')
assert('-' * 0 == '')
foo = '-'
assert(foo * 5 == '-----')
assert(foo * 1 == '-')
assert(foo * 0 == '')
$x = "a.gif"
assert($x.sub(/.*\.([^\.]+)$/, '\1') == "gif")
assert($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif")
assert($x.sub(/.*\.([^\.]+)$/, '\2') == "")
assert($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab")
assert($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>")
# 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 = <<EOS
#{
[1,2,3].join(",")
}
EOS
assert(s == "1,2,3\n")
assert("Just".to_i(36) == 926381)
assert("-another".to_i(36) == -23200231779)
assert(1299022.to_s(36) == "ruby")
assert(-1045307475.to_s(36) == "-hacker")
assert("Just_another_Ruby_hacker".to_i(36) == 265419172580680477752431643787347)
assert(-265419172580680477752431643787347.to_s(36) == "-justanotherrubyhacker")
a = []
(0..255).each {|n|
ch = [n].pack("C")
a.push ch if /a#{Regexp.quote ch}b/x =~ "ab"
}
assert(a.size == 0)
end
end

23
test/ruby/test_struct.rb Normal file
Просмотреть файл

@ -0,0 +1,23 @@
require 'test/unit'
$KCODE = 'none'
class TestStruct < Test::Unit::TestCase
def test_struct
struct_test = Struct.new("Test", :foo, :bar)
assert(struct_test == Struct::Test)
test = struct_test.new(1, 2)
assert(test.foo == 1 && test.bar == 2)
assert(test[0] == 1 && test[1] == 2)
a, b = test.to_a
assert(a == 1 && b == 2)
test[0] = 22
assert(test.foo == 22)
test.bar = 47
assert(test.bar == 47)
end
end

77
test/ruby/test_system.rb Normal file
Просмотреть файл

@ -0,0 +1,77 @@
require 'test/unit'
$KCODE = 'none'
class TestSystem < Test::Unit::TestCase
def test_system
assert(`echo foobar` == "foobar\n")
assert(`./miniruby -e 'print "foobar"'` == 'foobar')
tmp = open("script_tmp", "w")
tmp.print "print $zzz\n";
tmp.close
assert(`./miniruby -s script_tmp -zzz` == 'true')
assert(`./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
assert(`./miniruby script_tmp -zzz=678` == '678')
tmp = open("script_tmp", "w")
tmp.print "this is a leading junk\n";
tmp.print "#! /usr/local/bin/ruby -s\n";
tmp.print "print $zzz\n";
tmp.print "__END__\n";
tmp.print "this is a trailing junk\n";
tmp.close
assert(`./miniruby -x script_tmp` == 'nil')
assert(`./miniruby -x script_tmp -zzz=555` == '555')
tmp = open("script_tmp", "w")
for i in 1..5
tmp.print i, "\n"
end
tmp.close
`./miniruby -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
done = true
tmp = open("script_tmp", "r")
while tmp.gets
if $_.to_i % 5 != 0
done = false
break
end
end
tmp.close
assert(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
if (dir = File.dirname(File.dirname($0))) == '.'
dir = ""
else
dir << "/"
end
def valid_syntax?(code, fname)
eval("BEGIN {return true}\n#{code}", nil, fname, 0)
rescue Exception
puts $!.message
false
end
for script in Dir["#{dir}{lib,sample,ext}/**/*.rb"]
unless valid_syntax? IO::read(script), script
$bad = true
end
end
assert(!$bad)
end
end

23
test/ruby/test_trace.rb Normal file
Просмотреть файл

@ -0,0 +1,23 @@
require 'test/unit'
$KCODE = 'none'
class TestTrace < Test::Unit::TestCase
def test_trace
$x = 1234
$y = 0
trace_var :$x, proc{$y = $x}
$x = 40414
assert($y == $x)
untrace_var :$x
$x = 19660208
assert($y != $x)
trace_var :$x, proc{$x *= 2}
$x = 5
assert($x == 10)
untrace_var :$x
end
end

Просмотреть файл

@ -0,0 +1,58 @@
require 'test/unit'
$KCODE = 'none'
class TestVariable < Test::Unit::TestCase
class Gods
@@rule = "Uranus"
def ruler0
@@rule
end
def self.ruler1 # <= per method definition style
@@rule
end
class << self # <= multiple method definition style
def ruler2
@@rule
end
end
end
module Olympians
@@rule ="Zeus"
def ruler3
@@rule
end
end
class Titans < Gods
@@rule = "Cronus"
include Olympians # OK to cause warning (intentional)
end
def test_variable
assert($$.instance_of?(Fixnum))
# read-only variable
begin
$$ = 5
assert false
rescue NameError
assert true
end
foobar = "foobar"
$_ = foobar
assert($_ == foobar)
assert(Gods.new.ruler0 == "Cronus")
assert(Gods.ruler1 == "Cronus")
assert(Gods.ruler2 == "Cronus")
assert(Titans.ruler1 == "Cronus")
assert(Titans.ruler2 == "Cronus")
atlas = Titans.new
assert(atlas.ruler0 == "Cronus")
assert(atlas.ruler3 == "Zeus")
end
end

Просмотреть файл

@ -0,0 +1,82 @@
require 'test/unit'
$KCODE = 'none'
class TestWhileuntil < Test::Unit::TestCase
def test_while
tmp = open("while_tmp", "w")
tmp.print "tvi925\n";
tmp.print "tvi920\n";
tmp.print "vt100\n";
tmp.print "Amiga\n";
tmp.print "paper\n";
tmp.close
tmp = open("while_tmp", "r")
assert(tmp.kind_of?(File))
while line = tmp.gets()
break if /vt100/ =~ line
end
assert(!tmp.eof? && /vt100/ =~ line)
tmp.close
$bad = false
tmp = open("while_tmp", "r")
while line = tmp.gets()
next if /vt100/ =~ line
$bad = 1 if /vt100/ =~ line
end
assert(!(!tmp.eof? || /vt100/ =~ line || $bad))
tmp.close
$bad = false
tmp = open("while_tmp", "r")
while tmp.gets()
line = $_
gsub(/vt100/, 'VT100')
if $_ != line
$_.gsub!('VT100', 'Vt100')
redo
end
$bad = 1 if /vt100/ =~ $_
$bad = 1 if /VT100/ =~ $_
end
assert(tmp.eof? && !$bad)
tmp.close
sum=0
for i in 1..10
sum += i
i -= 1
if i > 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