Skip to content

Commit 505e3b4

Browse files
authored
Merge pull request swiftlang#15045 from lorentey/hashValue-is-subject-to-change
2 parents 96c0083 + b52a94d commit 505e3b4

File tree

6 files changed

+59
-88
lines changed

6 files changed

+59
-88
lines changed

test/Interpreter/SDK/Foundation_bridge.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,29 @@ do {
106106
}
107107

108108
// CHECK: dictionary bridges to {
109-
// CHECK-NEXT: 2 = World;
110-
// CHECK-NEXT: 1 = Hello;
111-
// CHECK-NEXT: }
109+
// CHECK-DAG: 1 = Hello;
110+
// CHECK-DAG: 2 = World;
111+
// CHECK: }
112112
do {
113113
var dict: Dictionary<NSNumber, NSString> = [1: "Hello", 2: "World"]
114114
let obj = _bridgeAnythingToObjectiveC(dict)
115115
print("dictionary bridges to \(obj.description!)")
116116
}
117117

118118
// CHECK: dictionary bridges to {
119-
// CHECK-NEXT: 2 = World;
120-
// CHECK-NEXT: 1 = Hello;
121-
// CHECK-NEXT: }
119+
// CHECK-DAG: 1 = Hello;
120+
// CHECK-DAG: 2 = World;
121+
// CHECK: }
122122
do {
123123
var dict2 = [1: "Hello", 2: "World"]
124124
let obj = _bridgeAnythingToObjectiveC(dict2)
125125
print("dictionary bridges to \(obj.description!)")
126126
}
127127

128128
// CHECK: dictionary bridges to {
129-
// CHECK-NEXT: 2 = "(\"World\", 2)";
130-
// CHECK-NEXT: 1 = "(\"Hello\", 1)";
131-
// CHECK-NEXT: }
129+
// CHECK-DAG: 1 = "(\"Hello\", 1)";
130+
// CHECK-DAG: 2 = "(\"World\", 2)";
131+
// CHECK: }
132132
do {
133133
var dict3 = [1: ("Hello", 1), 2: ("World", 2)]
134134
let obj = _bridgeAnythingToObjectiveC(dict3)
@@ -141,11 +141,11 @@ var dict4 = propListStr.propertyListFromStringsFileFormat()!
141141
var hello: NSString = "Hello"
142142
var world: NSString = "World"
143143

144-
// Print out the keys. We only check one of these because the order is
145-
// nondeterministic.
146-
// CHECK: Hello
144+
// Print out the keys.
145+
// CHECK-DAG: Bridged key: Hello
146+
// CHECK-DAG: Bridged key: World
147147
for key in dict4.keys {
148-
print(key.description)
148+
print("Bridged key: \(key.description)")
149149
}
150150

151151
// CHECK: Hello: 1

test/Interpreter/SDK/equatable_hashable.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ import Foundation
88

99
func testHash<H: Hashable>(_ x: H) -> Int { return x.hashValue }
1010

11+
// CHECK: (1 as UInt8): hash = [[HASH:-?[0-9]+]]
12+
print("(1 as UInt8): hash = \((1 as UInt8).hashValue)")
13+
1114
func test_CBool() {
1215
let x: CBool = true
1316
let hash = testHash(x)
1417
print("C_Bool: hash = \(hash)")
1518
}
16-
// CHECK: C_Bool: hash = 1
19+
// CHECK: C_Bool: hash = [[HASH]]
1720
test_CBool()
1821

1922
func test_ObjCBool() {
2023
let x = ObjCBool(true)
2124
let hash = testHash(x.boolValue)
2225
print("ObjCBool: hash = \(hash)")
2326
}
24-
// CHECK-NEXT: ObjCBool: hash = 1
27+
// CHECK-NEXT: ObjCBool: hash = [[HASH]]
2528
test_ObjCBool()
2629

2730
func testEquatable<E: Equatable>(_ x: E) {}

test/Interpreter/repl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ String(4.0) // CHECK: String = "4.0"
4141

4242
123 .
4343
hashValue
44-
// CHECK: Int = 123{{$}}
44+
// CHECK: Int = {{-?[0-9]+$}}
4545

4646
// Check that we handle unmatched parentheses in REPL.
4747
1+1)

