multiarray 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,19 @@
1
+ # multiarray - Lazy multi-dimensional arrays for Ruby
2
+ # Copyright (C) 2010 Jan Wedekind
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
1
17
  require 'test/unit'
2
18
  begin
3
19
  require 'rubygems'
@@ -8,105 +24,230 @@ Kernel::require 'multiarray'
8
24
  class TC_MultiArray < Test::Unit::TestCase
9
25
 
10
26
  O = Hornetseye::OBJECT
27
+ B = Hornetseye::BOOL
28
+ I = Hornetseye::INT
29
+ S = Hornetseye::Sequence
11
30
  M = Hornetseye::MultiArray
12
31
 
13
- def lazy( &action )
14
- Hornetseye::lazy &action
32
+ def S( *args )
33
+ Hornetseye::Sequence *args
15
34
  end
16
35
 
17
- def eager( &action )
18
- Hornetseye::eager &action
36
+ def M( *args )
37
+ Hornetseye::MultiArray *args
19
38
  end
20
39
 
21
- def setup
40
+ def sum( *args, &action )
41
+ Hornetseye::sum *args, &action
22
42
  end
23
43
 
24
- def teardown
44
+ def eager( *args, &action )
45
+ Hornetseye::eager *args, &action
25
46
  end
26
47
 
27
- def M( *args )
28
- Hornetseye::MultiArray *args
48
+ def setup
29
49
  end
30
50
 
31
- def test_default
32
- assert_equal [ [ nil ] * 3 ] * 2, M( O, 3, 2 ).new.to_a
51
+ def teardown
33
52
  end
34
53
 
35
54
  def test_multiarray_inspect
36
- assert_equal 'MultiArray.object(3,2)', M( O, 3, 2 ).inspect
55
+ assert_equal 'MultiArray(OBJECT,3,2)', M( O, 3, 2 ).inspect
37
56
  end
38
57
 
39
58
  def test_multiarray_to_s
40
- assert_equal 'MultiArray.object(3,2)', M( O, 3, 2 ).to_s
59
+ assert_equal 'MultiArray(OBJECT,3,2)', M( O, 3, 2 ).to_s
60
+ end
61
+
62
+ def test_multiarray_default
63
+ assert_equal [ [ O.default ] * 3 ] * 2, M( O, 3, 2 ).default.to_a
64
+ end
65
+
66
+ def test_multiarray_at
67
+ assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
68
+ M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_a
69
+ assert_equal O, M[ [ :a ] ].typecode
70
+ assert_equal B, M[ [ false ], [ true ] ].typecode
71
+ assert_equal I, M[ [ -2 ** 31, 2 ** 31 - 1 ] ].typecode
41
72
  end
42
73
 
43
- def test_multiarray_assign
44
- assert_equal [ [ :a, :b, :c ], [ :d, :e, :f ] ],
45
- M[ [ :a, :b, :c ], [ :d, :e, :f ] ].to_a
46
- assert_equal [ [ :a, nil ], [ :b, :c ] ],
47
- M[ [ :a ], [ :b, :c ] ].to_a
48
- assert_equal [ [ :a, :b ], [ :c, :c ] ],
49
- M[ [ :a, :b ], :c ].to_a
74
+ def test_multiarray_typecode
75
+ assert_equal O, M( O, 3, 2 ).typecode
76
+ end
77
+
78
+ def test_multiarray_dimension
79
+ assert_equal 2, M( O, 3, 2 ).dimension
80
+ end
81
+
82
+ def test_multiarray_shape
83
+ assert_equal [ 3, 2 ], M( O, 3, 2 ).shape
50
84
  end
51
85
 
52
86
  def test_inspect
53
- assert_equal "MultiArray.object(3,2):\n" +
54
- "[ [ :a, :b, :c ],\n [ :d, :e, :f ] ]",
55
- M[ [ :a, :b, :c ], [ :d, :e, :f ] ].inspect
87
+ assert_equal "MultiArray(OBJECT,3,2):\n[ [ :a, 2, 3 ],\n [ 4, 5, 6 ] ]",
88
+ M[ [ :a, 2, 3 ], [ 4, 5, 6 ] ].inspect
56
89
  end
