diff options
author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-17 12:14:21 +0000 |
---|---|---|
committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-17 12:14:21 +0000 |
commit | a38d1619b487a9b22422e6be5fdf96d7bdc4b703 (patch) | |
tree | ff47d04d683438a00f3f406bcd3a4e89d9732072 /test/ruby/test_array.rb | |
parent | 4b5cb8b79ce7d60a15b5edeb22637a8366503bbf (diff) | |
download | ruby-a38d1619b487a9b22422e6be5fdf96d7bdc4b703.tar.gz |
* array.c (rb_ary_max, rb_ary_min): Array#max and Array#min added.
[Feature #12172]
* internal.h (OPTIMIZED_CMP): moved from enum.c so that array.c can
use it.
* test/ruby/test_array.rb (test_max, test_min): tests for Array#max
and Array#min.
* test/ruby/test_enum.rb (test_max, test_min): revised a bit to test
Enumerable#max and #min explicitly.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54150 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r-- | test/ruby/test_array.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 9364a2731a..fa158185f8 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1552,6 +1552,37 @@ class TestArray < Test::Unit::TestCase 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 }) + cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } + assert_equal([3, 2], [1, 2, 3, 1, 2].each_with_index.min(&cond)) + ary = %w(albatross dog horse) + assert_equal("albatross", ary.min) + assert_equal("dog", ary.min {|a,b| a.length <=> b.length }) + assert_equal(1, [3,2,1].min) + assert_equal(%w[albatross dog], ary.min(2)) + assert_equal(%w[dog horse], + 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)) + end + + def test_max + assert_equal(3, [1, 2, 3, 1, 2].max) + assert_equal(1, [1, 2, 3, 1, 2].max {|a,b| b <=> a }) + cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } + assert_equal([1, 3], [1, 2, 3, 1, 2].each_with_index.max(&cond)) + ary = %w(albatross dog horse) + assert_equal("horse", ary.max) + assert_equal("albatross", ary.max {|a,b| a.length <=> b.length }) + assert_equal(1, [3,2,1].max{|a,b| b <=> a }) + assert_equal(%w[horse dog], ary.max(2)) + 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)) + end + def test_uniq a = [] b = a.uniq |