* test/ruby/test_primitive.rb: new test.

* yarvtest/test_bin.rb: removed (altered by test/ruby/test_{literal,primitive}.rb).


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11828 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2007-02-23 06:41:25 +00:00
Родитель 5834171904
Коммит f35a641adb
2 изменённых файлов: 397 добавлений и 585 удалений

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

@ -0,0 +1,397 @@
require 'test/unit'
class TestRubyPrimitive < Test::Unit::TestCase
def test_not
assert_equal false, !true
assert_equal true, !false
assert_equal true, !nil
assert_equal false, !(1+1)
assert_equal false, !!nil
assert_equal true, !!1
end
def test_lvar
a = 1
assert_equal 1, a
b = 2
assert_equal 1, a
a = b = 3
assert_equal 3, a
assert_equal 3, b
a = b = c = 4
assert_equal 4, a
assert_equal 4, b
assert_equal 4, c
end
C = 1
class A
Const = 1
class B
Const = 2
class C
Const = 3
def const
Const
end
end
end
end
(1..2).map {
A::B::C::Const
}
def test_constant
assert_equal 1, C
assert_equal 1, C
assert_equal 1, A::Const
assert_equal 2, A::B::Const
assert_equal 3, A::B::C::Const
assert_equal 3, A::B::C.new.const
assert_equal 1, ::TestRubyPrimitive::A::Const
A::B::C.funcall(:remove_const, :Const)
assert_equal 2, A::B::C.new.const
assert_raise(TypeError) {
C::CONST
}
end
class A2
class B2
class C2
C = 7
end
end
end
def test_constant_cache
i = 0
while i < 3
r = A2::B2::C2::C
i += 1
end
assert_equal 7, r
end
class A3
class B3
C = 99
end
end
i = 0
while i < 3
r = A3::B3::C # cache
class A3::B3
remove_const :C
end
A3::B3::C = i ** i
i += 1
end
def test_constant_cache2
assert_equal 4, A3::B3::C
end
class A4
Const = 7
(1..3).map {
$test_ruby_primitive_constant_cache3 = self::Const
}
end
def test_constant_cache3
assert_equal 7, $test_ruby_primitive_constant_cache3
end
class A5
Const = 8
(1..3).map {
$test_ruby_primitive_constant_cache4 = eval('self')::Const
}
end
def test_constatant_cache4
assert_equal 8, $test_ruby_primitive_constant_cache4
end
class A6
Const = 0
def self.foo
self::Const
end
end
class B6 < A6
Const = 1
end
class C6 < B6
Const = 2
end
$test_ruby_primitive_constant_cache5 = [A6.foo, B6.foo, C6.foo]
def test_constant_cache5
assert_equal [0, 1, 2], $test_ruby_primitive_constant_cache5
end
def test_gvar
$test_ruby_primitive_gvar = 7
assert_equal 7, $test_ruby_primitive_gvar
assert_equal 7, $test_ruby_primitive_gvar
$test_ruby_primitive_gvar = 88
assert_equal 88, $test_ruby_primitive_gvar
assert_equal 88, $test_ruby_primitive_gvar
assert_equal 7, ($test_ruby_primitive_gvar = 7)
assert_equal 7, ($test_ruby_primitive_gvar = 7)
end
class A7
@@c = 1
def m
@@c += 1
end
end
def test_cvar_from_instance_method
assert_equal 2, A7.new.m
assert_equal 3, A7.new.m
assert_equal 4, A7.new.m
end
class A8
@@c = 1
class << self
def m
@@c += 1
end
end
end
def test_cvar_from_singleton_method
assert_equal 2, A8.m
assert_equal 3, A8.m
assert_equal 4, A8.m
end
class A9
@@c = 1
def self.m
@@c += 1
end
end
def test_cvar_from_singleton_method2
assert_equal 2, A9.m
assert_equal 3, A9.m
assert_equal 4, A9.m
end
class A10
attr_accessor :a
end
def test_opassign
i = 0
i += 1
assert_equal 1, i
@iv = 2
@iv += 2
assert_equal 4, @iv
@@cv ||= 1
assert_equal 1, @@cv
@@cv &&= 2
assert_equal 2, @@cv
@@cv ||= 99
assert_equal 2, @@cv
$gv = 3
$gv += 4
assert_equal 7, $gv
obj = A10.new
obj.a = 9
obj.a &&= 7
assert_equal 7, obj.a
obj.a = nil
obj.a ||= 2
assert_equal 2, obj.a
obj.a &&= 3
assert_equal 3, obj.a
a = []
a[0] ||= 3
assert_equal 3, a[0]
a[0] &&= 7
assert_equal 7, a[0]
a[0] ||= 3
assert_equal 7, a[0]
end
def test_opassign_and_or
a = 1
a ||= 2
assert_equal 1, a
a = nil
a ||= 2
assert_equal 2, a
a = 1
a &&= 3
assert_equal 3, a
a = nil
a &&= 4
assert_nil a
h = {}
h[0] ||= 1
assert_equal 1, h[0]
h = {}
h[0] &&= 1
assert_nil h[0]
h = {0 => 7}
h[0] ||= 1
assert_equal 7, h[0]
h = {0 => 7}
h[0] &&= 1
assert_equal 1, h[0]
end
def test_backref
/a(b)(c)d/ =~ 'xyzabcdefgabcdefg'
assert_equal 'b', $1
assert_equal 'c', $2
assert_nil $3
assert_instance_of MatchData, $~
assert_equal 'abcd', $&
assert_equal 'xyz', $`
assert_equal 'efgabcdefg', $'
assert_equal 'c', $+
/(?!)/ =~ 'xyzabcdefgabcdefg'
assert_nil $1
assert_nil $2
assert_nil $3
assert_nil $~
assert_nil $&
assert_nil $`
assert_nil $'
assert_nil $+
end
def test_fact
assert_equal 306057512216440636035370461297268629388588804173576999416776741259476533176716867465515291422477573349939147888701726368864263907759003154226842927906974559841225476930271954604008012215776252176854255965356903506788725264321896264299365204576448830388909753943489625436053225980776521270822437639449120128678675368305712293681943649956460498166450227716500185176546469340112226034729724066333258583506870150169794168850353752137554910289126407157154830282284937952636580145235233156936482233436799254594095276820608062232812387383880817049600000000000000000000000000000000000000000000000000000000000000000000000000, fact(300)
end
def fact(n)
if n > 1
n * fact(n - 1)
else
1
end
end
def test_mul
assert_equal 0, 2 * 0
assert_equal 0, 0 * 2
assert_equal 4, 2 * 2
end
class MyNum
def /(a)
a * 100
end
end
def test_div
assert_equal 1, 3 / 2
assert_equal 1.5, 3.0 / 2.0
assert_equal 300, MyNum.new / 3
end
class MyArr
def length
'string'
end
end
def test_length
assert_equal 0, [].length
assert_equal 1, [1].length
assert_equal 2, [1,2].length
assert_equal 0, {}.length
assert_equal 1, {1=>1}.length
assert_equal 2, {1=>1, 2=>2}.length
assert_equal 'string', MyArr.new.length
end
class MyNum2
def %(a)
a * 100
end
end
def test_mod
assert_equal 2, 5 % 3
assert_equal 1.0, 3.0 % 2.0
assert_equal 300, MyNum2.new % 3
end
class MyObj
def [](*args)
args
end
def []=(*args)
args
end
end
def test_aref
a = [0,1]
assert_equal 0, a[0]
assert_equal 1, a[1]
assert_nil a[2]
h = {0=>0, 1=>1}
obj = MyObj.new
assert_equal 0, h[0]
assert_equal 1, h[1]
assert_nil h[2]
assert_equal [0], obj[0]
assert_equal [0,1], obj[0,1]
assert_equal [0,1,2], obj[0,1,2]
end
def test_aset
obj = MyObj.new
assert_equal 7, (obj[0] = 7)
assert_equal 7, (obj[0,1] = 7)
assert_equal 7, (obj[0,1,2] = 7)
end
class MyObj2
def attr=(*args)
args
end
end
def test_attr_setter
obj = MyObj2.new
assert_equal 1, (obj.attr = 1)
end
def test_list_expand
a = []
assert_equal [0], [0, *a]
a = [1]
assert_equal [0,1], [0, *a]
a = [1,2]
assert_equal [0,1,2], [0, *a]
a = [1,2,3]
assert_equal [0,1,2,3], [0, *a]
#a = [1,2,3]
#assert_equal [0,1,2,3,4], [0, *a, 4]
end
end

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