57
90
 
58
- def test_to_s
59
- # !!!
91
+ def test_typecode
92
+ assert_equal O, M( O, 3, 2 ).new.typecode
93
+ end
94
+
95
+ def test_dimension
96
+ assert_equal 2, M( O, 3, 2 ).new.dimension
97
+ end
98
+
99
+ def test_shape
100
+ assert_equal [ 3, 2 ], M( O, 3, 2 ).new.shape
60
101
  end
61
102
 
62
103
  def test_at_assign
63
104
  m = M.new O, 3, 2
64
105
  for j in 0 ... 2
65
106
  for i in 0 ... 3
66
- assert_equal i + j * 3 + 1, m[ j ][ i ] = i + j * 3 + 1
107
+ assert_equal j * 3 + i + 1, m[ j ][ i ] = j * 3 + i + 1
108
+ assert_equal j * 3 + i + 1, m[ i, j ] = j * 3 + i + 1
67
109
  end
68
110
  end
69
111
  for j in 0 ... 2
70
112
  for i in 0 ... 3
71
- assert_equal i + j * 3 + 1, m[ j ][ i ]
113
+ assert_equal j * 3 + i + 1, m[ j ][ i ]
114
+ assert_equal j * 3 + i + 1, m[ i, j ]
72
115
  end
73
116
  end
74
- assert_equal [ 4, 5, 6 ], m[ 1 ].to_a
75
- assert_equal 7, m[ 1 ] = 7
76
- assert_equal [ [ 1, 2, 3 ], [ 7, 7, 7 ] ], m.to_a
77
117
  end
78
118
 
79
119
  def test_equal
120
+ assert_equal M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ], M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
121
+ assert_not_equal M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
122
+ M[ [ 1, 2, 3 ], [ 4, 6, 5 ] ]
80
123
  # !!!
