File: DiscreteFormatStyleTestUtilities.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (429 lines) | stat: -rw-r--r-- 19,480 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//

#if canImport(TestSupport)
import TestSupport
#endif

#if canImport(FoundationEssentials)
@testable import FoundationEssentials
#endif

#if canImport(FoundationInternationalization)
@testable import FoundationInternationalization
#endif

@available(FoundationPreview 0.4, *)
extension DiscreteFormatStyle where FormatInput : Comparable {
    /// Produces a sequence that generates all outputs of a discrete format style from a given start to a given end.
    func evaluate(from initialInput: FormatInput, to end: FormatInput, _ advance: @escaping (FormatInput, FormatInput) -> FormatInput? = { prev, next in next }) -> LazySequence<DiscreteFormatStyleSequence<Self>> {
        DiscreteFormatStyleSequence(style: self, input: initialInput, end: end, advance: advance, isLower: <).lazy
    }
}

@available(FoundationPreview 0.4, *)
extension DiscreteFormatStyle {
    /// Produces a sequence that generates all outputs of a discrete format style from a given start to a given end.
    func evaluate(from initialInput: FormatInput, to end: FormatInput, _ advance: @escaping (FormatInput, FormatInput) -> FormatInput? = { prev, next in next }, isLower: @escaping (FormatInput, FormatInput) -> Bool) -> LazySequence<DiscreteFormatStyleSequence<Self>> {
        DiscreteFormatStyleSequence(style: self, input: initialInput, end: end, advance: advance, isLower: isLower).lazy
    }
}

/// A sequence that generates all outputs of a discrete format style from a given start to a given end.
@available(FoundationPreview 0.4, *)
struct DiscreteFormatStyleSequence<Style: DiscreteFormatStyle> : Sequence, IteratorProtocol {
    private let style: Style
    private var input: Style.FormatInput
    private let end: Style.FormatInput
    private let isIncreasing: Bool
    private let advance: (Style.FormatInput, Style.FormatInput) -> Style.FormatInput?
    private var abort: Bool = false
    private let isLower: (Style.FormatInput, Style.FormatInput) -> Bool

    init(style: Style, input: Style.FormatInput, end: Style.FormatInput, advance: @escaping (Style.FormatInput, Style.FormatInput) -> Style.FormatInput?, isLower: @escaping (Style.FormatInput, Style.FormatInput) -> Bool) {
        self.style = style
        self.input = input
        self.end = end
        self.isIncreasing = isLower(input, end)
        self.advance = advance
        self.isLower = isLower
    }

    func makeIterator() -> DiscreteFormatStyleSequence<Style> {
        self
    }

    mutating func next() -> (input: Style.FormatInput, output: Style.FormatOutput)? {
        guard !abort && (isIncreasing
                ? !isLower(end, input)
                : !isLower(input, end)) else {
            return nil
        }

        let input = self.input
        let output = style.format(input)

        guard let next = isIncreasing
                ? style.discreteInput(after: input)
                : style.discreteInput(before: input) else {
            self.abort = true
            return (input, output)
        }

        self.input = advance(input, next) ?? input

        if isIncreasing && !isLower(input, self.input) || !isIncreasing && !isLower(self.input, input) {
            self.abort = true
        }

        return (input, output)
    }
}

/// Verify that `sequence` contains the `expectedExcerpts` as non-overlapping subsequences.
func verify<T: Equatable>(
    sequence: some Sequence<T>,
    contains expectedExcerpts: some Sequence<some Sequence<T>>,
    _ message: @autoclosure () -> String = "",
    file: StaticString = #filePath,
    line: UInt = #line
) {
    var iterator = sequence.makeIterator()

    for expectedSequence in expectedExcerpts {
        var potentialMatches: [T] = []

        var expectedIterator = expectedSequence.makeIterator()

        guard let first = expectedIterator.next() else {
            continue
        }

        var next: T?
        while next != first {
            next = iterator.next()

            guard let next = next else {
                XCTFail("Expected '\(first)' but found \(potentialMatches.map { "\($0)" }.joined(separator: ", ")) instead \(message())", file: file, line: line)
                break
            }

            potentialMatches.append(next)
        }

        while let expected = expectedIterator.next() {
            let next = iterator.next()
            XCTAssertEqual(next, expected, message(), file: file, line: line)
            if next != expected {
                return
            }
        }
    }
}

