Skip to content

Commit

Permalink
Unit test wrapping adjacent pairs collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Zanchelli committed May 7, 2021
1 parent 8d96786 commit 98a5278
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Tests/SwiftAlgorithmsTests/AdjacentPairsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,57 @@ final class AdjacentPairsTests: XCTestCase {
XCTAssertEqualSequences(pairs, [], by: ==)
}

func testZeroElementsWrapping() {
let pairs = (0..<0).adjacentPairs(wrapping: true)
XCTAssertEqual(pairs.startIndex, pairs.endIndex)
XCTAssertEqualSequences(pairs, [], by: ==)
}

func testOneElement() {
let pairs = (0..<1).adjacentPairs()
XCTAssertEqual(pairs.startIndex, pairs.endIndex)
XCTAssertEqualSequences(pairs, [], by: ==)
}

func testOneElementWrapping() {
let pairs = (0..<1).adjacentPairs(wrapping: true)
XCTAssertEqualSequences(pairs, [(0, 0)], by: ==)
}

func testTwoElements() {
let pairs = (0..<2).adjacentPairs()
XCTAssertEqualSequences(pairs, [(0, 1)], by: ==)
}

func testTwoElementsWrapping() {
let pairs = (0..<2).adjacentPairs(wrapping: true)
XCTAssertEqualSequences(pairs, [(0, 1), (1, 0)], by: ==)
}

func testThreeElements() {
let pairs = (0..<3).adjacentPairs()
XCTAssertEqualSequences(pairs, [(0, 1), (1, 2)], by: ==)
}

func testThreeElementsWrapping() {
let pairs = (0..<3).adjacentPairs(wrapping: true)
XCTAssertEqualSequences(pairs, [(0, 1), (1, 2), (2, 0)], by: ==)
}

func testManyElements() {
for n in 4...100 {
let pairs = (0..<n).adjacentPairs()
XCTAssertEqualSequences(pairs, zip(0..., 1...).prefix(n - 1), by: ==)
}
}

func testManyElementsWrapping() {
for n in 4...100 {
let pairs = (0..<n).adjacentPairs(wrapping: true)
XCTAssertEqualSequences(pairs, chain(zip(0..., 1...).prefix(n - 1), [((n - 1), 0)]), by: ==)
}
}

func testIndexTraversals() {
validateIndexTraversals(
(0..<0).adjacentPairs(),
Expand All @@ -104,8 +132,21 @@ final class AdjacentPairsTests: XCTestCase {
(0..<5).adjacentPairs())
}

func testIndexTraversalsWrapping() {
validateIndexTraversals(
(0..<0).adjacentPairs(wrapping: true),
(0..<1).adjacentPairs(wrapping: true),
(0..<2).adjacentPairs(wrapping: true),
(0..<5).adjacentPairs(wrapping: true))
}

func testLaziness() {
XCTAssertLazySequence((0...).lazy.adjacentPairs())
XCTAssertLazyCollection((0..<100).lazy.adjacentPairs())
}

func testLazinessWrapping() {
XCTAssertLazySequence((0...).lazy.adjacentPairs(wrapping: true))
XCTAssertLazyCollection((0..<100).lazy.adjacentPairs(wrapping: true))
}
}

0 comments on commit 98a5278

Please sign in to comment.