Skip to content

Commit 2a21011

Browse files
authored
Merge pull request #4802 from apple/stdlib-test-fixes
stdlib: test fixes
2 parents 1f0c498 + 39a3a1f commit 2a21011

File tree

8 files changed

+27
-66
lines changed

8 files changed

+27
-66
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ public func checkHashable<Instances : Collection>(
20732073
}
20742074

20752075
public func checkHashable<T : Hashable>(
2076-
_ expectedEqual: Bool, _ lhs: T, _ rhs: T, ${TRACE}
2076+
expectedEqual: Bool, _ lhs: T, _ rhs: T, ${TRACE}
20772077
) {
20782078
checkHashable(
20792079
[lhs, rhs], equalityOracle: { expectedEqual || $0 == $1 }, ${trace})

test/Interpreter/SDK/CoreGraphics_CGFloat.swift

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,12 @@ CGFloatTestSuite.test("initOtherTypesFromCGFloat") {
5454
}
5555

5656
CGFloatTestSuite.test("comparisons") {
57-
let x = 3.14
58-
let y = 3.14
59-
let z = 2.71
60-
61-
expectTrue(x == y)
62-
expectFalse(x != y)
63-
checkHashable(true, x, y)
64-
65-
expectFalse(x == z)
66-
expectTrue(x != z)
67-
checkHashable(false, x, z)
68-
69-
expectFalse(x < z)
70-
expectFalse(x <= z)
71-
expectTrue(x >= z)
72-
expectTrue(x > z)
73-
checkComparable(.gt, x, z)
74-
75-
expectTrue(z < x)
76-
expectTrue(z <= x)
77-
expectFalse(z >= x)
78-
expectFalse(z > x)
79-
checkComparable(.lt, z, x)
80-
81-
expectFalse(x < y)
82-
expectTrue(x <= y)
83-
expectTrue(x >= y)
84-
expectFalse(x > y)
85-
checkComparable(.eq, x, y)
86-
}
57+
let instances: [CGFloat] = [ 2.71, 3.14 ]
58+
59+
checkHashable(instances, equalityOracle: { $0 == $1 })
8760

61+
checkComparable(instances, oracle: { $0 <=> $1 })
62+
}
8863

8964
CGFloatTestSuite.test("arithmetic") {
9065
let x: CGFloat = 0.25

test/stdlib/Character.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,7 @@ CharacterTests.test("Hashable") {
154154
continuingScalars.map { String($0) },
155155
testCharacters
156156
] {
157-
for i in characters.indices {
158-
for j in characters.indices {
159-
var ci = Character(characters[i])
160-
var cj = Character(characters[j])
161-
checkHashable(i == j, ci, cj, "i=\(i), j=\(j)")
162-
}
163-
}
157+
checkHashable(characters, equalityOracle: { $0 == $1 })
164158
}
165159
}
166160

test/stdlib/FloatingPoint.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ func checkFloatingPointComparison_${FloatSelf}(
518518
let message = { "expected: lhs=\(lhs) \(expected) rhs=\(rhs)" }
519519
expectEqual(expected.isEQ(), lhs == rhs, message(), stackTrace: newTrace)
520520
expectEqual(expected.isNE(), lhs != rhs, message(), stackTrace: newTrace)
521-
checkHashable(expected.isEQ(), lhs, rhs, message(), stackTrace: newTrace)
521+
checkHashable(
522+
expectedEqual: expected.isEQ(),
523+
lhs, rhs, message(), stackTrace: newTrace)
522524

523525
expectEqual(expected.isLT(), lhs < rhs, message(), stackTrace: newTrace)
524526
expectEqual(expected.isLE(), lhs <= rhs, message(), stackTrace: newTrace)

test/stdlib/StringAPI.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ func checkStringComparison(
145145
expectEqual(expected.isEQ(), lhs == rhs, stackTrace: stackTrace)
146146
expectEqual(expected.isNE(), lhs != rhs, stackTrace: stackTrace)
147147
checkHashable(
148-
expected.isEQ(), lhs, rhs, stackTrace: stackTrace.withCurrentLoc())
148+
expectedEqual: expected.isEQ(),
149+
lhs, rhs, stackTrace: stackTrace.withCurrentLoc())
149150

150151
expectEqual(expected.isLT(), lhs < rhs, stackTrace: stackTrace)
151152
expectEqual(expected.isLE(), lhs <= rhs, stackTrace: stackTrace)
@@ -167,7 +168,8 @@ func checkStringComparison(
167168
!expectedEqualUnicodeScalars, lhsNSString != rhsNSString,
168169
stackTrace: stackTrace)
169170
checkHashable(
170-
expectedEqualUnicodeScalars, lhsNSString, rhsNSString,
171+
expectedEqual: expectedEqualUnicodeScalars,
172+
lhsNSString, rhsNSString,
171173
stackTrace: stackTrace.withCurrentLoc())
172174
#endif
173175
}
@@ -215,7 +217,8 @@ func checkCharacterComparison(
215217
expectEqual(expected.isEQ(), lhs == rhs, stackTrace: stackTrace)
216218
expectEqual(expected.isNE(), lhs != rhs, stackTrace: stackTrace)
217219
checkHashable(
218-
expected.isEQ(), lhs, rhs, stackTrace: stackTrace.withCurrentLoc())
220+
expectedEqual: expected.isEQ(),
221+
lhs, rhs, stackTrace: stackTrace.withCurrentLoc())
219222

