|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import XCTest |
| 13 | +@testable import Algorithms |
| 14 | + |
| 15 | +final class UniquePermutationsTests: XCTestCase { |
| 16 | + static let numbers = [1, 1, 1, 2, 3] |
| 17 | + |
| 18 | + static let numbersPermutations: [[ArraySlice<Int>]] = [ |
| 19 | + // 0 |
| 20 | + [[]], |
| 21 | + // 1 |
| 22 | + [[1], [2], [3]], |
| 23 | + // 2 |
| 24 | + [[1, 1], [1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]], |
| 25 | + // 3 |
| 26 | + [[1, 1, 1], [1, 1, 2], [1, 1, 3], |
| 27 | + [1, 2, 1], [1, 2, 3], [1, 3, 1], [1, 3, 2], |
| 28 | + [2, 1, 1], [2, 1, 3], [2, 3, 1], |
| 29 | + [3, 1, 1], [3, 1, 2], [3, 2, 1]], |
| 30 | + // 4 |
| 31 | + [[1, 1, 1, 2], [1, 1, 1, 3], |
| 32 | + [1, 1, 2, 1], [1, 1, 2, 3], |
| 33 | + [1, 1, 3, 1], [1, 1, 3, 2], |
| 34 | + [1, 2, 1, 1], [1, 2, 1, 3], [1, 2, 3, 1], |
| 35 | + [1, 3, 1, 1], [1, 3, 1, 2], [1, 3, 2, 1], |
| 36 | + [2, 1, 1, 1], [2, 1, 1, 3], [2, 1, 3, 1], [2, 3, 1, 1], |
| 37 | + [3, 1, 1, 1], [3, 1, 1, 2], [3, 1, 2, 1], [3, 2, 1, 1]], |
| 38 | + // 5 |
| 39 | + [[1, 1, 1, 2, 3], [1, 1, 1, 3, 2], |
| 40 | + [1, 1, 2, 1, 3], [1, 1, 2, 3, 1], |
| 41 | + [1, 1, 3, 1, 2], [1, 1, 3, 2, 1], |
| 42 | + [1, 2, 1, 1, 3], [1, 2, 1, 3, 1], [1, 2, 3, 1, 1], |
| 43 | + [1, 3, 1, 1, 2], [1, 3, 1, 2, 1], [1, 3, 2, 1, 1], |
| 44 | + [2, 1, 1, 1, 3], [2, 1, 1, 3, 1], [2, 1, 3, 1, 1], [2, 3, 1, 1, 1], |
| 45 | + [3, 1, 1, 1, 2], [3, 1, 1, 2, 1], [3, 1, 2, 1, 1], [3, 2, 1, 1, 1]] |
| 46 | + ] |
| 47 | + |
| 48 | + func testEmpty() { |
| 49 | + XCTAssertEqualSequences(([] as [Int]).uniquePermutations(), [[]]) |
| 50 | + XCTAssertEqualSequences(([] as [Int]).uniquePermutations(ofCount: 0), [[]]) |
| 51 | + XCTAssertEqualSequences(([] as [Int]).uniquePermutations(ofCount: 1), []) |
| 52 | + XCTAssertEqualSequences(([] as [Int]).uniquePermutations(ofCount: 1...3), []) |
| 53 | + } |
| 54 | + |
| 55 | + func testSingleCounts() { |
| 56 | + for (k, expectation) in Self.numbersPermutations.enumerated() { |
| 57 | + XCTAssertEqualSequences(expectation, Self.numbers.uniquePermutations(ofCount: k)) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + func testRanges() { |
| 62 | + for lower in Self.numbersPermutations.indices { |
| 63 | + // upper bounded |
| 64 | + XCTAssertEqualSequences( |
| 65 | + Self.numbersPermutations[...lower].joined(), |
| 66 | + Self.numbers.uniquePermutations(ofCount: ...lower)) |
| 67 | + |
| 68 | + // lower bounded |
| 69 | + XCTAssertEqualSequences( |
| 70 | + Self.numbersPermutations[lower...].joined(), |
| 71 | + Self.numbers.uniquePermutations(ofCount: lower...)) |
| 72 | + |
| 73 | + for upper in lower..<Self.numbersPermutations.count { |
| 74 | + XCTAssertEqualSequences( |
| 75 | + Self.numbersPermutations[lower..<upper].joined(), |
| 76 | + Self.numbers.uniquePermutations(ofCount: lower..<upper)) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments