forked from apple/swift-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkedTests.swift
206 lines (169 loc) · 7.15 KB
/
ChunkedTests.swift
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
//===----------------------------------------------------------------------===//
//
// 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
import Algorithms
final class ChunkedTests: XCTestCase {
let fruits = [
"Apple", "Apricot", "Avocado", "Banana",
"Bilberry", "Blackberry", "Blackcurrant", "Blueberry",
"Currant", "Cherry", "Cherimoya", "Clementine",
"Date", "Damson", "Dragonfruit", "Durian",
"Eggplant", "Elderberry", "Feijoa",
"Grape", "Grapefruit", "Guava",
]
func validateFruitChunks<C: BidirectionalCollection>(_ fruitChunks: C)
where C.Element == ArraySlice<String>
{
let expectedChunks: Array<ArraySlice<String>> = [
fruits[0..<3],
fruits[3..<8],
fruits[8..<12],
fruits[12..<16],
fruits[16..<18],
fruits[18..<19],
fruits[19..<22],
]
XCTAssertEqualSequences(expectedChunks, fruitChunks, by: ==)
XCTAssertEqual(fruits[19..<22], fruitChunks.last)
XCTAssertEqual("Currant", fruitChunks.first(where: { $0.count == 4 })?.first)
XCTAssertEqual("Date", fruitChunks.last(where: { $0.count == 4 })?.first)
XCTAssertNil(fruitChunks.first(where: { $0.count == 0 }))
XCTAssertNil(fruitChunks.last(where: { $0.count == 0 }))
}
func testSimple() {
// Example
let names = ["David", "Kyle", "Karoy", "Nate"]
let chunks = names.chunked(on: { $0.first! })
let expected: [(Character, ArraySlice<String>)] = [
("D", ["David"]),
("K", ["Kyle", "Karoy"]),
("N", ["Nate"])]
XCTAssertEqualSequences(expected, chunks, by: ==)
// Empty sequence
XCTAssertEqual(0, names.prefix(0).chunked(on: { $0.first }).count)
// Single chunk
let namesStartingWithD = ["David", "Don", "Darren"]
XCTAssertEqual(1, namesStartingWithD.chunked(on: { $0.first }).count)
}
func testChunkedOn() {
validateFruitChunks(fruits.chunked(on: { $0.first }).map { $1 })
let lazyChunks = fruits.lazy.chunked(on: { $0.first })
validateFruitChunks(lazyChunks.map { $1 })
IndexValidator().validate(lazyChunks)
}
func testChunkedBy() {
validateFruitChunks(fruits.chunked(by: { $0.first == $1.first }))
let lazyChunks = fruits.lazy.chunked(by: { $0.first == $1.first })
validateFruitChunks(lazyChunks)
IndexValidator().validate(lazyChunks)
}
func testChunkedByComparesConsecutiveElements() {
XCTAssertEqualSequences(
[1, 2, 3, 4, 6, 7, 8, 9].chunked(by: { $1 - $0 == 1 }),
[[1, 2, 3, 4], [6, 7, 8, 9]])
XCTAssertEqualSequences(
[1, 2, 3, 4, 6, 7, 8, 9].lazy.chunked(by: { $1 - $0 == 1 }),
[[1, 2, 3, 4], [6, 7, 8, 9]])
XCTAssertEqualSequences(
[1, 2, 3, 4, 6, 7, 8, 9].lazy.chunked(by: { $1 - $0 == 1 }).reversed(),
[[6, 7, 8, 9], [1, 2, 3, 4]])
IndexValidator().validate([1, 2, 3].lazy.chunked(by: { $1 - $0 == 1 }))
}
func testChunkedLazy() {
XCTAssertLazySequence(fruits.lazy.chunked(by: { $0.first == $1.first }))
XCTAssertLazySequence(fruits.lazy.chunked(on: { $0.first }))
}
//===----------------------------------------------------------------------===//
// Tests for `chunks(ofCount:)`
//===----------------------------------------------------------------------===//
func testChunksOfCount() {
XCTAssertEqualSequences([Int]().chunks(ofCount: 1), [])
XCTAssertEqualSequences([Int]().chunks(ofCount: 5), [])
let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
XCTAssertEqualSequences(collection1.chunks(ofCount: 1),
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 3),
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 5),
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 11),
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
let collection2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
XCTAssertEqualSequences(collection2.chunks(ofCount: 3),
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]])
}
func testChunksOfCountBidirectional() {
let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
XCTAssertEqualSequences(collection1.chunks(ofCount: 1).reversed(),
[[10], [9], [8], [7], [6], [5], [4], [3], [2], [1]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 3).reversed(),
[[10], [7, 8, 9], [4, 5, 6], [1, 2, 3]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 5).reversed(),
[[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]])
XCTAssertEqualSequences(collection1.chunks(ofCount: 11).reversed(),
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
let collection2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
XCTAssertEqualSequences(collection2.chunks(ofCount: 3).reversed(),
[[10, 11], [7, 8, 9], [4, 5, 6], [1, 2, 3]])
}
func testChunksOfCountCount() {
XCTAssertEqual([Int]().chunks(ofCount: 1).count, 0)
XCTAssertEqual([Int]().chunks(ofCount: 5).count, 0)
let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
XCTAssertEqual(collection1.chunks(ofCount: 1).count, 10)
XCTAssertEqual(collection1.chunks(ofCount: 3).count, 4)
XCTAssertEqual(collection1.chunks(ofCount: 5).count, 2)
XCTAssertEqual(collection1.chunks(ofCount: 11).count, 1)
let collection2 = (1...50).map { $0 }
XCTAssertEqual(collection2.chunks(ofCount: 9).count, 6)
}
func testEmptyChunksOfCountTraversal() {
let emptyChunks = [Int]().chunks(ofCount: 1)
IndexValidator().validate(emptyChunks, expectedCount: 0)
}
func testChunksOfCountTraversal() {
let validator = IndexValidator<ChunksOfCountCollection<ClosedRange<Int>>>()
for i in 1...10 {
let range = 1...50
let chunks = range.chunks(ofCount: i)
validator.validate(
chunks,
expectedCount: range.count / i + (range.count % i).signum())
}
}
func testEvenChunks() {
XCTAssertEqualSequences(
(0..<10).evenlyChunked(in: 4),
[0..<3, 3..<6, 6..<8, 8..<10])
XCTAssertEqualSequences(
(0..<3).evenlyChunked(in: 5),
[0..<1, 1..<2, 2..<3, 3..<3, 3..<3])
XCTAssertEqualSequences(
"".evenlyChunked(in: 0),
[])
XCTAssertEqualSequences(
"".evenlyChunked(in: 1),
[""])
}
func testEvenChunksIndexTraversals() {
let validator = IndexValidator<EvenlyChunkedCollection<Range<Int>>>()
[
(0..<10).evenlyChunked(in: 1),
(0..<10).evenlyChunked(in: 2),
(0..<10).evenlyChunked(in: 3),
(0..<10).evenlyChunked(in: 10),
(0..<10).evenlyChunked(in: 20),
(0..<0).evenlyChunked(in: 0),
(0..<0).evenlyChunked(in: 1),
(0..<0).evenlyChunked(in: 10),
].forEach { validator.validate($0) }
}
}