test/stdlib/ReflectionHashing.swift

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
// RUN: %target-run %t.out
33
// REQUIRES: executable_test
44

5-
// This test expects consistent hash code generation. However String instance
6-
// generate different values on Linux depending on which version of libicu is
7-
// installed. Therefore we run this test only on non-linux OSes, which is
8-
// best described as having objc_interop.
9-
10-
// REQUIRES: objc_interop
11-
12-
//
135
// This file contains reflection tests that depend on hash values.
146
// Don't add other tests here.
157
//
@@ -36,46 +28,16 @@ Reflection.test("Dictionary") {
3628
var output = ""
3729
dump(dict, to: &output)
3830

39-
#if arch(i386) || arch(arm)
40-
var expected = ""
41-
expected += "▿ 5 key/value pairs\n"
42-
expected += " ▿ (2 elements)\n"
43-
expected += " - key: \"Four\"\n"
44-
expected += " - value: 4\n"
45-
expected += " ▿ (2 elements)\n"
46-
expected += " - key: \"One\"\n"
47-
expected += " - value: 1\n"
48-
expected += " ▿ (2 elements)\n"
49-
expected += " - key: \"Two\"\n"
50-
expected += " - value: 2\n"
51-
expected += " ▿ (2 elements)\n"
52-
expected += " - key: \"Five\"\n"
53-
expected += " - value: 5\n"
54-
expected += " ▿ (2 elements)\n"
55-
expected += " - key: \"Three\"\n"
56-
expected += " - value: 3\n"
57-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
31+
// The order of elements in output depends on the hash values of dict's items,
32+
// which isn't deterministic. However, iterating over dict will get us the
33+
// same elements in the same order.
5834
var expected = ""
5935
expected += "▿ 5 key/value pairs\n"
60-
expected += " ▿ (2 elements)\n"
61-
expected += " - key: \"Three\"\n"
62-
expected += " - value: 3\n"
63-
expected += " ▿ (2 elements)\n"
64-
expected += " - key: \"Two\"\n"
65-
expected += " - value: 2\n"
66-
expected += " ▿ (2 elements)\n"
67-
expected += " - key: \"Four\"\n"
68-
expected += " - value: 4\n"
69-
expected += " ▿ (2 elements)\n"
70-
expected += " - key: \"One\"\n"
71-
expected += " - value: 1\n"
72-
expected += " ▿ (2 elements)\n"
73-
expected += " - key: \"Five\"\n"
74-
expected += " - value: 5\n"
75-
#else
76-
fatalError("unimplemented")
77-
#endif
78-
36+
for (key, value) in dict {
37+
expected += " ▿ (2 elements)\n"
38+
expected += " - key: \"\(key)\"\n"
39+
expected += " - value: \(value)\n"
40+
}
7941
expectEqual(expected, output)
8042
}
8143

@@ -85,25 +47,14 @@ Reflection.test("Set") {
8547
var output = ""
8648
dump(s, to: &output)
8749

88-
#if arch(i386) || arch(arm)
89-
var expected = ""
90-
expected += "▿ 5 members\n"
91-
expected += " - 3\n"
92-
expected += " - 1\n"
93-
expected += " - 5\n"
94-
expected += " - 2\n"
95-
expected += " - 4\n"
96-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
50+
// The order of elements in output depends on the hash values of dict's items,
51+
// which isn't deterministic. However, iterating over dict will get us the
52+
// same elements in the same order.
9753
var expected = ""
9854
expected += "▿ 5 members\n"
99-
expected += " - 5\n"
100-
expected += " - 2\n"
101-
expected += " - 3\n"
102-
expected += " - 1\n"
103-
expected += " - 4\n"
104-
#else
105-
fatalError("unimplemented")
106-
#endif
55+
for i in s {
56+
expected += " - \(i)\n"
57+
}
10758

10859
expectEqual(expected, output)
10960
}

