summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/warp/string-indexof-constant-string-length-two.js
blob: 7659aee53ed63ba28659ab9e1196e25cfca697ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Test case to cover String.prototype.{indexOf,lastIndexOf,includes} with a constant string of length two.

const strings = [
  // Empty string.
  "",

  // Latin-1 string.
  "abcdefgh",

  // Two-byte string.
  "\u{101}\u{102}\u{103}\u{104}\u{105}\u{106}\u{107}\u{108}",
].flatMap(x => [
  x,

  // Add leading characters.
  "!".repeat(10) + x,

  // Add trailing characters.
  x + "!".repeat(10),
]).flatMap(x => [
  x,

  // To cover the case when the string is two-byte, but has only Latin-1 contents.
  newString(x, {twoByte: true}),
]);

const searchStrings = [
  // Latin-1 search strings:
  // - at the start of the input string
  "ab",
  // - in the middle of the input string
  "de",
  // - at the end of the input string
  "gh",
  // - not in the input string
  "zz",

  // Two-byte search strings:
  // - at the start of the input string
  "\u{101}\u{102}",
  // - in the middle of the input string
  "\u{104}\u{105}",
  // - at the end of the input string
  "\u{107}\u{108}",
  // - not in the input string
  "\u{1000}\u{1001}",
];

const stringFunctions = [
  "indexOf",
  "lastIndexOf",
  "includes",
];

for (let stringFunction of stringFunctions) {
  for (let searchString of searchStrings) {
    let fn = Function("strings", `
      const expected = strings.map(x => {
        // Prevent Warp compilation when computing the expected results.
        with ({}) ;
        return x.${stringFunction}("${searchString}");
      });

      for (let i = 0; i < 500; ++i) {
        let idx = i % strings.length;
        let str = strings[idx];

        let actual = str.${stringFunction}("${searchString}");
        assertEq(actual, expected[idx]);
      }
    `);
    fn(strings);
  }
}