multiarray 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_object.rb CHANGED
@@ -25,6 +25,10 @@ class TC_Object < Test::Unit::TestCase
25
25
 
26
26
  O = Hornetseye::OBJECT
27
27
 
28
+ def O( *args )
29
+ Hornetseye::OBJECT *args
30
+ end
31
+
28
32
  def setup
29
33
  end
30
34
 
@@ -43,6 +47,12 @@ class TC_Object < Test::Unit::TestCase
43
47
  assert_nil O.new[]
44
48
  end
45
49
 
50
+ def test_object_indgen
51
+ assert_equal 0, O.indgen
52
+ assert_equal 1, O.indgen( 1 )
53
+ assert_equal 1, O.indgen( 1, 2 )
54
+ end
55
+
46
56
  def test_object_typecode
47
57
  assert_equal O, O.typecode
48
58
  end
@@ -55,12 +65,16 @@ class TC_Object < Test::Unit::TestCase
55
65
  assert_equal [], O.shape
56
66
  end
57
67
 
68
+ def test_object_size
69
+ assert_equal 1, O.size
70
+ end
71
+
58
72
  def test_inspect
59
- assert_equal 'OBJECT(42)', O.new( 42 ).inspect
73
+ assert_equal 'OBJECT(42)', O( 42 ).inspect
60
74
  end
61
75
 
62
76
  def test_marshal
63
- assert_equal O.new( 42 ), Marshal.load( Marshal.dump( O.new( 42 ) ) )
77
+ assert_equal O( 42 ), Marshal.load( Marshal.dump( O( 42 ) ) )
64
78
  end
65
79
 
66
80
  def test_typecode
@@ -75,34 +89,38 @@ class TC_Object < Test::Unit::TestCase
75
89
  assert_equal [], O.new.shape
76
90
  end
77
91
 
92
+ def test_size
93
+ assert_equal 1, O.new.size
94
+ end
95
+
78
96
  def test_at_assign
79
- o = O.new 3
97
+ o = O 3
80
98
  assert_equal 3, o[]
81
99
  assert_equal 42, o[] = 42
82
100
  assert_equal 42, o[]
83
101
  end
84
102
 
85
103
  def test_equal
86
- assert_not_equal O.new( 3 ), O.new( 4 )
87
- assert_equal O.new( 3 ), O.new( 3 )
104
+ assert_not_equal O( 3 ), O( 4 )
105
+ assert_equal O( 3 ), O( 3 )
88
106
  end
89
107
 
90
108
  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 }[]
109
+ assert_equal 2, O( 2 ).inject { |a,b| a + b }[]
110
+ assert_equal 3, O( 2 ).inject( 1 ) { |a,b| a + b }[]
93
111
  end
94
112
 
95
113
  def test_zero
96
- assert O.new( 0 ).zero?[]
97
- assert !O.new( 3 ).zero?[]
114
+ assert O( 0 ).zero?[]
115
+ assert !O( 3 ).zero?[]
98
116
  end
99
117
 
100
118
  def test_negate
101
- assert_equal O.new( -5 ), -O.new( 5 )
119
+ assert_equal O( -5 ), -O( 5 )
102
120
  end
103
121
 
104
122
  def test_plus
105
- assert_equal O.new( 3 + 5 ), O.new( 3 ) + O.new( 5 )
123
+ assert_equal O( 3 + 5 ), O( 3 ) + O( 5 )
106
124
  end
107
125
 
108
126
  end
