Skip to content

Commit f2e64e4

Browse files
committed
Add test for initializing NSUUID with nil
1 parent 847583e commit f2e64e4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Foundation/NSUUID.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
3232
}
3333

3434
public init(UUIDBytes bytes: UnsafePointer<UInt8>) {
35-
memcpy(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), UnsafePointer<Void>(bytes), 16)
35+
if (bytes != nil) {
36+
memcpy(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), UnsafePointer<Void>(bytes), 16)
37+
} else {
38+
memset(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), 0, 16)
39+
}
3640
}
3741

3842
public func getUUIDBytes(uuid: UnsafeMutablePointer<UInt8>) {

TestFoundation/TestNSUUID.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestNSUUID : XCTestCase {
2323
return [
2424
("test_Equality", test_Equality),
2525
("test_InvalidUUID", test_InvalidUUID),
26+
("test_InitializationWithNil", test_InitializationWithNil),
2627
]
2728
}
2829

@@ -41,4 +42,9 @@ class TestNSUUID : XCTestCase {
4142
let uuid = NSUUID(UUIDString: "Invalid UUID")
4243
XCTAssertNil(uuid, "The convenience initializer `init?(UUIDString string:)` must return nil for an invalid UUID string.")
4344
}
45+
46+
func test_InitializationWithNil() {
47+
let uuid = NSUUID(UUIDBytes: nil)
48+
XCTAssertEqual(uuid, NSUUID(UUIDBytes: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]), "The convenience initializer `init(UUIDBytes bytes:)` must return the Nil UUID when UUIDBytes is nil.")
49+
}
4450
}

0 commit comments

Comments
 (0)