multiarray 0.2.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,30 +1,112 @@
1
1
  require 'test/unit'
2
+ begin
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ end
2
6
  Kernel::require 'multiarray'
3
7
 
4
8
  class TC_MultiArray < Test::Unit::TestCase
5
9
 
10
+ O = Hornetseye::OBJECT
11
+ M = Hornetseye::MultiArray
12
+
13
+ def lazy( &action )
14
+ Hornetseye::lazy &action
15
+ end
16
+
17
+ def eager( &action )
18
+ Hornetseye::eager &action
19
+ end
20
+
6
21
  def setup
7
- @@types = [ Hornetseye::UBYTE,
8
- Hornetseye::BYTE,
9
- Hornetseye::USINT,
10
- Hornetseye::SINT,
11
- Hornetseye::UINT,
12
- Hornetseye::INT,
13
- Hornetseye::ULONG,
14
- Hornetseye::LONG ]
15
22
  end
16
23
 
17
24
  def teardown
18
- @@types = nil
19
25
  end
20
26
 
21
- def test_multiarray_new
22
- for t in @@types
23
- m = Hornetseye::MultiArray.new t, 3, 2
24
- m.set
25
- assert_equal [ [ 0, 0, 0 ], [ 0, 0, 0 ] ], m.to_a
27
+ def M( *args )
28
+ Hornetseye::MultiArray *args
29
+ end
30
+
31
+ def test_default
32
+ assert_equal [ [ nil ] * 3 ] * 2, M( O, 3, 2 ).new.to_a
33
+ end
34
+
35
+ def test_multiarray_inspect
36
+ assert_equal 'MultiArray.object(3,2)', M( O, 3, 2 ).inspect
37
+ end
38
+
39
+ def test_multiarray_to_s
40
+ assert_equal 'MultiArray.object(3,2)', M( O, 3, 2 ).to_s
41
+ end
42
+
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
50
+ end
51
+
52
+ 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
56
+ end
57
+
58
+ def test_to_s
59
+ # !!!
60
+ end
61
+
62
+ def test_at_assign
63
+ m = M.new O, 3, 2
64
+ for j in 0 ... 2
65
+ for i in 0 ... 3
66
+ assert_equal i + j * 3 + 1, m[ j ][ i ] = i + j * 3 + 1
67
+ end
68
+ end
69
+ for j in 0 ... 2
70
+ for i in 0 ... 3
71
+ assert_equal i + j * 3 + 1, m[ j ][ i ]
72
+ end
26
73
  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
27
77
  end
28
78
 
29
- end
79
+ def test_equal
80
+ # !!!
81
+ end
82
+
83
+ def test_negate
84
+ m = M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
85
+ assert_equal [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], ( -m ).to_a
86
+ end
30
87
 
88
+ def test_lazy
89
+ m = M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
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
110
+ end
111
+
112
+ end
data/test/tc_object.rb CHANGED
@@ -1,79 +1,86 @@
1
1
  require 'test/unit'
2
+ begin
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ end
2
6
  Kernel::require 'multiarray'
3
7
 
4
8
  class TC_Object < Test::Unit::TestCase
5
9
 
6
- def setup
7
- @@t = Hornetseye::OBJECT
8
- end
10
+ O = Hornetseye::OBJECT
9
11
 
10
- def teardown
11
- @@t = nil
12
+ def lazy( &action )
13
+ Hornetseye::lazy &action
12
14
  end
13
15
 
14
- def test_object_default
15
- assert_nil @@t.default
16
+ def eager( &action )
17
+ Hornetseye::eager &action
16
18
  end
17
19
 
18
- def test_int_to_s
19
- assert_equal 'OBJECT', @@t.to_s
20
+ def setup
20
21
  end
21
22
 
22
- def test_int_inspect
23
- assert_equal @@t.to_s, @@t.inspect
23
+ def teardown
24
24
  end
25
25
 
26
- def test_bytesize
27
- assert_equal 1, @@t.bytesize
26
+ def test_object_default
27
+ assert_nil O.new[]
28
28
  end
29
29
 
30
- def test_typecode
31
- assert_equal @@t, @@t.typecode
30
+ def test_object_inspect
31
+ assert_equal 'OBJECT', O.inspect
32
32
  end
33
33
 
34
- def test_shape
35
- assert_equal [], @@t.shape
34
+ def test_object_to_s
35
+ assert_equal 'OBJECT', O.to_s
36
36
  end