data/test/tc_rgb.rb ADDED
@@ -0,0 +1,217 @@
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
+
17
+ require 'test/unit'
18
+ begin
19
+ require 'rubygems'
20
+ rescue LoadError
21
+ end
22
+ Kernel::require 'multiarray'
23
+
24
+ class TC_RGB < Test::Unit::TestCase
25
+
26
+ BYTERGB = Hornetseye::BYTERGB
27
+ UBYTERGB = Hornetseye::UBYTERGB
28
+ SINTRGB = Hornetseye::SINTRGB
29
+ USINTRGB = Hornetseye::USINTRGB
30
+ INTRGB = Hornetseye::INTRGB
31
+ UINTRGB = Hornetseye::UINTRGB
32
+ LONGRGB = Hornetseye::LONGRGB
33
+ ULONGRGB = Hornetseye::ULONGRGB
34
+ SFLOATRGB = Hornetseye::SFLOATRGB
35
+ DFLOATRGB = Hornetseye::DFLOATRGB
36
+
37
+ def RGB( *args )
38
+ Hornetseye::RGB *args
39
+ end
40
+
41
+ def INTRGB( value )
42
+ Hornetseye::INTRGB value
43
+ end
44
+
45
+ def sum( *args, &action )
46
+ Hornetseye::sum *args, &action
47
+ end
48
+
49
+ def setup
50
+ end
51
+
52
+ def teardown
53
+ end
54
+
55
+ def test_rgb_inspect
56
+ assert_equal 'BYTERGB', BYTERGB.inspect
57
+ assert_equal 'UBYTERGB', UBYTERGB.inspect
58
+ assert_equal 'SINTRGB', SINTRGB.inspect
59
+ assert_equal 'USINTRGB', USINTRGB.inspect
60
+ assert_equal 'INTRGB', INTRGB.inspect
61
+ assert_equal 'UINTRGB', UINTRGB.inspect
62
+ assert_equal 'LONGRGB', LONGRGB.inspect
63
+ assert_equal 'ULONGRGB', ULONGRGB.inspect
64
+ assert_equal 'SFLOATRGB', SFLOATRGB.inspect
65
+ assert_equal 'DFLOATRGB', DFLOATRGB.inspect
66
+ end
67
+
68
+ def test_rgb_to_s
69
+ assert_equal 'BYTERGB', BYTERGB.to_s
70
+ assert_equal 'UBYTERGB', UBYTERGB.to_s
71
+ assert_equal 'SINTRGB', SINTRGB.to_s
72
+ assert_equal 'USINTRGB', USINTRGB.to_s
73
+ assert_equal 'INTRGB', INTRGB.to_s
74
+ assert_equal 'UINTRGB', UINTRGB.to_s
75
+ assert_equal 'LONGRGB', LONGRGB.to_s
76
+ assert_equal 'ULONGRGB', ULONGRGB.to_s
77
+ assert_equal 'SFLOATRGB', SFLOATRGB.to_s
78
+ assert_equal 'DFLOATRGB', DFLOATRGB.to_s
79
+ end
80
+
81
+ def test_rgb_default
82
+ assert_equal RGB( 0, 0, 0 ), INTRGB.new[]
83
+ end
84
+
85
+ def test_rgb_indgen
86
+ assert_equal 0, INTRGB.indgen
87
+ assert_equal RGB( 1, 2, 3 ), INTRGB.indgen( RGB( 1, 2, 3 ) )
88
+ assert_equal RGB( 1, 2, 3 ), INTRGB.indgen( RGB( 1, 2, 3 ), 1 )
89
+ end
90
+
91
+ def test_rgb_typecode
92
+ assert_equal BYTERGB, BYTERGB.typecode
93
+ end
94
+
95
+ def test_rgb_dimension
96
+ assert_equal 0, SFLOATRGB.dimension
97
+ end
98
+
99
+ def test_rgb_shape
100
+ assert_equal [], SFLOATRGB.shape
101
+ end
102
+
103
+ def test_rgb_size
104
+ assert_equal 1, SINTRGB.size
105
+ end
106
+
107
+ def test_inspect
108
+ assert_equal 'RGB(1,2,3)', RGB( 1, 2, 3 ).inspect
109
+ end
110
+
111
+ def test_marshal
112
+ assert_equal RGB( 1, 2, 3 ), Marshal.load( Marshal.dump( RGB( 1, 2, 3 ) ) )
113
+ end
114
+
115
+ def test_typecode
116
+ assert_equal INTRGB, INTRGB.new.typecode
117
+ end
118
+
119
+ def test_dimension
120
+ assert_equal 0, INTRGB.new.dimension
121
+ end
122
+
123
+ def test_shape
124
+ assert_equal [], INTRGB.new.shape
125
+ end
126
+
127
+ def test_size
128
+ assert_equal 1, UBYTERGB.new.size
129
+ end
130
+
131
+ def test_at_assign
132
+ c = INTRGB RGB( 1, 2, 3 )
133
+ assert_equal RGB( 1, 2, 3 ), c[]
134
+ assert_equal RGB( 4, 5, 6 ), c[] = RGB( 4, 5, 6 )
135
+ assert_equal RGB( 4, 5, 6 ), c[]
136
+ end
137
+
138
+ def test_equal
139
+ assert_not_equal RGB( 1, 2, 3 ), RGB( 2, 2, 3 )
140
+ assert_not_equal RGB( 1, 2, 3 ), RGB( 1, 3, 3 )
141
+ assert_not_equal RGB( 1, 2, 3 ), RGB( 1, 2, 2 )
142
+ assert_equal RGB( 1, 2, 3 ), RGB( 1, 2, 3 )
143
+ assert_equal RGB( 3, 3, 3 ), 3
144
+ assert_not_equal RGB( 3, 3, 3 ), 4
145
+ end
146
+
147
+ def test_inject
148
+ assert_equal RGB( 1, 2, 3 ), INTRGB( RGB( 1, 2, 3 ) ).
149
+ inject { |a,b| a + b }[]
150
+ assert_equal RGB( 3, 5, 7 ), INTRGB( RGB( 1, 2, 3 ) ).
151
+ inject( RGB( 2, 3, 4 ) ) { |a,b| a + b }[]
152
+ end
153
+
154
+ def test_not
155
+ assert !RGB( 0, 0, 0 ).not
156
+ assert !RGB( 1, 2, 3 ).not
157
+ end
158
+
159
+ def test_sum
160
+ assert_equal RGB( 1, 2, 3 ), sum { || RGB 1, 2, 3 }
161
+ end
162
+
163
+ def test_zero
164
+ assert RGB( 0, 0, 0 ).zero?
165
+ assert !RGB( 1, 0, 0 ).zero?
166
+ assert !RGB( 0, 1, 0 ).zero?
167
+ assert !RGB( 0, 0, 1 ).zero?
168
+ end
169
+
170
+ def test_nonzero
171
+ assert !RGB( 0, 0, 0 ).nonzero?
172
+ assert RGB( 1, 0, 0 ).nonzero?
173
+ assert RGB( 0, 1, 0 ).nonzero?
174
+ assert RGB( 0, 0, 1 ).nonzero?
175
+ end
176
+
177
+ def test_bitwise_not
178
+ assert_equal RGB( -2, -3, -4 ), ~RGB( 1, 2, 3 )
179
+ end
180
+
181
+ def test_bitwise_and
182
+ assert_equal RGB( 1, 2, 1 ), RGB( 1, 2, 3 ) & RGB( 3, 2, 1 )
183
+ assert_equal RGB( 2, 2, 0 ), 2 & RGB( 3, 2, 1 )
184
+ assert_equal RGB( 0, 2, 2 ), RGB( 1, 2, 3 ) & 2
185
+ end
186
+
187
+ def test_bitwise_or
188
+ assert_equal RGB( 3, 2, 3 ), RGB( 1, 2, 3 ) | RGB( 3, 2, 1 )
189
+ end
190
+
191
+ def test_shl
192
+ assert_equal RGB( 2, 4, 6 ), RGB( 1, 2, 3 ) << 1
193
+ end
194
+
195
+ def test_shr
196
+ assert_equal RGB( 1, 2, 3 ), RGB( 2, 4, 6 ) >> 1
197
+ end
198
+
199
+ def test_negate
200
+ assert_equal RGB( -1, -2, -3 ), -RGB( 1, 2, 3 )
201
+ end
202
+
203
+ def test_plus
204
+ assert_equal RGB( 5, 7, 9 ), RGB( 1, 2, 3 ) + RGB( 4, 5, 6 )
205
+ assert_equal RGB( 2, 3, 4 ), RGB( 1, 2, 3 ) + 1
206
+ assert_equal RGB( 2, 3, 4 ), 1 + RGB( 1, 2, 3 )
207
+ end
208
+
209
+ def test_major
210
+ assert_equal RGB( 4, 3, 3 ), RGB( 1, 2, 3 ).major( RGB( 4, 3, 2 ) )
211
+ end
212
+
213
+ def test_minor
214
+ assert_equal RGB( 1, 2, 2 ), RGB( 1, 2, 3 ).minor( RGB( 4, 3, 2 ) )
215
+ end
216
+
217
+ end
data/test/tc_sequence.rb CHANGED
@@ -27,10 +27,15 @@ class TC_Sequence < Test::Unit::TestCase
27
27
  B = Hornetseye::BOOL
