summaryrefslogtreecommitdiff
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb280
1 files changed, 33 insertions, 247 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index a8b514c830..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?)
@@ -598,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
@@ -639,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
@@ -655,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
@@ -703,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
@@ -890,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
@@ -932,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(','))
$, = ""
@@ -954,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
@@ -1143,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
@@ -1628,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
@@ -1866,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
@@ -1911,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)
@@ -1953,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
@@ -1991,43 +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)}
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
@@ -2036,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
@@ -2049,60 +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)
end
def test_cycle
@@ -2133,7 +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")
+ assert_nothing_raised do
+ (0..100).to_a.combination(50) { break }
+ end
end
def test_product2
@@ -2227,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!
@@ -2253,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