FFI: FFI-Tests-mt.28.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.28.mcz

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

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

Name: FFI-Tests-mt.28
Author: mt
Time: 6 May 2021, 6:14:29.484981 pm
UUID: 5f1d805b-3654-e04c-85d5-efb697127cbf
Ancestors: FFI-Tests-mt.27

More tests. Favor #allocateExternal over #externalNew. No need to #zeroMemory in several tests. Complements FFI-Kernel-mt.136.

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

Item was changed:
  ----- Method: ExternalStructureTests>>test02CopyStructureFromExternal (in category 'tests - external structure') -----
  test02CopyStructureFromExternal
 
  | original copy |
+ original := heapObject := FFITestPoint2 allocateExternal.
- original := heapObject := FFITestPoint2 externalNew.
  original setX: 1 setY: 2.
 
  copy := original copy.
  self assert: copy getHandle isInternalMemory.
 
  copy setX: 3 setY: 4.
  self assert: 1@2 equals: original asPoint.
  self assert: 3@4 equals: copy asPoint.!

Item was changed:
  ----- Method: ExternalStructureTests>>test02FromToExternal (in category 'tests - external data') -----
  test02FromToExternal
  "Access a sub-range in the external data. External memory will not be copied."
 
  | points portion |
  points := heapObject := FFITestPoint2 allocateExternal: 5.
- points zeroMemory.
 
  portion := points from: 2 to: 3.
  self assert: portion getHandle isExternalAddress.
 
  portion withIndexDo: [:point :index | point setX: index+1 setY: index+1].
  self
  assert: { 0@0 . 2@2 . 3@3 . 0@0 . 0@0 }
  equals: (points collect: [:each | each asPoint]).!

Item was changed:
  ----- Method: ExternalStructureTests>>test03CopyFromExternalToInternal (in category 'tests - external data') -----
  test03CopyFromExternalToInternal
 
  | points copy |
  points := FFITestPoint2 allocateExternal: 5.
- points zeroMemory.
  self assert: points getHandle isExternalAddress.
 
  copy := points copyFrom: 2 to: 3.
  self assert: copy getHandle isInternalMemory.
 
  "We need a writer to modify internal memory."
  copy withIndexDo: [:point :index | point setX: index+1 setY: index+1].
  self deny: { 2@2 . 3@3 } equals: (copy collect: [:each | each asPoint]).
  copy writer withIndexDo: [:point :index | point setX: index+1 setY: index+1].
  self assert: { 2@2 . 3@3 } equals: (copy collect: [:each | each asPoint]).
 
  "Check that we did not touch the external memory."
  self
  assert: { 0@0 . 0@0 . 0@0 . 0@0 . 0@0 }
  equals: (points collect: [:each | each asPoint]).!

Item was changed:
  ----- Method: ExternalStructureTests>>test04AccessingInternalMemory (in category 'tests') -----
  test04AccessingInternalMemory
  "Check whether we can use a ByteArrayWriter to fill structures."
 
  | composite |
+ composite := FFITestSUfdUdSi2 allocate.
- composite := FFITestSUfdUdSi2 new.
 
  self assert: composite ~~ composite writer.
 
  self assert: 0.0 equals: composite ufd1 f1.
  composite ufd1 f1: 3.5.
  self deny: 3.5 equals: composite ufd1 f1.
  composite writer ufd1 f1: 3.5.
  self assert: 3.5 equals: composite ufd1 f1.
 
  self assert: 0 equals: composite udSii2 sii1 i1.
  composite udSii2 sii1 i1: 42.
  self deny: 42 equals: composite udSii2 sii1 i1.
  composite writer udSii2 sii1 i1: 42.
  self assert: 42 equals: composite udSii2 sii1 i1.!

Item was changed:
  ----- Method: ExternalStructureTests>>test05AccessingExternalMemory (in category 'tests') -----
  test05AccessingExternalMemory
  "Check whether we will stick to the ExternalAddress to fill structures."
 
  | composite |