28
28
  I = Hornetseye::INT
29
29
  S = Hornetseye::Sequence
30
+ C = Hornetseye::INTRGB
30
31
 
31
32
  def S( *args )
32
33
  Hornetseye::Sequence *args
33
34
  end
35
+
36
+ def C( *args )
37
+ Hornetseye::RGB *args
38
+ end
34
39
 
35
40
  def sum( *args, &action )
36
41
  Hornetseye::sum *args, &action
@@ -51,25 +56,42 @@ class TC_Sequence < Test::Unit::TestCase
51
56
  end
52
57
 
53
58
  def test_sequence_default
54
- assert_equal [ O.default ] * 3, S( O, 3 ).default.to_a
59
+ assert_equal [ nil ] * 3, S( O, 3 ).default.to_a
60
+ assert_equal [ 0 ] * 3, S( I, 3 ).default.to_a
61
+ assert_equal [ C( 0, 0, 0 ) ] * 3, S( C, 3 ).default.to_a
55
62
  end
56
63
 
57
64
  def test_sequence_indgen
58
- assert_equal [ 0, 1, 2 ], S( O, 3 ).indgen.to_a
59
- assert_equal [ 1, 2, 3 ], S( O, 3 ).indgen( 1 ).to_a
60
- assert_equal [ 0, 2, 4 ], S( O, 3 ).indgen( 0, 2 ).to_a
61
- assert_equal [ 1, 3, 5 ], S( O, 3 ).indgen( 1, 2 ).to_a
65
+ assert_equal [ 0, 1, 2 ], S( I, 3 ).indgen.to_a
66
+ assert_equal [ 1, 2, 3 ], S( I, 3 ).indgen( 1 ).to_a
67
+ assert_equal [ 0, 2, 4 ], S( I, 3 ).indgen( 0, 2 ).to_a
68
+ assert_equal [ 1, 3, 5 ], S( I, 3 ).indgen( 1, 2 ).to_a
69
+ assert_equal [ C( 1, 2, 3 ), C( 3, 5, 7 ) ],
70
+ S( C, 2 ).indgen( C( 1, 2, 3 ), C( 2, 3, 4 ) ).to_a
62
71
  end