220223
expectEqual(expected.isLT(), lhs < rhs, stackTrace: stackTrace)
221224
expectEqual(expected.isLE(), lhs <= rhs, stackTrace: stackTrace)

test/stdlib/UnsafePointer.swift.gyb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,7 @@ ${SelfName}TestSuite.test("Hashable") {
141141
${SelfType}(bitPattern: 0x12345678)!,
142142
${SelfType}(bitPattern: 0x87654321 as UInt)!,
143143
]
144-
for i in ptrs.indices {
145-
for j in ptrs.indices {
146-
var pi = ptrs[i]
147-
var pj = ptrs[j]
148-
checkHashable(i == j, pi, pj, "i=\(i), j=\(j)")
149-
}
150-
}
144+
checkHashable(ptrs, equalityOracle: { $0 == $1 })
151145
}
152146

153147
${SelfName}TestSuite.test("toInteger") {

validation-test/stdlib/ObjectiveC.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,15 @@ class NSObjectWithCustomHashable : NSObject {
2929
}
3030

3131
ObjectiveCTests.test("NSObject/Hashable") {
32-
let objects = [
33-
NSObjectWithCustomHashable(value: 10, hashValue: 100),
34-
NSObjectWithCustomHashable(value: 10, hashValue: 100),
35-
NSObjectWithCustomHashable(value: 20, hashValue: 100),
36-
NSObjectWithCustomHashable(value: 30, hashValue: 300),
32+
let instances: [(order: Int, object: NSObject)] = [
33+
(10, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
34+
(10, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
35+
(20, NSObjectWithCustomHashable(value: 20, hashValue: 100)),
36+
(30, NSObjectWithCustomHashable(value: 30, hashValue: 300)),
3737
]
38-
for (i, object1) in objects.enumerated() {
39-
for (j, object2) in objects.enumerated() {
40-
checkHashable(
41-
object1._value == object2._value,
42-
object1,
43-
object2,
44-
"i=\(i), j=\(j)")
45-
}
46-
}
38+
checkHashable(
39+
instances.map { $0.object },
40+
equalityOracle: { instances[$0].order == instances[$1].order })
4741
}
4842

4943
runAllTests()

validation-test/stdlib/Set.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,14 +3727,13 @@ SetTestSuite.test("misc") {
37273727
SetTestSuite.test("Hashable") {
37283728
let s1 = Set([1010])
37293729
let s2 = Set([2020])
3730-
checkHashable(s1 == s2, s1, s2)
3730+
checkHashable([s1, s2], equalityOracle: { $0 == $1 })
37313731

37323732
// Explicit types help the type checker quite a bit.
37333733
let ss1 = Set([Set([1010] as [Int]), Set([2020] as [Int]), Set([3030] as [Int])])
37343734
let ss11 = Set([Set([2020] as [Int]), Set([3030] as [Int]), Set([2020] as [Int])])
37353735
let ss2 = Set([Set([9090] as [Int])])
3736-
checkHashable(ss1 == ss11, ss1, ss11)
3737-
checkHashable(ss1 == ss2, ss1, ss2)
3736+
checkHashable([ss1, ss11, ss2], equalityOracle: { $0 == $1 })
37383737
}
37393738

37403739
SetTestSuite.test("Operator.Precedence") {

0 commit comments

Comments
 (0)