File: UTF8SpanQueriesComparisons.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (278 lines) | stat: -rw-r--r-- 7,184 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// RUN: %target-run-stdlib-swift %S/Inputs/

// REQUIRES: executable_test

import Swift
import StdlibUnittest

@available(SwiftStdlib 6.2, *)
extension UTF8Span {
  static func ~=(_ lhs: StaticString, _ rhs: UTF8Span) -> Bool {
    return lhs.withUTF8Buffer { str in
      rhs._withUnsafeBufferPointer { span in
        str.elementsEqual(span)
      }
    }
  }
}

var suite = TestSuite("UTF8SpanQueriesComparisons")
defer { runAllTests() }

@available(SwiftStdlib 6.2, *)
extension Array where Element == UInt8 {
  func withSpan<R>(_ f: (Span<Element>) throws -> R) rethrows -> R {
    try self.withUnsafeBufferPointer {
      try f(Span(_unsafeElements: $0))
    }
  }
  func withUTF8Span<R>(_ f: (UTF8Span) throws -> R) rethrows -> R {
    try self.withSpan { span in
      try f(try! UTF8Span(validating: span))
    }
  }
}

if #available(SwiftStdlib 6.2, *) {
  suite.test("UTF8Span/tilde equals") {
    Array("abcdefg".utf8).withUTF8Span { utf8Span in
      switch utf8Span {
      case "def":
        expectationFailure(
          "unexpected pattern match",
          trace: "",
          stackTrace: SourceLocStack().withCurrentLoc())
      case "abcdef":
        expectationFailure(
          "unexpected pattern match",
          trace: "",
          stackTrace: SourceLocStack().withCurrentLoc())
      case "abcdefg ":
        expectationFailure(
          "unexpected pattern match",
          trace: "",
          stackTrace: SourceLocStack().withCurrentLoc())
      case "abcdefg\0":
        expectationFailure(
          "unexpected pattern match",
          trace: "",
          stackTrace: SourceLocStack().withCurrentLoc())
      case "abcdefg":
        break
      default:
        expectationFailure(
          "expected a pattern match",
          trace: "",
          stackTrace: SourceLocStack().withCurrentLoc())
      }
    }
  }

  suite.test("UTF8Span/Sequence equal") {
    // // A string and its canonical equivalent
    // let testCases: [(String, String?)] = [
    //   ("abdefg", nil)
    //   ("café", "cafe\u{301}")
    // ]
  }

  suite.test("UTF8Span/isKnownASCII") {
    let tests: [(String, Bool)] = [
      ("abc", true),
      ("abcdefghil1235@#% _/.sladfj234 ", true),
      ("abcdefghil1\u{80}sladfj234 ", false),
    ]

    for (test, expected) in tests {
      Array(test.utf8).withUTF8Span {
        expectEqual(expected, $0.isKnownASCII)
      }
    }
  }

  suite.test("UTF8Span/isKnownNFC") {
    enum Normalness {
      case known
      case quickCheck
      case fullCheck
      case notNFC
    }

    let nfcQCNo = "\u{0374}"
    let nfcQCYes = "\u{0374}"

    let tests: [(String, Normalness)] = [
      ("abc", .known),
      ("abcdefghil123567890", .known),
      ("abcdefghil1\u{299}123345678 ", .quickCheck),
      ("abc日曜日xyz", .quickCheck),
      ("abcde日曜日\u{301}", .fullCheck),
      ("abcde\u{301}fghijkl", .notNFC),
    ]

    for (test, expected) in tests {
      Array(test.utf8).withUTF8Span {
        var span = $0
        if span.isKnownNFC {
          expectEqual(expected, .known)
        } else if span.checkForNFC(quickCheck: true) {
          expectEqual(expected, .quickCheck)
        } else if span.checkForNFC(quickCheck: false) {
          expectEqual(expected, .fullCheck)
        } else {
          expectEqual(expected, .notNFC)
        }
      }
    }
  }

  suite.test("UTF8Span/canonical equivalence") {

    // TODO: refactor to be test-case declaration driven, and add more tests...
    //   `(normalized: String, variants: [String], lessThan: String, greaterThan: String)`

    let precomposedStr = "café"
    let decomposedStr = "cafe\u{301}"

    let precomposed = Array(precomposedStr.utf8)
    let decomposed = Array(decomposedStr.utf8)

    precomposed.withSpan { pre in
      let utf8Precomposed = try! UTF8Span(validating: pre)
      decomposed.withSpan { de in
        let utf8Decomposed = try! UTF8Span(validating: de)

        // print("scalars for \(precomposedStr.unicodeScalars)")
        // var preScalars = utf8Precomposed.makeUnicodeScalarIterator()
        // while let s = preScalars.next() {
        //   print(s)
        // }

        // print("scalars for \(decomposedStr.unicodeScalars)")
        // var deScalars = utf8Decomposed.makeUnicodeScalarIterator()
        // while let s = deScalars.next() {
        //   print(s)
        // }
        
        expectTrue(utf8Precomposed.isCanonicallyEquivalent(to: utf8Decomposed))

        expectTrue(utf8Precomposed.bytesEqual(to: precomposedStr.utf8))
        expectFalse(utf8Precomposed.bytesEqual(to: decomposedStr.utf8))

        expectTrue(utf8Decomposed.bytesEqual(to: decomposedStr.utf8))
        expectFalse(utf8Decomposed.bytesEqual(to: precomposedStr.utf8))

        expectTrue(utf8Precomposed.unicodeScalarsEqual(to: precomposedStr.unicodeScalars))
        expectFalse(utf8Precomposed.unicodeScalarsEqual(to: decomposedStr.unicodeScalars))

        expectTrue(utf8Decomposed.unicodeScalarsEqual(to: decomposedStr.unicodeScalars))
        expectFalse(utf8Decomposed.unicodeScalarsEqual(to: precomposedStr.unicodeScalars))

        expectTrue(utf8Precomposed.charactersEqual(to: precomposedStr))
        expectTrue(utf8Precomposed.charactersEqual(to: decomposedStr))

        expectTrue(utf8Decomposed.charactersEqual(to: decomposedStr))
        expectTrue(utf8Decomposed.charactersEqual(to: precomposedStr))

        // Equivalence means no-one is less than the other
        expectFalse(utf8Decomposed.isCanonicallyLessThan(utf8Precomposed))
        expectFalse(utf8Precomposed.isCanonicallyLessThan(utf8Decomposed))

      }

    }


  }


}

