RegressionTests__FileOpenTest.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 18:53:03 +0200
changeset 2327 bf482d49aeaf
parent 1447 2351db93aa5b
child 1500 d406a10b2965
permissions -rw-r--r--
#QUALITY by exept class: RegressionTests::StringTests added: #test82c_expanding

"{ Package: 'stx:goodies/regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#FileOpenTest
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-Streams'
!


!FileOpenTest methodsFor:'tests'!

createWithSize10:fileName
    "helper"

    (fileName asFilename exists) ifTrue:[
	fileName asFilename delete
    ].
    fileName asFilename writeStream
	nextPutAll:'1234567890';
	close.
    self assert:(fileName asFilename exists).
    self assert:(fileName asFilename fileSize == 10).

    "
     self new createWithSize10:'xxx1'
    "
!

testCreate1
    "create a new file (initially not existing)"

    |h|

    ('xxx1' asFilename exists) ifTrue:[
	'xxx1' asFilename delete
    ].

    h := OperatingSystem createFileForReadWrite:'xxx1'.
    self assert:(h notNil).
    self assert:(h isValid).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address ~~ 0).
    ].

    h close.
    self assert:(h isValid not).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address == 0).
    ].

    self assert:('xxx1' asFilename exists).
    self assert:('xxx1' asFilename fileSize == 0).

    'xxx1' asFilename delete

    "
     self new testCreate1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testCreate2
    "create a file; checks if the file is truncated if already existing"

    |h|

    self createWithSize10:'xxx1'.


    h := OperatingSystem createFileForReadWrite:'xxx1'.
    self assert:(h notNil).
    self assert:(h isValid).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address ~~ 0).
    ].

    h close.
    self assert:(h isValid not).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address == 0).
    ].

    self assert:('xxx1' asFilename exists).
    self assert:('xxx1' asFilename fileSize == 0).

    "
     self new testCreate2
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testFailCreate1
    "try to create an uncreatable new file (directory not existing)"

    |fn|

    fn := (('.' asFilename construct:'foo')  construct:'bar') construct:'xxx1'.
    self
	should:[
	    OperatingSystem createFileForReadWrite:fn name]
	raise:(OSErrorHolder nonexistentSignal).

    "/ raised signal should be a child of inaccessibleSignal
    self
	should:[
	    OperatingSystem createFileForReadWrite:fn name]
	raise:(OSErrorHolder nonexistentSignal ).

    "/ raised signal should be a child of OsError
    self
	should:[
	    OperatingSystem createFileForReadWrite:fn name]
	raise:(OsError ).

    "
     self new testFailCreate1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testFailOpen1
    "open a non-existing file; must fail"

    |h|

    'xxx2' asFilename delete.

    self
	should:[
		h := OperatingSystem openFileForRead:'xxx2'.]
	raise:(OSErrorHolder nonexistentSignal).


    "
     self new testFailOpen1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testFailOpen2
    "open a non-existing file for writing; must fail"

    |h|

    'xxx2' asFilename delete.

    self
	should:[
		h := OperatingSystem openFileForWrite:'xxx2'.]
	raise:(OSErrorHolder nonexistentSignal).


    "
     self new testFailOpen2
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testOpen1
    "open an existing file for reading"

    |h|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.
    self assert:(h notNil).
    self assert:(h isValid).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address ~~ 0).
    ].

    h close.
    self assert:(h isValid not).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address == 0).
    ].

    self assert:('xxx2' asFilename fileSize == 10).   "/ should not change size
    'xxx2' asFilename delete.

    "
     self new testOpen1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testOpen2
    "open an existing file for writing; should be truncated"

    |h|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForWrite:'xxx2'.
    self assert:(h notNil).
    self assert:(h isValid).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address ~~ 0).
    ].

    h close.
    self assert:(h isValid not).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address == 0).
    ].

    self assert:('xxx2' asFilename fileSize == 10).   "/ should not change size
    'xxx2' asFilename delete.

    "
     self new testOpen2
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testPosition1
    |h pos buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:10.

    pos := h seekTo:1 from:#begin.
    self assert:(pos == 1).

    nBytesRead := h readBytes:9 into:buffer startingAt:1.

    pos := h seekTo:0 from:#current.
    self assert:(pos == 10).

    h close.

    self assert:(nBytesRead == 9).
    self assert:(buffer asString startsWith: '234567890').

    'xxx2' asFilename delete.

    "
     self new testPosition1
    "
