11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Text ;
45using Xunit ;
56
67namespace System . IO . Tests
@@ -213,11 +214,23 @@ public void FileModeTruncateExisting(string streamSpecifier)
213214 [ Theory , MemberData ( nameof ( StreamSpecifiers ) ) ]
214215 public virtual void FileModeAppend ( string streamSpecifier )
215216 {
216- using ( FileStream fs = CreateFileStream ( GetTestFilePath ( ) + streamSpecifier , FileMode . Append ) )
217+ string fileName = GetTestFilePath ( ) + streamSpecifier ;
218+ using ( FileStream fs = CreateFileStream ( fileName , FileMode . Append ) )
217219 {
218220 Assert . False ( fs . CanRead ) ;
219221 Assert . True ( fs . CanWrite ) ;
222+
223+ fs . Write ( Encoding . ASCII . GetBytes ( "abcde" ) ) ;
224+ Assert . Equal ( 5 , fs . Length ) ;
225+ Assert . Equal ( 5 , fs . Position ) ;
226+ Assert . Equal ( 1 , fs . Seek ( 1 , SeekOrigin . Begin ) ) ;
227+
228+ fs . Write ( Encoding . ASCII . GetBytes ( "xyz" ) ) ;
229+ Assert . Equal ( 4 , fs . Position ) ;
230+ Assert . Equal ( 5 , fs . Length ) ;
220231 }
232+
233+ Assert . Equal ( "axyze" , File . ReadAllText ( fileName ) ) ;
221234 }
222235
223236 [ Theory , MemberData ( nameof ( StreamSpecifiers ) ) ]
@@ -226,20 +239,35 @@ public virtual void FileModeAppendExisting(string streamSpecifier)
226239 string fileName = GetTestFilePath ( ) + streamSpecifier ;
227240 using ( FileStream fs = CreateFileStream ( fileName , FileMode . Create ) )
228241 {
229- fs . WriteByte ( 0 ) ;
242+ fs . WriteByte ( ( byte ) 's' ) ;
230243 }
231244
245+ string initialContents = File . ReadAllText ( fileName ) ;
232246 using ( FileStream fs = CreateFileStream ( fileName , FileMode . Append ) )
233247 {
234248 // Ensure that the file was re-opened and position set to end
235249 Assert . Equal ( Math . Max ( 1L , InitialLength ) , fs . Length ) ;
236- Assert . Equal ( fs . Length , fs . Position ) ;
250+
251+ long position = fs . Position ;
252+ Assert . Equal ( fs . Length , position ) ;
253+
237254 Assert . False ( fs . CanRead ) ;
238255 Assert . True ( fs . CanSeek ) ;
239256 Assert . True ( fs . CanWrite ) ;
257+
240258 Assert . Throws < IOException > ( ( ) => fs . Seek ( - 1 , SeekOrigin . Current ) ) ;
259+ Assert . Throws < IOException > ( ( ) => fs . Seek ( 0 , SeekOrigin . Begin ) ) ;
241260 Assert . Throws < NotSupportedException > ( ( ) => fs . ReadByte ( ) ) ;
261+
262+ fs . Write ( Encoding . ASCII . GetBytes ( "abcde" ) ) ;
263+ Assert . Equal ( position + 5 , fs . Position ) ;
264+
265+ Assert . Equal ( position , fs . Seek ( position , SeekOrigin . Begin ) ) ;
266+ Assert . Equal ( position + 1 , fs . Seek ( 1 , SeekOrigin . Current ) ) ;
267+ fs . Write ( Encoding . ASCII . GetBytes ( "xyz" ) ) ;
242268 }
269+
270+ Assert . Equal ( initialContents + "axyze" , File . ReadAllText ( fileName ) ) ;
243271 }
244272 }
245273}
0 commit comments