//===----------------------------------------------------------------------===// // // 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 final class PermutationsTests: XCTestCase { func testPermutations() { let c = "ABCDE" func t(count: Int?) { let p = c.permutations(ofCount: count) let count = count ?? c.count let permCount = c.count.factorial() / (c.count - count).factorial() XCTAssertEqual(permCount, p.count) XCTAssertEqual(p.count, Array(p).count) XCTAssertTrue(p.allSatisfy { $0.count == count }) XCTAssertTrue(p.isSorted(by: { $0.lexicographicallyPrecedes($1) })) } func ts(count: R) where R.Bound == Int { let p = count.relative(to: 0 ..< .max) .clamped(to: 0..