//===----------------------------------------------------------------------===// // // 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 extension Sequence { func isSorted(by f: (Element, Element) -> Bool) -> Bool { zip(self, self.dropFirst()).allSatisfy { !f($1, $0) } } } extension Sequence where Element: Comparable { func isSorted() -> Bool { isSorted(by: <) } } extension Numeric { func factorial() -> Self { guard self != 0 else { return 1 } return self * (self - 1).factorial() } } struct SplitMix64: RandomNumberGenerator { private var state: UInt64 init(seed: UInt64) { self.state = seed } mutating func next() -> UInt64 { self.state &+= 0x9e3779b97f4a7c15 var z: UInt64 = self.state z = (z ^ (z &>> 30)) &* 0xbf58476d1ce4e5b9 z = (z ^ (z &>> 27)) &* 0x94d049bb133111eb return z ^ (z &>> 31) } } func XCTAssertEqualSequences( _ expression1: @autoclosure () throws -> S1, _ expression2: @autoclosure () throws -> S2, file: StaticString = #file, line: UInt = #line ) rethrows where S1.Element: Equatable, S1.Element == S2.Element { var iterator1 = try expression1().makeIterator() var iterator2 = try expression2().makeIterator() var offset = 0 while true { guard let element1 = iterator1.next() else { if iterator2.next() != nil { XCTFail("First sequence had fewer elements than the second", file: file, line: line) } return } guard let element2 = iterator2.next() else { XCTFail("First sequence had more elements than the second", file: file, line: line) return } XCTAssertEqual(element1, element2, "Elements not equal at offset \(offset)", file: file, line: line) offset += 1 } } func XCTAssertEqualSequences( _ expression1: @autoclosure () throws -> S1, _ expression2: @autoclosure () throws -> S2, by areEquivalent: (S1.Element, S1.Element) -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line ) rethrows where S1.Element == S2.Element { try XCTAssert(expression1().elementsEqual(expression2(), by: areEquivalent), message(), file: file, line: line) } func XCTAssertLazy(_: S) {} /// Tests that all index traversal methods behave as expected. /// /// Verifies the correctness of the implementations of `startIndex`, `endIndex`, /// `indices`, `count`, `isEmpty`, `index(before:)`, `index(after:)`, /// `index(_:offsetBy:)`, `index(_:offsetBy:limitedBy:)`, and /// `distance(from:to:)` by calling them with just about all possible input /// combinations. When provided, the `indices` function is used to to test the /// collection methods against. /// /// - Parameters: /// - collections: The collections to be validated. /// - indices: A closure that returns the expected indices of the given /// collection, including its `endIndex`, in ascending order. Only use this /// parameter if you are able to compute the indices of the collection /// independently of the `Collection` conformance, e.g. by using the /// contents of the collection directly. /// /// - Complexity: O(*n*^3) for each collection, where *n* is the length of the /// collection. func validateIndexTraversals( _ collections: C..., indices: ((C) -> [C.Index])? = nil, file: StaticString = #file, line: UInt = #line ) where C: BidirectionalCollection { for c in collections { let indicesIncludingEnd = indices?(c) ?? (c.indices + [c.endIndex]) let count = indicesIncludingEnd.count - 1 XCTAssertEqual( c.count, count, "Count mismatch", file: file, line: line) XCTAssertEqual( c.isEmpty, count == 0, "Emptiness mismatch", file: file, line: line) XCTAssertEqual( c.startIndex, indicesIncludingEnd.first, "`startIndex` does not equal the first index", file: file, line: line) XCTAssertEqual( c.endIndex, indicesIncludingEnd.last, "`endIndex` does not equal the last index", file: file, line: line) // `index(after:)` do { var index = c.startIndex for (offset, expected) in indicesIncludingEnd.enumerated().dropFirst() { c.formIndex(after: &index) XCTAssertEqual( index, expected, """ `startIndex` incremented \(offset) times does not equal index at \ offset \(offset) """, file: file, line: line) } } // `index(before:)` do { var index = c.endIndex for (offset, expected) in indicesIncludingEnd.enumerated().dropLast().reversed() { c.formIndex(before: &index) XCTAssertEqual( index, expected, """ `endIndex` decremented \(count - offset) times does not equal index \ at offset \(offset) """, file: file, line: line) } } // `indices` XCTAssertEqual(c.indices.count, count) for (offset, index) in c.indices.enumerated() { XCTAssertEqual( index, indicesIncludingEnd[offset], "Index mismatch at offset \(offset) in `indices`", file: file, line: line) } // index comparison for (offsetA, a) in indicesIncludingEnd.enumerated() { XCTAssertEqual( a, a, "Index at offset \(offsetA) does not equal itself", file: file, line: line) XCTAssertFalse( a < a, "Index at offset \(offsetA) is less than itself", file: file, line: line) for (offsetB, b) in indicesIncludingEnd[.., pastLimit: Bool) { for targetOffset in range { let distance = targetOffset - startOffset let end = c.index(start, offsetBy: distance, limitedBy: limit) if pastLimit { XCTAssertNil( end, """ Index at offset \(startOffset) offset by \(distance) limited \ by index at offset \(limitOffset) does not equal `nil` """, file: file, line: line) } else { XCTAssertEqual( end, indicesIncludingEnd[targetOffset], """ Index at offset \(startOffset) offset by \(distance) limited \ by index at offset \(limitOffset) does not equal index at \ offset \(targetOffset) """, file: file, line: line) } } } // forward offsets if limit >= start { // the limit has an effect checkTargetRange(startOffset...limitOffset, pastLimit: false) checkTargetRange((limitOffset + 1)...(count + 1), pastLimit: true) } else { // the limit has no effect checkTargetRange(startOffset...count, pastLimit: false) } // backward offsets if limit <= start { // the limit has an effect checkTargetRange(limitOffset...startOffset, pastLimit: false) checkTargetRange(-1...(limitOffset - 1), pastLimit: true) } else { // the limit has no effect checkTargetRange(0...startOffset, pastLimit: false) } } } } }