-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathWindowsTests.swift
111 lines (90 loc) · 3.57 KB
/
WindowsTests.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
//===----------------------------------------------------------------------===//
//
// 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 WindowsTests: XCTestCase {
func testWindowsOfString() {
let s = "swift"
let w = s.windows(ofCount: 2)
var i = w.startIndex
XCTAssertEqualSequences(w[i], "sw")
w.formIndex(after: &i)
XCTAssertEqualSequences(w[i], "wi")
w.formIndex(after: &i)
XCTAssertEqualSequences(w[i], "if")
w.formIndex(after: &i)
XCTAssertEqualSequences(w[i], "ft")
// w.index(after: w.endIndex) // ← Precondition failed: windows index is out of range
// w.index(before: w.startIndex) // ← Precondition failed: windows index is out of range
// w.formIndex(after: &i); w[i] // ← Precondition failed: windows index is out of range
}
func testWindowsOfRange() {
let a = 0...100
XCTAssertTrue(a.windows(ofCount: 200).isEmpty)
let w = a.windows(ofCount: 10)
XCTAssertEqualSequences(w.first!, 0..<10)
XCTAssertEqualSequences(w.last!, 91..<101)
}
func testWindowsOfInt() {
let a = [ 0, 1, 0, 1 ].windows(ofCount: 2)
XCTAssertEqual(a.count, 3)
XCTAssertEqual(a.map { $0.reduce(0, +) }, [1, 1, 1])
let a2 = [0, 1, 2, 3, 4, 5, 6].windows(ofCount: 3).map {
$0.reduce(0, +)
}.reduce(0, +)
XCTAssertEqual(a2, 3 + 6 + 9 + 12 + 15)
}
func testWindowsCount() {
let a = [0, 1, 2, 3, 4, 5]
XCTAssertEqual(a.windows(ofCount: 3).count, 4)
let a2 = [0, 1, 2, 3, 4]
XCTAssertEqual(a2.windows(ofCount: 6).count, 0)
let a3 = [Int]()
XCTAssertEqual(a3.windows(ofCount: 2).count, 0)
}
func testWindowsSecondAndLast() {
let a = [0, 1, 2, 3, 4, 5]
let w = a.windows(ofCount: 4)
let snd = w[w.index(after: w.startIndex)]
XCTAssertEqualSequences(snd, [1, 2, 3, 4])
let w2 = a.windows(ofCount: 3)
XCTAssertEqualSequences(w2.last!, [3, 4, 5])
}
func testWindowsIndexAfterAndBefore() {
let a = [0, 1, 2, 3, 4, 5].windows(ofCount: 2)
var i = a.startIndex
a.formIndex(after: &i)
a.formIndex(after: &i)
a.formIndex(before: &i)
XCTAssertEqualSequences(a[i], [1, 2])
}
func testWindowsIndexTraversals() {
let validator = IndexValidator<WindowsOfCountCollection<String>>(
indicesIncludingEnd: { windows in
let endIndex = windows.base.endIndex
let indices = windows.base.indices + [endIndex]
return zip(indices, indices.dropFirst(windows.windowSize))
.map { .init(lowerBound: $0, upperBound: $1) }
+ [.init(lowerBound: endIndex, upperBound: endIndex)]
})
validator.validate("".windows(ofCount: 1), expectedCount: 0)
validator.validate("a".windows(ofCount: 1), expectedCount: 1)
validator.validate("ab".windows(ofCount: 1), expectedCount: 2)
validator.validate("abc".windows(ofCount: 1), expectedCount: 3)
validator.validate("".windows(ofCount: 3), expectedCount: 0)
validator.validate("a".windows(ofCount: 3), expectedCount: 0)
validator.validate("abc".windows(ofCount: 3), expectedCount: 1)
validator.validate("abcdefgh".windows(ofCount: 3), expectedCount: 6)
}
func testWindowsLazy() {
XCTAssertLazyCollection([0, 1, 2, 3].lazy.windows(ofCount: 2))
}
}