ffi 1.9.5 → 1.9.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ffi might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Rakefile +13 -6
- data/ext/ffi_c/extconf.rb +2 -2
- data/lib/ffi.rb +2 -0
- data/lib/ffi/version.rb +1 -1
- data/spec/ffi/async_callback_spec.rb +4 -5
- data/spec/ffi/bool_spec.rb +9 -8
- data/spec/ffi/buffer_spec.rb +64 -37
- data/spec/ffi/callback_spec.rb +195 -116
- data/spec/ffi/custom_param_type.rb +1 -1
- data/spec/ffi/custom_type_spec.rb +5 -6
- data/spec/ffi/dup_spec.rb +6 -8
- data/spec/ffi/enum_spec.rb +135 -129
- data/spec/ffi/errno_spec.rb +2 -2
- data/spec/ffi/ffi_spec.rb +4 -6
- data/spec/ffi/fixtures/EnumTest.o +0 -0
- data/spec/ffi/function_spec.rb +22 -11
- data/spec/ffi/io_spec.rb +0 -1
- data/spec/ffi/library_spec.rb +71 -36
- data/spec/ffi/long_double.rb +3 -4
- data/spec/ffi/managed_struct_spec.rb +14 -4
- data/spec/ffi/memorypointer_spec.rb +7 -1
- data/spec/ffi/number_spec.rb +43 -34
- data/spec/ffi/platform_spec.rb +76 -59
- data/spec/ffi/pointer_spec.rb +35 -31
- data/spec/ffi/rbx/attach_function_spec.rb +3 -4
- data/spec/ffi/rbx/memory_pointer_spec.rb +24 -22
- data/spec/ffi/rbx/spec_helper.rb +0 -1
- data/spec/ffi/rbx/struct_spec.rb +1 -2
- data/spec/ffi/spec_helper.rb +5 -2
- data/spec/ffi/string_spec.rb +22 -14
- data/spec/ffi/strptr_spec.rb +6 -7
- data/spec/ffi/struct_by_ref_spec.rb +4 -5
- data/spec/ffi/struct_callback_spec.rb +6 -7
- data/spec/ffi/struct_initialize_spec.rb +2 -3
- data/spec/ffi/struct_packed_spec.rb +12 -14
- data/spec/ffi/struct_spec.rb +203 -129
- data/spec/ffi/typedef_spec.rb +11 -10
- data/spec/ffi/union_spec.rb +8 -7
- data/spec/ffi/variadic_spec.rb +13 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbe4e813add8c92cda54f9eff70d9d4c5e9c6b2d
|
4
|
+
data.tar.gz: 8b44830705842817d995adc6bf071745048420bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85e073b1dca3bcc90b61e7efce2b75fad8aa39d76f6969faea08e74649b20cf139fdbeb8e75cffea11162cbf36e05fc94f59335773c9a4a26a03674d7fcaee5f
|
7
|
+
data.tar.gz: 81b40510ce47dc5b291cf694a058cda4bbea674be2b46593fc9db0fc090f9d92ffc8eb4556caed73fba52df0e9f68835429946ef58dd1e80f6d82c75f96c1589
|
data/Rakefile
CHANGED
@@ -150,11 +150,6 @@ task 'spec:specdoc' => TEST_DEPS
|
|
150
150
|
|
151
151
|
task :default => :specs
|
152
152
|
|
153
|
-
task 'gem:win32' do
|
154
|
-
sh("rake cross native gem RUBY_CC_VERSION='1.8.7:1.9.3:2.0.0:2.1.3'") || raise("win32 build failed!")
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
153
|
namespace 'java' do
|
159
154
|
|
160
155
|
java_gem_spec = Gem::Specification.new do |s|
|
@@ -190,11 +185,23 @@ if USE_RAKE_COMPILER
|
|
190
185
|
ext.cross_platform = %w[i386-mingw32 x64-mingw32] # forces the Windows platform instead of the default one
|
191
186
|
end
|
192
187
|
|
193
|
-
|
188
|
+
task 'gem:win32' => ['gem:win32-x64', 'gem:win32-i386']
|
189
|
+
|
190
|
+
task 'gem:win32-x86' do
|
191
|
+
sh("rake cross native:i386-mingw32 gem RUBY_CC_VERSION='1.8.7:1.9.3:2.0.0:2.1.3'") || raise("win32-i386 build failed!")
|
192
|
+
end
|
193
|
+
|
194
|
+
task 'gem:win32-x64' do
|
195
|
+
sh("rake cross native:x64-mingw32 gem RUBY_CC_VERSION='2.0.0:2.1.3'") || raise("win32-x64 build failed!")
|
196
|
+
end
|
197
|
+
|
198
|
+
(ENV['RUBY_CC_VERSION'] || '1.8.7:1.9.3:2.0.0:2.1.3' ).to_s.split(':').each do |ruby_version|
|
194
199
|
task "copy:ffi_c:i386-mingw32:#{ruby_version}" do |t|
|
195
200
|
sh "i686-w64-mingw32-strip -S #{BUILD_DIR}/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so"
|
196
201
|
end
|
202
|
+
end
|
197
203
|
|
204
|
+
(ENV['RUBY_CC_VERSION'] || '2.0.0:2.1.3' ).to_s.split(':').each do |ruby_version|
|
198
205
|
task "copy:ffi_c:x64-mingw32:#{ruby_version}" do |t|
|
199
206
|
sh "x86_64-w64-mingw32-strip -S #{BUILD_DIR}/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so"
|
200
207
|
end
|
data/ext/ffi_c/extconf.rb
CHANGED
@@ -9,8 +9,8 @@ if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx'
|
|
9
9
|
$warnflags = ''
|
10
10
|
$CFLAGS.gsub!(/[\s+]-ansi/, '')
|
11
11
|
$CFLAGS.gsub!(/[\s+]-std=[^\s]+/, '')
|
12
|
-
# solaris needs -c99 for <stdbool.h>
|
13
|
-
$CFLAGS << " -std=c99" if RbConfig::CONFIG['host_os'] =~ /solaris/
|
12
|
+
# solaris 10 needs -c99 for <stdbool.h>
|
13
|
+
$CFLAGS << " -std=c99" if RbConfig::CONFIG['host_os'] =~ /solaris(!?2\.11)/
|
14
14
|
|
15
15
|
if ENV['RUBY_CC_VERSION'].nil? && (pkg_config("libffi") ||
|
16
16
|
have_header("ffi.h") ||
|
data/lib/ffi.rb
CHANGED
data/lib/ffi/version.rb
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
#
|
5
5
|
|
6
6
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
7
|
-
require 'ffi'
|
8
7
|
|
9
8
|
describe "async callback" do
|
10
9
|
module LibTest
|
@@ -21,8 +20,8 @@ describe "async callback" do
|
|
21
20
|
called = false
|
22
21
|
cb = Proc.new {|i| v = i; called = true }
|
23
22
|
LibTest.testAsyncCallback(cb, 0x7fffffff)
|
24
|
-
called.
|
25
|
-
v.
|
23
|
+
expect(called).to be true
|
24
|
+
expect(v).to eq(0x7fffffff)
|
26
25
|
end
|
27
26
|
|
28
27
|
it "called a second time" do
|
@@ -30,7 +29,7 @@ describe "async callback" do
|
|
30
29
|
called = false
|
31
30
|
cb = Proc.new {|i| v = i; called = true }
|
32
31
|
LibTest.testAsyncCallback(cb, 0x7fffffff)
|
33
|
-
called.
|
34
|
-
v.
|
32
|
+
expect(called).to be true
|
33
|
+
expect(v).to eq(0x7fffffff)
|
35
34
|
end
|
36
35
|
end
|
data/spec/ffi/bool_spec.rb
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
#
|
5
5
|
|
6
6
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
7
|
-
require 'ffi'
|
8
7
|
|
9
8
|
describe "Function with primitive boolean arguments and return values" do
|
10
9
|
module LibTest
|
@@ -15,17 +14,19 @@ describe "Function with primitive boolean arguments and return values" do
|
|
15
14
|
attach_function :bool_return_val, [ :bool ], :bool
|
16
15
|
attach_function :bool_reverse_val, [ :bool ], :bool
|
17
16
|
end
|
17
|
+
|
18
18
|
it "bools" do
|
19
|
-
LibTest.bool_return_true.
|
20
|
-
LibTest.bool_return_false.
|
19
|
+
expect(LibTest.bool_return_true).to be true
|
20
|
+
expect(LibTest.bool_return_false).to be false
|
21
21
|
|
22
|
-
LibTest.bool_return_val(true).
|
23
|
-
LibTest.bool_return_val(false).
|
22
|
+
expect(LibTest.bool_return_val(true)).to be true
|
23
|
+
expect(LibTest.bool_return_val(false)).to be false
|
24
24
|
|
25
|
-
LibTest.bool_reverse_val(true).
|
26
|
-
LibTest.bool_reverse_val(false).
|
25
|
+
expect(LibTest.bool_reverse_val(true)).to be false
|
26
|
+
expect(LibTest.bool_reverse_val(false)).to be true
|
27
27
|
end
|
28
|
+
|
28
29
|
it "raise error on invalid types" do
|
29
|
-
|
30
|
+
expect { LibTest.bool_return_val(nil) }.to raise_error(::TypeError)
|
30
31
|
end
|
31
32
|
end
|
data/spec/ffi/buffer_spec.rb
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
#
|
5
5
|
|
6
6
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
7
|
-
require 'ffi'
|
8
7
|
|
9
8
|
describe "Buffer#total" do
|
10
9
|
[1,2,3].each do |i|
|
@@ -12,14 +11,17 @@ describe "Buffer#total" do
|
|
12
11
|
:uint => 4, :long => FFI::Type::LONG.size, :ulong => FFI::Type::ULONG.size,
|
13
12
|
:long_long => 8, :ulong_long => 8, :float => 4, :double => 8
|
14
13
|
}.each_pair do |t, s|
|
14
|
+
|
15
15
|
it "Buffer.alloc_in(#{t}, #{i}).total == #{i * s}" do
|
16
|
-
FFI::Buffer.alloc_in(t, i).total.
|
16
|
+
expect(FFI::Buffer.alloc_in(t, i).total).to eq(i * s)
|
17
17
|
end
|
18
|
+
|
18
19
|
it "Buffer.alloc_out(#{t}, #{i}).total == #{i * s}" do
|
19
|
-
FFI::Buffer.alloc_out(t, i).total.
|
20
|
+
expect(FFI::Buffer.alloc_out(t, i).total).to eq(i * s)
|
20
21
|
end
|
22
|
+
|
21
23
|
it "Buffer.alloc_inout(#{t}, #{i}).total == #{i * s}" do
|
22
|
-
FFI::Buffer.alloc_inout(t, i).total.
|
24
|
+
expect(FFI::Buffer.alloc_inout(t, i).total).to eq(i * s)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -30,214 +32,239 @@ describe "Buffer#put_char" do
|
|
30
32
|
(0..127).each do |i|
|
31
33
|
(0..bufsize-1).each do |offset|
|
32
34
|
it "put_char(#{offset}, #{i}).get_char(#{offset}) == #{i}" do
|
33
|
-
FFI::Buffer.alloc_in(bufsize).put_char(offset, i).get_char(offset).
|
35
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_char(offset, i).get_char(offset)).to eq(i)
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
37
39
|
end
|
40
|
+
|
38
41
|
describe "Buffer#put_uchar" do
|
39
42
|
bufsize = 4
|
40
43
|
(0..255).each do |i|
|
41
44
|
(0..bufsize-1).each do |offset|
|
42
45
|
it "Buffer.put_uchar(#{offset}, #{i}).get_uchar(#{offset}) == #{i}" do
|
43
|
-
FFI::Buffer.alloc_in(bufsize).put_uchar(offset, i).get_uchar(offset).
|
46
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_uchar(offset, i).get_uchar(offset)).to eq(i)
|
44
47
|
end
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
51
|
+
|
48
52
|
describe "Buffer#put_short" do
|
49
53
|
bufsize = 4
|
50
54
|
[0, 1, 128, 32767].each do |i|
|
51
55
|
(0..bufsize-2).each do |offset|
|
52
56
|
it "put_short(#{offset}, #{i}).get_short(#{offset}) == #{i}" do
|
53
|
-
FFI::Buffer.alloc_in(bufsize).put_short(offset, i).get_short(offset).
|
57
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_short(offset, i).get_short(offset)).to eq(i)
|
54
58
|
end
|
55
59
|
end
|
56
60
|
end
|
57
61
|
end
|
62
|
+
|
58
63
|
describe "Buffer#put_ushort" do
|
59
64
|
bufsize = 4
|
60
65
|
[ 0, 1, 128, 32767, 65535, 0xfee1, 0xdead, 0xbeef, 0xcafe ].each do |i|
|
61
66
|
(0..bufsize-2).each do |offset|
|
62
67
|
it "put_ushort(#{offset}, #{i}).get_ushort(#{offset}) == #{i}" do
|
63
|
-
FFI::Buffer.alloc_in(bufsize).put_ushort(offset, i).get_ushort(offset).
|
68
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_ushort(offset, i).get_ushort(offset)).to eq(i)
|
64
69
|
end
|
65
70
|
end
|
66
71
|
end
|
67
72
|
end
|
73
|
+
|
68
74
|
describe "Buffer#put_int" do
|
69
75
|
bufsize = 8
|
70
76
|
[0, 1, 128, 32767, 0x7ffffff ].each do |i|
|
71
77
|
(0..bufsize-4).each do |offset|
|
72
78
|
it "put_int(#{offset}, #{i}).get_int(#{offset}) == #{i}" do
|
73
|
-
FFI::Buffer.alloc_in(bufsize).put_int(offset, i).get_int(offset).
|
79
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_int(offset, i).get_int(offset)).to eq(i)
|
74
80
|
end
|
75
81
|
end
|
76
82
|
end
|
77
83
|
end
|
84
|
+
|
78
85
|
describe "Buffer#put_uint" do
|
79
86
|
bufsize = 8
|
80
87
|
[ 0, 1, 128, 32767, 65535, 0xfee1dead, 0xcafebabe, 0xffffffff ].each do |i|
|
81
88
|
(0..bufsize-4).each do |offset|
|
82
89
|
it "put_uint(#{offset}, #{i}).get_uint(#{offset}) == #{i}" do
|
83
|
-
FFI::Buffer.alloc_in(bufsize).put_uint(offset, i).get_uint(offset).
|
90
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_uint(offset, i).get_uint(offset)).to eq(i)
|
84
91
|
end
|
85
92
|
end
|
86
93
|
end
|
87
94
|
end
|
95
|
+
|
88
96
|
describe "Buffer#put_long" do
|
89
97
|
bufsize = 16
|
90
98
|
[0, 1, 128, 32767, 0x7ffffff ].each do |i|
|
91
99
|
(0..bufsize-FFI::Type::LONG.size).each do |offset|
|
92
100
|
it "put_long(#{offset}, #{i}).get_long(#{offset}) == #{i}" do
|
93
|
-
FFI::Buffer.alloc_in(bufsize).put_long(offset, i).get_long(offset).
|
101
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_long(offset, i).get_long(offset)).to eq(i)
|
94
102
|
end
|
95
103
|
end
|
96
104
|
end
|
97
105
|
end
|
106
|
+
|
98
107
|
describe "Buffer#put_ulong" do
|
99
108
|
bufsize = 16
|
100
109
|
[ 0, 1, 128, 32767, 65535, 0xfee1dead, 0xcafebabe, 0xffffffff ].each do |i|
|
101
110
|
(0..bufsize-FFI::Type::LONG.size).each do |offset|
|
102
111
|
it "put_ulong(#{offset}, #{i}).get_ulong(#{offset}) == #{i}" do
|
103
|
-
FFI::Buffer.alloc_in(bufsize).put_ulong(offset, i).get_ulong(offset).
|
112
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_ulong(offset, i).get_ulong(offset)).to eq(i)
|
104
113
|
end
|
105
114
|
end
|
106
115
|
end
|
107
116
|
end
|
117
|
+
|
108
118
|
describe "Buffer#put_long_long" do
|
109
119
|
bufsize = 16
|
110
120
|
[0, 1, 128, 32767, 0x7ffffffffffffff ].each do |i|
|
111
121
|
(0..bufsize-8).each do |offset|
|
112
122
|
it "put_long_long(#{offset}, #{i}).get_long_long(#{offset}) == #{i}" do
|
113
|
-
FFI::Buffer.alloc_in(bufsize).put_long_long(offset, i).get_long_long(offset).
|
123
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_long_long(offset, i).get_long_long(offset)).to eq(i)
|
114
124
|
end
|
115
125
|
end
|
116
126
|
end
|
117
127
|
end
|
128
|
+
|
118
129
|
describe "Buffer#put_ulong_long" do
|
119
130
|
bufsize = 16
|
120
131
|
[ 0, 1, 128, 32767, 65535, 0xdeadcafebabe, 0x7fffffffffffffff ].each do |i|
|
121
132
|
(0..bufsize-8).each do |offset|
|
122
133
|
it "put_ulong_long(#{offset}, #{i}).get_ulong_long(#{offset}) == #{i}" do
|
123
|
-
FFI::Buffer.alloc_in(bufsize).put_ulong_long(offset, i).get_ulong_long(offset).
|
134
|
+
expect(FFI::Buffer.alloc_in(bufsize).put_ulong_long(offset, i).get_ulong_long(offset)).to eq(i)
|
124
135
|
end
|
125
136
|
end
|
126
137
|
end
|
127
138
|
end
|
139
|
+
|
128
140
|
describe "Reading/Writing binary strings" do
|
129
141
|
it "Buffer#put_bytes" do
|
130
142
|
str = "hello\0world"
|
131
143
|
buf = FFI::Buffer.new 1024
|
132
144
|
buf.put_bytes(0, str);
|
133
145
|
s2 = buf.get_bytes(0, 11);
|
134
|
-
s2.
|
146
|
+
expect(s2).to eq(str)
|
135
147
|
end
|
148
|
+
|
136
149
|
it "Buffer#put_bytes with index and length" do
|
137
150
|
str = "hello\0world"
|
138
151
|
buf = FFI::Buffer.new 1024
|
139
152
|
buf.put_bytes(0, str, 5, 6);
|
140
153
|
s2 = buf.get_bytes(0, 6);
|
141
|
-
s2.
|
154
|
+
expect(s2).to eq(str[5..-1])
|
142
155
|
end
|
156
|
+
|
143
157
|
it "Buffer#put_bytes with only index" do
|
144
158
|
str = "hello\0world"
|
145
159
|
buf = FFI::Buffer.new 1024
|
146
160
|
buf.put_bytes(0, str, 5);
|
147
161
|
s2 = buf.get_bytes(0, 6);
|
148
|
-
s2.
|
162
|
+
expect(s2).to eq(str[5..-1])
|
149
163
|
end
|
164
|
+
|
150
165
|
it "Buffer#put_bytes with index > str.length" do
|
151
166
|
str = "hello\0world"
|
152
167
|
buf = FFI::Buffer.new 1024
|
153
|
-
|
168
|
+
expect { buf.put_bytes(0, str, 12); }.to raise_error
|
154
169
|
end
|
170
|
+
|
155
171
|
it "Buffer#put_bytes with length > str.length" do
|
156
172
|
str = "hello\0world"
|
157
173
|
buf = FFI::Buffer.new 1024
|
158
|
-
|
174
|
+
expect { buf.put_bytes(0, str, 0, 12); }.to raise_error
|
159
175
|
end
|
160
|
-
|
176
|
+
|
177
|
+
it "Buffer#put_bytes with negative index" do
|
161
178
|
str = "hello\0world"
|
162
179
|
buf = FFI::Buffer.new 1024
|
163
|
-
|
164
|
-
|
180
|
+
expect { buf.put_bytes(0, str, -1, 12); }.to raise_error
|
181
|
+
end
|
165
182
|
|
166
183
|
it "Buffer#write_bytes" do
|
167
184
|
str = "hello\0world"
|
168
185
|
buf = FFI::Buffer.new 1024
|
169
186
|
buf.write_bytes(str)
|
170
187
|
s2 = buf.get_bytes(0, 11)
|
171
|
-
s2.
|
188
|
+
expect(s2).to eq(str)
|
172
189
|
end
|
190
|
+
|
173
191
|
it "Buffer#write_bytes with index and length" do
|
174
192
|
str = "hello\0world"
|
175
193
|
buf = FFI::Buffer.new 1024
|
176
194
|
buf.write_bytes(str, 5, 6)
|
177
195
|
s2 = buf.get_bytes(0, 6)
|
178
|
-
s2.
|
196
|
+
expect(s2).to eq(str[5..-1])
|
179
197
|
end
|
198
|
+
|
180
199
|
it "Buffer#write_bytes with only index" do
|
181
200
|
str = "hello\0world"
|
182
201
|
buf = FFI::Buffer.new 1024
|
183
202
|
buf.write_bytes(str, 5)
|
184
203
|
s2 = buf.get_bytes(0, 6)
|
185
|
-
s2.
|
204
|
+
expect(s2).to eq(str[5..-1])
|
186
205
|
end
|
206
|
+
|
187
207
|
it "Buffer#write_bytes with index > str.length" do
|
188
208
|
str = "hello\0world"
|
189
209
|
buf = FFI::Buffer.new 1024
|
190
|
-
|
210
|
+
expect { buf.write_bytes(str, 12) }.to raise_error
|
191
211
|
end
|
212
|
+
|
192
213
|
it "Buffer#put_bytes with length > str.length" do
|
193
214
|
str = "hello\0world"
|
194
215
|
buf = FFI::Buffer.new 1024
|
195
|
-
|
216
|
+
expect { buf.put_bytes(0, str, 0, 12) }.to raise_error
|
196
217
|
end
|
197
|
-
|
218
|
+
|
219
|
+
it "Buffer#write_bytes with negative index" do
|
198
220
|
str = "hello\0world"
|
199
221
|
buf = FFI::Buffer.new 1024
|
200
|
-
|
222
|
+
expect { buf.write_bytes(str, -1, 12) }.to raise_error
|
201
223
|
end
|
202
224
|
end
|
225
|
+
|
203
226
|
describe "Reading/Writing ascii strings" do
|
204
227
|
it "Buffer#put_string with string containing zero byte" do
|
205
228
|
str = "hello\0world"
|
206
229
|
buf = FFI::Buffer.new 1024
|
207
230
|
buf.put_string(0, str);
|
208
231
|
s2 = buf.get_bytes(0, 11);
|
209
|
-
s2.
|
232
|
+
expect(s2).to eq(str)
|
210
233
|
end
|
234
|
+
|
211
235
|
it "Buffer#get_string with string containing zero byte" do
|
212
236
|
str = "hello\0world"
|
213
237
|
buf = FFI::Buffer.new 1024
|
214
238
|
buf.put_bytes(0, str);
|
215
239
|
s2 = buf.get_string(0, 11);
|
216
|
-
s2.
|
240
|
+
expect(s2).to eq("hello")
|
217
241
|
end
|
242
|
+
|
218
243
|
it "Buffer#put_string without length should NUL terminate" do
|
219
244
|
str = "hello"
|
220
245
|
buf = FFI::Buffer.new 1024
|
221
246
|
buf.put_string(0, str);
|
222
247
|
s2 = buf.get_bytes(0, 6);
|
223
|
-
s2.
|
248
|
+
expect(s2).to eq("hello\0")
|
224
249
|
end
|
225
250
|
end
|
251
|
+
|
226
252
|
describe "Buffer#put_pointer" do
|
227
253
|
it "put_pointer(0, p).get_pointer(0) == p" do
|
228
254
|
p = FFI::MemoryPointer.new :ulong_long
|
229
255
|
p.put_uint(0, 0xdeadbeef)
|
230
256
|
buf = FFI::Buffer.alloc_inout 8
|
231
257
|
p2 = buf.put_pointer(0, p).get_pointer(0)
|
232
|
-
p2.
|
233
|
-
p2.
|
234
|
-
p2.get_uint(0).
|
258
|
+
expect(p2).not_to be_nil
|
259
|
+
expect(p2).to eq(p)
|
260
|
+
expect(p2.get_uint(0)).to eq(0xdeadbeef)
|
235
261
|
end
|
236
262
|
end
|
263
|
+
|
237
264
|
describe "Buffer#size" do
|
238
265
|
it "should return size" do
|
239
266
|
buf = FFI::Buffer.new 14
|
240
|
-
buf.size.
|
267
|
+
expect(buf.size).to eq(14)
|
241
268
|
end
|
242
269
|
end
|
243
270
|
|
@@ -247,6 +274,6 @@ describe "Buffer#initialize" do
|
|
247
274
|
FFI::Buffer.new(:pointer) do |ptr|
|
248
275
|
block_executed = true
|
249
276
|
end
|
250
|
-
block_executed.
|
277
|
+
expect(block_executed).to be true
|
251
278
|
end
|
252
279
|
end
|
data/spec/ffi/callback_spec.rb
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
#
|
5
5
|
|
6
6
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
7
|
-
require 'ffi'
|
8
7
|
|
9
8
|
describe "Callback" do
|
10
9
|
# module LibC
|
@@ -85,161 +84,197 @@ describe "Callback" do
|
|
85
84
|
attach_function :testOptionalCallbackCrV, :testOptionalClosureBrV, [ :cbCrV, :char ], :void
|
86
85
|
|
87
86
|
end
|
87
|
+
|
88
88
|
it "returning :char (0)" do
|
89
|
-
LibTest.testCallbackVrS8 { 0 }.
|
89
|
+
expect(LibTest.testCallbackVrS8 { 0 }).to eq(0)
|
90
90
|
end
|
91
|
+
|
91
92
|
it "returning :char (127)" do
|
92
|
-
LibTest.testCallbackVrS8 { 127 }.
|
93
|
+
expect(LibTest.testCallbackVrS8 { 127 }).to eq(127)
|
93
94
|
end
|
95
|
+
|
94
96
|
it "returning :char (-128)" do
|
95
|
-
LibTest.testCallbackVrS8 { -128 }.
|
97
|
+
expect(LibTest.testCallbackVrS8 { -128 }).to eq(-128)
|
96
98
|
end
|
97
99
|
# test wrap around
|
98
100
|
it "returning :char (128)" do
|
99
|
-
LibTest.testCallbackVrS8 { 128 }.
|
101
|
+
expect(LibTest.testCallbackVrS8 { 128 }).to eq(-128)
|
100
102
|
end
|
103
|
+
|
101
104
|
it "returning :char (255)" do
|
102
|
-
LibTest.testCallbackVrS8 { 0xff }.
|
105
|
+
expect(LibTest.testCallbackVrS8 { 0xff }).to eq(-1)
|
103
106
|
end
|
107
|
+
|
104
108
|
it "returning :uchar (0)" do
|
105
|
-
LibTest.testCallbackVrU8 { 0 }.
|
109
|
+
expect(LibTest.testCallbackVrU8 { 0 }).to eq(0)
|
106
110
|
end
|
111
|
+
|
107
112
|
it "returning :uchar (0xff)" do
|
108
|
-
LibTest.testCallbackVrU8 { 0xff }.
|
113
|
+
expect(LibTest.testCallbackVrU8 { 0xff }).to eq(0xff)
|
109
114
|
end
|
115
|
+
|
110
116
|
it "returning :uchar (-1)" do
|
111
|
-
LibTest.testCallbackVrU8 { -1 }.
|
117
|
+
expect(LibTest.testCallbackVrU8 { -1 }).to eq(0xff)
|
112
118
|
end
|
119
|
+
|
113
120
|
it "returning :uchar (128)" do
|
114
|
-
LibTest.testCallbackVrU8 { 128 }.
|
121
|
+
expect(LibTest.testCallbackVrU8 { 128 }).to eq(128)
|
115
122
|
end
|
123
|
+
|
116
124
|
it "returning :uchar (-128)" do
|
117
|
-
LibTest.testCallbackVrU8 { -128 }.
|
125
|
+
expect(LibTest.testCallbackVrU8 { -128 }).to eq(128)
|
118
126
|
end
|
127
|
+
|
119
128
|
it "returning :short (0)" do
|
120
|
-
LibTest.testCallbackVrS16 { 0 }.
|
129
|
+
expect(LibTest.testCallbackVrS16 { 0 }).to eq(0)
|
121
130
|
end
|
131
|
+
|
122
132
|
it "returning :short (0x7fff)" do
|
123
|
-
LibTest.testCallbackVrS16 { 0x7fff }.
|
133
|
+
expect(LibTest.testCallbackVrS16 { 0x7fff }).to eq(0x7fff)
|
124
134
|
end
|
125
135
|
# test wrap around
|
126
136
|
it "returning :short (0x8000)" do
|
127
|
-
LibTest.testCallbackVrS16 { 0x8000 }.
|
137
|
+
expect(LibTest.testCallbackVrS16 { 0x8000 }).to eq(-0x8000)
|
128
138
|
end
|
139
|
+
|
129
140
|
it "returning :short (0xffff)" do
|
130
|
-
LibTest.testCallbackVrS16 { 0xffff }.
|
141
|
+
expect(LibTest.testCallbackVrS16 { 0xffff }).to eq(-1)
|
131
142
|
end
|
143
|
+
|
132
144
|
it "returning :ushort (0)" do
|
133
|
-
LibTest.testCallbackVrU16 { 0 }.
|
145
|
+
expect(LibTest.testCallbackVrU16 { 0 }).to eq(0)
|
134
146
|
end
|
147
|
+
|
135
148
|
it "returning :ushort (0x7fff)" do
|
136
|
-
LibTest.testCallbackVrU16 { 0x7fff }.
|
149
|
+
expect(LibTest.testCallbackVrU16 { 0x7fff }).to eq(0x7fff)
|
137
150
|
end
|
151
|
+
|
138
152
|
it "returning :ushort (0x8000)" do
|
139
|
-
LibTest.testCallbackVrU16 { 0x8000 }.
|
153
|
+
expect(LibTest.testCallbackVrU16 { 0x8000 }).to eq(0x8000)
|
140
154
|
end
|
155
|
+
|
141
156
|
it "returning :ushort (0xffff)" do
|
142
|
-
LibTest.testCallbackVrU16 { 0xffff }.
|
157
|
+
expect(LibTest.testCallbackVrU16 { 0xffff }).to eq(0xffff)
|
143
158
|
end
|
159
|
+
|
144
160
|
it "returning :ushort (-1)" do
|
145
|
-
LibTest.testCallbackVrU16 { -1 }.
|
161
|
+
expect(LibTest.testCallbackVrU16 { -1 }).to eq(0xffff)
|
146
162
|
end
|
163
|
+
|
147
164
|
it "returning :int (0)" do
|
148
|
-
LibTest.testCallbackVrS32 { 0 }.
|
165
|
+
expect(LibTest.testCallbackVrS32 { 0 }).to eq(0)
|
149
166
|
end
|
167
|
+
|
150
168
|
it "returning :int (0x7fffffff)" do
|
151
|
-
LibTest.testCallbackVrS32 { 0x7fffffff }.
|
169
|
+
expect(LibTest.testCallbackVrS32 { 0x7fffffff }).to eq(0x7fffffff)
|
152
170
|
end
|
153
171
|
# test wrap around
|
154
172
|
it "returning :int (-0x80000000)" do
|
155
|
-
LibTest.testCallbackVrS32 { -0x80000000 }.
|
173
|
+
expect(LibTest.testCallbackVrS32 { -0x80000000 }).to eq(-0x80000000)
|
156
174
|
end
|
175
|
+
|
157
176
|
it "returning :int (-1)" do
|
158
|
-
LibTest.testCallbackVrS32 { -1 }.
|
177
|
+
expect(LibTest.testCallbackVrS32 { -1 }).to eq(-1)
|
159
178
|
end
|
179
|
+
|
160
180
|
it "returning :uint (0)" do
|
161
|
-
LibTest.testCallbackVrU32 { 0 }.
|
181
|
+
expect(LibTest.testCallbackVrU32 { 0 }).to eq(0)
|
162
182
|
end
|
183
|
+
|
163
184
|
it "returning :uint (0x7fffffff)" do
|
164
|
-
LibTest.testCallbackVrU32 { 0x7fffffff }.
|
185
|
+
expect(LibTest.testCallbackVrU32 { 0x7fffffff }).to eq(0x7fffffff)
|
165
186
|
end
|
166
187
|
# test wrap around
|
167
188
|
it "returning :uint (0x80000000)" do
|
168
|
-
LibTest.testCallbackVrU32 { 0x80000000 }.
|
189
|
+
expect(LibTest.testCallbackVrU32 { 0x80000000 }).to eq(0x80000000)
|
169
190
|
end
|
191
|
+
|
170
192
|
it "returning :uint (0xffffffff)" do
|
171
|
-
LibTest.testCallbackVrU32 { 0xffffffff }.
|
193
|
+
expect(LibTest.testCallbackVrU32 { 0xffffffff }).to eq(0xffffffff)
|
172
194
|
end
|
195
|
+
|
173
196
|
it "returning :uint (-1)" do
|
174
|
-
LibTest.testCallbackVrU32 { -1 }.
|
197
|
+
expect(LibTest.testCallbackVrU32 { -1 }).to eq(0xffffffff)
|
175
198
|
end
|
199
|
+
|
176
200
|
it "returning :long (0)" do
|
177
|
-
LibTest.testCallbackVrL { 0 }.
|
201
|
+
expect(LibTest.testCallbackVrL { 0 }).to eq(0)
|
178
202
|
end
|
203
|
+
|
179
204
|
it "returning :long (0x7fffffff)" do
|
180
|
-
LibTest.testCallbackVrL { 0x7fffffff }.
|
205
|
+
expect(LibTest.testCallbackVrL { 0x7fffffff }).to eq(0x7fffffff)
|
181
206
|
end
|
182
207
|
# test wrap around
|
183
208
|
it "returning :long (-0x80000000)" do
|
184
|
-
LibTest.testCallbackVrL { -0x80000000 }.
|
209
|
+
expect(LibTest.testCallbackVrL { -0x80000000 }).to eq(-0x80000000)
|
185
210
|
end
|
211
|
+
|
186
212
|
it "returning :long (-1)" do
|
187
|
-
LibTest.testCallbackVrL { -1 }.
|
213
|
+
expect(LibTest.testCallbackVrL { -1 }).to eq(-1)
|
188
214
|
end
|
215
|
+
|
189
216
|
it "returning :ulong (0)" do
|
190
|
-
LibTest.testCallbackVrUL { 0 }.
|
217
|
+
expect(LibTest.testCallbackVrUL { 0 }).to eq(0)
|
191
218
|
end
|
219
|
+
|
192
220
|
it "returning :ulong (0x7fffffff)" do
|
193
|
-
LibTest.testCallbackVrUL { 0x7fffffff }.
|
221
|
+
expect(LibTest.testCallbackVrUL { 0x7fffffff }).to eq(0x7fffffff)
|
194
222
|
end
|
195
223
|
# test wrap around
|
196
224
|
it "returning :ulong (0x80000000)" do
|
197
|
-
LibTest.testCallbackVrUL { 0x80000000 }.
|
225
|
+
expect(LibTest.testCallbackVrUL { 0x80000000 }).to eq(0x80000000)
|
198
226
|
end
|
227
|
+
|
199
228
|
it "returning :ulong (0xffffffff)" do
|
200
|
-
LibTest.testCallbackVrUL { 0xffffffff }.
|
229
|
+
expect(LibTest.testCallbackVrUL { 0xffffffff }).to eq(0xffffffff)
|
201
230
|
end
|
231
|
+
|
202
232
|
it "Callback returning :ulong (-1)" do
|
203
233
|
if FFI::Platform::LONG_SIZE == 32
|
204
|
-
LibTest.testCallbackVrUL { -1 }.
|
234
|
+
expect(LibTest.testCallbackVrUL { -1 }).to eq(0xffffffff)
|
205
235
|
else
|
206
|
-
LibTest.testCallbackVrUL { -1 }.
|
236
|
+
expect(LibTest.testCallbackVrUL { -1 }).to eq(0xffffffffffffffff)
|
207
237
|
end
|
208
238
|
end
|
239
|
+
|
209
240
|
it "returning :long_long (0)" do
|
210
|
-
LibTest.testCallbackVrS64 { 0 }.
|
241
|
+
expect(LibTest.testCallbackVrS64 { 0 }).to eq(0)
|
211
242
|
end
|
243
|
+
|
212
244
|
it "returning :long_long (0x7fffffffffffffff)" do
|
213
|
-
LibTest.testCallbackVrS64 { 0x7fffffffffffffff }.
|
245
|
+
expect(LibTest.testCallbackVrS64 { 0x7fffffffffffffff }).to eq(0x7fffffffffffffff)
|
214
246
|
end
|
215
247
|
# test wrap around
|
216
248
|
it "returning :long_long (-0x8000000000000000)" do
|
217
|
-
LibTest.testCallbackVrS64 { -0x8000000000000000 }.
|
249
|
+
expect(LibTest.testCallbackVrS64 { -0x8000000000000000 }).to eq(-0x8000000000000000)
|
218
250
|
end
|
251
|
+
|
219
252
|
it "returning :long_long (-1)" do
|
220
|
-
LibTest.testCallbackVrS64 { -1 }.
|
253
|
+
expect(LibTest.testCallbackVrS64 { -1 }).to eq(-1)
|
221
254
|
end
|
255
|
+
|
222
256
|
it "returning bool" do
|
223
|
-
LibTest.testCallbackVrZ { true }.
|
257
|
+
expect(LibTest.testCallbackVrZ { true }).to be true
|
224
258
|
end
|
259
|
+
|
225
260
|
it "returning :pointer (nil)" do
|
226
|
-
LibTest.testCallbackVrP { nil }.
|
261
|
+
expect(LibTest.testCallbackVrP { nil }).to be_null
|
227
262
|
end
|
263
|
+
|
228
264
|
it "returning :pointer (MemoryPointer)" do
|
229
265
|
p = FFI::MemoryPointer.new :long
|
230
|
-
LibTest.testCallbackVrP { p }.
|
266
|
+
expect(LibTest.testCallbackVrP { p }).to eq(p)
|
231
267
|
end
|
232
268
|
|
233
|
-
|
234
269
|
it "returning struct by value" do
|
235
270
|
s = LibTest::S8F32S32.new
|
236
271
|
s[:s8] = 0x12
|
237
272
|
s[:s32] = 0x1eefbeef
|
238
273
|
s[:f32] = 1.234567
|
239
274
|
ret = LibTest.testCallbackVrT { s }
|
240
|
-
ret[:s8].
|
241
|
-
ret[:f32].
|
242
|
-
ret[:s32].
|
275
|
+
expect(ret[:s8]).to eq(s[:s8])
|
276
|
+
expect(ret[:f32]).to eq(s[:f32])
|
277
|
+
expect(ret[:s32]).to eq(s[:s32])
|
243
278
|
|
244
279
|
end
|
245
280
|
|
@@ -249,11 +284,13 @@ describe "Callback" do
|
|
249
284
|
s[:s32] = 0x1eefbeef
|
250
285
|
s[:f32] = 1.234567
|
251
286
|
s2 = LibTest::S8F32S32.new
|
287
|
+
|
252
288
|
LibTest.testCallbackTrV(s) do |struct|
|
253
289
|
s2[:s8] = struct[:s8]
|
254
290
|
s2[:f32] = struct[:f32]
|
255
291
|
s2[:s32] = struct[:s32]
|
256
292
|
end
|
293
|
+
|
257
294
|
expect(s2[:s8]).to eql 0x12
|
258
295
|
expect(s2[:s32]).to eql 0x1eefbeef
|
259
296
|
expect(s2[:f32]).to be_within(0.0000001).of 1.234567
|
@@ -263,12 +300,12 @@ describe "Callback" do
|
|
263
300
|
it "global variable" do
|
264
301
|
proc = Proc.new { 0x1e }
|
265
302
|
LibTest.cbVrS8 = proc
|
266
|
-
LibTest.testGVarCallbackVrS8(LibTest.pVrS8).
|
303
|
+
expect(LibTest.testGVarCallbackVrS8(LibTest.pVrS8)).to eq(0x1e)
|
267
304
|
end
|
268
305
|
|
269
306
|
describe "When the callback is considered optional by the underlying library" do
|
270
307
|
it "should handle receiving 'nil' in place of the closure" do
|
271
|
-
LibTest.testOptionalCallbackCrV(nil, 13).
|
308
|
+
expect(LibTest.testOptionalCallbackCrV(nil, 13)).to be_nil
|
272
309
|
end
|
273
310
|
end
|
274
311
|
|
@@ -279,20 +316,20 @@ describe "Callback" do
|
|
279
316
|
ffi_lib TestLibrary::PATH
|
280
317
|
attach_function :testAnonymousCallbackVrS8, :testClosureVrB, [ callback([ ], :char) ], :char
|
281
318
|
end
|
282
|
-
LibTest.testAnonymousCallbackVrS8 { 0 }.
|
319
|
+
expect(LibTest.testAnonymousCallbackVrS8 { 0 }).to eq(0)
|
283
320
|
end
|
284
321
|
end
|
285
322
|
|
286
323
|
describe "as return value" do
|
287
324
|
|
288
325
|
it "should not blow up when a callback is defined that returns a callback" do
|
289
|
-
module LibTest
|
326
|
+
expect(module LibTest
|
290
327
|
extend FFI::Library
|
291
328
|
ffi_lib TestLibrary::PATH
|
292
329
|
callback :cb_return_type_1, [ :short ], :short
|
293
330
|
callback :cb_lookup_1, [ :short ], :cb_return_type_1
|
294
331
|
attach_function :testReturnsCallback_1, :testReturnsClosure, [ :cb_lookup_1, :short ], :cb_return_type_1
|
295
|
-
end.
|
332
|
+
end).to be_an_instance_of FFI::Function
|
296
333
|
end
|
297
334
|
|
298
335
|
it "should return a callback" do
|
@@ -317,9 +354,9 @@ describe "Callback" do
|
|
317
354
|
end
|
318
355
|
|
319
356
|
val = LibTest.testReturnsCallback(lookup_proc, 0x1234)
|
320
|
-
val.
|
321
|
-
lookup_proc_called.
|
322
|
-
return_proc_called.
|
357
|
+
expect(val).to eq(0x1234 * 2)
|
358
|
+
expect(lookup_proc_called).to be true
|
359
|
+
expect(return_proc_called).to be true
|
323
360
|
end
|
324
361
|
|
325
362
|
it "should return a method callback" do
|
@@ -339,17 +376,17 @@ describe "Callback" do
|
|
339
376
|
end
|
340
377
|
end
|
341
378
|
|
342
|
-
LibTest.testReturnsCallback_2(MethodCallback.method(:lookup), 0x1234).
|
379
|
+
expect(LibTest.testReturnsCallback_2(MethodCallback.method(:lookup), 0x1234)).to eq(0x2468)
|
343
380
|
end
|
344
381
|
|
345
382
|
it 'should not blow up when a callback takes a callback as argument' do
|
346
|
-
module LibTest
|
383
|
+
expect(module LibTest
|
347
384
|
extend FFI::Library
|
348
385
|
ffi_lib TestLibrary::PATH
|
349
386
|
callback :cb_argument, [ :int ], :int
|
350
387
|
callback :cb_with_cb_argument, [ :cb_argument, :int ], :int
|
351
388
|
attach_function :testCallbackAsArgument_2, :testArgumentClosure, [ :cb_with_cb_argument, :int ], :int
|
352
|
-
end.
|
389
|
+
end).to be_an_instance_of FFI::Function
|
353
390
|
end
|
354
391
|
it 'should be able to use the callback argument' do
|
355
392
|
module LibTest
|
@@ -360,7 +397,7 @@ describe "Callback" do
|
|
360
397
|
attach_function :testCallbackAsArgument, :testArgumentClosure, [ :cb_with_cb_argument, :cb_argument, :int ], :int
|
361
398
|
end
|
362
399
|
callback_arg_called = false
|
363
|
-
callback_with_callback_arg_called = false
|
400
|
+
callback_with_callback_arg_called = false
|
364
401
|
callback_arg = Proc.new do |val|
|
365
402
|
callback_arg_called = true
|
366
403
|
val * 2
|
@@ -370,9 +407,9 @@ describe "Callback" do
|
|
370
407
|
cb.call(val)
|
371
408
|
end
|
372
409
|
val = LibTest.testCallbackAsArgument(callback_with_callback_arg, callback_arg, 0xff1)
|
373
|
-
val.
|
374
|
-
callback_arg_called.
|
375
|
-
callback_with_callback_arg_called.
|
410
|
+
expect(val).to eq(0xff1 * 2)
|
411
|
+
expect(callback_arg_called).to be true
|
412
|
+
expect(callback_with_callback_arg_called).to be true
|
376
413
|
end
|
377
414
|
it 'function returns callable object' do
|
378
415
|
module LibTest
|
@@ -382,7 +419,7 @@ describe "Callback" do
|
|
382
419
|
attach_function :testReturnsFunctionPointer, [ ], :funcptr
|
383
420
|
end
|
384
421
|
f = LibTest.testReturnsFunctionPointer
|
385
|
-
f.call(3).
|
422
|
+
expect(f.call(3)).to eq(6)
|
386
423
|
end
|
387
424
|
end
|
388
425
|
|
@@ -433,239 +470,281 @@ describe "Callback with " do
|
|
433
470
|
attach_function :testCallbackPrV, :testClosurePrV, [ :cbPrV, :pointer], :void
|
434
471
|
attach_function :testCallbackYrV, :testClosurePrV, [ :cbYrV, S8F32S32.in ], :void
|
435
472
|
end
|
473
|
+
|
436
474
|
it "function with Callback plus another arg should raise error if no arg given" do
|
437
|
-
|
475
|
+
expect { LibTest.testCallbackCrV { |*a| }}.to raise_error
|
438
476
|
end
|
477
|
+
|
439
478
|
it ":char (0) argument" do
|
440
479
|
v = 0xdeadbeef
|
441
480
|
LibTest.testCallbackCrV(0) { |i| v = i }
|
442
|
-
v.
|
481
|
+
expect(v).to eq(0)
|
443
482
|
end
|
483
|
+
|
444
484
|
it ":char (127) argument" do
|
445
485
|
v = 0xdeadbeef
|
446
486
|
LibTest.testCallbackCrV(127) { |i| v = i }
|
447
|
-
v.
|
487
|
+
expect(v).to eq(127)
|
448
488
|
end
|
489
|
+
|
449
490
|
it ":char (-128) argument" do
|
450
491
|
v = 0xdeadbeef
|
451
492
|
LibTest.testCallbackCrV(-128) { |i| v = i }
|
452
|
-
v.
|
493
|
+
expect(v).to eq(-128)
|
453
494
|
end
|
495
|
+
|
454
496
|
it ":char (-1) argument" do
|
455
497
|
v = 0xdeadbeef
|
456
498
|
LibTest.testCallbackCrV(-1) { |i| v = i }
|
457
|
-
v.
|
499
|
+
expect(v).to eq(-1)
|
458
500
|
end
|
501
|
+
|
459
502
|
it ":uchar (0) argument" do
|
460
503
|
v = 0xdeadbeef
|
461
504
|
LibTest.testCallbackU8rV(0) { |i| v = i }
|
462
|
-
v.
|
505
|
+
expect(v).to eq(0)
|
463
506
|
end
|
507
|
+
|
464
508
|
it ":uchar (127) argument" do
|
465
509
|
v = 0xdeadbeef
|
466
510
|
LibTest.testCallbackU8rV(127) { |i| v = i }
|
467
|
-
v.
|
511
|
+
expect(v).to eq(127)
|
468
512
|
end
|
513
|
+
|
469
514
|
it ":uchar (128) argument" do
|
470
515
|
v = 0xdeadbeef
|
471
516
|
LibTest.testCallbackU8rV(128) { |i| v = i }
|
472
|
-
v.
|
517
|
+
expect(v).to eq(128)
|
473
518
|
end
|
519
|
+
|
474
520
|
it ":uchar (255) argument" do
|
475
521
|
v = 0xdeadbeef
|
476
522
|
LibTest.testCallbackU8rV(255) { |i| v = i }
|
477
|
-
v.
|
523
|
+
expect(v).to eq(255)
|
478
524
|
end
|
479
525
|
|
480
526
|
it ":short (0) argument" do
|
481
527
|
v = 0xdeadbeef
|
482
528
|
LibTest.testCallbackSrV(0) { |i| v = i }
|
483
|
-
v.
|
529
|
+
expect(v).to eq(0)
|
484
530
|
end
|
531
|
+
|
485
532
|
it ":short (0x7fff) argument" do
|
486
533
|
v = 0xdeadbeef
|
487
534
|
LibTest.testCallbackSrV(0x7fff) { |i| v = i }
|
488
|
-
v.
|
535
|
+
expect(v).to eq(0x7fff)
|
489
536
|
end
|
537
|
+
|
490
538
|
it ":short (-0x8000) argument" do
|
491
539
|
v = 0xdeadbeef
|
492
540
|
LibTest.testCallbackSrV(-0x8000) { |i| v = i }
|
493
|
-
v.
|
541
|
+
expect(v).to eq(-0x8000)
|
494
542
|
end
|
543
|
+
|
495
544
|
it ":short (-1) argument" do
|
496
545
|
v = 0xdeadbeef
|
497
546
|
LibTest.testCallbackSrV(-1) { |i| v = i }
|
498
|
-
v.
|
547
|
+
expect(v).to eq(-1)
|
499
548
|
end
|
549
|
+
|
500
550
|
it ":ushort (0) argument" do
|
501
551
|
v = 0xdeadbeef
|
502
552
|
LibTest.testCallbackU16rV(0) { |i| v = i }
|
503
|
-
v.
|
553
|
+
expect(v).to eq(0)
|
504
554
|
end
|
555
|
+
|
505
556
|
it ":ushort (0x7fff) argument" do
|
506
557
|
v = 0xdeadbeef
|
507
558
|
LibTest.testCallbackU16rV(0x7fff) { |i| v = i }
|
508
|
-
v.
|
559
|
+
expect(v).to eq(0x7fff)
|
509
560
|
end
|
561
|
+
|
510
562
|
it ":ushort (0x8000) argument" do
|
511
563
|
v = 0xdeadbeef
|
512
564
|
LibTest.testCallbackU16rV(0x8000) { |i| v = i }
|
513
|
-
v.
|
565
|
+
expect(v).to eq(0x8000)
|
514
566
|
end
|
567
|
+
|
515
568
|
it ":ushort (0xffff) argument" do
|
516
569
|
v = 0xdeadbeef
|
517
570
|
LibTest.testCallbackU16rV(0xffff) { |i| v = i }
|
518
|
-
v.
|
571
|
+
expect(v).to eq(0xffff)
|
519
572
|
end
|
573
|
+
|
520
574
|
it ":bool (true) argument" do
|
521
575
|
v = false
|
522
576
|
LibTest.testCallbackZrV(true) { |i| v = i }
|
523
|
-
v.
|
577
|
+
expect(v).to be true
|
524
578
|
end
|
579
|
+
|
525
580
|
it ":int (0) argument" do
|
526
581
|
v = 0xdeadbeef
|
527
582
|
LibTest.testCallbackIrV(0) { |i| v = i }
|
528
|
-
v.
|
583
|
+
expect(v).to eq(0)
|
529
584
|
end
|
585
|
+
|
530
586
|
it ":int (0x7fffffff) argument" do
|
531
587
|
v = 0xdeadbeef
|
532
588
|
LibTest.testCallbackIrV(0x7fffffff) { |i| v = i }
|
533
|
-
v.
|
589
|
+
expect(v).to eq(0x7fffffff)
|
534
590
|
end
|
591
|
+
|
535
592
|
it ":int (-0x80000000) argument" do
|
536
593
|
v = 0xdeadbeef
|
537
594
|
LibTest.testCallbackIrV(-0x80000000) { |i| v = i }
|
538
|
-
v.
|
595
|
+
expect(v).to eq(-0x80000000)
|
539
596
|
end
|
597
|
+
|
540
598
|
it ":int (-1) argument" do
|
541
599
|
v = 0xdeadbeef
|
542
600
|
LibTest.testCallbackIrV(-1) { |i| v = i }
|
543
|
-
v.
|
601
|
+
expect(v).to eq(-1)
|
544
602
|
end
|
603
|
+
|
545
604
|
it ":uint (0) argument" do
|
546
605
|
v = 0xdeadbeef
|
547
606
|
LibTest.testCallbackU32rV(0) { |i| v = i }
|
548
|
-
v.
|
607
|
+
expect(v).to eq(0)
|
549
608
|
end
|
609
|
+
|
550
610
|
it ":uint (0x7fffffff) argument" do
|
551
611
|
v = 0xdeadbeef
|
552
612
|
LibTest.testCallbackU32rV(0x7fffffff) { |i| v = i }
|
553
|
-
v.
|
613
|
+
expect(v).to eq(0x7fffffff)
|
554
614
|
end
|
615
|
+
|
555
616
|
it ":uint (0x80000000) argument" do
|
556
617
|
v = 0xdeadbeef
|
557
618
|
LibTest.testCallbackU32rV(0x80000000) { |i| v = i }
|
558
|
-
v.
|
619
|
+
expect(v).to eq(0x80000000)
|
559
620
|
end
|
621
|
+
|
560
622
|
it ":uint (0xffffffff) argument" do
|
561
623
|
v = 0xdeadbeef
|
562
624
|
LibTest.testCallbackU32rV(0xffffffff) { |i| v = i }
|
563
|
-
v.
|
625
|
+
expect(v).to eq(0xffffffff)
|
564
626
|
end
|
627
|
+
|
565
628
|
it ":long (0) argument" do
|
566
629
|
v = 0xdeadbeef
|
567
630
|
LibTest.testCallbackLrV(0) { |i| v = i }
|
568
|
-
v.
|
631
|
+
expect(v).to eq(0)
|
569
632
|
end
|
633
|
+
|
570
634
|
it ":long (0x7fffffff) argument" do
|
571
635
|
v = 0xdeadbeef
|
572
636
|
LibTest.testCallbackLrV(0x7fffffff) { |i| v = i }
|
573
|
-
v.
|
637
|
+
expect(v).to eq(0x7fffffff)
|
574
638
|
end
|
639
|
+
|
575
640
|
it ":long (-0x80000000) argument" do
|
576
641
|
v = 0xdeadbeef
|
577
642
|
LibTest.testCallbackLrV(-0x80000000) { |i| v = i }
|
578
|
-
v.
|
643
|
+
expect(v).to eq(-0x80000000)
|
579
644
|
end
|
645
|
+
|
580
646
|
it ":long (-1) argument" do
|
581
647
|
v = 0xdeadbeef
|
582
648
|
LibTest.testCallbackLrV(-1) { |i| v = i }
|
583
|
-
v.
|
649
|
+
expect(v).to eq(-1)
|
584
650
|
end
|
651
|
+
|
585
652
|
it ":ulong (0) argument" do
|
586
653
|
v = 0xdeadbeef
|
587
654
|
LibTest.testCallbackULrV(0) { |i| v = i }
|
588
|
-
v.
|
655
|
+
expect(v).to eq(0)
|
589
656
|
end
|
657
|
+
|
590
658
|
it ":ulong (0x7fffffff) argument" do
|
591
659
|
v = 0xdeadbeef
|
592
660
|
LibTest.testCallbackULrV(0x7fffffff) { |i| v = i }
|
593
|
-
v.
|
661
|
+
expect(v).to eq(0x7fffffff)
|
594
662
|
end
|
663
|
+
|
595
664
|
it ":ulong (0x80000000) argument" do
|
596
665
|
v = 0xdeadbeef
|
597
666
|
LibTest.testCallbackULrV(0x80000000) { |i| v = i }
|
598
|
-
v.
|
667
|
+
expect(v).to eq(0x80000000)
|
599
668
|
end
|
669
|
+
|
600
670
|
it ":ulong (0xffffffff) argument" do
|
601
671
|
v = 0xdeadbeef
|
602
672
|
LibTest.testCallbackULrV(0xffffffff) { |i| v = i }
|
603
|
-
v.
|
673
|
+
expect(v).to eq(0xffffffff)
|
604
674
|
end
|
675
|
+
|
605
676
|
it ":long_long (0) argument" do
|
606
677
|
v = 0xdeadbeef
|
607
678
|
LibTest.testCallbackLLrV(0) { |i| v = i }
|
608
|
-
v.
|
679
|
+
expect(v).to eq(0)
|
609
680
|
end
|
681
|
+
|
610
682
|
it ":long_long (0x7fffffffffffffff) argument" do
|
611
683
|
v = 0xdeadbeef
|
612
684
|
LibTest.testCallbackLLrV(0x7fffffffffffffff) { |i| v = i }
|
613
|
-
v.
|
685
|
+
expect(v).to eq(0x7fffffffffffffff)
|
614
686
|
end
|
687
|
+
|
615
688
|
it ":long_long (-0x8000000000000000) argument" do
|
616
689
|
v = 0xdeadbeef
|
617
690
|
LibTest.testCallbackLLrV(-0x8000000000000000) { |i| v = i }
|
618
|
-
v.
|
691
|
+
expect(v).to eq(-0x8000000000000000)
|
619
692
|
end
|
693
|
+
|
620
694
|
it ":long_long (-1) argument" do
|
621
695
|
v = 0xdeadbeef
|
622
696
|
LibTest.testCallbackLLrV(-1) { |i| v = i }
|
623
|
-
v.
|
697
|
+
expect(v).to eq(-1)
|
624
698
|
end
|
699
|
+
|
625
700
|
it ":string argument" do
|
626
701
|
v = nil
|
627
702
|
LibTest.testCallbackArV("Hello, World") { |i| v = i }
|
628
|
-
v.
|
703
|
+
expect(v).to eq("Hello, World")
|
629
704
|
end
|
705
|
+
|
630
706
|
it ":string (nil) argument" do
|
631
707
|
v = "Hello, World"
|
632
708
|
LibTest.testCallbackArV(nil) { |i| v = i }
|
633
|
-
v.
|
709
|
+
expect(v).to be_nil
|
634
710
|
end
|
711
|
+
|
635
712
|
it ":pointer argument" do
|
636
713
|
v = nil
|
637
714
|
magic = FFI::Pointer.new(0xdeadbeef)
|
638
715
|
LibTest.testCallbackPrV(magic) { |i| v = i }
|
639
|
-
v.
|
716
|
+
expect(v).to eq(magic)
|
640
717
|
end
|
718
|
+
|
641
719
|
it ":pointer (nil) argument" do
|
642
720
|
v = "Hello, World"
|
643
721
|
LibTest.testCallbackPrV(nil) { |i| v = i }
|
644
|
-
v.
|
722
|
+
expect(v).to eq(FFI::Pointer::NULL)
|
645
723
|
end
|
724
|
+
|
646
725
|
it "struct by reference argument" do
|
647
726
|
v = nil
|
648
727
|
magic = LibTest::S8F32S32.new
|
649
728
|
LibTest.testCallbackYrV(magic) { |i| v = i }
|
650
|
-
v.class.
|
651
|
-
v.pointer.
|
729
|
+
expect(v.class).to eq(magic.class)
|
730
|
+
expect(v.pointer).to eq(magic.pointer)
|
652
731
|
end
|
653
732
|
|
654
733
|
it "struct by reference argument with nil value" do
|
655
734
|
v = LibTest::S8F32S32.new
|
656
735
|
LibTest.testCallbackYrV(nil) { |i| v = i }
|
657
|
-
v.is_a?(FFI::Struct).
|
658
|
-
v.pointer.
|
736
|
+
expect(v.is_a?(FFI::Struct)).to be true
|
737
|
+
expect(v.pointer).to eq(FFI::Pointer::NULL)
|
659
738
|
end
|
660
739
|
|
661
740
|
it "varargs parameters are rejected" do
|
662
|
-
|
741
|
+
expect {
|
663
742
|
Module.new do
|
664
743
|
extend FFI::Library
|
665
744
|
ffi_lib TestLibrary::PATH
|
666
745
|
callback :cbVrL, [ :varargs ], :long
|
667
746
|
end
|
668
|
-
}.
|
747
|
+
}.to raise_error(ArgumentError)
|
669
748
|
end
|
670
749
|
|
671
750
|
#
|
@@ -687,8 +766,8 @@ describe "Callback with " do
|
|
687
766
|
po = FFI::MemoryPointer.new :long
|
688
767
|
pr = proc{|a,i| v = a,i; i }
|
689
768
|
res = LibTestStdcall.testCallbackStdcall(po, pr, 0x7fffffff)
|
690
|
-
v.
|
691
|
-
res.
|
769
|
+
expect(v).to eq([po, 0x7fffffff])
|
770
|
+
expect(res).to be true
|
692
771
|
end
|
693
772
|
end
|
694
773
|
end
|