summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/large-arraybuffers/arraybuffer-transfer.js
blob: 3f522e741c961a1f1fe10433693f538fac2f4c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const gb = 1 * 1024 * 1024 * 1024;

const smallByteLength = 1024;
const largeByteLength = 4 * gb;

// Fast alternative to |assertEq| to avoid a slow VM-call.
// Should only be used when looping over the TypedArray contents to avoid
// unnecessary test slowdowns.
function assertEqNum(e, a) {
  if (e !== a) {
    assertEq(e, a);
  }
}

{
  let smallBuffer = new ArrayBuffer(smallByteLength);

  let i8 = new Uint8Array(smallBuffer);
  for (let i = 0; i < smallByteLength; ++i) {
    assertEqNum(i8[i], 0);
    i8[i] = i;
  }

  assertEq(smallBuffer.byteLength, smallByteLength);
  assertEq(smallBuffer.detached, false);

  // Copy from a small into a large buffer.
  let largeBuffer = smallBuffer.transfer(largeByteLength);

  assertEq(smallBuffer.byteLength, 0);
  assertEq(smallBuffer.detached, true);

  assertEq(largeBuffer.byteLength, largeByteLength);
  assertEq(largeBuffer.detached, false);

  i8 = new Uint8Array(largeBuffer);
  for (let i = 0; i < smallByteLength; ++i) {
    assertEqNum(i8[i], i & 0xff);
  }

  // Test the first 100 new bytes.
  for (let i = smallByteLength; i < smallByteLength + 100; ++i) {
    assertEqNum(i8[i], 0);
  }

  // And the last 100 new bytes.
  for (let i = largeByteLength - 100; i < largeByteLength; ++i) {
    assertEqNum(i8[i], 0);
  }

  // Copy back from a large into a small buffer.
  smallBuffer = largeBuffer.transfer(smallByteLength);

  assertEq(largeBuffer.byteLength, 0);
  assertEq(largeBuffer.detached, true);

  assertEq(smallBuffer.byteLength, smallByteLength);
  assertEq(smallBuffer.detached, false);

  i8 = new Uint8Array(smallBuffer);
  for (let i = 0; i < smallByteLength; ++i) {
    assertEqNum(i8[i], i & 0xff);
  }
}