-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathStringHashableComparable.swift.gyb
97 lines (81 loc) · 2.53 KB
/
StringHashableComparable.swift.gyb
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
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
// This test requires that the standard library calls ICU
// directly. It is not specific to Linux, it is just that on
// Apple platforms we are using the NSString bridge right now.
// REQUIRES: OS=linux-gnu
import StdlibUnittest
import StdlibUnicodeUnittest
func assertASCIIRepresentationIfPossible(_ s: String) {
for us in s.unicodeScalars {
if !us.isASCII {
return
}
}
precondition(s._core.isASCII)
}
func forceUTF16Representation(_ s: String) -> String {
var s = s
s += "\u{fffd}"
s.removeSubrange(s.index(before: s.endIndex)..<s.endIndex)
precondition(!s._core.isASCII)
return s
}
func calculateSortOrder(_ tests: inout [StringComparisonTest]) {
tests.sort {
collationElements(
$0.collationElements,
areLessThan: $1.collationElements
)
}
tests[0].order = 0
for i in tests.indices.dropFirst() {
if collationElements(
tests[i - 1].collationElements,
areLessThan: tests[i].collationElements
) {
tests[i].order = tests[i - 1].order! + 1
} else {
tests[i].order = tests[i - 1].order!
}
}
}
func checkStringHashableComparable(
_ tests: [StringComparisonTest],
stackTrace: SourceLocStack = SourceLocStack(),
file: String = #file, line: UInt = #line
) {
var tests = tests
calculateSortOrder(&tests)
func comparisonOracle(_ i: Int, _ j: Int) -> ExpectedComparisonResult {
return tests[i].order! <=> tests[j].order!
}
checkHashable(
tests.map { $0.string },
equalityOracle: { comparisonOracle($0, $1).isEQ() },
stackTrace: stackTrace.pushIf(true, file: file, line: line))
checkComparable(
tests.map { $0.string },
oracle: comparisonOracle,
stackTrace: stackTrace.pushIf(true, file: file, line: line))
}
var StringTests = TestSuite("StringTests")
StringTests.test("StringComparisonTest.allTests: tests are in ASCII representation")
.forEach(in: StringComparisonTest.allTests) {
test in
assertASCIIRepresentationIfPossible(test.string)
}
StringTests.test("Comparable") {
let allTestsInUTF16Representation = StringComparisonTest.allTests.map {
test -> StringComparisonTest in
return StringComparisonTest(
forceUTF16Representation(test.string),
test.collationElements,
sourceLocation: SourceLoc(
test.loc.file,
test.loc.line,
comment: (test.loc.comment ?? "") + "\nin Unicode representation"))
}
checkStringHashableComparable(StringComparisonTest.allTests + allTestsInUTF16Representation)
}
runAllTests()