/// Verify that a discrete format style fulfills the protocol requirements.
///
/// Takes random samples and verifies that the given `style` implementation behaves as expected for
/// types conforming to `DiscreteFormatStyle`.
///
/// - Parameter strict: If true, the test also fails if it finds a sample where the bounds provided by
/// `discreteInput(before:)` and `discreteInput(after:)` are shorter than they could
/// be, i.e. `style.format(style.discreteInput(after: sample)) == style.format(sample)`.
/// - Parameter samples: The number of random samples to verify.
@available(FoundationPreview 0.4, *)
func verifyDiscreteFormatStyleConformance<Style: DiscreteFormatStyle>(
    _ style: Style,
    strict: Bool = false,
    samples: Int = 10000,
    _ message: @autoclosure () -> String = "",
    file: StaticString = #filePath,
    line: UInt = #line
) throws where Style.FormatOutput : Equatable, Style.FormatInput == Duration {
    try verifyDiscreteFormatStyleConformance(
        style,
        samples: samples,
        randomInput: { range in
            if let range {
                return range.lowerBound + (range.upperBound - range.lowerBound) * Double.random(in: 0..<1)
            } else {
                return .seconds(Double.randomSample(max: Double(Int64.max).nextDown))
            }
        },
        isLower: <,
        min: .seconds(Int64.min),
        max: .seconds(Int64.max),
        codeFormatter: { "Duration(secondsComponent: \($0.components.seconds), attosecondsComponent: \($0.components.attoseconds))" },
        message(),
        file: file,
        line: line
    )
}

/// Verify that a discrete format style fulfills the protocol requirements.
///
/// Takes random samples and verifies that the given `style` implementation behaves as expected for
/// types conforming to `DiscreteFormatStyle`.
///
/// - Parameter strict: If true, the test also fails if it finds a sample where the bounds provided by
/// `discreteInput(before:)` and `discreteInput(after:)` are shorter than they could
/// be, i.e. `style.format(style.discreteInput(after: sample)) == style.format(sample)`.
/// - Parameter samples: The number of random samples to verify.
/// ````
@available(FoundationPreview 0.4, *)
func verifyDiscreteFormatStyleConformance<Style: DiscreteFormatStyle>(
    _ style: Style,
    strict: Bool = false,
    samples: Int = 10000,
    min: Date = Date(timeIntervalSinceReferenceDate: -2000 * 365 * 24 * 3600),
    max: Date = Date(timeIntervalSinceReferenceDate: 2000 * 365 * 24 * 3600),
    _ message: @autoclosure () -> String = "",
    file: StaticString = #filePath,
    line: UInt = #line
) throws where Style.FormatOutput : Equatable, Style.FormatInput == Date {
    try verifyDiscreteFormatStyleConformance(
        style,
        samples: samples,
        randomInput: { range in
            if let range {
                return Date(timeIntervalSinceReferenceDate: Double.random(in: range.lowerBound.timeIntervalSinceReferenceDate..<range.upperBound.timeIntervalSinceReferenceDate))
            } else {
                return Date(timeIntervalSinceReferenceDate: Double.randomSample(max: 2000 * 365 * 24 * 3600))
            }
        },
        isLower: <,
        min: min,
        max: max,
        codeFormatter: { "Date(timeIntervalSinceReferenceDate: \($0.timeIntervalSinceReferenceDate))" },
        message(),
        file: file,
        line: line
    )
}

