add more test for suffix Tests

This commit is contained in:
michiboo committed 2021-01-22 16:17:28 +02:00
1 parent 1acc2136d9
commit 813ea7905e
2 files changed
+19 -15

No files matched your search

+13 -14
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2021 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
@@ -14,18 +14,17 @@
//===----------------------------------------------------------------------===//
extension BidirectionalCollection {
@inlinable
public __consuming func Suffix(while predicate: (Element) throws -> Bool
) rethrows -> [Element] {
var result = ContiguousArray<Element>()
self.reverse()
for element in self {
guard try predicate(element) else {
break
}
result.append(element)
public func suffix(
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
let start = startIndex
var result = endIndex
while result != start {
let previous = index(before: result)
guard try predicate(self[previous]) else { break }
result = previous
}
result.reverse()
return Array(result)
return self[result...]
}
}
}
+6 -1
View File
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2021 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
@@ -17,4 +17,9 @@ final class SuffixTests: XCTestCase {
func testSuffix() {
let a = 0...10
XCTAssertEqualSequences(a.suffix(while: { $0 > 5 }), (6...10))
XCTAssertEqualSequences(a.suffix(while: { $0 > 10 }), [])
XCTAssertEqualSequences(a.suffix(while: { $0 > 9 }), [9])
XCTAssertEqualSequences(a.suffix(while: { $0 > -1 }), (0...10))
let empty = (0...).prefix(0)
XCTAssertEqualSequences(empty.suffix(while: { $0 > 10 }), [])
}