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

class DatabaseCollationTests: GRDBTestCase {
    
    func testDefaultCollations() throws {
        let dbQueue = try makeDatabaseQueue()
        try dbQueue.inDatabase { db in
            try db.execute("CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT)")
            try db.execute("INSERT INTO strings VALUES (1, '1')")
            try db.execute("INSERT INTO strings VALUES (2, '2')")
            try db.execute("INSERT INTO strings VALUES (3, '10')")
            try db.execute("INSERT INTO strings VALUES (4, 'a')")
            try db.execute("INSERT INTO strings VALUES (5, 'à')")
            try db.execute("INSERT INTO strings VALUES (6, 'A')")
            try db.execute("INSERT INTO strings VALUES (7, 'Z')")
            try db.execute("INSERT INTO strings VALUES (8, 'z')")
            
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
                [1,3,2,6,7,4,5,8])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.caseInsensitiveCompare.name), id"),
                [1,3,2,4,6,5,7,8])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCaseInsensitiveCompare.name), id"),
                [1,3,2,4,6,5,7,8])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCompare.name), id"),
                [1,3,2,4,6,5,8,7])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedStandardCompare.name), id"),
                [1,2,3,4,6,5,8,7])
        }
    }

    func testCollation() throws {
        let dbQueue = try makeDatabaseQueue()
        let collation = DatabaseCollation("localized_standard") { (string1, string2) in
            let length1 = string1.utf8.count
            let length2 = string2.utf8.count
            if length1 == length2 {
                return (string1 as NSString).compare(string2)
            } else if length1 < length2 {
                return .orderedAscending
            } else {
                return .orderedDescending<commit_msg>Who guessed String ordering would depend on Swift version? Fix a test so that it runs in both Swift 4.1 and Swift 4.2<commit_after>import XCTest
#if GRDBCIPHER
    import GRDBCipher
#elseif GRDBCUSTOMSQLITE
    import GRDBCustomSQLite
#else
    import GRDB
#endif

class DatabaseCollationTests: GRDBTestCase {
    
    func testDefaultCollations() throws {
        let dbQueue = try makeDatabaseQueue()
        try dbQueue.inDatabase { db in
            try db.execute("CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT)")
            try db.execute("INSERT INTO strings VALUES (1, '1')")
            try db.execute("INSERT INTO strings VALUES (2, '2')")
            try db.execute("INSERT INTO strings VALUES (3, '10')")
            try db.execute("INSERT INTO strings VALUES (4, 'a')")
            try db.execute("INSERT INTO strings VALUES (5, 'à')")
            try db.execute("INSERT INTO strings VALUES (6, 'A')")
            try db.execute("INSERT INTO strings VALUES (7, 'Z')")
            try db.execute("INSERT INTO strings VALUES (8, 'z')")
            
            // Swift 4.2 and Swift 4.1 don't sort strings in the same way
            if "z" < "à" {
                XCTAssertEqual(
                    try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
                    [1,3,2,6,7,4,8,5])
            } else {
                XCTAssertEqual(
                    try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"),
                    [1,3,2,6,7,4,5,8])
            }
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.caseInsensitiveCompare.name), id"),
                [1,3,2,4,6,5,7,8])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCaseInsensitiveCompare.name), id"),
                [1,3,2,4,6,5,7,8])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCompare.name), id"),
                [1,3,2,4,6,5,8,7])
            XCTAssertEqual(
                try Int.fetchAll(db, "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedStandardCompare.name), id"),
                [1,2,3,4,6,5,8,7])
        }
    }

    func testCollation() throws {
        let dbQueue = try makeDatabaseQueue()
        let collation = DatabaseCollation("localized_standard") { (string1, string2) in
            let length1 = string1.utf8.count
            let length2 = string2.utf8.count
            if length1 == length2 {
                return (string1 as NSString).compare(string2)
            } else if length1 < length2 {
                return .orderedAscending
            } else {
                return .orderedDescending
{'fraction_non_alphanumeric': 0.10418963616317531, 'fraction_numerical': 0.02719588386622565, 'mean_word_length': 2.140796306982112, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '1551023', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1533, 'n_words': 449}