[View] [Edit] [Lock] [References] [Attachments] [History] [Home] [Changes] [Search] [Help]
test[libc] String and Array Utilities
Run the tests, executing:
SwikiCodeRobot @> #testLibcStringAndArrayUtilities
"String Length"
((LibC current strlen: (NodeBuffer string: #hello , 0 asCharacter) json) == #hello size) ifFalse: [self error: 'Wrong result hello'].
((LibC current strlen: (NodeBuffer string: #Smalltalk , 0 asCharacter) json) == #Smalltalk size) ifFalse: [self error: 'Wrong result Smalltalk'].
"Copying Strings"
| a b |
a := NodeBuffer string: 'abcdefg', 0 asCharacter.
b := NodeBuffer string: 'qwerty', 0 asCharacter.
LibC current memcpy: a json from: b json size: 2.
(a readCString == 'qwcdefg' ) ifFalse: [self error: 'bad copy'].
(b readCString == 'qwerty' ) ifFalse: [self error: 'b should not be altered'].
"Concatenation of Strings"
| a b result|
a := NodeBuffer string: 'Small', 0 asCharacter.
b := NodeBuffer string: 'talk', 0 asCharacter.
result := LibC current strcat: a json from: b json.
(result readCString == 'Smalltalk' ) ifFalse: [self error: 'Error concatenating strings'].
"Copying Strings"
| from to result|
from := NodeBuffer string: 'abc', 0 asCharacter.
to := NodeBuffer string: 'cdef', 0 asCharacter.
result := LibC current strcpy: to json from: from json.
(result readCString == 'abc' ) ifFalse: [self error: 'Error copyingg strings'].
"Comparing Strings"
"strcmp: s1 s2: s2
returns:
a positive integer if s1 > s2,
a negative integer if s1 <s2
0 if s1 = s2. "
| stSorted cSorted|
cSorted := #(Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto)
sort: [:a :b | (LibC current strcmp: (NodeBuffer string: a, 0 asCharacter) json
s2:(NodeBuffer string: b, 0 asCharacter) json
) < 0 ] .
stSorted := #(Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto) sort.
self print: stSorted.
self print: cSorted.
cSorted = stSorted ifFalse: [ self error: 'Wrong String comparation'].
"Search in Strings using memchr"
|fileName separator extension|
fileName := 'doIt.st'.
separator := $. .
extension := LibC current memchr: (NodeBuffer string: fileName, 0 asCharacter) json c: separator asciiValue size: fileName size.
extension readCString == '.st' ifFalse: [ self error: 'Searching dot in fileName failed' ].
"Search in Strings using strchr"
|fileName separator extension|
fileName := 'doIt.st'.
separator := $. .
extension := LibC current strchr: (NodeBuffer string: fileName, 0 asCaracter) json c: separator asciiValue.
extension readCString == '.st' ifFalse: [ self error: 'Searching dot in fileName failed' ].
"Search in Strings using strrchr (from the end to the beginning) "
| url separator countryIdentification |
url := 'www.yahoo.com.ar'.
separator := $. .
countryIdentification := LibC current strrchr: (NodeBuffer string: url, 0 asCharacter) json c: separator asciiValue.
countryIdentification readCString == '.ar' ifFalse: [ self error: 'Searching dot in url failed' ].