~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/ruby/test_enum.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-05-16 12:37:06 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080516123706-r4llcdfd35aobrjv
Tags: 1.9.0.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.
* debian/control:
  - ruby1.9 pkg: moved rdoc1.9 suggestion to depends. (LP: #228345)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'test/unit'
 
2
require 'continuation'
 
3
 
 
4
class TestEnumerable < Test::Unit::TestCase
 
5
  def setup
 
6
    @obj = Object.new
 
7
    class << @obj
 
8
      include Enumerable
 
9
      def each
 
10
        yield 1
 
11
        yield 2
 
12
        yield 3
 
13
        yield 1
 
14
        yield 2
 
15
      end
 
16
    end
 
17
    @verbose = $VERBOSE
 
18
    $VERBOSE = nil
 
19
  end
 
20
 
 
21
  def teardown
 
22
    $VERBOSE = @verbose
 
23
  end
 
24
 
 
25
  def test_grep
 
26
    assert_equal([1, 2, 1, 2], @obj.grep(1..2))
 
27
    a = []
 
28
    @obj.grep(2) {|x| a << x }
 
29
    assert_equal([2, 2], a)
 
30
  end
 
31
 
 
32
  def test_count
 
33
    assert_equal(2, @obj.count(1))
 
34
    assert_equal(3, @obj.count {|x| x % 2 == 1 })
 
35
    assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
 
36
    assert_raise(ArgumentError) { @obj.count(0, 1) }
 
37
  end
 
38
 
 
39
  def test_find
 
40
    assert_equal(2, @obj.find {|x| x % 2 == 0 })
 
41
    assert_equal(nil, @obj.find {|x| false })
 
42
    assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
 
43
  end
 
44
 
 
45
  def test_find_index
 
46
    assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
 
47
    assert_equal(nil, @obj.find_index {|x| false })
 
48
  end
 
49
 
 
50
  def test_find_all
 
51
    assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 })
 
52
  end
 
53
 
 
54
  def test_reject
 
55
    assert_equal([2, 3, 2], @obj.reject {|x| x < 2 })
 
56
  end
 
57
 
 
58
  def test_to_a
 
59
    assert_equal([1, 2, 3, 1, 2], @obj.to_a)
 
60
  end
 
61
 
 
62
  def test_inject
 
63
    assert_equal(12, @obj.inject {|z, x| z * x })
 
64
    assert_equal(48, @obj.inject {|z, x| z * 2 + x })
 
65
    assert_equal(12, @obj.inject(:*))
 
66
    assert_equal(24, @obj.inject(2) {|z, x| z * x })
 
67
    assert_equal(24, @obj.inject(2, :*) {|z, x| z * x })
 
68
  end
 
69
 
 
70
  def test_partition
 
71
    assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
 
72
  end
 
73
 
 
74
  def test_group_by
 
75
    h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
 
76
    assert_equal(h, @obj.group_by {|x| x })
 
77
  end
 
78
 
 
79
  def test_first
 
80
    assert_equal(1, @obj.first)
 
81
    assert_equal([1, 2, 3], @obj.first(3))
 
82
  end
 
83
 
 
84
  def test_sort
 
85
    assert_equal([1, 1, 2, 2, 3], @obj.sort)
 
86
  end
 
87
 
 
88
  def test_sort_by
 
89
    assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x })
 
90
  end
 
91
 
 
92
  def test_all
 
93
    assert_equal(true, @obj.all? {|x| x <= 3 })
 
94
    assert_equal(false, @obj.all? {|x| x < 3 })
 
95
    assert_equal(true, @obj.all?)
 
96
    assert_equal(false, [true, true, false].all?)
 
97
  end
 
98
 
 
99
  def test_any
 
100
    assert_equal(true, @obj.any? {|x| x >= 3 })
 
101
    assert_equal(false, @obj.any? {|x| x > 3 })
 
102
    assert_equal(true, @obj.any?)
 
103
    assert_equal(false, [false, false, false].any?)
 
104
  end
 
105
 
 
106
  def test_one
 
107
    assert(@obj.one? {|x| x == 3 })
 
108
    assert(!(@obj.one? {|x| x == 1 }))
 
109
    assert(!(@obj.one? {|x| x == 4 }))
 
110
    assert(%w{ant bear cat}.one? {|word| word.length == 4})
 
111
    assert(!(%w{ant bear cat}.one? {|word| word.length > 4}))
 
112
    assert(!(%w{ant bear cat}.one? {|word| word.length < 4}))
 
113
    assert(!([ nil, true, 99 ].one?))
 
114
    assert([ nil, true, false ].one?)
 
115
  end
 
116
 
 
117
  def test_none
 
118
    assert(@obj.none? {|x| x == 4 })
 
119
    assert(!(@obj.none? {|x| x == 1 }))
 
120
    assert(!(@obj.none? {|x| x == 3 }))
 
121
    assert(%w{ant bear cat}.none? {|word| word.length == 5})
 
122
    assert(!(%w{ant bear cat}.none? {|word| word.length >= 4}))
 
123
    assert([].none?)
 
124
    assert([nil].none?)
 
125
    assert([nil,false].none?)
 
126
  end
 
