tor-browser

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

compare-empty-string.js (1063B)


      1 // Test case to cover empty string comparison folding.
      2 //
      3 // MCompare can fold comparison with an empty string constant and replace it
      4 // with |string.length <op> 0|.
      5 
      6 const strings = [
      7  // Zero length string.
      8  "",
      9 
     10  // Uncommon zero length strings.
     11  newString("", {external: true}),
     12 
     13  // Latin-1 strings.
     14  "a",
     15  "รค",
     16  "monkey",
     17 
     18  // Two-byte strings.
     19  "็Œฟ",
     20  "๐Ÿ’",
     21  newString("monkey", {twoByte: true}),
     22 ];
     23 
     24 const operators = [
     25  "==", "===", "!=", "!==",
     26  "<", "<=", ">=", ">",
     27 ];
     28 
     29 for (let op of operators) {
     30  let lhs = x => `${x} ${op} ""`;
     31  let rhs = x => `"" ${op} ${x}`;
     32 
     33  for (let input of [lhs, rhs]) {
     34    let fn = Function("strings", `
     35      const expected = strings.map(x => {
     36        // Prevent Warp compilation when computing the expected results.
     37        with ({}) ;
     38        return ${input("x")};
     39      });
     40 
     41      for (let i = 0; i < 200; ++i) {
     42        let idx = i % strings.length;
     43        let str = strings[idx];
     44        let res = ${input("str")};
     45        assertEq(res, expected[idx]);
     46      }
     47    `);
     48    fn(strings);
     49  }
     50 }