+ composite := heapObject := FFITestSUfdUdSi2 allocateExternal.
- composite := heapObject := FFITestSUfdUdSi2 externalNew.
- heapObject zeroMemory.
 
  self assert: composite == composite writer.
 
  self assert: 0.0 equals: composite ufd1 f1.
  composite ufd1 f1: 3.5.
  self assert: 3.5 equals: composite ufd1 f1.
 
  self assert: 0 equals: composite udSii2 sii1 i1.
  composite udSii2 sii1 i1: 42.
  self assert: 42 equals: composite udSii2 sii1 i1.!

Item was changed:
  ----- Method: ExternalStructureTests>>test06AccessingTypeAliasForAtomic (in category 'tests') -----
  test06AccessingTypeAliasForAtomic
 
  | char |
+ char := FFITestCharAlias new.
+ self assert: 0 equals: char value asInteger.
- self should: [FFITestCharAlias new] raise: Error.
- char := FFITestCharAlias fromHandle: $C.
- self assert: $C equals: char value.
  char value: $A.
  self assert: $A equals: char value.
  char zeroMemory.
  self assert: 0 equals: char value asInteger.!

Item was changed:
  ----- Method: ExternalStructureTests>>test07AccessingArrays (in category 'tests') -----
  test07AccessingArrays
 
  | data |
+ data := FFITestSdA5i allocate.
- data := FFITestSdA5i new.
  self assert: data a5i2 first equals: 0.
  data writer a5i2 at: 1 put: 42.
  self assert: data a5i2 first equals: 42.
 
+ data := heapObject := FFITestSdA5i allocateExternal.
- data := heapObject := FFITestSdA5i externalNew.
- data zeroMemory.
  self assert: data a5i2 first equals: 0.
  data a5i2 at: 1 put: 42.
  self assert: data a5i2 first equals: 42.!

Item was changed:
  TestCase subclass: #ExternalTypeTests
+ instanceVariableNames: 'heapObject'
- instanceVariableNames: ''
  classVariableNames: ''
  poolDictionaries: 'ExternalType'
  category: 'FFI-Tests'!

Item was added:
+ ----- Method: ExternalTypeTests>>tearDown (in category 'running') -----
+ tearDown
+
+ heapObject ifNotNil: [heapObject free].!

Item was added:
+ ----- Method: ExternalTypeTests>>testAllocateAtomics (in category 'tests') -----
+ testAllocateAtomics
+
+ self should: [ExternalType void allocate] raise: Error.
+ self assert: false equals: ExternalType bool allocate.
+
+ self assert: 0 equals: ExternalType int8_t "sbyte" allocate.
+ self assert: 0 equals: ExternalType uint8_t "byte" allocate.
+
+ self assert: 0 equals: ExternalType uint16_t "ushort" allocate.
+ self assert: 0 equals: ExternalType int16_t "short" allocate.
+
+ self assert: 0 equals: ExternalType uint32_t "ulong" allocate.
+ self assert: 0 equals: ExternalType int32_t "long" allocate.
+
+ self assert: 0 equals: ExternalType uint64_t "ulonglong" allocate.
+ self assert: 0 equals: ExternalType int64_t "longlong" allocate.
+
+ self assert: Character null equals: ExternalType schar allocate.
+ self assert: Character null equals: ExternalType char allocate.
+
+ self assert: 0.0 equals: ExternalType float allocate.
+ self assert: 0.0 equals: ExternalType double allocate.!

Item was added:
+ ----- Method: ExternalTypeTests>>testAllocateAtomicsExternal (in category 'tests') -----
+ testAllocateAtomicsExternal
+
+ self should: [ExternalType void allocateExternal] raise: Error.
+ self assert: false equals: ExternalType bool allocateExternal.
+
+ self assert: 0 equals: ExternalType int8_t "sbyte" allocateExternal.
+ self assert: 0 equals: ExternalType uint8_t "byte" allocateExternal.
+
+ self assert: 0 equals: ExternalType uint16_t "ushort" allocateExternal.
+ self assert: 0 equals: ExternalType int16_t "short" allocateExternal.
+
+ self assert: 0 equals: ExternalType uint32_t "ulong" allocateExternal.
+ self assert: 0 equals: ExternalType int32_t "long" allocateExternal.
+
+ self assert: 0 equals: ExternalType uint64_t "ulonglong" allocateExternal.
+ self assert: 0 equals: ExternalType int64_t "longlong" allocateExternal.
+
+ self assert: Character null equals: ExternalType schar allocateExternal.
+ self assert: Character null equals: ExternalType char allocateExternal.
+
+ self assert: 0.0 equals: ExternalType float allocateExternal.
+ self assert: 0.0 equals: ExternalType double allocateExternal.!

