flex_array 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,20 +3,20 @@ require 'minitest/autorun'
3
3
 
4
4
  class FlexArrayIndexTester < MiniTest::Unit::TestCase
5
5
  $do_this_only_one_time = "" unless defined? $do_this_only_one_time
6
-
6
+
7
7
  def initialize(*all)
8
8
  if $do_this_only_one_time != __FILE__
9
9
  puts
10
- puts "Running test file: #{File.split(__FILE__)[1]}"
10
+ puts "Running test file: #{File.split(__FILE__)[1]}"
11
11
  $do_this_only_one_time = __FILE__
12
12
  end
13
-
13
+
14
14
  super(*all)
15
15
  end
16
16
 
17
17
  def test_that_it_indexes_correctly
18
18
  q1 = FlexArray.new([3, 3, 3]) {|i| i.dup}
19
-
19
+
20
20
  (0...3).each do |x|
21
21
  (0...3).each do |y|
22
22
  (0...3).each do |z|
@@ -32,7 +32,7 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
32
32
  end
33
33
  end
34
34
  end
35
-
35
+
36
36
  q = q1[0, 0, :all]
37
37
  a = [[0,0,0], [0,0,1], [0,0,2]]
38
38
  assert_equal(a, q)
@@ -40,13 +40,11 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
40
40
  assert_equal(a, q)
41
41
  q = q1[0, 0, -3..-1]
42
42
  assert_equal(a, q)
43
- q = q1[0, 0, [0,2]]
44
- assert_equal(a, q)
45
- q = q1[0, 0, [0,-1]]
43
+ q = q1[0, 0, [0,1,2]]
46
44
  assert_equal(a, q)
47
- q = q1[0, 0, [-3,-1]]
45
+ q = q1[0, 0, [0,1,-1]]
48
46
  assert_equal(a, q)
49
- q = q1[0, 0, [-3,2]]
47
+ q = q1[0, 0, [-3,-2,-1]]
50
48
  assert_equal(a, q)
51
49
 
52
50
  q = q1[0, :all, 0]
@@ -56,15 +54,15 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
56
54
  assert_equal(a, q)
57
55
  q = q1[0, -3..-1, 0]
58
56
  assert_equal(a, q)
59
- q = q1[0, [0,2], 0]
57
+ q = q1[0, [0,1,2], 0]
60
58
  assert_equal(a, q)
61
- q = q1[0, [0,-1], 0]
59
+ q = q1[0, [0,1,-1], 0]
62
60
  assert_equal(a, q)
63
- q = q1[0, [-3,-1], 0]
61
+ q = q1[0, [-3,-2,-1], 0]
64
62
  assert_equal(a, q)
65
- q = q1[0, [-3,2], 0]
63
+ q = q1[0, [-3,1,2], 0]
66
64
  assert_equal(a, q)
67
-
65
+
68
66
  q = q1[:all, 0, 0]
69
67
  a = [[0,0,0], [1,0,0], [2,0,0]]
70
68
  assert_equal(a, q)
@@ -72,15 +70,15 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
72
70
  assert_equal(a, q)
73
71
  q = q1[-3..-1, 0, 0]
74
72
  assert_equal(a, q)
75
- q = q1[[0,2], 0, 0]
73
+ q = q1[[0,1,2], 0, 0]
76
74
  assert_equal(a, q)
77
- q = q1[[0,-1], 0, 0]
75
+ q = q1[[0,1,-1], 0, 0]
78
76
  assert_equal(a, q)
79
- q = q1[[-3,-1], 0, 0]
77
+ q = q1[[-3,-2,-1], 0, 0]
80
78
  assert_equal(a, q)
81
- q = q1[[-3,2], 0, 0]
79
+ q = q1[[-3,1,2], 0, 0]
82
80
  assert_equal(a, q)
83
-
81
+
84
82
  q = q1[0, 1, :all]
85
83
  a = [[0,1,0], [0,1,1], [0,1,2]]
86
84
  assert_equal(a, q)
@@ -88,15 +86,15 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
88
86
  assert_equal(a, q)
89
87
  q = q1[0, 1, -3..-1]
90
88
  assert_equal(a, q)
91
- q = q1[0, 1, [0,2]]
89
+ q = q1[0, 1, [0,1,2]]
92
90
  assert_equal(a, q)
93
- q = q1[0, 1, [0,-1]]
91
+ q = q1[0, 1, [0,1,-1]]
94
92
  assert_equal(a, q)
