[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[S8FFI] library

SwikiCodeRobot @> #testS8FFILibrary


"Native library should be a function"
(self typeOf: (S8FFI current functionNamed: #Library)) = #function ifFalse: [
    self error: 'Invalid type of Library'
].

"Should produce simple instance"
(self typeOf: (S8FFI current functionNamed: #Library) value) = #object ifFalse: [
    self error: 'Invalid instance of Library'
].

"Name can be nil"
| result |
result := NodeJS process platform = #win32
	ifTrue:  [ S8FFI current library: nil functions: (#uv_fs_open -> #(#void #(#char))) lib: nil  ]
	ifFalse: [ S8FFI current library: nil functions: (#printf -> #(#void #(#char))) lib: nil  ].
result isNil ifTrue: [ self error: 'Library missing' ].
self print: JS @> result handle

"Should accept a lib name as the first argument"
| name result |
name := NodeJS process platform = #win32 ifTrue:  [ #msvcrt  ] ifFalse: [ #libm ].
result := S8FFI current library: name functions: (#ceil -> #(#double #(#double))) lib: nil.
result isNil ifTrue: [ self error: 'Library missing' ].
"self print: JS @> result handle."
(result handle #ceil: 1.1 ) = 2 ifFalse: [ self error: 'Invalid result' ].

"should accept a lib name with file extension"
| name result |
name := NodeJS process platform = #win32 ifTrue:  [ 'msvcrt.dll'  ] ifFalse: [ 'libm' ,S8FFI current libExt ].
result := S8FFI current library: name functions: (#ceil -> #(#double #(#double))) lib: nil.
result isNil ifTrue: [ self error: 'Library missing' ].
(result handle #ceil: 100.91) = 101 ifFalse: [
	self error: 'Invalid result'
]. 

"should throw when an invalid function name is used"
| result |
[	result := S8FFI current library: nil functions: (#doesNotExist__ -> #(#void #())) lib: nil.
	result isNil ifTrue: [ self error: 'Library is nil' ].
] on: Error do: [:ex| ^self print: 'ok - ' ,ex toString stream nextLine ].
self print: 'result(',result toString,')=', (JS @> result handle).
self error: 'An error must be thrown'

"should work with strcpy and a 128 length string"
| name lib zeros buf result |
NodeJS process platform = #linux ifTrue:  [ ^#ok ].
name := NodeJS process platform = #win32 ifTrue:  [ 'msvcrt.dll'  ] ifFalse: [ nil ].
{' zeros = Array(128 + 1).join("0"); '}.
buf := NodeBuffer size: 256.
lib := S8FFI current library: name functions: (#strcpy -> #(char #(char string))) lib: nil.
lib isNil ifTrue: [ self error: 'Library missing' ].
(lib handle basicAt: #strcpy) value: buf handle value: zeros.
buf readCString = zeros ifFalse: [ self error: 'Invalid result' ]

"should work with strcpy and a 2k length string"
| name lib zeros buf result |
NodeJS process platform = #linux ifTrue:  [ ^#ok ].
name := NodeJS process platform = #win32 ifTrue:  [ 'msvcrt.dll'  ] ifFalse: [ nil ].
{' zeros = Array(2e3 + 1).join("0"); '}.
buf := NodeBuffer size: 4096.
lib := S8FFI current library: name functions: (#strcpy -> #(char #(char string))) lib: nil.
lib isNil ifTrue: [ self error: 'Library missing' ].
(lib handle basicAt: #strcpy) value: buf handle value: zeros.
buf readCString = zeros ifFalse: [ self error: 'Invalid result' ]


"Win32: should work with GetTimeOfDay and a FILETIME Struct pointer"
| fileTime lib uint32 result | 
NodeJS process platform = #win32 ifFalse: [ ^#ok ].
uint32 := S8Ref type: #uint32.
fileTime := S8Struct new: (
	  #dwLowDateTime -> uint32
	,(#dwHighDateTime -> uint32)).
lib := S8FFI current library: #kernel32 functions: #(GetSystemTimeAsFileTime  #(void #(pointer))) lib: nil.
result := (lib handle basicAt: #GetSystemTimeAsFileTime) value: fileTime ref.

"Other: should work with gettimeofday and a timeval Struct pointer"
| timeval lib long result timevalPtr timezonePtr  | 
NodeJS process platform = #win32 ifTrue: [ ^#ok ].
long := S8Ref type: #long.
timeval := S8Struct type: (#tv_sec -> long ,(#tv_usec -> long)).
timevalPtr := S8Ref refType: timeval.
timezonePtr := S8Ref refType: (S8Ref type: #void).
lib := S8FFI current library: nil functions: (Array with: #gettimeofday with: (Array with: timevalPtr with: timezonePtr)) lib: nil.
result := (lib handle basicAt: #gettimeofday) value: timeval ref json value: nil json.
DateTime now asNumber // 1000 = timevalPtr#tv_sec