tor-browser

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

map-get-object.js (2051B)


      1 // Similar test as "cacheir/map-get-object.js", except that we now perform
      2 // duplicate lookups to ensure GVN works properly.
      3 
      4 // Return a new map, possibly filling some dummy entries to enforce creating
      5 // multiple hash buckets.
      6 function createMap(values, n) {
      7  var xs = [...values];
      8  for (var i = 0; i < n; ++i) {
      9    xs.push({});
     10  }
     11  return new Map(xs.map((x, i) => [x, i + 1]));
     12 }
     13 
     14 function runTest(fn) {
     15  fn(0);
     16  fn(100);
     17 }
     18 
     19 function test_same_map(n) {
     20  var xs = [{}, {}];
     21  var ys = [{}, {}];
     22  var zs = [...xs, ...ys];
     23  var map = createMap(xs, n);
     24 
     25  var N = 100;
     26  var c = 0;
     27  for (var i = 0; i < N; ++i) {
     28    var z = zs[i & 3];
     29    var v = map.get(z);
     30    if (v !== undefined) c += v;
     31    var w = map.get(z);
     32    if (w !== undefined) c += w;
     33  }
     34  assertEq(c, N + N / 2);
     35 }
     36 runTest(test_same_map);
     37 
     38 // Duplicate the above tests, but this time use a different map.
     39 
     40 function test_different_map(n) {
     41  var xs = [{}, {}];
     42  var ys = [{}, {}];
     43  var zs = [...xs, ...ys];
     44  var map1 = createMap(xs, n);
     45  var map2 = createMap(xs, n);
     46 
     47  var N = 100;
     48  var c = 0;
     49  for (var i = 0; i < N; ++i) {
     50    var z = zs[i & 3];
     51    var v = map1.get(z);
     52    if (v !== undefined) c += v;
     53    var w = map2.get(z);
     54    if (w !== undefined) c += w;
     55  }
     56  assertEq(c, N + N / 2);
     57 }
     58 runTest(test_different_map);
     59 
     60 // Test the alias information is correct.
     61 
     62 function test_alias(n) {
     63  var xs = [{}, {}];
     64  var map = createMap([], n);
     65 
     66  var N = 100;
     67  var c = 0;
     68  for (var i = 0; i < N; ++i) {
     69    var x = xs[i & 1];
     70 
     71    map.set(x, 1);
     72    var v = map.get(x);
     73 
     74    map.delete(x);
     75    var w = map.get(x);
     76 
     77    c += v;
     78    assertEq(w, undefined);
     79  }
     80  assertEq(c, N);
     81 }
     82 runTest(test_alias);
     83 
     84 // And finally test that we don't actually support GVN for objects, because the
     85 // hash changes when moving an object.
     86 
     87 function testRekey() {
     88  var map = new Map();
     89  var c = 0;
     90  var N = 100;
     91  for (var i = 0; i < N; ++i) {
     92    var k = {};
     93    map.set(k, 1);
     94 
     95    c += map.get(k);
     96 
     97    minorgc();
     98 
     99    c += map.get(k);
    100  }
    101 
    102  assertEq(c, N * 2);
    103 }
    104 testRekey();