Skip to content

Commit e382503

Browse files
committed
Add test for NSUUID equality
1 parent 9f743bc commit e382503

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ extern void _CFURLInitWithFileSystemPathRelativeToBase(CFURLRef url, CFStringRef
209209
extern Boolean _CFURLInitWithURLString(CFURLRef url, CFStringRef string, Boolean checkForLegalCharacters, _Nullable CFURLRef baseURL);
210210
extern Boolean _CFURLInitAbsoluteURLWithBytes(CFURLRef url, const UInt8 *relativeURLBytes, CFIndex length, CFStringEncoding encoding, _Nullable CFURLRef baseURL);
211211

212+
extern CFHashCode CFHashBytes(uint8_t *bytes, CFIndex length);
212213
extern CFIndex __CFProcessorCount();
213214
extern uint64_t __CFMemorySize();
214215
extern CFIndex __CFActiveProcessorCount();

Foundation/NSUUID.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ public class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
6262
public func encodeWithCoder(aCoder: NSCoder) {
6363

6464
}
65+
66+
public override func isEqual(object: AnyObject?) -> Bool {
67+
if object === self {
68+
return true
69+
} else if let other = object as? NSUUID {
70+
return _cf_uuid_compare(buffer, other.buffer) == 0
71+
} else {
72+
return false
73+
}
74+
}
75+
76+
public override var hash: Int {
77+
return Int(CFHashBytes(buffer, 16))
78+
}
6579
}

TestFoundation/TestNSUUID.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class TestNSUUID : XCTestCase {
2121

2222
var allTests : [(String, () -> ())] {
2323
return [
24+
("test_Equality", test_Equality),
2425
]
2526
}
27+
28+
func test_Equality() {
29+
let uuidA = NSUUID(UUIDString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")
30+
let uuidB = NSUUID(UUIDString: "e621e1f8-c36c-495a-93fc-0c247a3e6e5f")
31+
let uuidC = NSUUID(UUIDBytes: [0xe6,0x21,0xe1,0xf8,0xc3,0x6c,0x49,0x5a,0x93,0xfc,0x0c,0x24,0x7a,0x3e,0x6e,0x5f])
32+
let uuidD = NSUUID()
33+
34+
XCTAssertEqual(uuidA, uuidB, "String case must not matter.")
35+
XCTAssertEqual(uuidA, uuidC, "A UUID initialized with a string must be equal to the same UUID initialized with its UnsafePointer<UInt8> equivalent representation.")
36+
XCTAssertNotEqual(uuidC, uuidD, "Two different UUIDs must not be equal.")
37+
}
2638
}

0 commit comments

Comments
 (0)