File: Combinatorics.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 (279 lines) | stat: -rw-r--r-- 8,124 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 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
//
//===----------------------------------------------------------------------===//

/// Run the supplied closure with all values in `items` in a loop,
/// recording the current value in the current test trace stack.
public func withEvery<Element>(
  _ label: String,
  by generator: () -> Element?,
  file: StaticString = #file,
  line: UInt = #line,
  run body: (Element) throws -> Void
) rethrows {
  let context = TestContext.current
  while let item = generator() {
    let entry = context.push("\(label): \(item)", file: file, line: line)
    var done = false
    defer {
      context.pop(entry)
      if !done {
        print(context.currentTrace(title: "Throwing trace"))
      }
    }
    try body(item)
    done = true
  }
}

/// Run the supplied closure with all values in `items` in a loop,
/// recording the current value in the current test trace stack.
public func withEvery<S: Sequence>(
  _ label: String,
  in items: S,
  file: StaticString = #file,
  line: UInt = #line,
  run body: (S.Element) throws -> Void
) rethrows {
  let context = TestContext.current
  for item in items {
    let entry = context.push("\(label): \(item)", file: file, line: line)
    var done = false
    defer {
      context.pop(entry)
      if !done {
        print(context.currentTrace(title: "Throwing trace"))
      }
    }
    try body(item)
    done = true
  }
}

public func withEveryRange<T: Strideable>(
  _ label: String,
  in bounds: Range<T>,
  file: StaticString = #file,
  line: UInt = #line,
  run body: (Range<T>) throws -> Void
) rethrows where T.Stride == Int {
  let context = TestContext.current
  for lowerBound in bounds.lowerBound ... bounds.upperBound {
    for upperBound in lowerBound ... bounds.upperBound {
      let range = lowerBound ..< upperBound
      let entry = context.push("\(label): \(range)", file: file, line: line)
      var done = false
      defer {
        context.pop(entry)
        if !done {
          print(context.currentTrace(title: "Throwing trace"))
        }
      }
      try body(range)
      done = true
    }
  }
}

internal func _samples<C: Collection>(from items: C) -> [C.Element] {
  let c = items.count
  guard c > 7 else { return Array(items) }
  let offsets = [0, 1, c / 2 - 1, c / 2, c / 2 + 1, c - 2, c - 1]
  var offset = 0
  var index = items.startIndex
  var result: [C.Element] = []
  result.reserveCapacity(7)
  for o in offsets {
    items.formIndex(&index, offsetBy: o - offset)
    offset = o
    result.append(items[index])
  }
  return result
}

/// Run the supplied closure with all values in `items` in a loop,
/// recording the current value in the current test trace stack.
public func withSome<C: Collection>(
  _ label: String,
  in items: C,
  maxSamples: Int? = nil,
  file: StaticString = #file,
  line: UInt = #line,
  run body: (C.Element) throws -> Void
) rethrows {
  let context = TestContext.current

  func check(item: C.Element) throws {
    let entry = context.push("\(label): \(item)", file: file, line: line)
    var done = false
    defer {
      if !done {
        print(context.currentTrace(title: "Throwing trace"))
      }
      context.pop(entry)
    }
    try body(item)
    done = true
  }

  let c = items.count
  
  if let maxSamples = maxSamples, c > maxSamples {
    // FIXME: Use randomSample() from swift-algorithms to eliminate dupes
    for _ in 0 ..< maxSamples {
      let r = Int.random(in: 0 ..< c)
      let i = items.index(items.startIndex, offsetBy: r)
      try check(item: items[i])
    }
  } else {
    for item in items {
      try check(item: item)
    }
  }
}

public func withSomeRanges<T: Strideable>(
  _ label: String,
  in bounds: Range<T>,
  maxSamples: Int? = nil,
  file: StaticString = #file,
  line: UInt = #line,
  run body: (Range<T>) throws -> Void
) rethrows where T.Stride == Int {
  try withSome(
    label,
    in: IndexRangeCollection(bounds: bounds),
    maxSamples: maxSamples,
    run: body)
}

/// Utility function for testing mutations with value semantics.
///
/// Calls `body` with on given collection value, while optionally keeping
/// hidden a hidden copy of it around. Once `body` returns, checks that the copy
/// remain unchanged.
///
/// - Parameters:
///    - `enabled`: if `false`, then no copies are made -- the values are passed to `body` with no processing.
///    - `value`: The collection value that is being tested.
///    - `checker`: An optional function that is used to check the consistency of the hidden copy.
///    - `body`: A closure performing a mutation on `value`.
public func withHiddenCopies<S: Sequence, R>(
  if enabled: Bool,
  of value: inout S,
  checker: (S) -> Void = { _ in },
  file: StaticString = #file, line: UInt = #line,
  _ body: (inout S) throws -> R
) rethrows -> R where S.Element: Equatable {
  guard enabled else { return try body(&value) }
  let copy = value
  let expected = Array(value)
  let result = try body(&value)
  expectEqualElements(copy, expected, file: file, line: line)
  checker(copy)
  return result
}

/// Utility function for testing mutations with value semantics.
///
/// Calls `body` with on given collection value, while optionally keeping
/// hidden a hidden copy of it around. Once `body` returns, checks that the copy
/// remain unchanged.
///
/// - Parameters:
///    - `enabled`: if `false`, then no copies are made -- the values are passed to `body` with no processing.
///    - `value`: The collection value that is being tested.
///    - `checker`: An optional function that is used to check the consistency of the hidden copy.
///    - `body`: A closure performing a mutation on `value`.
public func withHiddenCopies<
  S: Sequence,
  Key: Equatable,
  Value: Equatable,
  R
>(
  if enabled: Bool,
  of value: inout S,
  checker: (S) -> Void = { _ in },
  file: StaticString = #file, line: UInt = #line,
  _ body: (inout S) throws -> R
) rethrows -> R where S.Element == (key: Key, value: Value) {
  guard enabled else { return try body(&value) }
  let copy = value
  let expected = Array(value)
  let result = try body(&value)
  expectEqualElements(copy, expected, file: file, line: line)
  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
    }
  }
}