/
FFICalloutAPITest.class.st
381 lines (291 loc) · 9.49 KB
/
FFICalloutAPITest.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"
Tests for FFICalloutAPI
"
Class {
#name : #FFICalloutAPITest,
#superclass : #TestCase,
#category : #'UnifiedFFI-Tests-Tests'
}
{ #category : #running }
FFICalloutAPITest >> ffiCalloutClass [
^ FFICalloutAPI
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiCopyString: aString to: dest [
^ self ffiCall: #( String strcpy ( String dest, String aString ) )
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiDoubleAbs: number [
^ self ffiCall: #( double DoubleAbsolute (double number) ) options: #(+optCallbackCall)
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiFloatAbs: number [
^ self ffiCall: #( float FloatAbsolute (float number) ) options: #(+optCallbackCall)
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiIntAbs: number [
^ self ffiCall: #( int abs ( int number ) )
]
{ #category : #accessing }
FFICalloutAPITest >> ffiLibraryName [
^ LibC
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiLongLongAbs: number [
^ self ffiCall: #( longlong llabs (longlong number) )
]
{ #category : #'primitives constant' }
FFICalloutAPITest >> ffiTestConstantFormat: format to: buffer [
^ self ffiCall: #( int sprintf ( ByteArray buffer, String format, 65, 65, true ) )
]
{ #category : #'primitives constant' }
FFICalloutAPITest >> ffiTestContantFormat: format value: aNumber to: buffer [
^ self ffiCall: #( int sprintf ( ByteArray buffer, String format, 65, 65, long aNumber ) )
]
{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiToLower: c [
^ self ffiCall: #( char tolower (char c) )
]
{ #category : #accessing }
FFICalloutAPITest >> method1 [
| object index |
object := FFICalloutObjectForTest new.
index := FFICalloutObjectForTest class instVarIndexFor: #handle ifAbsent: [ nil ].
^ object instVarAt: index.
]
{ #category : #accessing }
FFICalloutAPITest >> method2 [
| result tmpResult |
result := self call: {1. 2. 3. 4}.
tmpResult := result.
result := FFICalloutObjectForTest basicNew.
result instVarAt: 1 put: tmpResult.
^ result
]
{ #category : #'primitives pointer' }
FFICalloutAPITest >> primFromByteArray: src toExternalAddress: dest size: n [
^ self
ffiCall: #( void *memcpy(ExternalAddress dest, ByteArray src, size_t n) )
module: LibC
]
{ #category : #'primitives pointer' }
FFICalloutAPITest >> primMemMoveFrom: src to: dest size: n [
^ self
ffiCall: #( void *memcpy(void *dest, const void *src, size_t n) )
module: LibC
]
{ #category : #'primitives pointer' }
FFICalloutAPITest >> primStr: aString cat: otherString [
^ self
ffiCall: #( char *strcat ( char *aString, char *otherString ) )
module: LibC
]
{ #category : #running }
FFICalloutAPITest >> resetFFIMethods [
| ffiMethods |
ffiMethods := FFICalloutAPITest methodDict select: [ :method | (method selector beginsWith: 'ffi') or: [method selector beginsWith: 'prim'] ].
ffiMethods do: #recompile
]
{ #category : #running }
FFICalloutAPITest >> runCase [
| oldAPIClass |
"Ensure I use this class for running the tests"
oldAPIClass := FFICalloutAPI calloutAPIClass.
[ FFICalloutAPI calloutAPIClass: FFICalloutAPI.
super runCase ]
ensure: [ FFICalloutAPI calloutAPIClass: oldAPIClass ]
]
{ #category : #'tests pointer' }
FFICalloutAPITest >> testByteArrayToExternalAddress [
| result dest |
result := self
primFromByteArray: 'Hello, World' asByteArray
toExternalAddress: (dest := ExternalAddress allocate: 12) autoRelease
size: 12.
self assert: (dest copyFrom: 1 to: 12) asString equals: 'Hello, World'.
]
{ #category : #'tests constant' }
FFICalloutAPITest >> testCallWithAllConstParameter [
"Tests a function in the form:
self nbCall: #( long ffiTestInts ( 65, 65, 65, long aNumber ) )
(passing constants as parameters)
"
| result buffer |
buffer := ByteArray new: 100.
result := self ffiTestConstantFormat: '%d, %d and %d' to: buffer.
self assert: result equals: 12.
self assert: (buffer first: result) asString equals: '65, 65 and 1'
]
{ #category : #'tests constant' }
FFICalloutAPITest >> testCallWithConstParameter [
"Tests a function in the form:
self nbCall: #( long ffiTestInts ( 65, 65, 65, long aNumber ) )
(passing constants as parameters)
"
| result buffer |
buffer := ByteArray new: 100.
result := self ffiTestContantFormat: '%d, %d and %d' value: true to: buffer.
self assert: result equals: 12.
self assert: (buffer first: result) asString equals: '65, 65 and 1'
]
{ #category : #'tests object' }
FFICalloutAPITest >> testCallWithObjectCreation [
"Test a call of the form:
self nbCall: #( NBFFICalloutVMObjectForTest malloc ( size_t size ) )
(obtaining an object as result)
"
| object |
[
self shouldnt: [ object := FFICalloutObjectForTest primCreate: 1 ] raise: Error.
self assert: object notNil.
self assert: object class equals: FFICalloutObjectForTest.
self assert: object handle notNil.
self deny: object handle isNull ]
ensure: [
object ifNotNil: [ object free ] ]
]
{ #category : #'tests object' }
FFICalloutAPITest >> testCallWithObjectParameter [
"Test a call of the form:
self nbCall: #( void function ( self ) )
(passing self as parameter)
"
| object text |
text := 'Hello, World'.
object := FFICalloutObjectForTest new
handle: (ByteArray new: text size + 1);
yourself.
self
shouldnt: [ FFICalloutObjectForTest primObject: object strcpy: text ]
raise: Error.
self assert: object handle asString equals: (text copyWith: (Character value: 0))
]
{ #category : #'tests object' }
FFICalloutAPITest >> testCallWithSelfParameter [
"Test a call of the form:
self nbCall: #( void function ( self ) )
(passing self as parameter)
"
| object |
object := FFICalloutObjectForTest new.
self
shouldnt: [ object strcpy: 'Hello, World' ]
raise: Error.
"Since is a strcpy it will answer a byte array terminated in zero"
self assert: object handle asString equals: ('Hello, World' copyWith: (Character value: 0))
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testCharCall [
"Test using generic FFI spec"
| result |
result := self ffiToLower: $A.
self assert: result equals: $a.
result := self ffiToLower: 65.
self assert: result equals: $a.
result := self ffiToLower: 65.0.
self assert: result equals: $a.
result := self ffiToLower: true.
self assert: result equals: (Character value: 1)
]
{ #category : #'tests pointer' }
FFICalloutAPITest >> testCharPointer [
| result |
result := self
primStr: ('Hello, ' asByteArray copyWith: 0)
cat: ('World' asByteArray copyWith: 0).
self assert: result equals: 'Hello, World'.
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testDoubleCall [
"Test using generic FFI spec"
| callback result |
callback := FFICallback
signature: #(double (double x))
block: [ :x | x abs ].
FFICallbackFunctionResolution registerCallback: callback as: #DoubleAbsolute.
result := self ffiDoubleAbs: 65.
self assert: result equals: 65.0.
result := self ffiDoubleAbs: 65.0.
self assert: result equals: 65.0.
result := self ffiDoubleAbs: $A.
self assert: result equals: 65.0.
result := self ffiDoubleAbs: true.
self assert: result equals: 1.0
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testFloatCall [
"Test using generic FFI spec"
| callback result |
callback := FFICallback
signature: #(float (float x))
block: [ :x | x abs ].
FFICallbackFunctionResolution registerCallback: callback as: #FloatAbsolute.
result := self ffiFloatAbs: 65.
self assert: result equals: 65.0.
result := self ffiFloatAbs: 65.0.
self assert: result equals: 65.0.
result := self ffiFloatAbs: $A.
self assert: result equals: 65.0.
result := self ffiFloatAbs: true.
self assert: result equals: 1.0
]
{ #category : #'tests pointer' }
FFICalloutAPITest >> testForwardedObjects [
| result dest |
dest := ByteArray new: 12.
"Pinning an object replaces it with a forwarded."
"We want to force the object to be forwarded by the ffi call primitive"
dest pinInMemory.
result := self
primMemMoveFrom: 'Hello, World' asByteArray
to: dest
size: 12.
self deny: result isNull.
self assert: dest asString equals: 'Hello, World'
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testIntCall [
"Test using generic FFI spec"
| result |
result := self ffiIntAbs: 65.
self assert: result equals: 65.
result := self ffiIntAbs: 65.0.
self assert: result equals: 65.
result := self ffiIntAbs: $A.
self assert: result equals: 65.
result := self ffiIntAbs: true.
self assert: result equals: 1.
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testLongLongs [
"Test passing and returning longlongs"
| long1 long2 result |
"This test fails in windows, but it is expected (is because the funtion used is in
another library)"
Smalltalk os isWindows ifTrue: [ ^ self skip ].
long1 := 16r123456789012.
long2 := (-1 << 31).
result := self ffiLongLongAbs: long1.
self assert: result equals: long1.
result := self ffiLongLongAbs: long2.
self assert: result equals: long2 abs
]
{ #category : #'tests atomic' }
FFICalloutAPITest >> testPrintString [
"Test using generic FFI spec"
| result buffer |
buffer := ByteArray new: 12.
result := self ffiCopyString: 'Hello World!' to: buffer.
self assert: result equals: 'Hello World!'.
self assert: buffer asString equals: 'Hello World!'
]
{ #category : #'tests pointer' }
FFICalloutAPITest >> testVoidPointer [
| result dest |
result := self
primMemMoveFrom: 'Hello, World' asByteArray
to: (dest := ByteArray new: 12)
size: 12.
self deny: result isNull.
self assert: dest asString equals: 'Hello, World'
]