|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing |
| 12 | + |
| 13 | +#if !SWT_TARGET_OS_APPLE && canImport(Synchronization) |
| 14 | +import Synchronization |
| 15 | +#endif |
| 16 | + |
| 17 | +@Suite |
| 18 | +struct LegacyPlanIterationTests { |
| 19 | + @Test("One iteration (default behavior)") |
| 20 | + func oneIteration() async { |
| 21 | + await confirmation("N iterations started") { started in |
| 22 | + await confirmation("N iterations ended") { ended in |
| 23 | + var configuration = Configuration() |
| 24 | + configuration.eventHandler = { event, _ in |
| 25 | + if case .iterationStarted = event.kind { |
| 26 | + started() |
| 27 | + } else if case .iterationEnded = event.kind { |
| 28 | + ended() |
| 29 | + } |
| 30 | + } |
| 31 | + configuration.repetitionPolicy = .once |
| 32 | + |
| 33 | + await Test { |
| 34 | + }.run(configuration: configuration) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test("Unconditional iteration") |
| 40 | + func unconditionalIteration() async { |
| 41 | + let iterationCount = 10 |
| 42 | + await confirmation("N iterations started", expectedCount: iterationCount) { started in |
| 43 | + await confirmation("N iterations ended", expectedCount: iterationCount) { ended in |
| 44 | + var configuration = Configuration() |
| 45 | + configuration.eventHandler = { event, _ in |
| 46 | + if case .iterationStarted = event.kind { |
| 47 | + started() |
| 48 | + } else if case .iterationEnded = event.kind { |
| 49 | + ended() |
| 50 | + } |
| 51 | + } |
| 52 | + configuration.repetitionPolicy = .repeating(maximumIterationCount: iterationCount) |
| 53 | + |
| 54 | + await Test { |
| 55 | + if Bool.random() { |
| 56 | + #expect(Bool(false)) |
| 57 | + } |
| 58 | + }.run(configuration: configuration) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test("Iteration until issue recorded") |
| 64 | + func iterationUntilIssueRecorded() async { |
| 65 | + let iterationIndex = Atomic(0) |
| 66 | + let iterationCount = 10 |
| 67 | + let iterationWithIssue = 5 |
| 68 | + await confirmation("N iterations started", expectedCount: iterationWithIssue + 1) { started in |
| 69 | + await confirmation("N iterations ended", expectedCount: iterationWithIssue + 1) { ended in |
| 70 | + var configuration = Configuration() |
| 71 | + configuration.eventHandler = { event, _ in |
| 72 | + if case let .iterationStarted(index) = event.kind { |
| 73 | + iterationIndex.store(index, ordering: .sequentiallyConsistent) |
| 74 | + started() |
| 75 | + } else if case .iterationEnded = event.kind { |
| 76 | + ended() |
| 77 | + } |
| 78 | + } |
| 79 | + configuration.repetitionPolicy = .repeating(.untilIssueRecorded, maximumIterationCount: iterationCount) |
| 80 | + |
| 81 | + await Test { |
| 82 | + let iterationIndex = iterationIndex.load(ordering: .sequentiallyConsistent) |
| 83 | + if iterationIndex == iterationWithIssue { |
| 84 | + #expect(Bool(false)) |
| 85 | + } |
| 86 | + }.run(configuration: configuration) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test("Iteration while issue recorded") |
| 92 | + func iterationWhileIssueRecorded() async { |
| 93 | + let iterationIndex = Atomic(0) |
| 94 | + let iterationCount = 10 |
| 95 | + let iterationWithoutIssue = 5 |
| 96 | + await confirmation("N iterations started", expectedCount: iterationWithoutIssue + 1) { started in |
| 97 | + await confirmation("N iterations ended", expectedCount: iterationWithoutIssue + 1) { ended in |
| 98 | + var configuration = Configuration() |
| 99 | + configuration.eventHandler = { event, _ in |
| 100 | + if case let .iterationStarted(index) = event.kind { |
| 101 | + iterationIndex.store(index, ordering: .sequentiallyConsistent) |
| 102 | + started() |
| 103 | + } else if case .iterationEnded = event.kind { |
| 104 | + ended() |
| 105 | + } |
| 106 | + } |
| 107 | + configuration.repetitionPolicy = .repeating(.whileIssueRecorded, maximumIterationCount: iterationCount) |
| 108 | + |
| 109 | + await Test { |
| 110 | + let iterationIndex = iterationIndex.load(ordering: .sequentiallyConsistent) |
| 111 | + if iterationIndex < iterationWithoutIssue { |
| 112 | + #expect(Bool(false)) |
| 113 | + } |
| 114 | + }.run(configuration: configuration) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +#if !SWT_NO_EXIT_TESTS |
| 120 | + @Test("Iteration count must be positive") |
| 121 | + func positiveIterationCount() async { |
| 122 | + await #expect(processExitsWith: .failure) { |
| 123 | + var configuration = Configuration() |
| 124 | + configuration.repetitionPolicy.maximumIterationCount = 0 |
| 125 | + } |
| 126 | + await #expect(processExitsWith: .failure) { |
| 127 | + var configuration = Configuration() |
| 128 | + configuration.repetitionPolicy.maximumIterationCount = -1 |
| 129 | + } |
| 130 | + } |
| 131 | +#endif |
| 132 | +} |
0 commit comments