#if FOUNDATION_FRAMEWORK
/// Verify that a discrete format style fulfills the protocol requirements.
///
/// Takes random samples and verifies that the given `style` implementation behaves as expected for
/// types conforming to `DiscreteFormatStyle`.
///
/// - Parameter strict: If true, the test also fails if it finds a sample where the bounds provided by
/// `discreteInput(before:)` and `discreteInput(after:)` are shorter than they could
/// be, i.e. `style.format(style.discreteInput(after: sample)) == style.format(sample)`.
/// - Parameter samples: The number of random samples to verify.
/// ````
@available(FoundationPreview 0.4, *)
func verifyDiscreteFormatStyleConformance(
    _ style: Date.ComponentsFormatStyle,
    strict: Bool = false,
    samples: Int = 10000,
    now: Date = .now,
    min: Date = Date(timeIntervalSinceReferenceDate: -2000 * 365 * 24 * 3600),
    max: Date = Date(timeIntervalSinceReferenceDate: 2000 * 365 * 24 * 3600),
    _ message: @autoclosure () -> String = "",
    file: StaticString = #filePath,
    line: UInt = #line
) throws {
    var style = style

    try verifyDiscreteFormatStyleConformance(
        style,
        samples: samples,
        randomInput: { range in
            if let range {
                return Swift.min(range.lowerBound.lowerBound, range.upperBound.lowerBound)..<Date(timeIntervalSinceReferenceDate: Double.random(in: range.lowerBound.upperBound.timeIntervalSinceReferenceDate..<range.upperBound.upperBound.timeIntervalSinceReferenceDate))
            } else {
                let bound = now + abs(Double.randomSample(max: max.timeIntervalSince(now)))

                return now..<bound
            }
        },
        isLower: { $0.upperBound < $1.upperBound },
        min: now..<now,
        max: now..<max,
        codeFormatter: { "Date(timeIntervalSinceReferenceDate: \($0.lowerBound.timeIntervalSinceReferenceDate))..<Date(timeIntervalSinceReferenceDate: \($0.upperBound.timeIntervalSinceReferenceDate))" },
        message() + "\nstyle.isPositive = true",
        file: file,
        line: line
    )

    style.isPositive = false

    try verifyDiscreteFormatStyleConformance(
        style,
        samples: samples,
        randomInput: { range in
            if let range {
                return Date(timeIntervalSinceReferenceDate: Double.random(in: range.lowerBound.lowerBound.timeIntervalSinceReferenceDate..<range.upperBound.lowerBound.timeIntervalSinceReferenceDate))..<Swift.max(range.lowerBound.upperBound, range.upperBound.upperBound)
            } else {
                let bound = now - abs(Double.randomSample(max: now.timeIntervalSince(min)))

                return bound..<now
            }
        },
        isLower: { $0.lowerBound < $1.lowerBound },
        min: min..<now,
        max: now..<now,
        codeFormatter: { "Date(timeIntervalSinceReferenceDate: \($0.lowerBound.timeIntervalSinceReferenceDate))..<Date(timeIntervalSinceReferenceDate: \($0.upperBound.timeIntervalSinceReferenceDate))" },
        message() + "\nstyle.isPositive = false",
        file: file,
        line: line
    )
}
#endif // FOUNDATION_FRAMEWORK