37
37
 
38
- def test_size
39
- assert_equal 1, @@t.size
38
+ def test_inspect
39
+ assert_equal 'OBJECT(42)', O.new( 42 ).inspect
40
40
  end
41
41
 
42
42
  def test_to_s
43
- assert_equal '42', @@t.new( 42 ).to_s
43
+ assert_equal '42', O.new( 42 ).to_s
44
44
  end
45
45
 
46
- def test_inspect
47
- assert_equal "OBJECT(42)", @@t.new( 42 ).inspect
46
+ def test_marshal
47
+ assert_equal O.new( 42 ), Marshal.load( Marshal.dump( O.new( 42 ) ) )
48
48
  end
49
49
 
50
- def test_get_set
51
- i = @@t.new nil
52
- assert_nil i.get
53
- assert_equal 42, i.set( 42 )
54
- assert_equal 42, i.get
55
- assert_nil i.set
56
- assert_nil i.get
50
+ def test_at_assign
51
+ o = O.new 3
52
+ assert_equal 3, o[]
53
+ assert_equal 42, o[] = 42
54
+ assert_equal 42, o[]
57
55
  end
58
56
 
59
- def test_at_assign
60
- i = @@t.new nil
61
- assert_nil i.at
62
- assert_equal 42, i.assign( 42 )
63
- assert_equal 42, i.at
64
- assert_raise( ArgumentError ) { i.at 0 }
65
- assert_raise( ArgumentError ) { i.assign 0, nil }
66
- i = @@t.new nil
67
- assert_nil i[]
68
- assert_equal 42, i[] = 42
69
- assert_equal 42, i[]
70
- assert_raise( ArgumentError ) { i[ 0 ] }
71
- assert_raise( ArgumentError ) { i[ 0 ] = nil }
57
+ def test_equal
58
+ assert_not_equal O.new( 3 ), O.new( 4 )
59
+ assert_equal O.new( 3 ), O.new( 3 )
60
+ end
61
+
62
+ def test_negate
63
+ o = O.new 5
64
+ assert_equal O.new( -5 ), -o
72
65
  end
73
66
 
74
- def test_op
75
- i = @@t.new 1
76
- assert_equal 3, i.op( 2 ) { |x| set get + x }.get
67
+ def test_lazy
68
+ o = lazy { -O.new( 3 ) }
69
+ assert_not_equal O.new( -3 ), o
70
+ assert_equal 'OBJECT(<delayed>)', o.inspect
71
+ assert_equal O.new( -3 ), o.force
72
+ o = lazy { --O.new( 3 ) }
73
+ assert_equal 'OBJECT(<delayed>)', o.inspect
74
+ assert_equal O.new( 3 ), o.force
75
+ o = -lazy { -O.new( 3 ) }
76
+ assert_equal O.new( 3 ), o
77
+ o = lazy { -lazy { -O.new( 3 ) } }
78
+ assert_equal 'OBJECT(<delayed>)', o.inspect
79
+ assert_equal O.new( 3 ), o.force
80
+ o = eager { lazy { -O.new( 3 ) } }
81
+ assert_equal 'OBJECT(<delayed>)', o.inspect
82
+ o = lazy { eager { -lazy { -O.new( 3 ) } } }
83
+ assert_equal O.new( 3 ), o
77
84
  end
78
85
 
79
86
  end
data/test/tc_sequence.rb CHANGED
@@ -1,144 +1,127 @@
1
1
  require 'test/unit'
2
+ begin
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ end
2
6
  Kernel::require 'multiarray'
3
7
 
4
8
  class TC_Sequence < Test::Unit::TestCase
5
9
 
