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.rb150
1 files changed, 83 insertions, 67 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 797ae95e97..04e15b6d87 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1309,32 +1309,7 @@ class TestArray < Test::Unit::TestCase
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
+ # more comprehensive tests are in test_pack.rb
end
def test_pack_with_buffer
@@ -1361,6 +1336,28 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.prepend(@cls[1, 2]))
end
+ def test_tolerant_to_redefinition
+ *code = __FILE__, __LINE__+1, "#{<<-"{#"}\n#{<<-'};'}"
+ {#
+ module M
+ def <<(a)
+ super(a * 2)
+ end
+ end
+ class Array; prepend M; end
+ ary = [*1..10]
+ mapped = ary.map {|i| i}
+ selected = ary.select {true}
+ module M
+ remove_method :<<
+ end
+ assert_equal(ary, mapped)
+ assert_equal(ary, selected)
+ };
+ assert_separately(%w[--disable-yjit], *code)
+ assert_separately(%w[--enable-yjit], *code)
+ end
+
def test_push
a = @cls[1, 2, 3]
assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))
@@ -2716,6 +2713,18 @@ class TestArray < Test::Unit::TestCase
assert_equal(2, [0, 1].fetch(2, 2))
end
+ def test_fetch_values
+ ary = @cls[1, 2, 3]
+ assert_equal([], ary.fetch_values())
+ assert_equal([1], ary.fetch_values(0))
+ assert_equal([3, 1, 3], ary.fetch_values(2, 0, -1))
+ assert_raise(TypeError) {ary.fetch_values("")}
+ assert_raise(IndexError) {ary.fetch_values(10)}
+ assert_raise(IndexError) {ary.fetch_values(-20)}
+ assert_equal(["10 not found"], ary.fetch_values(10) {|i| "#{i} not found"})
+ assert_equal(["10 not found", 3], ary.fetch_values(10, 2) {|i| "#{i} not found"})
+ end
+
def test_index2
a = [0, 1, 2]
assert_equal(a, a.index.to_a)
@@ -3032,13 +3041,12 @@ class TestArray < Test::Unit::TestCase
end
end
- def test_shuffle_random
- gen = proc do
- 10000000
- end
- class << gen
- alias rand call
- end
+ def test_shuffle_random_out_of_range
+ gen = random_generator {10000000}
+ assert_raise(RangeError) {
+ [*0..2].shuffle(random: gen)
+ }
+ gen = random_generator {-1}
assert_raise(RangeError) {
[*0..2].shuffle(random: gen)
}
@@ -3046,27 +3054,16 @@ class TestArray < Test::Unit::TestCase
def test_shuffle_random_clobbering
ary = (0...10000).to_a
- gen = proc do
+ gen = random_generator do
ary.replace([])
0.5
end
- class << gen
- alias rand call
- end
assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
end
def test_shuffle_random_zero
- 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
+ zero = Struct.new(:to_int).new(0)
+ gen_to_int = random_generator {|max| zero}
ary = (0...10000).to_a
assert_equal(ary.rotate, ary.shuffle(random: gen_to_int))
end
@@ -3134,19 +3131,11 @@ class TestArray < Test::Unit::TestCase
def test_sample_random_generator
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|
+ gen0 = random_generator {|max| max/2}
+ gen1 = random_generator 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)
@@ -3177,20 +3166,23 @@ class TestArray < Test::Unit::TestCase
end
def test_sample_random_generator_half
- 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
+ half = Struct.new(:to_int).new(5000)
+ gen_to_int = random_generator {|max| half}
ary = (0...10000).to_a
assert_equal(5000, ary.sample(random: gen_to_int))
end
+ def test_sample_random_out_of_range
+ gen = random_generator {10000000}
+ assert_raise(RangeError) {
+ [*0..2].sample(random: gen)
+ }
+ gen = random_generator {-1}
+ assert_raise(RangeError) {
+ [*0..2].sample(random: gen)
+ }
+ end
+
def test_sample_random_invalid_generator
ary = (0..10).to_a
assert_raise(NoMethodError) {
@@ -3614,6 +3606,23 @@ class TestArray < Test::Unit::TestCase
assert_equal((1..67).to_a.reverse, var_0)
end
+ def test_find
+ ary = [1, 2, 3, 4, 5]
+ assert_equal(2, ary.find {|x| x % 2 == 0 })
+ assert_equal(nil, ary.find {|x| false })
+ assert_equal(:foo, ary.find(proc { :foo }) {|x| false })
+ end
+
+ def test_rfind
+ ary = [1, 2, 3, 4, 5]
+ assert_equal(4, ary.rfind {|x| x % 2 == 0 })
+ assert_equal(1, ary.rfind {|x| x < 2 })
+ assert_equal(5, ary.rfind {|x| x > 4 })
+ assert_equal(nil, ary.rfind {|x| false })
+ assert_equal(:foo, ary.rfind(proc { :foo }) {|x| false })
+ assert_equal(nil, ary.rfind {|x| ary.clear; false })
+ end
+
private
def need_continuation
unless respond_to?(:callcc, true)
@@ -3621,6 +3630,13 @@ class TestArray < Test::Unit::TestCase
end
omit 'requires callcc support' unless respond_to?(:callcc, true)
end
+
+ def random_generator(&block)
+ class << block
+ alias rand call
+ end
+ block
+ end
end
class TestArraySubclass < TestArray