63
72
 
64
73
  def test_sequence_at
74
+ assert_equal "Sequence(INT,3):\n[ 1, 2, 3 ]",
75
+ S( I, 3 )[ 1, 2, 3 ].inspect
76
+ assert_equal "Sequence(OBJECT,3):\n[ 1, 2, 3 ]",
77
+ S( O, 3 )[ 1, 2, 3 ].inspect
78
+ assert_equal "Sequence(INTRGB,2):\n[ RGB(1,2,3), RGB(4,5,6) ]",
79
+ S( C, 2 )[ C( 1, 2, 3 ), C( 4, 5, 6 ) ].inspect
80
+ end
81
+
82
+ def test_sequence_match
65
83
  assert_equal [ 1, 2, 3 ], S[ 1, 2, 3 ].to_a
66
84
  assert_equal O, S[ :a ].typecode
67
85
  assert_equal B, S[ false, true ].typecode
68
86
  assert_equal I, S[ -2 ** 31, 2 ** 31 - 1 ].typecode
87
+ assert_equal C, S[ C( -2 ** 31, 2 ** 31 - 1, 0 ) ].typecode
69
88
  end
70
89
 
71
90
  def test_sequence_typecode
72
91
  assert_equal O, S( O, 3 ).typecode
92
+ assert_equal B, S( B, 3 ).typecode
93
+ assert_equal I, S( I, 3 ).typecode
94
+ assert_equal C, S( C, 3 ).typecode
73
95
  end
74
96
 
75
97
  def test_sequence_dimension
@@ -80,29 +102,59 @@ class TC_Sequence < Test::Unit::TestCase
80
102
  assert_equal [ 3 ], S( O, 3 ).shape
81
103
  end
82
104
 
105
+ def test_sequence_size
106
+ assert_equal 3, S( O, 3 ).size
107
+ assert_equal 3, S( C, 3 ).size
108
+ end
109
+
83
110
  def test_inspect
111
+ assert_equal "Sequence(OBJECT,0):\n[]", S[].inspect
84
112
  assert_equal "Sequence(OBJECT,3):\n[ :a, 2, 3 ]", S[ :a, 2, 3 ].inspect
85
113
  end
86
114
 
87
115
  def test_typecode
88
116
  assert_equal O, S.new( O, 3 ).typecode
117
+ assert_equal I, S.new( I, 3 ).typecode
89
118
  end
90
119
 
91
120
  def test_dimension
92
121
  assert_equal 1, S[ 1, 2, 3 ].dimension
122
+ assert_equal 1, S[ C( 1, 2, 3 ) ].dimension
93
123
  end
94
124
 
95
125
  def test_shape
96
126
  assert_equal [ 3 ], S[ 1, 2, 3 ].shape
97
127
  end
98
128
 
129
+ def test_size
130
+ assert_equal 3, S[ 1, 2, 3 ].size
131
+ end
132
+
99
133
  def test_at_assign
