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

test[S8Ref] string

readCString

"should return empty string for a Buffer containing \\0"
| buf |
buf := NodeBuffer string: 0 asCharacter.
(buf readCString: 0) isEmpty ifFalse: [ self error: 'Should be empty' ]


"should return string upto \\0"
| buf |
buf := NodeBuffer string: #hello ,0 asCharacter ,#world.
(buf readCString: 0) = #hello ifFalse: [ self error: 'Should be #hello' ]


"should throw an Error when reading from the NULL pointer"
[ S8Ref null readCString
] on: Error do: [:ex| ^self print: 'ok - should throw ' ,ex toString stream nextLine ].
self error: 'Should thrown an error'


writeCString

"should write a C string (NULL terminated) to a Buffer"
| buf str |
buf := NodeBuffer size: 20.
str := 'hello world'.
buf writeCString: str.
1 to: str size do: [:i|
    (str at: i) asciiValue = (buf at: i) ifFalse: [ ^self error: 'Invalid contents at ',i ].
].
(buf at: str size) = 0 ifFalse: [ self error: 'Should be 0 terminated' ].


allocCString

"should return a new Buffer containing the given string"
| buf str |
str := 'hello world'.
buf := S8Ref current allocCString: str.
buf readCString = str ifFalse: [ self error: 'Invalid contents' ].


"should return the NULL pointer for null value"
| buf str |
buf := S8Ref current allocCString: #{null}.
buf isNull ifFalse: [ self error: 'Should be NULL' ].
buf address = 0 ifFalse: [ ^self error: 'Invalid address' ].


"should return the NULL pointer for undefined value"
| buf str |
buf := S8Ref current allocCString: #{undefined}.
buf isNull ifFalse: [ self error: 'Should be NULL' ].
buf address = 0 ifFalse: [ ^self error: 'Invalid address' ].


"should return the NULL pointer for NULL pointer buffer"
| buf str |
buf := S8Ref current allocCString: S8Ref null.
buf isNull ifFalse: [ self error: 'Should be NULL' ].
buf address = 0 ifFalse: [ ^self error: 'Invalid address' ].


CString

"should return null when given a pointer pointing to NULL"
| buf result type |
type := S8Ref type: #CString.
buf := S8Ref alloc: type.
buf writePointer: S8Ref null.
buf deref = #{null} ifFalse: [ ^self error: 'Invalid address' ].
result := S8Ref current get: S8Ref nullPointer offset: 0 type: type.
result = #{null} ifFalse: [ ^self error: 'Invalid address 2' ].


"should read a utf8 string from a Buffer"
| buf str type |
str := 'hello world'.
type := S8Ref type: #CString.
buf := S8Ref alloc: type.
buf writePointer: (NodeBuffer string: str ,0 asCharacter).
buf deref = str ifFalse: [ ^self error: 'Invalid contents' ].


"should set a Buffer as backing store"
| buf str type store |
str := 'hey!!'.
store := NodeBuffer string: str ,0 asCharacter.
type := S8Ref type: #CString.
buf := S8Ref alloc: type.
S8Ref current set: buf offset: 0 value: store.
(S8Ref current get: buf offset: 0) = str ifFalse: [ ^self error: 'Invalid contents' ].