4 class TestEnumerable < Test::Unit::TestCase
26 assert_equal([1, 2, 1, 2], @obj.grep(1..2))
28 @obj.grep(2) {|x| a << x }
29 assert_equal([2, 2], a)
33 assert_equal(5, @obj.count)
34 assert_equal(2, @obj.count(1))
35 assert_equal(3, @obj.count {|x| x % 2 == 1 })
36 assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
37 assert_raise(ArgumentError) { @obj.count(0, 1) }
41 assert_equal(2, @obj.find {|x| x % 2 == 0 })
42 assert_equal(nil, @obj.find {|x| false })
43 assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
47 assert_equal(1, @obj.find_index(2))
48 assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
49 assert_equal(nil, @obj.find_index {|x| false })
50 assert_raise(ArgumentError) { @obj.find_index(0, 1) }
54 assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 })
58 assert_equal([2, 3, 2], @obj.reject {|x| x < 2 })
62 assert_equal([1, 2, 3, 1, 2], @obj.to_a)
66 assert_equal(12, @obj.inject {|z, x| z * x })
67 assert_equal(48, @obj.inject {|z, x| z * 2 + x })
68 assert_equal(12, @obj.inject(:*))
69 assert_equal(24, @obj.inject(2) {|z, x| z * x })
70 assert_equal(24, @obj.inject(2, :*) {|z, x| z * x })
74 assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
78 h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
79 assert_equal(h, @obj.group_by {|x| x })
83 assert_equal(1, @obj.first)
84 assert_equal([1, 2, 3], @obj.first(3))
88 assert_equal([1, 1, 2, 2, 3], @obj.sort)
92 assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x })
96 assert_equal(true, @obj.all? {|x| x <= 3 })
97 assert_equal(false, @obj.all? {|x| x < 3 })
98 assert_equal(true, @obj.all?)
99 assert_equal(false, [true, true, false].all?)
103 assert_equal(true, @obj.any? {|x| x >= 3 })
104 assert_equal(false, @obj.any? {|x| x > 3 })
105 assert_equal(true, @obj.any?)
106 assert_equal(false, [false, false, false].any?)
110 assert(@obj.one? {|x| x == 3 })
111 assert(!(@obj.one? {|x| x == 1 }))
112 assert(!(@obj.one? {|x| x == 4 }))
113 assert(%w{ant bear cat}.one? {|word| word.length == 4})
114 assert(!(%w{ant bear cat}.one? {|word| word.length > 4}))
115 assert(!(%w{ant bear cat}.one? {|word| word.length < 4}))
116 assert(!([ nil, true, 99 ].one?))
117 assert([ nil, true, false ].one?)
121 assert(@obj.none? {|x| x == 4 })
122 assert(!(@obj.none? {|x| x == 1 }))
123 assert(!(@obj.none? {|x| x == 3 }))
124 assert(%w{ant bear cat}.none? {|word| word.length == 5})
125 assert(!(%w{ant bear cat}.none? {|word| word.length >= 4}))
128 assert([nil,false].none?)
132 assert_equal(1, @obj.min)
133 assert_equal(3, @obj.min {|a,b| b <=> a })
134 a = %w(albatross dog horse)
135 assert_equal("albatross", a.min)
136 assert_equal("dog", a.min {|a,b| a.length <=> b.length })
137 assert_equal(1, [3,2,1].min)
141 assert_equal(3, @obj.max)
142 assert_equal(1, @obj.max {|a,b| b <=> a })
143 a = %w(albatross dog horse)
144 assert_equal("horse", a.max)
145 assert_equal("albatross", a.max {|a,b| a.length <=> b.length })
146 assert_equal(1, [3,2,1].max{|a,b| b <=> a })
150 assert_equal([1, 3], @obj.minmax)
151 assert_equal([3, 1], @obj.minmax {|a,b| b <=> a })
152 a = %w(albatross dog horse)
153 assert_equal(["albatross", "horse"], a.minmax)
154 assert_equal(["dog", "albatross"], a.minmax {|a,b| a.length <=> b.length })
155 assert_equal([1, 3], [2,3,1].minmax)
156 assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a })
160 assert_equal(3, @obj.min_by {|x| -x })
161 a = %w(albatross dog horse)
162 assert_equal("dog", a.min_by {|x| x.length })
163 assert_equal(3, [2,3,1].min_by {|x| -x })
167 assert_equal(1, @obj.max_by {|x| -x })
168 a = %w(albatross dog horse)
169 assert_equal("albatross", a.max_by {|x| x.length })
170 assert_equal(1, [2,3,1].max_by {|x| -x })
174 assert_equal([3, 1], @obj.minmax_by {|x| -x })
175 a = %w(albatross dog horse)
176 assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length })
177 assert_equal([3, 1], [2,3,1].minmax_by {|x| -x })
181 assert(@obj.member?(1))
182 assert(!(@obj.member?(4)))
183 assert([1,2,3].member?(1))
184 assert(!([1,2,3].member?(4)))
187 def test_each_with_index
189 @obj.each_with_index {|x, i| a << [x, i] }
190 assert_equal([[1,0],[2,1],[3,2],[1,3],[2,4]], a)
193 %w(cat dog wombat).each_with_index do |item, index|
196 assert_equal({"cat"=>0, "wombat"=>2, "dog"=>1}, hash)
200 assert_equal([[1,1],[2,2],[3,3],[1,1],[2,2]], @obj.zip(@obj))
202 @obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
203 assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
207 assert_equal([1,2,3], @obj.take(3))
211 assert_equal([1,2], @obj.take_while {|x| x <= 2})
215 assert_equal([3,1,2], @obj.drop(2))
219 assert_equal([3,1,2], @obj.drop_while {|x| x <= 2})
223 assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10))
227 assert_raise(RuntimeError) do
229 @obj.sort_by {|x| callcc {|c2| c ||= c2 }; x }
233 assert_raise(RuntimeError) do
236 class << o; self; end.class_eval do
237 define_method(:<=>) do |x|
238 callcc {|c2| c ||= c2 }
242 [o, o].sort_by {|x| x }
246 assert_raise(RuntimeError) do
249 class << o; self; end.class_eval do
250 define_method(:<=>) do |x|
251 callcc {|c2| c ||= c2 }
255 [o, o, o].sort_by {|x| x }