124
+ assert_not_equal M[ [ 1, 1 ], [ 1, 1 ] ], 1
125
+ assert_not_equal M[ [ 1, 1 ], [ 1, 1 ] ], S[ 1, 1 ]
126
+ end
127
+
128
+ def test_inject
129
+ assert_equal 21, M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].inject { |a,b| a + b }
130
+ assert_equal 28, M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].inject( 7 ) { |a,b| a + b }
131
+ end
132
+
133
+ def test_sum
134
+ m = M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
135
+ assert_equal 21, sum { |i,j| m[ i, j ] }
136
+ assert_equal [ 5, 7, 9 ], sum { |i| m[ i ] }.to_a
137
+ assert_equal [ 6, 15 ], eager { |j| sum { |i| m[ i, j ] } }.to_a
138
+ assert_equal [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ], sum { || m }.to_a
139
+ end
140
+
141
+ def dont_test_convolve
142
+ f = M[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
143
+ assert_equal M[ [ 5, 6, 0 ], [ 8, 9, 0 ], [ 0, 0, 0 ] ],
144
+ M[ [ 1, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ].convolve( f )
145
+ assert_equal M[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
146
+ M[ [ 0, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 0 ] ].convolve( f )
147
+ assert_equal M[ [ 0, 0, 0 ], [ 1, 2, 3 ], [ 4, 5, 6 ] ],
148
+ M[ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 1, 0 ] ].convolve( f )
149
+ end
150
+
151
+ def test_zero
152
+ assert_equal M[ [ false, true ], [ true, false ] ],
153
+ M[ [ -1, 0 ], [ 0, 1 ] ].zero?
154
+ end
155
+
156
+ def test_nonzero
157
+ assert_equal M[ [ true, false ], [ false, true ] ],
158
+ M[ [ -1, 0 ], [ 0, 1 ] ].nonzero?
159
+ end
160
+
161
+ def test_not
162
+ assert_equal [ [ true, false ], [ false, true ] ],
163
+ M[ [ false, true ], [ true, false ] ].not.to_a
164
+ assert_equal [ [ true, false ], [ false, true ] ],
165
+ M[ [ 0, 1 ], [ 2, 0 ] ].not.to_a
166
+ end
167
+
168
+ def test_and
169
+ assert_equal [ [ false, false ] ], M[ [ false, true ] ].and( false ).to_a
170
+ assert_equal [ [ false, false ] ], false.and( M[ [ false, true ] ] ).to_a
171
+ assert_equal [ [ false, true ] ], M[ [ false, true ] ].and( true ).to_a
172
+ assert_equal [ [ false, true ] ], true.and( M[ [ false, true ] ] ).to_a
173
+ assert_equal [ [ false, false ], [ false, true ] ],
174
+ M[ [ false, true ], [ false, true ] ].
175
+ and( M[ [ false, false ], [ true, true ] ] ).to_a
176
+ assert_equal [ [ false, false ], [ true, false ] ],
177
+ M[ [ false, true ], [ true, false ] ].
178
+ and( S[ false, true ] ).to_a
179
+ end
180
+
181
+ def test_or
182
+ assert_equal [ [ false, true ] ], M[ [ false, true ] ].or( false ).to_a
183
+ assert_equal [ [ false, true ] ], false.or( M[ [ false, true ] ] ).to_a
184
+ assert_equal [ [ true, true ] ], M[ [ false, true ] ].or( true ).to_a
185
+ assert_equal [ [ true, true ] ], true.or( M[ [ false, true ] ] ).to_a
186
+ assert_equal [ [ false, true ], [ true, true ] ],
187
+ M[ [ false, true ], [ false, true ] ].
188
+ or( M[ [ false, false ], [ true, true ] ] ).to_a
189
+ assert_equal [ [ false, true ], [ true, true ] ],
190
+ M[ [ false, true ], [ true, false ] ].
191
+ or( S[ false, true ] ).to_a
192
+ end
193
+
194
+ def test_bitwise_not
195
+ assert_equal [ [ 255, 254 ], [ 253, 252 ] ],
196
+ ( ~M[ [ 0, 1 ], [ 2, 3 ] ] ).to_a
197
+ assert_equal [ [ 0, -1 ], [ -2, -3 ] ],
198
+ ( ~M[ [ -1, 0 ], [ 1, 2 ] ] ).to_a
199
+ end
200
+
201
+ def test_bitwise_and
202
+ assert_equal [ [ 0, 1 ], [ 0, 1 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] & 1 ).to_a
203
+ assert_equal [ [ 0, 1 ], [ 0, 1 ] ], ( 1 & M[ [ 0, 1 ], [ 2, 3 ] ] ).to_a
204
+ assert_equal [ [ 0, 1 ], [ 0, 2 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] &
205
+ M[ [ 4, 3 ], [ 1, 2 ] ] ).to_a
206
+ end
207
+
208
+ def test_bitwise_or
209
+ assert_equal [ [ 1, 1 ], [ 3, 3 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] | 1 ).to_a
210
+ assert_equal [ [ 1, 1 ], [ 3, 3 ] ], ( 1 | M[ [ 0, 1 ], [ 2, 3 ] ] ).to_a
211
+ assert_equal [ [ 4, 3 ], [ 3, 3 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] |
212
+ M[ [ 4, 3 ], [ 1, 2 ] ] ).to_a
213
+ end
214
+
215
+ def test_bitwise_xor
216
+ assert_equal [ [ 1, 0 ], [ 3, 2 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] ^ 1 ).to_a
217
+ assert_equal [ [ 1, 0 ], [ 3, 2 ] ], ( 1 ^ M[ [ 0, 1 ], [ 2, 3 ] ] ).to_a
218
+ assert_equal [ [ 4, 2 ], [ 3, 1 ] ], ( M[ [ 0, 1 ], [ 2, 3 ] ] ^
219
+ M[ [ 4, 3 ], [ 1, 2 ] ] ).to_a
220
+ end
221
+
222
+ def test_shl
223
+ assert_equal [ [ 2, 4 ], [ 6, 8 ] ], ( M[ [ 1, 2 ], [ 3, 4 ] ] << 1 ).to_a
224
+ assert_equal [ [ 6, 12 ], [ 24, 48 ] ],
225
+ ( 3 << M[ [ 1, 2 ], [ 3, 4 ] ] ).to_a
226
+ assert_equal [ [ 8, 8 ], [ 6, 4 ] ],
227
+ ( M[ [ 1, 2 ], [ 3, 4 ] ] << M[ [ 3, 2 ], [ 1, 0 ] ] ).to_a
228
+ end
229
+
230
+ def test_shr
231
+ assert_equal [ [ 1, 2 ], [ 3, 4 ] ], ( M[ [ 2, 4 ], [ 6, 8 ] ] >> 1 ).to_a
232
+ assert_equal [ [ 24, 12 ], [ 6, 3 ] ],
233
+ ( 48 >> M[ [ 1, 2 ], [ 3, 4 ] ] ).to_a
234
+ assert_equal [ [ 2, 1 ], [ 3, 2 ] ],
235
+ ( M[ [ 16, 4 ], [ 6, 2 ] ] >> M[ [ 3, 2 ], [ 1, 0 ] ] ).to_a
81
236
  end
82
237
 
83
238
  def test_negate
84
- m = M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_type O
85
- assert_equal [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], ( -m ).to_a
86
- end
87
-
88
- def test_lazy
89
- m = M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_type O
90
- u = lazy { -m }
91
- assert_equal 'MultiArray.object(3,2):<delayed>', u.inspect
92
- assert_equal [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], u.force.to_a
93
- u = lazy { --m }
94
- assert_equal 'MultiArray.object(3,2):<delayed>', u.inspect
95
- assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], u.force.to_a
96
- u = -lazy { -m }
97
- assert_equal "MultiArray.object(3,2):\n[ [ 1, 2, 3 ],\n [ 4, 5, 6 ] ]",
98
- u.inspect
99
- assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], u.to_a
100
- u = lazy { -lazy { -m } }
101
- assert_equal 'MultiArray.object(3,2):<delayed>', u.inspect
102
- assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], u.force.to_a
103
- u = eager { lazy { -m } }
104
- assert_equal 'MultiArray.object(3,2):<delayed>', u.inspect
105
- assert_equal [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], u.force.to_a
106
- u = lazy { eager { -lazy { -m } } }
107
- assert_equal "MultiArray.object(3,2):\n[ [ 1, 2, 3 ],\n [ 4, 5, 6 ] ]",
108
- u.inspect
109
- assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], u.to_a
239
+ assert_equal M[ [ -1, 2, -3 ], [ 4, -5, 6 ] ],
240
+ -M[ [ 1, -2, 3 ], [ -4, 5, -6 ] ]
241
+ end
242
+
243
+ def test_plus
244
+ assert_equal M[ [ 2, 3, 5 ], [ 3, 5, 7 ] ],
245
+ M[ [ 1, 2, 4 ], [ 2, 4, 6 ] ] + 1
246
+ assert_equal M[ [ 2, 3, 5 ], [ 3, 5, 7 ] ],
247
+ 1 + M[ [ 1, 2, 4 ], [ 2, 4, 6 ] ]
248
+ assert_equal M[ [ -3, 2, 1 ], [ 8, 6, 4 ] ] +
249
+ M[ [ 2, 0, 2 ], [ -4, -1, 2 ] ],
250
+ M[ [ -1, 2, 3 ], [ 4, 5, 6 ] ]
110
251
  end