95
- q = q1[0, 1, [-3,-1]]
93
+ q = q1[0, 1, [-3,-2,-1]]
96
94
  assert_equal(a, q)
97
- q = q1[0, 1, [-3,2]]
95
+ q = q1[0, 1, [-3,1,2]]
98
96
  assert_equal(a, q)
99
-
97
+
100
98
  q = q1[0, :all, 1]
101
99
  a = [[0,0,1], [0,1,1], [0,2,1]]
102
100
  assert_equal(a, q)
@@ -104,13 +102,13 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
104
102
  assert_equal(a, q)
105
103
  q = q1[0, -3..-1, 1]
106
104
  assert_equal(a, q)
107
- q = q1[0, [0,2], 1]
105
+ q = q1[0, [0,1,2], 1]
108
106
  assert_equal(a, q)
109
- q = q1[0, [0,-1], 1]
107
+ q = q1[0, [0,1,-1], 1]
110
108
  assert_equal(a, q)
111
- q = q1[0, [-3,-1], 1]
109
+ q = q1[0, [-3,-2,-1], 1]
112
110
  assert_equal(a, q)
113
- q = q1[0, [-3,2], 1]
111
+ q = q1[0, [-3,1,2], 1]
114
112
  assert_equal(a, q)
115
113
 
116
114
  q = q1[:all, 1, 0]
@@ -120,15 +118,15 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
120
118
  assert_equal(a, q)
121
119
  q = q1[-3..-1, 1, 0]
122
120
  assert_equal(a, q)
123
- q = q1[[0,2], 1, 0]
121
+ q = q1[[0,1,2], 1, 0]
124
122
  assert_equal(a, q)
125
- q = q1[[0,-1], 1, 0]
123
+ q = q1[[0,1,-1], 1, 0]
126
124
  assert_equal(a, q)
127
- q = q1[[-3,-1], 1, 0]
125
+ q = q1[[-3,-2,-1], 1, 0]
128
126
  assert_equal(a, q)
129
- q = q1[[-3,2], 1, 0]
127
+ q = q1[[-3,1,2], 1, 0]
130
128
  assert_equal(a, q)
131
-
129
+
132
130
  q = q1[0, 2, :all]
133
131
  a = [[0,2,0], [0,2,1], [0,2,2]]
134
132
  assert_equal(a, q)
@@ -136,15 +134,15 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
136
134
  assert_equal(a, q)
137
135
  q = q1[0, 2, -3..-1]
138
136
  assert_equal(a, q)
139
- q = q1[0, 2, [0,2]]
137
+ q = q1[0, 2, [0,1,2]]
140
138
  assert_equal(a, q)
141
- q = q1[0, 2, [0,-1]]
139
+ q = q1[0, 2, [0,1,-1]]
142
140
  assert_equal(a, q)
143
- q = q1[0, 2, [-3,-1]]
141
+ q = q1[0, 2, [-3,-2,-1]]
144
142
  assert_equal(a, q)
145
- q = q1[0, 2, [-3,2]]
143
+ q = q1[0, 2, [-3,1,2]]
146
144
  assert_equal(a, q)
147
-
145
+
148
146
  q = q1[0, :all, 2]
149
147
  a = [[0,0,2], [0,1,2], [0,2,2]]
150
148
  assert_equal(a, q)
@@ -152,13 +150,13 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
152
150
  assert_equal(a, q)
153
151
  q = q1[0, -3..-1, 2]
154
152
  assert_equal(a, q)
155
- q = q1[0, [0,2], 2]
153
+ q = q1[0, [0,1,2], 2]
156
154
  assert_equal(a, q)
157
- q = q1[0, [0,-1], 2]
155
+ q = q1[0, [0,1,-1], 2]
158
156
  assert_equal(a, q)
159
- q = q1[0, [-3,-1], 2]
157
+ q = q1[0, [-3,-2,-1], 2]
160
158
  assert_equal(a, q)
161
- q = q1[0, [-3,2], 2]
159
+ q = q1[0, [-3,1,2], 2]
162
160
  assert_equal(a, q)
163
161
 
164
162
  q = q1[:all, 2, 0]
@@ -168,31 +166,31 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
168
166
  assert_equal(a, q)
169
167
  q = q1[-3..-1, 2, 0]
170
168
  assert_equal(a, q)
171
- q = q1[[0,2], 2, 0]
169
+ q = q1[[0,1,2], 2, 0]
172
170
  assert_equal(a, q)
173
- q = q1[[0,-1], 2, 0]
171
+ q = q1[[0,1,-1], 2, 0]
174
172
  assert_equal(a, q)
