storage/test/unit/test_levenshtein.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     5 // This file tests the Levenshtein Distance function we've registered.
     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'");
    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();
    19   // The value returned will actually be UTF-16le or UTF-16be.
    20   do_check_true(enc === "UTF-16le" || enc === "UTF-16be");
    22   return conn;
    23 }
    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 }
    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);
    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);
    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 }
    67 function run_test()
    68 {
    69   testLevenshtein(getOpenedDatabase());
    70   testLevenshtein(createUtf16Database());
    71 }

mercurial