Skip to content

Revert "Merge pull request #1362 from millenomi/pr/concurrent-nsarray… #1364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Foundation/NSIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}

internal func _enumerateWithOptions<P, R>(_ opts : NSEnumerationOptions, range: NSRange, paramType: P.Type, returnType: R.Type, block: (P, UnsafeMutablePointer<ObjCBool>) -> R) -> Int? {
guard !opts.contains(.concurrent) else {
NSUnimplemented()
}

guard let startRangeIndex = self._indexOfRangeAfterOrContainingIndex(range.location), let endRangeIndex = _indexOfRangeBeforeOrContainingIndex(NSMaxRange(range) - 1) else {
return nil
}
Expand Down
110 changes: 0 additions & 110 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class TestNSArray : XCTestCase {
("test_constructors", test_constructors),
("test_constructorWithCopyItems", test_constructorWithCopyItems),
("test_enumeration", test_enumeration),
("test_enumerationUsingBlock", test_enumerationUsingBlock),
("test_sequenceType", test_sequenceType),
("test_objectAtIndex", test_objectAtIndex),
("test_binarySearch", test_binarySearch),
Expand Down Expand Up @@ -124,115 +123,6 @@ class TestNSArray : XCTestCase {
XCTAssertNil(reverseEmpty.nextObject())
}

func test_enumerationUsingBlock() {
let array : NSArray = NSArray(array: Array(0..<100))
let createIndexesArrayHavingSeen = { (havingSeen: IndexSet) in
return (0 ..< array.count).map { havingSeen.contains($0) }
}

let noIndexes = IndexSet()
let allIndexes = IndexSet(integersIn: 0 ..< array.count)
let firstHalfOfIndexes = IndexSet(integersIn: 0 ..< array.count / 2)
let lastHalfOfIndexes = IndexSet(integersIn: array.count / 2 ..< array.count)
let evenIndexes : IndexSet = {
var indexes = IndexSet()
for index in allIndexes.filter({ $0 % 2 == 0 }) {
indexes.insert(index)
}
return indexes
}()

let testExpectingToSee = { (expectation: IndexSet, block: (inout UnsafeMutableBufferPointer<Bool>) -> Void) in
var indexesSeen = createIndexesArrayHavingSeen(noIndexes)
indexesSeen.withUnsafeMutableBufferPointer(block)
XCTAssertEqual(indexesSeen, createIndexesArrayHavingSeen(expectation))
}

// Test enumerateObjects(_:), allowing it to run to completion...

testExpectingToSee(allIndexes) { (indexesSeen) in
array.enumerateObjects { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)
indexesSeen[index] = true
}
}

// ... and stopping after the first half:

testExpectingToSee(firstHalfOfIndexes) { (indexesSeen) in
array.enumerateObjects { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)

if firstHalfOfIndexes.contains(index) {
indexesSeen[index] = true
} else {
stop.pointee = true
}
}
}

// -----
// Test enumerateObjects(options:using) and enumerateObjects(at:options:using:):

// Test each of these options combinations:
let optionsToTest : [NSEnumerationOptions] = [
[],
[.concurrent],
[.reverse],
[.concurrent, .reverse],
]

for options in optionsToTest {
// Run to completion,
testExpectingToSee(allIndexes) { (indexesSeen) in
array.enumerateObjects(options: options, using: { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)
indexesSeen[index] = true
})
}

// run it only for half the indexes (use the right half depending on where we start),
let indexesForHalfEnumeration = options.contains(.reverse) ? lastHalfOfIndexes : firstHalfOfIndexes

testExpectingToSee(indexesForHalfEnumeration) { (indexesSeen) in
array.enumerateObjects(options: options, using: { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)

if indexesForHalfEnumeration.contains(index) {
indexesSeen[index] = true
} else {
stop.pointee = true
}
})
}

// run only for a specific index set to test the at:… variant,
testExpectingToSee(evenIndexes) { (indexesSeen) in
array.enumerateObjects(at: evenIndexes, options: options, using: { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)
indexesSeen[index] = true
})
}

// and run for some indexes only to test stopping.
var indexesForStaggeredEnumeration = indexesForHalfEnumeration
indexesForStaggeredEnumeration.formIntersection(evenIndexes)

testExpectingToSee(indexesForStaggeredEnumeration) { (indexesSeen) in
array.enumerateObjects(at: evenIndexes, options: options, using: { (value, index, stop) in
XCTAssertEqual(value as! NSNumber, array[index] as! NSNumber)

if (indexesForStaggeredEnumeration.contains(index)) {
indexesSeen[index] = true
} else {
stop.pointee = true
}
})
}
}

}

func test_sequenceType() {
let array : NSArray = ["foo", "bar", "baz"]
var res = [String]()
Expand Down