6
- def setup
7
- @@types = [ Hornetseye::OBJECT,
8
- Hornetseye::UBYTE,
9
- Hornetseye::BYTE,
10
- Hornetseye::USINT,
11
- Hornetseye::SINT,
12
- Hornetseye::UINT,
13
- Hornetseye::INT,
14
- Hornetseye::ULONG,
15
- Hornetseye::LONG ]
10
+ O = Hornetseye::OBJECT
11
+ U8 = Hornetseye::UBYTE
12
+ S8 = Hornetseye::BYTE
13
+ U16 = Hornetseye::USINT
14
+ S16 = Hornetseye::SINT
15
+ U32 = Hornetseye::UINT
16
+ S32 = Hornetseye::INT
17
+ U64 = Hornetseye::ULONG
18
+ S64 = Hornetseye::LONG
19
+ S = Hornetseye::Sequence
20
+
21
+ T = [ O, U8, S8, U16, S16, U32, S32 ]
22
+ INSPECT = {
23
+ O => 'OBJECT',
24
+ U8 => 'UBYTE', S8 => 'BYTE',
25
+ U16 => 'USINT', S16 => 'SINT',
26
+ U32 => 'UINT', S32 => 'INT'
27
+ }
28
+ SIGNED = {
29
+ O => true,
30
+ U8 => false, S8 => true,
31
+ U16 => false, S16 => true,
32
+ U32 => false, S32 => true
33
+ }
34
+
35
+ def lazy( &action )
36
+ Hornetseye::lazy &action
37
+ end
38
+
39
+ def eager( &action )
40
+ Hornetseye::eager &action
16
41
  end
17
42
 
18
- def teardown
19
- @@types = nil
43
+ def setup
20
44
  end
21
45
 
22
- def test_default
23
- for t in @@types
24
- assert_equal [ t.default ] * 3, Hornetseye::Sequence( t, 3 ).default.to_a
25
- end
46
+ def teardown
26
47
  end
27
48
 
28
- def test_sequence_new
29
- for t in @@types
30
- s = Hornetseye::Sequence.new t, 3
31
- s.set
32
- assert_equal [ t.default ] * 3, s.to_a
33
- end
49
+ def S( *args )
50
+ Hornetseye::Sequence *args
34
51
  end
35
52
 
36
- def test_sequence_to_s
37
- for t in @@types
38
- assert_equal "Sequence.#{t.to_s.downcase}(3)",
39
- Hornetseye::Sequence( t, 3 ).to_s
53
+ def test_default
54
+ T.each do |t|
55
+ s = S( t, 3 ).new
56
+ s[] = t.new[]
57
+ assert_equal [ t.new[] ] * 3, s.to_a
40
58
  end
41
59
  end
42
60
 
43
61
  def test_sequence_inspect
44
- for t in @@types
45
- assert_equal "Sequence.#{t.inspect.downcase}(3)",
46
- Hornetseye::Sequence( t, 3 ).inspect
47
- end
48
- end
49
-
50
- def test_bytesize
51
- for t in @@types
52
- assert_equal t.bytesize * 3, Hornetseye::Sequence( t, 3 ).bytesize
53
- end
54
- end
55
-
56
- def test_typecode
57
- for t in @@types
58
- assert_equal t, Hornetseye::Sequence( t, 3 ).typecode
62
+ T.each do |t|
63
+ assert_equal "Sequence.#{t.inspect.downcase}(3)", S( t, 3 ).inspect
59
64
  end
60
65
  end
61
66
 
62
- def test_empty
63
- for t in @@types
64
- assert Hornetseye::Sequence( t, 0 ).new.empty?
65
- assert !Hornetseye::Sequence( t, 3 ).new.empty?
66
- end
67
- end
68
-
69
- def test_shape
70
- for t in @@types
71
- assert_equal [ 3 ], Hornetseye::Sequence( t, 3 ).shape
67
+ def test_sequence_to_s
68
+ T.each do |t|
69
+ assert_equal "Sequence.#{t.to_s.downcase}(3)", S( t, 3 ).to_s
72
70
  end
73
71
  end
74
72
 
75
- def test_size
76
- for t in @@types
77
- assert_equal 3, Hornetseye::Sequence( t, 3 ).size
78
- end
73
+ def test_sequence_assign
74
+ assert_equal [ :a, :b, :c ], S[ :a, :b, :c ].to_a
79
75
  end
80
76
 
81
77
  def test_inspect
82
- for t in @@types
83
- s = Hornetseye::Sequence( t, 3 ).new
84
- s[] = [ 1, 2, 3 ]
85
- assert_equal "Sequence.#{t.inspect.downcase}(3):\n[ 1, 2, 3 ]", s.inspect
86
- end
78
+ assert_equal "Sequence.object(3):\n[ :a, :b, :c ]", S[ :a, :b, :c ].inspect
87
79
  end
88
80
 
89
81
  def test_to_s
90
- for t in @@types
91
- s = Hornetseye::Sequence( t, 3 ).new
92
- s[] = [ 1, 2, 3 ]
93
- assert_equal '123', s.to_s
94
- end
82
+ # !!!
95
83
  end
96
84
 
