Skip to content

Commit 3e73f9e

Browse files
committed
Fix and reenable tests that depend on specific hash values
Hash values are now randomly seeded, and the hash algorithm may change between any two releases of the standard library — code should not depend on specific hash values or Set/Dictionary orderings.
1 parent f70c4ec commit 3e73f9e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

TestFoundation/TestNSCache.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TestNSCache : XCTestCase {
2020
static var allTests: [(String, (TestNSCache) -> () throws -> Void)] {
2121
return [
2222
("test_setWithUnmutableKeys", test_setWithUnmutableKeys),
23-
// FIXME: https://bugs.swift.org/browse/SR-7161 ("test_setWithMutableKeys", test_setWithMutableKeys),
23+
("test_setWithMutableKeys", test_setWithMutableKeys),
2424
("test_costLimit", test_costLimit),
2525
("test_countLimit", test_countLimit),
2626
("test_hashableKey", test_hashableKey),
@@ -72,9 +72,21 @@ class TestNSCache : XCTestCase {
7272
XCTAssertEqual(cache.object(forKey: key2), value, "should be equal to \(value) when using second key")
7373

7474
key1.append("1")
75-
76-
XCTAssertEqual(cache.object(forKey: key1), value, "should be equal to \(value) when using first key")
75+
76+
// Mutating the key probably changes the hash value, which often makes
77+
// the value inaccessible by sorting the key into a different bucket.
78+
// On the other hand, the bucket may remain the same by coincidence.
79+
// Therefore, `cache.object(forKey: key1)` may or may not be nil at
80+
// this point -- no useful check can be made.
81+
// The object can definitely not be reached via the original key,
82+
// though.
7783
XCTAssertNil(cache.object(forKey: key2), "should be nil")
84+
85+
// Restoring key1 to the original string will make the value
86+
// accessible again.
87+
key1.setString("key")
88+
XCTAssertEqual(cache.object(forKey: key1), value, "should be equal to \(value) when using first key")
89+
XCTAssertEqual(cache.object(forKey: key2), value, "should be equal to \(value) when using second key")
7890
}
7991

8092
func test_costLimit() {

TestFoundation/TestNSDictionary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class TestNSDictionary : XCTestCase {
2525
return [
2626
("test_BasicConstruction", test_BasicConstruction),
2727
("test_ArrayConstruction", test_ArrayConstruction),
28-
// XFAIL test https://bugs.swift.org/browse/SR-7166
29-
//("test_description", test_description),
28+
("test_description", test_description),
3029
("test_enumeration", test_enumeration),
3130
("test_equality", test_equality),
3231
("test_copying", test_copying),
@@ -47,7 +46,8 @@ class TestNSDictionary : XCTestCase {
4746

4847
func test_description() {
4948
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"]
50-
XCTAssertEqual(d1.description, "{\n baz = qux;\n foo = bar;\n}")
49+
XCTAssertTrue(d1.description == "{\n baz = qux;\n foo = bar;\n}" ||
50+
d1.description == "{\n foo = bar;\n baz = qux;\n}")
5151
let d2: NSDictionary = ["1" : ["1" : ["1" : "1"]]]
5252
XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
5353
}

0 commit comments

Comments
 (0)