type_array 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/type_array.rb ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ # Prefer compiled Rubinius bytecode in .rbx/
4
+ ENV["RBXOPT"] = "-Xrbc.db"
5
+
6
+ require "type_array_ext"
7
+
8
+ require 'type_array/version' unless defined? TypeArray::VERSION
9
+ require 'type_array/io'
10
+
11
+ [ArrayBuffer, TypeArray, DataView].each do |c|
12
+ c.extend TypeArray::IOReader
13
+ c.send(:include, TypeArray::IOWriter)
14
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'type_array'
5
+
6
+ class TypeArrayTestCase < Test::Unit::TestCase
7
+ undef_method :default_test if method_defined? :default_test
8
+
9
+ if ENV['STRESS_GC']
10
+ def setup
11
+ GC.stress = true
12
+ end
13
+
14
+ def teardown
15
+ GC.stress = false
16
+ end
17
+ end
18
+
19
+ private
20
+ def file_name
21
+ File.join(File.dirname(__FILE__), "typed.array")
22
+ end
23
+ end
@@ -0,0 +1,132 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestArrayBuffer < TypeArrayTestCase
6
+ def test_type
7
+ assert_instance_of Class, ArrayBuffer
8
+ end
9
+
10
+ def test_length_constructor
11
+ buf = ArrayBuffer.new(100)
12
+ assert_instance_of ArrayBuffer, buf
13
+
14
+ assert_raises TypeError do
15
+ ArrayBuffer.new(:symbol)
16
+ end
17
+
18
+ assert_raises RangeError do
19
+ ArrayBuffer.new(0)
20
+ end
21
+ end
22
+
23
+ def test_string_constructor
24
+ str = "01234567"
25
+ buf = ArrayBuffer.new(str)
26
+ assert_equal 8, buf.byte_length
27
+
28
+ assert_equal str, buf.to_s
29
+ end
30
+
31
+ def test_byte_length
32
+ buf = ArrayBuffer.new(100)
33
+ assert_equal 100, buf.byte_length
34
+ end
35
+
36
+ def test_slice
37
+ buf = ArrayBuffer.new(100)
38
+
39
+ assert_raises TypeError do
40
+ buf.slice(:invalid)
41
+ end
42
+
43
+ assert_raises TypeError do
44
+ buf.slice(50, :invalid)
45
+ end
46
+
47
+ assert_raises RangeError do
48
+ buf.slice(101)
49
+ end
50
+
51
+ copy = buf.slice(50)
52
+ assert buf.object_id != copy.object_id
53
+ assert_equal 50, copy.byte_length
54
+
55
+ copy = copy.slice(25)
56
+ assert_equal 25, copy.byte_length
57
+
58
+ copy = buf.slice(-30, -10)
59
+ assert_equal 20, copy.byte_length
60
+ end
61
+
62
+ def test_to_s
63
+ buf = ArrayBuffer.new(16)
64
+ str = buf.to_s
65
+ assert_equal "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", str
66
+ assert str.frozen?
67
+ end
68
+
69
+ def test_io
70
+ buf = ArrayBuffer.new(16)
71
+
72
+ f = File.open(file_name, File::CREAT | File::RDWR)
73
+
74
+ view = DataView.new(buf)
75
+ view.set_int8(0, 5)
76
+ view.set_int8(1, 12)
77
+ view.set_int16(2, 34)
78
+ view.set_float64(4, 8323.32)
79
+
80
+ view.write(f)
81
+
82
+ f.flush
83
+ assert_equal 16, File.size(file_name)
84
+
85
+ f.rewind
86
+
87
+ buf = ArrayBuffer.read(f, 16)
88
+
89
+ view = DataView.new(buf)
90
+ assert_equal 16, view.byte_length
91
+
92
+ assert_equal 5, view.get_int8(0)
93
+ assert_equal 12, view.get_int8(1)
94
+ assert_equal 34, view.get_int16(2)
95
+ assert_equal 8323.32, view.get_float64(4)
96
+
97
+ f.close
98
+ ensure
99
+ File.unlink(file_name) rescue nil
100
+ end
101
+
102
+ def test_socket_io
103
+ buf = ArrayBuffer.new(16)
104
+
105
+ server = TCPServer.new("127.0.0.1", 0)
106
+ port = server.addr[1]
107
+ client = TCPSocket.new("127.0.0.1", port)
108
+ s = server.accept
109
+
110
+ view = DataView.new(buf)
111
+ view.set_int8(0, 5)
112
+ view.set_int8(1, 12)
113
+ view.set_int16(2, 34)
114
+ view.set_float64(4, 8323.32)
115
+
116
+ view.write(client)
117
+
118
+ buf = ArrayBuffer.read(s, 16)
119
+
120
+ view = DataView.new(buf)
121
+ assert_equal 16, view.byte_length
122
+
123
+ assert_equal 5, view.get_int8(0)
124
+ assert_equal 12, view.get_int8(1)
125
+ assert_equal 34, view.get_int16(2)
126
+ assert_equal 8323.32, view.get_float64(4)
127
+
128
+ s.close
129
+ ensure
130
+ s.close rescue nil
131
+ end
132
+ end
@@ -0,0 +1,218 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestDataView < TypeArrayTestCase
6
+ def test_type
7
+ assert_instance_of Class, DataView
8
+ end
9
+
10
+ def test_buffer_constructor
11
+ buf = ArrayBuffer.new(100)
12
+
13
+ assert_raises TypeError do
14
+ DataView.new(:invalid)
15
+ end
16
+
17
+ assert_raises RangeError do
18
+ DataView.new(buf, 101)
19
+ end
20
+
21
+ assert_raises RangeError do
22
+ DataView.new(buf, 0, 101)
23
+ end
24
+
25
+ assert_raises RangeError do
26
+ DataView.new(buf, 4, 100)
27
+ end
28
+
29
+ view = DataView.new(buf)
30
+ assert_instance_of DataView, view
31
+ assert_equal 100, view.byte_length
32
+ assert_equal 0, view.byte_offset
33
+ assert_equal buf, view.buffer
34
+ end
35
+
36
+ def test_string_constructor
37
+ str = "01234567"
38
+ view = DataView.new(str)
39
+ assert_equal 8, view.byte_length
40
+ assert_equal str, view.to_s
41
+ end
42
+
43
+ def test_offset_constructor
44
+ buf = ArrayBuffer.new(100)
45
+
46
+ view = DataView.new(buf, 10)
47
+ assert_equal 90, view.byte_length
48
+ assert_equal 10, view.byte_offset
49
+ end
50
+
51
+ def test_byte_length_constructor
52
+ buf = ArrayBuffer.new(100)
53
+
54
+ view = DataView.new(buf, 0, 50)
55
+ assert_equal 50, view.byte_length
56
+ assert_equal 0, view.byte_offset
57
+
58
+ view = DataView.new(buf, 15, 50)
59
+ assert_equal 35, view.byte_length
60
+ assert_equal 15, view.byte_offset
61
+ end
62
+
63
+ def test_to_s
64
+ buf = ArrayBuffer.new(8)
65
+
66
+ view = DataView.new(buf)
67
+ str = view.to_s
68
+ assert_equal "\x00\x00\x00\x00\x00\x00\x00\x00", str
69
+ assert str.frozen?
70
+ end
71
+
72
+ def test_int8
73
+ buf = ArrayBuffer.new(100)
74
+
75
+ view = DataView.new(buf)
76
+ assert_equal 0, view.get_int8(1)
77
+ view.set_int8(1, 5)
78
+ assert_equal 5, view.get_int8(1)
79
+ end
80
+
81
+ def test_uint8
82
+ buf = ArrayBuffer.new(100)
83
+
84
+ view = DataView.new(buf)
85
+ assert_equal 0, view.get_uint8(1)
86
+ view.set_uint8(1, 5)
87
+ assert_equal 5, view.get_uint8(1)
88
+ end
89
+
90
+ def test_int16
91
+ buf = ArrayBuffer.new(100)
92
+
93
+ view = DataView.new(buf)
94
+ assert_equal 0, view.get_int16(1)
95
+ view.set_int16(1, 5000)
96
+ assert_equal 5000, view.get_int16(1)
97
+ end
98
+
99
+ def test_uint16
100
+ buf = ArrayBuffer.new(100)
101
+
102
+ view = DataView.new(buf)
103
+ assert_equal 0, view.get_uint16(1)
104
+ view.set_uint16(1, 19264)
105
+ assert_equal 19264, view.get_uint16(1)
106
+ end
107
+
108
+ def test_int32
109
+ buf = ArrayBuffer.new(100)
110
+
111
+ view = DataView.new(buf)
112
+ assert_equal 0, view.get_int32(1)
113
+ view.set_int32(1, 50000000)
114
+ assert_equal 50000000, view.get_int32(1)
115
+ end
116
+
117
+ def test_uint32
118
+ buf = ArrayBuffer.new(100)
119
+
120
+ view = DataView.new(buf)
121
+ assert_equal 0, view.get_uint32(1)
122
+ view.set_uint32(1, 80000000)
123
+ assert_equal 80000000, view.get_uint32(1)
124
+ end
125
+
126
+ def test_float32
127
+ buf = ArrayBuffer.new(100)
128
+
129
+ view = DataView.new(buf)
130
+ assert_equal 0, view.get_float32(1)
131
+ view.set_float32(1, 55.77)
132
+ assert_in_delta 55.77, view.get_float32(1), 0.01
133
+ end
134
+
135
+ def test_float64
136
+ buf = ArrayBuffer.new(100)
137
+
138
+ view = DataView.new(buf)
139
+ assert_equal 0, view.get_float64(1)
140
+ view.set_float64(1, 323233.77)
141
+ assert_equal 323233.77, view.get_float64(1)
142
+ end
143
+
144
+ def test_combo
145
+ buf = ArrayBuffer.new(100)
146
+
147
+ view = DataView.new(buf)
148
+ view.set_int8(0, 5)
149
+ view.set_int8(1, 12)
150
+ view.set_int16(2, 34)
151
+ view.set_float64(4, 8323.32)
152
+
153
+ assert_equal 5, view.get_int8(0)
154
+ assert_equal 12, view.get_int8(1)
155
+ assert_equal 34, view.get_int16(2)
156
+ assert_equal 8323.32, view.get_float64(4)
157
+ end
158
+
159
+ def test_std_io
160
+ buf = ArrayBuffer.new(16)
161
+
162
+ f = File.open(file_name, File::CREAT | File::RDWR)
163
+
164
+ view = DataView.new(buf)
165
+ view.set_int8(0, 5)
166
+ view.set_int8(1, 12)
167
+ view.set_int16(2, 34)
168
+ view.set_float64(4, 8323.32)
169
+
170
+ view.write(f)
171
+
172
+ f.flush
173
+ assert_equal 16, File.size(file_name)
174
+
175
+ f.rewind
176
+
177
+ view = DataView.read(f, 16)
178
+ assert_equal 16, view.byte_length
179
+
180
+ assert_equal 5, view.get_int8(0)
181
+ assert_equal 12, view.get_int8(1)
182
+ assert_equal 34, view.get_int16(2)
183
+ assert_equal 8323.32, view.get_float64(4)
184
+
185
+ f.close
186
+ ensure
187
+ File.unlink(file_name) rescue nil
188
+ end
189
+
190
+ def test_socket_io
191
+ buf = ArrayBuffer.new(16)
192
+
193
+ server = TCPServer.new("127.0.0.1", 0)
194
+ port = server.addr[1]
195
+ client = TCPSocket.new("127.0.0.1", port)
196
+ s = server.accept
197
+
198
+ view = DataView.new(buf)
199
+ view.set_int8(0, 5)
200
+ view.set_int8(1, 12)
201
+ view.set_int16(2, 34)
202
+ view.set_float64(4, 8323.32)
203
+
204
+ view.write(client)
205
+
206
+ view = DataView.read(s, 16)
207
+ assert_equal 16, view.byte_length
208
+
209
+ assert_equal 5, view.get_int8(0)
210
+ assert_equal 12, view.get_int8(1)
211
+ assert_equal 34, view.get_int16(2)
212
+ assert_equal 8323.32, view.get_float64(4)
213
+
214
+ s.close
215
+ ensure
216
+ s.close rescue nil
217
+ end
218
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestFloat32Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 4, Float32Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(1024)
12
+
13
+ assert_raises RangeError do
14
+ Float32Array.new(buf, 3)
15
+ end
16
+
17
+ ary = Float32Array.new(128)
18
+ assert_equal 128, ary.length
19
+ assert_equal 512, ary.byte_length
20
+ assert_equal 0, ary.byte_offset
21
+ end
22
+
23
+ def test_index
24
+ buf = ArrayBuffer.new(100)
25
+
26
+ ary = Float32Array.new(buf)
27
+ ary[0] = 0.775
28
+ ary[2] = 55.77
29
+ ary[3] = -55.77
30
+ assert_in_delta 0.775, ary[0], 0.01
31
+ assert_equal 0, ary[1]
32
+ assert_equal 0, ary[10]
33
+ assert_in_delta 55.77, ary[2], 0.01
34
+ assert_in_delta(-55.77, ary[3], 0.01)
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestFloat64Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 8, Float64Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(1024)
12
+
13
+ assert_raises RangeError do
14
+ Float64Array.new(buf, 3)
15
+ end
16
+
17
+ ary = Float64Array.new(128)
18
+ assert_equal 128, ary.length
19
+ assert_equal 1024, ary.byte_length
20
+ assert_equal 0, ary.byte_offset
21
+ end
22
+
23
+ def test_index
24
+ buf = ArrayBuffer.new(100)
25
+
26
+ ary = Float64Array.new(buf)
27
+ ary[0] = 77.643
28
+ assert_equal 77.643, ary[0]
29
+ ary[0] = 55.333
30
+ ary[3] = 323233.77
31
+ ary[4] = -323233.77
32
+ assert_equal 55.333, ary[0]
33
+ assert_equal 0, ary[1]
34
+ assert_equal 0, ary[10]
35
+ assert_equal 323233.77, ary[3]
36
+ assert_equal(-323233.77, ary[4])
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestInt16Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 2, Int16Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(8)
12
+
13
+ ary = Int16Array.new(buf, 2, 2);
14
+ assert_equal 2, ary.length
15
+
16
+ buf = ArrayBuffer.new(100)
17
+
18
+ ary = Int16Array.new(buf, 20, 4)
19
+ assert_equal 4, ary.length
20
+ assert_equal 8, ary.byte_length
21
+ assert_equal 20, ary.byte_offset
22
+ end
23
+
24
+ def test_index
25
+ buf = ArrayBuffer.new(100)
26
+
27
+ ary = Int16Array.new(buf)
28
+ ary[0] = 12
29
+ ary[2] = 5000
30
+ ary[12] = -5
31
+ assert_equal 12, ary[0]
32
+ assert_equal 0, ary[1]
33
+ assert_equal 0, ary[49]
34
+ assert_equal(-5, ary[12])
35
+ assert_equal 5000, ary[2]
36
+ end
37
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestInt32Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 4, Int32Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(8)
12
+
13
+ ary = Int32Array.new(buf)
14
+ assert_equal 2, ary.length
15
+
16
+ buf = ArrayBuffer.new(100)
17
+
18
+ ary = Int32Array.new(buf)
19
+ assert_equal 25, ary.length
20
+ assert_equal 100, ary.byte_length
21
+
22
+ ary = Int16Array.new(buf, 20, 4)
23
+ assert_equal 4, ary.length
24
+ assert_equal 8, ary.byte_length
25
+ assert_equal 20, ary.byte_offset
26
+ end
27
+
28
+ def test_index
29
+ buf = ArrayBuffer.new(100)
30
+
31
+ ary = Int32Array.new(buf)
32
+ ary[0] = 112
33
+ ary[2] = 50000000
34
+ ary[3] = 658
35
+ ary[5] = -900
36
+ assert_equal 112, ary[0]
37
+ assert_equal 658, ary[3]
38
+ assert_equal 0, ary[1]
39
+ assert_equal 0, ary[10]
40
+ assert_equal(-900, ary[5])
41
+ assert_equal 50000000, ary[2]
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestInt8Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 1, Int8Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(8)
12
+
13
+ ary = Int8Array.new(buf)
14
+ assert_equal 8, ary.length
15
+ assert_equal 8, ary.byte_length
16
+ end
17
+
18
+ def test_index
19
+ buf = ArrayBuffer.new(8)
20
+
21
+ ary = Int8Array.new(buf)
22
+ ary[0] = 3
23
+ ary[2] = -3
24
+ assert_equal 3, ary[0]
25
+ assert_equal 0, ary[1]
26
+ assert_equal 0, ary[7]
27
+ assert_equal(-3, ary[2])
28
+ end
29
+ end