Files
swift-algorithms/Tests/SwiftAlgorithmsTests/WindowsTests.swift
T
Nate Cook 66ae806bbe Enable CI format check (#253)
* Incorporate swift-format formatting fixes
* Resolve linting issues
* Doc edits, mostly to fix linter-identified issues
* Early exit improvements
* Indentation fixes
* Line lengths to 80 maximum
* Implement case binding rule, plus argument line break improvements
* Implement NoForceUnwrap rule
* Format the tests, as well
* Stop masquerading as XCTest
2025-03-05 09:11:17 -06:00

113 lines
3.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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
expectEqualSequences(w[i], "sw")
w.formIndex(after: &i)
expectEqualSequences(w[i], "wi")
w.formIndex(after: &i)
expectEqualSequences(w[i], "if")
w.formIndex(after: &i)
expectEqualSequences(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)
expectEqualSequences(w.first!, 0..<10)
expectEqualSequences(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)]
expectEqualSequences(snd, [1, 2, 3, 4])
let w2 = a.windows(ofCount: 3)
expectEqualSequences(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)
expectEqualSequences(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() {
requireLazyCollection([0, 1, 2, 3].lazy.windows(ofCount: 2))
}
}