Skip to content

Commit 620058e

Browse files
author
Itai Ferber
committed
Add NSNumber AnyHashable bridging test
1 parent e1d7410 commit 620058e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/stdlib/TestNSNumberBridging.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,31 @@ func test_numericBitPatterns_to_floatingPointTypes() {
873873
}
874874
}
875875

876+
func testNSNumberBridgeAnyHashable() {
877+
var dict = [AnyHashable : Any]()
878+
for i in -Int(UInt8.min) ... Int(UInt8.max) {
879+
dict[i] = "\(i)"
880+
}
881+
882+
// When bridging a dictionary to NSDictionary, we should be able to access
883+
// the keys through either an Int (the original type boxed in AnyHashable)
884+
// or NSNumber (the type Int bridged to).
885+
let ns_dict = dict as NSDictionary
886+
for i in -Int(UInt8.min) ... Int(UInt8.max) {
887+
guard let value = ns_dict[i] as? String else {
888+
expectUnreachable("Unable to look up value by Int key.")
889+
continue
890+
}
891+
892+
guard let ns_value = ns_dict[NSNumber(value: i)] as? String else {
893+
expectUnreachable("Unable to look up value by NSNumber key.")
894+
continue
895+
}
896+
897+
expectEqual(value, ns_value)
898+
}
899+
}
900+
876901
nsNumberBridging.test("Bridge Int8") { testNSNumberBridgeFromInt8() }
877902
nsNumberBridging.test("Bridge UInt8") { testNSNumberBridgeFromUInt8() }
878903
nsNumberBridging.test("Bridge Int16") { testNSNumberBridgeFromInt16() }
@@ -887,4 +912,5 @@ nsNumberBridging.test("Bridge Float") { testNSNumberBridgeFromFloat() }
887912
nsNumberBridging.test("Bridge Double") { testNSNumberBridgeFromDouble() }
888913
nsNumberBridging.test("Bridge CGFloat") { testNSNumberBridgeFromCGFloat() }
889914
nsNumberBridging.test("bitPattern to exactly") { test_numericBitPatterns_to_floatingPointTypes() }
915+
nsNumberBridging.test("Bridge AnyHashable") { testNSNumberBridgeAnyHashable() }
890916
runAllTests()

0 commit comments

Comments
 (0)