175
- q = q1[[-3,-1], 2, 0]
173
+ q = q1[[-3,-2,-1], 2, 0]
176
174
  assert_equal(a, q)
177
- q = q1[[-3,2], 2, 0]
175
+ q = q1[[-3,1,2], 2, 0]
178
176
  assert_equal(a, q)
179
-
177
+
180
178
  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],
179
+ a = [[0,0,0], [0,0,1], [0,0,2],
180
+ [0,1,0], [0,1,1], [0,1,2],
183
181
  [0,2,0], [0,2,1], [0,2,2]]
184
182
  assert_equal(a, q)
185
-
183
+
186
184
  q = q1[0, 1..2, :all]
187
185
  a = [[0,1,0], [0,1,1], [0,1,2], [0,2,0], [0,2,1], [0,2,2]]
188
186
  assert_equal(a, q)
189
-
187
+
190
188
  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],
189
+ a = [[0,0,0], [0,0,1], [0,0,2],
190
+ [0,1,0], [0,1,1], [0,1,2],
193
191
  [0,2,0], [0,2,1], [0,2,2]]
194
192
  assert_equal(a, q)
195
-
193
+
196
194
  q = q1[0, :all, 0..2]
197
195
  a = [[0,0,0], [0,0,1], [0,0,2],
198
196
  [0,1,0], [0,1,1], [0,1,2],
@@ -204,8 +202,8 @@ class FlexArrayIndexTester < MiniTest::Unit::TestCase
204
202
  [0,1,0], [0,1,1], [0,1,2],
205
203
  [0,2,0], [0,2,1], [0,2,2]]
206
204
  assert_equal(a, q)
207
-
208
- assert_equal([2,2,2], q1[-1,-1,-1])
205
+
206
+ assert_equal([2,2,2], q1[-1,-1,-1])
209
207
  end
210
208
 
211
209
  def test_that_it_rejects_bad_indexes
@@ -3,14 +3,14 @@ require 'minitest/autorun'
3
3
 
4
4
  class FlexArrayNewTester < MiniTest::Unit::TestCase
5
5
  $do_this_only_one_time = "" unless defined? $do_this_only_one_time
6
-
6
+
7
7
  def initialize(*all)
8
8
  if $do_this_only_one_time != __FILE__
9
9
  puts
10
- puts "Running test file: #{File.split(__FILE__)[1]}"
10
+ puts "Running test file: #{File.split(__FILE__)[1]}"
11
11
  $do_this_only_one_time = __FILE__
12
12
  end
13
-
13
+
14
14
  super(*all)
15
15
  end
16
16
 
@@ -21,18 +21,18 @@ class FlexArrayNewTester < MiniTest::Unit::TestCase
21
21
  assert_equal(1, q1.dimensions)
22
22
  assert_equal([0...10], q1.limits)
23
23
  assert_equal(a1, q1.array_data)
24
-
24
+
25
25
  q2 = FlexArray.new(1..10, 0)
26
26
  a2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
27
27
  assert_equal(10, q2.count)
28
28
  assert_equal(1, q2.dimensions)
29
29
  assert_equal([1..10], q2.limits)
30
30
  assert_equal(a2, q2.array_data)
31
-
31
+
32
32
  q3 = FlexArray.new(10) {|i| i[0]}
33
33
  a3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
34
- assert_equal(a3, q3.array_data)
35
-
34
+ assert_equal(a3, q3.array_data)
35
+
36
36
  q4 = FlexArray.new(1..10) {|i| i[0]}
37
37
  a4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
38
38
  assert_equal(a4, q4.array_data)
@@ -43,27 +43,27 @@ class FlexArrayNewTester < MiniTest::Unit::TestCase
43
43
  assert_equal(2, q5.dimensions)
44
44
  assert_equal([0...2, 0...2], q5.limits)
45
45
  assert_equal(a5, q5.array_data)
46
-
46
+
47
47
  q6 = FlexArray.new([2, 2]) {|i| i.clone}
48
48
  a6 = [[0,0], [0,1], [1,0], [1,1]]
49
49
  assert_equal(a6, q6.array_data)
50
50
  end
51
-
51
+
52
52
  def test_the_dup_method
53
53
  fa1 = FlexArray.new(10) {|idx| idx.clone}
54
54
  fa2 = fa1.dup
55
-
55
+
56
56
  #Test that the array reference cells are independent.
57
57
  fa2[0] = 'Hello'
58
58
  assert_equal([0], fa1[0])
