Skip to content

Commit 9daf8e3

Browse files
authored
Merge pull request #3490 from apple/stdlib-ObjectIdentifier-tests
stdlib: better tests for ObjectIdentifier
2 parents 705674a + d886e6a commit 9daf8e3

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

test/1_stdlib/Runtime.swift.gyb

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -949,41 +949,47 @@ Reflection.test("CustomMirror") {
949949

950950
expectEqual(expected, output)
951951
}
952+
}
952953

953-
do {
954-
// Check that object identifiers are unique to class instances.
955-
let a = Brilliant(1, "")
956-
let b = Brilliant(2, "")
957-
let c = Brilliant(3, "")
958-
959-
// Equatable
960-
checkEquatable(true, ObjectIdentifier(a), ObjectIdentifier(a))
961-
checkEquatable(false, ObjectIdentifier(a), ObjectIdentifier(b))
962-
963-
// Comparable
964-
func isComparable<X : Comparable>(_ x: X) {}
965-
isComparable(ObjectIdentifier(a))
966-
// Check the ObjectIdentifier created is stable
967-
expectTrue(
968-
(ObjectIdentifier(a) < ObjectIdentifier(b))
969-
!= (ObjectIdentifier(a) > ObjectIdentifier(b)))
970-
expectFalse(
971-
ObjectIdentifier(a) >= ObjectIdentifier(b)
972-
&& ObjectIdentifier(a) <= ObjectIdentifier(b))
973-
974-
// Check that ordering is transitive.
975-
expectEqual(
976-
[ ObjectIdentifier(a), ObjectIdentifier(b), ObjectIdentifier(c) ].sorted(),
977-
[ ObjectIdentifier(c), ObjectIdentifier(b), ObjectIdentifier(a) ].sorted())
978-
979-
// CustomDebugStringConvertible
980-
expectEqual(
981-
String(reflecting: ObjectIdentifier(a)),
982-
String(reflecting: ObjectIdentifier(a)))
983-
expectNotEqual(
984-
String(reflecting: ObjectIdentifier(a)),
985-
String(reflecting: ObjectIdentifier(b)))
986-
}
954+
Reflection.test("ObjectIdentifier/Hashable,Comparable") {
955+
// Check that object identifiers are unique to class instances.
956+
let a = Brilliant(1, "")
957+
let b = Brilliant(2, "")
958+
let c = Brilliant(3, "")
959+
960+
checkHashable(
961+
[a, b, c].map(ObjectIdentifier.init),
962+
equalityOracle: { $0 == $1 })
963+
964+
// Comparable
965+
func isComparable<X : Comparable>(_ x: X) {}
966+
isComparable(ObjectIdentifier(a))
967+
// Check the ObjectIdentifier created is stable
968+
expectTrue(
969+
(ObjectIdentifier(a) < ObjectIdentifier(b))
970+
!= (ObjectIdentifier(a) > ObjectIdentifier(b)))
971+
expectFalse(
972+
ObjectIdentifier(a) >= ObjectIdentifier(b)
973+
&& ObjectIdentifier(a) <= ObjectIdentifier(b))
974+
975+
// Check that ordering is transitive.
976+
expectEqual(
977+
[ ObjectIdentifier(a), ObjectIdentifier(b), ObjectIdentifier(c) ].sorted(),
978+
[ ObjectIdentifier(c), ObjectIdentifier(b), ObjectIdentifier(a) ].sorted())
979+
}
980+
981+
Reflection.test("ObjectIdentifier/CustomDebugStringConvertible") {
982+
let obj1 = Brilliant(1, "")
983+
let obj2 = Brilliant(2, "")
984+
985+
let oi1 = ObjectIdentifier(obj1)
986+
let oi2 = ObjectIdentifier(obj2)
987+
988+
expectEqual(String(reflecting: oi1), String(reflecting: oi1))
989+
expectNotEqual(String(reflecting: oi1), String(reflecting: oi2))
990+
991+
let p1 = UnsafePointer<Void>(bitPattern: UInt(oi1))!
992+
expectPrinted("ObjectIdentifier(\(p1))", oi1)
987993
}
988994

989995
Reflection.test("CustomMirrorIsInherited") {

0 commit comments

Comments
 (0)