/**************************************************************************** * Copyright(c) 2000-2001 Broadcom Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * Name: nicext.h * * Description: Broadcom Network Interface Card Extension (NICE) is an * extension to Linux NET device kernel mode drivers. * NICE is designed to provide additional functionalities, * such as receive packet intercept. To support Broadcom NICE, * the network device driver can be modified by adding an * device ioctl handler and by indicating receiving packets * to the NICE receive handler. Broadcom NICE will only be * enabled by a NICE-aware intermediate driver, such as * Broadcom Advanced Server Program Driver (BASP). When NICE * is not enabled, the modified network device drivers * functions exactly as other non-NICE aware drivers. * * Author: Frankie Fan * * Created: September 17, 2000 * ****************************************************************************/ #ifndef _nicext_h_ #define _nicext_h_ /* * ioctl for NICE */ #define SIOCNICE SIOCDEVPRIVATE+7 /* * SIOCNICE: * * The following structure needs to be less than IFNAMSIZ (16 bytes) because * we're overloading ifreq.ifr_ifru. * * If 16 bytes is not enough, we should consider relaxing this because * this is no field after ifr_ifru in the ifreq structure. But we may * run into future compatiability problem in case of changing struct ifreq. */ struct nice_req { __u32 cmd; union { #ifdef __KERNEL__ /* cmd = NICE_CMD_SET_RX or NICE_CMD_GET_RX */ struct { void (*nrqus1_rx)( struct sk_buff*, void* ); void* nrqus1_ctx; } nrqu_nrqus1; /* cmd = NICE_CMD_QUERY_SUPPORT */ struct { __u32 nrqus2_magic; __u32 nrqus2_support_rx:1; __u32 nrqus2_support_vlan:1; __u32 nrqus2_support_get_speed:1; } nrqu_nrqus2; #endif /* cmd = NICE_CMD_GET_SPEED */ struct { unsigned int nrqus3_speed; /* 0 if link is down, */ /* otherwise speed in Mbps */ } nrqu_nrqus3; /* cmd = NICE_CMD_BLINK_LED */ struct { unsigned int nrqus4_blink_time; /* blink duration in seconds */ } nrqu_nrqus4; } nrq_nrqu; }; #define nrq_rx nrq_nrqu.nrqu_nrqus1.nrqus1_rx #define nrq_ctx nrq_nrqu.nrqu_nrqus1.nrqus1_ctx #define nrq_support_rx nrq_nrqu.nrqu_nrqus2.nrqus2_support_rx #define nrq_magic nrq_nrqu.nrqu_nrqus2.nrqus2_magic #define nrq_support_vlan nrq_nrqu.nrqu_nrqus2.nrqus2_support_vlan #define nrq_support_get_speed nrq_nrqu.nrqu_nrqus2.nrqus2_support_get_speed #define nrq_speed nrq_nrqu.nrqu_nrqus3.nrqus3_speed #define nrq_blink_time nrq_nrqu.nrqu_nrqus4.nrqus4_blink_time /* * magic constants */ #define NICE_REQUESTOR_MAGIC 0x4543494E /* NICE in ascii */ #define NICE_DEVICE_MAGIC 0x4E494345 /* ECIN in ascii */ /* * command field */ #define NICE_CMD_QUERY_SUPPORT 0x00000001 #define NICE_CMD_SET_RX 0x00000002 #define NICE_CMD_GET_RX 0x00000003 #define NICE_CMD_GET_SPEED 0x00000004 #define NICE_CMD_BLINK_LED 0x00000005 #endif /* _nicext_h_ */ narip/public_git/ruby.git/tree/test/ruby/test_enum.rb?id=91521848927d2aab9f95bb75c07975bc7cf1ac40'>test_enum.rb
blob: a8a88640bd6e6a85030b51777dc0d3d937120841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require 'test/unit'
require 'continuation'

