Bug 1874683 - Part 10: Inline String.prototype.at in CacheIR. r=jandem
[gecko.git] / js / src / jit-test / tests / cacheir / string-at-rope.js
blobaa41fa1440953535a7d9bcd3a1c5dcf2b23b4382
1 function makeRope() {
2   var left = newRope("@ABCDEFGHIJKLMNO", "PQRSTUVWXYZ[\\]^_");
3   var right = newRope("`abcdefghijklmno", "pqrstuvwxyz{|}~");
4   var rope = newRope(left, right);
5   return {left, right, rope};
8 // Load a character from the left rope child using a constant index. The input
9 // to String.prototype.at is always rope.
10 function testLeftChildConstant() {
11   for (var i = 0; i < 200; ++i) {
12     var {rope} = makeRope();
14     var ch = rope.at(0);
15     assertEq(ch, "@");
16   }
18 for (var i = 0; i < 2; ++i) {
19   testLeftChildConstant();
22 // Load a character from the right rope child using a constant index. The input
23 // to String.prototype.at is always rope.
24 function testRightChildConstant() {
25   for (var i = 0; i < 200; ++i) {
26     var {rope} = makeRope();
28     var ch = rope.at(32);
29     assertEq(ch, "`");
30   }
32 for (var i = 0; i < 2; ++i) {
33   testRightChildConstant();
36 // Load a character from the left rope child using a variable index. The input
37 // to String.prototype.at is always rope.
38 function testLeftChildVariable() {
39   for (var i = 0; i < 200; ++i) {
40     var {left, rope} = makeRope();
42     var idx = i % left.length;
43     var ch = rope.at(idx);
44     assertEq(ch, String.fromCharCode(0x40 + idx));
45   }
47 for (var i = 0; i < 2; ++i) {
48   testLeftChildVariable();
51 // Load a character from the right rope child using a variable index. The input
52 // to String.prototype.at is always rope.
53 function testRightChildVariable() {
54   for (var i = 0; i < 200; ++i) {
55     var {left, right, rope} = makeRope();
57     var idx = i % right.length;
58     var ch = rope.at(left.length + idx);
59     assertEq(ch, String.fromCharCode(0x60 + idx));
60   }
62 for (var i = 0; i < 2; ++i) {
63   testRightChildVariable();
66 // Load all characters from both child ropes. This covers the case when the
67 // call to String.prototype.at linearizes the rope. 
68 function testBothChildren() {
69   for (var i = 0; i < 200; ++i) {
70     var {rope} = makeRope();
72     for (var j = 0; j < rope.length; ++j) {
73       var ch = rope.at(j);
74       assertEq(ch, String.fromCharCode(0x40 + j));
75     }
76   }
78 for (var i = 0; i < 2; ++i) {
79   testBothChildren();