@ -1,585 +0,0 @@
require 'yarvtest/yarvtest'
# test of basic instruction
class TestBIN < YarvTestBase
def test_literal
ae %q(true)
ae %q(false)
ae %q(nil)
ae %q(1234)
ae %q(:sym)
ae %q(123456789012345678901234567890)
ae %q(1.234)
ae %q(0x12)
ae %q(0b0101001)
ae %q(1_2_3) # 123
end
def test_self
ae %q(self)
end
def test_string
ae %q('str')
end
def test_dstring
ae %q(
"1+1 = #{1+1}"
)
ae %q{
i = 10
"#{i} ** #{i} = #{i ** i}"
}
ae %q{
s = "str"
s.__id__ == "#{s}".__id__
}
end
def test_dsym
ae %q{
:"a#{1+2}c"
}
end
def test_xstr
ae %q(`echo hoge`)
ae %q(hoge = 'huga'; `echo #{hoge}`)
end
def test_regexp
ae %q{
/test/ =~ 'test'
}
ae %q{
/test/ =~ 'tes'
}
ae %q{
r = /test/; l = 'test'
r =~ l
}
ae %q{
r = /testx/; l = 'test'
r =~ l
}
ae %q{
i = 10
/test#{i}/ =~ 'test10'
}
ae %q{
i = 10
/test#{i}/ =~ 'test20'
}
ae %q{
:sym =~ /sym/
}
ae %q{
sym = :sym
sym =~ /sym/
}
ae %q{
reg = /sym/
:sym =~ reg
}
end
def test_array
ae %q([])
ae %q([1,2,3])
ae %q([1+1,2+2,3+3])
ae %q([0][0]+=3)
ae %q([0][0]-=3)
end
def test_array_access
ae %q(ary = [1,2,3]; ary[1])
ae %q(ary = [1,2,3]; ary[1] = 10)
ae %q(ary = Array.new(10, 100); ary[3])
end
def test_hash
ae %q({})
ae %q({1 => 2})
ae %q({"str" => "val", "str2" => "valval"})
ae %q({1 => 2, 1=>3})
end
def test_range
ae %q((1..2))
ae %q((1...2))
ae %q(((1+1)..(2+2)))
ae %q(((1+1)...(2+2)))
end
def test_not
ae %q(!true)
ae %q(!nil)
ae %q(!false)
ae %q(!(1+1))
ae %q(!!nil)
ae %q(!!1)
end
# var
def test_local
ae %q(a = 1)
ae %q(a = 1; b = 2; a)
ae %q(a = b = 3)
ae %q(a = b = 3; a)
ae %q(a = b = c = 4)
ae %q(a = b = c = 4; c)
end
def test_constant
ae %q(C = 1; C)
ae %q(C = 1; $a = []; 2.times{$a << ::C}; $a)
ae %q(
class A
class B
class C
Const = 1
end
end
end
(1..2).map{
A::B::C::Const
}
) do
remove_const :A
end
ae %q(
class A
class B
Const = 1
class C
(1..2).map{
Const
}
end
end
end
) do
remove_const :A
end
ae %q(
class A
Const = 1
class B
class C
(1..2).map{
Const
}
end
end
end
) do
remove_const :A
end
ae %q(
Const = 1
class A
class B
class C
(1..2).map{
Const
}
end
end
end
) do
remove_const :A
remove_const :Const
end
ae %q{
C = 1
begin
C::D
rescue TypeError
:ok
else
:ng
end
}
end
def test_constant2
ae %q{
class A
class B
C = 10
end
end
i = 0
while i<3
i+=1
r = A::B::C
end
r
} do
remove_const :A
end
ae %q{
class A
class B
C = 10
end
end
i = 0
while i<3
i+=1
r = A::B::C
class A::B
remove_const :C
end
A::B::C = i**i
end
r
} do
remove_const :A
end
ae %q{
class C
Const = 1
(1..3).map{
self::Const
}
end
}
ae %q{
class C
Const = 1
(1..3).map{
eval('self')::Const
}
end
}
ae %q{
class C
Const = 0
def self.foo()
self::Const
end
end
class D < C
Const = 1
end
class E < C
Const = 2
end
[C.foo, D.foo, E.foo]
}
end
def test_gvar
ae %q(
$g1 = 1
)
ae %q(
$g2 = 2
$g2
)
end
def test_cvar
ae %q{
class C
@@c = 1
def m
@@c += 1
end
end
C.new.m
} do
remove_const :C
end
end
def test_cvar_from_singleton
ae %q{
class C
@@c=1
class << self
def m
@@c += 1
end
end
end
C.m
} do
remove_const :C
end
end
def test_cvar_from_singleton2
ae %q{
class C
@@c = 1
def self.m
@@c += 1
end
end
C.m
} do
remove_const :C
end
end
def test_op_asgin2
ae %q{
class C
attr_accessor :a
end
r = []
o = C.new
o.a &&= 1
r << o.a
o.a ||= 2
r << o.a
o.a &&= 3
r << o.a
r
} do
remove_const :C
end
ae %q{
@@x ||= 1
}
ae %q{
@@x = 0
@@x ||= 1
}
end
def test_op_assgin_and_or
ae %q{
r = []
a = 1 ; a ||= 2; r << a
a = nil; a ||= 2; r << a
a = 1 ; a &&= 2; r << a
a = nil; a &&= 2; r << a
r
}
ae %q{
a = {}
a[0] ||= 1
}
ae %q{
a = {}
a[0] &&= 1
}
ae %q{
a = {0 => 10}
a[0] ||= 1
}
ae %q{
a = {0 => 10}
a[0] &&= 1
}
end
def test_backref
ae %q{
/a(b)(c)d/ =~ 'xyzabcdefgabcdefg'
[$1, $2, $3, $~.class, $&, $`, $', $+]
}
ae %q{
def m
/a(b)(c)d/ =~ 'xyzabcdefgabcdefg'
[$1, $2, $3, $~.class, $&, $`, $', $+]
end
m
}
end
def test_fact
ae %q{
def fact(n)
if(n > 1)
n * fact(n-1)
else
1
end
end
fact(300)
}
end
def test_mul
ae %q{
2*0
}
ae %q{
0*2
}
ae %q{
2*2
}
end
def test_div
ae %q{
3/2
}
ae %q{
3.0/2.0
}
ae %q{
class C
def /(a)
a * 100
end
end
C.new/3
} do
remove_const :C
end
end
def test_length
ae %q{
[].length
}
ae %q{
[1, 2].length
}
ae %q{
{}.length
}
ae %q{
{:a => 1, :b => 2}.length
}
ae %q{
class C
def length
'hoge'
end
end
C.new.length
} do
remove_const :C
end
end
def test_mod
ae %q{
3%2
}
ae %q{
3.0%2.0
}
ae %q{
class C
def % (a)
a * 100
end
end
C.new%3
} do
remove_const :C
end
end
def test_attr_set
ae %q{
o = Object.new
def o.[]=(*args)
args
end
[o[]=:x, o[0]=:x, o[0, 1]=:x, o[0, 1, 2]=:x]
}
ae %q{
o = Object.new
def o.foo=(*args)
args
end
o.foo = :x
}
ae %q{
$r = []
class C
def [](*args)
$r << [:ref, args]
args.size
end
def []=(*args)
$r << [:set, args]
args.size
end
end
o = C.new
ary = [:x, :y]
o[1] = 2
o[1, 2] = 3
o[1, 2, *ary] = 3
o[1, 2, *ary, 3] = 4
$r
}
end
def test_aref_aset
ae %q{
a = []
a << 0
a[1] = 1
a[2] = 2
a[3] = a[1] + a[2]
}
ae %q{
a = {}
a[1] = 1
a[2] = 2
a[3] = a[1] + a[2]
a.sort
}
ae %q{
class C
attr_reader :a, :b
def [](a)
@a = a
end
def []=(a, b)
@b = [a, b]
end
end
c = C.new
c[3]
c[4] = 5
[c.a, c.b]
} do
remove_const :C
end
end
def test_array_concat
ae %q{
ary = []
[:x, *ary]
}
#ae %q{
# ary = 1
# [:x, *ary]
#}
ae %q{
ary = [1, 2]
[:x, *ary]
}
end
end