97
- def test_to_a
98
- for t in @@types
99
- s = Hornetseye::Sequence( t, 3 ).new
100
- s[] = [ 1, 2, 3 ]
101
- assert_equal [ 1, 2, 3 ], s.to_a
85
+ def test_at_assign
86
+ s = S.new O, 3
87
+ for i in 0 ... 3
88
+ assert_equal i + 1, s[ i ] = i + 1
102
89
  end
103
- end
104
-
105
- def test_get_set
106
- for t in @@types
107
- s1 = Hornetseye::Sequence( t, 3 ).new
108
- s1[] = 2
109
- assert_equal t.default, s1.set
110
- assert_equal [ t.default ] * 3, s1.get.to_a
111
- assert_equal [ t.default ] * 3, s1.to_a
112
- assert_equal 1, s1.set( 1 )
113
- assert_equal [ 1, 1, 1 ], s1.to_a
114
- assert_equal [ 2, 3 ], s1.set( [ 2, 3 ] )
115
- assert_equal [ 2, 3, t.default ], s1.to_a
116
- s2 = Hornetseye::Sequence( t, 3 ).new
117
- s2[] = [ 1, 2, 3 ]
118
- s1[] = s2
119
- assert_equal [ 1, 2, 3 ], s2.to_a
90
+ assert_equal [ 1, 2, 3 ], s[].to_a
91
+ for i in 0 ... 3
92
+ assert_equal i + 1, s[ i ]
120
93
  end
121
94
  end
122
95
 
123
- def test_at_assign
124
- for t in @@types
125
- s = Hornetseye::Sequence( t, 3 ).new
126
- s[] = 0
127
- assert_equal [ 0, 0, 0 ], [ s.at( 0 ), s.at( 1 ), s.at( 2 ) ]
128
- assert_equal [ 1, 2, 3 ], [ s.assign( 0, 1 ),
129
- s.assign( 1, 2 ),
130
- s.assign( 2, 3 ) ]
131
- assert_equal [ 1, 2, 3 ], [ s.at( 0 ), s.at( 1 ), s.at( 2 ) ]
132
- assert_raise( ArgumentError ) { s.at 0, 0 }
133
- assert_nothing_raised { s.at }
134
- s = Hornetseye::Sequence( t, 3 ).new
135
- s[] = 0
136
- assert_equal [ 0, 0, 0 ], [ s[ 0 ], s[ 1 ], s[ 2 ] ]
137
- assert_equal [ 1, 2, 3 ], [ s[ 0 ] = 1, s[ 1 ] = 2, s[ 2 ] = 3 ]
138
- assert_equal [ 1, 2, 3 ], [ s[ 0 ], s[ 1 ], s[ 2 ] ]
139
- assert_raise( ArgumentError ) { s[ 0, 0 ] }
140
- assert_nothing_raised { s[] }
141
- end
96
+ def test_equal
97
+ # !!!
98
+ end
99
+
100
+ def test_negate
101
+ s = S[ 1, 2, 3 ]
102
+ assert_equal [ -1, -2, -3 ], ( -s ).to_a
103
+ end
104
+
105
+ def test_lazy
106
+ s = S[ 1, 2, 3 ]
107
+ u = lazy { -s }
108
+ assert_equal 'Sequence.object(3):<delayed>', u.inspect
109
+ assert_equal [ -1, -2, -3 ], u.force.to_a
110
+ u = lazy { --s }
111
+ assert_equal 'Sequence.object(3):<delayed>', u.inspect
112
+ assert_equal [ 1, 2, 3 ], u.force.to_a
113
+ u = -lazy { -s }
114
+ assert_equal "Sequence.object(3):\n[ 1, 2, 3 ]", u.inspect
115
+ assert_equal [ 1, 2, 3 ], u.to_a
116
+ u = lazy { -lazy { -s } }
117
+ assert_equal 'Sequence.object(3):<delayed>', u.inspect
118
+ assert_equal [ 1, 2, 3 ], u.force.to_a
119
+ u = eager { lazy { -s } }
120
+ assert_equal 'Sequence.object(3):<delayed>', u.inspect
121
+ assert_equal [ -1, -2, -3 ], u.force.to_a
122
+ u = lazy { eager { -lazy { -s } } }
123
+ assert_equal "Sequence.object(3):\n[ 1, 2, 3 ]", u.inspect
124
+ assert_equal [ 1, 2, 3 ], u.to_a
142
125
  end
143
126
 
144
127
  end