100
- s = S.new O, 3
101
- for i in 0 ... 3
102
- assert_equal i + 1, s[ i ] = i + 1
134
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
135
+ s = t.new
136
+ for i in 0 ... 3
137
+ assert_equal i + 1, s[ i ] = i + 1
138
+ end
139
+ for i in 0 ... 3
140
+ assert_equal i + 1, s[ i ]
141
+ end
103
142
  end
104
- for i in 0 ... 3
105
- assert_equal i + 1, s[ i ]
143
+ end
144
+
145
+ def test_slice
146
+ [ S( O, 4 ), S( I, 4 ) ].each do |t|
147
+ s = t.indgen( 1 )[]
148
+ assert_equal [ 2, 3 ], s[ 1 .. 2 ].to_a
149
+ assert_equal [ 2, 3 ], s[ 1 ... 3 ].to_a
150
+ s[ 1 .. 2 ] = 0
151
+ assert_equal [ 1, 0, 0, 4 ], s.to_a
152
+ s[ 1 ... 3 ] = 5
153
+ assert_equal [ 1, 5, 5, 4 ], s.to_a
154
+ s[ 1 .. 2 ] = S[ 6, 7 ]
155
+ assert_equal [ 1, 6, 7, 4 ], s.to_a
156
+ s[ 1 ... 3 ] = S[ 8, 9 ]
157
+ assert_equal [ 1, 8, 9, 4 ], s.to_a
106
158
  end
107
159
  end
108
160
 
@@ -117,38 +169,74 @@ class TC_Sequence < Test::Unit::TestCase
117
169
  def test_inject
118
170
  assert_equal 6, S[ 1, 2, 3 ].inject { |a,b| a + b }
119
171
  assert_equal 10, S[ 1, 2, 3 ].inject( 4 ) { |a,b| a + b }
172
+ assert_equal 'abc', S[ 'a', 'b', 'c' ].inject { |a,b| a + b }
173
+ assert_equal 'abcd', S[ 'b', 'c', 'd' ].inject( 'a' ) { |a,b| a + b }
174
+ assert_equal C( 3, 5, 8 ), S[ C( 1, 2, 3 ), C( 2, 3, 5 ) ].inject { |a,b| a + b }
175
+ assert_equal C( 5, 6, 8 ), S[ C( 1, 2, 3 ), C( 2, 3, 5 ) ].
176
+ inject( C( 2, 1, 0 ) ) { |a,b| a + b }
120
177
  end
121
178
 
122
179
  def test_sum
123
- assert_equal 6, sum { |i| S[ 1, 2, 3 ][ i ] }
124
- assert_equal [ 1, 2, 3 ], sum { || S[ 1, 2, 3 ] }.to_a
180
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
181
+ assert_equal 6, sum { |i| t[ 1, 2, 3 ][ i ] }
182
+ assert_equal [ 1, 2, 3 ], sum { || t[ 1, 2, 3 ] }.to_a
183
+ end
184
+ assert_equal C( 3, 5, 8 ), sum { |i| S[ C( 1, 2, 3 ), C( 2, 3, 5 ) ][i] }
125
185
  end
126
186
 
