@@ -84,6 +84,16 @@ class TestFileHandle : XCTestCase {
84
84
return fh!
85
85
}
86
86
87
+ func createFileHandleForUpdating( ) -> FileHandle {
88
+ let url = createTemporaryFile ( containing: content)
89
+
90
+ var fh : FileHandle !
91
+ expectDoesNotThrow ( { fh = try FileHandle ( forUpdating: url) } , " Couldn't create file handle. " )
92
+
93
+ allHandles. append ( fh)
94
+ return fh
95
+ }
96
+
87
97
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
88
98
func createFileHandleForSeekErrors( ) -> FileHandle {
89
99
#if os(Windows)
@@ -380,6 +390,44 @@ class TestFileHandle : XCTestCase {
380
390
XCTAssertEqual ( data. count, 10 )
381
391
XCTAssertEqual ( data, Data ( [ 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 5 ] ) )
382
392
}
393
+
394
+ func test_truncate( ) throws {
395
+ // `func truncate(atOffset offset: UInt64) throws` is introduced in Swift 5.
396
+ // See also https://bugs.swift.org/browse/SR-11922
397
+
398
+ let fh = createFileHandleForUpdating ( )
399
+
400
+ try fh. truncate ( atOffset: 50 )
401
+ XCTAssertEqual ( fh. offsetInFile, 50 )
402
+
403
+ try fh. truncate ( atOffset: 0 )
404
+ XCTAssertEqual ( fh. offsetInFile, 0 )
405
+
406
+ try fh. truncate ( atOffset: 100 )
407
+ XCTAssertEqual ( fh. offsetInFile, 100 )
408
+
409
+ fh. write ( Data ( [ 1 , 2 ] ) )
410
+ XCTAssertEqual ( fh. offsetInFile, 102 )
411
+
412
+ try fh. seek ( toOffset: 4 )
413
+ XCTAssertEqual ( fh. offsetInFile, 4 )
414
+
415
+ ( 0 ..< 20 ) . forEach { fh. write ( Data ( [ $0] ) ) }
416
+ XCTAssertEqual ( fh. offsetInFile, 24 )
417
+
418
+ fh. seekToEndOfFile ( )
419
+ XCTAssertEqual ( fh. offsetInFile, 102 )
420
+
421
+ try fh. truncate ( atOffset: 10 )
422
+ XCTAssertEqual ( fh. offsetInFile, 10 )
423
+
424
+ try fh. seek ( toOffset: 0 )
425
+ XCTAssertEqual ( fh. offsetInFile, 0 )
426
+
427
+ let data = fh. readDataToEndOfFile ( )
428
+ XCTAssertEqual ( data. count, 10 )
429
+ XCTAssertEqual ( data, Data ( [ 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 5 ] ) )
430
+ }
383
431
384
432
func test_readabilityHandlerCloseFileRace( ) throws {
385
433
for _ in 0 ..< 10 {
@@ -541,6 +589,7 @@ class TestFileHandle : XCTestCase {
541
589
( " testWritingWithMultiregionData " , testWritingWithMultiregionData) ,
542
590
( " test_constants " , test_constants) ,
543
591
( " test_truncateFile " , test_truncateFile) ,
592
+ ( " test_truncate " , test_truncate) ,
544
593
( " test_readabilityHandlerCloseFileRace " , test_readabilityHandlerCloseFileRace) ,
545
594
( " test_readabilityHandlerCloseFileRaceWithError " , test_readabilityHandlerCloseFileRaceWithError) ,
546
595
( " test_availableData " , test_availableData) ,
0 commit comments