Item was added:
+ ----- Method: ExternalTypeTests>>testAllocateStructs (in category 'tests') -----
+ testAllocateStructs
+
+ | struct |
+ struct := FFITestPoint2 allocate.
+ self assert: 0 equals: struct x.
+ self assert: 0 equals: struct y.
+
+ struct := FFITestSd2 allocate.
+ self assert: 0.0 equals: struct d1.
+ self assert: 0.0 equals: struct d2.
+
+ struct := FFITestSsSsf allocate.
+ self assert: 0 equals: struct s1.
+ self assert: 0 equals: struct ssf2 s1.
+ self assert: 0.0 equals: struct ssf2 f2.
+
+ struct := FFITestUfd allocate.
+ self assert: 0.0 equals: struct d1.
+ self assert: 0.0 equals: struct f1.!

Item was added:
+ ----- Method: ExternalTypeTests>>testAllocateStructsExternal (in category 'tests') -----
+ testAllocateStructsExternal
+
+ | struct |
+ struct := heapObject := FFITestPoint2 allocateExternal.
+ self assert: 0 equals: struct x.
+ self assert: 0 equals: struct y.
+
+ struct := heapObject := FFITestSd2 allocateExternal.
+ self assert: 0.0 equals: struct d1.
+ self assert: 0.0 equals: struct d2.
+
+ struct := heapObject := FFITestSsSsf allocateExternal.
+ self assert: 0 equals: struct s1.
+ self assert: 0 equals: struct ssf2 s1.
+ self assert: 0.0 equals: struct ssf2 f2.
+
+ struct := heapObject := FFITestUfd allocateExternal.
+ self assert: 0.0 equals: struct d1.
+ self assert: 0.0 equals: struct f1.!

Item was changed:
  ----- Method: FFIPluginTests>>testSumStructSUfdUdsi2 (in category 'tests - structure') -----
  testSumStructSUfdUdsi2
+ "Sum up the double parts of two unions in a struct."
- "Sum up the double parts of two unions in a struct. We have to malloc because we cannot (yet?) share parts of byte arrays between structures."
 
  | sUfdUdsi2 sum |
+ sUfdUdsi2 := heapObject := FFITestSUfdUdSi2 allocateExternal.
- sUfdUdsi2 := heapObject := FFITestSUfdUdSi2 externalNew.
  sUfdUdsi2 ufd1 d1: 123.456.
  sUfdUdsi2 udSii2 d1: 456.123.
  sum := self invoke: 'ffiTestSumSUfdUdSi2_d' with: sUfdUdsi2.
  self assert: 123.456 + 456.123 equals: sum.!

Item was changed:
  ----- Method: FFIPluginTests>>testSumStructSUfdUfi (in category 'tests - structure') -----
  testSumStructSUfdUfi
+ "Sum up the float parts of two unions in a struct."
- "Sum up the float parts of two unions in a struct. We have to malloc because we cannot (yet?) share parts of byte arrays between structures."
 
  | sUfdUdsi2 result expected |
+ sUfdUdsi2 := heapObject := FFITestSUfdUfi allocateExternal.
- sUfdUdsi2 := heapObject := FFITestSUfdUfi externalNew.
  sUfdUdsi2 ufd1 f1: 123.456.
  sUfdUdsi2 ufi2 f1: 456.123.
  result := self invoke: 'ffiTestSumSUfdUfi_f' with: sUfdUdsi2.
  expected := 123.456 + 456.123.
  self assert: (result between: expected - 0.0005 and: expected + 0.0005).!