type_array 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestTypeArray < TypeArrayTestCase
6
+ def test_type
7
+ assert_instance_of Class, TypeArray
8
+ end
9
+
10
+ def test_direct_constructor
11
+ assert_raises TypeError do
12
+ TypeArray.new(100)
13
+ end
14
+ end
15
+
16
+ def test_unsupported_constructor
17
+ assert_raises TypeError do
18
+ Int32Array.new(:invalid)
19
+ end
20
+ end
21
+
22
+ def test_length_constructor
23
+ ary = Int32Array.new(100)
24
+
25
+ assert_instance_of Int32Array, ary
26
+ assert_instance_of ArrayBuffer, ary.buffer
27
+ assert_equal 100, ary.length
28
+ assert_equal 400, ary.byte_length
29
+ assert_equal 0, ary.byte_offset
30
+ end
31
+
32
+ def test_string_constructor
33
+ str = "01234567"
34
+ ary = Int32Array.new(str)
35
+
36
+ assert_equal 8, ary.byte_length
37
+ assert_equal 2, ary.length
38
+ assert_equal str, ary.to_s
39
+ end
40
+
41
+ def test_array_buf_constructor
42
+ buf = ArrayBuffer.new(100)
43
+
44
+ ary = Int32Array.new(buf)
45
+ assert_equal 25, ary.length
46
+ assert_equal 100, ary.byte_length
47
+ assert_equal 0, ary.byte_offset
48
+ assert_equal buf, ary.buffer
49
+
50
+ assert_raises TypeError do
51
+ Int32Array.new(buf, :symbol)
52
+ end
53
+
54
+ assert_raises RangeError do
55
+ Int32Array.new(buf, 13)
56
+ end
57
+ end
58
+
59
+ def test_offset_length_constructor
60
+ buf = ArrayBuffer.new(100)
61
+
62
+ ary = Int32Array.new(buf, 20)
63
+ assert_equal 20, ary.length
64
+ assert_equal 80, ary.byte_length
65
+ assert_equal 20, ary.byte_offset
66
+
67
+ ary = Int32Array.new(buf, 0, 20)
68
+ assert_equal 20, ary.length
69
+ assert_equal 80, ary.byte_length
70
+ assert_equal 0, ary.byte_offset
71
+
72
+ ary = Int32Array.new(buf, 20, 20)
73
+ assert_equal 20, ary.length
74
+ assert_equal 80, ary.byte_length
75
+ assert_equal 20, ary.byte_offset
76
+
77
+ assert_raises RangeError do
78
+ Int32Array.new(buf, 84, 20)
79
+ end
80
+ end
81
+
82
+ def test_type_array_constructor
83
+ buf = ArrayBuffer.new(100)
84
+
85
+ ary = Int16Array.new(buf)
86
+ ary[0] = 100
87
+ ary[1] = 200
88
+ ary[2] = 300
89
+
90
+ ary2 = Int8Array.new(ary)
91
+ assert_equal 100, ary2[0]
92
+ assert_equal -56, ary2[1]
93
+ assert_equal 44, ary2[2]
94
+ end
95
+
96
+ def test_std_io
97
+ buf = ArrayBuffer.new(16)
98
+
99
+ f = File.open(file_name, File::CREAT | File::RDWR)
100
+
101
+ ary = Int32Array.new(buf)
102
+ ary[0] = 78
103
+ ary[1] = 43
104
+ ary[2] = 54
105
+ ary[3] = 12
106
+
107
+ ary.write(f)
108
+
109
+ f.flush
110
+ assert_equal 16, File.size(file_name)
111
+
112
+ f.rewind
113
+
114
+ ary = Int32Array.read(f, 16)
115
+ assert_equal 16, ary.byte_length
116
+
117
+ assert_equal 78, ary[0]
118
+ assert_equal 43, ary[1]
119
+ assert_equal 54, ary[2]
120
+ assert_equal 12, ary[3]
121
+
122
+ f.close
123
+ ensure
124
+ File.unlink(file_name) rescue nil
125
+ end
126
+
127
+ def test_socket_io
128
+ buf = ArrayBuffer.new(16)
129
+
130
+ server = TCPServer.new("127.0.0.1", 0)
131
+ port = server.addr[1]
132
+ client = TCPSocket.new("127.0.0.1", port)
133
+ s = server.accept
134
+
135
+ ary = Int32Array.new(buf)
136
+ ary[0] = 78
137
+ ary[1] = 43
138
+ ary[2] = 54
139
+ ary[3] = 12
140
+
141
+ ary.write(client)
142
+
143
+ ary = Int32Array.read(s, 16)
144
+ assert_equal 16, ary.byte_length
145
+
146
+ assert_equal 78, ary[0]
147
+ assert_equal 43, ary[1]
148
+ assert_equal 54, ary[2]
149
+ assert_equal 12, ary[3]
150
+
151
+ s.close
152
+ ensure
153
+ s.close rescue nil
154
+ end
155
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestUInt16Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 2, UInt16Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(20)
12
+
13
+ assert_raises RangeError do
14
+ UInt16Array.new(buf, 3)
15
+ end
16
+
17
+ ary = UInt16Array.new(buf, 4)
18
+ assert_equal 8, ary.length
19
+ assert_equal 16, ary.byte_length
20
+ end
21
+
22
+ def test_index
23
+ buf = ArrayBuffer.new(100)
24
+
25
+ ary = UInt16Array.new(buf)
26
+ ary[0] = 9
27
+ ary[2] = 19264
28
+ ary[3] = -8
29
+ assert_equal 9, ary[0]
30
+ assert_equal 0, ary[1]
31
+ assert_equal 0, ary[49]
32
+ assert_equal 65528, ary[3]
33
+ assert_equal 19264, ary[2]
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestUInt32Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 4, UInt32Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(20)
12
+
13
+ assert_raises RangeError do
14
+ UInt32Array.new(buf, 2)
15
+ end
16
+
17
+ ary = UInt32Array.new(buf, 4)
18
+ assert_equal 4, ary.length
19
+ assert_equal 16, ary.byte_length
20
+ end
21
+
22
+ def test_index
23
+ buf = ArrayBuffer.new(100)
24
+
25
+ ary = UInt32Array.new(buf)
26
+ ary[0] = 77
27
+ ary[3] = -77
28
+ ary[2] = 80000000
29
+ assert_equal 77, ary[0]
30
+ assert_equal 0, ary[1]
31
+ assert_equal 0, ary[24]
32
+ assert_equal(4294967219, ary[3])
33
+ assert_equal 80000000, ary[2]
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestUInt8Array < TypeArrayTestCase
6
+ def test_size
7
+ assert_equal 1, UInt8Array::BYTES_PER_ELEMENT
8
+ end
9
+
10
+ def test_constructor
11
+ buf = ArrayBuffer.new(8)
12
+
13
+ assert_raises RangeError do
14
+ UInt32Array.new(buf, 1)
15
+ end
16
+
17
+ ary = UInt8Array.new(buf, 2)
18
+ assert_equal 6, ary.length
19
+ assert_equal 6, ary.byte_length
20
+ end
21
+
22
+ def test_index
23
+ buf = ArrayBuffer.new(8)
24
+
25
+ ary = UInt8Array.new(buf)
26
+ ary[0] = 5
27
+ ary[3] = -5
28
+ assert_equal 5, ary[0]
29
+ assert_equal 0, ary[1]
30
+ assert_equal 0, ary[7]
31
+ assert_equal(-5, ary[3])
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../lib/type_array/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "type_array"
7
+ s.version = TypeArray::VERSION
8
+ s.summary = "Typed Arrays for Ruby"
9
+ s.description = "TypeArray - Ruby implementation of the ECMAScript spec ( http://wiki.ecmascript.org/doku.php?id=strawman:typed_arrays )"
10
+ s.authors = ["Lourens Naudé"]
11
+ s.email = ["lourens@methodmissing.com"]
12
+ s.homepage = "http://github.com/methodmissing/type_array"
13
+ s.date = Time.now.utc.strftime('%Y-%m-%d')
14
+ s.platform = Gem::Platform::RUBY
15
+ s.extensions = "ext/type_array/extconf.rb"
16
+ s.has_rdoc = true
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files test`.split("\n")
19
+ s.rdoc_options = ["--charset=UTF-8"]
20
+ s.require_paths = ["lib"]
21
+ s.add_development_dependency('rake-compiler', '~> 0.8.0')
22
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: type_array
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - "Lourens Naud\xC3\xA9"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-07-12 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake-compiler
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.8.0
24
+ type: :development
25
+ version_requirements: *id001
26
+ description: TypeArray - Ruby implementation of the ECMAScript spec ( http://wiki.ecmascript.org/doku.php?id=strawman:typed_arrays )
27
+ email:
28
+ - lourens@methodmissing.com
29
+ executables: []
30
+
31
+ extensions:
32
+ - ext/type_array/extconf.rb
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - .travis.yml
38
+ - CHANGELOG.rdoc
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - README.rdoc
42
+ - Rakefile
43
+ - ext/type_array/array_buffer.c
44
+ - ext/type_array/array_buffer.h
45
+ - ext/type_array/data_view.c
46
+ - ext/type_array/data_view.h
47
+ - ext/type_array/extconf.rb
48
+ - ext/type_array/jruby.h
49
+ - ext/type_array/prelude.h
50
+ - ext/type_array/rubinius.h
51
+ - ext/type_array/ruby18.h
52
+ - ext/type_array/ruby19.h
53
+ - ext/type_array/type_array.c
54
+ - ext/type_array/type_array.h
55
+ - ext/type_array/type_array_ext.c
56
+ - ext/type_array/type_array_ext.h
57
+ - lib/type_array.rb
58
+ - lib/type_array/io.rb
59
+ - lib/type_array/version.rb
60
+ - test/helper.rb
61
+ - test/test_array_buffer.rb
62
+ - test/test_data_view.rb
63
+ - test/test_float_32_array.rb
64
+ - test/test_float_64_array.rb
65
+ - test/test_int_16_array.rb
66
+ - test/test_int_32_array.rb
67
+ - test/test_int_8_array.rb
68
+ - test/test_type_array.rb
69
+ - test/test_uint_16_array.rb
70
+ - test/test_uint_32_array.rb
71
+ - test/test_uint_8_array.rb
72
+ - type_array.gemspec
73
+ homepage: http://github.com/methodmissing/type_array
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.9
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Typed Arrays for Ruby
100
+ test_files:
101
+ - test/helper.rb
102
+ - test/test_array_buffer.rb
103
+ - test/test_data_view.rb
104
+ - test/test_float_32_array.rb
105
+ - test/test_float_64_array.rb
106
+ - test/test_int_16_array.rb
107
+ - test/test_int_32_array.rb
108
+ - test/test_int_8_array.rb
109
+ - test/test_type_array.rb
110
+ - test/test_uint_16_array.rb
111
+ - test/test_uint_32_array.rb
112
+ - test/test_uint_8_array.rb