1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/storage/test/unit/test_levenshtein.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// This file tests the Levenshtein Distance function we've registered. 1.9 + 1.10 +function createUtf16Database() 1.11 +{ 1.12 + print("Creating the in-memory UTF-16-encoded database."); 1.13 + let conn = getService().openSpecialDatabase("memory"); 1.14 + conn.executeSimpleSQL("PRAGMA encoding = 'UTF-16'"); 1.15 + 1.16 + print("Make sure the encoding was set correctly and is now UTF-16."); 1.17 + let stmt = conn.createStatement("PRAGMA encoding"); 1.18 + do_check_true(stmt.executeStep()); 1.19 + let enc = stmt.getString(0); 1.20 + stmt.finalize(); 1.21 + 1.22 + // The value returned will actually be UTF-16le or UTF-16be. 1.23 + do_check_true(enc === "UTF-16le" || enc === "UTF-16be"); 1.24 + 1.25 + return conn; 1.26 +} 1.27 + 1.28 +function check_levenshtein(db, s, t, expectedDistance) 1.29 +{ 1.30 + var stmt = db.createStatement("SELECT levenshteinDistance(:s, :t) AS result"); 1.31 + stmt.params.s = s; 1.32 + stmt.params.t = t; 1.33 + try { 1.34 + do_check_true(stmt.executeStep()); 1.35 + do_check_eq(expectedDistance, stmt.row.result); 1.36 + } 1.37 + finally { 1.38 + stmt.reset(); 1.39 + stmt.finalize(); 1.40 + } 1.41 +} 1.42 + 1.43 +function testLevenshtein(db) 1.44 +{ 1.45 + // Basic tests. 1.46 + check_levenshtein(db, "", "", 0); 1.47 + check_levenshtein(db, "foo", "", 3); 1.48 + check_levenshtein(db, "", "bar", 3); 1.49 + check_levenshtein(db, "yellow", "hello", 2); 1.50 + check_levenshtein(db, "gumbo", "gambol", 2); 1.51 + check_levenshtein(db, "kitten", "sitten", 1); 1.52 + check_levenshtein(db, "sitten", "sittin", 1); 1.53 + check_levenshtein(db, "sittin", "sitting", 1); 1.54 + check_levenshtein(db, "kitten", "sitting", 3); 1.55 + check_levenshtein(db, "Saturday", "Sunday", 3); 1.56 + check_levenshtein(db, "YHCQPGK", "LAHYQQKPGKA", 6); 1.57 + 1.58 + // Test SQL NULL handling. 1.59 + check_levenshtein(db, "foo", null, null); 1.60 + check_levenshtein(db, null, "bar", null); 1.61 + check_levenshtein(db, null, null, null); 1.62 + 1.63 + // The levenshteinDistance function allocates temporary memory on the stack 1.64 + // if it can. Test some strings long enough to force a heap allocation. 1.65 + var dots1000 = Array(1001).join("."); 1.66 + var dashes1000 = Array(1001).join("-"); 1.67 + check_levenshtein(db, dots1000, dashes1000, 1000); 1.68 +} 1.69 + 1.70 +function run_test() 1.71 +{ 1.72 + testLevenshtein(getOpenedDatabase()); 1.73 + testLevenshtein(createUtf16Database()); 1.74 +} 1.75 + 1.76 + 1.77 + 1.78 +