59
59
  assert_equal('Hello', fa2[0])
60
60
 
61
- #Test that the array contents are still dependant.
61
+ #Test that the array contents are still dependant.
62
62
  fa2[1][0] = 'Dolly'
63
63
  assert_equal(['Dolly'], fa1[1])
64
64
  assert_equal(['Dolly'], fa2[1])
65
65
  end
66
-
66
+
67
67
  def test_the_new_from_method
68
68
  #Test new_from a regular array.
69
69
  ba1 = [1,2,3,4,5,6]
@@ -83,26 +83,26 @@ class FlexArrayNewTester < MiniTest::Unit::TestCase
83
83
  a3 = [1,2,3,4,5,6,1,2]
84
84
  assert_equal(a3, q3.array_data)
85
85
  assert_equal(q3.dimensions, 1)
86
-
86
+
87
87
  #Test new_from with same size but different shape.
88
88
  q4 = FlexArray.new_from([3,2], q1)
89
89
  assert_equal(ba1, q4.array_data)
90
90
  assert_equal(q4.limits, [0...3, 0...2])
91
91
  assert_equal(q4.dimensions, 2)
92
92
  end
93
-
93
+
94
94
  def test_the_new_from_selection_method
95
95
  #Create a new array from a row of data.
96
96
  fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
97
97
  fa2 = FlexArray.new_from_selection(4, fa1, [0, :all])
98
98
  assert_equal([[0,0],[0,1],[0,2],[0,3]], fa2.array_data)
99
-
99
+
100
100
  #Create a new array from a column of data.
101
101
  fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
102
102
  fa2 = FlexArray.new_from_selection(3, fa1, [:all, 0])
103
103
  assert_equal([[0,0],[1,0],[2,0]], fa2.array_data)
104
104
  end
105
-
105
+
106
106
  def test_the_new_from_array_method
107
107
  a = [0,1,2,3,4,5,6,7,8,9]
108
108
  f = FlexArray.new_from_array(a)
@@ -114,18 +114,18 @@ class FlexArrayNewTester < MiniTest::Unit::TestCase
114
114
  assert_raises(ArgumentError) { FlexArray.new(10.5, 0) }
115
115
  assert_raises(ArgumentError) { FlexArray.new('Hello', 0) }
116
116
  assert_raises(ArgumentError) { FlexArray.new(:all, 0) }
117
-
117
+
118
118
  assert_raises(ArgumentError) { FlexArray.new([10.5], 0) }
119
119
  assert_raises(ArgumentError) { FlexArray.new(['Hello'], 0) }
120
120
  assert_raises(ArgumentError) { FlexArray.new([:all], 0) }
121
-
121
+
122
122
  assert_raises(ArgumentError) { FlexArray.new(1...1, 0) }
123
123
  assert_raises(ArgumentError) { FlexArray.new(-1..-5, 0) }
124
-
124
+
125
125
  assert_raises(ArgumentError) { FlexArray.new([1...1], 0) }
126
126
  assert_raises(ArgumentError) { FlexArray.new([-1..-5], 0) }
127
-
128
- assert_raises(ArgumentError) { FlexArray.new('a'..'z', 0) }
127
+
128
+ assert_raises(ArgumentError) { FlexArray.new('a'..'z', 0) }
129
129
  assert_raises(ArgumentError) { FlexArray.new(['a'..'z'], 0) }
130
130
  end
131
131
  end
@@ -3,14 +3,14 @@ require 'minitest/autorun'
3
3
 
4
4
  class FlexArrayReshapeTester < MiniTest::Unit::TestCase
5
5
  $do_this_only_one_time = "" unless defined? $do_this_only_one_time
6
-
6
+
7
7
  def initialize(*all)
8
8
  if $do_this_only_one_time != __FILE__
9
9
  puts
10
- puts "Running test file: #{File.split(__FILE__)[1]}"
10
+ puts "Running test file: #{File.split(__FILE__)[1]}"
11
11
  $do_this_only_one_time = __FILE__
12
12
  end
13
-
13
+
14
14
  super(*all)
15
15
  end
16
16
 
@@ -18,7 +18,7 @@ class FlexArrayReshapeTester < MiniTest::Unit::TestCase
18
18
  ba1 = [[0,0], [0,1], [0,2], [0,3],
19
19
  [1,0], [1,1], [1,2], [1,3],
20
20
  [2,0], [2,1], [2,2], [2,3]]
21
-
21
+
22
22
  fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
23
23
  fa2 = fa1.reshape([2,6])
