Skip to content

Commit

Permalink
Fixing the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tesonep committed Nov 25, 2020
1 parent 91a06c2 commit 86e9ae2
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 40 deletions.
12 changes: 6 additions & 6 deletions src/UnifiedFFI-Tests/FFICallbackParametersTest.class.st
Expand Up @@ -138,7 +138,7 @@ FFICallbackParametersTest >> testIntegerPointerParameters [
ExternalAddress fromAddress: 6.}.

callback := FFICallback
signature: #(void (void* a, void* b, int* c, int* d, char* e, float* f))
signature: #(void (void* a, void* b, int* c, int* d, int* e, float* f))
block: [ :a :b :c :d :e :f |
self assert: a value equals: 1.
self assert: b value equals: 2.
Expand Down Expand Up @@ -235,9 +235,9 @@ FFICallbackParametersTest >> testPassing4DoubleStructureInTheStack [

param := FFITestStructureDoubleStructure externalNew
x: 2.0;
y: 3;
y: 3.0;
w: 5.0;
z: 7;
z: 7.0;
autoRelease;
yourself.

Expand Down Expand Up @@ -306,12 +306,12 @@ FFICallbackParametersTest >> testPassingStructureInTheStack [
self assert: a x equals: 2.0.
self assert: a y equals: 3.
self assert: a w equals: 5.0.
self assert: a z equals: 7.
self assert: a z equals: 7.0.

a x: 3.0.
a y: 4.
a w: 5.0.
a z: 7.
a z: 7.0.

77
].
Expand All @@ -320,7 +320,7 @@ FFICallbackParametersTest >> testPassingStructureInTheStack [
x: 2.0;
y: 3;
w: 5.0;
z: 7;
z: 7.0;
autoRelease;
yourself.

Expand Down
4 changes: 4 additions & 0 deletions src/UnifiedFFI-Tests/FFICallbackTest.class.st
Expand Up @@ -87,6 +87,10 @@ FFICallbackTest >> testCqsortWithByteArray [
{ #category : #tests }
FFICallbackTest >> testEnumerationReturnValue [
| cb returnValue |

"This tests is only valid if using the alien callbacks"
(FFIBackend current isKindOf: OldFFIBackend) ifFalse: [ ^ self skip].

"Issue 19145 - change semantics so that enumerations can be returned
from callback without sending #value to it."
cb := FFICallback
Expand Down
56 changes: 31 additions & 25 deletions src/UnifiedFFI-Tests/FFICalloutAPITest.class.st
Expand Up @@ -19,7 +19,7 @@ FFICalloutAPITest >> ffiCalloutClass [

{ #category : #'primitives atomic' }
FFICalloutAPITest >> ffiCopyString: aString to: dest [
^ self ffiCall: #( String strcpy ( String dest, String aString ) )
^ self ffiCall: #( String strcpy ( void* dest, String aString ) )
]

{ #category : #'primitives atomic' }
Expand Down Expand Up @@ -49,7 +49,7 @@ FFICalloutAPITest >> ffiLongLongAbs: number [

{ #category : #'primitives constant' }
FFICalloutAPITest >> ffiTestConstantFormat: format to: buffer [
^ self ffiCall: #( int sprintf ( ByteArray buffer, String format, 65, 65, true ) )
^ self ffiCall: #( int sprintf ( ByteArray buffer, String format, 65, 65, 1 ) )
]

{ #category : #'primitives constant' }
Expand Down Expand Up @@ -85,7 +85,7 @@ FFICalloutAPITest >> method2 [
{ #category : #'primitives pointer' }
FFICalloutAPITest >> primFromByteArray: src toExternalAddress: dest size: n [
^ self
ffiCall: #( void *memcpy(ExternalAddress dest, ByteArray src, size_t n) )
ffiCall: #( void *memcpy(void* dest, ByteArray src, size_t n) )
module: LibC
]

Expand All @@ -111,17 +111,6 @@ FFICalloutAPITest >> resetFFIMethods [
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 : #running }
FFICalloutAPITest >> setUp [

Expand Down Expand Up @@ -158,7 +147,7 @@ FFICalloutAPITest >> testByteArrayToExternalAddress [


result := self
primFromByteArray: 'Hello, World' asByteArray
primFromByteArray: ('Hello, World' asByteArray pinInMemory; yourself)
toExternalAddress: (dest := ExternalAddress allocate: 12) autoRelease
size: 12.

Expand All @@ -176,7 +165,9 @@ FFICalloutAPITest >> testCallWithAllConstParameter [
"
| result buffer |

buffer := ByteArray new: 100.
buffer := (ByteArray new: 100)
pinInMemory;
yourself.
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'
Expand All @@ -192,7 +183,9 @@ FFICalloutAPITest >> testCallWithConstParameter [
"
| result buffer |

buffer := ByteArray new: 100.
buffer := (ByteArray new: 100)
pinInMemory; yourself.

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'
Expand Down Expand Up @@ -278,28 +271,33 @@ FFICalloutAPITest >> testCharCall [

{ #category : #'tests pointer' }
FFICalloutAPITest >> testCharPointer [
| result |
| result first second |

result := self
primStr: ('Hello, ' asByteArray copyWith: 0)
cat: ('World' asByteArray copyWith: 0).
primStr: 'Hello, '
cat: 'World'.

self assert: result equals: 'Hello, World'.
]

{ #category : #'tests atomic' }
FFICalloutAPITest >> testDoubleCall [
"Test using generic FFI spec"

| callback result |

(FFIBackend current isKindOf: OldFFIBackend)
ifFalse: [
"Registering callbacks like functions is from the old backend"
^ self skip ].

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.
Expand All @@ -313,6 +311,12 @@ FFICalloutAPITest >> testDoubleCall [
FFICalloutAPITest >> testFloatCall [
"Test using generic FFI spec"
| callback result |

(FFIBackend current isKindOf: OldFFIBackend)
ifFalse: [
"Registering callbacks like functions is from the old backend"
^ self skip ].

callback := FFICallback
signature: #(float (float x))
block: [ :x | x abs ].
Expand Down Expand Up @@ -394,7 +398,9 @@ FFICalloutAPITest >> testPrintString [
"Test using generic FFI spec"
| result buffer |

buffer := ByteArray new: 12.
buffer := (ByteArray new: 12)
pinInMemory;
yourself.
result := self ffiCopyString: 'Hello World!' to: buffer.
self assert: result equals: 'Hello World!'.
self assert: buffer asString equals: 'Hello World!'
Expand Down
11 changes: 11 additions & 0 deletions src/UnifiedFFI-Tests/FFICalloutMethodBuilderTestContext.class.st
Expand Up @@ -33,6 +33,12 @@ FFICalloutMethodBuilderTestContext >> compiledMethod [
^ self
]

{ #category : #'as yet unclassified' }
FFICalloutMethodBuilderTestContext >> ffiLibrary [

^ LibC
]

{ #category : #accessing }
FFICalloutMethodBuilderTestContext >> method [
^ self
Expand All @@ -55,6 +61,11 @@ FFICalloutMethodBuilderTestContext >> methodProperties [
^ nil
]

{ #category : #'as yet unclassified' }
FFICalloutMethodBuilderTestContext >> receiver [
^ self
]

{ #category : #accessing }
FFICalloutMethodBuilderTestContext >> selector [
^ selector
Expand Down
2 changes: 1 addition & 1 deletion src/UnifiedFFI-Tests/FFICompilerPluginTest.class.st
Expand Up @@ -16,7 +16,7 @@ FFICompilerPluginTest class >> compiler [

{ #category : #tests }
FFICompilerPluginTest >> ffiCopyString: aString to: dest [
^ self ffiCall: #(String strcpy #(String dest , String aString))
^ self ffiCall: #(String strcpy #(void* dest , String aString))
]

{ #category : #tests }
Expand Down
2 changes: 1 addition & 1 deletion src/UnifiedFFI-Tests/FFIConstantHandleTest.class.st
Expand Up @@ -37,5 +37,5 @@ FFIConstantHandleTest >> testReturn [

object := TIME_T_PTR new value: 0.
time := self getTimeReturnConstantHandle: object.
self assert: time handle equals: object value
self assert: time handle asInteger equals: object value asInteger
]
4 changes: 2 additions & 2 deletions src/UnifiedFFI-Tests/FFIExternalEnumerationTest.class.st
Expand Up @@ -15,14 +15,14 @@ FFIExternalEnumerationTest >> enumClass [
{ #category : #primitives }
FFIExternalEnumerationTest >> ffiTestCall: enumValue format: format to: buffer [
^ self
ffiCall: #( int sprintf ( ByteArray buffer, String format, FFITestEnumeration enumValue ) )
ffiCall: #( int sprintf ( void* buffer, String format, FFITestEnumeration enumValue ) )
module: LibC
]

{ #category : #primitives }
FFIExternalEnumerationTest >> ffiTestReturn: aNumber format: format to: buffer [
^ self
ffiCall: #( FFITestEnumeration sprintf ( ByteArray buffer, String format, int aNumber ) )
ffiCall: #( FFITestEnumeration sprintf ( void* buffer, String format, int aNumber ) )
module: LibC
]

Expand Down
6 changes: 6 additions & 0 deletions src/UnifiedFFI-Tests/FFIMockCalloutMethodBuilder.class.st
Expand Up @@ -23,6 +23,12 @@ FFIMockCalloutMethodBuilder >> doing: aBlockClosure [
block := aBlockClosure
]

{ #category : #compatibility }
FFIMockCalloutMethodBuilder >> extractLibrary [

^ LibC uniqueInstance
]

{ #category : #'type-resolution' }
FFIMockCalloutMethodBuilder >> ffiBindingOf: aString [

Expand Down
6 changes: 6 additions & 0 deletions src/UnifiedFFI-Tests/FFITestObject.class.st
Expand Up @@ -28,3 +28,9 @@ FFITestObject >> c [

^c
]

{ #category : #'library path' }
FFITestObject >> ffiLibraryName [

^ 'testLibrary'
]
19 changes: 14 additions & 5 deletions src/UnifiedFFI/FFICalloutMethodBuilder.class.st
Expand Up @@ -84,6 +84,19 @@ FFICalloutMethodBuilder >> createFFICalloutLiteralFromSpec: functionSpec [
^ externalFunction
]

{ #category : #'as yet unclassified' }
FFICalloutMethodBuilder >> extractLibrary [
"Obtain the library to use"
| ffiLibrary |

ffiLibrary := library ifNil: [sender receiver ffiLibrary].
ffiLibrary := ffiLibrary isClass
ifFalse: [ ffiLibrary asFFILibrary ]
ifTrue: [ ffiLibrary uniqueInstance ].

^ ffiLibrary
]

{ #category : #private }
FFICalloutMethodBuilder >> generate [
^ self generateMethodFromSpec: (self parseSignature: self signature)
Expand Down Expand Up @@ -115,11 +128,7 @@ FFICalloutMethodBuilder >> generateMethodFromSpec: functionSpec [
| ir ffiLibrary properties |
functionSpec resolveUsing: self requestor.

"Obtain the library to use"
ffiLibrary := library ifNil: [sender receiver ffiLibrary].
ffiLibrary := ffiLibrary isClass
ifFalse: [ ffiLibrary asFFILibrary ]
ifTrue: [ ffiLibrary uniqueInstance ].
ffiLibrary := self extractLibrary.

ir := IRBuilder buildIR: [ :builder | | r |

Expand Down
7 changes: 7 additions & 0 deletions src/UnifiedFFI/False.extension.st
@@ -0,0 +1,7 @@
Extension { #name : #False }

{ #category : #'*UnifiedFFI' }
False >> asInteger [

^ 0
]
7 changes: 7 additions & 0 deletions src/UnifiedFFI/True.extension.st
@@ -0,0 +1,7 @@
Extension { #name : #True }

{ #category : #'*UnifiedFFI' }
True >> asInteger [

^ 1
]

0 comments on commit 86e9ae2

Please sign in to comment.