flex_array 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -15
  3. data/.reek.yml +17 -0
  4. data/README.md +11 -0
  5. data/bench/benchmark.rb +243 -0
  6. data/bench/results.txt +97 -0
  7. data/flex_array.gemspec +5 -9
  8. data/lib/flex_array.rb +13 -30
  9. data/lib/flex_array/array.rb +9 -24
  10. data/lib/flex_array/flex_array_append.rb +4 -23
  11. data/lib/flex_array/flex_array_each.rb +33 -208
  12. data/lib/flex_array/flex_array_forever.rb +6 -4
  13. data/lib/flex_array/flex_array_index.rb +4 -29
  14. data/lib/flex_array/flex_array_new.rb +9 -56
  15. data/lib/flex_array/flex_array_process.rb +23 -63
  16. data/lib/flex_array/flex_array_reshape.rb +13 -23
  17. data/lib/flex_array/flex_array_transpose.rb +7 -16
  18. data/lib/flex_array/flex_array_validate.rb +5 -14
  19. data/lib/flex_array/integer.rb +4 -13
  20. data/lib/flex_array/object.rb +4 -13
  21. data/lib/flex_array/range.rb +4 -13
  22. data/lib/flex_array/spec_array.rb +5 -9
  23. data/lib/flex_array/spec_component.rb +14 -22
  24. data/lib/flex_array/version.rb +1 -2
  25. data/rakefile.rb +0 -10
  26. data/reek.txt +9 -2
  27. data/tests/array_test.rb +0 -4
  28. data/tests/flex_array_append_test.rb +0 -23
  29. data/tests/flex_array_each_test.rb +483 -487
  30. data/tests/flex_array_index_test.rb +0 -4
  31. data/tests/flex_array_new_test.rb +0 -4
  32. data/tests/flex_array_reshape_test.rb +4 -4
  33. data/tests/flex_array_test.rb +0 -4
  34. data/tests/flex_array_transpose_test.rb +0 -4
  35. data/tests/flex_array_validate_test.rb +0 -4
  36. data/tests/integer_test.rb +0 -4
  37. data/tests/object_test.rb +0 -4
  38. data/tests/range_test.rb +0 -4
  39. data/tests/spec_array_test.rb +0 -4
  40. data/tests/spec_component_test.rb +0 -4
  41. metadata +12 -43
  42. data/docs/Flex_Array_UG.odt +0 -0
  43. data/docs/Flex_Array_UG_Version_0_3_0.pdf +0 -0
  44. data/flex_array.reek +0 -109
  45. data/sire.rb +0 -82
@@ -1,10 +1,7 @@
1
- #Extensions to Range needed to support flex array.
1
+ # Extensions to Range needed to support flex array.
2
+
2
3
  class Range
3
- #Convert this integer to a limits component.
4
- #<br>Parameters
5
- #* stride - the number of cells separating data with adjacent indexes.
6
- #<br>Returns
7
- #* A SpecComponent object with the same range as self.
4
+ # Convert this integer to a limits component.
8
5
  def to_spec_component(stride)
9
6
  min = self.min
10
7
 
@@ -17,13 +14,7 @@ class Range
17
14
  end
18
15
  end
19
16
 
20
- #Convert this range to an range index against the spec.
21
- #<br>Parameters
22
- #* spec - The spec component used to validate this index.
23
- #<br>Returns
24
- #* A range.
25
- #<br>Exceptions
26
- #* IndexError if the range is not valid.
17
+ # Convert this range to an range index against the spec.
27
18
  def to_index_range(spec)
28
19
  self_min, self_max, spec_max = self.begin, self.end, spec.max
29
20
  self_max -= 1 if self_max > 0 && self.exclude_end?
@@ -1,4 +1,5 @@
1
- #This helper class encapsulates an array of flex array spec components.
1
+ # This helper class encapsulates an array of flex array spec components.
2
+
2
3
  class SpecArray < Array
3
4
  #The number of elements defined by this array specification.
4
5
  attr_reader :spec_count
@@ -6,12 +7,7 @@ class SpecArray < Array
6
7
  #The number of dimensions specified by this specification
7
8
  alias spec_dimensions length
8
9
 
9
- #Create a flex array specification.
10
- #<br>Parameters
11
- #* array_specs - The specification of the flex array subscript limits.
12
- # These can either be an array of containing integers, in which case
13
- # the limits are 0...n or they can be ranges, in which case the
14
- # limits are those of the range.
10
+ # Create a flex array specification.
15
11
  def initialize(array_specs)
16
12
  super(0)
17
13
  @spec_count = 1
@@ -25,7 +21,7 @@ class SpecArray < Array
25
21
  self
26
22
  end
27
23
 
28
- #Is this array specification transposed in any way?
24
+ # Is this array specification transposed in any way?
29
25
  def transposed?
30
26
  check = 1
31
27
 
@@ -37,7 +33,7 @@ class SpecArray < Array
37
33
  false
38
34
  end
39
35
 
40
- #Enlarge the flex array along its first dimension.
36
+ # Enlarge the flex array along its first dimension.
41
37
  def enlarge(growth)
42
38
  self[0].enlarge(growth)
43
39
 
@@ -1,50 +1,44 @@
1
- #This helper class encapsulates a single component of the flex array spec.
1
+ # This helper class encapsulates a single component of the flex array spec.
2
+
2
3
  class SpecComponent
