FFI: FFI-Tests-mt.34.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FFI: FFI-Tests-mt.34.mcz

commits-2
Marcel Taeumel uploaded a new version of FFI-Tests to project FFI:
http://source.squeak.org/FFI/FFI-Tests-mt.34.mcz

==================== Summary ====================

Name: FFI-Tests-mt.34
Author: mt
Time: 15 May 2021, 2:57:58.142282 pm
UUID: 88a07f6a-4b02-d042-a0e5-e6e3613789a7
Ancestors: FFI-Tests-mt.33

More tests :-) And #test03GlobalVariable actually fails to meet the requirements when allocated externally.

=============== Diff against FFI-Tests-mt.33 ===============

Item was changed:
  ----- Method: FFIAllocateExternalTests>>expectedFailures (in category 'failures') -----
  expectedFailures
 
+ ^ (super expectedFailures
+
+ copyWithoutAll: #(
- ^ super expectedFailures copyWithoutAll: #(
  test04LinkedList "Storing pointers works fine."
+ )), #(
+ test03GlobalVariable "Atomic values in an alias container will be fetched immediately. Hmm..."
  )!

Item was added:
+ ----- Method: FFIAllocateExternalTests>>test03GlobalVariable (in category 'tests') -----
+ test03GlobalVariable
+ "If you happen to have to address to a global variable you can use a type alias."
+ | global |
+ global := self allocate: FFITestAliasForInt32.
+ self assert: global getHandle isExternalAddress.
+ self assert: global externalType isPointerType.
+ self assert: 0 equals: global value.
+ global value: 42.
+ self assert: 42 equals: global value.!

Item was changed:
  ----- Method: FFIAllocateTests>>test03GlobalVariable (in category 'tests') -----
  test03GlobalVariable
  "If you happen to have to address to a global variable you can use a type alias or just external data for it. See ExternalObject class >> #fromHandle:."
  | global |
  global := self allocate: FFITestAliasForInt32.
  self assert: 0 equals: global value.
  global value: 42.
  self assert: 42 equals: global value.!

