Debian Codegraph @

js/src/jit-test/tests/cacheir/string-at.js

raw
1 // Test String.prototype.at with negative and non-negative indices.
2
3 function* characters(...ranges) {
4 for (let [start, end] of ranges) {
5 for (let i = start; i <= end; ++i) {
6 yield i;
7 }
8 }
9 }
10
11 const empty = [];
12
13 const ascii = [...characters(
14 [0x41, 0x5A], // A..Z
15 [0x61, 0x7A], // a..z
16 )];
17
18 const latin1 = [...characters(
19 [0xC0, 0xFF], // À..ÿ
20 )];
21
22 const twoByte = [...characters(
23 [0x100, 0x17E], // Ā..ž
24 )];
25
26 function atomize(s) {
27 return Object.keys({[s]: 0})[0];
28 }
29
30 function codePoints() {
31 return [empty, ascii, latin1, twoByte];
32 }
33
34 function toRope(s) {
35 // Ropes have at least two characters.
36 if (s.length < 2) {
37 return s;
38 }
39 if (s.length === 2) {
40 return newRope(s[0], s[1]);
41 }
42 return newRope(s[0], s.substring(1));
43 }
44
45 function makeStrings() {
46 let strings = codePoints()
47 .map(codePoints => String.fromCodePoint(...codePoints))
48 .flatMap(x => [
49 x,
50 toRope(x),
51 newString(x, {twoByte: true}),
52 atomize(x),
53 ]);
54 return strings;
55 }
56
57 function testNonNegativeIndexConstant() {
58 let strings = makeStrings();
59 for (let i = 0; i < 200; ++i) {
60 let str = strings[i % strings.length];
61 let index = 0;
62 let ch = str.at(index);
63 let expected = str.charAt(index);
64 if (expected === "") expected = undefined;
65 assertEq(ch, expected);
66 }
67 }
68 for (let i = 0; i < 2; ++i) {
69 testNonNegativeIndexConstant();
70 }
71
72 function testNonNegativeIndex() {
73 let strings = makeStrings();
74 for (let i = 0; i < 200; ++i) {
75 let str = strings[i % strings.length];
76 let index = i & 3;
77 let ch = str.at(index);
78 let expected = str.charAt(index);
79 if (expected === "") expected = undefined;
80 assertEq(ch, expected);
81 }
82 }
83 for (let i = 0; i < 2; ++i) {
84 testNonNegativeIndex();
85 }
86
87 function testNegativeIndexConstant() {
88 let strings = makeStrings();
89 for (let i = 0; i < 200; ++i) {
90 let str = strings[i % strings.length];
91 let index = -1;
92 let ch = str.at(index);
93 let expected = str.charAt(str.length + index);
94 if (expected === "") expected = undefined;
95 assertEq(ch, expected);
96 }
97 }
98 for (let i = 0; i < 2; ++i) {
99 testNegativeIndexConstant();
100 }
101
102 function testNegativeIndex() {
103 let strings = makeStrings();
104 for (let i = 0; i < 200; ++i) {
105 let str = strings[i % strings.length];
106 let index = -(i & 3) - 1;
107 let ch = str.at(index);
108 let expected = str.charAt(str.length + index);
109 if (expected === "") expected = undefined;
110 assertEq(ch, expected);
111 }
112 }
113 for (let i = 0; i < 2; ++i) {
114 testNegativeIndex();
115 }