tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

basic.js (3664B)


      1 assertEq(isLatin1("Foo123\u1200"), false);
      2 
      3 s = "Foo123";
      4 assertEq(isLatin1(s), true);
      5 
      6 function testEq(s) {
      7    assertEq(isLatin1(s), true);
      8    assertEq(s === "foo02", false);
      9    assertEq(s == "foo02", false);
     10 
     11    // Non-atomized to force char comparison.
     12    var nonAtomized = "\u1234foo01\u00c7".substr(1);
     13    assertEq(isLatin1(nonAtomized), false);
     14    assertEq(s === nonAtomized, true);
     15    assertEq(nonAtomized !== s, false);
     16    assertEq(nonAtomized == s, true);
     17    assertEq(s, nonAtomized);
     18 
     19    nonAtomized = "\u1234foo02".substr(1);
     20    assertEq(isLatin1(nonAtomized), false);
     21    assertEq(s === nonAtomized, false);
     22    assertEq(nonAtomized == s, false);
     23 }
     24 
     25 s = "foo01\u00c7";
     26 testEq(s);
     27 testEq(s);
     28 
     29 function testConcat() {
     30    function concat(s1, s2) {
     31 return s1 + s2;
     32    }
     33 
     34    // Following tests create fat inline strings.
     35    assertEq(concat("abc", "def"), "abcdef");
     36    var s1 = "ABC";
     37    var s2 = "DEF";
     38    assertEq(concat(s1, s2), "ABCDEF");
     39    assertEq(concat(s1, "GHI\u0580"), "ABCGHI\u0580");
     40    assertEq(concat("GHI\u0580", s2), "GHI\u0580DEF");
     41    assertEq(concat(concat("GHI\u0580", s2), s1), "GHI\u0580DEFABC");
     42    assertEq(isLatin1(s1), true);
     43    assertEq(isLatin1(s2), true);
     44 
     45    // Create a Latin1 rope.
     46    var s3 = "0123456789012345678901234567890123456789";
     47    var rope = concat(s1, s3);
     48    assertEq(isLatin1(rope), true);
     49    assertEq(rope, "ABC0123456789012345678901234567890123456789");
     50    assertEq(isLatin1(rope), true); // Still Latin1 after flattening.
     51 
     52    // Latin1 + TwoByte => TwoByte rope.
     53    assertEq(isLatin1(s3), true);
     54    rope = concat(s3, "someTwoByteString\u0580");
     55    assertEq(isLatin1(rope), false);
     56    assertEq(rope, "0123456789012345678901234567890123456789someTwoByteString\u0580");
     57    assertEq(isLatin1(rope), false);
     58 
     59    assertEq(isLatin1(s3), true);
     60    rope = concat("twoByteString\u0580", concat(s3, "otherTwoByte\u0580"));
     61    assertEq(isLatin1(rope), false);
     62    assertEq(rope, "twoByteString\u05800123456789012345678901234567890123456789otherTwoByte\u0580");
     63    assertEq(isLatin1(rope), false);
     64 
     65    // Build a Latin1 rope with left-most string an extensible string.
     66    var s4 = "adsfasdfjkasdfkjasdfasasdfasdf";
     67    for (var i=0; i<5; i++) {
     68 s4 = concat(s4, s1);
     69 assertEq(s4 === ".".repeat(s4.length), false); // Flatten rope.
     70    }
     71 
     72    assertEq(isLatin1(s4), true);
     73 
     74    // Appending a TwoByte string must inflate.
     75    s4 = concat(s4, "--\u0580");
     76    assertEq(s4, "adsfasdfjkasdfkjasdfasasdfasdfABCABCABCABCABC--\u0580");
     77 }
     78 testConcat();
     79 
     80 function testFlattenDependent() {
     81    function concat(s1, s2) {
     82 return s1 + s2;
     83    }
     84 
     85    // Create some latin1 strings.
     86    var s1 = "Foo0123456789bar012345---";
     87    var s2 = "Foo0123456789bar012345+++";
     88    assertEq(isLatin1(s1), true);
     89    assertEq(isLatin1(s2), true);
     90 
     91    // And some ropes.
     92    var rope1 = concat(s1, s1);
     93    assertEq(isLatin1(rope1), true);
     94 
     95    var rope2 = concat(rope1, s2);
     96    assertEq(isLatin1(rope2), true);
     97 
     98    var rope3 = concat("twoByte\u0581", rope2);
     99    assertEq(isLatin1(rope3), false);
    100 
    101    // Flatten everything.
    102    assertEq(rope3, "twoByte\u0581Foo0123456789bar012345---Foo0123456789bar012345---Foo0123456789bar012345+++");
    103    assertEq(isLatin1(rope3), false);
    104 
    105    // rope1 and rope2 were Latin1, but flattening rope3 turned them into
    106    // dependent strings, so rope1 and rope2 must also be TwoByte now.
    107    assertEq(isLatin1(rope1), false);
    108    assertEq(isLatin1(rope2), false);
    109 
    110    assertEq(rope1, "Foo0123456789bar012345---Foo0123456789bar012345---");
    111    assertEq(rope2, "Foo0123456789bar012345---Foo0123456789bar012345---Foo0123456789bar012345+++");
    112 }
    113 testFlattenDependent();