Item was changed:
  ----- Method: FFIAllocateTests>>test05ArrayFromCString (in category 'tests - array') -----
  test05ArrayFromCString
 
  | data |
 
  ExternalData allowDetectForUnknownSizeDuring: [
  data := self allocate: ExternalType char size: 4.
+ data setContentType: ExternalType byte.
+ data setSize: nil.
- data setType: ExternalType byte.
  self assert: data size isNil.
 
  #[65 66 67 0] withIndexDo: [:byte :index | data at: index put: byte].
+ data setContentType: ExternalType char.
- data setType: ExternalType char.
  self assert: 'ABC' equals: data fromCString.
 
  data := self allocate: ExternalType char size: 9.
  data setType: ExternalType byte.
  self assert: data size isNil.
 
  #[65 66 67 0 68 69 70 0 0] withIndexDo: [:byte :index | data at: index put: byte].
  data setType: ExternalType char.
  self assert:#('ABC' 'DEF') equals: data fromCStrings].!

Item was added:
+ ----- Method: FFIAllocateTests>>test07ArraySetSize (in category 'tests - array') -----
+ test07ArraySetSize
+
+ | data |
+ data := self allocate: ExternalType byte size: 10.
+
+ data setSize: 2. "limit access"
+ self assert: ExternalType byte identical: data contentType.
+ self assert: 2 equals: data size.
+ self shouldnt: [data at: 2] raise: Error.
+ self should: [data at: 3] raise: Error.
+
+ data setSize: 5. "move limit"
+ self assert: ExternalType byte identical: data contentType.
+ self assert: 5 equals: data size.
+ self shouldnt: [data at: 5] raise: Error.
+ self should: [data at: 6] raise: Error.
+
+ data setSize: nil. "ignore size"
+ self assert: ExternalType byte identical: data contentType.
+ self shouldnt: [data at: 3] raise: Error.
+ self shouldnt: [data at: 6] raise: Error.
+ self shouldnt: [data at: 10] raise: Error.!

Item was added:
+ ----- Method: FFIAllocateTests>>test08ArraySetContentType (in category 'tests - array') -----
+ test08ArraySetContentType
+
+ | data |
+ data := self allocate: ExternalType byte size: 1.
+ data at: 1 put: 65.
+
+ self assert: 65 equals: data first.
+ data setContentType: ExternalType char.
+ self assert: $A equals: data first.!

Item was added:
+ ----- Method: FFIPluginTests>>testReturnPointerAlias (in category 'tests - type alias') -----
+ testReturnPointerAlias
+ "Check the handle of a returned alias-to-pointer type. Should be an external address."
+
+ | pt1 pt2 pt3 |
+ pt1 := FFITestPoint4 new.
+ pt1 x: 1. pt1 y: 2. pt1 z: 3. pt1 w: 4.
+ pt2 := FFITestPoint4 new.
+ pt2 x: 5. pt2 y: 6. pt2 z: 7. pt2 w: 8.
+ pt3 := heapObject := FFITestLibrary ffiTestAliasForPointerResult: pt1 with: pt2.
+
+ self assert: pt3 getHandle isExternalAddress.
+ self assert: pt3 externalType isPointerType.
+
+ self assert: pt3 x = 6.
+ self assert: pt3 y = 8.
+ self assert: pt3 z = 10.
+ self assert: pt3 w = 12.!

Item was added:
+ ExternalTypeAlias subclass: #FFITestAliasForPoint4Pointer
+ instanceVariableNames: ''
+ classVariableNames: ''
+ poolDictionaries: ''
+ category: 'FFI-Tests-Fixtures'!

Item was added:
+ ----- Method: FFITestAliasForPoint4Pointer class>>originalTypeName (in category 'type alias') -----
+ originalTypeName
+
+ ^ 'FFITestPoint4*'!

Item was added:
+ ----- Method: FFITestLibrary class>>ffiTestAliasForPointerResult:with: (in category 'experiments') -----
+ ffiTestAliasForPointerResult: pt1 with: pt2
+ "Allocates the result. Needs to be free'd after calling."
+ <cdecl: FFITestAliasForPoint4Pointer 'ffiTestPointers' (FFITestPoint4* FFITestPoint4*) module:'SqueakFFIPrims'>
+ ^self externalCallFailed!

Item was changed:
+ ----- Method: FFITestLibrary class>>ffiTestArrayResultWith:with: (in category 'experiments') -----
- ----- Method: FFITestLibrary class>>ffiTestArrayResultWith:with: (in category 'mocks') -----
  ffiTestArrayResultWith: pt1 with: pt2
  "Allocates the result. Needs to be free'd after calling."
  <cdecl: FFITestPoint4[] 'ffiTestPointers' (FFITestPoint4* FFITestPoint4*) module:'SqueakFFIPrims'>
  ^self externalCallFailed!

Item was changed:
+ ----- Method: FFITestLibrary class>>ffiTestArrayResultWithString: (in category 'experiments') -----
- ----- Method: FFITestLibrary class>>ffiTestArrayResultWithString: (in category 'mocks') -----
  ffiTestArrayResultWithString: aString
  "
  FFITestLibrary ffiTestArrayResultWithString: 'Hello Squeak'.
  "
  <cdecl: char[] 'ffiPrintString' (char *) module:'SqueakFFIPrims'>
  ^self externalCallFailed!

Item was changed:
+ ----- Method: FFITestLibrary class>>ffiTestArrayType (in category 'experiments') -----
- ----- Method: FFITestLibrary class>>ffiTestArrayType (in category 'mocks') -----
  ffiTestArrayType
  "Just a mock. Not sure whether there will ever be call signatures using array types ... isn't this pass-by-pointer anyway?"
 
  <cdecl: char[5] 'ffiTestArrayType' (float[5] FFITestPoint2[10] int[1]) module: 'SqueakFFIPrims'>
  ^ self externalCallFailed !

Item was changed:
+ ----- Method: FFITestLibrary class>>ffiTestVoid (in category 'experiments') -----
- ----- Method: FFITestLibrary class>>ffiTestVoid (in category 'mocks') -----
  ffiTestVoid
  "Note that ffiTestVoid does exist in the module."
  <cdecl: void 'ffiTestVoid' (void) module:'SqueakFFIPrims'>
  ^self externalCallFailed!

Item was changed:
+ ----- Method: FFITestLibrary class>>ffiTestVoidPointer (in category 'experiments') -----
- ----- Method: FFITestLibrary class>>ffiTestVoidPointer (in category 'mocks') -----
  ffiTestVoidPointer
  "Note that ffiTestVoidPointer does exist in the module."
  <cdecl: void* 'ffiTestVoidPointer' (void*) module:'SqueakFFIPrims'>
  ^self externalCallFailed!