tor-browser

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

string-indexof-constant-string-length-two.js (1699B)


      1 // Test case to cover String.prototype.{indexOf,lastIndexOf,includes} with a constant string of length two.
      2 
      3 const strings = [
      4  // Empty string.
      5  "",
      6 
      7  // Latin-1 string.
      8  "abcdefgh",
      9 
     10  // Two-byte string.
     11  "\u{101}\u{102}\u{103}\u{104}\u{105}\u{106}\u{107}\u{108}",
     12 ].flatMap(x => [
     13  x,
     14 
     15  // Add leading characters.
     16  "!".repeat(10) + x,
     17 
     18  // Add trailing characters.
     19  x + "!".repeat(10),
     20 ]).flatMap(x => [
     21  x,
     22 
     23  // To cover the case when the string is two-byte, but has only Latin-1 contents.
     24  newString(x, {twoByte: true}),
     25 ]);
     26 
     27 const searchStrings = [
     28  // Latin-1 search strings:
     29  // - at the start of the input string
     30  "ab",
     31  // - in the middle of the input string
     32  "de",
     33  // - at the end of the input string
     34  "gh",
     35  // - not in the input string
     36  "zz",
     37 
     38  // Two-byte search strings:
     39  // - at the start of the input string
     40  "\u{101}\u{102}",
     41  // - in the middle of the input string
     42  "\u{104}\u{105}",
     43  // - at the end of the input string
     44  "\u{107}\u{108}",
     45  // - not in the input string
     46  "\u{1000}\u{1001}",
     47 ];
     48 
     49 const stringFunctions = [
     50  "indexOf",
     51  "lastIndexOf",
     52  "includes",
     53 ];
     54 
     55 for (let stringFunction of stringFunctions) {
     56  for (let searchString of searchStrings) {
     57    let fn = Function("strings", `
     58      const expected = strings.map(x => {
     59        // Prevent Warp compilation when computing the expected results.
     60        with ({}) ;
     61        return x.${stringFunction}("${searchString}");
     62      });
     63 
     64      for (let i = 0; i < 500; ++i) {
     65        let idx = i % strings.length;
     66        let str = strings[idx];
     67 
     68        let actual = str.${stringFunction}("${searchString}");
     69        assertEq(actual, expected[idx]);
     70      }
     71    `);
     72    fn(strings);
     73  }
     74 }