class TestEnumerable < Test::Unit::TestCase
  def setup
    @obj = Object.new
    class << @obj
      include Enumerable
      def each
        yield 1
        yield 2
        yield 3
        yield 1
        yield 2
      end
    end
    @verbose = $VERBOSE
    $VERBOSE = nil
  end

  def teardown
    $VERBOSE = @verbose
  end

  def test_grep
    assert_equal([1, 2, 1, 2], @obj.grep(1..2))
    a = []
    @obj.grep(2) {|x| a << x }
    assert_equal([2, 2], a)
  end

  def test_count
    assert_equal(5, @obj.count)
    assert_equal(2, @obj.count(1))
    assert_equal(3, @obj.count {|x| x % 2 == 1 })
    assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
    assert_raise(ArgumentError) { @obj.count(0, 1) }
  end

  def test_find
    assert_equal(2, @obj.find {|x| x % 2 == 0 })
    assert_equal(nil, @obj.find {|x| false })
    assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
  end

  def test_find_index
    assert_equal(1, @obj.find_index(2))
    assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
    assert_equal(nil, @obj.find_index {|x| false })
    assert_raise(ArgumentError) { @obj.find_index(0, 1) }
  end

  def test_find_all
    assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 })
  end

  def test_reject
    assert_equal([2, 3, 2], @obj.reject {|x| x < 2 })
  end

  def test_to_a
    assert_equal([1, 2, 3, 1, 2], @obj.to_a)
  end

  def test_inject
    assert_equal(12, @obj.inject {|z, x| z * x })
    assert_equal(48, @obj.inject {|z, x| z * 2 + x })
    assert_equal(12, @obj.inject(:*))
    assert_equal(24, @obj.inject(2) {|z, x| z * x })
    assert_equal(24, @obj.inject(2, :*) {|z, x| z * x })
  end

  def test_partition
    assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
  end

  def test_group_by
    h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
    assert_equal(h, @obj.group_by {|x| x })
  end

  def test_first
    assert_equal(1, @obj.first)
    assert_equal([1, 2, 3], @obj.first(3))
  end

  def test_sort
    assert_equal([1, 1, 2, 2, 3], @obj.sort)
  end

  def test_sort_by
    assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x })
  end

  def test_all
    assert_equal(true, @obj.all? {|x| x <= 3 })
    assert_equal(false, @obj.all? {|x| x < 3 })
    assert_equal(true, @obj.all?)
    assert_equal(false, [true, true, false].all?)
  end

  def test_any
    assert_equal(true, @obj.any? {|x| x >= 3 })
    assert_equal(false, @obj.any? {|x| x > 3 })
    assert_equal(true, @obj.any?)
    assert_equal(false, [false, false, false].any?)
  end

  def test_one
    assert(@obj.one? {|x| x == 3 })
    assert(!(@obj.one? {|x| x == 1 }))
    assert(!(@obj.one? {|x| x == 4 }))
    assert(%w{ant bear cat}.one? {|word| word.length == 4})
    assert(!(%w{ant bear cat}.one? {|word| word.length > 4}))
    assert(!(%w{ant bear cat}.one? {|word| word.length < 4}))
    assert(!([ nil, true, 99 ].one?))
    assert([ nil, true, false ].one?)
  end

  def test_none
    assert(@obj.none? {|x| x == 4 })
    assert(!(@obj.none? {|x| x == 1 }))
    assert(!(@obj.none? {|x| x == 3 }))
    assert(%w{ant bear cat}.none? {|word| word.length == 5})
    assert(!(%w{ant bear cat}.none? {|word| word.length >= 4}))
    assert([].none?)
    assert([nil].none?)
    assert([nil,false].none?)
  end

  def test_min
    assert_equal(1, @obj.min)
    assert_equal(3, @obj.min {|a,b| b <=> a })
    a = %w(albatross dog horse)
    assert_equal("albatross", a.min)
    assert_equal("dog", a.min {|a,b| a.length <=> b.length })
    assert_equal(1, [3,2,1].min)
  end

  def test_max
    assert_equal(3, @obj.max)
    assert_equal(1, @obj.max {|a,b| b <=> a })
    a = %w(albatross dog horse)
    assert_equal("horse", a.max)
    assert_equal("albatross", a.max {|a,b| a.length <=> b.length })
    assert_equal(1, [3,2,1].max{|a,b| b <=> a })
  end

  def test_minmax
    assert_equal([1, 3], @obj.minmax)
    assert_equal([3, 1], @obj.minmax {|a,b| b <=> a })
    a = %w(albatross dog horse)
    assert_equal(["albatross", "horse"], a.minmax)
    assert_equal(["dog", "albatross"], a.minmax {|a,b| a.length <=> b.length })
    assert_equal([1, 3], [2,3,1].minmax)
    assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a })
  end

  def test_min_by
    assert_equal(3, @obj.min_by {|x| -x })
    a = %w(albatross dog horse)
    assert_equal("dog", a.min_by {|x| x.length })
    assert_equal(3, [2,3,1].min_by {|x| -x })
  end

  def test_max_by
    assert_equal(1, @obj.max_by {|x| -x })
    a = %w(albatross dog horse)
    assert_equal("albatross", a.max_by {|x| x.length })
    assert_equal(1, [2,3,1].max_by {|x| -x })
  end

  def test_minmax_by
    assert_equal([3, 1], @obj.minmax_by {|x| -x })
    a = %w(albatross dog horse)
    assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length })
    assert_equal([3, 1], [2,3,1].minmax_by {|x| -x })
  end

  def test_member
    assert(@obj.member?(1))
    assert(!(@obj.member?(4)))
    assert([1,2,3].member?(1))
    assert(!([1,2,3].member?(4)))
  end

  def test_each_with_index
    a = []
    @obj.each_with_index {|x, i| a << [x, i] }
    assert_equal([[1,0],[2,1],[3,2],[1,3],[2,4]], a)

    hash = Hash.new
    %w(cat dog wombat).each_with_index do |item, index|
      hash[item] = index
    end
    assert_equal({"cat"=>0, "wombat"=>2, "dog"=>1}, hash)
  end

  def test_each_with_object
    obj = [0, 1]
    ret = (1..10).each_with_object(obj) {|i, memo|
      memo[0] += i
      memo[1] *= i
    }
    assert_same(obj, ret)
    assert_equal([55, 3628800], ret)
  end

  def test_zip
    assert_equal([[1,1],[2,2],[3,3],[1,1],[2,2]], @obj.zip(@obj))
    a = []
    @obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
    assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)

    ary = Object.new
    def ary.to_a;   [1, 2]; end
    assert_raise(NoMethodError){ %w(a b).zip(ary) }
    def ary.each; [3, 4].each{|e|yield e}; end
    assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
    def ary.to_ary; [5, 6]; end
    assert_equal([[1, 5], [2, 6], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
  end

  def test_take
    assert_equal([1,2,3], @obj.take(3))
  end

  def test_take_while
    assert_equal([1,2], @obj.take_while {|x| x <= 2})
  end

  def test_drop
    assert_equal([3,1,2], @obj.drop(2))
  end

  def test_drop_while
    assert_equal([3,1,2], @obj.drop_while {|x| x <= 2})
  end

  def test_cycle
    assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10))
  end

  def test_callcc
    assert_raise(RuntimeError) do
      c = nil
      @obj.sort_by {|x| callcc {|c2| c ||= c2 }; x }
      c.call
    end

    assert_raise(RuntimeError) do
      c = nil
      o = Object.new
      class << o; self; end.class_eval do
        define_method(:<=>) do |x|
          callcc {|c2| c ||= c2 }
          0
        end
      end
      [o, o].sort_by {|x| x }
      c.call
    end

    assert_raise(RuntimeError) do
      c = nil
      o = Object.new
      class << o; self; end.class_eval do
        define_method(:<=>) do |x|
          callcc {|c2| c ||= c2 }
          0
        end
      end
      [o, o, o].sort_by {|x| x }
      c.call
    end
  end
end