!

testPosition2
    |h pos buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:10.

    pos := h seekTo:0 from:#begin.
    self assert:(pos == 0).

    pos := h seekTo:1 from:#begin.
    self assert:(pos == 1).

    pos := h seekTo:2 from:#current.
    self assert:(pos == 3).
    nBytesRead := h readBytes:7 into:buffer startingAt:1.

    h close.

    self assert:(nBytesRead == 7).
    self assert:(buffer asString startsWith: '4567890').

    'xxx2' asFilename delete.

    "
     self new testPosition2
    "
!

testPosition3
    |h pos buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:10.

    pos := h seekTo:0 from:#end.
    self assert:(pos == 10).
    pos := h seekTo:-2 from:#current.
    self assert:(pos == 8).

    nBytesRead := h readBytes:2 into:buffer startingAt:1.

    h close.

    self assert:(nBytesRead == 2).
    self assert:(buffer asString startsWith: '90').

    'xxx2' asFilename delete.

    "
     self new testPosition3
    "
!

testPosition4
    |h pos buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:10.

    pos := h seekTo:-2 from:#end.
    self assert:(pos == 8).
    nBytesRead := h readBytes:2 into:buffer startingAt:1.

    h close.

    self assert:(nBytesRead == 2).
    self assert:(buffer asString startsWith: '90').

    'xxx2' asFilename delete.

    "
     self new testPosition4
    "
!

testRead1
    "open an existing file for reading; should read 10 chars"

    |h buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:10.
    nBytesRead := h readBytes:10 into:buffer startingAt:1.

    h close.

    self assert:(nBytesRead == 10).
    self assert:(buffer asString = '1234567890').

    'xxx2' asFilename delete.

    "
     self new testRead1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testRead2
    "open an existing file for reading; should read 10 chars"

    |h buffer nBytesRead|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForRead:'xxx2'.

    buffer := ExternalBytes new:20.
    nBytesRead := h readBytes:20 into:buffer startingAt:1.

    h close.

    self assert:(nBytesRead == 10).
    self assert:(buffer asString startsWith:'1234567890').

    'xxx2' asFilename delete.

    "
     self new testRead2
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

testWrite1
    "create a new file (initially not existing); write some data into it"

    |h buffer nBytesWritten|

    buffer := ExternalBytes new:10.
    buffer replaceFrom:1 with:'1234567890'.

    ('xxx1' asFilename exists) ifTrue:[
	'xxx1' asFilename delete
    ].

    h := OperatingSystem createFileForReadWrite:'xxx1'.

    nBytesWritten := h writeBytes:10 from:buffer startingAt:1.

    h close.

    self assert:('xxx1' asFilename fileSize == 10).
    self assert:('xxx1' asFilename contentsOfEntireFile = '1234567890').

    'xxx1' asFilename delete

    "
     self new testWrite1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

xtestFailCreate1
    "try to create an uncreatable new file (directory not existing)"

    |fn|

    fn := (('.' asFilename construct:'foo')  construct:'bar') construct:'xxx1'.
    OperatingSystem createFileForReadWrite:fn name

    "
     self new xtestFailCreate1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

xtestFailOpen2
    "open a non-existing file for writing; must fail"

    |h|

    'xxx2' asFilename delete.

    h := OperatingSystem openFileForWrite:'xxx2'.
    h close


    "
     self new xtestFailOpen2
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
!

xtestFinalization1
    "open an existing file; check if GC closes it"

    |h|

    self createWithSize10:'xxx2'.

    h := OperatingSystem openFileForWrite:'xxx2'.

    self assert:(h notNil).
    self assert:(h isValid).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address ~~ 0).
    ].

    h := nil.
    ObjectMemory tenure.
    ObjectMemory garbageCollect.

    self assert:(h isValid not).
    OperatingSystem isMSDOSlike ifTrue:[
	self assert:(h address == 0).
    ].

    self assert:('xxx2' asFilename fileSize == 10).   "/ should not change size
    'xxx2' asFilename delete.

    "
     self new testFinalization1
    "

    "Modified: / 12.8.1998 / 13:29:55 / cg"
! !

!FileOpenTest class methodsFor:'documentation'!

version
    ^ '$Header$'
! !