flex_array 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/flex_array/array.rb +55 -0
- data/lib/flex_array/flex_array_append.rb +51 -0
- data/lib/flex_array/flex_array_each.rb +98 -0
- data/lib/flex_array/flex_array_index.rb +42 -0
- data/lib/flex_array/flex_array_new.rb +98 -0
- data/lib/flex_array/flex_array_process.rb +103 -0
- data/lib/flex_array/flex_array_reshape.rb +46 -0
- data/lib/flex_array/flex_array_transpose.rb +17 -0
- data/lib/flex_array/flex_array_validate.rb +37 -0
- data/lib/flex_array/integer.rb +34 -0
- data/lib/flex_array/object.rb +29 -0
- data/lib/flex_array/range.rb +44 -0
- data/lib/flex_array/spec_component.rb +71 -0
- data/lib/flex_array.rb +89 -0
- data/license.txt +21 -0
- data/rakefile.rb +44 -0
- data/readme.txt +2 -0
- data/reek.txt +29 -0
- data/tests/array_test.rb +62 -0
- data/tests/flex_array_append_test.rb +82 -0
- data/tests/flex_array_each_test.rb +120 -0
- data/tests/flex_array_index_test.rb +222 -0
- data/tests/flex_array_new_test.rb +131 -0
- data/tests/flex_array_reshape_test.rb +76 -0
- data/tests/flex_array_test.rb +58 -0
- data/tests/flex_array_transpose_test.rb +32 -0
- data/tests/flex_array_validate_test.rb +29 -0
- data/tests/integer_test.rb +40 -0
- data/tests/object_test.rb +38 -0
- data/tests/range_test.rb +41 -0
- data/tests/spec_component_test.rb +88 -0
- metadata +144 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayIndexTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_it_indexes_correctly
|
18
|
+
q1 = FlexArray.new([3, 3, 3]) {|i| i.dup}
|
19
|
+
|
20
|
+
(0...3).each do |x|
|
21
|
+
(0...3).each do |y|
|
22
|
+
(0...3).each do |z|
|
23
|
+
assert_equal([x,y,z], q1[x,y,z])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
(-3..-1).each do |x|
|
29
|
+
(-3..-1).each do |y|
|
30
|
+
(-3..-1).each do |z|
|
31
|
+
assert_equal([x+3,y+3,z+3], q1[x,y,z])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
q = q1[0, 0, :all]
|
37
|
+
a = [[0,0,0], [0,0,1], [0,0,2]]
|
38
|
+
assert_equal(a, q)
|
39
|
+
q = q1[0, 0, 0...3]
|
40
|
+
assert_equal(a, q)
|
41
|
+
q = q1[0, 0, -3..-1]
|
42
|
+
assert_equal(a, q)
|
43
|
+
q = q1[0, 0, [0,2]]
|
44
|
+
assert_equal(a, q)
|
45
|
+
q = q1[0, 0, [0,-1]]
|
46
|
+
assert_equal(a, q)
|
47
|
+
q = q1[0, 0, [-3,-1]]
|
48
|
+
assert_equal(a, q)
|
49
|
+
q = q1[0, 0, [-3,2]]
|
50
|
+
assert_equal(a, q)
|
51
|
+
|
52
|
+
q = q1[0, :all, 0]
|
53
|
+
a = [[0,0,0], [0,1,0], [0,2,0]]
|
54
|
+
assert_equal(a, q)
|
55
|
+
q = q1[0, 0...3, 0]
|
56
|
+
assert_equal(a, q)
|
57
|
+
q = q1[0, -3..-1, 0]
|
58
|
+
assert_equal(a, q)
|
59
|
+
q = q1[0, [0,2], 0]
|
60
|
+
assert_equal(a, q)
|
61
|
+
q = q1[0, [0,-1], 0]
|
62
|
+
assert_equal(a, q)
|
63
|
+
q = q1[0, [-3,-1], 0]
|
64
|
+
assert_equal(a, q)
|
65
|
+
q = q1[0, [-3,2], 0]
|
66
|
+
assert_equal(a, q)
|
67
|
+
|
68
|
+
q = q1[:all, 0, 0]
|
69
|
+
a = [[0,0,0], [1,0,0], [2,0,0]]
|
70
|
+
assert_equal(a, q)
|
71
|
+
q = q1[0...3, 0, 0]
|
72
|
+
assert_equal(a, q)
|
73
|
+
q = q1[-3..-1, 0, 0]
|
74
|
+
assert_equal(a, q)
|
75
|
+
q = q1[[0,2], 0, 0]
|
76
|
+
assert_equal(a, q)
|
77
|
+
q = q1[[0,-1], 0, 0]
|
78
|
+
assert_equal(a, q)
|
79
|
+
q = q1[[-3,-1], 0, 0]
|
80
|
+
assert_equal(a, q)
|
81
|
+
q = q1[[-3,2], 0, 0]
|
82
|
+
assert_equal(a, q)
|
83
|
+
|
84
|
+
q = q1[0, 1, :all]
|
85
|
+
a = [[0,1,0], [0,1,1], [0,1,2]]
|
86
|
+
assert_equal(a, q)
|
87
|
+
q = q1[0, 1, 0...3]
|
88
|
+
assert_equal(a, q)
|
89
|
+
q = q1[0, 1, -3..-1]
|
90
|
+
assert_equal(a, q)
|
91
|
+
q = q1[0, 1, [0,2]]
|
92
|
+
assert_equal(a, q)
|
93
|
+
q = q1[0, 1, [0,-1]]
|
94
|
+
assert_equal(a, q)
|
95
|
+
q = q1[0, 1, [-3,-1]]
|
96
|
+
assert_equal(a, q)
|
97
|
+
q = q1[0, 1, [-3,2]]
|
98
|
+
assert_equal(a, q)
|
99
|
+
|
100
|
+
q = q1[0, :all, 1]
|
101
|
+
a = [[0,0,1], [0,1,1], [0,2,1]]
|
102
|
+
assert_equal(a, q)
|
103
|
+
q = q1[0, 0...3, 1]
|
104
|
+
assert_equal(a, q)
|
105
|
+
q = q1[0, -3..-1, 1]
|
106
|
+
assert_equal(a, q)
|
107
|
+
q = q1[0, [0,2], 1]
|
108
|
+
assert_equal(a, q)
|
109
|
+
q = q1[0, [0,-1], 1]
|
110
|
+
assert_equal(a, q)
|
111
|
+
q = q1[0, [-3,-1], 1]
|
112
|
+
assert_equal(a, q)
|
113
|
+
q = q1[0, [-3,2], 1]
|
114
|
+
assert_equal(a, q)
|
115
|
+
|
116
|
+
q = q1[:all, 1, 0]
|
117
|
+
a = [[0,1,0], [1,1,0], [2,1,0]]
|
118
|
+
assert_equal(a, q)
|
119
|
+
q = q1[0...3, 1, 0]
|
120
|
+
assert_equal(a, q)
|
121
|
+
q = q1[-3..-1, 1, 0]
|
122
|
+
assert_equal(a, q)
|
123
|
+
q = q1[[0,2], 1, 0]
|
124
|
+
assert_equal(a, q)
|
125
|
+
q = q1[[0,-1], 1, 0]
|
126
|
+
assert_equal(a, q)
|
127
|
+
q = q1[[-3,-1], 1, 0]
|
128
|
+
assert_equal(a, q)
|
129
|
+
q = q1[[-3,2], 1, 0]
|
130
|
+
assert_equal(a, q)
|
131
|
+
|
132
|
+
q = q1[0, 2, :all]
|
133
|
+
a = [[0,2,0], [0,2,1], [0,2,2]]
|
134
|
+
assert_equal(a, q)
|
135
|
+
q = q1[0, 2, 0...3]
|
136
|
+
assert_equal(a, q)
|
137
|
+
q = q1[0, 2, -3..-1]
|
138
|
+
assert_equal(a, q)
|
139
|
+
q = q1[0, 2, [0,2]]
|
140
|
+
assert_equal(a, q)
|
141
|
+
q = q1[0, 2, [0,-1]]
|
142
|
+
assert_equal(a, q)
|
143
|
+
q = q1[0, 2, [-3,-1]]
|
144
|
+
assert_equal(a, q)
|
145
|
+
q = q1[0, 2, [-3,2]]
|
146
|
+
assert_equal(a, q)
|
147
|
+
|
148
|
+
q = q1[0, :all, 2]
|
149
|
+
a = [[0,0,2], [0,1,2], [0,2,2]]
|
150
|
+
assert_equal(a, q)
|
151
|
+
q = q1[0, 0...3, 2]
|
152
|
+
assert_equal(a, q)
|
153
|
+
q = q1[0, -3..-1, 2]
|
154
|
+
assert_equal(a, q)
|
155
|
+
q = q1[0, [0,2], 2]
|
156
|
+
assert_equal(a, q)
|
157
|
+
q = q1[0, [0,-1], 2]
|
158
|
+
assert_equal(a, q)
|
159
|
+
q = q1[0, [-3,-1], 2]
|
160
|
+
assert_equal(a, q)
|
161
|
+
q = q1[0, [-3,2], 2]
|
162
|
+
assert_equal(a, q)
|
163
|
+
|
164
|
+
q = q1[:all, 2, 0]
|
165
|
+
a = [[0,2,0], [1,2,0], [2,2,0]]
|
166
|
+
assert_equal(a, q)
|
167
|
+
q = q1[0...3, 2, 0]
|
168
|
+
assert_equal(a, q)
|
169
|
+
q = q1[-3..-1, 2, 0]
|
170
|
+
assert_equal(a, q)
|
171
|
+
q = q1[[0,2], 2, 0]
|
172
|
+
assert_equal(a, q)
|
173
|
+
q = q1[[0,-1], 2, 0]
|
174
|
+
assert_equal(a, q)
|
175
|
+
q = q1[[-3,-1], 2, 0]
|
176
|
+
assert_equal(a, q)
|
177
|
+
q = q1[[-3,2], 2, 0]
|
178
|
+
assert_equal(a, q)
|
179
|
+
|
180
|
+
q = q1[0, :all, :all]
|
181
|
+
a = [[0,0,0], [0,0,1], [0,0,2],
|
182
|
+
[0,1,0], [0,1,1], [0,1,2],
|
183
|
+
[0,2,0], [0,2,1], [0,2,2]]
|
184
|
+
assert_equal(a, q)
|
185
|
+
|
186
|
+
q = q1[0, 1..2, :all]
|
187
|
+
a = [[0,1,0], [0,1,1], [0,1,2], [0,2,0], [0,2,1], [0,2,2]]
|
188
|
+
assert_equal(a, q)
|
189
|
+
|
190
|
+
q = q1[0, 0..2, :all]
|
191
|
+
a = [[0,0,0], [0,0,1], [0,0,2],
|
192
|
+
[0,1,0], [0,1,1], [0,1,2],
|
193
|
+
[0,2,0], [0,2,1], [0,2,2]]
|
194
|
+
assert_equal(a, q)
|
195
|
+
|
196
|
+
q = q1[0, :all, 0..2]
|
197
|
+
a = [[0,0,0], [0,0,1], [0,0,2],
|
198
|
+
[0,1,0], [0,1,1], [0,1,2],
|
199
|
+
[0,2,0], [0,2,1], [0,2,2]]
|
200
|
+
assert_equal(a, q)
|
201
|
+
|
202
|
+
q = q1[0, 0..2, 0..2]
|
203
|
+
a = [[0,0,0], [0,0,1], [0,0,2],
|
204
|
+
[0,1,0], [0,1,1], [0,1,2],
|
205
|
+
[0,2,0], [0,2,1], [0,2,2]]
|
206
|
+
assert_equal(a, q)
|
207
|
+
|
208
|
+
assert_equal([2,2,2], q1[-1,-1,-1])
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_that_it_rejects_bad_indexes
|
212
|
+
q1 = FlexArray.new([3, 3, 3]) {|i| i.clone}
|
213
|
+
|
214
|
+
assert_raises(IndexError) { q1[0,0,3] }
|
215
|
+
assert_raises(IndexError) { q1[0,3,0] }
|
216
|
+
assert_raises(IndexError) { q1[3,0,0] }
|
217
|
+
|
218
|
+
assert_raises(IndexError) { q1[0,0,0..3] }
|
219
|
+
assert_raises(IndexError) { q1[0,0..3,0] }
|
220
|
+
assert_raises(IndexError) { q1[0..3,0,0] }
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayNewTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_the_new_method
|
18
|
+
q1 = FlexArray.new(10, 0)
|
19
|
+
a1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
20
|
+
assert_equal(10, q1.count)
|
21
|
+
assert_equal(1, q1.dimensions)
|
22
|
+
assert_equal([0...10], q1.limits)
|
23
|
+
assert_equal(a1, q1.array_data)
|
24
|
+
|
25
|
+
q2 = FlexArray.new(1..10, 0)
|
26
|
+
a2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
27
|
+
assert_equal(10, q2.count)
|
28
|
+
assert_equal(1, q2.dimensions)
|
29
|
+
assert_equal([1..10], q2.limits)
|
30
|
+
assert_equal(a2, q2.array_data)
|
31
|
+
|
32
|
+
q3 = FlexArray.new(10) {|i| i[0]}
|
33
|
+
a3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
34
|
+
assert_equal(a3, q3.array_data)
|
35
|
+
|
36
|
+
q4 = FlexArray.new(1..10) {|i| i[0]}
|
37
|
+
a4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
38
|
+
assert_equal(a4, q4.array_data)
|
39
|
+
|
40
|
+
q5 = FlexArray.new([2, 2], 0)
|
41
|
+
a5 = [0, 0, 0, 0]
|
42
|
+
assert_equal(4, q5.count)
|
43
|
+
assert_equal(2, q5.dimensions)
|
44
|
+
assert_equal([0...2, 0...2], q5.limits)
|
45
|
+
assert_equal(a5, q5.array_data)
|
46
|
+
|
47
|
+
q6 = FlexArray.new([2, 2]) {|i| i.clone}
|
48
|
+
a6 = [[0,0], [0,1], [1,0], [1,1]]
|
49
|
+
assert_equal(a6, q6.array_data)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_the_dup_method
|
53
|
+
fa1 = FlexArray.new(10) {|idx| idx.clone}
|
54
|
+
fa2 = fa1.dup
|
55
|
+
|
56
|
+
#Test that the array reference cells are independent.
|
57
|
+
fa2[0] = 'Hello'
|
58
|
+
assert_equal([0], fa1[0])
|
59
|
+
assert_equal('Hello', fa2[0])
|
60
|
+
|
61
|
+
#Test that the array contents are still dependant.
|
62
|
+
fa2[1][0] = 'Dolly'
|
63
|
+
assert_equal(['Dolly'], fa1[1])
|
64
|
+
assert_equal(['Dolly'], fa2[1])
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_the_new_from_method
|
68
|
+
#Test new_from a regular array.
|
69
|
+
ba1 = [1,2,3,4,5,6]
|
70
|
+
q1 = FlexArray.new_from([2,3], ba1)
|
71
|
+
assert_equal(ba1, q1.array_data)
|
72
|
+
assert_equal(q1.limits, [0...2, 0...3])
|
73
|
+
assert_equal(q1.dimensions, 2)
|
74
|
+
|
75
|
+
#Test new_from with a smaller target.
|
76
|
+
q2 = FlexArray.new_from(4, q1)
|
77
|
+
a2 = [1,2,3,4]
|
78
|
+
assert_equal(a2, q2.array_data)
|
79
|
+
assert_equal(q2.dimensions, 1)
|
80
|
+
|
81
|
+
#Test new_from with a larger target.
|
82
|
+
q3 = FlexArray.new_from(8, q1)
|
83
|
+
a3 = [1,2,3,4,5,6,1,2]
|
84
|
+
assert_equal(a3, q3.array_data)
|
85
|
+
assert_equal(q3.dimensions, 1)
|
86
|
+
|
87
|
+
#Test new_from with same size but different shape.
|
88
|
+
q4 = FlexArray.new_from([3,2], q1)
|
89
|
+
assert_equal(ba1, q4.array_data)
|
90
|
+
assert_equal(q4.limits, [0...3, 0...2])
|
91
|
+
assert_equal(q4.dimensions, 2)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_the_new_from_selection_method
|
95
|
+
#Create a new array from a row of data.
|
96
|
+
fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
|
97
|
+
fa2 = FlexArray.new_from_selection(4, fa1, [0, :all])
|
98
|
+
assert_equal([[0,0],[0,1],[0,2],[0,3]], fa2.array_data)
|
99
|
+
|
100
|
+
#Create a new array from a column of data.
|
101
|
+
fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
|
102
|
+
fa2 = FlexArray.new_from_selection(3, fa1, [:all, 0])
|
103
|
+
assert_equal([[0,0],[1,0],[2,0]], fa2.array_data)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_the_new_from_array_method
|
107
|
+
a = [0,1,2,3,4,5,6,7,8,9]
|
108
|
+
f = FlexArray.new_from_array(a)
|
109
|
+
assert_equal([0...10], f.limits)
|
110
|
+
assert_equal(a.object_id, f.array_data.object_id)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_that_it_rejects_bad_specs
|
114
|
+
assert_raises(ArgumentError) { FlexArray.new(10.5, 0) }
|
115
|
+
assert_raises(ArgumentError) { FlexArray.new('Hello', 0) }
|
116
|
+
assert_raises(ArgumentError) { FlexArray.new(:all, 0) }
|
117
|
+
|
118
|
+
assert_raises(ArgumentError) { FlexArray.new([10.5], 0) }
|
119
|
+
assert_raises(ArgumentError) { FlexArray.new(['Hello'], 0) }
|
120
|
+
assert_raises(ArgumentError) { FlexArray.new([:all], 0) }
|
121
|
+
|
122
|
+
assert_raises(ArgumentError) { FlexArray.new(1...1, 0) }
|
123
|
+
assert_raises(ArgumentError) { FlexArray.new(-1..-5, 0) }
|
124
|
+
|
125
|
+
assert_raises(ArgumentError) { FlexArray.new([1...1], 0) }
|
126
|
+
assert_raises(ArgumentError) { FlexArray.new([-1..-5], 0) }
|
127
|
+
|
128
|
+
assert_raises(ArgumentError) { FlexArray.new('a'..'z', 0) }
|
129
|
+
assert_raises(ArgumentError) { FlexArray.new(['a'..'z'], 0) }
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayReshapeTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_the_reshape_method
|
18
|
+
ba1 = [[0,0], [0,1], [0,2], [0,3],
|
19
|
+
[1,0], [1,1], [1,2], [1,3],
|
20
|
+
[2,0], [2,1], [2,2], [2,3]]
|
21
|
+
|
22
|
+
fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
|
23
|
+
fa2 = fa1.reshape([2,6])
|
24
|
+
assert_equal([0...3, 0...4], fa1.limits)
|
25
|
+
assert_equal([0...2, 0...6], fa2.limits)
|
26
|
+
assert_equal(ba1, fa1.array_data)
|
27
|
+
assert_equal(ba1, fa2.array_data)
|
28
|
+
|
29
|
+
ba2 = [[0,0], [0,1], [0,2], [0,3],
|
30
|
+
[1,0], [1,1], [1,2], [1,3],
|
31
|
+
[2,0], [2,1], [2,2], [2,3],
|
32
|
+
[0,0], [0,1], [0,2]]
|
33
|
+
|
34
|
+
fa3 = fa1.reshape([3,5])
|
35
|
+
assert_equal(ba2, fa3.array_data)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_the_reshape_e_m_method
|
39
|
+
ba1 = [[0,0], [0,1], [0,2], [0,3],
|
40
|
+
[1,0], [1,1], [1,2], [1,3],
|
41
|
+
[2,0], [2,1], [2,2], [2,3]]
|
42
|
+
|
43
|
+
fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
|
44
|
+
fa1.reshape!([2,6])
|
45
|
+
assert_equal([0...2, 0...6], fa1.limits)
|
46
|
+
assert_equal(ba1, fa1.array_data)
|
47
|
+
assert_equal(2, fa1.dimensions)
|
48
|
+
|
49
|
+
ba2 = [[0,0], [0,1], [0,2], [0,3],
|
50
|
+
[1,0], [1,1], [1,2], [1,3],
|
51
|
+
[2,0], [2,1], [2,2], [2,3],
|
52
|
+
[0,0], [0,1], [0,2]]
|
53
|
+
|
54
|
+
fa1.reshape!([3,5])
|
55
|
+
assert_equal(ba2, fa1.array_data)
|
56
|
+
assert_equal(2, fa1.dimensions)
|
57
|
+
|
58
|
+
fa1.reshape!(15)
|
59
|
+
assert_equal([0...15], fa1.limits)
|
60
|
+
assert_equal(1, fa1.dimensions)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_the_flatten_method
|
64
|
+
fa = FlexArray.new([2,3]) {|idx| idx.clone}
|
65
|
+
a = [0,0,0,1,0,2,1,0,1,1,1,2]
|
66
|
+
assert_equal(a, fa.flatten)
|
67
|
+
assert(fa.array_data.object_id != fa.flatten.object_id)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_the_to_a_method
|
71
|
+
fa = FlexArray.new([2,3]) {|idx| idx.clone}
|
72
|
+
a = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]
|
73
|
+
assert_equal(a, fa.to_a)
|
74
|
+
assert(fa.array_data.object_id != fa.to_a.object_id)
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_version_reporting
|
18
|
+
version = '0.2.0'
|
19
|
+
f = FlexArray.new([3,3], 'test')
|
20
|
+
|
21
|
+
assert_equal(version, FlexArray.version)
|
22
|
+
assert_equal(version, f.version)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_the_limits_method
|
26
|
+
f = FlexArray.new([3,3], 'test')
|
27
|
+
assert_equal([0...3, 0...3], f.limits)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_the_to_flex_array_method
|
31
|
+
f = FlexArray.new([3,3], 'test')
|
32
|
+
g = f.to_flex_array
|
33
|
+
|
34
|
+
assert_equal(f, g)
|
35
|
+
assert_equal(f.object_id, g.object_id)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_the_equal_method
|
39
|
+
f = FlexArray.new([3,3], 'test')
|
40
|
+
g = FlexArray.new([3,3], 'test')
|
41
|
+
h = FlexArray.new([3,3], 'not')
|
42
|
+
|
43
|
+
assert(f == g)
|
44
|
+
refute(f == h)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_the_compariositality_method
|
48
|
+
f = FlexArray.new([3,3], 1)
|
49
|
+
g = FlexArray.new([3,3], 1)
|
50
|
+
h = FlexArray.new([3,3], 2)
|
51
|
+
i = FlexArray.new([3,3], 0)
|
52
|
+
|
53
|
+
assert_equal( 0, f <=> g)
|
54
|
+
assert_equal(-1, f <=> h)
|
55
|
+
assert_equal( 1, f <=> i)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayTransposeTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_the_transpose_method
|
18
|
+
f = FlexArray.new([3,3]) { |i| i[0]*3 + i[1] }
|
19
|
+
a = [0,1,2,
|
20
|
+
3,4,5,
|
21
|
+
6,7,8]
|
22
|
+
assert_equal(a, f[:all, :all])
|
23
|
+
assert_equal(a, f[:all])
|
24
|
+
|
25
|
+
f.transpose(0,1)
|
26
|
+
a = [0,3,6,
|
27
|
+
1,4,7,
|
28
|
+
2,5,8]
|
29
|
+
assert_equal(a, f[:all, :all])
|
30
|
+
assert_equal(a, f[:all])
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../lib/flex_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class FlexArrayValidateTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_the_compatible_method
|
18
|
+
f = FlexArray.new([3,3], 'f')
|
19
|
+
g = FlexArray.new([0...3,0...3], 'g')
|
20
|
+
h = FlexArray.new([1..3,1..3], 'h')
|
21
|
+
i = FlexArray.new([3,3,3], 'i')
|
22
|
+
j = FlexArray.new([3], 'j')
|
23
|
+
|
24
|
+
assert(f.compatible?(g))
|
25
|
+
refute(f.compatible?(h))
|
26
|
+
refute(f.compatible?(i))
|
27
|
+
refute(f.compatible?(j))
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative '../lib/flex_array/integer'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class IntegerTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_to_spec_component_works
|
18
|
+
lc = (10).to_spec_component(3)
|
19
|
+
assert_equal(0...10, lc.range)
|
20
|
+
assert_equal(3, lc.stride)
|
21
|
+
|
22
|
+
lc = (0).to_spec_component(3)
|
23
|
+
assert_equal(0...0, lc.range)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_that_to_spec_component_rejects_bad
|
27
|
+
assert_raises(ArgumentError) { (-100).to_spec_component(3) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_the_to_index_range_method
|
31
|
+
lc = SpecComponent.new(0...10, 1)
|
32
|
+
assert_equal(0..0, (0).to_index_range(lc))
|
33
|
+
assert_equal(1..1, (1).to_index_range(lc))
|
34
|
+
assert_equal(9..9, (-1).to_index_range(lc))
|
35
|
+
|
36
|
+
assert_raises(IndexError) { (10).to_index_range(lc) }
|
37
|
+
assert_raises(IndexError) { (-12).to_index_range(lc) }
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative '../lib/flex_array/object'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class ObjectTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_to_spec_component_fails
|
18
|
+
obj = Object.new
|
19
|
+
assert_raises(ArgumentError) { obj.to_spec_component(42) }
|
20
|
+
|
21
|
+
#Test other invalid objects as well...
|
22
|
+
assert_raises(ArgumentError) { (89.0).to_spec_component(42) }
|
23
|
+
assert_raises(ArgumentError) { 'Hello'.to_spec_component(42) }
|
24
|
+
assert_raises(ArgumentError) { :Hello.to_spec_component(42) }
|
25
|
+
assert_raises(ArgumentError) { [1,2,3].to_spec_component(42) }
|
26
|
+
assert_raises(ArgumentError) { {:hi => 42}.to_spec_component(42) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_the_to_index_range_method
|
30
|
+
lc = SpecComponent.new(0...10, 1)
|
31
|
+
assert_equal(0...10, (:all).to_index_range(lc))
|
32
|
+
assert_raises(IndexError) { (:y_all).to_index_range(lc) }
|
33
|
+
assert_raises(IndexError) { (:all_y_all).to_index_range(lc) }
|
34
|
+
assert_raises(IndexError) { ('all').to_index_range(lc) }
|
35
|
+
assert_raises(IndexError) { (98.9).to_index_range(lc) }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/tests/range_test.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative '../lib/flex_array/range'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class RangeTester < MiniTest::Unit::TestCase
|
5
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
6
|
+
|
7
|
+
def initialize(*all)
|
8
|
+
if $do_this_only_one_time != __FILE__
|
9
|
+
puts
|
10
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
11
|
+
$do_this_only_one_time = __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
super(*all)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_to_spec_component_works
|
18
|
+
lc = (0...10).to_spec_component(3)
|
19
|
+
assert_equal(0...10, lc.range)
|
20
|
+
assert_equal(3, lc.stride)
|
21
|
+
|
22
|
+
lc = (0...0).to_spec_component(3)
|
23
|
+
assert_equal(0...0, lc.range)
|
24
|
+
assert_equal(3, lc.stride)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_that_to_spec_component_rejects_bad
|
28
|
+
assert_raises(ArgumentError) { (8...0).to_spec_component(3) }
|
29
|
+
assert_raises(ArgumentError) { ('a'..'b').to_spec_component(3) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_the_to_index_range_method
|
33
|
+
lc = SpecComponent.new(0...10, 1)
|
34
|
+
assert_equal(1...3, (1...3).to_index_range(lc))
|
35
|
+
assert_equal(8..9, (-2..-1).to_index_range(lc))
|
36
|
+
|
37
|
+
assert_raises(IndexError) { (1...11).to_index_range(lc) }
|
38
|
+
assert_raises(IndexError) { (1..10).to_index_range(lc) }
|
39
|
+
assert_raises(IndexError) { (-2..2).to_index_range(lc) }
|
40
|
+
end
|
41
|
+
end
|