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

test[S8FFI] NodeBuffer readCString

SwikiCodeRobot @> 377

"Instantiating a NodeBuffer from String and obtaining contents."
| str hello x contents |
(Smalltalk includesKey: #LibC) ifFalse: [ ^self error: 'Missing LibC' ].
hello := 'hello world'.
1000 timesRepeat: [:i|
	str := NodeBuffer string: hello ,0 asCharacter.
	x := str readCString.
	x = hello ifFalse: [
		self error: 'Wrong[',i,'] contents='
			,x printString,' size=',x size , '
hello=',(hello asArray collect: [:c| c asciiValue ]) , '
      x=',(x asArray collect: [:c| c asciiValue ]) 
	].
	str length - 1 = hello size ifFalse: [
		self error: 'NodeBuffer[',i,'] failed with length=',str length
	].
	x := LibC current strlen: str json.
	x = hello size ifFalse: [
		self error: 'Wrong[',i,'] result='
			,x ,' should be ',hello size
			,' contents=', str readCString printString
	].
].#ok