Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This file tests the Levenshtein Distance function we've registered. |
michael@0 | 6 | |
michael@0 | 7 | function createUtf16Database() |
michael@0 | 8 | { |
michael@0 | 9 | print("Creating the in-memory UTF-16-encoded database."); |
michael@0 | 10 | let conn = getService().openSpecialDatabase("memory"); |
michael@0 | 11 | conn.executeSimpleSQL("PRAGMA encoding = 'UTF-16'"); |
michael@0 | 12 | |
michael@0 | 13 | print("Make sure the encoding was set correctly and is now UTF-16."); |
michael@0 | 14 | let stmt = conn.createStatement("PRAGMA encoding"); |
michael@0 | 15 | do_check_true(stmt.executeStep()); |
michael@0 | 16 | let enc = stmt.getString(0); |
michael@0 | 17 | stmt.finalize(); |
michael@0 | 18 | |
michael@0 | 19 | // The value returned will actually be UTF-16le or UTF-16be. |
michael@0 | 20 | do_check_true(enc === "UTF-16le" || enc === "UTF-16be"); |
michael@0 | 21 | |
michael@0 | 22 | return conn; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function check_levenshtein(db, s, t, expectedDistance) |
michael@0 | 26 | { |
michael@0 | 27 | var stmt = db.createStatement("SELECT levenshteinDistance(:s, :t) AS result"); |
michael@0 | 28 | stmt.params.s = s; |
michael@0 | 29 | stmt.params.t = t; |
michael@0 | 30 | try { |
michael@0 | 31 | do_check_true(stmt.executeStep()); |
michael@0 | 32 | do_check_eq(expectedDistance, stmt.row.result); |
michael@0 | 33 | } |
michael@0 | 34 | finally { |
michael@0 | 35 | stmt.reset(); |
michael@0 | 36 | stmt.finalize(); |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function testLevenshtein(db) |
michael@0 | 41 | { |
michael@0 | 42 | // Basic tests. |
michael@0 | 43 | check_levenshtein(db, "", "", 0); |
michael@0 | 44 | check_levenshtein(db, "foo", "", 3); |
michael@0 | 45 | check_levenshtein(db, "", "bar", 3); |
michael@0 | 46 | check_levenshtein(db, "yellow", "hello", 2); |
michael@0 | 47 | check_levenshtein(db, "gumbo", "gambol", 2); |
michael@0 | 48 | check_levenshtein(db, "kitten", "sitten", 1); |
michael@0 | 49 | check_levenshtein(db, "sitten", "sittin", 1); |
michael@0 | 50 | check_levenshtein(db, "sittin", "sitting", 1); |
michael@0 | 51 | check_levenshtein(db, "kitten", "sitting", 3); |
michael@0 | 52 | check_levenshtein(db, "Saturday", "Sunday", 3); |
michael@0 | 53 | check_levenshtein(db, "YHCQPGK", "LAHYQQKPGKA", 6); |
michael@0 | 54 | |
michael@0 | 55 | // Test SQL NULL handling. |
michael@0 | 56 | check_levenshtein(db, "foo", null, null); |
michael@0 | 57 | check_levenshtein(db, null, "bar", null); |
michael@0 | 58 | check_levenshtein(db, null, null, null); |
michael@0 | 59 | |
michael@0 | 60 | // The levenshteinDistance function allocates temporary memory on the stack |
michael@0 | 61 | // if it can. Test some strings long enough to force a heap allocation. |
michael@0 | 62 | var dots1000 = Array(1001).join("."); |
michael@0 | 63 | var dashes1000 = Array(1001).join("-"); |
michael@0 | 64 | check_levenshtein(db, dots1000, dashes1000, 1000); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function run_test() |
michael@0 | 68 | { |
michael@0 | 69 | testLevenshtein(getOpenedDatabase()); |
michael@0 | 70 | testLevenshtein(createUtf16Database()); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | |
michael@0 | 74 | |
michael@0 | 75 |