validation-test/stdlib/Dictionary.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,7 +4492,8 @@ DictionaryTestSuite.test("dropsBridgedCache") {
44924492
}
44934493

44944494
DictionaryTestSuite.test("getObjects:andKeys:") {
4495-
let d = ([1: "one", 2: "two"] as Dictionary<Int, String>) as NSDictionary
4495+
let native = [1: "one", 2: "two"] as Dictionary<Int, String>
4496+
let d = native as NSDictionary
44964497
var keys = UnsafeMutableBufferPointer(
44974498
start: UnsafeMutablePointer<NSNumber>.allocate(capacity: 2), count: 2)
44984499
var values = UnsafeMutableBufferPointer(
@@ -4501,17 +4502,27 @@ DictionaryTestSuite.test("getObjects:andKeys:") {
45014502
var vp = AutoreleasingUnsafeMutablePointer<AnyObject?>(values.baseAddress!)
45024503
var null: AutoreleasingUnsafeMutablePointer<AnyObject?>?
45034504

4505+
let expectedKeys: [NSNumber]
4506+
let expectedValues: [NSString]
4507+
if native.first?.key == 1 {
4508+
expectedKeys = [1, 2]
4509+
expectedValues = ["one", "two"]
4510+
} else {
4511+
expectedKeys = [2, 1]
4512+
expectedValues = ["two", "one"]
4513+
}
4514+
45044515
d.available_getObjects(null, andKeys: null) // don't segfault
45054516

45064517
d.available_getObjects(null, andKeys: kp)
4507-
expectEqual([2, 1] as [NSNumber], Array(keys))
4518+
expectEqual(expectedKeys, Array(keys))
45084519

45094520
d.available_getObjects(vp, andKeys: null)
4510-
expectEqual(["two", "one"] as [NSString], Array(values))
4521+
expectEqual(expectedValues, Array(values))
45114522

45124523
d.available_getObjects(vp, andKeys: kp)
4513-
expectEqual([2, 1] as [NSNumber], Array(keys))
4514-
expectEqual(["two", "one"] as [NSString], Array(values))
4524+
expectEqual(expectedKeys, Array(keys))
4525+
expectEqual(expectedValues, Array(values))
45154526
}
45164527
#endif
45174528

@@ -4530,11 +4541,14 @@ DictionaryTestSuite.test("popFirst") {
45304541
2020: 2020,
45314542
3030: 3030,
45324543
]
4533-
let expected = Array(d.map{($0.0, $0.1)})
4544+
let expected = [(1010, 1010), (2020, 2020), (3030, 3030)]
45344545
while let element = d.popFirst() {
45354546
popped.append(element)
45364547
}
4537-
expectEqualSequence(expected, Array(popped)) {
4548+
// Note that removing an element may reorder remaining items, so we
4549+
// can't compare ordering here.
4550+
popped.sort(by: { $0.0 < $1.0 })
4551+
expectEqualSequence(expected, popped) {
45384552
(lhs: (Int, Int), rhs: (Int, Int)) -> Bool in
45394553
lhs.0 == rhs.0 && lhs.1 == rhs.1
45404554
}

validation-test/stdlib/Set.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,11 +3365,14 @@ SetTestSuite.test("popFirst") {
33653365
do {
33663366
var popped = [Int]()
33673367
var s = Set([1010, 2020, 3030])
3368-
let expected = Array(s)
3368+
let expected = [1010, 2020, 3030]
33693369
while let element = s.popFirst() {
33703370
popped.append(element)
33713371
}
3372-
expectEqualSequence(expected, Array(popped))
3372+
// Note that removing an element may reorder remaining items, so we
3373+
// can't compare ordering here.
3374+
popped.sort()
3375+
expectEqualSequence(expected, popped)
33733376
expectTrue(s.isEmpty)
33743377
}
33753378
}

0 commit comments

Comments
 (0)