| 1 |
function makeString(from) { |
| 2 |
var codePoints = []; |
| 3 |
for (var i = 0; i < 16; ++i) { |
| 4 |
codePoints.push(from + i); |
| 5 |
} |
| 6 |
return String.fromCodePoint(...codePoints); |
| 7 |
} |
| 8 |
|
| 9 |
function makeRope() { |
| 10 |
var left = newRope(makeString(0x140), makeString(0x140 + 16)); |
| 11 |
var right = newRope(makeString(0x160), makeString(0x160 + 16)); |
| 12 |
var rope = newRope(left, right); |
| 13 |
return {left, right, rope}; |
| 14 |
} |
| 15 |
|
| 16 |
// Load a character from the left rope child using a constant index. The input |
| 17 |
// to String.prototype.codePointAt is always rope. |
| 18 |
function testLeftChildConstant() { |
| 19 |
for (var i = 0; i < 200; ++i) { |
| 20 |
var {rope} = makeRope(); |
| 21 |
|
| 22 |
var ch = rope.codePointAt(0); |
| 23 |
assertEq(ch, 0x140); |
| 24 |
} |
| 25 |
} |
| 26 |
for (var i = 0; i < 2; ++i) { |
| 27 |
testLeftChildConstant(); |
| 28 |
} |
| 29 |
|
| 30 |
// Load a character from the right rope child using a constant index. The input |
| 31 |
// to String.prototype.codePointAt is always rope. |
| 32 |
function testRightChildConstant() { |
| 33 |
for (var i = 0; i < 200; ++i) { |
| 34 |
var {rope} = makeRope(); |
| 35 |
|
| 36 |
var ch = rope.codePointAt(32); |
| 37 |
assertEq(ch, 0x160); |
| 38 |
} |
| 39 |
} |
| 40 |
for (var i = 0; i < 2; ++i) { |
| 41 |
testRightChildConstant(); |
| 42 |
} |
| 43 |
|
| 44 |
// Load a character from the left rope child using a variable index. The input |
| 45 |
// to String.prototype.codePointAt is always rope. |
| 46 |
function testLeftChildVariable() { |
| 47 |
for (var i = 0; i < 200; ++i) { |
| 48 |
var {left, rope} = makeRope(); |
| 49 |
|
| 50 |
var idx = i % left.length; |
| 51 |
var ch = rope.codePointAt(idx); |
| 52 |
assertEq(ch, 0x140 + idx); |
| 53 |
} |
| 54 |
} |
| 55 |
for (var i = 0; i < 2; ++i) { |
| 56 |
testLeftChildVariable(); |
| 57 |
} |
| 58 |
|
| 59 |
// Load a character from the right rope child using a variable index. The input |
| 60 |
// to String.prototype.codePointAt is always rope. |
| 61 |
function testRightChildVariable() { |
| 62 |
for (var i = 0; i < 200; ++i) { |
| 63 |
var {left, right, rope} = makeRope(); |
| 64 |
|
| 65 |
var idx = i % right.length; |
| 66 |
var ch = rope.codePointAt(left.length + idx); |
| 67 |
assertEq(ch, 0x160 + idx); |
| 68 |
} |
| 69 |
} |
| 70 |
for (var i = 0; i < 2; ++i) { |
| 71 |
testRightChildVariable(); |
| 72 |
} |
| 73 |
|
| 74 |
// Load all characters from both child ropes. This covers the case when the |
| 75 |
// call to String.prototype.codePointAt linearizes the rope. |
| 76 |
function testBothChildren() { |
| 77 |
for (var i = 0; i < 200; ++i) { |
| 78 |
var {rope} = makeRope(); |
| 79 |
|
| 80 |
for (var j = 0; j < rope.length; ++j) { |
| 81 |
var ch = rope.codePointAt(j); |
| 82 |
assertEq(ch, 0x140 + j); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
for (var i = 0; i < 2; ++i) { |
| 87 |
testBothChildren(); |
| 88 |
} |