Skip to content

Commit 1728ac9

Browse files
committed
Fixed CharacterSet equality
1 parent ee8221b commit 1728ac9

File tree

2 files changed

+115
-20
lines changed

2 files changed

+115
-20
lines changed

Foundation/CharacterSet.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
469469

470470
/// Returns true if the two `CharacterSet`s are equal.
471471
public static func ==(lhs : CharacterSet, rhs: CharacterSet) -> Bool {
472-
return lhs._wrapped.isEqual(rhs._bridgeToObjectiveC()) // TODO: mlehew - as NSCharacterSet
472+
return lhs._mapUnmanaged { l in rhs._mapUnmanaged { r in l.isEqual(r) } }
473473
}
474474
}
475475

@@ -486,7 +486,10 @@ extension CharacterSet : _ObjectTypeBridgeable {
486486

487487
@_semantics("convertToObjectiveC")
488488
public func _bridgeToObjectiveC() -> NSCharacterSet {
489-
return _wrapped
489+
switch _wrapped.__wrapped {
490+
case .Mutable(let wrapped): return wrapped.takeUnretainedValue()
491+
case .Immutable(let wrapped): return wrapped.takeUnretainedValue()
492+
}
490493
}
491494

492495
public static func _forceBridgeFromObjectiveC(_ input: NSCharacterSet, result: inout CharacterSet?) {

TestFoundation/TestNSCharacterSet.swift

Lines changed: 110 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This source file is part of the Swift.org open source project
1+
// This source file is part of the Swift.org open source project
22
//
33
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -7,8 +7,6 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
11-
1210
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
1311
import Foundation
1412
import XCTest
@@ -17,7 +15,93 @@ import SwiftFoundation
1715
import SwiftXCTest
1816
#endif
1917

18+
private struct Box {
19+
fileprivate let ns: NSCharacterSet
20+
fileprivate let swift: CharacterSet
21+
22+
private init(ns: NSCharacterSet, swift: CharacterSet) {
23+
self.ns = ns
24+
self.swift = swift
25+
}
26+
27+
init(charactersIn string: String) {
28+
self.ns = NSCharacterSet(charactersIn: string)
29+
self.swift = CharacterSet(charactersIn: string)
30+
}
31+
32+
static var alphanumerics: Box {
33+
return Box(ns: NSCharacterSet.alphanumerics._bridgeToObjectiveC(),
34+
swift: CharacterSet.alphanumerics)
35+
}
36+
37+
static var decimalDigits: Box {
38+
return Box(ns: NSCharacterSet.decimalDigits._bridgeToObjectiveC(),
39+
swift: CharacterSet.decimalDigits)
40+
}
41+
}
42+
43+
private func assertEqual(_ lhs: Box,
44+
_ rhs: Box,
45+
_ message: @autoclosure () -> String = "",
46+
file: StaticString = #file,
47+
line: UInt = #line) {
48+
49+
assert(equal: true, lhs, rhs, message, file: file, line: line)
50+
}
51+
52+
private func assertNotEqual(_ lhs: Box,
53+
_ rhs: Box,
54+
_ message: @autoclosure () -> String = "",
55+
file: StaticString = #file,
56+
line: UInt = #line) {
57+
58+
assert(equal: false, lhs, rhs, message, file: file, line: line)
59+
}
60+
61+
private func assert<T: Equatable>(equal: Bool,
62+
_ lhs: T,
63+
_ rhs: T,
64+
_ message: @autoclosure () -> String = "",
65+
file: StaticString = #file,
66+
line: UInt = #line) {
67+
68+
if equal {
69+
XCTAssertEqual(lhs, rhs, message, file: file, line: line)
70+
}
71+
else {
72+
XCTAssertNotEqual(lhs, rhs, message, file: file, line: line)
73+
}
74+
}
2075

76+
private func assert(equal: Bool,
77+
_ lhs: Box,
78+
_ rhs: Box,
79+
_ message: @autoclosure () -> String = "",
80+
file: StaticString = #file,
81+
line: UInt = #line) {
82+
83+
for pair in [(lhs, rhs), (rhs, lhs)] {
84+
assert(equal: equal, pair.0.ns, pair.1.ns, message, file: file, line: line)
85+
assert(equal: equal, pair.0.swift, pair.1.swift, message, file: file, line: line)
86+
87+
assert(equal: equal,
88+
pair.0.ns._bridgeToSwift(),
89+
pair.1.ns._bridgeToSwift(),
90+
message,
91+
file: file,
92+
line: line)
93+
94+
assert(equal: equal,
95+
pair.0.swift._bridgeToObjectiveC(),
96+
pair.1.swift._bridgeToObjectiveC(),
97+
message,
98+
file: file,
99+
line: line)
100+
101+
XCTAssertTrue(pair.0.ns.isEqual(pair.1.ns) == equal, message, file: file, line: line)
102+
XCTAssertTrue(pair.0.ns.isEqual(pair.1.swift) == equal, message, file: file, line: line)
103+
}
104+
}
21105

22106
class TestNSCharacterSet : XCTestCase {
23107

@@ -283,28 +367,36 @@ class TestNSCharacterSet : XCTestCase {
283367
let expected = CharacterSet(charactersIn: "abc")
284368
XCTAssertEqual(expected, symmetricDifference)
285369
}
286-
370+
287371
func test_Equatable() {
288-
XCTAssertEqual(NSCharacterSet(charactersIn: ""), NSCharacterSet(charactersIn: ""))
289-
XCTAssertEqual(NSCharacterSet(charactersIn: "a"), NSCharacterSet(charactersIn: "a"))
290-
XCTAssertEqual(NSCharacterSet(charactersIn: "ab"), NSCharacterSet(charactersIn: "ab"))
291-
292-
XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: "123"))
293-
XCTAssertNotEqual(NSCharacterSet(charactersIn: "123"), NSCharacterSet(charactersIn: "abc"))
294-
295-
XCTAssertNotEqual(NSCharacterSet(charactersIn: ""), nil)
296-
372+
let equalPairs = [
373+
("", ""),
374+
("a", "a"),
375+
("abcde", "abcde"),
376+
("12345", "12345")
377+
]
378+
297379
/*
298380
Tests disabled due to CoreFoundation bug?
299381
These NSCharacterSet pairs are (wrongly?) evaluated to be equal. Same behaviour can be observed on macOS 10.12.
300382
Interestingly, on iOS 11 Simulator, they are evaluted to be _not_ equal,
301383
while on iOS 10.3.1 Simulator, they are evaluted to be equal.
302384
*/
303-
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "ab"), NSCharacterSet(charactersIn: "abc"))
304-
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: "ab"))
305-
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: ""))
306-
// XCTAssertNotEqual(NSCharacterSet(charactersIn: ""), NSCharacterSet(charactersIn: "abc"))
385+
let notEqualPairs = [
386+
("abc", "123"),
387+
// ("ab", "abc"),
388+
// ("abc", "")
389+
]
390+
391+
for pair in equalPairs {
392+
assertEqual(Box(charactersIn: pair.0), Box(charactersIn: pair.1))
393+
}
394+
assertEqual(Box.alphanumerics, Box.alphanumerics)
395+
396+
for pair in notEqualPairs {
397+
assertNotEqual(Box(charactersIn: pair.0), Box(charactersIn: pair.1))
398+
}
399+
assertNotEqual(Box.alphanumerics, Box.decimalDigits)
307400
}
308401

309402
}
310-

0 commit comments

Comments
 (0)