3
- #The range of acceptable values for this limit.
4
+ # The range of acceptable values for this limit.
4
5
  attr_reader :range
5
6
 
6
- #The stride of this array dimension. That is, for each step in this
7
- #dimension, how many steps are required in the low level array?
7
+ # The stride of this array dimension. That is, for each step in this
8
+ # dimension, how many steps are required in the low level array?
8
9
  attr_reader :stride
9
10
 
10
- #Create a limits component from its constituent data.
11
- #<br>Parameters
12
- #* range - the range of values for this index limit.
13
- #* stride - the number of cells separating data with adjacent indexes.
11
+ # Create a limits component from its constituent data.
14
12
  def initialize(range, stride)
15
13
  @range, @stride = range, stride
16
14
  end
17
15
 
18
- #Forward '=== value' to the range.
16
+ # Forward '=== value' to the range.
19
17
  def ===(value)
20
18
  @range === value
21
19
  end
22
20
 
23
- #Limits are equal if their ranges are equal.
24
- #<br>Returns
25
- #* true if the spec components have equal ranges.
21
+ # Limits are equal if their ranges are equal.
26
22
  def ==(other)
27
23
  @range == other.range
28
24
  end
29
25
 
30
- #Forward 'min' to the range.
26
+ # Forward 'min' to the range.
31
27
  def min
32
28
  @range.min
33
29
  end
34
30
 
35
- #Forward 'max' to the range.
31
+ # Forward 'max' to the range.
36
32
  def max
37
33
  @range.max
38
34
  end
39
35
 
40
- #Forward 'each' and the block to the range.
36
+ # Forward 'each' and the block to the range.
41
37
  def each(&block)
42
38
  @range.each(&block)
43
39
  end
44
40
 
45
- #Compute the span of indexes in this limit component.
46
- #<br>Returns
47
- #* The span of the range or zero if there is none.
41
+ # Compute the span of indexes in this limit component.
48
42
  def span
49
43
  if @range.none?
50
44
  0
@@ -53,7 +47,7 @@ class SpecComponent
53
47
  end
54
48
  end
55
49
 
56
- #Enlarge the range of this spec by the growth term.
50
+ # Enlarge the range of this spec by the growth term.
57
51
  def enlarge(growth)
58
52
  if @range.none?
59
53
  @range = 0...growth
@@ -62,9 +56,7 @@ class SpecComponent
62
56
  end
63
57
  end
64
58
 
65
- #Compute the step required for the index value.
66
- #<br>Returns
67
- #* The number of array cells to be skipped for this index.
59
+ # Compute the step required for the index value.
68
60
  def index_step(index)
69
61
  (index - @range.min) * @stride
70
62
  end
@@ -1,7 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- #* version.rb - The version string for flex array.
4
3
  class FlexArray
5
4
  #The version string for flex array.
6
- VERSION = "0.3.5"
5
+ VERSION = "0.3.6".freeze
7
6
  end
@@ -2,18 +2,8 @@
2
2
  # coding: utf-8
3
3
 
4
4
  require 'rake/testtask'
5
- require 'rdoc/task'
6
5
  require "bundler/gem_tasks"
7
6
 
8
- RDoc::Task.new do |rdoc|
9
- rdoc.rdoc_dir = "rdoc"
10
-
11
- #List out all the files to be documented.
12
- rdoc.rdoc_files.include("lib/**/*.rb", "license.txt", "readme.txt")
13
-
14
- rdoc.options << '--visibility' << 'private'
15
- end
16
-
17
7
  Rake::TestTask.new do |t|
18
8
  #List out all the test files.
19
9
  t.test_files = FileList['tests/**/*.rb']