127
 
 
128
  def test_min
 
129
    assert_equal(1, @obj.min)
 
130
    assert_equal(3, @obj.min {|a,b| b <=> a })
 
131
    a = %w(albatross dog horse)
 
132
    assert_equal("albatross", a.min)
 
133
    assert_equal("dog", a.min {|a,b| a.length <=> b.length })
 
134
    assert_equal(1, [3,2,1].min)
 
135
  end
 
136
 
 
137
  def test_max
 
138
    assert_equal(3, @obj.max)
 
139
    assert_equal(1, @obj.max {|a,b| b <=> a })
 
140
    a = %w(albatross dog horse)
 
141
    assert_equal("horse", a.max)
 
142
    assert_equal("albatross", a.max {|a,b| a.length <=> b.length })
 
143
    assert_equal(1, [3,2,1].max{|a,b| b <=> a })
 
144
  end
 
145
 
 
146
  def test_minmax
 
147
    assert_equal([1, 3], @obj.minmax)
 
148
    assert_equal([3, 1], @obj.minmax {|a,b| b <=> a })
 
149
    a = %w(albatross dog horse)
 
150
    assert_equal(["albatross", "horse"], a.minmax)
 
151
    assert_equal(["dog", "albatross"], a.minmax {|a,b| a.length <=> b.length })
 
152
    assert_equal([1, 3], [2,3,1].minmax)
 
153
    assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a })
 
154
  end
 
155
 
 
156
  def test_min_by
 
157
    assert_equal(3, @obj.min_by {|x| -x })
 
158
    a = %w(albatross dog horse)
 
159
    assert_equal("dog", a.min_by {|x| x.length })
 
160
    assert_equal(3, [2,3,1].min_by {|x| -x })
 
161
  end
 
162
 
 
163
  def test_max_by
 
164
    assert_equal(1, @obj.max_by {|x| -x })
 
165
    a = %w(albatross dog horse)
 
166
    assert_equal("albatross", a.max_by {|x| x.length })
 
167
    assert_equal(1, [2,3,1].max_by {|x| -x })
 
168
  end
 
169
 
 
170
  def test_minmax_by
 
171
    assert_equal([3, 1], @obj.minmax_by {|x| -x })
 
172
    a = %w(albatross dog horse)
 
173
    assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length })
 
174
    assert_equal([3, 1], [2,3,1].minmax_by {|x| -x })
 
175
  end
 
176
 
 
177
  def test_member
 
178
    assert(@obj.member?(1))
 
179
    assert(!(@obj.member?(4)))
 
180
    assert([1,2,3].member?(1))
 
181
    assert(!([1,2,3].member?(4)))
 
182
  end
 
183
 
 
184
  def test_each_with_index
 
185
    a = []
 
186
    @obj.each_with_index {|x, i| a << [x, i] }
 
187
    assert_equal([[1,0],[2,1],[3,2],[1,3],[2,4]], a)
 
188
 
 
189
    hash = Hash.new
 
190
    %w(cat dog wombat).each_with_index do |item, index|
 
191
      hash[item] = index
 
192
    end
 
193
    assert_equal({"cat"=>0, "wombat"=>2, "dog"=>1}, hash)
 
194
  end
 
195
 
 
196
  def test_zip
 
197
    assert_equal([[1,1],[2,2],[3,3],[1,1],[2,2]], @obj.zip(@obj))
 
198
    a = []
 
199
    @obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
 
200
    assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
 
201
  end
 
202
 
 
203
  def test_take
 
204
    assert_equal([1,2,3], @obj.take(3))
 
205
  end
 
206
 
 
207
  def test_take_while
 
208
    assert_equal([1,2], @obj.take_while {|x| x <= 2})
 
209
  end
 
210
 
 
211
  def test_drop
 
212
    assert_equal([3,1,2], @obj.drop(2))
 
213
  end
 
214
 
 
215
  def test_drop_while
 
216
    assert_equal([3,1,2], @obj.drop_while {|x| x <= 2})
 
217
  end
 
218
 
 
219
  def test_cycle
 
220
    assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10))
 
221
  end
 
222
 
 
223
  def test_callcc
 
224
    assert_raise(RuntimeError) do
 
225
      c = nil
 
226
      @obj.sort_by {|x| callcc {|c2| c ||= c2 }; x }
 
227
      c.call
 
228
    end
 
229
 
 
230
    assert_raise(RuntimeError) do
 
231
      c = nil
 
232
      o = Object.new
 
233
      class << o; self; end.class_eval do
 
234
        define_method(:<=>) do |x|
 
235
          callcc {|c2| c ||= c2 }
 
236
          0
 
237
        end
 
238
      end
 
239
      [o, o].sort_by {|x| x }
 
240
      c.call
 
241
    end
 
242
 
 
243
    assert_raise(RuntimeError) do
 
244
      c = nil
 
245
      o = Object.new
 
246
      class << o; self; end.class_eval do
 
247
        define_method(:<=>) do |x|
 
248
          callcc {|c2| c ||= c2 }
 
249
          0
 
250
        end
 
251
      end
 
252
      [o, o, o].sort_by {|x| x }
 
253
      c.call
 
254
    end
 
255
  end
 
256
end