111
252
 
112
253
  end
data/test/tc_object.rb CHANGED
@@ -1,3 +1,19 @@
1
+ # multiarray - Lazy multi-dimensional arrays for Ruby
2
+ # Copyright (C) 2010 Jan Wedekind
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
1
17
  require 'test/unit'
2
18
  begin
3
19
  require 'rubygems'
@@ -9,44 +25,56 @@ class TC_Object < Test::Unit::TestCase
9
25
 
10
26
  O = Hornetseye::OBJECT
11
27
 
12
- def lazy( &action )
13
- Hornetseye::lazy &action
28
+ def setup
14
29
  end
15
30
 
16
- def eager( &action )
17
- Hornetseye::eager &action
31
+ def teardown
18
32
  end
19
33
 
20
- def setup
34
+ def test_object_inspect
35
+ assert_equal 'OBJECT', O.inspect
21
36
  end
22
37
 
23
- def teardown
38
+ def test_object_to_s
39
+ assert_equal 'OBJECT', O.to_s
24
40
  end
25
41
 
26
42
  def test_object_default
27
43
  assert_nil O.new[]
28
44
  end
29
45
 
30
- def test_object_inspect
31
- assert_equal 'OBJECT', O.inspect
46
+ def test_object_typecode
47
+ assert_equal O, O.typecode
32
48
  end
