multiarray 0.2.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -2
- data/lib/multiarray.rb +45 -20
- data/lib/multiarray/int.rb +1 -1
- data/lib/multiarray/int_.rb +87 -60
- data/lib/multiarray/lazy.rb +76 -0
- data/lib/multiarray/list.rb +33 -74
- data/lib/multiarray/malloc.rb +11 -0
- data/lib/multiarray/multiarray.rb +19 -1
- data/lib/multiarray/object.rb +36 -19
- data/lib/multiarray/pointer.rb +11 -0
- data/lib/multiarray/pointer_.rb +223 -0
- data/lib/multiarray/sequence.rb +14 -10
- data/lib/multiarray/sequence_.rb +40 -106
- data/lib/multiarray/type.rb +101 -129
- data/test/tc_int.rb +78 -76
- data/test/tc_multiarray.rb +97 -15
- data/test/tc_object.rb +52 -45
- data/test/tc_sequence.rb +89 -106
- data/test/ts_multiarray.rb +0 -1
- metadata +6 -8
- data/lib/multiarray/composite_type.rb +0 -67
- data/lib/multiarray/descriptortype.rb +0 -43
- data/lib/multiarray/memory.rb +0 -112
- data/lib/multiarray/sequence_operation.rb +0 -52
- data/lib/multiarray/storage.rb +0 -23
- data/lib/multiarray/type_operation.rb +0 -32
data/test/tc_multiarray.rb
CHANGED
@@ -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
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
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
|
-
|
7
|
-
@@t = Hornetseye::OBJECT
|
8
|
-
end
|
10
|
+
O = Hornetseye::OBJECT
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
+
def lazy( &action )
|
13
|
+
Hornetseye::lazy &action
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
15
|
-
|
16
|
+
def eager( &action )
|
17
|
+
Hornetseye::eager &action
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
19
|
-
assert_equal 'OBJECT', @@t.to_s
|
20
|
+
def setup
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
-
assert_equal @@t.to_s, @@t.inspect
|
23
|
+
def teardown
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
|
26
|
+
def test_object_default
|
27
|
+
assert_nil O.new[]
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
assert_equal
|
30
|
+
def test_object_inspect
|
31
|
+
assert_equal 'OBJECT', O.inspect
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
assert_equal
|
34
|
+
def test_object_to_s
|
35
|
+
assert_equal 'OBJECT', O.to_s
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
assert_equal
|
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',
|
43
|
+
assert_equal '42', O.new( 42 ).to_s
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
47
|
-
assert_equal
|
46
|
+
def test_marshal
|
47
|
+
assert_equal O.new( 42 ), Marshal.load( Marshal.dump( O.new( 42 ) ) )
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
assert_equal 42,
|
54
|
-
assert_equal 42,
|
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
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
75
|
-
|
76
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
19
|
-
@@types = nil
|
43
|
+
def setup
|
20
44
|
end
|
21
45
|
|
22
|
-
def
|
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
|
29
|
-
|
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
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
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
|
63
|
-
|
64
|
-
|
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
|
76
|
-
|
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
|
-
|
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
|
-
|
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
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
104
|
-
|
105
|
-
|
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
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|