@@ -16,7 +16,7 @@ final class UniquePermutationsTests: XCTestCase {
16
16
static let numbers = [ 1 , 1 , 1 , 2 , 3 ]
17
17
18
18
static let numbersPermutations : [ [ [ Int ] ] ] = [
19
- // 0
19
+ // k = 0
20
20
[ [ ] ] ,
21
21
// 1
22
22
[ [ 1 ] , [ 2 ] , [ 3 ] ] ,
@@ -44,28 +44,27 @@ final class UniquePermutationsTests: XCTestCase {
44
44
[ 2 , 1 , 1 , 1 , 3 ] , [ 2 , 1 , 1 , 3 , 1 ] , [ 2 , 1 , 3 , 1 , 1 ] , [ 2 , 3 , 1 , 1 , 1 ] ,
45
45
[ 3 , 1 , 1 , 1 , 2 ] , [ 3 , 1 , 1 , 2 , 1 ] , [ 3 , 1 , 2 , 1 , 1 ] , [ 3 , 2 , 1 , 1 , 1 ] ]
46
46
]
47
-
48
- static var numbersAndNils : [ Int ? ] {
49
- numbers. map { $0 == 1 ? nil : $0 }
50
- }
51
-
52
- static var numbersAndNilsPermutations : [ [ ArraySlice < Int ? > ] ] {
53
- numbersPermutations. map { $0. map { ArraySlice ( $0. map { $0 == 1 ? nil : $0 } ) } }
54
- }
55
47
}
56
48
57
49
extension UniquePermutationsTests {
58
50
func testEmpty( ) {
59
51
XCTAssertEqualSequences ( ( [ ] as [ Int ] ) . uniquePermutations ( ) , [ [ ] ] )
60
52
XCTAssertEqualSequences ( ( [ ] as [ Int ] ) . uniquePermutations ( ofCount: 0 ) , [ [ ] ] )
61
53
XCTAssertEqualSequences ( ( [ ] as [ Int ] ) . uniquePermutations ( ofCount: 1 ) , [ ] )
62
- XCTAssertEqualSequences ( ( [ ] as [ Int ] ) . uniquePermutations ( ofCount: 1 ... 3 ) , [ ] )
54
+ XCTAssertEqualSequences (
55
+ ( [ ] as [ Int ] ) . uniquePermutations ( ofCount: 1 ... 3 ) , [ ] )
63
56
}
64
57
65
58
func testSingleCounts( ) {
66
59
for (k, expectation) in Self . numbersPermutations. enumerated ( ) {
67
- XCTAssertEqualSequences ( expectation, Self . numbers. uniquePermutations ( ofCount: k) )
60
+ XCTAssertEqualSequences (
61
+ expectation,
62
+ Self . numbers. uniquePermutations ( ofCount: k) )
68
63
}
64
+
65
+ XCTAssertEqualSequences (
66
+ Self . numbersPermutations [ 5 ] ,
67
+ Self . numbers. uniquePermutations ( ) )
69
68
}
70
69
71
70
func testRanges( ) {
@@ -87,40 +86,31 @@ extension UniquePermutationsTests {
87
86
}
88
87
}
89
88
}
89
+ }
90
90
91
- // func testEmptyWithPredicate() {
92
- // XCTAssertEqualSequences(([] as [Int?]).uniquePermutations(by: <), [[]])
93
- // XCTAssertEqualSequences(([] as [Int?]).uniquePermutations(ofCount: 0, by: <), [[]])
94
- // XCTAssertEqualSequences(([] as [Int?]).uniquePermutations(ofCount: 1, by: <), [])
95
- // XCTAssertEqualSequences(([] as [Int?]).uniquePermutations(ofCount: 1...3, by: <), [])
96
- // }
97
- //
98
- // func testSingleCountsWithPredicate() {
99
- // for (k, expectation) in Self.numbersAndNilsPermutations.enumerated() {
100
- // XCTAssertEqualSequences(expectation, Self.numbersAndNils.uniquePermutations(ofCount: k, by: <))
101
- // }
102
- // }
103
- //
104
- // func testRangesWithPredicate() {
105
- // let numbersAndNils = Self.numbersAndNils
106
- // let numbersAndNilsPermutations = Self.numbersAndNilsPermutations
107
- //
108
- // for lower in numbersAndNilsPermutations.indices {
109
- // // upper bounded
110
- // XCTAssertEqualSequences(
111
- // numbersAndNilsPermutations[...lower].joined(),
112
- // numbersAndNils.uniquePermutations(ofCount: ...lower, by: <))
113
- //
114
- // // lower bounded
115
- // XCTAssertEqualSequences(
116
- // numbersAndNilsPermutations[lower...].joined(),
117
- // numbersAndNils.uniquePermutations(ofCount: lower..., by: <))
118
- //
119
- // for upper in lower..<numbersAndNilsPermutations.count {
120
- // XCTAssertEqualSequences(
121
- // numbersAndNilsPermutations[lower..<upper].joined(),
122
- // numbersAndNils.uniquePermutations(ofCount: lower..<upper, by: <))
123
- // }
124
- // }
125
- // }
91
+ extension UniquePermutationsTests {
92
+ private final class IntBox : Hashable {
93
+ var value : Int
94
+
95
+ init ( _ value: Int ) {
96
+ self . value = value
97
+ }
98
+
99
+ static func == ( lhs: IntBox , rhs: IntBox ) -> Bool {
100
+ lhs. value == rhs. value
101
+ }
102
+
103
+ func hash( into hasher: inout Hasher ) {
104
+ hasher. combine ( value)
105
+ }
106
+ }
107
+
108
+ func testFirstUnique( ) {
109
+ let numbers = Self . numbers. map ( IntBox . init)
110
+ for k in 0 ... numbers. count {
111
+ for p in numbers. uniquePermutations ( ofCount: k) {
112
+ XCTAssertTrue ( p. filter { $0. value == 1 } . allSatisfy { $0 === numbers [ 0 ] } )
113
+ }
114
+ }
115
+ }
126
116
}
0 commit comments