1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Text ;
4
5
using Xunit ;
5
6
6
7
namespace System . IO . Tests
@@ -213,11 +214,23 @@ public void FileModeTruncateExisting(string streamSpecifier)
213
214
[ Theory , MemberData ( nameof ( StreamSpecifiers ) ) ]
214
215
public virtual void FileModeAppend ( string streamSpecifier )
215
216
{
216
- using ( FileStream fs = CreateFileStream ( GetTestFilePath ( ) + streamSpecifier , FileMode . Append ) )
217
+ string fileName = GetTestFilePath ( ) + streamSpecifier ;
218
+ using ( FileStream fs = CreateFileStream ( fileName , FileMode . Append ) )
217
219
{
218
220
Assert . False ( fs . CanRead ) ;
219
221
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 ) ;
220
231
}
232
+
233
+ Assert . Equal ( "axyze" , File . ReadAllText ( fileName ) ) ;
221
234
}
222
235
223
236
[ Theory , MemberData ( nameof ( StreamSpecifiers ) ) ]
@@ -226,20 +239,35 @@ public virtual void FileModeAppendExisting(string streamSpecifier)
226
239
string fileName = GetTestFilePath ( ) + streamSpecifier ;
227
240
using ( FileStream fs = CreateFileStream ( fileName , FileMode . Create ) )
228
241
{
229
- fs . WriteByte ( 0 ) ;
242
+ fs . WriteByte ( ( byte ) 's' ) ;
230
243
}
231
244
245
+ string initialContents = File . ReadAllText ( fileName ) ;
232
246
using ( FileStream fs = CreateFileStream ( fileName , FileMode . Append ) )
233
247
{
234
248
// Ensure that the file was re-opened and position set to end
235
249
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
+
237
254
Assert . False ( fs . CanRead ) ;
238
255
Assert . True ( fs . CanSeek ) ;
239
256
Assert . True ( fs . CanWrite ) ;
257
+
240
258
Assert . Throws < IOException > ( ( ) => fs . Seek ( - 1 , SeekOrigin . Current ) ) ;
259
+ Assert . Throws < IOException > ( ( ) => fs . Seek ( 0 , SeekOrigin . Begin ) ) ;
241
260
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" ) ) ;
242
268
}
269
+
270
+ Assert . Equal ( initialContents + "axyze" , File . ReadAllText ( fileName ) ) ;
243
271
}
244
272
}
245
273
}
0 commit comments