summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/lib/dataview.js
blob: 68228f324b0298b1c7dd90a37be8bb29b7a9c2f1 (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
71
72
73
74
75
76
77
78
79
80
81
82
function typeName(typedArrayCtor) {
  return typedArrayCtor.name.slice(0, -"Array".length);
}

const nativeIsLittleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;

function toEndianess(type, v, littleEndian) {
  // Disable Ion compilation to call the native, non-inlined DataView functions.
  with ({}); // no-ion
  assertEq(inIon() !== true, true);

  let dv = new DataView(new ArrayBuffer(type.BYTES_PER_ELEMENT));

  let name = typeName(type);
  dv[`set${name}`](0, v, nativeIsLittleEndian);
  return dv[`get${name}`](0, littleEndian);
}

function toLittleEndian(type, v) {
  return toEndianess(type, v, true);
}

function toBigEndian(type, v) {
  return toEndianess(type, v, false);
}

// Shared test data for various DataView tests.
function createTestData() {
  const tests = [
    {
      type: Int8Array,
      values: [-128, -127, -2, -1, 0, 1, 2, 126, 127],
    },
    {
      type: Uint8Array,
      values: [0, 1, 2, 126, 127, 128, 254, 255],
    },
    {
      type: Int16Array,
      values: [-32768, -32767, -2, -1, 0, 1, 2, 32766, 32767],
    },
    {
      type: Uint16Array,
      values: [0, 1, 2, 32766, 32767, 32768, 65534, 65535],
    },
    {
      type: Int32Array,
      values: [-2147483648, -2147483647, -2, -1, 0, 1, 2, 2147483646, 2147483647],
    },
    {
      type: Uint32Array,
      values: [0, 1, 2, 2147483646, 2147483647],  // Representable as Int32
    },
    {
      type: Uint32Array,
      values: [0, 1, 2, 2147483646, 2147483647, 2147483648, 4294967294, 4294967295],
    },
    {
      type: Float32Array,
      values: [-NaN, -Infinity, -0.5, -0, +0, 0.5, Infinity, NaN],
    },
    {
      type: Float64Array,
      values: [-NaN, -Infinity, -0.5, -0, +0, 0.5, Infinity, NaN],
    },
    {
      type: BigInt64Array,
      values: [-9223372036854775808n, -9223372036854775807n, -2n, -1n, 0n, 1n, 2n, 9223372036854775806n, 9223372036854775807n],
    },
    {
      type: BigUint64Array,
      values: [0n, 1n, 2n, 9223372036854775806n, 9223372036854775807n, 9223372036854775808n, 18446744073709551614n, 18446744073709551615n],
    },
  ];

  tests.forEach(data => {
    data.littleEndian = data.values.map(v => toLittleEndian(data.type, v));
    data.bigEndian = data.values.map(v => toBigEndian(data.type, v));
  });

  return tests;
}