24
24
  assert_equal([0...3, 0...4], fa1.limits)
@@ -30,16 +30,16 @@ class FlexArrayReshapeTester < MiniTest::Unit::TestCase
30
30
  [1,0], [1,1], [1,2], [1,3],
31
31
  [2,0], [2,1], [2,2], [2,3],
32
32
  [0,0], [0,1], [0,2]]
33
-
33
+
34
34
  fa3 = fa1.reshape([3,5])
35
35
  assert_equal(ba2, fa3.array_data)
36
36
  end
37
-
37
+
38
38
  def test_the_reshape_e_m_method
39
39
  ba1 = [[0,0], [0,1], [0,2], [0,3],
40
40
  [1,0], [1,1], [1,2], [1,3],
41
41
  [2,0], [2,1], [2,2], [2,3]]
42
-
42
+
43
43
  fa1 = FlexArray.new([3,4]) {|idx| idx.clone}
44
44
  fa1.reshape!([2,6])
45
45
  assert_equal([0...2, 0...6], fa1.limits)
@@ -50,23 +50,16 @@ class FlexArrayReshapeTester < MiniTest::Unit::TestCase
50
50
  [1,0], [1,1], [1,2], [1,3],
51
51
  [2,0], [2,1], [2,2], [2,3],
52
52
  [0,0], [0,1], [0,2]]
53
-
53
+
54
54
  fa1.reshape!([3,5])
55
55
  assert_equal(ba2, fa1.array_data)
56
56
  assert_equal(2, fa1.dimensions)
57
-
57
+
58
58
  fa1.reshape!(15)
59
59
  assert_equal([0...15], fa1.limits)
60
60
  assert_equal(1, fa1.dimensions)
61
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
-
62
+
70
63
  def test_the_to_a_method
71
64
  fa = FlexArray.new([2,3]) {|idx| idx.clone}
72
65
  a = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]
@@ -3,21 +3,21 @@ require 'minitest/autorun'
3
3
 
4
4
  class FlexArrayTester < MiniTest::Unit::TestCase
5
5
  $do_this_only_one_time = "" unless defined? $do_this_only_one_time
6
-
6
+
7
7
  def initialize(*all)
8
8
  if $do_this_only_one_time != __FILE__
9
9
  puts
10
- puts "Running test file: #{File.split(__FILE__)[1]}"
10
+ puts "Running test file: #{File.split(__FILE__)[1]}"
11
11
  $do_this_only_one_time = __FILE__
12
12
  end
13
-
13
+
14
14
  super(*all)
15
15
  end
16
-
16
+
17
17
  def test_version_reporting
18
- version = '0.2.0'
18
+ version = '0.3.0'
19
19
  f = FlexArray.new([3,3], 'test')
20
-
20
+
21
21
  assert_equal(version, FlexArray.version)
22
22
  assert_equal(version, f.version)
23
23
  end
@@ -26,20 +26,20 @@ class FlexArrayTester < MiniTest::Unit::TestCase
26
26
  f = FlexArray.new([3,3], 'test')
27
27
  assert_equal([0...3, 0...3], f.limits)
28
28
  end
29
-
29
+
30
30
  def test_the_to_flex_array_method
31
31
  f = FlexArray.new([3,3], 'test')
32
32
  g = f.to_flex_array
33
-
33
+
34
34
  assert_equal(f, g)
35
35
  assert_equal(f.object_id, g.object_id)
36
36
  end
37
-
37
+
38
38
  def test_the_equal_method
39
39
  f = FlexArray.new([3,3], 'test')
40
40
  g = FlexArray.new([3,3], 'test')
41
41
  h = FlexArray.new([3,3], 'not')
42
-
42
+
43
43
  assert(f == g)
44
44
  refute(f == h)
45
45
  end
@@ -49,10 +49,18 @@ class FlexArrayTester < MiniTest::Unit::TestCase
49
49
  g = FlexArray.new([3,3], 1)
50
50
  h = FlexArray.new([3,3], 2)
51
51
  i = FlexArray.new([3,3], 0)
52
-
52
+
53
53
  assert_equal( 0, f <=> g)
54
54
  assert_equal(-1, f <=> h)
55
55
  assert_equal( 1, f <=> i)
56
56
  end
57
-
57
+
58
+ def test_the_empty_method
59
+ f = FlexArray.new([3,3], 1)
60
+ refute(f.empty?)
61
+
62
+ g = FlexArray.new([0,3], 1)
63
+ assert(g.empty?)
64
+ end
65
+
58
66
  end