mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[_CollectionsTestSupport] Richer support for combinatorial testing
This commit is contained in:
1 parent
098495b4ab
commit
2e9447fb1c
3 files changed
+116
-27
No files matched your search
@@ -212,3 +212,68 @@ public func withHiddenCopies<
|
||||
checker(copy)
|
||||
return result
|
||||
}
|
||||
|
||||
/// Run the supplied closure with all subsets `items` in a loop,
|
||||
/// recording the current subset in the test trace stack.
|
||||
///
|
||||
/// The subsets are generated so that they contain elements in the same order
|
||||
/// as the original collection.
|
||||
public func withEverySubset<C: Collection>(
|
||||
_ label: String,
|
||||
of items: C,
|
||||
body: ([C.Element]) -> Void
|
||||
) {
|
||||
var set: [C.Element] = []
|
||||
_withEverySubset(label, of: items, extending: &set, body: body)
|
||||
}
|
||||
|
||||
func _withEverySubset<C: Collection>(
|
||||
_ label: String,
|
||||
of items: C,
|
||||
extending set: inout [C.Element],
|
||||
body: ([C.Element]) -> Void
|
||||
) {
|
||||
guard let item = items.first else {
|
||||
let entry = TestContext.current.push("\(label): \(set)")
|
||||
defer { TestContext.current.pop(entry) }
|
||||
body(set)
|
||||
return
|
||||
}
|
||||
_withEverySubset(label, of: items.dropFirst(), extending: &set, body: body)
|
||||
set.append(item)
|
||||
_withEverySubset(label, of: items.dropFirst(), extending: &set, body: body)
|
||||
set.removeLast()
|
||||
}
|
||||
|
||||
public func withEveryPermutation<C: Collection>(
|
||||
_ label: String,
|
||||
of items: C,
|
||||
body: ([C.Element]) -> Void
|
||||
) {
|
||||
func send(_ items: [C.Element]) {
|
||||
let entry = TestContext.current.push("\(label): \(items)")
|
||||
body(items)
|
||||
TestContext.current.pop(entry)
|
||||
}
|
||||
|
||||
// Heap's algorithm.
|
||||
var items = Array(items)
|
||||
send(items)
|
||||
var i = 1
|
||||
var c = [Int](repeating: 0, count: items.count)
|
||||
while i < items.count {
|
||||
if c[i] < i {
|
||||
if i.isMultiple(of: 2) {
|
||||
items.swapAt(0, i)
|
||||
} else {
|
||||
items.swapAt(c[i], i)
|
||||
}
|
||||
send(items)
|
||||
c[i] += 1
|
||||
i = 1
|
||||
} else {
|
||||
c[i] = 0
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import _CollectionsTestSupport
|
||||
|
||||
class CombinatoricsTests: CollectionTestCase {
|
||||
func testEverySubset_smoke() {
|
||||
func collectSubsets(of set: [Int]) -> Set<[Int]> {
|
||||
var result: Set<[Int]> = []
|
||||
withEverySubset("subset", of: set) { subset in
|
||||
let r = result.insert(subset)
|
||||
expectTrue(r.inserted)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
expectEqual(collectSubsets(of: []), [[]])
|
||||
expectEqual(collectSubsets(of: [0]), [[], [0]])
|
||||
expectEqual(collectSubsets(of: [0, 1]), [[], [0], [1], [0, 1]])
|
||||
expectEqual(
|
||||
collectSubsets(of: [0, 1, 2]),
|
||||
[[], [0], [1], [2], [0, 1], [0, 2], [1, 2], [0, 1, 2]])
|
||||
}
|
||||
|
||||
func testEveryPermutation_smoke() {
|
||||
func collectPermutations(of items: [Int]) -> Set<[Int]> {
|
||||
var result: Set<[Int]> = []
|
||||
withEveryPermutation("permutation", of: items) { permutation in
|
||||
let r = result.insert(permutation)
|
||||
expectTrue(r.inserted)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
expectEqual(collectPermutations(of: []), [[]])
|
||||
expectEqual(collectPermutations(of: [0]), [[0]])
|
||||
expectEqual(collectPermutations(of: [0, 1]), [[0, 1], [1, 0]])
|
||||
expectEqual(
|
||||
collectPermutations(of: [0, 1, 2]),
|
||||
[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]])
|
||||
}
|
||||
}
|
||||
@@ -241,30 +241,3 @@ func mutate<T, R>(
|
||||
) rethrows -> R {
|
||||
try body(&value)
|
||||
}
|
||||
|
||||
func withEverySubset<C: Collection>(
|
||||
_ label: String,
|
||||
of items: C,
|
||||
body: ([C.Element]) -> Void
|
||||
) {
|
||||
var set: [C.Element] = []
|
||||
_withEverySubset(label, of: items, extending: &set, body: body)
|
||||
}
|
||||
|
||||
func _withEverySubset<C: Collection>(
|
||||
_ label: String,
|
||||
of items: C,
|
||||
extending set: inout [C.Element],
|
||||
body: ([C.Element]) -> Void
|
||||
) {
|
||||
guard let item = items.first else {
|
||||
let entry = TestContext.current.push("\(label): \(set)")
|
||||
defer { TestContext.current.pop(entry) }
|
||||
body(set)
|
||||
return
|
||||
}
|
||||
_withEverySubset(label, of: items.dropFirst(), extending: &set, body: body)
|
||||
set.append(item)
|
||||
_withEverySubset(label, of: items.dropFirst(), extending: &set, body: body)
|
||||
set.removeLast()
|
||||
}
|
||||
Reference in new issue
Block a user