<commit_before>import XCTest
#if GRDBCIPHER
    import GRDBCipher
#elseif GRDBCUSTOMSQLITE
    import GRDBCustomSQLite
#else
    import GRDB
#endif

private struct TestError : Error { }

class CursorTests: GRDBTestCase {
    
    func testContainsEquatable() {
        XCTAssertTrue(try IteratorCursor([1, 2]).contains(1))
        XCTAssertFalse(try IteratorCursor([1, 2]).contains(3))
    }
    
    func testContainsClosure() {
        XCTAssertTrue(try IteratorCursor([1, 2]).contains { $0 == 1 })
        XCTAssertFalse(try IteratorCursor([1, 2]).contains { $0 == 3 })
        do {
            _ = try IteratorCursor([1, 2]).contains { _ -> Bool in throw TestError() }
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testContainsIsLazy() {
        func makeCursor() -> AnyCursor<Int> {
            var i = 0
            return AnyCursor {
                guard i < 1 else { throw TestError() }
                defer { i += 1 }
                return i
            }
        }
        XCTAssertTrue(try makeCursor().contains(0))
        do {
            _ = try makeCursor().contains(-1)
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testFirst() {
        XCTAssertEqual(try IteratorCursor([1, 2]).first { $0 == 1 }!, 1)
        XCTAssertTrue(try IteratorCursor([1, 2]).first { $0 == 3 } == nil)
    }
    
    func testFirstIsLazy() {
        func makeCursor() -> AnyCursor<Int> {
            var i = 0
            return AnyCursor {
                guard i < 1 else { throw TestError() }
                defer { i += 1 }
                return i
            }
        }
        XCTAssertEqual(try makeCursor().first { $0 == 0 }!, 0)
        do {
            _ = try makeCursor().first { $0 == 1 }
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testFlatMapOfOptional() {
        let cursor = IteratorCursor(["1", "foo", "2"]).flatMap { Int($0) }
        XCTAssertEqual(try cursor.next()!, 1)
        XCTAssertEqual(try cursor.next()!, 2)
        XCTAssertTrue(try cursor.next() == nil) // end
    }
    
    func testForEach() throws {
        let cursor = IteratorCursor([1, 2])
        var ints: [Int] = []
        try cursor.forEach { ints.append($0) }
        XCTAssertEqual(ints, [1, 2])
    }
    
    func testThrowingForEach() {
        var i = 0
        let cursor: AnyCursor<Int> = AnyCursor {
            guard i < 1 else { throw TestError() }
            defer { i += 1 }
            return i
        }
        var ints: [Int] = []
        do {
            try cursor.forEach { ints.append($0) }
            XCTFail()
        } catch is TestError {
            XCTAssertEqual(ints, [0])
        } catch {
            XCTFail()
        }
    }
    
    func testReduce() throws {
        let cursor = IteratorCursor([1, 2])
        let squareSum = try cursor.reduce(0) { (acc, int) in acc + int * int }
        XCTAssertEqual(squareSum, 5)
    }
}<commit_msg>Tests for Cursor.suffix(_:)<commit_after>import XCTest
#if GRDBCIPHER
    import GRDBCipher
#elseif GRDBCUSTOMSQLITE
    import GRDBCustomSQLite
#else
    import GRDB
#endif

private struct TestError : Error { }

class CursorTests: GRDBTestCase {
    
    func testContainsEquatable() {
        XCTAssertTrue(try IteratorCursor([1, 2]).contains(1))
        XCTAssertFalse(try IteratorCursor([1, 2]).contains(3))
    }
    
    func testContainsClosure() {
        XCTAssertTrue(try IteratorCursor([1, 2]).contains { $0 == 1 })
        XCTAssertFalse(try IteratorCursor([1, 2]).contains { $0 == 3 })
        do {
            _ = try IteratorCursor([1, 2]).contains { _ -> Bool in throw TestError() }
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testContainsIsLazy() {
        func makeCursor() -> AnyCursor<Int> {
            var i = 0
            return AnyCursor {
                guard i < 1 else { throw TestError() }
                defer { i += 1 }
                return i
            }
        }
        XCTAssertTrue(try makeCursor().contains(0))
        do {
            _ = try makeCursor().contains(-1)
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testFirst() {
        XCTAssertEqual(try IteratorCursor([1, 2]).first { $0 == 1 }!, 1)
        XCTAssertTrue(try IteratorCursor([1, 2]).first { $0 == 3 } == nil)
    }
    
    func testFirstIsLazy() {
        func makeCursor() -> AnyCursor<Int> {
            var i = 0
            return AnyCursor {
                guard i < 1 else { throw TestError() }
                defer { i += 1 }
                return i
            }
        }
        XCTAssertEqual(try makeCursor().first { $0 == 0 }!, 0)
        do {
            _ = try makeCursor().first { $0 == 1 }
            XCTFail()
        } catch is TestError {
        } catch {
            XCTFail()
        }
    }
    
    func testFlatMapOfOptional() {
        let cursor = IteratorCursor(["1", "foo", "2"]).flatMap { Int($0) }
        XCTAssertEqual(try cursor.next()!, 1)
        XCTAssertEqual(try cursor.next()!, 2)
        XCTAssertTrue(try cursor.next() == nil) // end
    }
    
    func testForEach() throws {
        let cursor = IteratorCursor([1, 2])
        var ints: [Int] = []
        try cursor.forEach { ints.append($0) }
        XCTAssertEqual(ints, [1, 2])
    }
    
    func testThrowingForEach() {
        var i = 0
        let cursor: AnyCursor<Int> = AnyCursor {
            guard i < 1 else { throw TestError() }
            defer { i += 1 }
            return i
        }
        var ints: [Int] = []
        do {
            try cursor.forEach { ints.append($0) }
            XCTFail()
        } catch is TestError {
            XCTAssertEqual(ints, [0])
        } catch {
            XCTFail()
        }
    }
    
    func testReduce() throws {
        let cursor = IteratorCursor([1, 2])
        let squareSum = try cursor.reduce(0) { (acc, int) in acc + int * int }
        XCTAssertEqual(squareSum, 5)
    }
    
    func testSuffix() throws {
        do {
            let cursor = IteratorCursor([1, 2, 3, 4, 5])
            let suffix = try cursor.suffix(0)
            XCTAssertTrue(suffix == [])
        }
        do {
            let cursor = IteratorCursor([1, 2, 3, 4, 5])
            let suffix = try cursor.suffix(2)
            XCTAssertTrue(suffix == [4, 5])
        }
        do {
            let cursor = IteratorCursor([1, 2, 3, 4, 5])
            let suffix = try cursor.suffix(10)
            XCTAssertTrue(suffix == [1, 2, 3, 4, 5])
        }
    }
}
{'fraction_non_alphanumeric': 0.12160566706021252, 'fraction_numerical': 0.02066115702479339, 'mean_word_length': 1.5334579439252336, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491333', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2057, 'n_words': 503}