@@ -36,6 +36,7 @@ class TestNSNumber : XCTestCase {
36
36
( " test_stringValue " , test_stringValue) ,
37
37
( " test_Equals " , test_Equals) ,
38
38
( " test_boolValue " , test_boolValue) ,
39
+ ( " test_hash " , test_hash) ,
39
40
]
40
41
}
41
42
@@ -1262,4 +1263,26 @@ class TestNSNumber : XCTestCase {
1262
1263
XCTAssertEqual ( NSNumber ( value: Int . min + 1 ) . boolValue, true )
1263
1264
XCTAssertEqual ( NSNumber ( value: Int ( - 1 ) ) . boolValue, true )
1264
1265
}
1266
+
1267
+ func test_hash( ) {
1268
+ // A zero double hashes as zero.
1269
+ XCTAssertEqual ( NSNumber ( value: 0 as Double ) . hash, 0 )
1270
+
1271
+ // A positive double without fractional part should hash the same as the
1272
+ // equivalent 64 bit number.
1273
+ XCTAssertEqual ( NSNumber ( value: 123456 as Double ) . hash, NSNumber ( value: 123456 as Int64 ) . hash)
1274
+
1275
+ // A negative double without fractional part should hash the same as the
1276
+ // equivalent 64 bit number.
1277
+ XCTAssertEqual ( NSNumber ( value: - 123456 as Double ) . hash, NSNumber ( value: - 123456 as Int64 ) . hash)
1278
+
1279
+ #if arch(i386) || arch(arm)
1280
+ // This test used to fail in 32 bit platforms.
1281
+ XCTAssertNotEqual ( NSNumber ( value: 551048378.24883795 as Double ) . hash, 0 )
1282
+
1283
+ // Some hashes are correctly zero, though. Like the following which
1284
+ // was found by trial and error.
1285
+ XCTAssertEqual ( NSNumber ( value: 1.3819660135 as Double ) . hash, 0 )
1286
+ #endif
1287
+ }
1265
1288
}
0 commit comments