tor-browser

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

string-at-rope.js (2520B)


      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};
      6 }
      7 
      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();
     13 
     14    var ch = rope.at(0);
     15    assertEq(ch, "@");
     16  }
     17 }
     18 for (var i = 0; i < 2; ++i) {
     19  testLeftChildConstant();
     20 }
     21 
     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();
     27 
     28    var ch = rope.at(32);
     29    assertEq(ch, "`");
     30  }
     31 }
     32 for (var i = 0; i < 2; ++i) {
     33  testRightChildConstant();
     34 }
     35 
     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();
     41 
     42    var idx = i % left.length;
     43    var ch = rope.at(idx);
     44    assertEq(ch, String.fromCharCode(0x40 + idx));
     45  }
     46 }
     47 for (var i = 0; i < 2; ++i) {
     48  testLeftChildVariable();
     49 }
     50 
     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();
     56 
     57    var idx = i % right.length;
     58    var ch = rope.at(left.length + idx);
     59    assertEq(ch, String.fromCharCode(0x60 + idx));
     60  }
     61 }
     62 for (var i = 0; i < 2; ++i) {
     63  testRightChildVariable();
     64 }
     65 
     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();
     71 
     72    for (var j = 0; j < rope.length; ++j) {
     73      var ch = rope.at(j);
     74      assertEq(ch, String.fromCharCode(0x40 + j));
     75    }
     76  }
     77 }
     78 for (var i = 0; i < 2; ++i) {
     79  testBothChildren();
     80 }
     81 
     82 // Load a character from the left rope child using an absent index. The input
     83 // to String.prototype.at is always rope.
     84 function testLeftChildAbsentIndex() {
     85  for (var i = 0; i < 200; ++i) {
     86    var {rope} = makeRope();
     87 
     88    var ch = rope.at(/* ToInteger(ToNumber(undefined)) = ToInteger(NaN) = 0 */);
     89    assertEq(ch, "@");
     90  }
     91 }
     92 for (var i = 0; i < 2; ++i) {
     93  testLeftChildAbsentIndex();
     94 }