Skip to content

Commit

Permalink
Issue #79: add ZnCharacterStreamTests >> test8BitEncodingStreamPositi…
Browse files Browse the repository at this point in the history
…on test for completeness, reorder the samples in ZnCharacterStreamTests >> testUpToAll and fix some regressions that snuck in while working on utf8 variant
  • Loading branch information
dalehenrich committed Dec 12, 2022
1 parent 69e837d commit a8bd0a0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
18 changes: 10 additions & 8 deletions src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,12 @@ ZnBufferedReadStream >> uint8 [
{ #category : 'accessing' }
ZnBufferedReadStream >> upTo: value [
"Read upto but not including value and return them as a collection.
If value is not found, return the entire contents of the stream.
This could be further optimzed."
If value is not found, return the entire contents of the stream."

^ self collectionSpecies
streamContents: [ :writeStream |
[ self atEnd or: [ (self peek) = value ] ] whileFalse: [
writeStream nextPut: self next ] ]
streamContents: [ :writeStream | | ch |
[ self atEnd or: [ (ch := self next) = value ] ] whileFalse: [
writeStream nextPut: ch ] ]
]

{ #category : 'accessing' }
Expand All @@ -421,9 +420,12 @@ aCollection isEmpty ifTrue: [ ^aCollection ].
startPos := self position.
"upTo: will stop before aCollection first"
x := self upTo: aCollection first.
(self atEnd or: [aCollection size = 1 ])
ifTrue: [ ^ x ].
self next. "move past the matching char from upTo:"
self atEnd
ifTrue: [
aCollection size <= 1
ifTrue: [ ^ x ].
self position: startPos.
^ self upToEnd].
2 to: aCollection size do: [:i | | y |
(y := self peek) = (aCollection at: i)
ifTrue: [ self next ]
Expand Down
80 changes: 52 additions & 28 deletions src/Zinc-Character-Encoding-Tests/ZnCharacterStreamTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ ZnCharacterStreamTests >> testReadStream [
self assert: stream peek isNil
]

{ #category : 'testing' }
ZnCharacterStreamTests >> testReadUpTo [
| string |
string := '0123456789'.
{(self eightBitReadStreamOn: string).
(self utf8ReadStreamOn: string)}
do: [ :stream |
self assert: (stream upTo: $5) equals: '01234'.
self assert: stream upToEnd equals: '6789'.
self assert: stream atEnd ]
]

{ #category : 'testing' }
ZnCharacterStreamTests >> testSimpleUTF8ReadStream [
| string bytes stream |
Expand All @@ -136,18 +148,6 @@ ZnCharacterStreamTests >> testSimpleUTF8WriteStream [
equals: bytes asByteArray
]

{ #category : 'testing' }
ZnCharacterStreamTests >> testUtf8EncodingStreamPosition [
| string bytes stream res |
string := 'eißendeße'.
bytes := ZnUTF8Encoder new encodeString: string.
stream := (ZnCharacterReadStream on: bytes readStreamPortable).
res := stream next; next; next.
self assert: res equals: $ß.
self assert: stream position equals: 3.

]

{ #category : 'testing' }
ZnCharacterStreamTests >> testUpTo [
| char1 char2 string1 string2 |
Expand Down Expand Up @@ -206,6 +206,22 @@ ZnCharacterStreamTests >> testUpTo [
ZnCharacterStreamTests >> testUpToAll [
#(
('' '' '')
('a' '' '')
('a' 'a' '')
('a' 'b' 'a')
('ab' '' '')
('ab' 'a' '')
('ab' 'b' 'a')
('ab' 'c' 'ab')
('ab' 'ab' '')
('abc' '' '')
('abc' 'a' '')
('abc' 'b' 'a')
('abc' 'c' 'ab')
('abc' 'd' 'abc')
('abc' 'ab' '')
('abc' 'bc' 'a')
('abc' 'cd' 'abc')
('' 'ß' '')
('' 'ße' '')
('ß' '' '')
Expand All @@ -228,22 +244,6 @@ ZnCharacterStreamTests >> testUpToAll [
('eißen' 'ßend' 'eißen')
('eißendeße' 'ße' 'ei')
('abcdefgh' 'cd' 'ab')
('a' '' '')
('a' 'a' '')
('a' 'b' 'a')
('ab' '' '')
('ab' 'a' '')
('ab' 'b' 'a')
('ab' 'c' 'ab')
('ab' 'ab' '')
('abc' '' '')
('abc' 'a' '')
('abc' 'b' 'a')
('abc' 'c' 'ab')
('abc' 'd' 'abc')
('abc' 'ab' '')
('abc' 'bc' 'a')
('abc' 'cd' 'abc')
) do: [ :array | self assertUpToAll: array ]
]

Expand Down Expand Up @@ -272,6 +272,30 @@ ZnCharacterStreamTests >> testUpToAllTwice [

]

{ #category : 'testing' }
ZnCharacterStreamTests >> test8BitEncodingStreamPosition [
| string bytes stream res |
string := 'eiSendeSe'.
bytes := Zn8BITEncoder new encodeString: string.
stream := (ZnCharacterReadStream on: bytes readStreamPortable).
res := stream next; next; next.
self assert: res equals: $S.
self assert: stream position equals: 3.

]

{ #category : 'testing' }
ZnCharacterStreamTests >> testUtf8EncodingStreamPosition [
| string bytes stream res |
string := 'eißendeße'.
bytes := ZnUTF8Encoder new encodeString: string.
stream := (ZnCharacterReadStream on: bytes readStreamPortable).
res := stream next; next; next.
self assert: res equals: $ß.
self assert: stream position equals: 3.

]

{ #category : 'testing' }
ZnCharacterStreamTests >> testUTF8ReadStreamReadInto [
| string bytes stream buffer |
Expand Down

0 comments on commit a8bd0a0

Please sign in to comment.