diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-12-31 15:02:22 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-12-31 15:02:22 +0000 |
commit | a3e1b1ce7ed7e7ffac23015fc2fde56511b30681 (patch) | |
tree | 7b725552a9a4ded93849ca2faab1b257f7761790 /test/ruby | |
parent | 3e7566d8fb5138bb9cd647e5fdefc54fc9803509 (diff) | |
download | ruby-a3e1b1ce7ed7e7ffac23015fc2fde56511b30681.tar.gz |
* Merge YARV
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11439 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/beginmainend.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_alias.rb | 56 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 1039 | ||||
-rw-r--r-- | test/ruby/test_basicinstructions.rb | 628 | ||||
-rw-r--r-- | test/ruby/test_beginendblock.rb | 15 | ||||
-rw-r--r-- | test/ruby/test_class.rb | 80 | ||||
-rw-r--r-- | test/ruby/test_clone.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_const.rb | 27 | ||||
-rw-r--r-- | test/ruby/test_dir.rb | 10 | ||||
-rw-r--r-- | test/ruby/test_eval.rb | 273 | ||||
-rw-r--r-- | test/ruby/test_gc.rb | 5 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 562 | ||||
-rw-r--r-- | test/ruby/test_iterator.rb | 8 | ||||
-rw-r--r-- | test/ruby/test_lambda.rb | 8 | ||||
-rw-r--r-- | test/ruby/test_marshal.rb | 29 | ||||
-rw-r--r-- | test/ruby/test_module.rb | 316 | ||||
-rw-r--r-- | test/ruby/test_pipe.rb | 1 | ||||
-rw-r--r-- | test/ruby/test_proc.rb | 21 | ||||
-rw-r--r-- | test/ruby/test_readpartial.rb | 8 | ||||
-rw-r--r-- | test/ruby/test_signal.rb | 37 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 1298 |
21 files changed, 4307 insertions, 118 deletions
diff --git a/test/ruby/beginmainend.rb b/test/ruby/beginmainend.rb index 646140dd22..6cdfb15ea6 100644 --- a/test/ruby/beginmainend.rb +++ b/test/ruby/beginmainend.rb @@ -16,7 +16,7 @@ BEGIN { } # for scope check -raise if defined?(local_begin1) +#raise if defined?(local_begin1) raise unless defined?($global_begin1) raise unless defined?(::ConstBegin1) local_for_end2 = "e2" diff --git a/test/ruby/test_alias.rb b/test/ruby/test_alias.rb index 83f897fb00..18a7d475e2 100644 --- a/test/ruby/test_alias.rb +++ b/test/ruby/test_alias.rb @@ -2,39 +2,63 @@ require 'test/unit' class TestAlias < Test::Unit::TestCase class Alias0 - def foo; "foo" end + def foo + "foo" + end end - class Alias1<Alias0 + + class Alias1 < Alias0 alias bar foo - def foo; "foo+" + super end + + def foo + "foo+#{super}" + end end - class Alias2<Alias1 + + class Alias2 < Alias1 alias baz foo undef foo end - class Alias3<Alias2 + + class Alias3 < Alias2 def foo - defined? super + super end + def bar - defined? super + super end + def quux - defined? super + super end end def test_alias x = Alias2.new - assert_equal("foo", x.bar) - assert_equal("foo+foo", x.baz) - - # test_check for cache - assert_equal("foo+foo", x.baz) + assert_equal "foo", x.bar + assert_equal "foo+foo", x.baz + assert_equal "foo+foo", x.baz # test_check for cache x = Alias3.new - assert(!x.foo) - assert(x.bar) - assert(!x.quux) + assert_raise(NoMethodError) { x.foo } + assert_equal "foo", x.bar + assert_raise(NoMethodError) { x.quux } + end + + class C + def m + $SAFE + end + end + + def test_JVN_83768862 + d = lambda { + $SAFE = 4 + dclass = Class.new(C) + dclass.funcall(:alias_method, :mm, :m) + dclass.new + }.call + assert_raise(SecurityError) { d.mm } end end diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index fc55b86176..4667f4cff7 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1,7 +1,7 @@ require 'test/unit' class TestArray < Test::Unit::TestCase - def test_array + def test_0_literal assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) assert_equal([1, 2, 1, 2], [1, 2] * 2) assert_equal("1:2", [1, 2] * ":") @@ -27,29 +27,32 @@ class TestArray < Test::Unit::TestCase assert(x[-1] == 20 && x.pop == 20) end - def test_array_andor + def test_array_andor_0 assert_equal([2], ([1,2,3]&[2,4,6])) assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6])) end - def test_compact - x = [nil, 1, nil, nil, 5, nil, nil] - x.compact! - assert_equal([1, 5], x) + def test_compact_0 + a = [nil, 1, nil, nil, 5, nil, nil] + assert_equal [1, 5], a.compact + assert_equal [nil, 1, nil, nil, 5, nil, nil], a + a.compact! + assert_equal [1, 5], a end - def test_uniq + def test_uniq_0 x = [1, 1, 4, 2, 5, 4, 5, 1, 2] x.uniq! assert_equal([1, 4, 2, 5], x) + end - # empty? - assert(!x.empty?) - x = [] - assert(x.empty?) + def test_empty_0 + assert_equal true, [].empty? + assert_equal false, [1].empty? + assert_equal false, [1, 1, 4, 2, 5, 4, 5, 1, 2].empty? end - def test_sort + def test_sort_0 x = ["it", "came", "to", "pass", "that", "..."] x = x.sort.join(" ") assert_equal("... came it pass that to", x) @@ -60,7 +63,7 @@ class TestArray < Test::Unit::TestCase assert_equal([7,5,3,2,1], x) end - def test_split + def test_split_0 x = "The Boassert of Mormon" assert_equal(x.reverse, x.split(//).reverse!.join) assert_equal(x.reverse, x.reverse!) @@ -70,7 +73,7 @@ class TestArray < Test::Unit::TestCase assert_equal(['a', 'b', 'c', 'd'], x.split(' ')) end - def test_misc + def test_misc_0 assert(defined? "a".chomp) assert_equal(["a", "b", "c"], "abc".scan(/./)) assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/)) @@ -110,7 +113,7 @@ class TestArray < Test::Unit::TestCase assert_equal([1,2,3,5], y) end - def test_beg_end + def test_beg_end_0 x = [1, 2, 3, 4, 5] assert_equal(1, x.first) @@ -138,7 +141,7 @@ class TestArray < Test::Unit::TestCase assert_equal([1, 2, 3, 4, 5], x) end - def test_find_all + def test_find_all_0 assert_respond_to([], :find_all) assert_respond_to([], :select) # Alias assert_equal([], [].find_all{ |obj| obj == "foo"}) @@ -148,7 +151,7 @@ class TestArray < Test::Unit::TestCase assert_equal([3,3], x.find_all{ |obj| obj == 3 }) end - def test_fill + def test_fill_0 assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1)) assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3)) assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2)) @@ -170,4 +173,1006 @@ class TestArray < Test::Unit::TestCase assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10}) assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10}) end + + # From rubicon + + def setup + @cls = Array + end + + def test_00_new + a = @cls.new() + assert_instance_of(@cls, a) + assert_equal(0, a.length) + assert_nil(a[0]) + end + + def test_01_square_brackets + a = @cls[ 5, 4, 3, 2, 1 ] + assert_instance_of(@cls, a) + assert_equal(5, a.length) + 5.times { |i| assert_equal(5-i, a[i]) } + assert_nil(a[6]) + end + + def test_AND # '&' + assert_equal(@cls[1, 3], @cls[ 1, 1, 3, 5 ] & @cls[ 1, 2, 3 ]) + assert_equal(@cls[], @cls[ 1, 1, 3, 5 ] & @cls[ ]) + assert_equal(@cls[], @cls[ ] & @cls[ 1, 2, 3 ]) + assert_equal(@cls[], @cls[ 1, 2, 3 ] & @cls[ 4, 5, 6 ]) + end + + def test_MUL # '*' + assert_equal(@cls[], @cls[]*3) + assert_equal(@cls[1, 1, 1], @cls[1]*3) + assert_equal(@cls[1, 2, 1, 2, 1, 2], @cls[1, 2]*3) + assert_equal(@cls[], @cls[1, 2, 3] * 0) + assert_raise(ArgumentError) { @cls[1, 2]*(-3) } + + assert_equal('1-2-3-4-5', @cls[1, 2, 3, 4, 5] * '-') + assert_equal('12345', @cls[1, 2, 3, 4, 5] * '') + + end + + def test_PLUS # '+' + assert_equal(@cls[], @cls[] + @cls[]) + assert_equal(@cls[1], @cls[1] + @cls[]) + assert_equal(@cls[1], @cls[] + @cls[1]) + assert_equal(@cls[1, 1], @cls[1] + @cls[1]) + assert_equal(@cls['cat', 'dog', 1, 2, 3], %w(cat dog) + (1..3).to_a) + end + + def test_MINUS # '-' + assert_equal(@cls[], @cls[1] - @cls[1]) + assert_equal(@cls[1], @cls[1, 2, 3, 4, 5] - @cls[2, 3, 4, 5]) + # Ruby 1.8 feature change + #assert_equal(@cls[1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5]) + assert_equal(@cls[1, 1, 1, 1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5]) + a = @cls[] + 1000.times { a << 1 } + assert_equal(1000, a.length) + #assert_equal(@cls[1], a - @cls[2]) + assert_equal(@cls[1] * 1000, a - @cls[2]) + #assert_equal(@cls[1], @cls[1, 2, 1] - @cls[2]) + assert_equal(@cls[1, 1], @cls[1, 2, 1] - @cls[2]) + assert_equal(@cls[1, 2, 3], @cls[1, 2, 3] - @cls[4, 5, 6]) + end + + def test_LSHIFT # '<<' + a = @cls[] + a << 1 + assert_equal(@cls[1], a) + a << 2 << 3 + assert_equal(@cls[1, 2, 3], a) + a << nil << 'cat' + assert_equal(@cls[1, 2, 3, nil, 'cat'], a) + a << a + assert_equal(@cls[1, 2, 3, nil, 'cat', a], a) + end + + def test_CMP # '<=>' + assert_equal(0, @cls[] <=> @cls[]) + assert_equal(0, @cls[1] <=> @cls[1]) + assert_equal(0, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'cat']) + assert_equal(-1, @cls[] <=> @cls[1]) + assert_equal(1, @cls[1] <=> @cls[]) + assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 'cat']) + assert_equal(1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3]) + assert_equal(-1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'dog']) + assert_equal(1, @cls[1, 2, 3, 'dog'] <=> @cls[1, 2, 3, 'cat']) + end + + def test_EQUAL # '==' + assert(@cls[] == @cls[]) + assert(@cls[1] == @cls[1]) + assert(@cls[1, 1, 2, 2] == @cls[1, 1, 2, 2]) + assert(@cls[1.0, 1.0, 2.0, 2.0] == @cls[1, 1, 2, 2]) + end + + def test_VERY_EQUAL # '===' + assert(@cls[] === @cls[]) + assert(@cls[1] === @cls[1]) + assert(@cls[1, 1, 2, 2] === @cls[1, 1, 2, 2]) + assert(@cls[1.0, 1.0, 2.0, 2.0] === @cls[1, 1, 2, 2]) + end + + def test_AREF # '[]' + a = @cls[*(1..100).to_a] + + assert_equal(1, a[0]) + assert_equal(100, a[99]) + assert_nil(a[100]) + assert_equal(100, a[-1]) + assert_equal(99, a[-2]) + assert_equal(1, a[-100]) + assert_nil(a[-101]) + assert_nil(a[-101,0]) + assert_nil(a[-101,1]) + assert_nil(a[-101,-1]) + assert_nil(a[10,-1]) + + assert_equal(@cls[1], a[0,1]) + assert_equal(@cls[100], a[99,1]) + assert_equal(@cls[], a[100,1]) + assert_equal(@cls[100], a[99,100]) + assert_equal(@cls[100], a[-1,1]) + assert_equal(@cls[99], a[-2,1]) + assert_equal(@cls[], a[-100,0]) + assert_equal(@cls[1], a[-100,1]) + + assert_equal(@cls[10, 11, 12], a[9, 3]) + assert_equal(@cls[10, 11, 12], a[-91, 3]) + + assert_equal(@cls[1], a[0..0]) + assert_equal(@cls[100], a[99..99]) + assert_equal(@cls[], a[100..100]) + assert_equal(@cls[100], a[99..200]) + assert_equal(@cls[100], a[-1..-1]) + assert_equal(@cls[99], a[-2..-2]) + + assert_equal(@cls[10, 11, 12], a[9..11]) + assert_equal(@cls[10, 11, 12], a[-91..-89]) + + assert_nil(a[10, -3]) + # Ruby 1.8 feature change: + # Array#[size..x] returns [] instead of nil. + #assert_nil(a[10..7]) + assert_equal [], a[10..7] + + assert_raise(TypeError) {a['cat']} + end + + def test_ASET # '[]=' + a = @cls[*(0..99).to_a] + assert_equal(0, a[0] = 0) + assert_equal(@cls[0] + @cls[*(1..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(0, a[10,10] = 0) + assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(0, a[-1] = 0) + assert_equal(@cls[*(0..98).to_a] + @cls[0], a) + + a = @cls[*(0..99).to_a] + assert_equal(0, a[-10, 10] = 0) + assert_equal(@cls[*(0..89).to_a] + @cls[0], a) + + a = @cls[*(0..99).to_a] + assert_equal(0, a[0,1000] = 0) + assert_equal(@cls[0] , a) + + a = @cls[*(0..99).to_a] + assert_equal(0, a[10..19] = 0) + assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a) + + b = @cls[*%w( a b c )] + a = @cls[*(0..99).to_a] + assert_equal(b, a[0,1] = b) + assert_equal(b + @cls[*(1..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(b, a[10,10] = b) + assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(b, a[-1, 1] = b) + assert_equal(@cls[*(0..98).to_a] + b, a) + + a = @cls[*(0..99).to_a] + assert_equal(b, a[-10, 10] = b) + assert_equal(@cls[*(0..89).to_a] + b, a) + + a = @cls[*(0..99).to_a] + assert_equal(b, a[0,1000] = b) + assert_equal(b , a) + + a = @cls[*(0..99).to_a] + assert_equal(b, a[10..19] = b) + assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a) + + # Ruby 1.8 feature change: + # assigning nil does not remove elements. +=begin + a = @cls[*(0..99).to_a] + assert_equal(nil, a[0,1] = nil) + assert_equal(@cls[*(1..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[10,10] = nil) + assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[-1, 1] = nil) + assert_equal(@cls[*(0..98).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[-10, 10] = nil) + assert_equal(@cls[*(0..89).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[0,1000] = nil) + assert_equal(@cls[] , a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[10..19] = nil) + assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a) +=end + + a = @cls[1, 2, 3] + a[1, 0] = a + assert_equal([1, 1, 2, 3, 2, 3], a) + + a = @cls[1, 2, 3] + a[-1, 0] = a + assert_equal([1, 2, 1, 2, 3, 3], a) + end + + def test_assoc + a1 = @cls[*%w( cat feline )] + a2 = @cls[*%w( dog canine )] + a3 = @cls[*%w( mule asinine )] + + a = @cls[ a1, a2, a3 ] + + assert_equal(a1, a.assoc('cat')) + assert_equal(a3, a.assoc('mule')) + assert_equal(nil, a.assoc('asinine')) + assert_equal(nil, a.assoc('wombat')) + assert_equal(nil, a.assoc(1..2)) + end + + def test_at + a = @cls[*(0..99).to_a] + assert_equal(0, a.at(0)) + assert_equal(10, a.at(10)) + assert_equal(99, a.at(99)) + assert_equal(nil, a.at(100)) + assert_equal(99, a.at(-1)) + assert_equal(0, a.at(-100)) + assert_equal(nil, a.at(-101)) + assert_raise(TypeError) { a.at('cat') } + end + + def test_clear + a = @cls[1, 2, 3] + b = a.clear + assert_equal(@cls[], a) + assert_equal(@cls[], b) + assert_equal(a.__id__, b.__id__) + end + + def test_clone + for taint in [ false, true ] + for frozen in [ false, true ] + a = @cls[*(0..99).to_a] + a.taint if taint + a.freeze if frozen + b = a.clone + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert_equal(a.frozen?, b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_collect + a = @cls[ 1, 'cat', 1..1 ] + assert_equal([ Fixnum, String, Range], a.collect {|e| e.class} ) + assert_equal([ 99, 99, 99], a.collect { 99 } ) + + assert_equal([], @cls[].collect { 99 }) + + # Ruby 1.9 feature change: + # Enumerable#collect without block returns an Enumerator. + #assert_equal([1, 2, 3], @cls[1, 2, 3].collect) + assert_kind_of Enumerable::Enumerator, @cls[1, 2, 3].collect + end + + # also update map! + def test_collect! + a = @cls[ 1, 'cat', 1..1 ] + assert_equal([ Fixnum, String, Range], a.collect! {|e| e.class} ) + assert_equal([ Fixnum, String, Range], a) + + a = @cls[ 1, 'cat', 1..1 ] + assert_equal([ 99, 99, 99], a.collect! { 99 } ) + assert_equal([ 99, 99, 99], a) + + a = @cls[ ] + assert_equal([], a.collect! { 99 }) + assert_equal([], a) + end + + def test_compact + a = @cls[ 1, nil, nil, 2, 3, nil, 4 ] + assert_equal(@cls[1, 2, 3, 4], a.compact) + + a = @cls[ nil, 1, nil, 2, 3, nil, 4 ] + assert_equal(@cls[1, 2, 3, 4], a.compact) + + a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ] + assert_equal(@cls[1, 2, 3, 4], a.compact) + + a = @cls[ 1, 2, 3, 4 ] + assert_equal(@cls[1, 2, 3, 4], a.compact) + end + + def test_compact! + a = @cls[ 1, nil, nil, 2, 3, nil, 4 ] + assert_equal(@cls[1, 2, 3, 4], a.compact!) + assert_equal(@cls[1, 2, 3, 4], a) + + a = @cls[ nil, 1, nil, 2, 3, nil, 4 ] + assert_equal(@cls[1, 2, 3, 4], a.compact!) + assert_equal(@cls[1, 2, 3, 4], a) + + a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ] + assert_equal(@cls[1, 2, 3, 4], a.compact!) + assert_equal(@cls[1, 2, 3, 4], a) + + a = @cls[ 1, 2, 3, 4 ] + assert_equal(nil, a.compact!) + assert_equal(@cls[1, 2, 3, 4], a) + end + + def test_concat + assert_equal(@cls[1, 2, 3, 4], @cls[1, 2].concat(@cls[3, 4])) + assert_equal(@cls[1, 2, 3, 4], @cls[].concat(@cls[1, 2, 3, 4])) + assert_equal(@cls[1, 2, 3, 4], @cls[1, 2, 3, 4].concat(@cls[])) + assert_equal(@cls[], @cls[].concat(@cls[])) + assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]])) + + a = @cls[1, 2, 3] + a.concat(a) + assert_equal([1, 2, 3, 1, 2, 3], a) + end + + def test_delete + a = @cls[*('cab'..'cat').to_a] + assert_equal('cap', a.delete('cap')) + assert_equal(@cls[*('cab'..'cao').to_a] + @cls[*('caq'..'cat').to_a], a) + + a = @cls[*('cab'..'cat').to_a] + assert_equal('cab', a.delete('cab')) + assert_equal(@cls[*('cac'..'cat').to_a], a) + + a = @cls[*('cab'..'cat').to_a] + assert_equal('cat', a.delete('cat')) + assert_equal(@cls[*('cab'..'cas').to_a], a) + + a = @cls[*('cab'..'cat').to_a] + assert_equal(nil, a.delete('cup')) + assert_equal(@cls[*('cab'..'cat').to_a], a) + + a = @cls[*('cab'..'cat').to_a] + assert_equal(99, a.delete('cup') { 99 } ) + assert_equal(@cls[*('cab'..'cat').to_a], a) + end + + def test_delete_at + a = @cls[*(1..5).to_a] + assert_equal(3, a.delete_at(2)) + assert_equal(@cls[1, 2, 4, 5], a) + + a = @cls[*(1..5).to_a] + assert_equal(4, a.delete_at(-2)) + assert_equal(@cls[1, 2, 3, 5], a) + + a = @cls[*(1..5).to_a] + assert_equal(nil, a.delete_at(5)) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[*(1..5).to_a] + assert_equal(nil, a.delete_at(-6)) + assert_equal(@cls[1, 2, 3, 4, 5], a) + end + + # also reject! + def test_delete_if + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.delete_if { false }) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.delete_if { true }) + assert_equal(@cls[], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.delete_if { |i| i > 3 }) + assert_equal(@cls[1, 2, 3], a) + end + + def test_dup + for taint in [ false, true ] + for frozen in [ false, true ] + a = @cls[*(0..99).to_a] + a.taint if taint + a.freeze if frozen + b = a.dup + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert_equal(false, b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_each + a = @cls[*%w( ant bat cat dog )] + i = 0 + a.each { |e| + assert_equal(a[i], e) + i += 1 + } + assert_equal(4, i) + + a = @cls[] + i = 0 + a.each { |e| + assert_equal(a[i], e) + i += 1 + } + assert_equal(0, i) + + assert_equal(a, a.each {}) + end + + def test_each_index + a = @cls[*%w( ant bat cat dog )] + i = 0 + a.each_index { |ind| + assert_equal(i, ind) + i += 1 + } + assert_equal(4, i) + + a = @cls[] + i = 0 + a.each_index { |ind| + assert_equal(i, ind) + i += 1 + } + assert_equal(0, i) + + assert_equal(a, a.each_index {}) + end + + def test_empty? + assert(@cls[].empty?) + assert(!@cls[1].empty?) + end + + def test_eql? + assert(@cls[].eql?(@cls[])) + assert(@cls[1].eql?(@cls[1])) + assert(@cls[1, 1, 2, 2].eql?(@cls[1, 1, 2, 2])) + assert(!@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2])) + end + + def test_fill + assert_equal(@cls[], @cls[].fill(99)) + assert_equal(@cls[], @cls[].fill(99, 0)) + assert_equal(@cls[99], @cls[].fill(99, 0, 1)) + assert_equal(@cls[99], @cls[].fill(99, 0..0)) + + assert_equal(@cls[99], @cls[1].fill(99)) + assert_equal(@cls[99], @cls[1].fill(99, 0)) + assert_equal(@cls[99], @cls[1].fill(99, 0, 1)) + assert_equal(@cls[99], @cls[1].fill(99, 0..0)) + + assert_equal(@cls[99, 99], @cls[1, 2].fill(99)) + assert_equal(@cls[99, 99], @cls[1, 2].fill(99, 0)) + assert_equal(@cls[99, 99], @cls[1, 2].fill(99, nil)) + assert_equal(@cls[1, 99], @cls[1, 2].fill(99, 1, nil)) + assert_equal(@cls[99, 2], @cls[1, 2].fill(99, 0, 1)) + assert_equal(@cls[99, 2], @cls[1, 2].fill(99, 0..0)) + end + + def test_first + assert_equal(3, @cls[3, 4, 5].first) + assert_equal(nil, @cls[].first) + end + + def test_flatten + a1 = @cls[ 1, 2, 3] + a2 = @cls[ 5, 6 ] + a3 = @cls[ 4, a2 ] + a4 = @cls[ a1, a3 ] + assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten) + assert_equal(@cls[ a1, a3], a4) + + a5 = @cls[ a1, @cls[], a3 ] + assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten) + assert_equal(@cls[], @cls[].flatten) + assert_equal(@cls[], + @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten) + end + + def test_flatten! + a1 = @cls[ 1, 2, 3] + a2 = @cls[ 5, 6 ] + a3 = @cls[ 4, a2 ] + a4 = @cls[ a1, a3 ] + assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten!) + assert_equal(@cls[1, 2, 3, 4, 5, 6], a4) + + a5 = @cls[ a1, @cls[], a3 ] + assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten!) + assert_equal(@cls[1, 2, 3, 4, 5, 6], a5) + + assert_equal(@cls[], @cls[].flatten) + assert_equal(@cls[], + @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten) + end + + def test_hash + a1 = @cls[ 'cat', 'dog' ] + a2 = @cls[ 'cat', 'dog' ] + a3 = @cls[ 'dog', 'cat' ] + assert(a1.hash == a2.hash) + assert(a1.hash != a3.hash) + end + + def test_include? + a = @cls[ 'cat', 99, /a/, @cls[ 1, 2, 3] ] + assert(a.include?('cat')) + assert(a.include?(99)) + assert(a.include?(/a/)) + assert(a.include?([1,2,3])) + assert(!a.include?('ca')) + assert(!a.include?([1,2])) + end + + def test_index + a = @cls[ 'cat', 99, /a/, 99, @cls[ 1, 2, 3] ] + assert_equal(0, a.index('cat')) + assert_equal(1, a.index(99)) + assert_equal(4, a.index([1,2,3])) + assert_nil(a.index('ca')) + assert_nil(a.index([1,2])) + end + + def test_values_at + a = @cls[*('a'..'j').to_a] + assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4)) + assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5)) + assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0)) + end + + def test_join + $, = "" + a = @cls[] + assert_equal("", a.join) + assert_equal("", a.join(',')) + + $, = "" + a = @cls[1, 2] + assert_equal("12", a.join) + assert_equal("1,2", a.join(',')) + + $, = "" + a = @cls[1, 2, 3] + assert_equal("123", a.join) + assert_equal("1,2,3", a.join(',')) + + $, = ":" + a = @cls[1, 2, 3] + assert_equal("1:2:3", a.join) + assert_equal("1,2,3", a.join(',')) + + $, = "" + end + + def test_last + assert_equal(nil, @cls[].last) + assert_equal(1, @cls[1].last) + assert_equal(99, @cls[*(3..99).to_a].last) + end + + def test_length + assert_equal(0, @cls[].length) + assert_equal(1, @cls[1].length) + assert_equal(2, @cls[1, nil].length) + assert_equal(2, @cls[nil, 1].length) + assert_equal(234, @cls[*(0..233).to_a].length) + end + + # also update collect! + def test_map! + a = @cls[ 1, 'cat', 1..1 ] + assert_equal(@cls[ Fixnum, String, Range], a.map! {|e| e.class} ) + assert_equal(@cls[ Fixnum, String, Range], a) + + a = @cls[ 1, 'cat', 1..1 ] + assert_equal(@cls[ 99, 99, 99], a.map! { 99 } ) + assert_equal(@cls[ 99, 99, 99], a) + + a = @cls[ ] + assert_equal(@cls[], a.map! { 99 }) + assert_equal(@cls[], a) + end + + def test_nitems + assert_equal(0, @cls[].nitems) + assert_equal(1, @cls[1].nitems) + assert_equal(1, @cls[1, nil].nitems) + assert_equal(1, @cls[nil, 1].nitems) + assert_equal(3, @cls[1, nil, nil, 2, nil, 3, nil].nitems) + end + + def test_pack + a = @cls[*%w( cat wombat x yy)] + assert_equal("catwomx yy ", a.pack("A3A3A3A3")) + assert_equal("cat", a.pack("A*")) + assert_equal("cwx yy ", a.pack("A3@1A3@2A3A3")) + assert_equal("catwomx\000\000yy\000", a.pack("a3a3a3a3")) + assert_equal("cat", a.pack("a*")) + assert_equal("ca", a.pack("a2")) + assert_equal("cat\000\000", a.pack("a5")) + + assert_equal("\x61", @cls["01100001"].pack("B8")) + assert_equal("\x61", @cls["01100001"].pack("B*")) + assert_equal("\x61", @cls["0110000100110111"].pack("B8")) + assert_equal("\x61\x37", @cls["0110000100110111"].pack("B16")) + assert_equal("\x61\x37", @cls["01100001", "00110111"].pack("B8B8")) + assert_equal("\x60", @cls["01100001"].pack("B4")) + assert_equal("\x40", @cls["01100001"].pack("B2")) + + assert_equal("\x86", @cls["01100001"].pack("b8")) + assert_equal("\x86", @cls["01100001"].pack("b*")) + assert_equal("\x86", @cls["0110000100110111"].pack("b8")) + assert_equal("\x86\xec", @cls["0110000100110111"].pack("b16")) + assert_equal("\x86\xec", @cls["01100001", "00110111"].pack("b8b8")) + assert_equal("\x06", @cls["01100001"].pack("b4")) + assert_equal("\x02", @cls["01100001"].pack("b2")) + + assert_equal("ABC", @cls[ 65, 66, 67 ].pack("C3")) + assert_equal("\377BC", @cls[ -1, 66, 67 ].pack("C*")) + assert_equal("ABC", @cls[ 65, 66, 67 ].pack("c3")) + assert_equal("\377BC", @cls[ -1, 66, 67 ].pack("c*")) + + + assert_equal("AB\n\x10", @cls["4142", "0a", "12"].pack("H4H2H1")) + assert_equal("AB\n\x02", @cls["1424", "a0", "21"].pack("h4h2h1")) + + assert_equal("abc=02def=\ncat=\n=01=\n", + @cls["abc\002def", "cat", "\001"].pack("M9M3M4")) + + assert_equal("aGVsbG8K\n", @cls["hello\n"].pack("m")) + assert_equal(",:&5L;&\\*:&5L;&\\*\n", @cls["hello\nhello\n"].pack("u")) + + assert_equal("\xc2\xa9B\xe2\x89\xa0", @cls[0xa9, 0x42, 0x2260].pack("U*")) + + + 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_equal(ary.length, ary2.length) + assert_equal(ary.join(':'), ary2.join(':')) + assert_not_nil(x =~ /def/) + +=begin + skipping "Not tested: + D,d & double-precision float, native format\\ + E & double-precision float, little-endian byte order\\ + e & single-precision float, little-endian byte order\\ + F,f & single-precision float, native format\\ + G & double-precision float, network (big-endian) byte order\\ + g & single-precision float, network (big-endian) byte order\\ + I & unsigned integer\\ + i & integer\\ + L & unsigned long\\ + l & long\\ + + N & long, network (big-endian) byte order\\ + n & short, network (big-endian) byte-order\\ + P & pointer to a structure (fixed-length string)\\ + p & pointer to a null-terminated string\\ + S & unsigned short\\ + s & short\\ + V & long, little-endian byte order\\ + v & short, little-endian byte order\\ + X & back up a byte\\ + x & null byte\\ + Z & ASCII string (null padded, count is width)\\ +" +=end + end + + def test_pop + a = @cls[ 'cat', 'dog' ] + assert_equal('dog', a.pop) + assert_equal(@cls['cat'], a) + assert_equal('cat', a.pop) + assert_equal(@cls[], a) + assert_nil(a.pop) + assert_equal(@cls[], a) + end + + def test_push + a = @cls[1, 2, 3] + assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5)) + assert_equal(@cls[1, 2, 3, 4, 5, nil], a.push(nil)) + # Ruby 1.8 feature: + # Array#push accepts any number of arguments. + #assert_raise(ArgumentError, "a.push()") { a.push() } + a.push + assert_equal @cls[1, 2, 3, 4, 5, nil], a + a.push 6, 7 + assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a + end + + def test_rassoc + a1 = @cls[*%w( cat feline )] + a2 = @cls[*%w( dog canine )] + a3 = @cls[*%w( mule asinine )] + a = @cls[ a1, a2, a3 ] + + assert_equal(a1, a.rassoc('feline')) + assert_equal(a3, a.rassoc('asinine')) + assert_equal(nil, a.rassoc('dog')) + assert_equal(nil, a.rassoc('mule')) + assert_equal(nil, a.rassoc(1..2)) + end + + # also delete_if + def test_reject! + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(nil, a.reject! { false }) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.reject! { true }) + assert_equal(@cls[], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.reject! { |i| i > 3 }) + assert_equal(@cls[1, 2, 3], a) + end + + def test_replace + a = @cls[ 1, 2, 3] + a_id = a.__id__ + assert_equal(@cls[4, 5, 6], a.replace(@cls[4, 5, 6])) + assert_equal(@cls[4, 5, 6], a) + assert_equal(a_id, a.__id__) + assert_equal(@cls[], a.replace(@cls[])) + end + + def test_reverse + a = @cls[*%w( dog cat bee ant )] + assert_equal(@cls[*%w(ant bee cat dog)], a.reverse) + assert_equal(@cls[*%w(dog cat bee ant)], a) + assert_equal(@cls[], @cls[].reverse) + end + + def test_reverse! + a = @cls[*%w( dog cat bee ant )] + assert_equal(@cls[*%w(ant bee cat dog)], a.reverse!) + assert_equal(@cls[*%w(ant bee cat dog)], a) + # Ruby 1.8 feature change: + # Array#reverse always returns self. + #assert_nil(@cls[].reverse!) + assert_equal @cls[], @cls[].reverse! + end + + def test_reverse_each + a = @cls[*%w( dog cat bee ant )] + i = a.length + a.reverse_each { |e| + i -= 1 + assert_equal(a[i], e) + } + assert_equal(0, i) + + a = @cls[] + i = 0 + a.reverse_each { |e| + assert(false, "Never get here") + } + assert_equal(0, i) + end + + def test_rindex + a = @cls[ 'cat', 99, /a/, 99, [ 1, 2, 3] ] + assert_equal(0, a.rindex('cat')) + assert_equal(3, a.rindex(99)) + assert_equal(4, a.rindex([1,2,3])) + assert_nil(a.rindex('ca')) + assert_nil(a.rindex([1,2])) + end + + def test_shift + a = @cls[ 'cat', 'dog' ] + assert_equal('cat', a.shift) + assert_equal(@cls['dog'], a) + assert_equal('dog', a.shift) + assert_equal(@cls[], a) + assert_nil(a.shift) + assert_equal(@cls[], a) + end + + def test_size + assert_equal(0, @cls[].size) + assert_equal(1, @cls[1].size) + assert_equal(100, @cls[*(0..99).to_a].size) + end + + def test_slice + a = @cls[*(1..100).to_a] + + assert_equal(1, a.slice(0)) + assert_equal(100, a.slice(99)) + assert_nil(a.slice(100)) + assert_equal(100, a.slice(-1)) + assert_equal(99, a.slice(-2)) + assert_equal(1, a.slice(-100)) + assert_nil(a.slice(-101)) + + assert_equal(@cls[1], a.slice(0,1)) + assert_equal(@cls[100], a.slice(99,1)) + assert_equal(@cls[], a.slice(100,1)) + assert_equal(@cls[100], a.slice(99,100)) + assert_equal(@cls[100], a.slice(-1,1)) + assert_equal(@cls[99], a.slice(-2,1)) + + assert_equal(@cls[10, 11, 12], a.slice(9, 3)) + assert_equal(@cls[10, 11, 12], a.slice(-91, 3)) + + assert_equal(@cls[1], a.slice(0..0)) + assert_equal(@cls[100], a.slice(99..99)) + assert_equal(@cls[], a.slice(100..100)) + assert_equal(@cls[100], a.slice(99..200)) + assert_equal(@cls[100], a.slice(-1..-1)) + assert_equal(@cls[99], a.slice(-2..-2)) + + assert_equal(@cls[10, 11, 12], a.slice(9..11)) + assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) + + assert_nil(a.slice(10, -3)) + # Ruby 1.8 feature change: + # Array#slice[size..x] always returns []. + #assert_nil(a.slice(10..7)) + assert_equal @cls[], a.slice(10..7) + end + + def test_slice! + a = @cls[1, 2, 3, 4, 5] + assert_equal(3, a.slice!(2)) + assert_equal(@cls[1, 2, 4, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(4, a.slice!(-2)) + assert_equal(@cls[1, 2, 3, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(@cls[3,4], a.slice!(2,2)) + assert_equal(@cls[1, 2, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(@cls[4,5], a.slice!(-2,2)) + assert_equal(@cls[1, 2, 3], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(@cls[3,4], a.slice!(2..3)) + assert_equal(@cls[1, 2, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(nil, a.slice!(20)) + assert_equal(@cls[1, 2, 3, 4, 5], a) + end + + def test_sort + a = @cls[ 4, 1, 2, 3 ] + assert_equal(@cls[1, 2, 3, 4], a.sort) + assert_equal(@cls[4, 1, 2, 3], a) + + assert_equal(@cls[4, 3, 2, 1], a.sort { |x, y| y <=> x} ) + assert_equal(@cls[4, 1, 2, 3], a) + + a.fill(1) + assert_equal(@cls[1, 1, 1, 1], a.sort) + + assert_equal(@cls[], @cls[].sort) + end + + def test_sort! + a = @cls[ 4, 1, 2, 3 ] + assert_equal(@cls[1, 2, 3, 4], a.sort!) + assert_equal(@cls[1, 2, 3, 4], a) + + assert_equal(@cls[4, 3, 2, 1], a.sort! { |x, y| y <=> x} ) + assert_equal(@cls[4, 3, 2, 1], a) + + a.fill(1) + assert_equal(@cls[1, 1, 1, 1], a.sort!) + + assert_equal(@cls[1], @cls[1].sort!) + assert_equal(@cls[], @cls[].sort!) + end + + def test_to_a + a = @cls[ 1, 2, 3 ] + a_id = a.__id__ + assert_equal(a, a.to_a) + assert_equal(a_id, a.to_a.__id__) + end + + def test_to_ary + a = [ 1, 2, 3 ] + b = @cls[*a] + + a_id = a.__id__ + assert_equal(a, b.to_ary) + if (@cls == Array) + assert_equal(a_id, a.to_ary.__id__) + end + end + + def test_to_s + $, = "" + a = @cls[] + assert_equal("[]", a.to_s) + + $, = "" + a = @cls[1, 2] + assert_equal("[1, 2]", a.to_s) + + $, = "" + a = @cls[1, 2, 3] + assert_equal("[1, 2, 3]", a.to_s) + + $, = ":" + a = @cls[1, 2, 3] + assert_equal("[1, 2, 3]", a.to_s) + + $, = "" + end + + def test_uniq + a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ] + b = a.dup + assert_equal(@cls[1, 2, 3, 4, nil], a.uniq) + assert_equal(b, a) + + assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq) + end + + def test_uniq! + a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ] + assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!) + assert_equal(@cls[1, 2, 3, 4, nil], a) + + assert_nil(@cls[1, 2, 3].uniq!) + end + + def test_unshift + a = @cls[] + assert_equal(@cls['cat'], a.unshift('cat')) + assert_equal(@cls['dog', 'cat'], a.unshift('dog')) + assert_equal(@cls[nil, 'dog', 'cat'], a.unshift(nil)) + assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2])) + end + + def test_OR # '|' + assert_equal(@cls[], @cls[] | @cls[]) + assert_equal(@cls[1], @cls[1] | @cls[]) + assert_equal(@cls[1], @cls[] | @cls[1]) + assert_equal(@cls[1], @cls[1] | @cls[1]) + + assert_equal(@cls[1,2], @cls[1] | @cls[2]) + assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2]) + assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2]) + end + end diff --git a/test/ruby/test_basicinstructions.rb b/test/ruby/test_basicinstructions.rb new file mode 100644 index 0000000000..3e52ef62d3 --- /dev/null +++ b/test/ruby/test_basicinstructions.rb @@ -0,0 +1,628 @@ +require 'test/unit' + +ConstTest = 3 +class Class + alias _remove_const remove_const + public :_remove_const +end + +class TestBasicInstructions < Test::Unit::TestCase + + def test_immediates + assert_equal((1==1), true) + assert_equal((1==2), false) + assert_equal [][0], nil + assert_equal "sym".intern, :sym + assert_equal "sym".intern, :"sym" + assert_equal 1234 + 0, 1234 + assert_equal 1234, 1_2_3_4 + assert_equal 41, 0b0101001 + assert_equal 420, 0644 + assert_equal 18, 0x12 + assert_equal 123456789012345678901234567890 + 0, + 123456789012345678901234567890 + assert_equal 1.234 + 0.0, 1.234 + end + + def test_self + assert_equal self, self + assert_equal false, (self == false) # Qfalse==0 in C + assert_equal false, (self == nil) + assert_equal false, (self == 0) + end + + def test_string + expected = "str" + "ing" + assert_equal expected, 'string' + assert_equal expected, "string" + assert_equal expected, %q(string) + assert_equal expected, %Q(string) + assert_equal expected, %(string) + end + + def test_dstring + assert_equal "2", "#{1+1}" + s = "OK" + assert_equal "OK", "#{s}" + assert_equal "OKx", "#{s}x" + assert_equal "xOK", "x#{s}" + assert_equal "xOKx", "x#{s}x" + end + + def test_dsym + assert_equal :a3c, :"a#{1+2}c" + s = "sym" + assert_equal :sym, :"#{s}" + assert_equal :sym, :"#{"#{"#{s}"}"}" + end + + def test_xstr + assert_equal 'hoge', `echo hoge`.chomp + assert_equal '3', `echo #{1 + 2}`.chomp + hoge = 'huga' + assert_equal 'huga', `echo #{hoge}`.chomp + end + + def test_regexp + assert_equal /test/, /test/ + assert_equal 'test', /test/.source + assert_equal 'TEST', /TEST/.source + assert_equal true, !!(/test/ =~ 'test') + assert_equal false, !!(/test/ =~ 'does not match') + + re = /test/ + assert_equal re, re + assert_equal 'test', re.source + assert_equal true, !!(re =~ 'test') + assert_equal false, !!(re =~ 'does not match') + + assert_equal /x#{1+1}x/, /x#{1+1}x/ + s = "OK" + assert_equal /x#{s}x/, /x#{s}x/ + assert_equal true, !!(/x#{s}x/ =~ "xOKx") + assert_equal false, !!(/x#{s}x/ =~ "does not match") + + s = "OK" + prev = nil + 3.times do + assert_equal prev.object_id, (prev ||= /#{s}/o).object_id if prev + end + end + + def test_array + assert_equal [], [] + assert_equal 0, [].size + assert_equal [1, 2, 3], [1, 2, 3] + assert_equal [3, 7, 11], [1+2, 3+4, 5+6] + assert_equal [[1], [2], [3]], [[1], [2], [3]] + + a = [1, 2, 3] + assert_equal 1, a[0] + assert_equal 2, a[1] + assert_equal 3, a[2] + assert_nil a[3] + + a = %w( a b c ) + assert_equal 'a', a[0] + assert_equal 'b', a[1] + assert_equal 'c', a[2] + assert_nil a[3] + end + + def test_hash + assert_equal({}, {}) + assert_equal({1=>2}, {1=>2}) + assert_equal({1=>2, 3=>4}, {1=>2, 3=>4}) + assert_equal({1=>2, 3=>4}, {3=>4, 1=>2}) + assert_equal({1=>2, 3=>4}, {1,2, 3,4}) + assert_equal({"key"=>"val"}, {"key"=>"val"}) + end + + def test_range + assert_equal((1..3), (1..3)) + assert_equal((1...3), (1...3)) + assert_not_equal((1...3), (1..3)) + assert_not_equal((1..3), (1...3)) + assert_equal((1..3), (1..2+1)) + assert_equal((1...3), (1...2+1)) + assert_equal(('a'..'z'), ('a'..'z')) + end + + def test_not + assert_equal true, !nil + assert_equal true, !false + assert_equal false, !true + assert_equal false, !3 + assert_equal false, !(1+1) + + assert_equal false, !!nil + assert_equal false, !!false + assert_equal true, !!true + assert_equal true, !!3 + assert_equal true, !!(1+1) + + assert_equal true, (not nil) + assert_equal true, (not false) + assert_equal false, (not true) + assert_equal false, (not 3) + assert_equal false, (not (1 + 1)) + + assert_equal false, (not not nil) + assert_equal false, (not not false) + assert_equal true, (not not true) + assert_equal true, (not not 3) + assert_equal true, (not not (1+1)) + end + + def test_local_variable + a = 7 + assert_equal 7, a + assert_equal a, a + b = 17 + assert_equal 7, a + assert_equal 17, b + assert_equal a, a + assert_equal b, b + assert_not_equal a, b + assert_not_equal b, a + a = b + assert_equal 17, a + assert_equal 17, b + assert_equal a, a + assert_equal b, b + assert_equal a, b + assert_equal b, a + c = 28 + assert_equal 17, a + assert_equal 17, b + assert_equal 28, c + assert_equal a, a + assert_equal b, b + assert_equal a, b + assert_equal c, c + assert_not_equal a, c + assert_not_equal b, c + a = b = c + assert_equal 28, a + assert_equal 28, b + assert_equal 28, c + assert_equal a, a + assert_equal b, b + assert_equal a, b + assert_equal b, a + assert_equal a, c + assert_equal c, a + assert_equal b, c + assert_equal c, b + + a = 1 + b = 2 + c = 3 + set_lvar_in_another_method + assert_equal 1, a + assert_equal 2, b + assert_equal 3, c + end + + def set_lvar_in_another_method + assert_raise(NameError) { a } + assert_raise(NameError) { b } + assert_raise(NameError) { c } + a = "NOT OK" + b = "NOT OK" + c = "NOT OK" + end + + class Const + $Const = self + C = 'Const::C' + def self.c() C end + def c() C end + + class A + C = 'Const::A::C' + def self.c() C end + def c() C end + CC = 'Const::A::CC' + def self.cc() CC end + def cc() CC end + + class B + C = 'Const::A::B::C' + def self.c() C end + def c() C end + def self.cc() CC end + def cc() CC end + end + end + + class AA < A + def self.cc() CC end + def cc() CC end + end + + class AAA < AA + def self.cc() CC end + def cc() CC end + end + end + C = 0 + + def test_const_path + do_test_const_path + do_test_const_path + do_test_const_path + end + + def do_test_const_path + assert_equal 0, C + assert_equal 0, C + assert_equal 3, ::ConstTest + assert_equal 3, ::ConstTest + assert_equal $Const, Const + + assert_equal 'Const::C', Const::C + assert_equal 'Const::C', Const::C + assert_equal 'Const::A::C', Const::A::C + assert_equal 'Const::A::C', Const::A::C + assert_equal 'Const::A::B::C', Const::A::B::C + assert_equal 'Const::A::B::C', Const::A::B::C + + Const::A::B._remove_const :C + assert_equal 'Const::C', Const::C + assert_equal 'Const::A::C', Const::A::C + assert_raise(NameError) { Const::A::B::C } + + Const::A._remove_const :C + assert_equal 'Const::C', Const::C + assert_raise(NameError) { Const::A::C } + assert_raise(NameError) { Const::A::B::C } + + Const._remove_const :C + assert_raise(NameError) { Const::C } + assert_raise(NameError) { Const::A::C } + assert_raise(NameError) { Const::A::B::C } + + Const::A.const_set :C, 'Const::A::C' + assert_raise(NameError) { Const::C } + assert_equal 'Const::A::C', Const::A::C + assert_raise(NameError) { Const::A::B::C } + + Const::A::B.const_set :C, 'Const::A::B::C' + assert_raise(NameError) { Const::C } + assert_equal 'Const::A::C', Const::A::C + assert_equal 'Const::A::B::C', Const::A::B::C + + Const.const_set :C, 'Const::C' + assert_equal 'Const::C', Const::C + assert_equal 'Const::A::C', Const::A::C + assert_equal 'Const::A::B::C', Const::A::B::C + end + + def test_const_cref + do_test_const_cref + do_test_const_cref + do_test_const_cref + end + + def do_test_const_cref + assert_equal 'Const::C', Const.new.c + assert_equal 'Const::A::C', Const::A.new.c + assert_equal 'Const::A::B::C', Const::A::B.new.c + + assert_equal 'Const::C', Const.c + assert_equal 'Const::A::C', Const::A.c + assert_equal 'Const::A::B::C', Const::A::B.c + + Const::A::B._remove_const :C + assert_equal 'Const::C', Const.c + assert_equal 'Const::A::C', Const::A.c + assert_equal 'Const::A::C', Const::A::B.c + assert_equal 'Const::C', Const.new.c + assert_equal 'Const::A::C', Const::A.new.c + assert_equal 'Const::A::C', Const::A::B.new.c + + Const::A._remove_const :C + assert_equal 'Const::C', Const.c + assert_equal 'Const::C', Const::A.c + assert_equal 'Const::C', Const::A::B.c + assert_equal 'Const::C', Const.new.c + assert_equal 'Const::C', Const::A.new.c + assert_equal 'Const::C', Const::A::B.new.c + + Const::A::B.const_set :C, 'Const::A::B::C' + assert_equal 'Const::C', Const.c + assert_equal 'Const::C', Const::A.c + assert_equal 'Const::A::B::C', Const::A::B.c + assert_equal 'Const::C', Const.new.c + assert_equal 'Const::C', Const::A.new.c + assert_equal 'Const::A::B::C', Const::A::B.new.c + + Const::A.const_set :C, 'Const::A::C' + assert_equal 'Const::C', Const.c + assert_equal 'Const::A::C', Const::A.c + assert_equal 'Const::A::B::C', Const::A::B.c + assert_equal 'Const::C', Const.new.c + assert_equal 'Const::A::C', Const::A.new.c + assert_equal 'Const::A::B::C', Const::A::B.new.c + ensure + # reset + Const.const_set :C, 'Const::C' unless Const.const_defined?(:C) + Const::A.const_set :C, 'Const::A::C' unless Const::A.const_defined?(:C) + Const::A::B.const_set :C, 'Const::A::B::C' unless Const::A::B.const_defined?(:C) + end + + def test_const_inherit + do_test_const_inherit + do_test_const_inherit + do_test_const_inherit + end + + def do_test_const_inherit + assert_equal 'Const::A::CC', Const::A.cc + assert_equal 'Const::A::CC', Const::AA.cc + assert_equal 'Const::A::CC', Const::AAA.cc + assert_equal 'Const::A::CC', Const::A.new.cc + assert_equal 'Const::A::CC', Const::AA.new.cc + assert_equal 'Const::A::CC', Const::AAA.new.cc + + Const::AA.const_set :CC, 'Const::AA::CC' + assert_equal 'Const::A::CC', Const::A.cc + assert_equal 'Const::AA::CC', Const::AA.cc + assert_equal 'Const::AA::CC', Const::AAA.cc + assert_equal 'Const::A::CC', Const::A.new.cc + assert_equal 'Const::AA::CC', Const::AA.new.cc + assert_equal 'Const::AA::CC', Const::AAA.new.cc + + Const::AAA.const_set :CC, 'Const::AAA::CC' + assert_equal 'Const::A::CC', Const::A.cc + assert_equal 'Const::AA::CC', Const::AA.cc + assert_equal 'Const::AAA::CC', Const::AAA.cc + assert_equal 'Const::A::CC', Const::A.new.cc + assert_equal 'Const::AA::CC', Const::AA.new.cc + assert_equal 'Const::AAA::CC', Const::AAA.new.cc + + Const::AA._remove_const :CC + assert_equal 'Const::A::CC', Const::A.cc + assert_equal 'Const::A::CC', Const::AA.cc + assert_equal 'Const::AAA::CC', Const::AAA.cc + assert_equal 'Const::A::CC', Const::A.new.cc + assert_equal 'Const::A::CC', Const::AA.new.cc + assert_equal 'Const::AAA::CC', Const::AAA.new.cc + + Const::AAA._remove_const :CC + assert_equal 'Const::A::CC', Const::A.cc + assert_equal 'Const::A::CC', Const::AA.cc + assert_equal 'Const::A::CC', Const::AAA.cc + assert_equal 'Const::A::CC', Const::A.new.cc + assert_equal 'Const::A::CC', Const::AA.new.cc + assert_equal 'Const::A::CC', Const::AAA.new.cc + end + + def test_global_variable + $gvar1 = 1 + assert_equal 1, $gvar1 + $gvar1 = 2 + assert_equal 2, $gvar1 + $gvar2 = 77 + assert_equal 2, $gvar1 + assert_equal 77, $gvar2 + $gvar2 = $gvar1 + assert_equal 2, $gvar1 + assert_equal 2, $gvar2 + $gvar1 = 1 + assert_equal 1, $gvar1 + assert_equal 2, $gvar2 + set_gvar_in_another_method + assert_equal "OK1", $gvar1 + assert_equal "OK2", $gvar2 + end + + def set_gvar_in_another_method + assert_equal 1, $gvar1 + assert_equal 2, $gvar2 + $gvar1 = "OK1" + $gvar2 = "OK2" + end + + class CVarA + @@cv = 'CVarA@@cv' + def self.cv() @@cv end + def self.cv=(v) @@cv = v end + class << self + def cv2() @@cv end + end + def cv() @@cv end + def cv=(v) @@cv = v end + end + + class CVarB < CVarA + def self.cvB() @@cv end + def self.cvB=(v) @@cv = v end + class << self + def cvB2() @@cv end + end + def cvB() @@cv end + def cvB=(v) @@cv = v end + end + + def test_class_variable + assert_equal 'CVarA@@cv', CVarA.cv + assert_equal 'CVarA@@cv', CVarA.cv2 + assert_equal 'CVarA@@cv', CVarA.new.cv + CVarA.cv = 'singleton' + assert_equal 'singleton', CVarA.cv + assert_equal 'singleton', CVarA.cv2 + assert_equal 'singleton', CVarA.new.cv + CVarA.new.cv = 'instance' + assert_equal 'instance', CVarA.cv + assert_equal 'instance', CVarA.cv2 + assert_equal 'instance', CVarA.new.cv + + CVarA.cv = 'CVarA@@cv' + CVarB.cv = 'B/singleton' + assert_equal 'B/singleton', CVarB.cv + assert_equal 'B/singleton', CVarB.cv2 + assert_equal 'B/singleton', CVarB.new.cv + assert_equal 'B/singleton', CVarA.cv + assert_equal 'B/singleton', CVarA.cv2 + assert_equal 'B/singleton', CVarA.new.cv + CVarB.new.cv = 'B/instance' + assert_equal 'B/instance', CVarB.cv + assert_equal 'B/instance', CVarB.cv2 + assert_equal 'B/instance', CVarB.new.cv + assert_equal 'B/instance', CVarA.cv + assert_equal 'B/instance', CVarA.cv2 + assert_equal 'B/instance', CVarA.new.cv + + CVarA.cv = 'CVarA@@cv' + assert_raise(NameError) { CVarB.cvB } + assert_raise(NameError) { CVarB.cvB2 } + assert_raise(NameError) { CVarB.new.cvB } + CVarB.cvB = 'B/singleton' + assert_equal 'B/singleton', CVarB.cvB + assert_equal 'B/singleton', CVarB.cvB2 + assert_equal 'B/singleton', CVarB.new.cvB + assert_equal 'CVarA@@cv', CVarA.cv + assert_equal 'CVarA@@cv', CVarA.cv2 + assert_equal 'CVarA@@cv', CVarA.new.cv + CVarB.new.cvB = 'B/instance' + assert_equal 'B/instance', CVarB.cvB + assert_equal 'B/instance', CVarB.cvB2 + assert_equal 'B/instance', CVarB.new.cvB + assert_equal 'CVarA@@cv', CVarA.cv + assert_equal 'CVarA@@cv', CVarA.cv2 + assert_equal 'CVarA@@cv', CVarA.new.cv + + CVarA.cv = 'CVarA@@cv' + CVarB.cvB = 'CVarB@@cv' + end + + class OP + attr_accessor :x + end + + def test_opassign + x = nil + x ||= 1 + assert_equal 1, x + x &&= 2 + assert_equal 2, x + x ||= 3 + assert_equal 2, x + x &&= 4 + assert_equal 4, x + + y = OP.new + y.x = nil + y.x ||= 1 + assert_equal 1, y.x + y.x &&= 2 + assert_equal 2, y.x + y.x ||= 3 + assert_equal 2, y.x + y.x &&= 4 + assert_equal 4, y.x + + z = OP.new + z.x = y + z.x.x = nil + z.x.x ||= 1 + assert_equal 1, z.x.x + z.x.x &&= 2 + assert_equal 2, z.x.x + z.x.x ||= 3 + assert_equal 2, z.x.x + z.x.x &&= 4 + assert_equal 4, z.x.x + + a = [] + a[0] = nil + a[0] ||= 1 + assert_equal 1, a[0] + a[0] &&= 2 + assert_equal 2, a[0] + a[0] ||= 3 + assert_equal 2, a[0] + a[0] &&= 4 + assert_equal 4, a[0] + end + + def test_backref + /re/ =~ 'not match' + assert_nil $~ + assert_nil $` + assert_nil $& + assert_nil $' + assert_nil $+ + assert_nil $1 + assert_nil $2 + assert_nil $3 + assert_nil $4 + assert_nil $5 + assert_nil $6 + assert_nil $7 + assert_nil $8 + assert_nil $9 + + /(a)(b)(c)(d)(e)(f)(g)(h)(i)/ =~ 'xabcdefghiy' + assert_not_nil $~ + assert_instance_of MatchData, $~ + assert_equal 'abcdefghi', $~[0] + assert_equal 'a', $~[1] + assert_equal 'b', $~[2] + assert_equal 'c', $~[3] + assert_equal 'd', $~[4] + assert_equal 'e', $~[5] + assert_equal 'f', $~[6] + assert_equal 'g', $~[7] + assert_equal 'h', $~[8] + assert_equal 'i', $~[9] + assert_equal 'x', $` + assert_equal 'abcdefghi', $& + assert_equal 'y', $' + assert_equal 'i', $+ + assert_equal 'a', $1 + assert_equal 'b', $2 + assert_equal 'c', $3 + assert_equal 'd', $4 + assert_equal 'e', $5 + assert_equal 'f', $6 + assert_equal 'g', $7 + assert_equal 'h', $8 + assert_equal 'i', $9 + + /re/ =~ 'not match' + assert_nil $~ + assert_nil $` + assert_nil $& + assert_nil $' + assert_nil $+ + assert_nil $1 + assert_nil $2 + assert_nil $3 + assert_nil $4 + assert_nil $5 + assert_nil $6 + assert_nil $7 + assert_nil $8 + assert_nil $9 + end + + def test_array_splat + a = [] + assert_equal [], [*a] + assert_equal [1], [1, *a] + a = [2] + assert_equal [2], [*a] + assert_equal [1, 2], [1, *a] + a = [2, 3] + assert_equal [2, 3], [*a] + assert_equal [1, 2, 3], [1, *a] + + a = nil + assert_equal [nil], [*a] # FIXME: []? [nil]? error? + assert_equal [1], [1, *a] # FIXME: [1, nil]? error? + end + +end diff --git a/test/ruby/test_beginendblock.rb b/test/ruby/test_beginendblock.rb index a8e5913b10..82b8880894 100644 --- a/test/ruby/test_beginendblock.rb +++ b/test/ruby/test_beginendblock.rb @@ -12,9 +12,9 @@ class TestBeginEndBlock < Test::Unit::TestCase def test_beginendblock ruby = EnvUtil.rubybin target = File.join(DIR, 'beginmainend.rb') - io = IO.popen("#{q(ruby)} #{q(target)}") - assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e4 e3 e2 e4-2 e4-1 e1-1 e4-1-1), io.read.split) - io.close + IO.popen("#{q(ruby)} #{q(target)}"){|io| + assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e4 e3 e2 e4-2 e4-1 e1-1 e4-1-1), io.read.split) + } end def test_begininmethod @@ -46,11 +46,16 @@ EOF errout.close erroutpath = errout.path system("#{q(ruby)} #{q(launcherpath)} #{q(erroutpath)}") +# expected = <<EOW +#endblockwarn.rb:2: warning: END in method; use at_exit +#(eval):2: warning: END in method; use at_exit +#EOW expected = <<EOW -endblockwarn.rb:2: warning: END in method; use at_exit -(eval):2: warning: END in method; use at_exit +warning: END in method; use at_exit +warning: END in method; use at_exit EOW assert_equal(expected, File.read(erroutpath)) # expecting Tempfile to unlink launcher and errout file. end + end diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb new file mode 100644 index 0000000000..fa8a40f39b --- /dev/null +++ b/test/ruby/test_class.rb @@ -0,0 +1,80 @@ +require 'test/unit' + +class TestClass < Test::Unit::TestCase + + # ------------------ + # Various test classes + # ------------------ + + class ClassOne + attr :num_args + @@subs = [] + def initialize(*args) + @num_args = args.size + @args = args + end + def [](n) + @args[n] + end + def ClassOne.inherited(klass) + @@subs.push klass + end + def subs + @@subs + end + end + + class ClassTwo < ClassOne + end + + class ClassThree < ClassOne + end + + class ClassFour < ClassThree + end + + # ------------------ + # Start of tests + # ------------------ + + def test_s_inherited + assert_equal([ClassTwo, ClassThree, ClassFour], ClassOne.new.subs) + end + + def test_s_new + c = Class.new + assert_same(Class, c.class) + assert_same(Object, c.superclass) + + c = Class.new(Fixnum) + assert_same(Class, c.class) + assert_same(Fixnum, c.superclass) + end + + def test_00_new_basic + a = ClassOne.new + assert_equal(ClassOne, a.class) + assert_equal(0, a.num_args) + + a = ClassOne.new(1, 2, 3) + assert_equal(3, a.num_args) + assert_equal(1, a[0]) + end + + def test_01_new_inherited + a = ClassTwo.new + assert_equal(ClassTwo, a.class) + assert_equal(0, a.num_args) + + a = ClassTwo.new(1, 2, 3) + assert_equal(3, a.num_args) + assert_equal(1, a[0]) + end + + def test_superclass + assert_equal(ClassOne, ClassTwo.superclass) + assert_equal(Object, ClassTwo.superclass.superclass) + assert_equal(BasicObject, ClassTwo.superclass.superclass.superclass) + end + +end diff --git a/test/ruby/test_clone.rb b/test/ruby/test_clone.rb index 43c0cffa1d..67d79fb8a5 100644 --- a/test/ruby/test_clone.rb +++ b/test/ruby/test_clone.rb @@ -23,6 +23,6 @@ class TestClone < Test::Unit::TestCase assert_raises(NoMethodError) {foo.test2} - assert_equal([M003, M002, M001], M003.ancestors) + assert_equal([M003, M002, M001, M002], M003.ancestors) end end diff --git a/test/ruby/test_const.rb b/test/ruby/test_const.rb index 8d01379dbd..3708a5a0ca 100644 --- a/test/ruby/test_const.rb +++ b/test/ruby/test_const.rb @@ -15,19 +15,34 @@ class TestConst < Test::Unit::TestCase end def test_const + assert defined?(TEST1) + assert_equal 1, TEST1 + assert defined?(TEST2) + assert_equal 2, TEST2 + self.class.class_eval { include Const } - assert_equal([1,2,3,4], [TEST1,TEST2,TEST3,TEST4]) + assert defined?(TEST1) + assert_equal 1, TEST1 + assert defined?(TEST2) + assert_equal 2, TEST2 + assert defined?(TEST3) + assert_equal 3, TEST3 + assert defined?(TEST4) + assert_equal 4, TEST4 self.class.class_eval { include Const2 } STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE - assert_equal([1,2,6,8], [TEST1,TEST2,TEST3,TEST4]) - - assert_equal(-1, (String <=> Object)) - assert_equal(1, (Object <=> String)) - assert_equal(nil, (Array <=> String)) + assert defined?(TEST1) + assert_equal 1, TEST1 + assert defined?(TEST2) + assert_equal 2, TEST2 + assert defined?(TEST3) + assert_equal 6, TEST3 + assert defined?(TEST4) + assert_equal 8, TEST4 end end diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 7887d37670..c419bd8d4e 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -39,4 +39,14 @@ class TestDir < Test::Unit::TestCase dir.close end end + + def test_JVN_13947696 + b = lambda { + d = Dir.open('.') + $SAFE = 4 + d.close + } + assert_raise(SecurityError) { b.call } + end + end diff --git a/test/ruby/test_eval.rb b/test/ruby/test_eval.rb index 28e5cd2c25..e79d1c82ec 100644 --- a/test/ruby/test_eval.rb +++ b/test/ruby/test_eval.rb @@ -1,7 +1,216 @@ require 'test/unit' class TestEval < Test::Unit::TestCase - # eval with binding + + @ivar = 12 + @@cvar = 13 + $gvar__eval = 14 + Const = 15 + + def test_eval_basic + assert_equal nil, eval("nil") + assert_equal true, eval("true") + assert_equal false, eval("false") + assert_equal self, eval("self") + assert_equal 1, eval("1") + assert_equal :sym, eval(":sym") + + assert_equal 11, eval("11") + @ivar = 12 + assert_equal 12, eval("@ivar") + assert_equal 13, eval("@@cvar") + assert_equal 14, eval("$gvar__eval") + assert_equal 15, eval("Const") + + assert_equal 16, eval("7 + 9") + assert_equal 17, eval("17.to_i") + assert_equal "18", eval(%q("18")) + assert_equal "19", eval(%q("1#{9}")) + + 1.times { + assert_equal 12, eval("@ivar") + assert_equal 13, eval("@@cvar") + assert_equal 14, eval("$gvar__eval") + assert_equal 15, eval("Const") + } + end + + def test_eval_binding_basic + assert_equal nil, eval("nil", binding()) + assert_equal true, eval("true", binding()) + assert_equal false, eval("false", binding()) + assert_equal self, eval("self", binding()) + assert_equal 1, eval("1", binding()) + assert_equal :sym, eval(":sym", binding()) + + assert_equal 11, eval("11", binding()) + @ivar = 12 + assert_equal 12, eval("@ivar", binding()) + assert_equal 13, eval("@@cvar", binding()) + assert_equal 14, eval("$gvar__eval", binding()) + assert_equal 15, eval("Const", binding()) + + assert_equal 16, eval("7 + 9", binding()) + assert_equal 17, eval("17.to_i", binding()) + assert_equal "18", eval(%q("18"), binding()) + assert_equal "19", eval(%q("1#{9}"), binding()) + + 1.times { + assert_equal 12, eval("@ivar") + assert_equal 13, eval("@@cvar") + assert_equal 14, eval("$gvar__eval") + assert_equal 15, eval("Const") + } + end + + def test_module_eval_string_basic + c = self.class + assert_equal nil, c.module_eval("nil") + assert_equal true, c.module_eval("true") + assert_equal false, c.module_eval("false") + assert_equal c, c.module_eval("self") + assert_equal :sym, c.module_eval(":sym") + assert_equal 11, c.module_eval("11") + @ivar = 12 + assert_equal 12, c.module_eval("@ivar") + assert_equal 13, c.module_eval("@@cvar") + assert_equal 14, c.module_eval("$gvar__eval") + assert_equal 15, c.module_eval("Const") + assert_equal 16, c.module_eval("7 + 9") + assert_equal 17, c.module_eval("17.to_i") + assert_equal "18", c.module_eval(%q("18")) + assert_equal "19", c.module_eval(%q("1#{9}")) + + @ivar = 12 + 1.times { + assert_equal 12, c.module_eval("@ivar") + assert_equal 13, c.module_eval("@@cvar") + assert_equal 14, c.module_eval("$gvar__eval") + assert_equal 15, c.module_eval("Const") + } + end + + def test_module_eval_block_basic + c = self.class + assert_equal nil, c.module_eval { nil } + assert_equal true, c.module_eval { true } + assert_equal false, c.module_eval { false } + assert_equal c, c.module_eval { self } + assert_equal :sym, c.module_eval { :sym } + assert_equal 11, c.module_eval { 11 } + @ivar = 12 + assert_equal 12, c.module_eval { @ivar } + assert_equal 13, c.module_eval { @@cvar } + assert_equal 14, c.module_eval { $gvar__eval } + assert_equal 15, c.module_eval { Const } + assert_equal 16, c.module_eval { 7 + 9 } + assert_equal 17, c.module_eval { "17".to_i } + assert_equal "18", c.module_eval { "18" } + assert_equal "19", c.module_eval { "1#{9}" } + + @ivar = 12 + 1.times { + assert_equal 12, c.module_eval { @ivar } + assert_equal 13, c.module_eval { @@cvar } + assert_equal 14, c.module_eval { $gvar__eval } + assert_equal 15, c.module_eval { Const } + } + end + + def forall_TYPE(mid) + objects = [Object.new, [], nil, true, false, 77, ] #:sym] # TODO: check + objects.each do |obj| + obj.instance_variable_set :@ivar, 12 + obj.class.class_variable_set :@@cvar, 13 + # Use same value with env. See also test_instance_variable_cvar. + obj.class.const_set :Const, 15 unless obj.class.const_defined?(:Const) + funcall mid, obj + end + end + + def test_instance_eval_string_basic + forall_TYPE :instance_eval_string_basic_i + end + + def instance_eval_string_basic_i(o) + assert_equal nil, o.instance_eval("nil") + assert_equal true, o.instance_eval("true") + assert_equal false, o.instance_eval("false") + assert_equal o, o.instance_eval("self") + assert_equal 1, o.instance_eval("1") + assert_equal :sym, o.instance_eval(":sym") + + assert_equal 11, o.instance_eval("11") + assert_equal 12, o.instance_eval("@ivar") + begin + assert_equal 13, o.instance_eval("@@cvar") + rescue => err + assert false, "cannot get cvar from #{o.class}" + end + assert_equal 14, o.instance_eval("$gvar__eval") + assert_equal 15, o.instance_eval("Const") + assert_equal 16, o.instance_eval("7 + 9") + assert_equal 17, o.instance_eval("17.to_i") + assert_equal "18", o.instance_eval(%q("18")) + assert_equal "19", o.instance_eval(%q("1#{9}")) + + 1.times { + assert_equal 12, o.instance_eval("@ivar") + assert_equal 13, o.instance_eval("@@cvar") + assert_equal 14, o.instance_eval("$gvar__eval") + assert_equal 15, o.instance_eval("Const") + } + end + + def test_instance_eval_block_basic + forall_TYPE :instance_eval_block_basic_i + end + + def instance_eval_block_basic_i(o) + assert_equal nil, o.instance_eval { nil } + assert_equal true, o.instance_eval { true } + assert_equal false, o.instance_eval { false } + assert_equal o, o.instance_eval { self } + assert_equal 1, o.instance_eval { 1 } + assert_equal :sym, o.instance_eval { :sym } + + assert_equal 11, o.instance_eval { 11 } + assert_equal 12, o.instance_eval { @ivar } + assert_equal 13, o.instance_eval { @@cvar } + assert_equal 14, o.instance_eval { $gvar__eval } + assert_equal 15, o.instance_eval { Const } + assert_equal 16, o.instance_eval { 7 + 9 } + assert_equal 17, o.instance_eval { 17.to_i } + assert_equal "18", o.instance_eval { "18" } + assert_equal "19", o.instance_eval { "1#{9}" } + + 1.times { + assert_equal 12, o.instance_eval { @ivar } + assert_equal 13, o.instance_eval { @@cvar } + assert_equal 14, o.instance_eval { $gvar__eval } + assert_equal 15, o.instance_eval { Const } + } + end + + def test_instance_eval_cvar + env = @@cvar + cls = "class" + [Object.new, [], 7, ].each do |obj| # TODO: check :sym + obj.class.class_variable_set :@@cvar, cls + assert_equal env, obj.instance_eval("@@cvar") + assert_equal env, obj.instance_eval { @@cvar } + end + [true, false, nil].each do |obj| + obj.class.class_variable_set :@@cvar, cls + assert_equal cls, obj.instance_eval("@@cvar") + assert_equal cls, obj.instance_eval { @@cvar } + end + end + + # + # From ruby/test/ruby/test_eval.rb + # + def test_ev local1 = "local1" lambda { @@ -10,7 +219,7 @@ class TestEval < Test::Unit::TestCase }.call end - def test_eval + def test_eval_orig assert_nil(eval("")) $bad=false eval 'while false; $bad = true; print "foo\n" end' @@ -66,16 +275,19 @@ class TestEval < Test::Unit::TestCase end assert(!$bad) - x = proc{} - eval "i4 = 1", x - assert_equal(1, eval("i4", x)) - x = proc{proc{}}.call - eval "i4 = 22", x - assert_equal(22, eval("i4", x)) - $x = [] - x = proc{proc{}}.call - eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x - assert_equal(8, $x[4].call) + if false + # Ruby 2.0 doesn't see Proc as Binding + x = proc{} + eval "i4 = 1", x + assert_equal(1, eval("i4", x)) + x = proc{proc{}}.call + eval "i4 = 22", x + assert_equal(22, eval("i4", x)) + $x = [] + x = proc{proc{}}.call + eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x + assert_equal(8, $x[4].call) + end x = binding eval "i = 1", x @@ -98,26 +310,32 @@ class TestEval < Test::Unit::TestCase foo22 = 5 proc{foo11=22}.call proc{foo22=55}.call - assert_equal(eval("foo11"), eval("foo11", p)) - assert_equal(1, eval("foo11")) + # assert_equal(eval("foo11"), eval("foo11", p)) + # assert_equal(1, eval("foo11")) assert_equal(eval("foo22"), eval("foo22", p)) assert_equal(55, eval("foo22")) }.call - p1 = proc{i7 = 0; proc{i7}}.call - assert_equal(0, p1.call) - eval "i7=5", p1 - assert_equal(5, p1.call) - assert(!defined?(i7)) + if false + # Ruby 2.0 doesn't see Proc as Binding + p1 = proc{i7 = 0; proc{i7}}.call + assert_equal(0, p1.call) + eval "i7=5", p1 + assert_equal(5, p1.call) + assert(!defined?(i7)) + end - p1 = proc{i7 = 0; proc{i7}}.call - i7 = nil - assert_equal(0, p1.call) - eval "i7=1", p1 - assert_equal(1, p1.call) - eval "i7=5", p1 - assert_equal(5, p1.call) - assert_nil(i7) + if false + # Ruby 2.0 doesn't see Proc as Binding + p1 = proc{i7 = 0; proc{i7}}.call + i7 = nil + assert_equal(0, p1.call) + eval "i7=1", p1 + assert_equal(1, p1.call) + eval "i7=5", p1 + assert_equal(5, p1.call) + assert_nil(i7) + end end def test_nil_instance_eval_cvar # [ruby-dev:24103] @@ -154,4 +372,5 @@ class TestEval < Test::Unit::TestCase v.call } end + end diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index d0b4e3df77..72c38e4b27 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -8,6 +8,9 @@ class TestGc < Test::Unit::TestCase end def test_gc + prev_stress = GC.stress + GC.stress = false + assert_nothing_raised do 1.upto(10000) { tmp = [0,1,2,3,4,5,6,7,8,9] @@ -26,5 +29,7 @@ class TestGc < Test::Unit::TestCase } GC.start assert true # reach here or dumps core + + GC.stress = prev_stress end end diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 5bec012bf8..35136da57c 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1,6 +1,7 @@ require 'test/unit' class TestHash < Test::Unit::TestCase + def test_hash x = {1=>2, 2=>4, 3=>6} y = {1, 2, 2, 4, 3, 6} @@ -71,4 +72,565 @@ class TestHash < Test::Unit::TestCase assert_equal(44, x[22]) assert_equal(0, $z) end + + # From rubicon + + def setup + @cls = Hash + @h = @cls[ + 1 => 'one', 2 => 'two', 3 => 'three', + self => 'self', true => 'true', nil => 'nil', + 'nil' => nil + ] + end + + def test_s_AREF + h = @cls["a" => 100, "b" => 200] + assert_equal(100, h['a']) + assert_equal(200, h['b']) + assert_nil(h['c']) + + h = @cls.[]("a" => 100, "b" => 200) + assert_equal(100, h['a']) + assert_equal(200, h['b']) + assert_nil(h['c']) + end + + def test_s_new + h = @cls.new + assert_instance_of(@cls, h) + assert_nil(h.default) + assert_nil(h['spurious']) + + h = @cls.new('default') + assert_instance_of(@cls, h) + assert_equal('default', h.default) + assert_equal('default', h['spurious']) + + end + + def test_AREF # '[]' + t = Time.now + h = @cls[ + 1 => 'one', 2 => 'two', 3 => 'three', + self => 'self', t => 'time', nil => 'nil', + 'nil' => nil + ] + + assert_equal('one', h[1]) + assert_equal('two', h[2]) + assert_equal('three', h[3]) + assert_equal('self', h[self]) + assert_equal('time', h[t]) + assert_equal('nil', h[nil]) + assert_equal(nil, h['nil']) + assert_equal(nil, h['koala']) + + h1 = h.dup + h1.default = :default + + assert_equal('one', h1[1]) + assert_equal('two', h1[2]) + assert_equal('three', h1[3]) + assert_equal('self', h1[self]) + assert_equal('time', h1[t]) + assert_equal('nil', h1[nil]) + assert_equal(nil, h1['nil']) + assert_equal(:default, h1['koala']) + + + end + + def test_ASET # '[]=' + t = Time.now + h = @cls.new + h[1] = 'one' + h[2] = 'two' + h[3] = 'three' + h[self] = 'self' + h[t] = 'time' + h[nil] = 'nil' + h['nil'] = nil + assert_equal('one', h[1]) + assert_equal('two', h[2]) + assert_equal('three', h[3]) + assert_equal('self', h[self]) + assert_equal('time', h[t]) + assert_equal('nil', h[nil]) + assert_equal(nil, h['nil']) + assert_equal(nil, h['koala']) + + h[1] = 1 + h[nil] = 99 + h['nil'] = nil + z = [1,2] + h[z] = 256 + assert_equal(1, h[1]) + assert_equal('two', h[2]) + assert_equal('three', h[3]) + assert_equal('self', h[self]) + assert_equal('time', h[t]) + assert_equal(99, h[nil]) + assert_equal(nil, h['nil']) + assert_equal(nil, h['koala']) + assert_equal(256, h[z]) + end + + def test_EQUAL # '==' + h1 = @cls[ "a" => 1, "c" => 2 ] + h2 = @cls[ "a" => 1, "c" => 2, 7 => 35 ] + h3 = @cls[ "a" => 1, "c" => 2, 7 => 35 ] + h4 = @cls[ ] + assert(h1 == h1) + assert(h2 == h2) + assert(h3 == h3) + assert(h4 == h4) + assert(!(h1 == h2)) + assert(h2 == h3) + assert(!(h3 == h4)) + end + + def test_clear + assert(@h.size > 0) + @h.clear + assert_equal(0, @h.size) + assert_nil(@h[1]) + end + + def test_clone + for taint in [ false, true ] + for frozen in [ false, true ] + a = @h.clone + a.taint if taint + a.freeze if frozen + b = a.clone + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert_equal(a.frozen?, b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_default + assert_nil(@h.default) + h = @cls.new(:xyzzy) + assert_equal(:xyzzy, h.default) + end + + def test_default= + assert_nil(@h.default) + @h.default = :xyzzy + assert_equal(:xyzzy, @h.default) + end + + def test_delete + h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true' ] + h2 = @cls[ 1 => 'one', 2 => 'two' ] + h3 = @cls[ 2 => 'two' ] + + assert_equal('true', h1.delete(true)) + assert_equal(h2, h1) + + assert_equal('one', h1.delete(1)) + assert_equal(h3, h1) + + assert_equal('two', h1.delete(2)) + assert_equal(@cls[], h1) + + assert_nil(h1.delete(99)) + assert_equal(@cls[], h1) + + assert_equal('default 99', h1.delete(99) {|i| "default #{i}" }) + end + + def test_delete_if + base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ] + h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ] + h2 = @cls[ 2 => false, 'cat' => 99 ] + h3 = @cls[ 2 => false ] + + h = base.dup + assert_equal(h, h.delete_if { false }) + assert_equal(@cls[], h.delete_if { true }) + + h = base.dup + assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) }) + assert_equal(h1, h) + + h = base.dup + assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) }) + assert_equal(h2, h) + + h = base.dup + assert_equal(h3, h.delete_if {|k,v| v }) + assert_equal(h3, h) + end + + def test_dup + for taint in [ false, true ] + for frozen in [ false, true ] + a = @h.dup + a.taint if taint + a.freeze if frozen + b = a.dup + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert_equal(false, b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_each + count = 0 + @cls[].each { |k, v| count + 1 } + assert_equal(0, count) + + h = @h + h.each do |k, v| + assert_equal(v, h.delete(k)) + end + assert_equal(@cls[], h) + end + + def test_each_key + count = 0 + @cls[].each_key { |k| count + 1 } + assert_equal(0, count) + + h = @h + h.each_key do |k| + h.delete(k) + end + assert_equal(@cls[], h) + end + + def test_each_pair + count = 0 + @cls[].each_pair { |k, v| count + 1 } + assert_equal(0, count) + + h = @h + h.each_pair do |k, v| + assert_equal(v, h.delete(k)) + end + assert_equal(@cls[], h) + end + + def test_each_value + res = [] + @cls[].each_value { |v| res << v } + assert_equal(0, [].length) + + @h.each_value { |v| res << v } + assert_equal(0, [].length) + + expected = [] + @h.each { |k, v| expected << v } + + assert_equal([], expected - res) + assert_equal([], res - expected) + end + + def test_empty? + assert(@cls[].empty?) + assert(!@h.empty?) + end + + def test_fetch + assert_raise(KeyError) { @cls[].fetch(1) } + assert_raise(KeyError) { @h.fetch('gumby') } + assert_equal('gumbygumby', @h.fetch('gumby') {|k| k * 2 }) + assert_equal('pokey', @h.fetch('gumby', 'pokey')) + + assert_equal('one', @h.fetch(1)) + assert_equal(nil, @h.fetch('nil')) + assert_equal('nil', @h.fetch(nil)) + end + + def test_key? + assert(!@cls[].key?(1)) + assert(!@cls[].key?(nil)) + assert(@h.key?(nil)) + assert(@h.key?(1)) + assert(!@h.key?('gumby')) + end + + def test_value? + assert(!@cls[].value?(1)) + assert(!@cls[].value?(nil)) + assert(@h.value?('one')) + assert(@h.value?(nil)) + assert(!@h.value?('gumby')) + end + + def test_include? + assert(!@cls[].include?(1)) + assert(!@cls[].include?(nil)) + assert(@h.include?(nil)) + assert(@h.include?(1)) + assert(!@h.include?('gumby')) + end + + def test_key + assert_equal(1, @h.key('one')) + assert_equal(nil, @h.key('nil')) + assert_equal('nil', @h.key(nil)) + + assert_equal(nil, @h.key('gumby')) + assert_equal(nil, @cls[].key('gumby')) + end + + def test_values_at + res = @h.values_at('dog', 'cat', 'horse') + assert(res.length == 3) + assert_equal([nil, nil, nil], res) + + res = @h.values_at + assert(res.length == 0) + + res = @h.values_at(3, 2, 1, nil) + assert_equal 4, res.length + assert_equal %w( three two one nil ), res + + res = @h.values_at(3, 99, 1, nil) + assert_equal 4, res.length + assert_equal ['three', nil, 'one', 'nil'], res + end + + + def test_invert + h = @h.invert + assert_equal(1, h['one']) + assert_equal(true, h['true']) + assert_equal(nil, h['nil']) + + h.each do |k, v| + assert(@h.key?(v)) # not true in general, but works here + end + + h = @cls[ 'a' => 1, 'b' => 2, 'c' => 1].invert + assert_equal(2, h.length) + assert(h[1] == 'a' || h[1] == 'c') + assert_equal('b', h[2]) + end + + def test_key? + assert(!@cls[].key?(1)) + assert(!@cls[].key?(nil)) + assert(@h.key?(nil)) + assert(@h.key?(1)) + assert(!@h.key?('gumby')) + end + + def test_keys + assert_equal([], @cls[].keys) + + keys = @h.keys + expected = [] + @h.each { |k, v| expected << k } + assert_equal([], keys - expected) + assert_equal([], expected - keys) + end + + def test_length + assert_equal(0, @cls[].length) + assert_equal(7, @h.length) + end + + def test_member? + assert(!@cls[].member?(1)) + assert(!@cls[].member?(nil)) + assert(@h.member?(nil)) + assert(@h.member?(1)) + assert(!@h.member?('gumby')) + end + + def test_rehash + a = [ "a", "b" ] + c = [ "c", "d" ] + h = @cls[ a => 100, c => 300 ] + assert_equal(100, h[a]) + a[0] = "z" + assert_nil(h[a]) + h.rehash + assert_equal(100, h[a]) + end + + def test_reject + base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ] + h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ] + h2 = @cls[ 2 => false, 'cat' => 99 ] + h3 = @cls[ 2 => false ] + + h = base.dup + assert_equal(h, h.reject { false }) + assert_equal(@cls[], h.reject { true }) + + h = base.dup + assert_equal(h1, h.reject {|k,v| k.instance_of?(String) }) + + assert_equal(h2, h.reject {|k,v| v.instance_of?(String) }) + + assert_equal(h3, h.reject {|k,v| v }) + assert_equal(base, h) + end + + def test_reject! + base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ] + h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ] + h2 = @cls[ 2 => false, 'cat' => 99 ] + h3 = @cls[ 2 => false ] + + h = base.dup + assert_equal(nil, h.reject! { false }) + assert_equal(@cls[], h.reject! { true }) + + h = base.dup + assert_equal(h1, h.reject! {|k,v| k.instance_of?(String) }) + assert_equal(h1, h) + + h = base.dup + assert_equal(h2, h.reject! {|k,v| v.instance_of?(String) }) + assert_equal(h2, h) + + h = base.dup + assert_equal(h3, h.reject! {|k,v| v }) + assert_equal(h3, h) + end + + def test_replace + h = @cls[ 1 => 2, 3 => 4 ] + h1 = h.replace(@cls[ 9 => 8, 7 => 6 ]) + assert_equal(h, h1) + assert_equal(8, h[9]) + assert_equal(6, h[7]) + assert_nil(h[1]) + assert_nil(h[2]) + end + + def test_shift + h = @h.dup + + @h.length.times { + k, v = h.shift + assert(@h.key?(k)) + assert_equal(@h[k], v) + } + + assert_equal(0, h.length) + end + + def test_size + assert_equal(0, @cls[].length) + assert_equal(7, @h.length) + end + + def test_sort + h = @cls[].sort + assert_equal([], h) + + h = @cls[ 1 => 1, 2 => 1 ].sort + assert_equal([[1,1], [2,1]], h) + + h = @cls[ 'cat' => 'feline', 'ass' => 'asinine', 'bee' => 'beeline' ] + h1 = h.sort + assert_equal([ %w(ass asinine), %w(bee beeline), %w(cat feline)], h1) + end + + def test_store + t = Time.now + h = @cls.new + h.store(1, 'one') + h.store(2, 'two') + h.store(3, 'three') + h.store(self, 'self') + h.store(t, 'time') + h.store(nil, 'nil') + h.store('nil', nil) + assert_equal('one', h[1]) + assert_equal('two', h[2]) + assert_equal('three', h[3]) + assert_equal('self', h[self]) + assert_equal('time', h[t]) + assert_equal('nil', h[nil]) + assert_equal(nil, h['nil']) + assert_equal(nil, h['koala']) + + h.store(1, 1) + h.store(nil, 99) + h.store('nil', nil) + assert_equal(1, h[1]) + assert_equal('two', h[2]) + assert_equal('three', h[3]) + assert_equal('self', h[self]) + assert_equal('time', h[t]) + assert_equal(99, h[nil]) + assert_equal(nil, h['nil']) + assert_equal(nil, h['koala']) + end + + def test_to_a + assert_equal([], @cls[].to_a) + assert_equal([[1,2]], @cls[ 1=>2 ].to_a) + a = @cls[ 1=>2, 3=>4, 5=>6 ].to_a + assert_equal([1,2], a.delete([1,2])) + assert_equal([3,4], a.delete([3,4])) + assert_equal([5,6], a.delete([5,6])) + assert_equal(0, a.length) + end + + def test_to_hash + h = @h.to_hash + assert_equal(@h, h) + end + + def test_to_s + h = @cls[ 1 => 2, "cat" => "dog", 1.5 => :fred ] + assert_equal(h.inspect, h.to_s) + $, = ":" + assert_equal(h.inspect, h.to_s) + h = @cls[] + assert_equal(h.inspect, h.to_s) + $, = nil + end + + def test_update + h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ] + h2 = @cls[ 2 => 'two', 4 => 'four' ] + + ha = @cls[ 1 => 2, 2 => 'two', 3 => 4, 4 => 'four' ] + hb = @cls[ 1 => 2, 2 => 3, 3 => 4, 4 => 'four' ] + + assert_equal(ha, h1.update(h2)) + assert_equal(ha, h1) + + h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ] + h2 = @cls[ 2 => 'two', 4 => 'four' ] + + assert_equal(hb, h2.update(h1)) + assert_equal(hb, h2) + end + + def test_value? + assert(!@cls[].value?(1)) + assert(!@cls[].value?(nil)) + assert(@h.value?(nil)) + assert(@h.value?('one')) + assert(!@h.value?('gumby')) + end + + def test_values + assert_equal([], @cls[].values) + + vals = @h.values + expected = [] + @h.each { |k, v| expected << v } + assert_equal([], vals - expected) + assert_equal([], expected - vals) + end + end diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb index 0db54302f5..0632881a53 100644 --- a/test/ruby/test_iterator.rb +++ b/test/ruby/test_iterator.rb @@ -1,3 +1,11 @@ +# TODO: tmp + +class Object + Proc_orig = Proc + remove_const :Proc + Proc = YARVCore::VM::Proc +end + require 'test/unit' class Array diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb index 53a7562c8e..5687722c31 100644 --- a/test/ruby/test_lambda.rb +++ b/test/ruby/test_lambda.rb @@ -1,6 +1,14 @@ require 'test/unit' class TestLambdaParameters < Test::Unit::TestCase + def test_not_supported + flunk("YARV doesn't support NODE_LAMBDA") + end +end + +__END__ + +class TestLambdaParametersBackup def test_call_simple assert_equal(1, ->(a){ a }.call(1)) assert_equal([1,2], ->(a,b){ [a,b] }.call(1,2)) diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb index 9c9fd9470b..f594971e25 100644 --- a/test/ruby/test_marshal.rb +++ b/test/ruby/test_marshal.rb @@ -1,12 +1,5 @@ require 'test/unit' -dir = File.dirname(File.expand_path(__FILE__)) -orgpath = $:.dup -begin - $:.push(dir) - require 'marshaltestlib' -ensure - $:.replace(orgpath) -end +require 'marshaltestlib' class TestMarshal < Test::Unit::TestCase include MarshalTestLib @@ -29,20 +22,18 @@ class TestMarshal < Test::Unit::TestCase 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_equal($x, Marshal.load($y)) - - assert_instance_of(StrClone, Marshal.load(Marshal.dump(StrClone.new("abc")))) + x = [1, 2, 3, [4,5,"foo"], {1=>"bar"}, 2.5, fact(30)] + assert_equal x, Marshal.load(Marshal.dump(x)) [[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_equal(a, b) + obj = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f)) + assert_equal obj, Marshal.load(Marshal.dump(obj)) } end + + StrClone = String.clone + def test_marshal_cloned_class + assert_instance_of(StrClone, Marshal.load(Marshal.dump(StrClone.new("abc")))) + end end diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb new file mode 100644 index 0000000000..471d6f6481 --- /dev/null +++ b/test/ruby/test_module.rb @@ -0,0 +1,316 @@ +require 'test/unit' +require 'pp' + +$m0 = Module.nesting + +class TestModule < Test::Unit::TestCase + + def test_LT_0 + assert_equal true, String < Object + assert_equal false, Object < String + assert_nil String < Array + assert_equal true, Array < Enumerable + assert_equal false, Enumerable < Array + assert_nil Proc < Comparable + assert_nil Comparable < Proc + end + + def test_GT_0 + assert_equal false, String > Object + assert_equal true, Object > String + assert_nil String > Array + assert_equal false, Array > Enumerable + assert_equal true, Enumerable > Array + assert_nil Comparable > Proc + assert_nil Proc > Comparable + end + + def test_CMP_0 + assert_equal -1, (String <=> Object) + assert_equal 1, (Object <=> String) + assert_nil(Array <=> String) + end + + ExpectedException = NoMethodError + + # Support stuff + + def remove_pp_mixins(list) + list.reject {|c| c == PP::ObjectMixin } + end + + module Mixin + MIXIN = 1 + def mixin + end + end + + module User + USER = 2 + include Mixin + def user + end + end + + module Other + def other + end + end + + class AClass + def AClass.cm1 + "cm1" + end + def AClass.cm2 + cm1 + "cm2" + cm3 + end + def AClass.cm3 + "cm3" + end + + private_class_method :cm1, "cm3" + + def aClass + end + + def aClass1 + end + + def aClass2 + end + + private :aClass1 + protected :aClass2 + end + + class BClass < AClass + def bClass1 + end + + private + + def bClass2 + end + + protected + def bClass3 + end + end + + MyClass = AClass.clone + class MyClass + public_class_method :cm1 + end + + # ----------------------------------------------------------- + + def test_CMP # '<=>' + assert_equal( 0, Mixin <=> Mixin) + assert_equal(-1, User <=> Mixin) + assert_equal( 1, Mixin <=> User) + + assert_equal( 0, Object <=> Object) + assert_equal(-1, String <=> Object) + assert_equal( 1, Object <=> String) + end + + def test_GE # '>=' + assert(Mixin >= User) + assert(Mixin >= Mixin) + assert(!(User >= Mixin)) + + assert(Object >= String) + assert(String >= String) + assert(!(String >= Object)) + end + + def test_GT # '>' + assert(Mixin > User) + assert(!(Mixin > Mixin)) + assert(!(User > Mixin)) + + assert(Object > String) + assert(!(String > String)) + assert(!(String > Object)) + end + + def test_LE # '<=' + assert(User <= Mixin) + assert(Mixin <= Mixin) + assert(!(Mixin <= User)) + + assert(String <= Object) + assert(String <= String) + assert(!(Object <= String)) + end + + def test_LT # '<' + assert(User < Mixin) + assert(!(Mixin < Mixin)) + assert(!(Mixin < User)) + + assert(String < Object) + assert(!(String < String)) + assert(!(Object < String)) + end + + def test_VERY_EQUAL # '===' + assert(Object === self) + assert(Test::Unit::TestCase === self) + assert(TestModule === self) + assert(!(String === self)) + end + + def test_ancestors + assert_equal([User, Mixin], User.ancestors) + assert_equal([Mixin], Mixin.ancestors) + + assert_equal([Object, Kernel, BasicObject], remove_pp_mixins(Object.ancestors)) + assert_equal([String, Comparable, Object, Kernel, BasicObject], + remove_pp_mixins(String.ancestors)) + end + + def test_class_eval + Other.class_eval("CLASS_EVAL = 1") + assert_equal(1, Other::CLASS_EVAL) + assert(Other.constants.include?("CLASS_EVAL")) + end + + def test_class_variable_set + # TODO + end + + def test_class_variable_get + # TODO + end + + def test_const_defined? + assert(Math.const_defined?(:PI)) + assert(Math.const_defined?("PI")) + assert(!Math.const_defined?(:IP)) + assert(!Math.const_defined?("IP")) + end + + def test_const_get + assert_equal(Math::PI, Math.const_get("PI")) + assert_equal(Math::PI, Math.const_get(:PI)) + end + + def test_const_set + assert(!Other.const_defined?(:KOALA)) + Other.const_set(:KOALA, 99) + assert(Other.const_defined?(:KOALA)) + assert_equal(99, Other::KOALA) + Other.const_set("WOMBAT", "Hi") + assert_equal("Hi", Other::WOMBAT) + end + + def test_constants + assert_equal(["MIXIN"], Mixin.constants) + assert_equal(["MIXIN", "USER"], User.constants.sort) + end + + def test_included_modules + assert_equal([], Mixin.included_modules) + assert_equal([Mixin], User.included_modules) + assert_equal([Kernel], remove_pp_mixins(Object.included_modules)) + assert_equal([Comparable, Kernel], + remove_pp_mixins(String.included_modules)) + end + + def test_instance_methods + assert_equal(["user" ], User.instance_methods(false)) + assert_equal(["user", "mixin"].sort, User.instance_methods(true).sort) + assert_equal(["mixin"], Mixin.instance_methods) + assert_equal(["mixin"], Mixin.instance_methods(true)) + # Ruby 1.8 feature change: + # #instance_methods includes protected methods. + #assert_equal(["aClass"], AClass.instance_methods(false)) + assert_equal(["aClass", "aClass2"], AClass.instance_methods(false).sort) + assert_equal(["aClass", "aClass2"], + (AClass.instance_methods(true) - Object.instance_methods(true)).sort) + end + + def test_method_defined? + assert(!User.method_defined?(:wombat)) + assert(User.method_defined?(:user)) + assert(User.method_defined?(:mixin)) + assert(!User.method_defined?("wombat")) + assert(User.method_defined?("user")) + assert(User.method_defined?("mixin")) + end + + def test_module_eval + User.module_eval("MODULE_EVAL = 1") + assert_equal(1, User::MODULE_EVAL) + assert(User.constants.include?("MODULE_EVAL")) + User.instance_eval("remove_const(:MODULE_EVAL)") + assert(!User.constants.include?("MODULE_EVAL")) + end + + def test_name + assert_equal("Fixnum", Fixnum.name) + assert_equal("TestModule::Mixin", Mixin.name) + assert_equal("TestModule::User", User.name) + end + + def test_private_class_method + assert_raise(ExpectedException) { AClass.cm1 } + assert_raise(ExpectedException) { AClass.cm3 } + assert_equal("cm1cm2cm3", AClass.cm2) + end + + def test_private_instance_methods + assert_equal(["aClass1"], AClass.private_instance_methods(false)) + assert_equal(["bClass2"], BClass.private_instance_methods(false)) + assert_equal(["aClass1", "bClass2"], + (BClass.private_instance_methods(true) - + Object.private_instance_methods(true)).sort) + end + + def test_protected_instance_methods + assert_equal(["aClass2"], AClass.protected_instance_methods) + assert_equal(["bClass3"], BClass.protected_instance_methods(false)) + assert_equal(["bClass3", "aClass2"].sort, + (BClass.protected_instance_methods(true) - + Object.protected_instance_methods(true)).sort) + end + + def test_public_class_method + assert_equal("cm1", MyClass.cm1) + assert_equal("cm1cm2cm3", MyClass.cm2) + assert_raise(ExpectedException) { eval "MyClass.cm3" } + end + + def test_public_instance_methods + assert_equal(["aClass"], AClass.public_instance_methods(false)) + assert_equal(["bClass1"], BClass.public_instance_methods(false)) + end + + def test_s_constants + c1 = Module.constants + Object.module_eval "WALTER = 99" + c2 = Module.constants + assert_equal(["WALTER"], c2 - c1) + end + + module M1 + $m1 = Module.nesting + module M2 + $m2 = Module.nesting + end + end + + def test_s_nesting + assert_equal([], $m0) + assert_equal([TestModule::M1, TestModule], $m1) + assert_equal([TestModule::M1::M2, + TestModule::M1, TestModule], $m2) + end + + def test_s_new + m = Module.new + assert_instance_of(Module, m) + end + +end diff --git a/test/ruby/test_pipe.rb b/test/ruby/test_pipe.rb index c2f355185b..73c7965e8c 100644 --- a/test/ruby/test_pipe.rb +++ b/test/ruby/test_pipe.rb @@ -1,6 +1,5 @@ require 'test/unit' require 'ut_eof' -require 'envutil' class TestPipe < Test::Unit::TestCase include TestEOF diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index aaeb189e27..f293bbc1a7 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -71,6 +71,7 @@ class TestProc < Test::Unit::TestCase def m(x) lambda { x } end + def test_eq # [ruby-dev:22592] a = m(1) @@ -88,8 +89,9 @@ class TestProc < Test::Unit::TestCase end def test_block_par - assert_equal(10, Proc.new{|&b| b.call(10)}.call {|x| x}) - assert_equal(12, Proc.new{|a,&b| b.call(a)}.call(12) {|x| x}) + assert false, "TODO: block parameter |&b| not supported" + # assert_equal(10, Proc.new{|&b| b.call(10)}.call {|x| x}) + # assert_equal(12, Proc.new{|a,&b| b.call(a)}.call(12) {|x| x}) end def test_safe @@ -122,4 +124,19 @@ class TestProc < Test::Unit::TestCase assert_equal(safe + 1, proc {x.method(:inc).to_proc.call; $SAFE}.call) assert_equal(safe, $SAFE) end + + def m2 + "OK" + end + + def block + method(:m2).to_proc + end + + # [yarv-dev:777] block made by Method#to_proc + def test_method_to_proc + b = block() + assert_equal "OK", b.call + end + end diff --git a/test/ruby/test_readpartial.rb b/test/ruby/test_readpartial.rb index 526425dc57..6bfafc3aa2 100644 --- a/test/ruby/test_readpartial.rb +++ b/test/ruby/test_readpartial.rb @@ -1,6 +1,6 @@ require 'test/unit' require 'timeout' -require 'fcntl' +#require 'fcntl' class TestReadPartial < Test::Unit::TestCase def make_pipe @@ -48,6 +48,8 @@ class TestReadPartial < Test::Unit::TestCase w << 'abc' assert_equal('ab', r.readpartial(2)) assert_equal('c', r.readpartial(2)) +assert false, "TODO: doesn't work on cygwin" if /cygwin/ =~ RUBY_PLATFORM +assert false, "TODO: doesn't work on mswin32" if /mswin32/ =~ RUBY_PLATFORM assert_raises(TimeoutError) { timeout(0.1) { r.readpartial(2) } } @@ -62,11 +64,11 @@ class TestReadPartial < Test::Unit::TestCase assert_equal("de", r.readpartial(2)) assert_equal("f\n", r.readpartial(4096)) assert_equal("ghi\n", r.readpartial(4096)) +assert false, "TODO: doesn't work on cygwin" if /cygwin/ =~ RUBY_PLATFORM +assert false, "TODO: doesn't work on mswin32" if /mswin32/ =~ RUBY_PLATFORM assert_raises(TimeoutError) { timeout(0.1) { r.readpartial(2) } } } end - end - diff --git a/test/ruby/test_signal.rb b/test/ruby/test_signal.rb index 8daa1cfa21..27a0d5021d 100644 --- a/test/ruby/test_signal.rb +++ b/test/ruby/test_signal.rb @@ -4,32 +4,31 @@ require 'timeout' class TestSignal < Test::Unit::TestCase def have_fork? begin - fork{} - true + Process.fork {} + return true rescue NotImplementedError - false + return false end end def test_signal - defined?(Process.kill) or return + return unless Process.method_defined?(:kill) begin - $x = 0 - oldtrap = trap "SIGINT", proc{|sig| $x = 2} - Process.kill "SIGINT", $$ + x = 0 + oldtrap = Signal.trap(:INT) {|sig| x = 2 } + Process.kill :INT, Process.pid sleep 0.1 - assert_equal(2, $x) + assert_equal 2, x - trap "SIGINT", proc{raise "Interrupt"} - - x = assert_raises(RuntimeError) do - Process.kill "SIGINT", $$ + Signal.trap(:INT) { raise "Interrupt" } + ex = assert_raises(RuntimeError) { + Process.kill :INT, Process.pid sleep 0.1 - end - assert(x) - assert_match(/Interrupt/, x.message) + } + assert_kind_of Exception, ex + assert_match(/Interrupt/, ex.message) ensure - trap "SIGINT", oldtrap + Signal.trap :INT, oldtrap if oldtrap end end @@ -38,8 +37,8 @@ class TestSignal < Test::Unit::TestCase begin r, w = IO.pipe r0, w0 = IO.pipe - pid = fork { - trap(:USR1, "EXIT") + pid = Process.fork { + Signal.trap(:USR1, "EXIT") w0.close w.syswrite("a") Thread.start { Thread.pass } @@ -50,7 +49,7 @@ class TestSignal < Test::Unit::TestCase assert_nothing_raised("[ruby-dev:26128]") { Process.kill(:USR1, pid) begin - Timeout.timeout(1) { + Timeout.timeout(3) { Process.waitpid pid } rescue Timeout::Error diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index f8938cad84..db508dc239 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1,13 +1,1115 @@ require 'test/unit' +# use of $= is deprecated after 1.7.1 +def pre_1_7_1 +end + class TestString < Test::Unit::TestCase + + def initialize(*args) + @cls = String + @aref_re_nth = true + @aref_re_silent = false + @aref_slicebang_silent = true + super + end + + def S(str) + @cls.new(str) + end + + def test_s_new + assert_equal("RUBY", S("RUBY")) + end + + def test_AREF # '[]' + assert_equal("A", S("AooBar")[0]) + assert_equal("B", S("FooBaB")[-1]) + assert_equal(nil, S("FooBar")[6]) + assert_equal(nil, S("FooBar")[-7]) + + assert_equal(S("Foo"), S("FooBar")[0,3]) + assert_equal(S("Bar"), S("FooBar")[-3,3]) + assert_equal(S(""), S("FooBar")[6,2]) + assert_equal(nil, S("FooBar")[-7,10]) + + assert_equal(S("Foo"), S("FooBar")[0..2]) + assert_equal(S("Foo"), S("FooBar")[0...3]) + assert_equal(S("Bar"), S("FooBar")[-3..-1]) + assert_equal(S(""), S("FooBar")[6..2]) + assert_equal(nil, S("FooBar")[-10..-7]) + + assert_equal(S("Foo"), S("FooBar")[/^F../]) + assert_equal(S("Bar"), S("FooBar")[/..r$/]) + assert_equal(nil, S("FooBar")[/xyzzy/]) + assert_equal(nil, S("FooBar")[/plugh/]) + + assert_equal(S("Foo"), S("FooBar")[S("Foo")]) + assert_equal(S("Bar"), S("FooBar")[S("Bar")]) + assert_equal(nil, S("FooBar")[S("xyzzy")]) + assert_equal(nil, S("FooBar")[S("plugh")]) + + if @aref_re_nth + assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1]) + assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2]) + assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3]) + assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1]) + assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2]) + assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3]) + end + end + + def test_ASET # '[]=' + s = S("FooBar") + s[0] = S('A') + assert_equal(S("AooBar"), s) + + s[-1]= S('B') + assert_equal(S("AooBaB"), s) + assert_raise(IndexError) { s[-7] = S("xyz") } + assert_equal(S("AooBaB"), s) + s[0] = S("ABC") + assert_equal(S("ABCooBaB"), s) + + s = S("FooBar") + s[0,3] = S("A") + assert_equal(S("ABar"),s) + s[0] = S("Foo") + assert_equal(S("FooBar"), s) + s[-3,3] = S("Foo") + assert_equal(S("FooFoo"), s) + assert_raise (IndexError) { s[7,3] = S("Bar") } + assert_raise (IndexError) { s[-7,3] = S("Bar") } + + s = S("FooBar") + s[0..2] = S("A") + assert_equal(S("ABar"), s) + s[1..3] = S("Foo") + assert_equal(S("AFoo"), s) + s[-4..-4] = S("Foo") + assert_equal(S("FooFoo"), s) + assert_raise (RangeError) { s[7..10] = S("Bar") } + assert_raise (RangeError) { s[-7..-10] = S("Bar") } + + s = S("FooBar") + s[/^F../]= S("Bar") + assert_equal(S("BarBar"), s) + s[/..r$/] = S("Foo") + assert_equal(S("BarFoo"), s) + if @aref_re_silent + s[/xyzzy/] = S("None") + assert_equal(S("BarFoo"), s) + else + assert_raise (IndexError) { s[/xyzzy/] = S("None") } + end + if @aref_re_nth + s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo") + assert_equal(S("FooFoo"), s) + s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar") + assert_equal(S("FooBar"), s) + assert_raise (IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" } + s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo") + assert_equal(S("FooFoo"), s) + s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar") + assert_equal(S("BarFoo"), s) + assert_raise (IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" } + end + + s = S("FooBar") + s[S("Foo")] = S("Bar") + assert_equal(S("BarBar"), s) + + pre_1_7_1 do + s = S("FooBar") + s[S("Foo")] = S("xyz") + assert_equal(S("xyzBar"), s) + + $= = true + s = S("FooBar") + s[S("FOO")] = S("Bar") + assert_equal(S("BarBar"), s) + s[S("FOO")] = S("xyz") + assert_equal(S("BarBar"), s) + $= = false + end + + s = S("a string") + s[0..s.size] = S("another string") + assert_equal(S("another string"), s) + end + + def test_CMP # '<=>' + assert_equal(1, S("abcdef") <=> S("abcde")) + assert_equal(0, S("abcdef") <=> S("abcdef")) + assert_equal(-1, S("abcde") <=> S("abcdef")) + + assert_equal(-1, S("ABCDEF") <=> S("abcdef")) + + pre_1_7_1 do + $= = true + assert_equal(0, S("ABCDEF") <=> S("abcdef")) + $= = false + end + end + + def test_EQUAL # '==' + assert_equal(true, S("foo") == :foo) + assert(S("abcdef") == S("abcdef")) + + pre_1_7_1 do + $= = true + assert(S("CAT") == S('cat')) + assert(S("CaT") == S('cAt')) + $= = false + end + + assert(S("CAT") != S('cat')) + assert(S("CaT") != S('cAt')) + end + + def test_LSHIFT # '<<' + assert_equal(S("world!"), S("world") << 33) + assert_equal(S("world!"), S("world") << S('!')) + end + + def test_MATCH # '=~' + assert_equal(10, S("FeeFieFoo-Fum") =~ /Fum$/) + assert_equal(nil, S("FeeFieFoo-Fum") =~ /FUM$/) + + pre_1_7_1 do + $= = true + assert_equal(10, S("FeeFieFoo-Fum") =~ /FUM$/) + $= = false + end + end + + def test_MOD # '%' + assert_equal(S("00123"), S("%05d") % 123) + assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1]) + x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") % + [S("hi"), + 123, + S("never seen"), + 456, + 0, + ?A, + 3.0999, + 11, + 171, + 171, + 11, + 171, + 171] + + assert_equal(S(' hi 123 %foo 456 0x0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x) + end + + def test_MUL # '*' + assert_equal(S("XXX"), S("X") * 3) + assert_equal(S("HOHO"), S("HO") * 2) + end + + def test_PLUS # '+' + assert_equal(S("Yodel"), S("Yo") + S("del")) + end + + def casetest(a, b, rev=false) + case a + when b + assert(!rev) + else + assert(rev) + end + end + + def test_VERY_EQUAL # '===' + assert_equal(true, S("foo") === :foo) + casetest(S("abcdef"), S("abcdef")) + + pre_1_7_1 do + $= = true + casetest(S("CAT"), S('cat')) + casetest(S("CaT"), S('cAt')) + $= = false + end + + casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to + casetest(S("CaT"), S('cAt'), true) # find these in the case. + end + + def test_capitalize + assert_equal(S("Hello"), S("hello").capitalize) + assert_equal(S("Hello"), S("hELLO").capitalize) + assert_equal(S("123abc"), S("123ABC").capitalize) + end + + def test_capitalize! + a = S("hello"); a.capitalize! + assert_equal(S("Hello"), a) + + a = S("hELLO"); a.capitalize! + assert_equal(S("Hello"), a) + + a = S("123ABC"); a.capitalize! + assert_equal(S("123abc"), a) + + assert_equal(nil, S("123abc").capitalize!) + assert_equal(S("123abc"), S("123ABC").capitalize!) + assert_equal(S("Abc"), S("ABC").capitalize!) + assert_equal(S("Abc"), S("abc").capitalize!) + assert_equal(nil, S("Abc").capitalize!) + + a = S("hello") + b = a.dup + assert_equal(S("Hello"), a.capitalize!) + assert_equal(S("hello"), b) + + end + + def test_center + assert_equal(S("hello"), S("hello").center(4)) + assert_equal(S(" hello "), S("hello").center(11)) + end + + def test_chomp + assert_equal(S("hello"), S("hello").chomp("\n")) + assert_equal(S("hello"), S("hello\n").chomp("\n")) + + $/ = "\n" + + assert_equal(S("hello"), S("hello").chomp) + assert_equal(S("hello"), S("hello\n").chomp) + + $/ = "!" + assert_equal(S("hello"), S("hello").chomp) + assert_equal(S("hello"), S("hello!").chomp) + $/ = "\n" + end + + def test_chomp! + a = S("hello") + a.chomp!(S("\n")) + + assert_equal(S("hello"), a) + assert_equal(nil, a.chomp!(S("\n"))) + + a = S("hello\n") + a.chomp!(S("\n")) + assert_equal(S("hello"), a) + + $/ = "\n" + a = S("hello") + a.chomp! + assert_equal(S("hello"), a) + + a = S("hello\n") + a.chomp! + assert_equal(S("hello"), a) + + $/ = "!" + a = S("hello") + a.chomp! + assert_equal(S("hello"), a) + + a="hello!" + a.chomp! + assert_equal(S("hello"), a) + + $/ = "\n" + + a = S("hello\n") + b = a.dup + assert_equal(S("hello"), a.chomp!) + assert_equal(S("hello\n"), b) + + end + + def test_chop + assert_equal(S("hell"), S("hello").chop) + assert_equal(S("hello"), S("hello\r\n").chop) + assert_equal(S("hello\n"), S("hello\n\r").chop) + assert_equal(S(""), S("\r\n").chop) + assert_equal(S(""), S("").chop) + end + + def test_chop! + a = S("hello").chop! + assert_equal(S("hell"), a) + + a = S("hello\r\n").chop! + assert_equal(S("hello"), a) + + a = S("hello\n\r").chop! + assert_equal(S("hello\n"), a) + + a = S("\r\n").chop! + assert_equal(S(""), a) + + a = S("").chop! + assert_nil(a) + + a = S("hello\n") + b = a.dup + assert_equal(S("hello"), a.chop!) + assert_equal(S("hello\n"), b) + end + + def test_clone + for taint in [ false, true ] + for frozen in [ false, true ] + a = S("Cool") + a.taint if taint + a.freeze if frozen + b = a.clone + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert_equal(a.frozen?, b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_concat + assert_equal(S("world!"), S("world").concat(33)) + assert_equal(S("world!"), S("world").concat(S('!'))) + end + + def test_count + a = S("hello world") + assert_equal(5, a.count(S("lo"))) + assert_equal(2, a.count(S("lo"), S("o"))) + assert_equal(4, a.count(S("hello"), S("^l"))) + assert_equal(4, a.count(S("ej-m"))) + end + + def test_crypt + assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa"))) + assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab"))) + end + + def test_delete + assert_equal(S("heo"), S("hello").delete(S("l"), S("lo"))) + assert_equal(S("he"), S("hello").delete(S("lo"))) + assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e"))) + assert_equal(S("ho"), S("hello").delete(S("ej-m"))) + end + + def test_delete! + a = S("hello") + a.delete!(S("l"), S("lo")) + assert_equal(S("heo"), a) + + a = S("hello") + a.delete!(S("lo")) + assert_equal(S("he"), a) + + a = S("hello") + a.delete!(S("aeiou"), S("^e")) + assert_equal(S("hell"), a) + + a = S("hello") + a.delete!(S("ej-m")) + assert_equal(S("ho"), a) + + a = S("hello") + assert_nil(a.delete!(S("z"))) + + a = S("hello") + b = a.dup + a.delete!(S("lo")) + assert_equal(S("he"), a) + assert_equal(S("hello"), b) + end + + + def test_downcase + assert_equal(S("hello"), S("helLO").downcase) + assert_equal(S("hello"), S("hello").downcase) + assert_equal(S("hello"), S("HELLO").downcase) + assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase) + end + + def test_downcase! + a = S("helLO") + b = a.dup + assert_equal(S("hello"), a.downcase!) + assert_equal(S("hello"), a) + assert_equal(S("helLO"), b) + + a=S("hello") + assert_nil(a.downcase!) + assert_equal(S("hello"), a) + end + + def test_dump + a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10 + assert_equal(S('"Test\\001\\002\\003\\t\\r\\n"'), a.dump) + end + + def test_dup + for taint in [ false, true ] + for frozen in [ false, true ] + a = S("hello") + a.taint if taint + a.freeze if frozen + b = a.dup + + assert_equal(a, b) + assert(a.__id__ != b.__id__) + assert(!b.frozen?) + assert_equal(a.tainted?, b.tainted?) + end + end + end + + def test_each + $/ = "\n" + res=[] + S("hello\nworld").lines.each {|x| res << x} + assert_equal(S("hello\n"), res[0]) + assert_equal(S("world"), res[1]) + + res=[] + S("hello\n\n\nworld").lines(S('')).each {|x| res << x} + assert_equal(S("hello\n\n\n"), res[0]) + assert_equal(S("world"), res[1]) + + $/ = "!" + res=[] + S("hello!world").lines.each {|x| res << x} + assert_equal(S("hello!"), res[0]) + assert_equal(S("world"), res[1]) + $/ = "\n" + end + + def test_each_byte + res = [] + S("ABC").each_byte {|x| res << x } + assert_equal(65, res[0]) + assert_equal(66, res[1]) + assert_equal(67, res[2]) + end + + def test_each_line + $/ = "\n" + res=[] + S("hello\nworld").lines.each {|x| res << x} + assert_equal(S("hello\n"), res[0]) + assert_equal(S("world"), res[1]) + + res=[] + S("hello\n\n\nworld").lines(S('')).each {|x| res << x} + assert_equal(S("hello\n\n\n"), res[0]) + assert_equal(S("world"), res[1]) + + $/ = "!" + + res=[] + S("hello!world").lines.each {|x| res << x} + assert_equal(S("hello!"), res[0]) + assert_equal(S("world"), res[1]) + + $/ = "\n" + end + + def test_empty? + assert(S("").empty?) + assert(!S("not").empty?) + end + + def test_eql? + a = S("hello") + assert(a.eql?(S("hello"))) + assert(a.eql?(a)) + end + + def test_gsub + assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*'))) + assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>'))) + assert_equal(S("h e l l o "), + S("hello").gsub(/./) { |s| s[0].to_s + S(' ')}) + assert_equal(S("HELL-o"), + S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }) + + a = S("hello") + a.taint + assert(a.gsub(/./, S('X')).tainted?) + end + + def test_gsub! + a = S("hello") + b = a.dup + a.gsub!(/[aeiou]/, S('*')) + assert_equal(S("h*ll*"), a) + assert_equal(S("hello"), b) + + a = S("hello") + a.gsub!(/([aeiou])/, S('<\1>')) + assert_equal(S("h<e>ll<o>"), a) + + a = S("hello") + a.gsub!(/./) { |s| s[0].to_s + S(' ')} + assert_equal(S("h e l l o "), a) + + a = S("hello") + a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 } + assert_equal(S("HELL-o"), a) + + r = S('X') + r.taint + a.gsub!(/./, r) + assert(a.tainted?) + + a = S("hello") + assert_nil(a.sub!(S('X'), S('Y'))) + end + + def test_hash + assert_equal(S("hello").hash, S("hello").hash) + assert(S("hello").hash != S("helLO").hash) + end + + def test_hex + assert_equal(255, S("0xff").hex) + assert_equal(-255, S("-0xff").hex) + assert_equal(255, S("ff").hex) + assert_equal(-255, S("-ff").hex) + assert_equal(0, S("-ralph").hex) + assert_equal(-15, S("-fred").hex) + assert_equal(15, S("fred").hex) + end + + def test_include? + assert( S("foobar").include?(?f)) + assert( S("foobar").include?(S("foo"))) + assert(!S("foobar").include?(S("baz"))) + assert(!S("foobar").include?(?z)) + end + + def test_index + assert_equal(0, S("hello").index(?h)) + assert_equal(1, S("hello").index(S("ell"))) + assert_equal(2, S("hello").index(/ll./)) + + assert_equal(3, S("hello").index(?l, 3)) + assert_equal(3, S("hello").index(S("l"), 3)) + assert_equal(3, S("hello").index(/l./, 3)) + + assert_nil(S("hello").index(?z, 3)) + assert_nil(S("hello").index(S("z"), 3)) + assert_nil(S("hello").index(/z./, 3)) + + assert_nil(S("hello").index(?z)) + assert_nil(S("hello").index(S("z"))) + assert_nil(S("hello").index(/z./)) + end + + def test_intern + assert_equal(:koala, S("koala").intern) + assert(:koala != S("Koala").intern) + end + + def test_length + assert_equal(0, S("").length) + assert_equal(4, S("1234").length) + assert_equal(6, S("1234\r\n").length) + assert_equal(7, S("\0011234\r\n").length) + end + + def test_ljust + assert_equal(S("hello"), S("hello").ljust(4)) + assert_equal(S("hello "), S("hello").ljust(11)) + end + + def test_next + assert_equal(S("abd"), S("abc").next) + assert_equal(S("z"), S("y").next) + assert_equal(S("aaa"), S("zz").next) + + assert_equal(S("124"), S("123").next) + assert_equal(S("1000"), S("999").next) + + assert_equal(S("2000aaa"), S("1999zzz").next) + assert_equal(S("AAAAA000"), S("ZZZZ999").next) + + assert_equal(S("*+"), S("**").next) + end + + def test_next! + a = S("abc") + b = a.dup + assert_equal(S("abd"), a.next!) + assert_equal(S("abd"), a) + assert_equal(S("abc"), b) + + a = S("y") + assert_equal(S("z"), a.next!) + assert_equal(S("z"), a) + + a = S("zz") + assert_equal(S("aaa"), a.next!) + assert_equal(S("aaa"), a) + + a = S("123") + assert_equal(S("124"), a.next!) + assert_equal(S("124"), a) + + a = S("999") + assert_equal(S("1000"), a.next!) + assert_equal(S("1000"), a) + + a = S("1999zzz") + assert_equal(S("2000aaa"), a.next!) + assert_equal(S("2000aaa"), a) + + a = S("ZZZZ999") + assert_equal(S("AAAAA000"), a.next!) + assert_equal(S("AAAAA000"), a) + + a = S("**") + assert_equal(S("*+"), a.next!) + assert_equal(S("*+"), a) + end + + def test_oct + assert_equal(255, S("0377").oct) + assert_equal(255, S("377").oct) + assert_equal(-255, S("-0377").oct) + assert_equal(-255, S("-377").oct) + assert_equal(0, S("OO").oct) + assert_equal(24, S("030OO").oct) + end + + def test_replace + a = S("foo") + assert_equal(S("f"), a.replace(S("f"))) + + a = S("foo") + assert_equal(S("foobar"), a.replace(S("foobar"))) + + a = S("foo") + a.taint + b = a.replace(S("xyz")) + assert_equal(S("xyz"), b) + assert(b.tainted?) + end + + def test_reverse + assert_equal(S("beta"), S("ateb").reverse) + assert_equal(S("madamImadam"), S("madamImadam").reverse) + + a=S("beta") + assert_equal(S("ateb"), a.reverse) + assert_equal(S("beta"), a) + end + + def test_reverse! + a = S("beta") + b = a.dup + assert_equal(S("ateb"), a.reverse!) + assert_equal(S("ateb"), a) + assert_equal(S("beta"), b) + + assert_equal(S("madamImadam"), S("madamImadam").reverse!) + + a = S("madamImadam") + assert_equal(S("madamImadam"), a.reverse!) # ?? + assert_equal(S("madamImadam"), a) + end + + def test_rindex + assert_equal(3, S("hello").rindex(?l)) + assert_equal(6, S("ell, hello").rindex(S("ell"))) + assert_equal(7, S("ell, hello").rindex(/ll./)) + + assert_equal(3, S("hello,lo").rindex(?l, 3)) + assert_equal(3, S("hello,lo").rindex(S("l"), 3)) + assert_equal(3, S("hello,lo").rindex(/l./, 3)) + + assert_nil(S("hello").rindex(?z, 3)) + assert_nil(S("hello").rindex(S("z"), 3)) + assert_nil(S("hello").rindex(/z./, 3)) + + assert_nil(S("hello").rindex(?z)) + assert_nil(S("hello").rindex(S("z"))) + assert_nil(S("hello").rindex(/z./)) + end + + def test_rjust + assert_equal(S("hello"), S("hello").rjust(4)) + assert_equal(S(" hello"), S("hello").rjust(11)) + end + + def test_scan + a = S("cruel world") + assert_equal([S("cruel"), S("world")],a.scan(/\w+/)) + assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../)) + assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/)) + + res = [] + a.scan(/\w+/) { |w| res << w } + assert_equal([S("cruel"), S("world") ],res) + + res = [] + a.scan(/.../) { |w| res << w } + assert_equal([S("cru"), S("el "), S("wor")],res) + + res = [] + a.scan(/(...)/) { |w| res << w } + assert_equal([[S("cru")], [S("el ")], [S("wor")]],res) + end + + def test_size + assert_equal(0, S("").size) + assert_equal(4, S("1234").size) + assert_equal(6, S("1234\r\n").size) + assert_equal(7, S("\0011234\r\n").size) + end + + def test_slice + assert_equal(?A, S("AooBar").slice(0)) + assert_equal(?B, S("FooBaB").slice(-1)) + assert_nil(S("FooBar").slice(6)) + assert_nil(S("FooBar").slice(-7)) + + assert_equal(S("Foo"), S("FooBar").slice(0,3)) + assert_equal(S(S("Bar")), S("FooBar").slice(-3,3)) + assert_nil(S("FooBar").slice(7,2)) # Maybe should be six? + assert_nil(S("FooBar").slice(-7,10)) + + assert_equal(S("Foo"), S("FooBar").slice(0..2)) + assert_equal(S("Bar"), S("FooBar").slice(-3..-1)) + assert_equal(S(""), S("FooBar").slice(6..2)) + assert_nil(S("FooBar").slice(-10..-7)) + + assert_equal(S("Foo"), S("FooBar").slice(/^F../)) + assert_equal(S("Bar"), S("FooBar").slice(/..r$/)) + assert_nil(S("FooBar").slice(/xyzzy/)) + assert_nil(S("FooBar").slice(/plugh/)) + + assert_equal(S("Foo"), S("FooBar").slice(S("Foo"))) + assert_equal(S("Bar"), S("FooBar").slice(S("Bar"))) + assert_nil(S("FooBar").slice(S("xyzzy"))) + assert_nil(S("FooBar").slice(S("plugh"))) + end + + def test_slice! + a = S("AooBar") + b = a.dup + assert_equal(?A, a.slice!(0)) + assert_equal(S("ooBar"), a) + assert_equal(S("AooBar"), b) + + a = S("FooBar") + assert_equal(?r,a.slice!(-1)) + assert_equal(S("FooBa"), a) + + a = S("FooBar") + if @aref_slicebang_silent + assert_nil( a.slice!(6) ) + else + assert_raise(IndexError) { a.slice!(6) } + end + assert_equal(S("FooBar"), a) + + if @aref_slicebang_silent + assert_nil( a.slice!(-7) ) + else + assert_raise(IndexError) { a.slice!(-7) } + end + assert_equal(S("FooBar"), a) + + a = S("FooBar") + assert_equal(S("Foo"), a.slice!(0,3)) + assert_equal(S("Bar"), a) + + a = S("FooBar") + assert_equal(S("Bar"), a.slice!(-3,3)) + assert_equal(S("Foo"), a) + + a=S("FooBar") + if @aref_slicebang_silent + assert_nil(a.slice!(7,2)) # Maybe should be six? + else + assert_raise(IndexError) {a.slice!(7,2)} # Maybe should be six? + end + assert_equal(S("FooBar"), a) + if @aref_slicebang_silent + assert_nil(a.slice!(-7,10)) + else + assert_raise(IndexError) {a.slice!(-7,10)} + end + assert_equal(S("FooBar"), a) + + a=S("FooBar") + assert_equal(S("Foo"), a.slice!(0..2)) + assert_equal(S("Bar"), a) + + a=S("FooBar") + assert_equal(S("Bar"), a.slice!(-3..-1)) + assert_equal(S("Foo"), a) + + a=S("FooBar") + if @aref_slicebang_silent + assert_equal(S(""), a.slice!(6..2)) + else + assert_raise(RangeError) {a.slice!(6..2)} + end + assert_equal(S("FooBar"), a) + if @aref_slicebang_silent + assert_nil(a.slice!(-10..-7)) + else + assert_raise(RangeError) {a.slice!(-10..-7)} + end + assert_equal(S("FooBar"), a) + + a=S("FooBar") + assert_equal(S("Foo"), a.slice!(/^F../)) + assert_equal(S("Bar"), a) + + a=S("FooBar") + assert_equal(S("Bar"), a.slice!(/..r$/)) + assert_equal(S("Foo"), a) + + a=S("FooBar") + if @aref_slicebang_silent + assert_nil(a.slice!(/xyzzy/)) + else + assert_raise(IndexError) {a.slice!(/xyzzy/)} + end + assert_equal(S("FooBar"), a) + if @aref_slicebang_silent + assert_nil(a.slice!(/plugh/)) + else + assert_raise(IndexError) {a.slice!(/plugh/)} + end + assert_equal(S("FooBar"), a) + + a=S("FooBar") + assert_equal(S("Foo"), a.slice!(S("Foo"))) + assert_equal(S("Bar"), a) + + a=S("FooBar") + assert_equal(S("Bar"), a.slice!(S("Bar"))) + assert_equal(S("Foo"), a) + + pre_1_7_1 do + a=S("FooBar") + assert_nil(a.slice!(S("xyzzy"))) + assert_equal(S("FooBar"), a) + assert_nil(a.slice!(S("plugh"))) + assert_equal(S("FooBar"), a) + end + end + + def test_split + assert_nil($;) + assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split) + assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" "))) + + assert_equal([S(" a "), S(" b "), S(" c ")], S(" a | b | c ").split(S("|"))) + + assert_equal([S("a"), S("b"), S("c")], S("aXXbXXcXX").split(/X./)) + + assert_equal([S("a"), S("b"), S("c")], S("abc").split(//)) + + assert_equal([S("a|b|c")], S("a|b|c").split(S('|'), 1)) + + assert_equal([S("a"), S("b|c")], S("a|b|c").split(S('|'), 2)) + assert_equal([S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3)) + + assert_equal([S("a"), S("b"), S("c"), S("")], S("a|b|c|").split(S('|'), -1)) + assert_equal([S("a"), S("b"), S("c"), S(""), S("")], S("a|b|c||").split(S('|'), -1)) + + assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|'))) + assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1)) + end + + def test_squeeze + assert_equal(S("abc"), S("aaabbbbccc").squeeze) + assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" "))) + assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z"))) + end + + def test_squeeze! + a = S("aaabbbbccc") + b = a.dup + assert_equal(S("abc"), a.squeeze!) + assert_equal(S("abc"), a) + assert_equal(S("aaabbbbccc"), b) + + a = S("aa bb cc") + assert_equal(S("aa bb cc"), a.squeeze!(S(" "))) + assert_equal(S("aa bb cc"), a) + + a = S("BxxxTyyyWzzzzz") + assert_equal(S("BxTyWz"), a.squeeze!(S("a-z"))) + assert_equal(S("BxTyWz"), a) + + a=S("The quick brown fox") + assert_nil(a.squeeze!) + end + + def test_strip + assert_equal(S("x"), S(" x ").strip) + assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip) + end + + def test_strip! + a = S(" x ") + b = a.dup + assert_equal(S("x") ,a.strip!) + assert_equal(S("x") ,a) + assert_equal(S(" x "), b) + + a = S(" \n\r\t x \t\r\n\n ") + assert_equal(S("x"), a.strip!) + assert_equal(S("x"), a) + + a = S("x") + assert_nil(a.strip!) + assert_equal(S("x") ,a) + end + + def test_sub + assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*'))) + assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>'))) + assert_equal(S("h ello"), S("hello").sub(/./) { + |s| s[0].to_s + S(' ')}) + assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) { + |s| $1.upcase + S('-') + $2 + }) + + assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\')) + assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\')) + assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1')) + assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1')) + assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1')) + assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1')) + assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1')) + + assert_equal(S("a--ababababababababab"), + S("abababababababababab").sub(/(b)/, '-\9-')) + assert_equal(S("1-b-0"), + S("1b2b3b4b5b6b7b8b9b0"). + sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-')) + assert_equal(S("1-b-0"), + S("1b2b3b4b5b6b7b8b9b0"). + sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-')) + assert_equal(S("1-\\9-0"), + S("1b2b3b4b5b6b7b8b9b0"). + sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-')) + assert_equal(S("k"), + S("1a2b3c4d5e6f7g8h9iAjBk"). + sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+')) + + assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\')) + assert_equal(S("ababa"), S("ababa").sub(/b/, '\&')) + assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&')) + assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&')) + assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&')) + assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&')) + + a = S("hello") + a.taint + assert(a.sub(/./, S('X')).tainted?) + end + + def test_sub! + a = S("hello") + b = a.dup + a.sub!(/[aeiou]/, S('*')) + assert_equal(S("h*llo"), a) + assert_equal(S("hello"), b) + + a = S("hello") + a.sub!(/([aeiou])/, S('<\1>')) + assert_equal(S("h<e>llo"), a) + + a = S("hello") + a.sub!(/./) { |s| s[0].to_s + S(' ')} + assert_equal(S("h ello"), a) + + a = S("hello") + a.sub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 } + assert_equal(S("HELL-o"), a) + + a=S("hello") + assert_nil(a.sub!(/X/, S('Y'))) + + r = S('X') + r.taint + a.sub!(/./, r) + assert(a.tainted?) + end + + def test_succ + assert_equal(S("abd"), S("abc").succ) + assert_equal(S("z"), S("y").succ) + assert_equal(S("aaa"), S("zz").succ) + + assert_equal(S("124"), S("123").succ) + assert_equal(S("1000"), S("999").succ) + + assert_equal(S("2000aaa"), S("1999zzz").succ) + assert_equal(S("AAAAA000"), S("ZZZZ999").succ) + assert_equal(S("*+"), S("**").succ) + end + + def test_succ! + a = S("abc") + b = a.dup + assert_equal(S("abd"), a.succ!) + assert_equal(S("abd"), a) + assert_equal(S("abc"), b) + + a = S("y") + assert_equal(S("z"), a.succ!) + assert_equal(S("z"), a) + + a = S("zz") + assert_equal(S("aaa"), a.succ!) + assert_equal(S("aaa"), a) + + a = S("123") + assert_equal(S("124"), a.succ!) + assert_equal(S("124"), a) + + a = S("999") + assert_equal(S("1000"), a.succ!) + assert_equal(S("1000"), a) + + a = S("1999zzz") + assert_equal(S("2000aaa"), a.succ!) + assert_equal(S("2000aaa"), a) + + a = S("ZZZZ999") + assert_equal(S("AAAAA000"), a.succ!) + assert_equal(S("AAAAA000"), a) + + a = S("**") + assert_equal(S("*+"), a.succ!) + assert_equal(S("*+"), a) + end + + def test_sum + n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001") + assert_equal(15, n.sum) + n += S("\001") + assert_equal(16, n.sum(17)) + n[0] = 2.chr + assert(15 != n.sum) + end + def check_sum(str, bits=16) sum = 0 str.each_byte {|c| sum += c} sum = sum & ((1 << bits) - 1) if bits != 0 assert_equal(sum, str.sum(bits)) end - def test_sum + + def test_sum_2 assert_equal(0, "".sum) assert_equal(294, "abc".sum) check_sum("abc") @@ -16,4 +1118,198 @@ class TestString < Test::Unit::TestCase check_sum("xyz", bits) } end + + def test_swapcase + assert_equal(S("hi&LOW"), S("HI&low").swapcase) + end + + def test_swapcase! + a = S("hi&LOW") + b = a.dup + assert_equal(S("HI&low"), a.swapcase!) + assert_equal(S("HI&low"), a) + assert_equal(S("hi&LOW"), b) + + a = S("$^#^%$#!!") + assert_nil(a.swapcase!) + assert_equal(S("$^#^%$#!!"), a) + end + + def test_to_f + assert_equal(344.3, S("344.3").to_f) + assert_equal(5.9742e24, S("5.9742e24").to_f) + assert_equal(98.6, S("98.6 degrees").to_f) + assert_equal(0.0, S("degrees 100.0").to_f) + end + + def test_to_i + assert_equal(1480, S("1480ft/sec").to_i) + assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i) + end + + def test_to_s + a = S("me") + assert_equal("me", a.to_s) + assert_equal(a.__id__, a.to_s.__id__) if @cls == String + end + + def test_to_str + a = S("me") + assert_equal("me", a.to_s) + assert_equal(a.__id__, a.to_s.__id__) if @cls == String + end + + def test_tr + assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip"))) + assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*"))) + assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z"))) + end + + def test_tr! + a = S("hello") + b = a.dup + assert_equal(S("hippo"), a.tr!(S("el"), S("ip"))) + assert_equal(S("hippo"), a) + assert_equal(S("hello"),b) + + a = S("hello") + assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*"))) + assert_equal(S("*e**o"), a) + + a = S("IBM") + assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z"))) + assert_equal(S("HAL"), a) + + a = S("ibm") + assert_nil(a.tr!(S("B-Z"), S("A-Z"))) + assert_equal(S("ibm"), a) + end + + def test_tr_s + assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp"))) + assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*"))) + end + + def test_tr_s! + a = S("hello") + b = a.dup + assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp"))) + assert_equal(S("hypo"), a) + assert_equal(S("hello"), b) + + a = S("hello") + assert_equal(S("h*o"), a.tr_s!(S("el"), S("*"))) + assert_equal(S("h*o"), a) + end + + def test_unpack + a = [S("cat"), S("wom"), S("x"), S("yy")] + assert_equal(a, S("catwomx yy ").unpack(S("A3A3A3A3"))) + + assert_equal([S("cat")], S("cat \000\000").unpack(S("A*"))) + assert_equal([S("cwx"), S("wx"), S("x"), S("yy")], + S("cwx yy ").unpack(S("A3@1A3@2A3A3"))) + assert_equal([S("cat"), S("wom"), S("x\000\000"), S("yy\000")], + S("catwomx\000\000yy\000").unpack(S("a3a3a3a3"))) + assert_equal([S("cat \000\000")], S("cat \000\000").unpack(S("a*"))) + assert_equal([S("ca")], S("catdog").unpack(S("a2"))) + + assert_equal([S("cat\000\000")], + S("cat\000\000\000\000\000dog").unpack(S("a5"))) + + assert_equal([S("01100001")], S("\x61").unpack(S("B8"))) + assert_equal([S("01100001")], S("\x61").unpack(S("B*"))) + assert_equal([S("0110000100110111")], S("\x61\x37").unpack(S("B16"))) + assert_equal([S("01100001"), S("00110111")], S("\x61\x37").unpack(S("B8B8"))) + assert_equal([S("0110")], S("\x60").unpack(S("B4"))) + + assert_equal([S("01")], S("\x40").unpack(S("B2"))) + + assert_equal([S("01100001")], S("\x86").unpack(S("b8"))) + assert_equal([S("01100001")], S("\x86").unpack(S("b*"))) + + assert_equal([S("0110000100110111")], S("\x86\xec").unpack(S("b16"))) + assert_equal([S("01100001"), S("00110111")], S("\x86\xec").unpack(S("b8b8"))) + + assert_equal([S("0110")], S("\x06").unpack(S("b4"))) + assert_equal([S("01")], S("\x02").unpack(S("b2"))) + + assert_equal([ 65, 66, 67 ], S("ABC").unpack(S("C3"))) + assert_equal([ 255, 66, 67 ], S("\377BC").unpack("C*")) + assert_equal([ 65, 66, 67 ], S("ABC").unpack("c3")) + assert_equal([ -1, 66, 67 ], S("\377BC").unpack("c*")) + + + assert_equal([S("4142"), S("0a"), S("1")], S("AB\n\x10").unpack(S("H4H2H1"))) + assert_equal([S("1424"), S("a0"), S("2")], S("AB\n\x02").unpack(S("h4h2h1"))) + + assert_equal([S("abc\002defcat\001"), S(""), S("")], + S("abc=02def=\ncat=\n=01=\n").unpack(S("M9M3M4"))) + + assert_equal([S("hello\n")], S("aGVsbG8K\n").unpack(S("m"))) + + assert_equal([S("hello\nhello\n")], S(",:&5L;&\\*:&5L;&\\*\n").unpack(S("u"))) + + assert_equal([0xa9, 0x42, 0x2260], S("\xc2\xa9B\xe2\x89\xa0").unpack(S("U*"))) + +=begin + skipping "Not tested: + D,d & double-precision float, native format\\ + E & double-precision float, little-endian byte order\\ + e & single-precision float, little-endian byte order\\ + F,f & single-precision float, native format\\ + G & double-precision float, network (big-endian) byte order\\ + g & single-precision float, network (big-endian) byte order\\ + I & unsigned integer\\ + i & integer\\ + L & unsigned long\\ + l & long\\ + + m & string encoded in base64 (uuencoded)\\ + N & long, network (big-endian) byte order\\ + n & short, network (big-endian) byte-order\\ + P & pointer to a structure (fixed-length string)\\ + p & pointer to a null-terminated string\\ + S & unsigned short\\ + s & short\\ + V & long, little-endian byte order\\ + v & short, little-endian byte order\\ + X & back up a byte\\ + x & null byte\\ + Z & ASCII string (null padded, count is width)\\ +" +=end + end + + def test_upcase + assert_equal(S("HELLO"), S("hello").upcase) + assert_equal(S("HELLO"), S("hello").upcase) + assert_equal(S("HELLO"), S("HELLO").upcase) + assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase) + end + + def test_upcase! + a = S("hello") + b = a.dup + assert_equal(S("HELLO"), a.upcase!) + assert_equal(S("HELLO"), a) + assert_equal(S("hello"), b) + + a = S("HELLO") + assert_nil(a.upcase!) + assert_equal(S("HELLO"), a) + end + + def test_upto + a = S("aa") + start = S("aa") + count = 0 + assert_equal(S("aa"), a.upto(S("zz")) {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(676, count) + end + end |