//===----------------------------------------------------------------------===// // // This source file is part of the Swift Algorithms open source project // // Copyright (c) 2020 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 @testable import Algorithms func validateRandomSamples( _ samples: [Int: Int], elements: S, expectedValue: Int, file: StaticString = (#file), line: UInt = #line ) where S.Element == Int { let expectedRange = ((expectedValue / 3) * 2) ... ((expectedValue / 3) * 4) XCTAssertEqualSequences(samples.keys.sorted(), elements, file: file, line: line) for v in samples.values { XCTAssert(expectedRange.contains(v), file: file, line: line) } } private let n = 100 private let k = 12 private let iterations = 10_000 private let c = 0.. UInt64 { 0 } } var zero = ZeroGenerator() _ = nextOffset(w: 1, using: &zero) // must not crash struct AlmostAllZeroGenerator: RandomNumberGenerator { private var forward: SplitMix64 private var count: Int = 0 init(seed: UInt64) { forward = SplitMix64(seed: seed) } mutating func next() -> UInt64 { defer { count &+= 1 } if count % 1000 == 0 { return forward.next() } return 0 } } var almostAllZero = AlmostAllZeroGenerator(seed: 0) _ = s.randomSample(count: k, using: &almostAllZero) // must not crash almostAllZero = AlmostAllZeroGenerator(seed: 0) _ = c.randomSample(count: k, using: &almostAllZero) // must not crash } }