@@ -24,6 +24,8 @@ class TestNSArray : XCTestCase {
24
24
static var allTests : [ ( String , ( TestNSArray ) -> ( ) throws -> Void ) ] {
25
25
return [
26
26
( " test_BasicConstruction " , test_BasicConstruction) ,
27
+ ( " test_constructors " , test_constructors) ,
28
+ ( " test_constructorWithCopyItems " , test_constructorWithCopyItems) ,
27
29
( " test_enumeration " , test_enumeration) ,
28
30
( " test_sequenceType " , test_sequenceType) ,
29
31
( " test_objectAtIndex " , test_objectAtIndex) ,
@@ -43,7 +45,8 @@ class TestNSArray : XCTestCase {
43
45
( " test_copying " , test_copying) ,
44
46
( " test_mutableCopying " , test_mutableCopying) ,
45
47
( " test_writeToFile " , test_writeToFile) ,
46
- ( " test_initWithContentsOfFile " , test_initWithContentsOfFile)
48
+ ( " test_initWithContentsOfFile " , test_initWithContentsOfFile) ,
49
+ ( " test_readWriteURL " , test_readWriteURL)
47
50
]
48
51
}
49
52
@@ -53,6 +56,45 @@ class TestNSArray : XCTestCase {
53
56
XCTAssertEqual ( array. count, 0 )
54
57
XCTAssertEqual ( array2. count, 2 )
55
58
}
59
+
60
+ func test_constructors( ) {
61
+ let arrayNil = NSArray ( objects: nil , count: 0 )
62
+ XCTAssertEqual ( arrayNil. count, 0 )
63
+
64
+ let array1 = NSArray ( object: " foo " )
65
+ XCTAssertEqual ( array1. count, 1 )
66
+
67
+ let testStrings : [ AnyObject ] = [ NSString ( string: " foo " ) , NSString ( string: " bar " ) ]
68
+ testStrings. withUnsafeBufferPointer { ptr in
69
+ let array2 = NSArray ( objects: ptr. baseAddress, count: 1 )
70
+ XCTAssertEqual ( array1, array2)
71
+ }
72
+
73
+ let array3 = NSArray ( objects: " foo " as NSString , " bar " as NSString , " baz " as NSString )
74
+ XCTAssertEqual ( array3. count, 3 )
75
+ let array4 = NSArray ( array: [ " foo " , " bar " , " baz " ] )
76
+ XCTAssertEqual ( array4. count, 3 )
77
+ let array5 = NSArray ( arrayLiteral: " foo " , " bar " , " baz " )
78
+ XCTAssertEqual ( array5. count, 3 )
79
+ XCTAssertEqual ( array3, array4)
80
+ XCTAssertEqual ( array3, array5)
81
+ XCTAssertEqual ( array4, array5)
82
+
83
+ }
84
+
85
+ func test_constructorWithCopyItems( ) {
86
+ let foo = " foo " as NSMutableString
87
+
88
+ let array1 = NSArray ( array: [ foo] , copyItems: false )
89
+ let array2 = NSArray ( array: [ foo] , copyItems: true )
90
+
91
+ XCTAssertEqual ( array1 [ 0 ] as! String , " foo " )
92
+ XCTAssertEqual ( array2 [ 0 ] as! String , " foo " )
93
+
94
+ foo. append ( " 1 " )
95
+ XCTAssertEqual ( array1 [ 0 ] as! String , " foo1 " )
96
+ XCTAssertEqual ( array2 [ 0 ] as! String , " foo " )
97
+ }
56
98
57
99
func test_enumeration( ) {
58
100
let array : NSArray = [ " foo " , " bar " , " baz " ]
@@ -464,7 +506,7 @@ class TestNSArray : XCTestCase {
464
506
}
465
507
}
466
508
467
- func test_initWithContentsOfFile( ) {
509
+ func test_initWithContentsOfFile( ) {
468
510
let testFilePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 234 ) )
469
511
if let _ = testFilePath {
470
512
let a1 : NSArray = [ " foo " , " bar " ]
@@ -504,7 +546,23 @@ class TestNSArray : XCTestCase {
504
546
XCTFail ( " Temporary file creation failed " )
505
547
}
506
548
}
507
-
549
+
550
+ func test_readWriteURL( ) {
551
+ let data = NSArray ( arrayLiteral: " one " , " two " , " three " , " four " , " five " )
552
+ do {
553
+ let tempDir = NSTemporaryDirectory ( ) + " TestFoundation_Playground_ " + NSUUID( ) . uuidString
554
+ try FileManager . default. createDirectory ( atPath: tempDir, withIntermediateDirectories: false , attributes: nil )
555
+ let testFile = tempDir + " /readWriteURL.txt "
556
+ let url = URL ( fileURLWithPath: testFile)
557
+ try data. write ( to: url)
558
+ let data2 = try NSArray ( contentsOf: url, error: ( ) )
559
+ XCTAssertEqual ( data, data2)
560
+ removeTestFile ( testFile)
561
+ } catch let e {
562
+ XCTFail ( " Failed to write to file: \( e) " )
563
+ }
564
+ }
565
+
508
566
private func createTestFile( _ path: String , _contents: Data ) -> String ? {
509
567
let tempDir = NSTemporaryDirectory ( ) + " TestFoundation_Playground_ " + NSUUID( ) . uuidString + " / "
510
568
do {
0 commit comments