@@ -37,6 +37,8 @@ class TestNSData: XCTestCase {
37
37
( " test_base64Data_medium " , test_base64Data_medium) ,
38
38
( " test_base64Data_small " , test_base64Data_small) ,
39
39
( " test_openingNonExistentFile " , test_openingNonExistentFile) ,
40
+ ( " test_contentsOfFile " , test_contentsOfFile) ,
41
+ ( " test_contentsOfZeroFile " , test_contentsOfZeroFile) ,
40
42
( " test_basicReadWrite " , test_basicReadWrite) ,
41
43
( " test_bufferSizeCalculation " , test_bufferSizeCalculation) ,
42
44
// ("test_dataHash", test_dataHash), Disabled due to lack of brdiging in swift runtime -- infinite loops
@@ -908,6 +910,45 @@ extension TestNSData {
908
910
XCTAssertTrue ( didCatchError)
909
911
}
910
912
913
+ func test_contentsOfFile( ) {
914
+ let testDir = testBundle ( ) . resourcePath
915
+ let filename = testDir!. appending ( " /NSStringTestData.txt " )
916
+
917
+ let contents = NSData ( contentsOfFile: filename)
918
+ XCTAssertNotNil ( contents)
919
+ if let contents = contents {
920
+ let ptr = UnsafeMutableRawPointer ( mutating: contents. bytes)
921
+ let str = String ( bytesNoCopy: ptr, length: contents. length,
922
+ encoding: . ascii, freeWhenDone: false )
923
+ XCTAssertEqual ( str, " swift-corelibs-foundation " )
924
+ }
925
+ }
926
+
927
+ func test_contentsOfZeroFile( ) {
928
+ #if os(Linux)
929
+ let contents = NSData ( contentsOfFile: " /proc/self/cmdline " )
930
+ XCTAssertNotNil ( contents)
931
+ if let contents = contents {
932
+ XCTAssertTrue ( contents. length > 0 )
933
+ let ptr = UnsafeMutableRawPointer ( mutating: contents. bytes)
934
+ let str = String ( bytesNoCopy: ptr, length: contents. length,
935
+ encoding: . ascii, freeWhenDone: false )
936
+ XCTAssertNotNil ( str)
937
+ if let str = str {
938
+ XCTAssertTrue ( str. hasSuffix ( " TestFoundation " ) )
939
+ }
940
+ }
941
+
942
+ do {
943
+ let maps = try String ( contentsOfFile: " /proc/self/maps " , encoding: . utf8)
944
+ XCTAssertTrue ( maps. characters. count > 0 )
945
+ print ( maps)
946
+ } catch {
947
+ XCTFail ( )
948
+ }
949
+ #endif
950
+ }
951
+
911
952
func test_basicReadWrite( ) {
912
953
let url = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true ) . appendingPathComponent ( " testfile " )
913
954
let count = 1 << 24
0 commit comments