storage/test/unit/test_levenshtein.js

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

mercurial