Debian Codegraph @

js/src/jit-test/tests/warp/string-indexof-constant-string-length-one.js

raw
1 // Test case to cover String.prototype.{indexOf,lastIndexOf,includes} with a constant string of length one.
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 "a",
31 // - in the middle of the input string
32 "d",
33 // - at the end of the input string
34 "h",
35 // - not in the input string
36 "z",
37
38 // Two-byte search strings:
39 // - at the start of the input string
40 "\u{101}",
41 // - in the middle of the input string
42 "\u{104}",
43 // - at the end of the input string
44 "\u{108}",
45 // - not in the input string
46 "\u{1000}",
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 }