diff options
Diffstat (limited to 'test/ruby/test_array.rb')
| -rw-r--r-- | test/ruby/test_array.rb | 358 |
1 files changed, 35 insertions, 323 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 5812958f0d..6170e916ac 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1,4 +1,3 @@ -# coding: US-ASCII require 'test/unit' require_relative 'envutil' @@ -13,17 +12,6 @@ class TestArray < Test::Unit::TestCase $VERBOSE = @verbose end - def test_percent_i - assert_equal([:foo, :bar], %i[foo bar]) - assert_equal([:"\"foo"], %i["foo]) - end - - def test_percent_I - x = 10 - assert_equal([:foo, :b10], %I[foo b#{x}]) - assert_equal([:"\"foo10"], %I["foo#{x}]) - end - def test_0_literal assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) assert_equal([1, 2, 1, 2], [1, 2] * 2) @@ -282,17 +270,17 @@ class TestArray < Test::Unit::TestCase end def test_EQUAL # '==' - assert_operator(@cls[], :==, @cls[]) - assert_operator(@cls[1], :==, @cls[1]) - assert_operator(@cls[1, 1, 2, 2], :==, @cls[1, 1, 2, 2]) - assert_operator(@cls[1.0, 1.0, 2.0, 2.0], :==, @cls[1, 1, 2, 2]) + 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_operator(@cls[], :===, @cls[]) - assert_operator(@cls[1], :===, @cls[1]) - assert_operator(@cls[1, 1, 2, 2], :===, @cls[1, 1, 2, 2]) - assert_operator(@cls[1.0, 1.0, 2.0, 2.0], :===, @cls[1, 1, 2, 2]) + 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 # '[]' @@ -426,18 +414,6 @@ 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 @@ -485,7 +461,7 @@ class TestArray < Test::Unit::TestCase b = a.clone assert_equal(a, b) - assert_not_equal(a.__id__, b.__id__) + assert(a.__id__ != b.__id__) assert_equal(a.frozen?, b.frozen?) assert_equal(a.untrusted?, b.untrusted?) assert_equal(a.tainted?, b.tainted?) @@ -576,29 +552,6 @@ 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 @@ -621,14 +574,6 @@ 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 @@ -662,11 +607,6 @@ class TestArray < Test::Unit::TestCase a = @cls[ 1, 2, 3, 4, 5 ] assert_equal(a, a.delete_if { |i| i > 3 }) assert_equal(@cls[1, 2, 3], a) - - 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(@cls[7, 8, 9, 10], a, bug2545) end def test_dup @@ -678,7 +618,7 @@ class TestArray < Test::Unit::TestCase b = a.dup assert_equal(a, b) - assert_not_equal(a.__id__, b.__id__) + assert(a.__id__ != b.__id__) assert_equal(false, b.frozen?) assert_equal(a.tainted?, b.tainted?) end @@ -726,15 +666,15 @@ class TestArray < Test::Unit::TestCase end def test_empty? - assert_empty(@cls[]) - assert_not_empty(@cls[1]) + assert(@cls[].empty?) + assert(!@cls[1].empty?) end def test_eql? - assert_send([@cls[], :eql?, @cls[]]) - assert_send([@cls[1], :eql?, @cls[1]]) - assert_send([@cls[1, 1, 2, 2], :eql?, @cls[1, 1, 2, 2]]) - assert_not_send([@cls[1.0, 1.0, 2.0, 2.0], :eql?, @cls[1, 1, 2, 2]]) + 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 @@ -913,18 +853,18 @@ class TestArray < Test::Unit::TestCase a1 = @cls[ 'cat', 'dog' ] a2 = @cls[ 'cat', 'dog' ] a3 = @cls[ 'dog', 'cat' ] - assert_equal(a1.hash, a2.hash) - assert_not_equal(a1.hash, a3.hash) + assert(a1.hash == a2.hash) + assert(a1.hash != a3.hash) end def test_include? a = @cls[ 'cat', 99, /a/, @cls[ 1, 2, 3] ] - assert_include(a, 'cat') - assert_include(a, 99) - assert_include(a, /a/) - assert_include(a, [1,2,3]) - assert_not_include(a, 'ca') - assert_not_include(a, [1,2]) + 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 @@ -955,19 +895,16 @@ class TestArray < Test::Unit::TestCase $, = "" a = @cls[1, 2] assert_equal("12", a.join) - assert_equal("12", a.join(nil)) assert_equal("1,2", a.join(',')) $, = "" a = @cls[1, 2, 3] assert_equal("123", a.join) - assert_equal("123", a.join(nil)) 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(nil)) assert_equal("1,2,3", a.join(',')) $, = "" @@ -977,29 +914,6 @@ class TestArray < Test::Unit::TestCase s = a.join assert_equal(true, s.tainted?) assert_equal(true, s.untrusted?) - - bug5902 = '[ruby-core:42161]' - sep = ":".taint.untrust - - 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') - assert_equal(Encoding::US_ASCII, [[]].join.encoding) - 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) - bug5379 = '[ruby-core:39776]' - assert_equal(Encoding::US_ASCII, [[], u, nil].join.encoding, bug5379) - assert_equal(Encoding::UTF_8, [[], "\u3042", nil].join.encoding, bug5379) ensure $, = nil end @@ -1166,11 +1080,6 @@ class TestArray < Test::Unit::TestCase a = @cls[ 1, 2, 3, 4, 5 ] assert_equal(a, a.reject! { |i| i > 3 }) assert_equal(@cls[1, 2, 3], a) - - 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(@cls[7, 8, 9, 10], a, bug2545) end def test_replace @@ -1651,9 +1560,6 @@ class TestArray < Test::Unit::TestCase a.permutation {|x| b << x; a.replace(@cls[9, 8, 7, 6]) } assert_equal(@cls[9, 8, 7, 6], a) assert_equal(@cls[1, 2, 3, 4].permutation.to_a, b) - - bug3708 = '[ruby-dev:42067]' - assert_equal(b, @cls[0, 1, 2, 3, 4][1, 4].permutation.to_a, bug3708) end def test_repeated_permutation @@ -1695,8 +1601,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{|e|e.sort}.sort, - "edcba".each_char.to_a.repeated_combination(5).map{|e|e.sort}.sort) + 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(@cls[].repeated_combination(0).to_a, @cls[[]]) assert_equal(@cls[].repeated_combination(1).to_a, @cls[]) @@ -1889,9 +1795,7 @@ class TestArray < Test::Unit::TestCase def test_values_at2 a = [0, 1, 2, 3, 4, 5] assert_equal([1, 2, 3], a.values_at(1..3)) - assert_equal([nil, nil], a.values_at(7..8)) - bug6203 = '[ruby-core:43678]' - assert_equal([4, 5, nil, nil], a.values_at(4..7), bug6203) + assert_equal([], a.values_at(7..8)) assert_equal([nil], a.values_at(2**31-1)) end @@ -1925,22 +1829,6 @@ class TestArray < Test::Unit::TestCase assert_equal([1, 3], [0, 1, 2, 3].reject {|x| x % 2 == 0 }) end - def test_reject_with_callcc - respond_to?(:callcc, true) or require '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"])) @@ -1950,22 +1838,13 @@ 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(NoMethodError){ %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) @@ -1992,20 +1871,6 @@ class TestArray < Test::Unit::TestCase assert_not_equal([0, 1, 2], [0, 1, 3]) end - 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_hash2 a = [] a << a @@ -2030,56 +1895,14 @@ class TestArray < Test::Unit::TestCase 100.times do assert_equal([0, 1, 2], [2, 1, 0].shuffle.sort) end - - gen = Random.new(0) - assert_raise(ArgumentError) {[1, 2, 3].shuffle(1, random: gen)} - srand(0) - 100.times do - assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen)) - end - end - - def test_shuffle_random - gen = proc do - 10000000 - end - class << gen - alias rand call - end - assert_raise(RangeError) { - [*0..2].shuffle(random: gen) - } - - ary = (0...10000).to_a - gen = proc do - ary.replace([]) - 0.5 - end - class << gen - 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 100.times do - assert_include([0, 1, 2], [2, 1, 0].sample) + assert([0, 1, 2].include?([2, 1, 0].sample)) samples = [2, 1, 0].sample(2) samples.each{|sample| - assert_include([0, 1, 2], sample) + assert([0, 1, 2].include?(sample)) } end @@ -2088,7 +1911,7 @@ class TestArray < Test::Unit::TestCase (0..20).each do |n| 100.times do b = a.sample(n) - assert_equal([n, 18].min, b.size) + assert_equal([n, 18].min, b.uniq.size) assert_equal(a, (a | b).sort) assert_equal(b.sort, (a & b).sort) end @@ -2101,73 +1924,6 @@ class TestArray < Test::Unit::TestCase end assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)} - - gen = Random.new(0) - srand(0) - a = (1..18).to_a - (0..20).each do |n| - 100.times do |i| - assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}") - end - 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/2 - end - class << gen0 - alias rand call - end - gen1 = proc do |max| - ary.replace([]) - max/2 - end - class << gen1 - alias rand call - end - assert_equal(5000, ary.sample(random: gen0)) - assert_nil(ary.sample(random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - assert_equal([5000], ary.sample(1, random: gen0)) - assert_equal([], ary.sample(1, random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - assert_equal([5000, 4999], ary.sample(2, random: gen0)) - assert_equal([], ary.sample(2, random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - assert_equal([5000, 4999, 5001], ary.sample(3, random: gen0)) - assert_equal([], ary.sample(3, random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - assert_equal([5000, 4999, 5001, 4998], ary.sample(4, random: gen0)) - assert_equal([], ary.sample(4, random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - assert_equal([5000, 4999, 5001, 4998, 5002, 4997, 5003, 4996, 5004, 4995], ary.sample(10, random: gen0)) - assert_equal([], ary.sample(10, random: gen1)) - assert_equal([], ary) - ary = (0...10000).to_a - 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 @@ -2198,16 +1954,9 @@ class TestArray < Test::Unit::TestCase end def test_combination2 - 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_separately([], <<-'end;') - 100_000.times {Array.new(1000)} - a = [*0..100] - a.combination(3) {|*,x| a.clear} - end; + assert_nothing_raised do + (0..100).to_a.combination(50) { break } + end end def test_product2 @@ -2301,8 +2050,6 @@ class TestArray < Test::Unit::TestCase assert_equal([], a.rotate(-13)) a = [1,2,3] assert_raise(ArgumentError) { a.rotate(1, 1) } - assert_equal([1,2,3,4,5].rotate(2**31-1), [1,2,3,4,5].rotate(2**31-0.1)) - assert_equal([1,2,3,4,5].rotate(-2**31), [1,2,3,4,5].rotate(-2**31-0.9)) end def test_rotate! @@ -2327,43 +2074,8 @@ class TestArray < Test::Unit::TestCase assert_equal([], a.rotate!(-13)) a = [].freeze e = assert_raise(RuntimeError) {a.rotate!} - assert_match(/can't modify frozen/, e.message) + assert_match(/can't modify frozen array/, e.message) 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 - 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 end |
