diff options
Diffstat (limited to 'test/ruby/test_array.rb')
| -rw-r--r-- | test/ruby/test_array.rb | 625 |
1 files changed, 557 insertions, 68 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index b4115ac5dd..26bf942c61 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1,5 +1,6 @@ +# coding: US-ASCII +# frozen_string_literal: false require 'test/unit' -require_relative 'envutil' class TestArray < Test::Unit::TestCase def setup @@ -40,13 +41,14 @@ class TestArray < Test::Unit::TestCase assert_equal([1, 2, 3], x[1,3]) x[0, 2] = 10 - assert(x[0] == 10 && x[1] == 2) + assert_equal([10, 2, 3, 4, 5], x) x[0, 0] = -1 - assert(x[0] == -1 && x[1] == 10) + assert_equal([-1, 10, 2, 3, 4, 5], x) x[-1, 1] = 20 - assert(x[-1] == 20 && x.pop == 20) + assert_equal(20, x[-1]) + assert_equal(20, x.pop) end def test_array_andor_0 @@ -96,7 +98,7 @@ class TestArray < Test::Unit::TestCase end def test_misc_0 - assert(defined? "a".chomp) + assert(defined? "a".chomp, '"a".chomp is not defined') assert_equal(["a", "b", "c"], "abc".scan(/./)) assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/)) # non-greedy match @@ -425,6 +427,18 @@ class TestArray < Test::Unit::TestCase a = @cls[1, 2, 3] a[-1, 0] = a assert_equal([1, 2, 1, 2, 3, 3], a) + + a = @cls[] + a[5,0] = [5] + assert_equal([nil, nil, nil, nil, nil, 5], a) + + a = @cls[1] + a[1,0] = [2] + assert_equal([1, 2], a) + + a = @cls[1] + a[1,1] = [2] + assert_equal([1, 2], a) end def test_assoc @@ -463,20 +477,16 @@ class TestArray < Test::Unit::TestCase def test_clone for taint in [ false, true ] - for untrust in [ false, true ] - for frozen in [ false, true ] - a = @cls[*(0..99).to_a] - a.taint if taint - a.untrust if untrust - a.freeze if frozen - b = a.clone - - assert_equal(a, b) - assert_not_equal(a.__id__, b.__id__) - assert_equal(a.frozen?, b.frozen?) - assert_equal(a.untrusted?, b.untrusted?) - assert_equal(a.tainted?, b.tainted?) - end + 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_not_equal(a.__id__, b.__id__) + assert_equal(a.frozen?, b.frozen?) + assert_equal(a.tainted?, b.tainted?) end end end @@ -563,6 +573,29 @@ class TestArray < Test::Unit::TestCase assert_equal(3, a.count {|x| x % 2 == 1 }) assert_equal(2, a.count(1) {|x| x % 2 == 1 }) assert_raise(ArgumentError) { a.count(0, 1) } + + bug8654 = '[ruby-core:56072]' + assert_in_out_err [], <<-EOS, ["0"], [], bug8654 + a1 = [] + a2 = Array.new(100) { |i| i } + a2.count do |i| + p i + a2.replace(a1) if i == 0 + end + EOS + + assert_in_out_err [], <<-EOS, ["[]", "0"], [], bug8654 + ARY = Array.new(100) { |i| i } + class Fixnum + alias old_equal == + def == other + ARY.replace([]) if self.equal?(0) + p ARY + self.equal?(other) + end + end + p ARY.count(42) + EOS end def test_delete @@ -585,6 +618,14 @@ class TestArray < Test::Unit::TestCase a = @cls[*('cab'..'cat').to_a] assert_equal(99, a.delete('cup') { 99 } ) assert_equal(@cls[*('cab'..'cat').to_a], a) + + o = Object.new + def o.==(other); true; end + o2 = Object.new + def o2.==(other); true; end + a = @cls[1, o, o2, 2] + assert_equal(o2, a.delete(42)) + assert_equal([1, 2], a) end def test_delete_at @@ -621,7 +662,7 @@ class TestArray < Test::Unit::TestCase bug2545 = '[ruby-core:27366]' a = @cls[ 5, 6, 7, 8, 9, 10 ] - assert_equal(9, a.delete_if {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7}) + assert_equal(9, a.delete_if {|i| break i if i > 8; i < 7}) assert_equal(@cls[7, 8, 9, 10], a, bug2545) end @@ -727,25 +768,52 @@ class TestArray < Test::Unit::TestCase 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.flatten(1)) assert_equal(@cls[], @cls[].flatten) assert_equal(@cls[], @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten) + end + def test_flatten_wrong_argument assert_raise(TypeError, "[ruby-dev:31197]") { [[]].flatten("") } + end + def test_flatten_taint a6 = @cls[[1, 2], 3] a6.taint - a6.untrust a7 = a6.flatten assert_equal(true, a7.tainted?) - assert_equal(true, a7.untrusted?) + end + def test_flatten_level0 a8 = @cls[[1, 2], 3] a9 = a8.flatten(0) assert_equal(a8, a9) assert_not_same(a8, a9) end + def test_flatten_splat + bug10748 = '[ruby-core:67637] [Bug #10748]' + o = Object.new + o.singleton_class.class_eval do + define_method(:to_ary) do + raise bug10748 + end + end + a = @cls[@cls[o]] + assert_raise_with_message(RuntimeError, bug10748) {a.flatten} + assert_nothing_raised(RuntimeError, bug10748) {a.flatten(1)} + end + + def test_flattern_singleton_class + bug12738 = '[ruby-dev:49781] [Bug #12738]' + a = [[0]] + class << a + def m; end + end + assert_raise(NoMethodError, bug12738) { a.flatten.m } + end + def test_flatten! a1 = @cls[ 1, 2, 3] a2 = @cls[ 5, 6 ] @@ -758,16 +826,42 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten!) assert_nil(a5.flatten!(0), '[ruby-core:23382]') assert_equal(@cls[1, 2, 3, 4, 5, 6], a5) + end - assert_equal(@cls[], @cls[].flatten) + def test_flatten_empty! + assert_nil(@cls[].flatten!) assert_equal(@cls[], - @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten) + @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten!) + end + def test_flatten_level0! assert_nil(@cls[].flatten!(0), '[ruby-core:23382]') end + def test_flatten_splat! + bug10748 = '[ruby-core:67637] [Bug #10748]' + o = Object.new + o.singleton_class.class_eval do + define_method(:to_ary) do + raise bug10748 + end + end + a = @cls[@cls[o]] + assert_raise_with_message(RuntimeError, bug10748) {a.flatten!} + assert_nothing_raised(RuntimeError, bug10748) {a.flatten!(1)} + end + + def test_flattern_singleton_class! + bug12738 = '[ruby-dev:49781] [Bug #12738]' + a = [[0]] + class << a + def m; end + end + assert_nothing_raised(NameError, bug12738) { a.flatten!.m } + end + def test_flatten_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation o = Object.new def o.to_ary() callcc {|k| @cont = k; [1,2,3]} end begin @@ -780,8 +874,30 @@ class TestArray < Test::Unit::TestCase assert_match(/reentered/, e.message, '[ruby-dev:34798]') end + def test_flatten_respond_to_missing + bug11465 = '[ruby-core:70460] [Bug #11465]' + + obj = Class.new do + def respond_to_missing?(method, stuff) + return false if method == :to_ary + super + end + + def method_missing(*args) + super + end + end.new + + ex = nil + trace = TracePoint.new(:raise) do |tp| + ex = tp.raised_exception + end + trace.enable {[obj].flatten} + assert_nil(ex, bug11465) + end + def test_permutation_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = [1,2,3] @@ -798,7 +914,7 @@ class TestArray < Test::Unit::TestCase end def test_product_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = [1,2,3] @@ -815,7 +931,7 @@ class TestArray < Test::Unit::TestCase end def test_combination_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = [1,2,3] @@ -832,7 +948,7 @@ class TestArray < Test::Unit::TestCase end def test_repeated_permutation_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = [1,2,3] @@ -849,7 +965,7 @@ class TestArray < Test::Unit::TestCase end def test_repeated_combination_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = [1,2,3] @@ -871,6 +987,8 @@ class TestArray < Test::Unit::TestCase a3 = @cls[ 'dog', 'cat' ] assert_equal(a1.hash, a2.hash) assert_not_equal(a1.hash, a3.hash) + bug9231 = '[ruby-core:58993] [Bug #9231]' + assert_not_equal(false.hash, @cls[].hash, bug9231) end def test_include? @@ -929,23 +1047,18 @@ class TestArray < Test::Unit::TestCase $, = "" a = @cls[1, 2, 3] a.taint - a.untrust s = a.join assert_equal(true, s.tainted?) - assert_equal(true, s.untrusted?) bug5902 = '[ruby-core:42161]' - sep = ":".taint.untrust + sep = ":".taint s = @cls[].join(sep) assert_equal(false, s.tainted?, bug5902) - assert_equal(false, s.untrusted?, bug5902) s = @cls[1].join(sep) assert_equal(false, s.tainted?, bug5902) - assert_equal(false, s.untrusted?, bug5902) s = @cls[1, 2].join(sep) assert_equal(true, s.tainted?, bug5902) - assert_equal(true, s.untrusted?, bug5902) e = ''.force_encoding('EUC-JP') u = ''.force_encoding('UTF-8') @@ -1125,7 +1238,7 @@ class TestArray < Test::Unit::TestCase bug2545 = '[ruby-core:27366]' a = @cls[ 5, 6, 7, 8, 9, 10 ] - assert_equal(9, a.reject! {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7}) + assert_equal(9, a.reject! {|i| break i if i > 8; i < 7}) assert_equal(@cls[7, 8, 9, 10], a, bug2545) end @@ -1174,6 +1287,7 @@ class TestArray < Test::Unit::TestCase a = @cls[] i = 0 a.reverse_each { |e| + i += 1 assert(false, "Never get here") } assert_equal(0, i) @@ -1329,7 +1443,7 @@ class TestArray < Test::Unit::TestCase end def test_sort_with_callcc - respond_to?(:callcc, true) or require 'continuation' + need_continuation n = 1000 cont = nil ary = (1..100).to_a @@ -1357,6 +1471,32 @@ class TestArray < Test::Unit::TestCase end end + def test_sort_bang_with_freeze + ary = [] + o1 = Object.new + o1.singleton_class.class_eval { + define_method(:<=>) {|v| + ary.freeze + 1 + } + } + o2 = o1.dup + ary << o1 << o2 + orig = ary.dup + assert_raise(RuntimeError, "frozen during comparison") {ary.sort!} + assert_equal(orig, ary, "must not be modified once frozen") + end + + def test_short_heap_array_sort_bang_memory_leak + bug11332 = '[ruby-dev:49166] [Bug #11332]' + assert_no_memory_leak([], <<-PREP, <<-TEST, bug11332, limit: 1.3, timeout: 60) + def t; ary = [*1..5]; ary.pop(2); ary.sort!; end + 1.times {t} + PREP + 500000.times {t} + TEST + end + def test_to_a a = @cls[ 1, 2, 3 ] a_id = a.__id__ @@ -1407,6 +1547,29 @@ class TestArray < Test::Unit::TestCase $, = nil end + def test_to_h + kvp = Object.new + def kvp.to_ary + [:obtained, :via_to_ary] + end + array = [ + [:key, :value], + kvp, + ] + assert_equal({key: :value, obtained: :via_to_ary}, array.to_h) + + e = assert_raise(TypeError) { + [[:first_one, :ok], :not_ok].to_h + } + assert_equal "wrong element type Symbol at 1 (expected array)", e.message + array = [eval("class C\u{1f5ff}; self; end").new] + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {array.to_h} + e = assert_raise(ArgumentError) { + [[:first_one, :ok], [1, 2], [:not_ok]].to_h + } + assert_equal "wrong array length at 2 (expected 2, was 1)", e.message + end + def test_uniq a = [] b = a.uniq @@ -1443,6 +1606,18 @@ class TestArray < Test::Unit::TestCase assert_equal(d, c) assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq) + + a = %w(a a) + b = a.uniq + assert_equal(%w(a a), a) + assert(a.none?(&:frozen?)) + assert_equal(%w(a), b) + assert(b.none?(&:frozen?)) + + bug9340 = "[ruby-core:59457]" + ary = [bug9340, bug9340.dup, bug9340.dup] + assert_equal 1, ary.uniq.size + assert_same bug9340, ary.uniq[0] end def test_uniq_with_block @@ -1463,6 +1638,13 @@ class TestArray < Test::Unit::TestCase assert_equal([1,3], a) assert_equal([1], b) assert_not_same(a, b) + + a = %w(a a) + b = a.uniq {|v| v } + assert_equal(%w(a a), a) + assert(a.none?(&:frozen?)) + assert_equal(%w(a), b) + assert(b.none?(&:frozen?)) end def test_uniq! @@ -1509,6 +1691,13 @@ class TestArray < Test::Unit::TestCase a.sort_by!{|e| e[:c]} a.uniq! {|e| e[:c]} end + + a = %w(a a) + b = a.uniq + assert_equal(%w(a a), a) + assert(a.none?(&:frozen?)) + assert_equal(%w(a), b) + assert(b.none?(&:frozen?)) end def test_uniq_bang_with_block @@ -1530,6 +1719,21 @@ class TestArray < Test::Unit::TestCase b = a.uniq! {|v| v.even? } assert_equal([1,2], a) assert_equal(nil, b) + + a = %w(a a) + b = a.uniq! {|v| v } + assert_equal(%w(a), b) + assert_same(a, b) + assert b.none?(&:frozen?) + end + + def test_uniq_bang_with_freeze + ary = [1,2] + orig = ary.dup + assert_raise(RuntimeError, "frozen during comparison") { + ary.uniq! {|v| ary.freeze; 1} + } + assert_equal(orig, ary, "must not be modified once frozen") end def test_unshift @@ -1549,6 +1753,29 @@ class TestArray < Test::Unit::TestCase 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]) + + a = %w(a b c) + b = %w(a b c d e) + c = a | 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_OR_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]|[obj2]) end def test_combination @@ -1585,7 +1812,7 @@ class TestArray < Test::Unit::TestCase a = [] [1, 2].product([0, 1, 2, 3, 4][1, 4]) {|x| a << x } - assert(a.all?{|x| !x.include?(0) }) + a.all? {|x| assert_not_include(x, 0)} end def test_permutation @@ -1612,6 +1839,15 @@ class TestArray < Test::Unit::TestCase assert_equal(b, @cls[0, 1, 2, 3, 4][1, 4].permutation.to_a, bug3708) end + def test_permutation_stack_error + bug9932 = '[ruby-core:63103] [Bug #9932]' + assert_separately([], <<-"end;") # do + assert_nothing_raised(SystemStackError, "#{bug9932}") do + assert_equal(:ok, Array.new(100_000, nil).permutation {break :ok}) + end + end; + end + def test_repeated_permutation a = @cls[1,2] assert_equal(@cls[[]], a.repeated_permutation(0).to_a) @@ -1634,7 +1870,15 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[1, 2, 3, 4].repeated_permutation(4).to_a, b) a = @cls[0, 1, 2, 3, 4][1, 4].repeated_permutation(2) - assert(a.all?{|x| !x.include?(0) }) + assert_empty(a.reject {|x| !x.include?(0)}) + end + + def test_repeated_permutation_stack_error + assert_separately([], <<-"end;", timeout: 30) # do + assert_nothing_raised(SystemStackError) do + assert_equal(:ok, Array.new(100_000, nil).repeated_permutation(500_000) {break :ok}) + end + end; end def test_repeated_combination @@ -1651,8 +1895,8 @@ class TestArray < Test::Unit::TestCase [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]], a.repeated_combination(4).to_a.sort) assert_equal(@cls[], a.repeated_combination(-1).to_a) - assert_equal("abcde".each_char.to_a.repeated_combination(5).map{|a|a.sort}.sort, - "edcba".each_char.to_a.repeated_combination(5).map{|a|a.sort}.sort) + assert_equal("abcde".each_char.to_a.repeated_combination(5).map{|e|e.sort}.sort, + "edcba".each_char.to_a.repeated_combination(5).map{|e|e.sort}.sort) assert_equal(@cls[].repeated_combination(0).to_a, @cls[[]]) assert_equal(@cls[].repeated_combination(1).to_a, @cls[]) @@ -1663,7 +1907,15 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[1, 2, 3, 4].repeated_combination(4).to_a, b) a = @cls[0, 1, 2, 3, 4][1, 4].repeated_combination(2) - assert(a.all?{|x| !x.include?(0) }) + assert_empty(a.reject {|x| !x.include?(0)}) + end + + def test_repeated_combination_stack_error + assert_separately([], <<-"end;", timeout: 20) # do + assert_nothing_raised(SystemStackError) do + assert_equal(:ok, Array.new(100_000, nil).repeated_combination(500_000) {break :ok}) + end + end; end def test_take @@ -1686,19 +1938,6 @@ class TestArray < Test::Unit::TestCase assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 }) end - def test_modify_check - a = [] - a.freeze - assert_raise(RuntimeError) { a.shift } - a = [1, 2] - assert_raise(SecurityError) do - Thread.new do - $SAFE = 4 - a.shift - end.value - end - end - LONGP = [127, 63, 31, 15, 7].map {|x| 2**x-1 }.find do |x| begin [].first(x) @@ -1749,6 +1988,11 @@ class TestArray < Test::Unit::TestCase assert_raise(ArgumentError) { [0].first(-1) } end + def test_last2 + assert_equal([0], [0].last(2)) + assert_raise(ArgumentError) { [0].last(-1) } + end + def test_shift2 assert_equal(0, ([0] * 16).shift) # check @@ -1756,6 +2000,8 @@ class TestArray < Test::Unit::TestCase a[3] = 3 a.shift(2) assert_equal([2, 3], a) + + assert_equal([1,1,1], ([1] * 100).shift(3)) end def test_unshift_error @@ -1817,6 +2063,7 @@ 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)} @@ -1869,6 +2116,21 @@ class TestArray < Test::Unit::TestCase a = @cls[ 1, 2, 3, 4, 5 ] assert_equal(a, a.select! { |i| i > 3 }) assert_equal(@cls[4, 5], a) + + bug10722 = '[ruby-dev:48805] [Bug #10722]' + a = @cls[ 5, 6, 7, 8, 9, 10 ] + r = a.select! {|i| + break i if i > 8 + # assert_equal(a[0], i, "should be selected values only") if i == 7 + i >= 7 + } + 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 def test_delete2 @@ -1881,6 +2143,22 @@ class TestArray < Test::Unit::TestCase assert_equal([1, 3], [0, 1, 2, 3].reject {|x| x % 2 == 0 }) end + def test_reject_with_callcc + need_continuation + bug9727 = '[ruby-dev:48101] [Bug #9727]' + cont = nil + a = [*1..10].reject do |i| + callcc {|c| cont = c} if !cont and i == 10 + false + end + if a.size < 1000 + a.unshift(:x) + cont.call + end + assert_equal(1000, a.size, bug9727) + assert_equal([:x, *1..10], a.uniq, bug9727) + end + def test_zip assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], [1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"])) @@ -1890,13 +2168,22 @@ class TestArray < Test::Unit::TestCase ary = Object.new def ary.to_a; [1, 2]; end - assert_raise(TypeError, NoMethodError) {%w(a b).zip(ary)} + assert_raise(TypeError) {%w(a b).zip(ary)} def ary.each; [3, 4].each{|e|yield e}; end assert_equal([['a', 3], ['b', 4]], %w(a b).zip(ary)) def ary.to_ary; [5, 6]; end assert_equal([['a', 5], ['b', 6]], %w(a b).zip(ary)) end + def test_zip_bug + bug8153 = "ruby-core:53650" + r = 1..1 + def r.respond_to?(*) + super + end + assert_equal [[42, 1]], [42].zip(r), bug8153 + end + def test_transpose assert_equal([[1, :a], [2, :b], [3, :c]], [[1, 2, 3], [:a, :b, :c]].transpose) @@ -1923,11 +2210,18 @@ class TestArray < Test::Unit::TestCase assert_not_equal([0, 1, 2], [0, 1, 3]) end - def test_hash2 - a = [] - a << a - assert_equal([[a]].hash, a.hash) - assert_not_equal([a, a].hash, a.hash) # Implementation dependent + A = Array.new(3, &:to_s) + B = A.dup + + def test_equal_resize + o = Object.new + def o.==(o) + A.clear + B.clear + true + end + A[1] = o + assert_equal(A, B) end def test_flatten_error @@ -1954,6 +2248,13 @@ class TestArray < Test::Unit::TestCase 100.times do assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen)) end + + assert_raise_with_message(ArgumentError, /unknown keyword/) do + [0, 1, 2].shuffle(xawqij: "a") + end + assert_raise_with_message(ArgumentError, /unknown keyword/) do + [0, 1, 2].shuffle!(xawqij: "a") + end end def test_shuffle_random @@ -1976,6 +2277,19 @@ class TestArray < Test::Unit::TestCase alias rand call end assert_raise(RuntimeError) {ary.shuffle!(random: gen)} + + zero = Object.new + def zero.to_int + 0 + end + gen_to_int = proc do |max| + zero + end + class << gen_to_int + alias rand call + end + ary = (0...10000).to_a + assert_equal(ary.rotate, ary.shuffle(random: gen_to_int)) end def test_sample @@ -2014,20 +2328,24 @@ class TestArray < Test::Unit::TestCase assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}") end end + + assert_raise_with_message(ArgumentError, /unknown keyword/) do + [0, 1, 2].sample(xawqij: "a") + end end def test_sample_random ary = (0...10000).to_a assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)} gen0 = proc do |max| - (max+1)/2 + max/2 end class << gen0 alias rand call end gen1 = proc do |max| ary.replace([]) - (max+1)/2 + max/2 end class << gen1 alias rand call @@ -2059,6 +2377,19 @@ class TestArray < Test::Unit::TestCase assert_equal([5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0)) ary.sample(11, random: gen1) # implementation detail, may change in the future assert_equal([], ary) + + half = Object.new + def half.to_int + 5000 + end + gen_to_int = proc do |max| + half + end + class << gen_to_int + alias rand call + end + ary = (0...10000).to_a + assert_equal(5000, ary.sample(random: gen_to_int)) end def test_cycle @@ -2092,6 +2423,22 @@ class TestArray < Test::Unit::TestCase assert_equal(:called, (0..100).to_a.combination(50) { break :called }, "[ruby-core:29240] ... must be yielded even if 100C50 > signed integer") end + def test_combination_clear + bug9939 = '[ruby-core:63149] [Bug #9939]' + assert_nothing_raised(bug9939) { + a = [*0..100] + a.combination(3) {|*,x| a.clear} + } + + 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 a = (0..100).to_a assert_raise(RangeError) do @@ -2114,10 +2461,8 @@ class TestArray < Test::Unit::TestCase def test_inspect a = @cls[1, 2, 3] a.taint - a.untrust s = a.inspect assert_equal(true, s.tainted?) - assert_equal(true, s.untrusted?) end def test_initialize2 @@ -2132,6 +2477,11 @@ class TestArray < Test::Unit::TestCase b.replace(a) assert_equal((1..10).to_a, a.shift(10)) assert_equal((11..100).to_a, a) + + a = (1..30).to_a + assert_equal((1..3).to_a, a.shift(3)) + # occupied + assert_equal((4..6).to_a, a.shift(3)) end def test_replace_shared_ary @@ -2208,9 +2558,148 @@ class TestArray < Test::Unit::TestCase assert_equal([], a.rotate!(13)) assert_equal([], a.rotate!(-13)) a = [].freeze - e = assert_raise(RuntimeError) {a.rotate!} - assert_match(/can't modify frozen/, e.message) + assert_raise_with_message(RuntimeError, /can\'t modify frozen/) {a.rotate!} a = [1,2,3] assert_raise(ArgumentError) { a.rotate!(1, 1) } end + + def test_bsearch_typechecks_return_values + assert_raise(TypeError) do + [1, 2, 42, 100, 666].bsearch{ "not ok" } + end + c = eval("class C\u{309a 26a1 26c4 1f300};self;end") + assert_raise_with_message(TypeError, /C\u{309a 26a1 26c4 1f300}/) do + [0,1].bsearch {c.new} + end + assert_equal [1, 2, 42, 100, 666].bsearch{}, [1, 2, 42, 100, 666].bsearch{false} + end + + def test_bsearch_with_no_block + enum = [1, 2, 42, 100, 666].bsearch + assert_nil enum.size + assert_equal 42, enum.each{|x| x >= 33 } + end + + def test_bsearch_in_find_minimum_mode + a = [0, 4, 7, 10, 12] + assert_equal(4, a.bsearch {|x| x >= 4 }) + assert_equal(7, a.bsearch {|x| x >= 6 }) + assert_equal(0, a.bsearch {|x| x >= -1 }) + assert_equal(nil, a.bsearch {|x| x >= 100 }) + end + + def test_bsearch_in_find_any_mode + a = [0, 4, 7, 10, 12] + assert_include([4, 7], a.bsearch {|x| 1 - x / 4 }) + assert_equal(nil, a.bsearch {|x| 4 - x / 2 }) + assert_equal(nil, a.bsearch {|x| 1 }) + assert_equal(nil, a.bsearch {|x| -1 }) + + assert_include([4, 7], a.bsearch {|x| (1 - x / 4) * (2**100) }) + assert_equal(nil, a.bsearch {|x| 1 * (2**100) }) + assert_equal(nil, a.bsearch {|x| (-1) * (2**100) }) + + assert_include([4, 7], a.bsearch {|x| (2**100).coerce((1 - x / 4) * (2**100)).first }) + end + + def test_bsearch_index_typechecks_return_values + assert_raise(TypeError) do + [1, 2, 42, 100, 666].bsearch_index {"not ok"} + end + assert_equal [1, 2, 42, 100, 666].bsearch_index {}, [1, 2, 42, 100, 666].bsearch_index {false} + end + + def test_bsearch_index_with_no_block + enum = [1, 2, 42, 100, 666].bsearch_index + assert_nil enum.size + assert_equal 2, enum.each{|x| x >= 33 } + end + + def test_bsearch_index_in_find_minimum_mode + a = [0, 4, 7, 10, 12] + assert_equal(1, a.bsearch_index {|x| x >= 4 }) + assert_equal(2, a.bsearch_index {|x| x >= 6 }) + assert_equal(0, a.bsearch_index {|x| x >= -1 }) + assert_equal(nil, a.bsearch_index {|x| x >= 100 }) + end + + def test_bsearch_index_in_find_any_mode + a = [0, 4, 7, 10, 12] + assert_include([1, 2], a.bsearch_index {|x| 1 - x / 4 }) + assert_equal(nil, a.bsearch_index {|x| 4 - x / 2 }) + assert_equal(nil, a.bsearch_index {|x| 1 }) + assert_equal(nil, a.bsearch_index {|x| -1 }) + + assert_include([1, 2], a.bsearch_index {|x| (1 - x / 4) * (2**100) }) + assert_equal(nil, a.bsearch_index {|x| 1 * (2**100) }) + assert_equal(nil, a.bsearch_index {|x| (-1) * (2**100) }) + + assert_include([1, 2], a.bsearch_index {|x| (2**100).coerce((1 - x / 4) * (2**100)).first }) + end + + def test_shared_marking + reduce = proc do |s| + s.gsub(/(verify_internal_consistency_reachable_i:\sWB\smiss\s\S+\s\(T_ARRAY\)\s->\s)\S+\s\((proc|T_NONE)\)\n + \K(?:\1\S+\s\(\2\)\n)*/x) do + "...(snip #{$&.count("\n")} lines)...\n" + end + end + begin + assert_normal_exit(<<-EOS, '[Bug #9718]', timeout: 5, stdout_filter: reduce) + queue = [] + 50.times do + 10_000.times do + queue << lambda{} + end + GC.start(full_mark: false, immediate_sweep: true) + GC.verify_internal_consistency + queue.shift.call + end + EOS + rescue Timeout::Error => e + skip e.message + end + end + + sizeof_long = [0].pack("l!").size + sizeof_voidp = [""].pack("p").size + if sizeof_long < sizeof_voidp + ARY_MAX = (1<<(8*sizeof_long-1)) / sizeof_voidp - 1 + Bug11235 = '[ruby-dev:49043] [Bug #11235]' + + def test_push_over_ary_max + assert_separately(['-', ARY_MAX.to_s, Bug11235], <<-"end;") + 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;") + 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;") + a = Array.new(ARGV[0].to_i) + assert_raise(IndexError, ARGV[1]) {a[0, 0] = Array.new(0x1000)} + end; + end + end + + def test_dig + h = @cls[@cls[{a: 1}], 0] + assert_equal(1, h.dig(0, 0, :a)) + assert_nil(h.dig(2, 0)) + assert_raise(TypeError) {h.dig(1, 0)} + end + + private + def need_continuation + unless respond_to?(:callcc, true) + EnvUtil.suppress_warning {require 'continuation'} + end + end end |
