diff options
Diffstat (limited to 'test/ruby/test_array.rb')
| -rw-r--r-- | test/ruby/test_array.rb | 474 |
1 files changed, 398 insertions, 76 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index bdb603bec5..8d48b86ea9 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -41,6 +41,7 @@ class TestArray < Test::Unit::TestCase assert_equal(2, x[2]) assert_equal([1, 2, 3], x[1..3]) assert_equal([1, 2, 3], x[1,3]) + assert_equal([3, 4, 5], x[3..]) x[0, 2] = 10 assert_equal([10, 2, 3, 4, 5], x) @@ -145,14 +146,17 @@ class TestArray < Test::Unit::TestCase assert_equal(1, x.first) assert_equal([1], x.first(1)) assert_equal([1, 2, 3], x.first(3)) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.first(1, 2)} assert_equal(5, x.last) assert_equal([5], x.last(1)) assert_equal([3, 4, 5], x.last(3)) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.last(1, 2)} assert_equal(1, x.shift) assert_equal([2, 3, 4], x.shift(3)) assert_equal([5], x) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.first(1, 2)} assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4)) assert_equal([1, 2, 3, 4, 5], x.unshift(1)) @@ -161,6 +165,7 @@ class TestArray < Test::Unit::TestCase assert_equal(5, x.pop) assert_equal([3, 4], x.pop(2)) assert_equal([1, 2], x) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.pop(1, 2)} assert_equal([1, 2, 3, 4], x.push(3, 4)) assert_equal([1, 2, 3, 4, 5], x.push(5)) @@ -170,6 +175,7 @@ class TestArray < Test::Unit::TestCase def test_find_all_0 assert_respond_to([], :find_all) assert_respond_to([], :select) # Alias + assert_respond_to([], :filter) # Alias assert_equal([], [].find_all{ |obj| obj == "foo"}) x = ["foo", "bar", "baz", "baz", 1, 2, 3, 3, 4] @@ -198,6 +204,8 @@ class TestArray < Test::Unit::TestCase assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10}) 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}) + assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3..){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3...){|i| i+10}) end # From rubicon @@ -224,6 +232,13 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[], @cls[ 1, 2, 3 ] & @cls[ 4, 5, 6 ]) end + def test_AND_big_array # '&' + assert_equal(@cls[1, 3], @cls[ 1, 1, 3, 5 ]*64 & @cls[ 1, 2, 3 ]*64) + assert_equal(@cls[], @cls[ 1, 1, 3, 5 ]*64 & @cls[ ]) + assert_equal(@cls[], @cls[ ] & @cls[ 1, 2, 3 ]*64) + assert_equal(@cls[], @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64) + end + def test_MUL # '*' assert_equal(@cls[], @cls[]*3) assert_equal(@cls[1, 1, 1], @cls[1]*3) @@ -247,19 +262,45 @@ class TestArray < Test::Unit::TestCase 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_MINUS_big_array # '-' + assert_equal(@cls[1]*64, @cls[1, 2, 3, 4, 5]*64 - @cls[2, 3, 4, 5]*64) + assert_equal(@cls[1, 1, 1, 1]*64, @cls[1, 2, 1, 3, 1, 4, 1, 5]*64 - @cls[2, 3, 4, 5]*64) + a = @cls[] + 1000.times { a << 1 } + assert_equal(1000, a.length) + assert_equal(@cls[1] * 1000, a - @cls[2]) + end + + def test_difference + assert_equal(@cls[], @cls[1].difference(@cls[1])) + assert_equal(@cls[1], @cls[1, 2, 3, 4, 5].difference(@cls[2, 3, 4, 5])) + assert_equal(@cls[1, 1], @cls[1, 2, 1].difference(@cls[2])) + assert_equal(@cls[1, 1, 1, 1], @cls[1, 2, 1, 3, 1, 4, 1, 5].difference(@cls[2, 3, 4, 5])) + assert_equal(@cls[], @cls[1, 2, 3, 4].difference(@cls[1], @cls[2], @cls[3], @cls[4])) + a = [1] + assert_equal(@cls[1], a.difference(@cls[2], @cls[2])) + assert_equal(@cls[], a.difference(@cls[1])) + assert_equal(@cls[1], a) + end + + def test_difference_big_array + assert_equal(@cls[1]*64, (@cls[1, 2, 3, 4, 5] * 64).difference(@cls[2, 3, 4] * 64, @cls[3, 5] * 64)) + assert_equal(@cls[1, 1, 1, 1]*64, (@cls[1, 2, 1, 3, 1, 4, 1, 5] * 64).difference(@cls[2, 3, 4, 5] * 64)) + a = @cls[1] * 1000 + assert_equal(@cls[1] * 1000, a.difference(@cls[2], @cls[2])) + assert_equal(@cls[], a.difference(@cls[1])) + assert_equal(@cls[1] * 1000, a) + end + def test_LSHIFT # '<<' a = @cls[] a << 1 @@ -333,12 +374,11 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[99], a[-2..-2]) assert_equal(@cls[10, 11, 12], a[9..11]) + assert_equal(@cls[98, 99, 100], a[97..]) assert_equal(@cls[10, 11, 12], a[-91..-89]) + assert_equal(@cls[98, 99, 100], a[-3..]) 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']} @@ -394,33 +434,33 @@ class TestArray < Test::Unit::TestCase 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) + assert_equal(@cls[nil] + @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) + assert_equal(@cls[*(0..9).to_a] + @cls[nil] + @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) + assert_equal(@cls[*(0..98).to_a] + @cls[nil], a) a = @cls[*(0..99).to_a] assert_equal(nil, a[-10, 10] = nil) - assert_equal(@cls[*(0..89).to_a], a) + assert_equal(@cls[*(0..89).to_a] + @cls[nil], a) a = @cls[*(0..99).to_a] assert_equal(nil, a[0,1000] = nil) - assert_equal(@cls[] , a) + assert_equal(@cls[nil] , 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 + assert_equal(@cls[*(0..9).to_a] + @cls[nil] + @cls[*(20..99).to_a], a) + + a = @cls[*(0..99).to_a] + assert_equal(nil, a[10..] = nil) + assert_equal(@cls[*(0..9).to_a] + @cls[nil], a) a = @cls[1, 2, 3] a[1, 0] = a @@ -443,6 +483,17 @@ class TestArray < Test::Unit::TestCase assert_equal([1, 2], a) end + def test_append + a = @cls[1, 2, 3] + assert_equal(@cls[1, 2, 3, 4, 5], a.append(4, 5)) + assert_equal(@cls[1, 2, 3, 4, 5, nil], a.append(nil)) + + a.append + assert_equal @cls[1, 2, 3, 4, 5, nil], a + a.append 6, 7 + assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a + end + def test_assoc a1 = @cls[*%w( cat feline )] a2 = @cls[*%w( dog canine )] @@ -500,10 +551,11 @@ class TestArray < Test::Unit::TestCase 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 Enumerator, @cls[1, 2, 3].collect + + assert_raise(ArgumentError) { + assert_equal([[1, 2, 3]], [[1, 2, 3]].collect(&->(a, b, c) {[a, b, c]})) + } end # also update map! @@ -571,7 +623,7 @@ class TestArray < Test::Unit::TestCase assert_equal([4, 5, 4, 5, 4, 5], b) assert_raise(TypeError) { [0].concat(:foo) } - assert_raise(RuntimeError) { [0].freeze.concat(:foo) } + assert_raise(FrozenError) { [0].freeze.concat(:foo) } end def test_count @@ -702,7 +754,7 @@ class TestArray < Test::Unit::TestCase a = @cls[] i = 0 a.each { |e| - assert_equal(a[i], e) + assert(false, "Never get here") i += 1 } assert_equal(0, i) @@ -722,7 +774,7 @@ class TestArray < Test::Unit::TestCase a = @cls[] i = 0 a.each_index { |ind| - assert_equal(i, ind) + assert(false, "Never get here") i += 1 } assert_equal(0, i) @@ -1074,6 +1126,7 @@ class TestArray < Test::Unit::TestCase assert_equal(Encoding::US_ASCII, [1, [u]].join.encoding) assert_equal(Encoding::UTF_8, [u, [e]].join.encoding) assert_equal(Encoding::UTF_8, [u, [1]].join.encoding) + assert_equal(Encoding::UTF_8, [Struct.new(:to_str).new(u)].join.encoding) bug5379 = '[ruby-core:39776]' assert_equal(Encoding::US_ASCII, [[], u, nil].join.encoding, bug5379) assert_equal(Encoding::UTF_8, [[], "\u3042", nil].join.encoding, bug5379) @@ -1204,13 +1257,18 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[], a) end + def test_prepend + a = @cls[] + assert_equal(@cls['cat'], a.prepend('cat')) + assert_equal(@cls['dog', 'cat'], a.prepend('dog')) + assert_equal(@cls[nil, 'dog', 'cat'], a.prepend(nil)) + assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.prepend(@cls[1, 2])) + 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 @@ -1250,6 +1308,65 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[7, 8, 9, 10], a, bug2545) end + def test_shared_array_reject! + c = [] + b = [1, 2, 3, 4] + 3.times do + a = b.dup + c << a.dup + + begin + a.reject! do |x| + case x + when 2 then true + when 3 then raise StandardError, 'Oops' + else false + end + end + rescue StandardError + end + + c << a.dup + end + + bug90781 = '[ruby-core:90781]' + assert_equal [[1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4]], c, bug90781 + end + + def test_iseq_shared_array_reject! + c = [] + 3.times do + a = [1, 2, 3, 4] + c << a.dup + + begin + a.reject! do |x| + case x + when 2 then true + when 3 then raise StandardError, 'Oops' + else false + end + end + rescue StandardError + end + + c << a.dup + end + + bug90781 = '[ruby-core:90781]' + assert_equal [[1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4], + [1, 2, 3, 4], + [1, 3, 4]], c, bug90781 + end + def test_replace a = @cls[ 1, 2, 3] a_id = a.__id__ @@ -1260,10 +1377,10 @@ class TestArray < Test::Unit::TestCase fa = a.dup.freeze assert_nothing_raised(RuntimeError) { a.replace(a) } - assert_raise(RuntimeError) { fa.replace(fa) } + assert_raise(FrozenError) { fa.replace(fa) } assert_raise(ArgumentError) { fa.replace() } assert_raise(TypeError) { a.replace(42) } - assert_raise(RuntimeError) { fa.replace(42) } + assert_raise(FrozenError) { fa.replace(42) } end def test_reverse @@ -1277,9 +1394,6 @@ class TestArray < Test::Unit::TestCase 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 @@ -1359,14 +1473,14 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[99], a.slice(-2..-2)) assert_equal(@cls[10, 11, 12], a.slice(9..11)) + assert_equal(@cls[98, 99, 100], a.slice(97..)) + assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) assert_nil(a.slice(-101..-1)) + assert_nil(a.slice(-101..)) 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 @@ -1407,6 +1521,8 @@ class TestArray < Test::Unit::TestCase assert_equal(nil, a.slice!(-6,2)) assert_equal(@cls[1, 2, 3, 4, 5], a) + assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug") + assert_raise(ArgumentError) { @cls[1].slice! } assert_raise(ArgumentError) { @cls[1].slice!(0, 0, 0) } end @@ -1491,7 +1607,7 @@ class TestArray < Test::Unit::TestCase o2 = o1.clone ary << o1 << o2 orig = ary.dup - assert_raise(RuntimeError, "frozen during comparison") {ary.sort!} + assert_raise(FrozenError, "frozen during comparison") {ary.sort!} assert_equal(orig, ary, "must not be modified once frozen") end @@ -1532,7 +1648,7 @@ class TestArray < Test::Unit::TestCase def o.to_ary foo_bar() end - assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message) + assert_raise_with_message(NoMethodError, /foo_bar/) {a.concat(o)} end def test_to_s @@ -1555,15 +1671,17 @@ class TestArray < Test::Unit::TestCase $, = nil end + StubToH = [ + [:key, :value], + Object.new.tap do |kvp| + def kvp.to_ary + [:obtained, :via_to_ary] + end + end, + ] + def test_to_h - kvp = Object.new - def kvp.to_ary - [:obtained, :via_to_ary] - end - array = [ - [:key, :value], - kvp, - ] + array = StubToH assert_equal({key: :value, obtained: :via_to_ary}, array.to_h) e = assert_raise(TypeError) { @@ -1578,6 +1696,27 @@ class TestArray < Test::Unit::TestCase assert_equal "wrong array length at 2 (expected 2, was 1)", e.message end + def test_to_h_block + array = StubToH + assert_equal({"key" => "value", "obtained" => "via_to_ary"}, + array.to_h {|k, v| [k.to_s, v.to_s]}) + + assert_equal({first_one: :ok, not_ok: :ng}, + [[:first_one, :ok], :not_ok].to_h {|k, v| [k, v || :ng]}) + + e = assert_raise(TypeError) { + [[:first_one, :ok], :not_ok].to_h {|k, v| v ? [k, v] : k} + } + assert_equal "wrong element type Symbol at 1 (expected array)", e.message + array = [1] + k = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {array.to_h {k}} + e = assert_raise(ArgumentError) { + [[:first_one, :ok], [1, 2], [:not_ok]].to_h {|kv| kv} + } + assert_equal "wrong array length at 2 (expected 2, was 1)", e.message + end + def test_min assert_equal(1, [1, 2, 3, 1, 2].min) assert_equal(3, [1, 2, 3, 1, 2].min {|a,b| b <=> a }) @@ -1592,6 +1731,12 @@ class TestArray < Test::Unit::TestCase ary.min(2) {|a,b| a.length <=> b.length }) assert_equal([13, 14], [20, 32, 32, 21, 30, 25, 29, 13, 14].min(2)) assert_equal([2, 4, 6, 7], [2, 4, 8, 6, 7].min(4)) + + class << (obj = Object.new) + def <=>(x) 1 <=> x end + def coerce(x) [x, 1] end + end + assert_same(obj, [obj, 1.0].min) end def test_max @@ -1607,6 +1752,12 @@ class TestArray < Test::Unit::TestCase assert_equal(%w[albatross horse], ary.max(2) {|a,b| a.length <=> b.length }) assert_equal([3, 2], [0, 0, 0, 0, 0, 0, 1, 3, 2].max(2)) + + class << (obj = Object.new) + def <=>(x) 1 <=> x end + def coerce(x) [x, 1] end + end + assert_same(obj, [obj, 1.0].max) end def test_uniq @@ -1723,7 +1874,7 @@ class TestArray < Test::Unit::TestCase f = a.dup.freeze assert_raise(ArgumentError) { a.uniq!(1) } assert_raise(ArgumentError) { f.uniq!(1) } - assert_raise(RuntimeError) { f.uniq! } + assert_raise(FrozenError) { f.uniq! } assert_nothing_raised do a = [ {c: "b"}, {c: "r"}, {c: "w"}, {c: "g"}, {c: "g"} ] @@ -1769,7 +1920,7 @@ class TestArray < Test::Unit::TestCase def test_uniq_bang_with_freeze ary = [1,2] orig = ary.dup - assert_raise(RuntimeError, "frozen during comparison") { + assert_raise(FrozenError, "frozen during comparison") { ary.uniq! {|v| ary.freeze; 1} } assert_equal(orig, ary, "must not be modified once frozen") @@ -1783,6 +1934,17 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2])) end + def test_unshift_frozen + bug15952 = '[Bug #15952]' + assert_raise(FrozenError, bug15952) do + a = [1] * 100 + b = a[4..-1] + a.replace([1]) + b.freeze + b.unshift("a") + end + end + def test_OR # '|' assert_equal(@cls[], @cls[] | @cls[]) assert_equal(@cls[1], @cls[1] | @cls[]) @@ -1817,13 +1979,86 @@ class TestArray < Test::Unit::TestCase assert_equal([obj1], [obj1]|[obj2]) end + def test_OR_big_in_order + obj1, obj2 = Class.new do + attr_reader :name + def initialize(name) @name = name; end + def inspect; "test_OR_in_order(#{@name})"; end + def hash; 0; end + def eql?(a) true; end + break [new("1"), new("2")] + end + assert_equal([obj1], [obj1]*64|[obj2]*64) + end + + def test_OR_big_array # '|' + assert_equal(@cls[1,2], @cls[1]*64 | @cls[2]*64) + assert_equal(@cls[1,2], @cls[1, 2]*64 | @cls[1, 2]*64) + + a = (1..64).to_a + b = (1..128).to_a + c = a | b + assert_equal(c, b) + assert_not_same(c, b) + assert_equal((1..64).to_a, a) + assert_equal((1..128).to_a, b) + end + + def test_union + assert_equal(@cls[], @cls[].union(@cls[])) + assert_equal(@cls[1], @cls[1].union(@cls[])) + assert_equal(@cls[1], @cls[].union(@cls[1])) + assert_equal(@cls[1], @cls[].union(@cls[], @cls[1])) + assert_equal(@cls[1], @cls[1].union(@cls[1])) + assert_equal(@cls[1], @cls[1].union(@cls[1], @cls[1], @cls[1])) + + assert_equal(@cls[1,2], @cls[1].union(@cls[2])) + assert_equal(@cls[1,2], @cls[1, 1].union(@cls[2, 2])) + assert_equal(@cls[1,2], @cls[1, 2].union(@cls[1, 2])) + assert_equal(@cls[1,2], @cls[1, 1].union(@cls[1, 1], @cls[1, 2], @cls[2, 1], @cls[2, 2, 2])) + + a = %w(a b c) + b = %w(a b c d e) + c = a.union(b) + assert_equal(c, b) + assert_not_same(c, b) + assert_equal(%w(a b c), a) + assert_equal(%w(a b c d e), b) + assert(a.none?(&:frozen?)) + assert(b.none?(&:frozen?)) + assert(c.none?(&:frozen?)) + end + + def test_union_big_array + assert_equal(@cls[1,2], (@cls[1]*64).union(@cls[2]*64)) + assert_equal(@cls[1,2,3], (@cls[1, 2]*64).union(@cls[1, 2]*64, @cls[3]*60)) + + a = (1..64).to_a + b = (1..128).to_a + c = a | b + assert_equal(c, b) + assert_not_same(c, b) + assert_equal((1..64).to_a, a) + assert_equal((1..128).to_a, b) + end + def test_combination - assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a) - assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a) - assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a) - assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a) - assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a) - assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a) + a = @cls[] + assert_equal(1, a.combination(0).size) + assert_equal(0, a.combination(1).size) + a = @cls[1,2,3,4] + assert_equal(1, a.combination(0).size) + assert_equal(4, a.combination(1).size) + assert_equal(6, a.combination(2).size) + assert_equal(4, a.combination(3).size) + assert_equal(1, a.combination(4).size) + assert_equal(0, a.combination(5).size) + assert_equal(@cls[[]], a.combination(0).to_a) + assert_equal(@cls[[1],[2],[3],[4]], a.combination(1).to_a) + assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a.combination(2).to_a) + assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a.combination(3).to_a) + assert_equal(@cls[[1,2,3,4]], a.combination(4).to_a) + assert_equal(@cls[], a.combination(5).to_a) end def test_product @@ -1855,7 +2090,16 @@ class TestArray < Test::Unit::TestCase end def test_permutation + a = @cls[] + assert_equal(1, a.permutation(0).size) + assert_equal(0, a.permutation(1).size) a = @cls[1,2,3] + assert_equal(1, a.permutation(0).size) + assert_equal(3, a.permutation(1).size) + assert_equal(6, a.permutation(2).size) + assert_equal(6, a.permutation(3).size) + assert_equal(0, a.permutation(4).size) + assert_equal(6, a.permutation.size) assert_equal(@cls[[]], a.permutation(0).to_a) assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort) assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], @@ -1880,15 +2124,24 @@ class TestArray < Test::Unit::TestCase def test_permutation_stack_error bug9932 = '[ruby-core:63103] [Bug #9932]' - assert_separately([], <<-"end;", timeout: 30) # do - assert_nothing_raised(SystemStackError, "#{bug9932}") do + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 30) + bug = "#{bug9932}" + begin; + assert_nothing_raised(SystemStackError, bug) do assert_equal(:ok, Array.new(100_000, nil).permutation {break :ok}) end end; end def test_repeated_permutation + a = @cls[] + assert_equal(1, a.repeated_permutation(0).size) + assert_equal(0, a.repeated_permutation(1).size) a = @cls[1,2] + assert_equal(1, a.repeated_permutation(0).size) + assert_equal(2, a.repeated_permutation(1).size) + assert_equal(4, a.repeated_permutation(2).size) + assert_equal(8, a.repeated_permutation(3).size) assert_equal(@cls[[]], a.repeated_permutation(0).to_a) assert_equal(@cls[[1],[2]], a.repeated_permutation(1).to_a.sort) assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @@ -1913,7 +2166,8 @@ class TestArray < Test::Unit::TestCase end def test_repeated_permutation_stack_error - assert_separately([], <<-"end;", timeout: 30) # do + assert_separately([], "#{<<-"begin;"}\n#{<<~'end;'}", timeout: 30) + begin; assert_nothing_raised(SystemStackError) do assert_equal(:ok, Array.new(100_000, nil).repeated_permutation(500_000) {break :ok}) end @@ -1921,7 +2175,15 @@ class TestArray < Test::Unit::TestCase end def test_repeated_combination + a = @cls[] + assert_equal(1, a.repeated_combination(0).size) + assert_equal(0, a.repeated_combination(1).size) a = @cls[1,2,3] + assert_equal(1, a.repeated_combination(0).size) + assert_equal(3, a.repeated_combination(1).size) + assert_equal(6, a.repeated_combination(2).size) + assert_equal(10, a.repeated_combination(3).size) + assert_equal(15, a.repeated_combination(4).size) assert_equal(@cls[[]], a.repeated_combination(0).to_a) assert_equal(@cls[[1],[2],[3]], a.repeated_combination(1).to_a.sort) assert_equal(@cls[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]], @@ -1950,7 +2212,8 @@ class TestArray < Test::Unit::TestCase end def test_repeated_combination_stack_error - assert_separately([], <<-"end;", timeout: 20) # do + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 20) + begin; assert_nothing_raised(SystemStackError) do assert_equal(:ok, Array.new(100_000, nil).repeated_combination(500_000) {break :ok}) end @@ -2013,13 +2276,14 @@ class TestArray < Test::Unit::TestCase assert_raise(IndexError) { [0][LONGP] = 2 } assert_raise(IndexError) { [0][(LONGP + 1) / 2 - 1] = 2 } assert_raise(IndexError) { [0][LONGP..-1] = 2 } + assert_raise(IndexError) { [0][LONGP..] = 2 } a = [0] a[2] = 4 assert_equal([0, nil, 4], a) assert_raise(ArgumentError) { [0][0, 0, 0] = 0 } assert_raise(ArgumentError) { [0].freeze[0, 0, 0] = 0 } assert_raise(TypeError) { [0][:foo] = 0 } - assert_raise(RuntimeError) { [0].freeze[:foo] = 0 } + assert_raise(FrozenError) { [0].freeze[:foo] = 0 } end def test_first2 @@ -2044,12 +2308,13 @@ class TestArray < Test::Unit::TestCase end def test_unshift_error - assert_raise(RuntimeError) { [].freeze.unshift('cat') } - assert_raise(RuntimeError) { [].freeze.unshift() } + assert_raise(FrozenError) { [].freeze.unshift('cat') } + assert_raise(FrozenError) { [].freeze.unshift() } end def test_aref assert_raise(ArgumentError) { [][0, 0, 0] } + assert_raise(TypeError) { [][(1..10).step(2)] } end def test_fetch @@ -2102,9 +2367,11 @@ class TestArray < Test::Unit::TestCase assert_equal([0], a.insert(1)) assert_equal([0, 1], a.insert(1, 1)) assert_raise(ArgumentError) { a.insert } + assert_raise(TypeError) { a.insert(Object.new) } assert_equal([0, 1, 2], a.insert(-1, 2)) assert_equal([0, 1, 3, 2], a.insert(-2, 3)) - assert_raise(RuntimeError) { [0].freeze.insert(0)} + assert_raise_with_message(IndexError, /-6/) { a.insert(-6, 4) } + assert_raise(FrozenError) { [0].freeze.insert(0)} assert_raise(ArgumentError) { [0].freeze.insert } end @@ -2163,6 +2430,11 @@ class TestArray < Test::Unit::TestCase } assert_equal(9, r) assert_equal(@cls[7, 8, 9, 10], a, bug10722) + + bug13053 = '[ruby-core:78739] [Bug #13053] Array#select! can resize to negative size' + a = @cls[ 1, 2, 3, 4, 5 ] + a.select! {|i| a.clear if i == 5; false } + assert_equal(0, a.size, bug13053) end # also select! @@ -2180,6 +2452,25 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[4, 5], a) end + def test_filter + assert_equal([0, 2], [0, 1, 2, 3].filter {|x| x % 2 == 0 }) + end + + # alias for select! + def test_filter! + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(nil, a.filter! { true }) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.filter! { false }) + assert_equal(@cls[], a) + + a = @cls[ 1, 2, 3, 4, 5 ] + assert_equal(a, a.filter! { |i| i > 3 }) + assert_equal(@cls[4, 5], a) + end + def test_delete2 a = [0] * 1024 + [1] + [0] * 1024 a.delete(0) @@ -2280,8 +2571,8 @@ class TestArray < Test::Unit::TestCase assert_raise(ArgumentError) { a.flatten!(1, 2) } assert_raise(TypeError) { a.flatten!(:foo) } assert_raise(ArgumentError) { f.flatten!(1, 2) } - assert_raise(RuntimeError) { f.flatten! } - assert_raise(RuntimeError) { f.flatten!(:foo) } + assert_raise(FrozenError) { f.flatten! } + assert_raise(FrozenError) { f.flatten!(:foo) } end def test_shuffle @@ -2337,6 +2628,13 @@ class TestArray < Test::Unit::TestCase end ary = (0...10000).to_a assert_equal(ary.rotate, ary.shuffle(random: gen_to_int)) + + assert_raise(NoMethodError) { + ary.shuffle(random: Object.new) + } + assert_raise(NoMethodError) { + ary.shuffle!(random: Object.new) + } end def test_sample @@ -2437,6 +2735,10 @@ class TestArray < Test::Unit::TestCase end ary = (0...10000).to_a assert_equal(5000, ary.sample(random: gen_to_int)) + + assert_raise(NoMethodError) { + ary.sample(random: Object.new) + } end def test_cycle @@ -2453,6 +2755,9 @@ class TestArray < Test::Unit::TestCase a = [] [0, 1, 2].cycle(3) {|i| a << i } assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2], a) + + assert_equal(Float::INFINITY, a.cycle.size) + assert_equal(27, a.cycle(3).size) end def test_reverse_each2 @@ -2472,11 +2777,18 @@ class TestArray < Test::Unit::TestCase def test_combination_clear bug9939 = '[ruby-core:63149] [Bug #9939]' - assert_ruby_status([], <<-'end;', bug9939) - 100_000.times {Array.new(1000)} + assert_nothing_raised(bug9939) { a = [*0..100] a.combination(3) {|*,x| a.clear} - end; + } + + bug13052 = '[ruby-core:78738] [Bug #13052] Array#combination segfaults if the Array is modified during iteration' + assert_nothing_raised(bug13052) { + a = [*0..100] + a.combination(1) { a.clear } + a = [*0..100] + a.repeated_combination(1) { a.clear } + } end def test_product2 @@ -2494,8 +2806,8 @@ class TestArray < Test::Unit::TestCase def test_array_subclass assert_equal(Array2, Array2[1,2,3].uniq.class, "[ruby-dev:34581]") - assert_equal(Array2, Array2[1,2][0,1].class) # embeded - assert_equal(Array2, Array2[*(1..100)][1..99].class) #not embeded + assert_equal(Array2, Array2[1,2][0,1].class) # embedded + assert_equal(Array2, Array2[*(1..100)][1..99].class) #not embedded end def test_inspect @@ -2598,7 +2910,7 @@ class TestArray < Test::Unit::TestCase assert_equal([], a.rotate!(13)) assert_equal([], a.rotate!(-13)) a = [].freeze - assert_raise_with_message(RuntimeError, /can\'t modify frozen/) {a.rotate!} + assert_raise_with_message(FrozenError, /can\'t modify frozen/) {a.rotate!} a = [1,2,3] assert_raise(ArgumentError) { a.rotate!(1, 1) } end @@ -2708,21 +3020,24 @@ class TestArray < Test::Unit::TestCase Bug11235 = '[ruby-dev:49043] [Bug #11235]' def test_push_over_ary_max - assert_separately(['-', ARY_MAX.to_s, Bug11235], <<-"end;", timeout: 20) + assert_separately(['-', ARY_MAX.to_s, Bug11235], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 30) + begin; a = Array.new(ARGV[0].to_i) assert_raise(IndexError, ARGV[1]) {0x1000.times {a.push(1)}} end; end def test_unshift_over_ary_max - assert_separately(['-', ARY_MAX.to_s, Bug11235], <<-"end;") + assert_separately(['-', ARY_MAX.to_s, Bug11235], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; a = Array.new(ARGV[0].to_i) assert_raise(IndexError, ARGV[1]) {0x1000.times {a.unshift(1)}} end; end def test_splice_over_ary_max - assert_separately(['-', ARY_MAX.to_s, Bug11235], <<-"end;") + assert_separately(['-', ARY_MAX.to_s, Bug11235], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; a = Array.new(ARGV[0].to_i) assert_raise(IndexError, ARGV[1]) {a[0, 0] = Array.new(0x1000)} end; @@ -2736,8 +3051,8 @@ class TestArray < Test::Unit::TestCase assert_raise(TypeError) {h.dig(1, 0)} end - FIXNUM_MIN = -(1 << (8 * RbConfig::SIZEOF['long'] - 2)) - FIXNUM_MAX = (1 << (8 * RbConfig::SIZEOF['long'] - 2)) - 1 + FIXNUM_MIN = RbConfig::LIMITS['FIXNUM_MIN'] + FIXNUM_MAX = RbConfig::LIMITS['FIXNUM_MAX'] def assert_typed_equal(e, v, cls, msg=nil) assert_kind_of(cls, v, msg) @@ -2810,13 +3125,20 @@ class TestArray < Test::Unit::TestCase assert_float_equal(large_number+(small_number*10), [large_number, *[small_number]*10].sum) assert_float_equal(large_number+(small_number*10), [large_number/1r, *[small_number]*10].sum) assert_float_equal(large_number+(small_number*11), [small_number, large_number/1r, *[small_number]*10].sum) + assert_float_equal(small_number, [large_number, small_number, -large_number].sum) + assert_equal(+Float::INFINITY, [+Float::INFINITY].sum) + assert_equal(+Float::INFINITY, [0.0, +Float::INFINITY].sum) + assert_equal(+Float::INFINITY, [+Float::INFINITY, 0.0].sum) + assert_equal(-Float::INFINITY, [-Float::INFINITY].sum) + assert_equal(-Float::INFINITY, [0.0, -Float::INFINITY].sum) + assert_equal(-Float::INFINITY, [-Float::INFINITY, 0.0].sum) + assert_predicate([-Float::INFINITY, Float::INFINITY].sum, :nan?) assert_equal("abc", ["a", "b", "c"].sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].sum([])) - assert_separately(%w[-rmathn], <<-EOS, ignore_stderr: true) - assert_equal(6, [1r, 2, 3r].sum) - EOS + assert_raise(TypeError) {[0].sum("")} + assert_raise(TypeError) {[1].sum("")} end private |