/// Verify that a discrete format style fulfills the protocol requirements.
///
/// Takes random samples and verifies that the given `style` implementation behaves as expected for
/// types conforming to `DiscreteFormatStyle`.
///
/// - Parameter strict: If true, the test also fails if it finds a sample where the bounds provided by
/// `discreteInput(before:)` and `discreteInput(after:)` are shorter than they could
/// be, i.e. `style.format(style.discreteInput(after: sample)) == style.format(sample)`.
/// - Parameter samples: The number of random samples to verify.
@available(FoundationPreview 0.4, *)
func verifyDiscreteFormatStyleConformance<Style: DiscreteFormatStyle>(
    _ style: Style,
    strict: Bool = false,
    samples: Int = 10000,
    randomInput: ((lowerBound: Style.FormatInput, upperBound: Style.FormatInput)?) -> Style.FormatInput,
    isLower: (Style.FormatInput, Style.FormatInput) -> Bool,
    min: Style.FormatInput,
    max: Style.FormatInput,
    codeFormatter: (Style.FormatInput) -> String,
    _ message: @autoclosure () -> String = "",
    file: StaticString = #filePath,
    line: UInt = #line
) throws where Style.FormatOutput : Equatable, Style.FormatInput : Equatable {
    func _message(assertion: Assertion, before: Bool, inputValue: Style.FormatInput, expectedValue: Style.FormatInput?, note: String) -> String {
        let message = message()
        let prefix = (message.isEmpty ? "\(note)" : "\(message): \(note)") + "\n"

        let reason: String
        if let expectedValue {
            if assertion == .greaterEqual || assertion == .lowerEqual {
                if let discreteInput = before ? style.discreteInput(before: inputValue) : style.discreteInput(after: inputValue),
                   let nextInput = before ? style.input(after: discreteInput) : style.input(before: discreteInput) {
                    reason = """
                    style.discreteInput(\(before ? "before" : "after"): \(codeFormatter(inputValue))) returned \(codeFormatter(discreteInput)), but
                    \(codeFormatter(expectedValue)), which is a valid input, because style.input(\(before ? "after" : "before"): \(codeFormatter(discreteInput))) = \(codeFormatter(nextInput)),
                    already produces a different formatted output '\(style.format(expectedValue))' compared to style.format(\(codeFormatter(inputValue))), which is '\(style.format(inputValue))'
                    """
                } else {
                    reason = ""
                }
            } else {
                reason = "invalid ordering or short bound"
            }
        } else {
            reason = """
            style.discreteInput(\(before ? "before" : "after"): \(codeFormatter(inputValue))) returned nil, but
            style.format(\(codeFormatter(inputValue))) = '\(style.format(inputValue))', which is different from
            style.format(\(codeFormatter(before ? min : max))) = '\(style.format(before ? min : max))'
            """
        }

        return prefix + """
        XCTAssert\(assertion.rawValue)(try XCTUnwrap(style.discreteInput(\(before ? "before" : "after"): \(codeFormatter(inputValue)))), \(expectedValue == nil ? "nil" : codeFormatter(expectedValue!)))

        \(reason)
        """
    }

    func nextUp(_ input: Style.FormatInput) throws -> Style.FormatInput {
        try XCTUnwrap(style.input(after: input), "\(message().isEmpty ? "" : message() + "\n")XCTAssertNotNil(style.input(after: \(codeFormatter(input))))", file: file, line: line)
    }

    func nextDown(_ input: Style.FormatInput) throws -> Style.FormatInput {
        try XCTUnwrap(style.input(before: input), "\(message().isEmpty ? "" : message() + "\n")XCTAssertNotNil(style.input(before: \(codeFormatter(input))))", file: file, line: line)
    }

    for _ in 0..<samples {
        let input = randomInput(nil)
        let output = style.format(input)

        guard let inputAfter = style.discreteInput(after: input) else {
            // if `inputAfter` is `nil`, we should get the same formatted output everywhere between `input` and `max`
            XCTAssertEqual(style.format(max), output, _message(assertion: .unequal, before: false, inputValue: input, expectedValue: nil, note: "invalid upper bound"), file: file, line: line)
            return
        }

        // check for invalid ordering
        guard isLower(input, inputAfter) else {
            XCTFail(_message(assertion: .greater, before: false, inputValue: input, expectedValue: input, note: "invalid ordering"), file: file, line: line)
            return
        }

        guard let inputBefore = style.discreteInput(before: input) else {
            // if `inputBefore` is `nil`, we should get the same formatted output everywhere between `input` and `min`
            XCTAssertEqual(style.format(min), output, _message(assertion: .unequal, before: true, inputValue: input, expectedValue: nil, note: "invalid lower bound"), file: file, line: line)
            return
        }

        // check for invalid ordering
        guard isLower(inputBefore, input) else {
            XCTFail(_message(assertion: .lower, before: true, inputValue: input, expectedValue: input, note: "invalid ordering"), file: file, line: line)
            return
        }

        // check that all values in `nextUp(inputBefore)...nextDown(inputAfter)` produce the same formatted output as `input`
        let lowerSampleBound = try nextUp(inputBefore)
        let upperSampleBound = try nextDown(inputAfter)
        if !isLower(upperSampleBound, lowerSampleBound), lowerSampleBound != upperSampleBound {
            for check in [lowerSampleBound] + (0..<10).map({ _ in randomInput((lowerSampleBound, upperSampleBound)) }) + [upperSampleBound] {
                if isLower(check, input) {
                    guard style.format(check) == output else {
                        XCTFail(_message(assertion: .greaterEqual, before: true, inputValue: input, expectedValue: check, note: "invalid lower bound"), file: file, line: line)
                        return
                    }
                } else {
                    guard style.format(check) == output else {
                        XCTFail(_message(assertion: .lowerEqual, before: false, inputValue: input, expectedValue: check, note: "invalid upper bound"), file: file, line: line)
                        return
                    }
                }
            }
        }

        // if strict checking is enabled, we also check that the formatted output for `inputAfter` and `inputBefore` is indeed different from `format(input)`
        if strict {
            guard style.format(inputAfter) != output else {
                XCTFail(_message(assertion: .greater, before: false, inputValue: input, expectedValue: inputAfter, note: "short upper bound (strict)"), file: file, line: line)
                return
            }

            guard style.format(inputBefore) != output else {
                XCTFail(_message(assertion: .lower, before: true, inputValue: input, expectedValue: inputBefore, note: "short lower bound (strict)"), file: file, line: line)
                return
            }
        }
    }
}

private enum Assertion: String {
    case equal = "Equal"
    case unequal = "NotEqual"
    case lower = "LessThan"
    case greater = "GreaterThan"
    case greaterEqual = "GreaterThanOrEqual"
    case lowerEqual = "LessThanOrEqual"
}

private extension Double {
    // Produces random samples between -max and +max with an approximately uniform
    // distribution over the number's _magnitude_, i.e. it will produce approximately
    // the same number of samples in the range 0..<1 as in the range 1000..<10000.
    static func randomSample(max: Double) -> Double {
        let d = 10.0
        let r = pow(d, Double.random(in: 0..<((log(max) + log(1 + (1.0/max))) / log(d)))) - 1
        return Bool.random() ? r : -r
    }
}