tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

string-codePointAt-rope-twobyte.js (2748B)


      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 }
     89 
     90 // Load a character from the left rope child using an absent index. The input
     91 // to String.prototype.codePointAt is always rope.
     92 function testLeftChildAbsentIndex() {
     93  for (var i = 0; i < 200; ++i) {
     94    var {rope} = makeRope();
     95 
     96    var ch = rope.codePointAt(/* ToInteger(ToNumber(undefined)) = ToInteger(NaN) = 0 */);
     97    assertEq(ch, 0x140);
     98  }
     99 }
    100 for (var i = 0; i < 2; ++i) {
    101  testLeftChildAbsentIndex();
    102 }