Untitled diff

Created Diff never expires
The two files are identical
There is no difference to show between these two files
0 removals
348 lines
0 additions
348 lines
"
"
SUnit tests for MultiByteFileStream
SUnit tests for MultiByteFileStream


"
"
Class {
Class {
#name : #MultiByteFileStreamTest,
#name : #MultiByteFileStreamTest,
#superclass : #TestCase,
#superclass : #TestCase,
#category : #Deprecated70
#category : #Deprecated70
}
}


{ #category : #support }
{ #category : #support }
MultiByteFileStreamTest >> chunkString [
MultiByteFileStreamTest >> chunkString [
"A chunk larger than buffer (currently 2048), but not long enough to fill it completely again. Therefore, buffer should contain some elements at end that are not part of the real buffer"
"A chunk larger than buffer (currently 2048), but not long enough to fill it completely again. Therefore, buffer should contain some elements at end that are not part of the real buffer"
| smallChunk chunkString |
| smallChunk chunkString |
chunkString :=
chunkString :=
String new: (MultiByteFileStream new ascii enableReadBuffering braceArray size * 1.3) floor.
String new: (MultiByteFileStream new ascii enableReadBuffering braceArray size * 1.3) floor.
smallChunk := 'ASDFASDFASDFASDFasdfasdfasdfasdfQWERQWERQWERqwerqwerqwer! !'.
smallChunk := 'ASDFASDFASDFASDFasdfasdfasdfasdfQWERQWERQWERqwerqwerqwer! !'.
1 to: chunkString size by: smallChunk size do: [:ix |
1 to: chunkString size by: smallChunk size do: [:ix |
chunkString replaceFrom: ix to: (ix + smallChunk size -1 min: chunkString size) with: smallChunk startingAt: 1].
chunkString replaceFrom: ix to: (ix + smallChunk size -1 min: chunkString size) with: smallChunk startingAt: 1].
^chunkString
^chunkString
]
]


{ #category : #accessing }
{ #category : #accessing }
MultiByteFileStreamTest >> fileOutTestClass [
MultiByteFileStreamTest >> fileOutTestClass [
^ MultiByteFileStreamFileOutTestClass
^ MultiByteFileStreamFileOutTestClass
]
]


{ #category : #support }
{ #category : #support }
MultiByteFileStreamTest >> lineEndTestFile [
MultiByteFileStreamTest >> lineEndTestFile [
^'lineEndTesting.txt'
^'lineEndTesting.txt'
]
]


{ #category : #running }
{ #category : #running }
MultiByteFileStreamTest >> tearDown [
MultiByteFileStreamTest >> tearDown [
'foobug6933' asFileReference ensureDelete.
'foobug6933' asFileReference ensureDelete.
self lineEndTestFile asFileReference ensureDelete.
self lineEndTestFile asFileReference ensureDelete.
super tearDown
super tearDown
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testBasicChunk [
MultiByteFileStreamTest >> testBasicChunk [
| internalStream chunkKey |
| internalStream chunkKey |
internalStream := ChunkReadStream on: self chunkString readStream.
internalStream := ChunkReadStream on: self chunkString readStream.
chunkKey := OrderedCollection new.
chunkKey := OrderedCollection new.
[internalStream atEnd] whileFalse:
[internalStream atEnd] whileFalse:
[ | chunk |
[ | chunk |
chunk := internalStream next.
chunk := internalStream next.
chunkKey add: {internalStream position. chunk size. chunk}].
chunkKey add: {internalStream position. chunk size. chunk}].
[self writeChunkToFile.
[self writeChunkToFile.
(File named: 'chunkTest.txt') readStreamDo: [:fileStream | | stream |
(File named: 'chunkTest.txt') readStreamDo: [:fileStream | | stream |
stream := ChunkReadStream on: (ZnCharacterReadStream on: fileStream).
stream := ChunkReadStream on: (ZnCharacterReadStream on: fileStream).
1 to: chunkKey size do: [:ix | |chunk|
1 to: chunkKey size do: [:ix | |chunk|
chunk := stream next.
chunk := stream next.
self assert: (chunkKey at: ix) second equals: chunk size.
self assert: (chunkKey at: ix) second equals: chunk size.
self assert: (chunkKey at: ix) last equals: chunk.]].
self assert: (chunkKey at: ix) last equals: chunk.]].
] ensure: [ | f |
] ensure: [ | f |
f := File named: 'chunkTest.txt'.
f := File named: 'chunkTest.txt'.
f exists ifTrue: [ f delete ]]
f exists ifTrue: [ f delete ]]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testBinaryUpTo [
MultiByteFileStreamTest >> testBinaryUpTo [
"This is a non regression test for bug http://bugs.squeak.org/view.php?id=6933"
"This is a non regression test for bug http://bugs.squeak.org/view.php?id=6933"
| foo fileName |
| foo fileName |
fileName := 'foobug6933'.
fileName := 'foobug6933'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: fileName
forceNewFileNamed: fileName
do: [ :stream | stream binary.
do: [ :stream | stream binary.
stream nextPutAll: #[1 2 3 4] ].
stream nextPutAll: #[1 2 3 4] ].


foo := MultiByteFileStream oldFileNamed: fileName.
foo := MultiByteFileStream oldFileNamed: fileName.
[foo binary.
[foo binary.
self assert: (foo upTo: 3) = #[1 2]
self assert: (foo upTo: 3) = #[1 2]
] ensure: [
] ensure: [
foo close.
foo close.
fileName asFileReference delete].
fileName asFileReference delete].
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testFileOutNonAscii [
MultiByteFileStreamTest >> testFileOutNonAscii [
"My test class, class side, contains a method with non-ascii characters,
"My test class, class side, contains a method with non-ascii characters,
fileOut used to fail due to #backChunk problems"
fileOut used to fail due to #backChunk problems"
(self fileOutTestClass name asString, '.st') asFileReference ensureDelete.
(self fileOutTestClass name asString, '.st') asFileReference ensureDelete.
self fileOutTestClass fileOut
self fileOutTestClass fileOut
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testLineEndingsWith: anEncodingName [
MultiByteFileStreamTest >> testLineEndingsWith: anEncodingName [
| byteMap |
| byteMap |
"Make a map of different endings, and the expected byte pattern when three cr's are added using it"
"Make a map of different endings, and the expected byte pattern when three cr's are added using it"
byteMap := Dictionary new.
byteMap := Dictionary new.
byteMap at: #cr put: #[13 13 13];
byteMap at: #cr put: #[13 13 13];
at: #lf put: #[10 10 10];
at: #lf put: #[10 10 10];
at: #crlf put: #[13 10 13 10 13 10].
at: #crlf put: #[13 10 13 10 13 10].
byteMap keysDo: [:lineEnding |
byteMap keysDo: [:lineEnding |
FileStream forceNewFileNamed: self lineEndTestFile do: [:stream |
FileStream forceNewFileNamed: self lineEndTestFile do: [:stream |
stream
stream
lineEndConvention: lineEnding;
lineEndConvention: lineEnding;
converter: (TextConverter newForEncoding: anEncodingName).
converter: (TextConverter newForEncoding: anEncodingName).
"Test 3 cases: Use of nextPut, nextPutAll:, and nextPutAll with WideStrings"
"Test 3 cases: Use of nextPut, nextPutAll:, and nextPutAll with WideStrings"
stream nextPut: Character cr;
stream nextPut: Character cr;
nextPutAll: Character cr asString;
nextPutAll: Character cr asString;
nextPutAll: Character cr asString asWideString.].
nextPutAll: Character cr asString asWideString.].
FileStream oldFileNamed: self lineEndTestFile do: [:stream | | bytes expected|
FileStream oldFileNamed: self lineEndTestFile do: [:stream | | bytes expected|
bytes := stream binary; contents.
bytes := stream binary; contents.
self assert: bytes equals: (byteMap at: lineEnding)]]
self assert: bytes equals: (byteMap at: lineEnding)]]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testNext [
MultiByteFileStreamTest >> testNext [
| testString filename |
| testString filename |
testString := 'éèàôüößäóñíá'.
testString := 'éèàôüößäóñíá'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream peek = $é ).
self assert: (stream peek = $é ).
self assert: (stream next = $é ).
self assert: (stream next = $é ).
self assert: (stream next = $è ).
self assert: (stream next = $è ).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testNextMatchAll [
MultiByteFileStreamTest >> testNextMatchAll [
| testString filename |
| testString filename |
testString := 'é è à ô ü ö ß ä ó ñ í á'.
testString := 'é è à ô ü ö ß ä ó ñ í á'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream nextMatchAll: testString).
self assert: (stream nextMatchAll: testString).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testNextPut [
MultiByteFileStreamTest >> testNextPut [
| filename |
| filename |
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPut: $1;
nextPut: $1;
nextPut: $a;
nextPut: $a;
nextPut: $2;
nextPut: $2;
nextPut: $b ;
nextPut: $b ;
crlf].
crlf].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream nextMatchAll: '1a2b').
self assert: (stream nextMatchAll: '1a2b').
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testNextPutAll [
MultiByteFileStreamTest >> testNextPutAll [
| testString filename |
| testString filename |
testString := '123abcd'.
testString := '123abcd'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: '123';
nextPutAll: '123';
nextPutAll: 'abcd';
nextPutAll: 'abcd';
crlf].
crlf].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream nextMatchAll: testString).
self assert: (stream nextMatchAll: testString).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testNextPutAllStartingAt [
MultiByteFileStreamTest >> testNextPutAllStartingAt [
| testString filename |
| testString filename |
testString := 'éèàôüößäóñíá'.
testString := 'éèàôüößäóñíá'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
next: 3 putAll: testString startingAt: 3;
next: 3 putAll: testString startingAt: 3;
crlf].
crlf].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream nextMatchAll: 'àôü' ).
self assert: (stream nextMatchAll: 'àôü' ).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testPeek [
MultiByteFileStreamTest >> testPeek [
| testString filename |
| testString filename |
testString := 'é è à ô ü ö ß ä ó ñ í á'.
testString := 'é è à ô ü ö ß ä ó ñ í á'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream peek = $é ).
self assert: (stream peek = $é ).
self assert: (stream next = $é ).
self assert: (stream next = $é ).
stream upToEnd.
stream upToEnd.
self assert: (stream peek = nil ).
self assert: (stream peek = nil ).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testPeekFor [
MultiByteFileStreamTest >> testPeekFor [
| testString filename |
| testString filename |
testString := 'éèàôüößäóñíá'.
testString := 'éèàôüößäóñíá'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream peekFor: $é ).
self assert: (stream peekFor: $é ).
self assert: (stream peekFor: $è ).
self assert: (stream peekFor: $è ).
self deny: (stream peekFor: $a). "If peekFor fails, it doesn't advance position"
self deny: (stream peekFor: $a). "If peekFor fails, it doesn't advance position"
self assert: (stream peekFor: $à)
self assert: (stream peekFor: $à)
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testReadIntoStartingAtCount [
MultiByteFileStreamTest >> testReadIntoStartingAtCount [
| testString filename buffer |
| testString filename buffer |
testString := 'élève en Français'.
testString := 'élève en Français'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename asFileReference ensureDelete.
filename asFileReference ensureDelete.
filename asFileReference
filename asFileReference
writeStreamDo: [ :stream |
writeStreamDo: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
buffer := String new: testString size.
buffer := String new: testString size.
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream readInto: buffer startingAt: 1 count: testString size) equals: testString size.
self assert: (stream readInto: buffer startingAt: 1 count: testString size) equals: testString size.
self assert: buffer equals: testString ] ]
self assert: buffer equals: testString ] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testReset [
MultiByteFileStreamTest >> testReset [
| testString filename |
| testString filename |
testString := 'éèàôüößäóñíá'.
testString := 'éèàôüößäóñíá'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString;
nextPutAll: testString;
crlf ].
crlf ].
[
[
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
self assert: (stream peekFor: $é ).
self assert: (stream peekFor: $é ).
self assert: (stream peekFor: $è ).
self assert: (stream peekFor: $è ).
stream reset.
stream reset.
self assert: (stream peekFor: $é ).
self assert: (stream peekFor: $é ).
] ]
] ]
ensure: [ filename asFileReference ensureDelete ]
ensure: [ filename asFileReference ensureDelete ]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testUpToAnyOfDo [
MultiByteFileStreamTest >> testUpToAnyOfDo [
| testString filename str1 ch1 ch2|
| testString filename str1 ch1 ch2|
testString := 'élève en Français'.
testString := 'élève en Français'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString. ].
nextPutAll: testString. ].
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
str1 := stream upToAnyOf: 'd e n F' do: [ :c | ch1 := c].
str1 := stream upToAnyOf: 'd e n F' do: [ :c | ch1 := c].
self assert: (ch1 = $e).
self assert: (ch1 = $e).
self assert: (str1 = 'élèv' ).
self assert: (str1 = 'élèv' ).
str1 := stream upToAnyOf: 'xyz' do: [ :c | ch2 := c ].
str1 := stream upToAnyOf: 'xyz' do: [ :c | ch2 := c ].
self assert: (str1 = ' en Français').
self assert: (str1 = ' en Français').
self assert: (ch2 isNil ).
self assert: (ch2 isNil ).
]
]
]
]


{ #category : #testing }
{ #category : #testing }
MultiByteFileStreamTest >> testUpToEnd [
MultiByteFileStreamTest >> testUpToEnd [
| testString filename |
| testString filename |
testString := 'élève en Français'.
testString := 'élève en Français'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
filename := 'test-file-' , 99 atRandom printString , '.txt'.
MultiByteFileStream
MultiByteFileStream
forceNewFileNamed: filename
forceNewFileNamed: filename
do: [ :stream |
do: [ :stream |
stream
stream
nextPutAll: testString .
nextPutAll: testString .
].
].
filename asFileReference
filename asFileReference
readStreamDo: [ :stream |
readStreamDo: [ :stream |
| str1 |
| str1 |
self assert: ( stream peek = $é ).
self assert: ( stream peek = $é ).
self assert: ( (stream next: 5) = 'élève' ).
self assert: ( (stream next: 5) = 'élève' ).
str1 := stream upToEnd .
str1 := stream upToEnd .
self assert: ( str1 = ' en Français') .
self assert: ( str1 = ' en Français') .
]
]
]
]


{ #category : #support }
{ #category : #support }
MultiByteFileStreamTest >> writeChunkToFile [
MultiByteFileStreamTest >> writeChunkToFile [
(File openAndTruncateForWriteFileNamed: 'chunkTest.txt')
(File openAndTruncateForWriteFileNamed: 'chunkTest.txt')
nextPutAll: self chunkString;
nextPutAll: self chunkString;
close
close
]
]