33
49
 
34
- def test_object_to_s
35
- assert_equal 'OBJECT', O.to_s
50
+ def test_object_dimension
51
+ assert_equal 0, O.dimension
36
52
  end
37
53
 
38
- def test_inspect
39
- assert_equal 'OBJECT(42)', O.new( 42 ).inspect
54
+ def test_object_shape
55
+ assert_equal [], O.shape
40
56
  end
41
57
 
42
- def test_to_s
43
- assert_equal '42', O.new( 42 ).to_s
58
+ def test_inspect
59
+ assert_equal 'OBJECT(42)', O.new( 42 ).inspect
44
60
  end
45
61
 
46
62
  def test_marshal
47
63
  assert_equal O.new( 42 ), Marshal.load( Marshal.dump( O.new( 42 ) ) )
48
64
  end
49
65
 
66
+ def test_typecode
67
+ assert_equal O, O.new.typecode
68
+ end
69
+
70
+ def test_dimension
71
+ assert_equal 0, O.new.dimension
72
+ end
73
+
74
+ def test_shape
75
+ assert_equal [], O.new.shape
76
+ end
77
+
50
78
  def test_at_assign
51
79
  o = O.new 3
52
80
  assert_equal 3, o[]
@@ -59,45 +87,22 @@ class TC_Object < Test::Unit::TestCase
59
87
  assert_equal O.new( 3 ), O.new( 3 )
60
88
  end
61
89
 
90
+ def test_inject
91
+ assert_equal 2, O.new( 2 ).inject { |a,b| a + b }[]
92
+ assert_equal 3, O.new( 2 ).inject( 1 ) { |a,b| a + b }[]
93
+ end
94
+
95
+ def test_zero
96
+ assert O.new( 0 ).zero?[]
97
+ assert !O.new( 3 ).zero?[]
98
+ end
99
+
62
100
  def test_negate
63
- o = O.new 5
64
- assert_equal O.new( -5 ), -o
101
+ assert_equal O.new( -5 ), -O.new( 5 )
65
102
  end
66
103
 
67
104
  def test_plus
68
- v = O.new 3
69
- w = O.new 5
70
- assert_equal O.new( 3 + 5 ), v + w
71
- end
72
-
73
- def test_lazy_unary
74
- o = lazy { -O.new( 3 ) }
75
- assert_not_equal O.new( -3 ), o
76
- assert_equal 'OBJECT(<delayed>)', o.inspect
77
- assert_equal O.new( -3 ), o.force
78
- o = lazy { --O.new( 3 ) }
79
- assert_equal 'OBJECT(<delayed>)', o.inspect
80
- assert_equal O.new( 3 ), o.force
81
- o = -lazy { -O.new( 3 ) }
82
- assert_equal O.new( 3 ), o
83
- o = lazy { -lazy { -O.new( 3 ) } }
84
- assert_equal 'OBJECT(<delayed>)', o.inspect
85
- assert_equal O.new( 3 ), o.force
86
- o = eager { lazy { -O.new( 3 ) } }
87
- assert_equal 'OBJECT(<delayed>)', o.inspect
88
- o = lazy { eager { -lazy { -O.new( 3 ) } } }
89
- assert_equal O.new( 3 ), o
90
- end
91
-
92
- def test_lazy_binary
93
- v = O.new 3
94
- w = O.new 5
95
- o = lazy { v + w }
96
- assert_not_equal v + w, o
97
- assert_equal 'OBJECT(<delayed>)', o.inspect
98
- assert_equal O.new( 8 ), o.force
99
- assert_equal O.new( 9 ), o + O.new( 1 )
100
- assert_equal O.new( 9 ), O.new( 1 ) + o
105
+ assert_equal O.new( 3 + 5 ), O.new( 3 ) + O.new( 5 )
101
106
  end
102
107
 
103
108
  end