// TODO: Rest of this file is in-progress TODOs


/*


isASCII
isKnownNFC
checkForNFC(quickCheck:)
isKnownSingleScalarCharacters
checkForSingleScalarCharacters(quickCheck:)

public func bytesEqual(to other: UTF8Span) -> Bool
public func bytesEqual(to other: some Sequence<UInt8>) -> Bool

public func scalarsEqual(
  to other: some Sequence<Unicode.Scalar>
) -> Bool

public func charactersEqual(
  to other: some Sequence<Character>
) -> Bool

public func isCanonicallyEquivalent(
  to other: UTF8Span
) -> Bool

public func isCanonicallyLessThan(
  _ other: UTF8Span
) -> Bool

*/

// @available(SwiftStdlib 6.2, *)
// private struct QueryTestCase {
//   var content: String

//   var loc: SourceLocStack

//   var isASCII: Bool

//   // TODO: This might become API, or otherwise calculated at init time
//   var isLatinyNFC: Bool {
//     bytes.allSatisfy { $0 < 0xCC }
//   }

//   var isQuickNFC: Bool
//   var isNFC: Bool

//   var isQuickSSC: Bool
//   var isSSC: Bool
// }

// if #available(SwiftStdlib 6.2, *) {
//   suite.test("UTF8Span/queries") {
//   }
// }

// enum ComparisonResult {
//   binaryEqual
//   canonicallyEqual
//   canonicallyLess
//   inequal
// }

// private struct ComparisonTestCase {
//   var content: String
//   var comparisons: [(String, ComparisonResult)]

//   var loc: SourceLocStack
// }

// if #available(SwiftStdlib 6.2, *) {
//   suite.test("UTF8Span/comparisons") {
//     func test()
//   }
// }


/*

  input string, to check the bits and relevant info
  comparison string and expected comparison level

*/


// }