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 | 68a6f2e9e0c6fc2ba09463c848dbbb4ece3e07a1 (patch) | |
tree | ff47d04d683438a00f3f406bcd3a4e89d9732072 /test | |
parent | a22455199bf5b3ce68a6bded62a5c184373d1adb (diff) | |
download | ruby-68a6f2e9e0c6fc2ba09463c848dbbb4ece3e07a1.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')
-rw-r--r-- | test/ruby/test_array.rb | 31 | ||||
-rw-r--r-- | test/ruby/test_enum.rb | 30 |
2 files changed, 46 insertions, 15 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 diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index c7e19fa7cc..e13f6a3f6c 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -331,15 +331,15 @@ class TestEnumerable < Test::Unit::TestCase assert_equal(3, @obj.min {|a,b| b <=> a }) cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } assert_equal([3, 2], @obj.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)) + enum = %w(albatross dog horse).to_enum + assert_equal("albatross", enum.min) + assert_equal("dog", enum.min {|a,b| a.length <=> b.length }) + assert_equal(1, [3,2,1].to_enum.min) + assert_equal(%w[albatross dog], enum.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)) + enum.min(2) {|a,b| a.length <=> b.length }) + assert_equal([13, 14], [20, 32, 32, 21, 30, 25, 29, 13, 14].to_enum.min(2)) + assert_equal([2, 4, 6, 7], [2, 4, 8, 6, 7].to_enum.min(4)) end def test_max @@ -347,14 +347,14 @@ class TestEnumerable < Test::Unit::TestCase assert_equal(1, @obj.max {|a,b| b <=> a }) cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } assert_equal([1, 3], @obj.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)) + enum = %w(albatross dog horse).to_enum + assert_equal("horse", enum.max) + assert_equal("albatross", enum.max {|a,b| a.length <=> b.length }) + assert_equal(1, [3,2,1].to_enum.max{|a,b| b <=> a }) + assert_equal(%w[horse dog], enum.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)) + enum.max(2) {|a,b| a.length <=> b.length }) + assert_equal([3, 2], [0, 0, 0, 0, 0, 0, 1, 3, 2].to_enum.max(2)) end def test_minmax |