forked from apple/swift-collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckComparable.swift
176 lines (155 loc) · 5.33 KB
/
CheckComparable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Loosely adapted from https://github.com/apple/swift/tree/main/stdlib/private/StdlibUnittest
public enum ExpectedComparisonResult: Hashable {
case lt, eq, gt
public func flip() -> ExpectedComparisonResult {
switch self {
case .lt:
return .gt
case .eq:
return .eq
case .gt:
return .lt
}
}
public static func comparing<C: Comparable>(_ left: C, _ right: C) -> Self {
left < right ? .lt
: left > right ? .gt
: .eq
}
}
extension ExpectedComparisonResult: CustomStringConvertible {
public var description: String {
switch self {
case .lt:
return "<"
case .eq:
return "=="
case .gt:
return ">"
}
}
}
public func checkComparable<Instance: Comparable>(
sortedEquivalenceClasses: [[Instance]],
file: StaticString = #file, line: UInt = #line
) {
let instances = sortedEquivalenceClasses.flatMap { $0 }
// oracle[i] is the index of the equivalence class that contains instances[i].
let oracle = sortedEquivalenceClasses.indices.flatMap { i in repeatElement(i, count: sortedEquivalenceClasses[i].count) }
checkComparable(
instances,
oracle: {
if oracle[$0] < oracle[$1] { return .lt }
if oracle[$0] > oracle[$1] { return .gt }
return .eq
},
file: file, line: line)
}
/// Test that the elements of `instances` satisfy the semantic
/// requirements of `Comparable`, using `oracle` to generate comparison
/// expectations from pairs of positions in `instances`.
public func checkComparable<Instances: Collection>(
_ instances: Instances,
oracle: (Instances.Index, Instances.Index) -> ExpectedComparisonResult,
file: StaticString = #file, line: UInt = #line
) where Instances.Element: Comparable {
checkEquatable(instances,
oracle: { oracle($0, $1) == .eq },
file: file, line: line)
_checkComparable(instances, oracle: oracle, file: file, line: line)
}
public func checkComparable<T : Comparable>(
expected: ExpectedComparisonResult, _ lhs: T, _ rhs: T,
file: StaticString = #file, line: UInt = #line
) {
checkComparable(
[lhs, rhs],
oracle: { [[ .eq, expected], [ expected.flip(), .eq]][$0][$1] },
file: file, line: line)
}
/// Same as `checkComparable(_:oracle:file:line:)` but doesn't check
/// `Equatable` conformance. Useful for preventing duplicate testing.
public func _checkComparable<Instances: Collection>(
_ instances: Instances,
oracle: (Instances.Index, Instances.Index) -> ExpectedComparisonResult,
file: StaticString = #file, line: UInt = #line
) where Instances.Element: Comparable {
let entry = TestContext.current.push("checkComparable", file: file, line: line)
defer { TestContext.current.pop(entry) }
for i in instances.indices {
let x = instances[i]
expectFalse(
x < x,
"found 'x < x' at index \(i): \(String(reflecting: x))")
expectFalse(
x > x,
"found 'x > x' at index \(i): \(String(reflecting: x))")
expectTrue(x <= x,
"found 'x <= x' to be false at index \(i): \(String(reflecting: x))")
expectTrue(x >= x,
"found 'x >= x' to be false at index \(i): \(String(reflecting: x))")
for j in instances.indices where i != j {
let y = instances[j]
let expected = oracle(i, j)
expectEqual(
expected.flip(), oracle(j, i),
"""
bad oracle: missing antisymmetry:
lhs (at index \(i)): \(String(reflecting: x))
rhs (at index \(j)): \(String(reflecting: y))
""")
expectEqual(
expected == .lt, x < y,
"""
x < y doesn't match oracle
lhs (at index \(i)): \(String(reflecting: x))
rhs (at index \(j)): \(String(reflecting: y))
""")
expectEqual(
expected != .gt, x <= y,
"""
x <= y doesn't match oracle
lhs (at index \(i)): \(String(reflecting: x))
rhs (at index \(j)): \(String(reflecting: y))
""")
expectEqual(
expected != .lt, x >= y,
"""
x >= y doesn't match oracle
lhs (at index \(i)): \(String(reflecting: x))
rhs (at index \(j)): \(String(reflecting: y))
""")
expectEqual(
expected == .gt, x > y,
"""
x > y doesn't match oracle
lhs (at index \(i)): \(String(reflecting: x))
rhs (at index \(j)): \(String(reflecting: y))
""")
for k in instances.indices {
let expected2 = oracle(j, k)
if expected == expected2 {
expectEqual(
expected, oracle(i, k),
"""
bad oracle: transitivity violation
x (at index \(i)): \(String(reflecting: x))
y (at index \(j)): \(String(reflecting: y))
z (at index \(k)): \(String(reflecting: instances[k]))
""")
}
}
}
}
}