data/reek.txt CHANGED
@@ -1,2 +1,9 @@
1
-
2
- 0 total warnings
1
+ lib/flex_array/flex_array_each.rb -- 2 warnings:
2
+ [178, 198, 217, 237]:RepeatedConditional: FlexArray tests 'blk' at least 4 times [https://github.com/troessner/reek/blob/v5.0.2/docs/Repeated-Conditional.md]
3
+ [180, 200, 219, 239]:RepeatedConditional: FlexArray tests 'blk.call(@array_data[posn])' at least 4 times [https://github.com/troessner/reek/blob/v5.0.2/docs/Repeated-Conditional.md]
4
+ lib/flex_array/spec_array.rb -- 1 warning:
5
+ [3]:SubclassedFromCoreClass: SpecArray inherits from core class 'Array' [https://github.com/troessner/reek/blob/v5.0.2/docs/Subclassed-From-Core-Class.md]
6
+ lib/flex_array.rb -- 2 warnings:
7
+ [40]:Attribute: FlexArray#array_data is a writable attribute [https://github.com/troessner/reek/blob/v5.0.2/docs/Attribute.md]
8
+ [37]:Attribute: FlexArray#array_specs is a writable attribute [https://github.com/troessner/reek/blob/v5.0.2/docs/Attribute.md]
9
+ 5 total warnings
@@ -1,13 +1,9 @@
1
1
  require_relative '../lib/flex_array'
2
2
  gem 'minitest'
3
3
  require 'minitest/autorun'
4
- require 'minitest_visible'
5
4
 
6
5
  class ArrayTester < Minitest::Test
7
6
 
8
- #Track mini-test progress.
9
- include MinitestVisible
10
-
11
7
  def test_the_limits_method
12
8
  test = [1,2,3,4,5,6]
13
9
  assert_equal([0...6], test.limits)
@@ -1,13 +1,9 @@
1
1
  require_relative '../lib/flex_array'
2
2
  gem 'minitest'
3
3
  require 'minitest/autorun'
4
- require 'minitest_visible'
5
4
 
6
5
  class FlexArrayAppendTester < Minitest::Test
7
6
 
8
- #Track mini-test progress.
9
- include MinitestVisible
10
-
11
7
  def test_append_onto_an_existing_array
12
8
  f = FlexArray.new([1,3], "Title")
13
9
  assert_equal(2, f.dimensions)
@@ -54,23 +50,4 @@ class FlexArrayAppendTester < Minitest::Test
54
50
  assert_raises(ArgumentError) { f << g }
55
51
  end
56
52
 
57
- def test_the_copy_data_method
58
- f = FlexArray.new([2,3], "Title")
59
- g = FlexArray.new([2,3], "Fight")
60
- f.copy_data(g)
61
- assert_equal(f.array_data, g.array_data)
62
- assert(f.array_data.object_id != g.array_data.object_id)
63
- end
64
-
65
- def test_copy_data_method_failures
66
- f = FlexArray.new([2,3], "Title")
67
- g = FlexArray.new([2,4], "Fight")
68
- assert_raises(ArgumentError) { f.copy_data(g) }
69
-
70
- g = FlexArray.new([1,3], "Fight")
71
- assert_raises(ArgumentError) { f.copy_data(g) }
72
-
73
- g = FlexArray.new([2,2], "Fight")
74
- assert_raises(ArgumentError) { f.copy_data(g) }
75
- end
76
53
  end
@@ -1,487 +1,483 @@
1
- require_relative '../lib/flex_array'
2
- gem 'minitest'
3
- require 'minitest/autorun'
4
- require 'minitest_visible'
5
-
6
- class FlexArrayEachTester < Minitest::Test
7
-
8
- #Track mini-test progress.
9
- include MinitestVisible
10
-
11
- def test_each
12
- d = [0,1,2,3,4,5,6,7,8]
13
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
14
- it = d.each
15
-
16
- result = q.each do |v|
17
- assert_equal(it.next, v)
18
- end
19
-
20
- assert_equal(result, q)
21
-
22
- it = q.each
23
-
24
- result = d.each do |v|
25
- assert_equal(it.next, v)
26
- end
27
- end
28
-
29
- def test_select_each
30
- d = [0,1,2,3,4,5,6,7,8]
31
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
32
- it = d.each
33
-
34
- result = q.select_each([:all]) do |v|
35
- assert_equal(it.next, v)
36
- end
37
-
38
- assert_equal(result, q)
39
-
40
- it = q.select_each([:all])
41
- d.each do |v|
42
- assert_equal(it.next, v)
43
- end
44
-
45
- it = d.each
46
-
47
- result = q.select_each([:all, :all]) do |v|
48
- assert_equal(it.next, v)
49
- end
50
-
51
- d = [0,1,2]
52
- it = d.each
53
-
54
- result = q.select_each([0, :all]) do |v|
55
- assert_equal(it.next, v)
56
- end
57
-
58
- d = [0,3,6]
59
- it = d.each
60
-
61
- result = q.select_each([:all, 0]) do |v|
62
- assert_equal(it.next, v)
63
- end
64
- end
65
-
66
- def test_each_with_index
67
- d1 = [0,1,2,3,4,5,6,7,8]
68
- d2 = [[0,0], [0,1], [0,2],
69
- [1,0], [1,1], [1,2],
70
- [2,0], [2,1], [2,2]]
71
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
72
- i1 = d1.each
73
- i2 = d2.each
74
-
75
- q.each_with_index do |v, i|
76
- assert_equal(v, i1.next)
77
- assert_equal(i, i2.next)
78
- end
79
-
80
- iq = q.each_with_index
81
- i2 = d2.each
82
-
83
- d1.each do |v|
84
- a,b = iq.next
85
- assert_equal(a, v)
86
- assert_equal(b, i2.next)
87
- end
88
- end
89
-
90
- def test_select_each_with_index
91
- d1 = [0,1,2,3,4,5,6,7,8]
92
- d2 = [[0,0], [0,1], [0,2],
93
- [1,0], [1,1], [1,2],
94
- [2,0], [2,1], [2,2]]
95
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
96
- i1 = d1.each
97
- i2 = d2.each
98
-
99
- q.select_each_with_index([:all]) do |v, i|
100
- assert_equal(v, i1.next)
101
- assert_equal(i, i2.next)
102
- end
103
-
104
- iq = q.select_each_with_index([:all])
105
- i2 = d2.each
106
-
107
- d1.each do |v|
108
- a,b = iq.next
109
- assert_equal(a, v)
110
- assert_equal(b, i2.next)
111
- end
112
-
113
- i1 = d1.each
114
- i2 = d2.each
115
-
116
- q.select_each_with_index([:all, :all]) do |v, i|
117
- assert_equal(v, i1.next)
118
- assert_equal(i, i2.next)
119
- end
120
-
121
- iq = q.select_each_with_index([:all, :all])
122
- i2 = d2.each
123
-
124
- d1.each do |v|
125
- a,b = iq.next
126
- assert_equal(a, v)
127
- assert_equal(b, i2.next)
128
- end
129
-
130
- d1 = [0,1,2]
131
- d2 = [[0,0], [0,1], [0,2]]
132
-
133
- i1 = d1.each
134
- i2 = d2.each
135
-
136
- q.select_each_with_index([0, :all]) do |v, i|
137
- assert_equal(v, i1.next)
138
- assert_equal(i, i2.next)
139
- end
140
-
141
- d1 = [0,3,6]
142
- d2 = [[0,0], [1,0], [2,0]]
143
-
144
- i1 = d1.each
145
- i2 = d2.each
146
-
147
- q.select_each_with_index([:all, 0]) do |v, i|
148
- assert_equal(v, i1.next)
149
- assert_equal(i, i2.next)
150
- end
151
- end
152
-
153
- def test_each_raw
154
- d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
155
- d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
156
- d2 = [[0,0], [0,1], [0,2],
157
- [1,0], [1,1], [1,2],
158
- [2,0], [2,1], [2,2]]
159
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
160
- i0 = d0.each
161
- i1 = d1.each
162
- i2 = d2.each
163
-
164
- q._each_raw do |i, posn|
165
- assert_equal(posn, i0.next)
166
- assert_equal(q.array_data[posn], i1.next)
167
- assert_equal(i, i2.next)
168
- end
169
-
170
- iq = q._each_raw
171
- i1 = d1.each
172
- i2 = d2.each
173
-
174
- d0.each do |v|
175
- i, posn = iq.next
176
- assert_equal(posn, v)
177
- assert_equal(q.array_data[posn], i1.next)
178
- assert_equal(i, i2.next)
179
- end
180
- end
181
-
182
- def test_select_each_raw
183
- d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
184
- d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
185
- d2 = [[0,0], [0,1], [0,2],
186
- [1,0], [1,1], [1,2],
187
- [2,0], [2,1], [2,2]]
188
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
189
- i0 = d0.each
190
- i1 = d1.each
191
- i2 = d2.each
192
-
193
- q._select_each_raw([:all]) do |i, posn|
194
- assert_equal(posn, i0.next)
195
- assert_equal(q.array_data[posn], i1.next)
196
- assert_equal(i, i2.next)
197
- end
198
-
199
- i0 = d0.each
200
- i1 = d1.each
201
- i2 = d2.each
202
-
203
- q._select_each_raw([:all, :all]) do |i, posn|
204
- assert_equal(posn, i0.next)
205
- assert_equal(q.array_data[posn], i1.next)
206
- assert_equal(i, i2.next)
207
- end
208
-
209
- d0 = [0, 1, 2]
210
- d1 = [100, 101, 102]
211
- d2 = [[0,0], [0,1], [0,2]]
212
-
213
- i0 = d0.each
214
- i1 = d1.each
215
- i2 = d2.each
216
-
217
- q._select_each_raw([0, :all]) do |i, posn|
218
- assert_equal(posn, i0.next)
219
- assert_equal(q.array_data[posn], i1.next)
220
- assert_equal(i, i2.next)
221
- end
222
-
223
- d0 = [0, 3, 6]
224
- d1 = [100, 103, 106]
225
- d2 = [[0,0], [1,0], [2,0]]
226
-
227
- i0 = d0.each
228
- i1 = d1.each
229
- i2 = d2.each
230
-
231
- q._select_each_raw([:all, 0]) do |i, posn|
232
- assert_equal(posn, i0.next)
233
- assert_equal(q.array_data[posn], i1.next)
234
- assert_equal(i, i2.next)
235
- end
236
- end
237
-
238
- def test_cycle
239
- d = [0,1,2,3,4,5,6,7,8,0,1,2]
240
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
241
- i = 0
242
-
243
- q.cycle do |v|
244
- assert_equal(d[i], v)
245
- i += 1
246
- break if i == 12
247
- end
248
-
249
- iq = q.cycle
250
-
251
- d.each do |v|
252
- assert_equal(iq.next, v)
253
- end
254
- end
255
-
256
- def test_select_cycle
257
- d = [0,1,2,3,4,5,6,7,8,0,1,2]
258
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
259
- i = 0
260
-
261
- q.select_cycle([:all]) do |v|
262
- assert_equal(d[i], v)
263
- i += 1
264
- break if i == 12
265
- end
266
-
267
- iq = q.select_cycle([:all])
268
-
269
- d.each do |v|
270
- assert_equal(iq.next, v)
271
- end
272
-
273
- i = 0
274
-
275
- q.select_cycle([:all, :all]) do |v|
276
- assert_equal(d[i], v)
277
- i += 1
278
- break if i == 12
279
- end
280
-
281
- iq = q.select_cycle([:all, :all])
282
-
283
- d.each do |v|
284
- assert_equal(iq.next, v)
285
- end
286
-
287
- d = [0,1,2,0,1,2,0,1,2,0,1,2]
288
- i = 0
289
-
290
- q.select_cycle([0, :all]) do |v|
291
- assert_equal(d[i], v)
292
- i += 1
293
- break if i == 12
294
- end
295
-
296
- iq = q.select_cycle([0, :all])
297
-
298
- d.each do |v|
299
- assert_equal(iq.next, v)
300
- end
301
-
302
- d = [0,3,6,0,3,6,0,3,6,0,3,6]
303
- i = 0
304
-
305
- q.select_cycle([:all, 0]) do |v|
306
- assert_equal(d[i], v)
307
- i += 1
308
- break if i == 12
309
- end
310
-
311
- iq = q.select_cycle([:all, 0])
312
-
313
- d.each do |v|
314
- assert_equal(iq.next, v)
315
- end
316
- end
317
-
318
- def test_flatten_collect
319
- d = [0,1,4,9,16,25,36,49,64]
320
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
321
-
322
- b = q.flatten_collect {|v| v * v }
323
- assert_equal(d, b)
324
- end
325
-
326
- def test_select_flatten_collect
327
- d = [0,1,4,9,16,25,36,49,64]
328
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
329
-
330
- b = q.select_flatten_collect([:all]) {|v| v * v }
331
- assert_equal(d, b)
332
-
333
- b = q.select_flatten_collect([:all, :all]) {|v| v * v }
334
- assert_equal(d, b)
335
-
336
- d = [0,1,4]
337
- b = q.select_flatten_collect([0, :all]) {|v| v * v }
338
- assert_equal(d, b)
339
-
340
- d = [0,9,36]
341
- b = q.select_flatten_collect([:all, 0]) {|v| v * v }
342
- assert_equal(d, b)
343
- end
344
-
345
-
346
-
347
-
348
-
349
- def test_collect
350
- d = [0,1,4,9,16,25,36,49,64]
351
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
352
- a = q.collect {|v| v * v }
353
- assert_equal(d, a.array_data)
354
- end
355
-
356
- def test_select_collect
357
- d = [0,1,4,9,16,25,36,49,64]
358
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
359
- a = q.select_collect([:all]) {|v| v * v }
360
- assert_equal(d, a.array_data)
361
-
362
- d = [0,1,16,81,256,625,1296,2401,4096]
363
- b = a.select_collect([:all, :all]) {|v| v * v }
364
- assert_equal(d, b.array_data)
365
-
366
- d = [0,1,256,81,256,625,1296,2401,4096]
367
- c = b.select_collect([0, :all]) {|v| v * v }
368
- assert_equal(d, c.array_data)
369
-
370
- d = [0,1,256,6561,256,625,1679616,2401,4096]
371
- e = c.select_collect([:all, 0]) {|v| v * v }
372
- assert_equal(d, e.array_data)
373
- end
374
-
375
- def test_collect_em
376
- d = [0,1,4,9,16,25,36,49,64]
377
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
378
- q.collect! {|v| v * v }
379
- assert_equal(d, q.array_data)
380
- end
381
-
382
- def test_select_collect_em
383
- d = [0,1,4,9,16,25,36,49,64]
384
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
385
- q.select_collect!([:all]) {|v| v * v }
386
- assert_equal(d, q.array_data)
387
-
388
- d = [0,1,16,81,256,625,1296,2401,4096]
389
- q.select_collect!([:all, :all]) {|v| v * v }
390
- assert_equal(d, q.array_data)
391
-
392
- d = [0,1,256,81,256,625,1296,2401,4096]
393
- q.select_collect!([0, :all]) {|v| v * v }
394
- assert_equal(d, q.array_data)
395
-
396
- d = [0,1,256,6561,256,625,1679616,2401,4096]
397
- q.select_collect!([:all, 0]) {|v| v * v }
398
- assert_equal(d, q.array_data)
399
- end
400
-
401
- def test_find_index
402
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
403
- i = q.find_index(107)
404
- assert_equal([2, 1], i)
405
-
406
- i = q.find_index { |v| v > 106 }
407
- assert_equal([2, 1], i)
408
-
409
- i = q.find_index(300)
410
- assert_equal(nil, i)
411
-
412
- i = q.find_index { |v| v > 206 }
413
- assert_equal(nil, i)
414
- end
415
-
416
- def test_select_find_index
417
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
418
- i = q.select_find_index([:all], 107)
419
- assert_equal([2, 1], i)
420
-
421
- i = q.select_find_index([:all, :all], 107)
422
- assert_equal([2, 1], i)
423
-
424
- i = q.select_find_index([0, :all], 107)
425
- assert_equal(nil, i)
426
-
427
- i = q.select_find_index([1, :all], 107)
428
- assert_equal(nil, i)
429
-
430
- i = q.select_find_index([2, :all], 107)
431
- assert_equal([2, 1], i)
432
-
433
- i = q.select_find_index([:all, 0], 107)
434
- assert_equal(nil, i)
435
-
436
- i = q.select_find_index([:all, 1], 107)
437
- assert_equal([2, 1], i)
438
-
439
- i = q.select_find_index([:all, 2], 107)
440
- assert_equal(nil, i)
441
- end
442
-
443
- def test_find_indexes
444
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
445
- i = q.find_indexes(107)
446
- assert_equal([[2, 1]], i)
447
-
448
- i = q.find_indexes { |v| v == 107 }
449
- assert_equal([[2, 1]], i)
450
-
451
- i = q.find_indexes { |v| v > 106 }
452
- assert_equal([[2, 1], [2, 2]], i)
453
-
454
- i = q.find_indexes(307)
455
- assert_equal([], i)
456
-
457
- i = q.find_indexes { |v| v > 406 }
458
- assert_equal([], i)
459
- end
460
-
461
- def test_select_find_indexes
462
- q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
463
- i = q.select_find_indexes([:all], 107)
464
- assert_equal([[2, 1]], i)
465
-
466
- i = q.select_find_indexes([:all, :all], 107)
467
- assert_equal([[2, 1]], i)
468
-
469
- i = q.select_find_indexes([0, :all], 107)
470
- assert_equal([], i)
471
-
472
- i = q.select_find_indexes([1, :all], 107)
473
- assert_equal([], i)
474
-
475
- i = q.select_find_indexes([2, :all], 107)
476
- assert_equal([[2, 1]], i)
477
-
478
- i = q.select_find_indexes([:all, 0], 107)
479
- assert_equal([], i)
480
-
481
- i = q.select_find_indexes([:all, 1], 107)
482
- assert_equal([[2, 1]], i)
483
-
484
- i = q.select_find_indexes([:all, 2], 107)
485
- assert_equal([], i)
486
- end
487
- end
1
+ require_relative '../lib/flex_array'
2
+ gem 'minitest'
3
+ require 'minitest/autorun'
4
+
5
+ class FlexArrayEachTester < Minitest::Test
6
+
7
+ def test_each
8
+ d = [0,1,2,3,4,5,6,7,8]
9
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
10
+ it = d.each
11
+
12
+ result = q.each do |v|
13
+ assert_equal(it.next, v)
14
+ end
15
+
16
+ assert_equal(result, q)
17
+
18
+ it = q.each
19
+
20
+ result = d.each do |v|
21
+ assert_equal(it.next, v)
22
+ end
23
+ end
24
+
25
+ def test_select_each
26
+ d = [0,1,2,3,4,5,6,7,8]
27
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
28
+ it = d.each
29
+
30
+ result = q.select_each([:all]) do |v|
31
+ assert_equal(it.next, v)
32
+ end
33
+
34
+ assert_equal(result, q)
35
+
36
+ it = q.select_each([:all])
37
+ d.each do |v|
38
+ assert_equal(it.next, v)
39
+ end
40
+
41
+ it = d.each
42
+
43
+ result = q.select_each([:all, :all]) do |v|
44
+ assert_equal(it.next, v)
45
+ end
46
+
47
+ d = [0,1,2]
48
+ it = d.each
49
+
50
+ result = q.select_each([0, :all]) do |v|
51
+ assert_equal(it.next, v)
52
+ end
53
+
54
+ d = [0,3,6]
55
+ it = d.each
56
+
57
+ result = q.select_each([:all, 0]) do |v|
58
+ assert_equal(it.next, v)
59
+ end
60
+ end
61
+
62
+ def test_each_with_index
63
+ d1 = [0,1,2,3,4,5,6,7,8]
64
+ d2 = [[0,0], [0,1], [0,2],
65
+ [1,0], [1,1], [1,2],
66
+ [2,0], [2,1], [2,2]]
67
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
68
+ i1 = d1.each
69
+ i2 = d2.each
70
+
71
+ q.each_with_index do |v, i|
72
+ assert_equal(v, i1.next)
73
+ assert_equal(i, i2.next)
74
+ end
75
+
76
+ iq = q.each_with_index
77
+ i2 = d2.each
78
+
79
+ d1.each do |v|
80
+ a,b = iq.next
81
+ assert_equal(a, v)
82
+ assert_equal(b, i2.next)
83
+ end
84
+ end
85
+
86
+ def test_select_each_with_index
87
+ d1 = [0,1,2,3,4,5,6,7,8]
88
+ d2 = [[0,0], [0,1], [0,2],
89
+ [1,0], [1,1], [1,2],
90
+ [2,0], [2,1], [2,2]]
91
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
92
+ i1 = d1.each
93
+ i2 = d2.each
94
+
95
+ q.select_each_with_index([:all]) do |v, i|
96
+ assert_equal(v, i1.next)
97
+ assert_equal(i, i2.next)
98
+ end
99
+
100
+ iq = q.select_each_with_index([:all])
101
+ i2 = d2.each
102
+
103
+ d1.each do |v|
104
+ a,b = iq.next
105
+ assert_equal(a, v)
106
+ assert_equal(b, i2.next)
107
+ end
108
+
109
+ i1 = d1.each
110
+ i2 = d2.each
111
+
112
+ q.select_each_with_index([:all, :all]) do |v, i|
113
+ assert_equal(v, i1.next)
114
+ assert_equal(i, i2.next)
115
+ end
116
+
117
+ iq = q.select_each_with_index([:all, :all])
118
+ i2 = d2.each
119
+
120
+ d1.each do |v|
121
+ a,b = iq.next
122
+ assert_equal(a, v)
123
+ assert_equal(b, i2.next)
124
+ end
125
+
126
+ d1 = [0,1,2]
127
+ d2 = [[0,0], [0,1], [0,2]]
128
+
129
+ i1 = d1.each
130
+ i2 = d2.each
131
+
132
+ q.select_each_with_index([0, :all]) do |v, i|
133
+ assert_equal(v, i1.next)
134
+ assert_equal(i, i2.next)
135
+ end
136
+
137
+ d1 = [0,3,6]
138
+ d2 = [[0,0], [1,0], [2,0]]
139
+
140
+ i1 = d1.each
141
+ i2 = d2.each
142
+
143
+ q.select_each_with_index([:all, 0]) do |v, i|
144
+ assert_equal(v, i1.next)
145
+ assert_equal(i, i2.next)
146
+ end
147
+ end
148
+
149
+ def test_each_raw
150
+ d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
151
+ d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
152
+ d2 = [[0,0], [0,1], [0,2],
153
+ [1,0], [1,1], [1,2],
154
+ [2,0], [2,1], [2,2]]
155
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
156
+ i0 = d0.each
157
+ i1 = d1.each
158
+ i2 = d2.each
159
+
160
+ q._each_raw do |i, posn|
161
+ assert_equal(posn, i0.next)
162
+ assert_equal(q.array_data[posn], i1.next)
163
+ assert_equal(i, i2.next)
164
+ end
165
+
166
+ iq = q._each_raw
167
+ i1 = d1.each
168
+ i2 = d2.each
169
+
170
+ d0.each do |v|
171
+ i, posn = iq.next
172
+ assert_equal(posn, v)
173
+ assert_equal(q.array_data[posn], i1.next)
174
+ assert_equal(i, i2.next)
175
+ end
176
+ end
177
+
178
+ def test_select_each_raw
179
+ d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
180
+ d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
181
+ d2 = [[0,0], [0,1], [0,2],
182
+ [1,0], [1,1], [1,2],
183
+ [2,0], [2,1], [2,2]]
184
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
185
+ i0 = d0.each
186
+ i1 = d1.each
187
+ i2 = d2.each
188
+
189
+ q._select_each_raw([:all]) do |i, posn|
190
+ assert_equal(posn, i0.next)
191
+ assert_equal(q.array_data[posn], i1.next)
192
+ assert_equal(i, i2.next)
193
+ end
194
+
195
+ i0 = d0.each
196
+ i1 = d1.each
197
+ i2 = d2.each
198
+
199
+ q._select_each_raw([:all, :all]) do |i, posn|
200
+ assert_equal(posn, i0.next)
201
+ assert_equal(q.array_data[posn], i1.next)
202
+ assert_equal(i, i2.next)
203
+ end
204
+
205
+ d0 = [0, 1, 2]
206
+ d1 = [100, 101, 102]
207
+ d2 = [[0,0], [0,1], [0,2]]
208
+
209
+ i0 = d0.each
210
+ i1 = d1.each
211
+ i2 = d2.each
212
+
213
+ q._select_each_raw([0, :all]) do |i, posn|
214
+ assert_equal(posn, i0.next)
215
+ assert_equal(q.array_data[posn], i1.next)
216
+ assert_equal(i, i2.next)
217
+ end
218
+
219
+ d0 = [0, 3, 6]
220
+ d1 = [100, 103, 106]
221
+ d2 = [[0,0], [1,0], [2,0]]
222
+
223
+ i0 = d0.each
224
+ i1 = d1.each
225
+ i2 = d2.each
226
+
227
+ q._select_each_raw([:all, 0]) do |i, posn|
228
+ assert_equal(posn, i0.next)
229
+ assert_equal(q.array_data[posn], i1.next)
230
+ assert_equal(i, i2.next)
231
+ end
232
+ end
233
+
234
+ def test_cycle
235
+ d = [0,1,2,3,4,5,6,7,8,0,1,2]
236
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
237
+ i = 0
238
+
239
+ q.cycle do |v|
240
+ assert_equal(d[i], v)
241
+ i += 1
242
+ break if i == 12
243
+ end
244
+
245
+ iq = q.cycle
246
+
247
+ d.each do |v|
248
+ assert_equal(iq.next, v)
249
+ end
250
+ end
251
+
252
+ def test_select_cycle
253
+ d = [0,1,2,3,4,5,6,7,8,0,1,2]
254
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
255
+ i = 0
256
+
257
+ q.select_cycle([:all]) do |v|
258
+ assert_equal(d[i], v)
259
+ i += 1
260
+ break if i == 12
261
+ end
262
+
263
+ iq = q.select_cycle([:all])
264
+
265
+ d.each do |v|
266
+ assert_equal(iq.next, v)
267
+ end
268
+
269
+ i = 0
270
+
271
+ q.select_cycle([:all, :all]) do |v|
272
+ assert_equal(d[i], v)
273
+ i += 1
274
+ break if i == 12
275
+ end
276
+
277
+ iq = q.select_cycle([:all, :all])
278
+
279
+ d.each do |v|
280
+ assert_equal(iq.next, v)
281
+ end
282
+
283
+ d = [0,1,2,0,1,2,0,1,2,0,1,2]
284
+ i = 0
285
+
286
+ q.select_cycle([0, :all]) do |v|
287
+ assert_equal(d[i], v)
288
+ i += 1
289
+ break if i == 12
290
+ end
291
+
292
+ iq = q.select_cycle([0, :all])
293
+
294
+ d.each do |v|
295
+ assert_equal(iq.next, v)
296
+ end
297
+
298
+ d = [0,3,6,0,3,6,0,3,6,0,3,6]
299
+ i = 0
300
+
301
+ q.select_cycle([:all, 0]) do |v|
302
+ assert_equal(d[i], v)
303
+ i += 1
304
+ break if i == 12
305
+ end
306
+
307
+ iq = q.select_cycle([:all, 0])
308
+
309
+ d.each do |v|
310
+ assert_equal(iq.next, v)
311
+ end
312
+ end
313
+
314
+ def test_flatten_collect
315
+ d = [0,1,4,9,16,25,36,49,64]
316
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
317
+
318
+ b = q.flatten_collect {|v| v * v }
319
+ assert_equal(d, b)
320
+ end
321
+
322
+ def test_select_flatten_collect
323
+ d = [0,1,4,9,16,25,36,49,64]
324
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
325
+
326
+ b = q.select_flatten_collect([:all]) {|v| v * v }
327
+ assert_equal(d, b)
328
+
329
+ b = q.select_flatten_collect([:all, :all]) {|v| v * v }
330
+ assert_equal(d, b)
331
+
332
+ d = [0,1,4]
333
+ b = q.select_flatten_collect([0, :all]) {|v| v * v }
334
+ assert_equal(d, b)
335
+
336
+ d = [0,9,36]
337
+ b = q.select_flatten_collect([:all, 0]) {|v| v * v }
338
+ assert_equal(d, b)
339
+ end
340
+
341
+
342
+
343
+
344
+
345
+ def test_collect
346
+ d = [0,1,4,9,16,25,36,49,64]
347
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
348
+ a = q.collect {|v| v * v }
349
+ assert_equal(d, a.array_data)
350
+ end
351
+
352
+ def test_select_collect
353
+ d = [0,1,4,9,16,25,36,49,64]
354
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
355
+ a = q.select_collect([:all]) {|v| v * v }
356
+ assert_equal(d, a.array_data)
357
+
358
+ d = [0,1,16,81,256,625,1296,2401,4096]
359
+ b = a.select_collect([:all, :all]) {|v| v * v }
360
+ assert_equal(d, b.array_data)
361
+
362
+ d = [0,1,256,81,256,625,1296,2401,4096]
363
+ c = b.select_collect([0, :all]) {|v| v * v }
364
+ assert_equal(d, c.array_data)
365
+
366
+ d = [0,1,256,6561,256,625,1679616,2401,4096]
367
+ e = c.select_collect([:all, 0]) {|v| v * v }
368
+ assert_equal(d, e.array_data)
369
+ end
370
+
371
+ def test_collect_em
372
+ d = [0,1,4,9,16,25,36,49,64]
373
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
374
+ q.collect! {|v| v * v }
375
+ assert_equal(d, q.array_data)
376
+ end
377
+
378
+ def test_select_collect_em
379
+ d = [0,1,4,9,16,25,36,49,64]
380
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
381
+ q.select_collect!([:all]) {|v| v * v }
382
+ assert_equal(d, q.array_data)
383
+
384
+ d = [0,1,16,81,256,625,1296,2401,4096]
385
+ q.select_collect!([:all, :all]) {|v| v * v }
386
+ assert_equal(d, q.array_data)
387
+
388
+ d = [0,1,256,81,256,625,1296,2401,4096]
389
+ q.select_collect!([0, :all]) {|v| v * v }
390
+ assert_equal(d, q.array_data)
391
+
392
+ d = [0,1,256,6561,256,625,1679616,2401,4096]
393
+ q.select_collect!([:all, 0]) {|v| v * v }
394
+ assert_equal(d, q.array_data)
395
+ end
396
+
397
+ def test_find_index
398
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
399
+ i = q.find_index(107)
400
+ assert_equal([2, 1], i)
401
+
402
+ i = q.find_index { |v| v > 106 }
403
+ assert_equal([2, 1], i)
404
+
405
+ i = q.find_index(300)
406
+ assert_nil(i)
407
+
408
+ i = q.find_index { |v| v > 206 }
409
+ assert_nil(i)
410
+ end
411
+
412
+ def test_select_find_index
413
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
414
+ i = q.select_find_index([:all], 107)
415
+ assert_equal([2, 1], i)
416
+
417
+ i = q.select_find_index([:all, :all], 107)
418
+ assert_equal([2, 1], i)
419
+
420
+ i = q.select_find_index([0, :all], 107)
421
+ assert_nil(i)
422
+
423
+ i = q.select_find_index([1, :all], 107)
424
+ assert_nil(i)
425
+
426
+ i = q.select_find_index([2, :all], 107)
427
+ assert_equal([2, 1], i)
428
+
429
+ i = q.select_find_index([:all, 0], 107)
430
+ assert_nil(i)
431
+
432
+ i = q.select_find_index([:all, 1], 107)
433
+ assert_equal([2, 1], i)
434
+
435
+ i = q.select_find_index([:all, 2], 107)
436
+ assert_nil(i)
437
+ end
438
+
439
+ def test_find_indexes
440
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
441
+ i = q.find_indexes(107)
442
+ assert_equal([[2, 1]], i)
443
+
444
+ i = q.find_indexes { |v| v == 107 }
445
+ assert_equal([[2, 1]], i)
446
+
447
+ i = q.find_indexes { |v| v > 106 }
448
+ assert_equal([[2, 1], [2, 2]], i)
449
+
450
+ i = q.find_indexes(307)
451
+ assert_equal([], i)
452
+
453
+ i = q.find_indexes { |v| v > 406 }
454
+ assert_equal([], i)
455
+ end
456
+
457
+ def test_select_find_indexes
458
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
459
+ i = q.select_find_indexes([:all], 107)
460
+ assert_equal([[2, 1]], i)
461
+
462
+ i = q.select_find_indexes([:all, :all], 107)
463
+ assert_equal([[2, 1]], i)
464
+
465
+ i = q.select_find_indexes([0, :all], 107)
466
+ assert_equal([], i)
467
+
468
+ i = q.select_find_indexes([1, :all], 107)
469
+ assert_equal([], i)
470
+
471
+ i = q.select_find_indexes([2, :all], 107)
472
+ assert_equal([[2, 1]], i)
473
+
474
+ i = q.select_find_indexes([:all, 0], 107)
475
+ assert_equal([], i)
476
+
477
+ i = q.select_find_indexes([:all, 1], 107)
478
+ assert_equal([[2, 1]], i)
479
+
480
+ i = q.select_find_indexes([:all, 2], 107)
481
+ assert_equal([], i)
482
+ end
483
+ end