127
- def dont_test_convolve
128
- assert_equal S[ 2, 3, 0, 0, 0 ],
129
- S[ 1, 0, 0, 0, 0 ].convolve( S[ 1, 2, 3 ] )
130
- assert_equal S[ 1, 2, 3, 0, 0 ],
131
- S[ 0, 1, 0, 0, 0 ].convolve( S[ 1, 2, 3 ] )
132
- assert_equal S[ 0, 1, 2, 3, 0 ],
133
- S[ 0, 0, 1, 0, 0 ].convolve( S[ 1, 2, 3 ] )
134
- assert_equal S[ 0, 0, 1, 2, 3 ],
135
- S[ 0, 0, 0, 1, 0 ].convolve( S[ 1, 2, 3 ] )
136
- assert_equal S[ 0, 0, 0, 1, 2 ],
137
- S[ 0, 0, 0, 0, 1 ].convolve( S[ 1, 2, 3 ] )
138
- assert_equal S[ 1, 2, 3, 0 ],
139
- S[ 0, 1, 0, 0 ].convolve( S[ 1, 2, 3 ] )
187
+ def test_min
188
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
189
+ assert_equal 2, t[ 4, 2, 3 ].min
190
+ end
191
+ assert_equal C( 1, 2, 1 ), S[ C( 1, 2, 3 ), C( 3, 2, 1 ) ].min
192
+ end
193
+
194
+ def test_max
195
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
196
+ assert_equal 4, t[ 4, 2, 3 ].max
197
+ end
198
+ assert_equal C( 3, 2, 3 ), S[ C( 1, 2, 3 ), C( 3, 2, 1 ) ].max
199
+ end
200
+
201
+ def test_convolve
202
+ [ O, I ].each do |t|
203
+ assert_equal S( t, 5 )[ 2, 3, 0, 0, 0 ],
204
+ S( t, 5 )[ 1, 0, 0, 0, 0 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
205
+ assert_equal S( t, 5 )[ 1, 2, 3, 0, 0 ],
206
+ S( t, 5 )[ 0, 1, 0, 0, 0 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
207
+ assert_equal S( t, 5 )[ 0, 1, 2, 3, 0 ],
208
+ S( t, 5 )[ 0, 0, 1, 0, 0 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
209
+ assert_equal S( t, 5 )[ 0, 0, 1, 2, 3 ],
210
+ S( t, 5 )[ 0, 0, 0, 1, 0 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
211
+ assert_equal S( t, 5 )[ 0, 0, 0, 1, 2 ],
212
+ S( t, 5 )[ 0, 0, 0, 0, 1 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
213
+ assert_equal S( t, 4 )[ 1, 2, 3, 0 ],
214
+ S( t, 4 )[ 0, 1, 0, 0 ].convolve( S( t, 3 )[ 1, 2, 3 ] )
215
+ end
216
+ assert_equal S[ C( 1, 0, 0 ), C( 2, 1, 0 ), C( 3, 2, 1 ), C( 0, 3, 2 ),
217
+ C( 0, 0, 3 ) ],
218
+ S[ 0, 1, 2, 3, 0 ].
219
+ convolve( S[ C( 1, 0, 0 ), C( 0, 1, 0 ), C( 0, 0, 1 ) ] )
140
220
  end
141
221
 
142
222
  def test_zero
143
- assert_equal S[ false, true, false ], S[ -1, 0, 1 ].zero?
223
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
224
+ assert_equal [ false, true, false ], t[ -1, 0, 1 ].zero?.to_a
225
+ end
226
+ assert_equal S[ false, false, false, true ],
227
+ S[ C( 1, 0, 0 ), C( 0, 1, 0 ), C( 0, 0, 1 ), C( 0, 0, 0 ) ].zero?
144
228
  end
145
229
 
146
230
  def test_nonzero
147
- assert_equal S[ true, false, true ], S[ -1, 0, 1 ].nonzero?
231
+ assert_equal S[ true, false, true ], S( I, 3 )[ -1, 0, 1 ].nonzero?
232
+ assert_equal S[ -1, nil, 1 ], S( O, 3 )[ -1, 0, 1 ].nonzero?
233
+ assert_equal S[ true, true, true, false ],
234
+ S[ C( 1, 0, 0 ), C( 0, 1, 0 ), C( 0, 0, 1 ), C( 0, 0, 0 ) ].nonzero?
148
235
  end
149
236
 
150
237
  def test_not
151
- assert_equal [ true, false ], S[ false, true ].not.to_a
238
+ assert_equal [ true, false ], S( O, 2 )[ false, true ].not.to_a
239
+ assert_equal [ true, false ], S( B, 2 )[ false, true ].not.to_a
152
240
  assert_equal [ true, false, false ], S[ 0, 1, 2 ].not.to_a
153
241
  end
154
242
 
@@ -171,42 +259,68 @@ class TC_Sequence < Test::Unit::TestCase
171
259
  end
172
260
 
173
261
  def test_bitwise_not
174
- assert_equal [ 255, 254, 253 ], ( ~S[ 0, 1, 2 ] ).to_a
175
- assert_equal [ 0, -1, -2, -3 ], ( ~S[ -1, 0, 1, 2 ] ).to_a
262
+ [ S( O, 4 ), S( I, 4 ) ].each do |t|
263
+ assert_equal [ 0, -1, -2, -3 ], ( ~t[ -1, 0, 1, 2 ] ).to_a
264
+ end
265
+ assert_equal [ C( -2, -3, -4 ), C( -5, -6, -7 ) ],
266
+ ( ~S( C, 2 )[ C( 1, 2, 3 ), C( 4, 5, 6 ) ] ).to_a
176
267
  end
177
268
 
178
269
  def test_bitwise_and
179
- assert_equal [ 0, 1, 0 ], ( S[ 0, 1, 2 ] & 1 ).to_a
180
- assert_equal [ 0, 1, 0 ], ( 1 & S[ 0, 1, 2 ] ).to_a
181
- assert_equal [ 0, 1, 0, 2 ], ( S[ 0, 1, 2, 3 ] & S[ 4, 3, 1, 2 ] ).to_a
270
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
271
+ assert_equal [ 0, 1, 0 ], ( t[ 0, 1, 2 ] & 1 ).to_a
272
+ assert_equal [ 0, 1, 0 ], ( 1 & t[ 0, 1, 2 ] ).to_a
273
+ assert_equal [ 0, 1, 2 ], ( t[ 0, 1, 3 ] & t[ 4, 3, 2 ] ).to_a
274
+ end
275
+ assert_equal [ C( 0, 2, 2 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] & 2 ).to_a
276
+ assert_equal [ C( 1, 0, 1 ) ], ( 1 & S( C, 1 )[ C( 1, 2, 3 ) ] ).to_a
277
+ assert_equal [ C( 1, 2, 1 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] & C( 3, 2, 1 ) ).to_a
182
278
  end
183
279
 
184
280
  def test_bitwise_or
185
- assert_equal [ 1, 1, 3 ], ( S[ 0, 1, 2 ] | 1 ).to_a
186
- assert_equal [ 1, 1, 3 ], ( 1 | S[ 0, 1, 2 ] ).to_a
187
- assert_equal [ 4, 3, 3, 3 ], ( S[ 0, 1, 2, 3 ] | S[ 4, 3, 1, 2 ] ).to_a
281
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
282
+ assert_equal [ 1, 1, 3 ], ( t[ 0, 1, 2 ] | 1 ).to_a
283
+ assert_equal [ 1, 1, 3 ], ( 1 | t[ 0, 1, 2 ] ).to_a
284
+ assert_equal [ 4, 3, 3 ], ( t[ 0, 1, 2 ] | t[ 4, 3, 1 ] ).to_a
285
+ end
286
+ assert_equal [ C( 3, 2, 3 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] | 2 ).to_a
287
+ assert_equal [ C( 1, 3, 3 ) ], ( 1 | S( C, 1 )[ C( 1, 2, 3 ) ] ).to_a
288
+ assert_equal [ C( 3, 2, 3 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] | C( 3, 2, 1 ) ).to_a
188
289
  end
189
290
 
190
291
  def test_bitwise_xor
191
- assert_equal [ 1, 0, 3 ], ( S[ 0, 1, 2 ] ^ 1 ).to_a
192
- assert_equal [ 1, 0, 3 ], ( 1 ^ S[ 0, 1, 2 ] ).to_a
193
- assert_equal [ 4, 2, 3, 1 ], ( S[ 0, 1, 2, 3 ] ^ S[ 4, 3, 1, 2 ] ).to_a
292
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
293
+ assert_equal [ 1, 0, 3 ], ( t[ 0, 1, 2 ] ^ 1 ).to_a
294
+ assert_equal [ 1, 0, 3 ], ( 1 ^ t[ 0, 1, 2 ] ).to_a
295
+ assert_equal [ 4, 2, 3 ], ( t[ 0, 1, 2 ] ^ t[ 4, 3, 1 ] ).to_a
296
+ end
297
+ assert_equal [ C( 3, 0, 1 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] ^ 2 ).to_a
298
+ assert_equal [ C( 0, 3, 2 ) ], ( 1 ^ S( C, 1 )[ C( 1, 2, 3 ) ] ).to_a
299
+ assert_equal [ C( 2, 0, 2 ) ], ( S( C, 1 )[ C( 1, 2, 3 ) ] ^ C( 3, 2, 1 ) ).to_a
194
300
  end
195
301
 
196
302
  def test_shl
197
- assert_equal [ 2, 4, 6 ], ( S[ 1, 2, 3 ] << 1 ).to_a
198
- assert_equal [ 6, 12, 24 ], ( 3 << S[ 1, 2, 3 ] ).to_a
199
- assert_equal [ 8, 8, 6 ], ( S[ 1, 2, 3 ] << S[ 3, 2, 1 ] ).to_a
303
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
304
+ assert_equal [ 2, 4, 6 ], ( t[ 1, 2, 3 ] << 1 ).to_a
305
+ assert_equal [ 6, 12, 24 ], ( 3 << t[ 1, 2, 3 ] ).to_a
306
+ assert_equal [ 8, 8, 6 ], ( t[ 1, 2, 3 ] << t[ 3, 2, 1 ] ).to_a
307
+ end
200
308
  end
201
309
 
202
310
  def test_shr
203
- assert_equal [ 1, 2, 3 ], ( S[ 2, 4, 6 ] >> 1 ).to_a
204
- assert_equal [ 12, 6, 3 ], ( 24 >> S[ 1, 2, 3 ] ).to_a
205
- assert_equal [ 2, 1, 3 ], ( S[ 16, 4, 6 ] >> S[ 3, 2, 1 ] ).to_a
311
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
312
+ assert_equal [ 1, 2, 3 ], ( t[ 2, 4, 6 ] >> 1 ).to_a
313
+ assert_equal [ 12, 6, 3 ], ( 24 >> t[ 1, 2, 3 ] ).to_a
314
+ assert_equal [ 2, 1, 3 ], ( t[ 16, 4, 6 ] >> t[ 3, 2, 1 ] ).to_a
315
+ end
206
316
  end
207
317
 
208
318
  def test_negate
209
- assert_equal S[ -1, 2, -3 ], -S[ 1, -2, 3 ]
319
+ [ S( O, 3 ), S( I, 3 ) ].each do |t|
320
+ assert_equal t[ -1, 2, -3 ], -t[ 1, -2, 3 ]
321
+ end
322
+ assert_equal S( C, 2 )[ C( -1, -2, -3 ), C( -2, -1, 0 ) ],
323
+ -S( C, 2 )[ C( 1, 2, 3 ), C( 2, 1, 0 ) ]
210
324
  end
211
325
 
212
326
  def test_plus
@@ -216,14 +330,32 @@ class TC_Sequence < Test::Unit::TestCase
216
330
  assert_equal S[ 2, 3, 5 ], S[ 1, 2, 4 ] + 1
217
331
  assert_equal S[ 2, 3, 5 ], 1 + S[ 1, 2, 4 ]
218
332
  assert_equal S[ 2, 3, 5 ], S[ 1, 2, 3 ] + S[ 1, 1, 2 ]
333
+ assert_equal S[ C( 2, 3, 4 ), C( 5, 6, 7 ) ], S[ C( 1, 2, 3 ), C( 4, 5, 6 ) ] + 1
334
+ assert_equal S[ C( 2, 3, 4 ), C( 5, 6, 7 ) ], 1 + S[ C( 1, 2, 3 ), C( 4, 5, 6 ) ]
335
+ assert_equal S[ C( 2, 3, 4 ), C( 3, 4, 5 ) ], S[ 1, 2 ] + C( 1, 2, 3 )
219
336
  end
220
337
 
221
- def test_indgen
222
- assert_equal [ 0, 1, 2 ], S( I, 3 ).indgen.to_a
223
- assert_equal [ 1, 2, 3 ], ( S( I, 3 ).indgen + 1 ).to_a
224
- assert_equal [ 1, 2, 3 ], S( I, 3 ).indgen( 1 ).to_a
225
- assert_equal [ 1, 3, 5 ], ( 2 * S( I, 3 ).indgen + 1 ).to_a
226
- assert_equal [ 1, 3, 5 ], S( I, 3 ).indgen( 1, 2 ).to_a
338
+ def test_sqrt
339
+ assert_equal S( O, 3 )[ 1, 2, 3 ], Math.sqrt( S( O, 3 )[ 1, 4, 9 ] )
340
+ assert_equal S[ 1.0, 2.0, 3.0 ], Math.sqrt( S[ 1.0, 4.0, 9.0 ] )
341
+ end
342
+
343
+ def test_major
344
+ assert_equal [ 2, 2, 3 ], S[ 1, 2, 3 ].major( 2 ).to_a
345
+ assert_equal [ 2, 2, 3 ], 2.major( S[ 1, 2, 3 ] ).to_a
346
+ assert_equal [ 3, 2, 3 ], S[ 1, 2, 3 ].major( S[ 3, 2, 1 ] ).to_a
347
+ assert_equal [ C( 2, 2, 3 ) ], S[ C( 1, 2, 3 ) ].major( 2 ).to_a
348
+ end
349
+
350
+ def test_minor
351
+ assert_equal [ 1, 2, 2 ], S[ 1, 2, 3 ].minor( 2 ).to_a
352
+ assert_equal [ 1, 2, 2 ], 2.minor( S[ 1, 2, 3 ] ).to_a
353
+ assert_equal [ 1, 2, 1 ], S[ 1, 2, 3 ].minor( S[ 3, 2, 1 ] ).to_a
354
+ assert_equal [ C( 1, 2, 2 ) ], S[ C( 1, 2, 3 ) ].minor( 2 ).to_a
355
+ end
356
+
357
+ def test_hypot
358
+ assert_equal S[ 5.0, 5.0 ], Math.hypot( S[ 3, 4 ], S[ 4, 3 ] )
227
359
  end
228
360
 
229
361
  end