summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/dataview/write-unaligned.js
blob: e8d0b262f4bf29d2c857f8fdfe0a09628dad4090 (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
65
66
67
68
69
70
// Test unaligned write access.

load(libdir + "dataview.js");

// Create a new test function for each scalar type.
function createWrite(data) {
  const name = typeName(data.type);
  const offset = 1;

  return Function("data", `
    const {values, littleEndian, bigEndian} = data;

    // Load from array so that Ion doesn't treat as constants.
    const True = [true, 1];
    const False = [false, 0];

    const src = new ${data.type.name}(values);

    const ab = new ArrayBuffer(${data.type.BYTES_PER_ELEMENT + offset});
    const dv = new DataView(ab);

    const srcUint8 = new Uint8Array(src.buffer);
    const dstUint8 = new Uint8Array(ab);

    function assertSameContents(idx, msg) {
      for (let i = 0; i < ${data.type.BYTES_PER_ELEMENT}; ++i) {
        assertEq(dstUint8[i + ${offset}], srcUint8[i + idx * ${data.type.BYTES_PER_ELEMENT}]);
      }
    }

    for (let i = 0; i < 100; ++i) {
      let j = i % values.length;

      // Skip over NaNs to avoid false-negatives due to NaN canonicalisation.
      if (${name === "Float32" || name === "Float64"}) {
        if (Number.isNaN(bigEndian[j]) || Number.isNaN(littleEndian[j])) {
          continue;
        }
      }

      dstUint8.fill(0);
      dv.set${name}(${offset}, bigEndian[j]);
      assertSameContents(j, "default");

      dstUint8.fill(0);
      dv.set${name}(${offset}, littleEndian[j], true);
      assertSameContents(j, "little");

      dstUint8.fill(0);
      dv.set${name}(${offset}, bigEndian[j], false);
      assertSameContents(j, "big");

      dstUint8.fill(0);
      dv.set${name}(${offset}, littleEndian[j], True[i & 1]);
      assertSameContents(j, "little, dynamic");

      dstUint8.fill(0);
      dv.set${name}(${offset}, bigEndian[j], False[i & 1]);
      assertSameContents(j, "big, dynamic");
    }
  `);
}

for (let data of createTestData()) {
  let f = createWrite(data);

  for (let i = 0; i < 2; ++i) {
    f(data);
  }
}