summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ctypes/conversion-to-number.js
blob: 6a334ecf3aebb424a538b7c7df0cb25ba0b4da82 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Type conversion to number should use ECMA-style semantics.

load(libdir + 'asserts.js');

function test() {
  function checkValue(type, provided, expected) {
    assertEq(ctypes[type](provided).value, expected,
             `ctypes.${type}(${provided}) contains unexpected value`);
  }

  function checkCantConvert(type, value) {
    var ctor = ctypes[type];
    assertTypeErrorMessage(() => ctor(value),
                           /can't convert the number/);
  }

  let testInt8 = checkValue.bind(undefined, "int8_t");
  let testInt8Throws = checkCantConvert.bind(undefined, "int8_t");
  testInt8(1e100, 0);
  testInt8Throws(-129);
  testInt8(-128, -128);
  testInt8(-1, -1);
  testInt8(0, 0);
  testInt8(1, 1);
  testInt8(127, 127);
  testInt8Throws(128);

  let testUint8 = checkValue.bind(undefined, "uint8_t");
  let testUint8Throws = checkCantConvert.bind(undefined, "uint8_t");
  testUint8(1e100, 0);
  testUint8Throws(-1);
  testUint8(0, 0);
  testUint8(1, 1);
  testUint8(127, 127);
  testUint8(128, 128);
  testUint8(255, 255);
  testUint8Throws(256);

  let testInt16 = checkValue.bind(undefined, "int16_t");
  let testInt16Throws = checkCantConvert.bind(undefined, "int16_t");
  testInt16(1e100, 0);
  testInt16Throws(-32769);
  testInt16(-32768, -32768);
  testInt16(-1, -1);
  testInt16(0, 0);
  testInt16(1, 1);
  testInt16(32767, 32767);
  testInt16Throws(32768);

  let testUint16 = checkValue.bind(undefined, "uint16_t");
  let testUint16Throws = checkCantConvert.bind(undefined, "uint16_t");
  testUint16(1e100, 0);
  testUint16Throws(-1);
  testUint16(0, 0);
  testUint16(1, 1);
  testUint16(32767, 32767);
  testUint16(32768, 32768);
  testUint16(65535, 65535);
  testUint16Throws(65536);

  let testInt32 = checkValue.bind(undefined, "int32_t");
  let testInt32Throws = checkCantConvert.bind(undefined, "int32_t");
  testInt32(1e100, 0);
  // This probably should pass, but right now doubles fall into a different
  // code path where no error occurs.  ctypes is probably/hopefully declining in
  // use now, so just don't bother with this test.
  //testInt32Throws(-2147483649);
  testInt32(-2147483648, -2147483648);
  testInt32(-1, -1);
  testInt32(0, 0);
  testInt32(1, 1);
  testInt32(2147483647, 2147483647);
  // This probably should pass, but right now doubles fall into a different
  // code path where no error occurs.  ctypes is probably/hopefully declining in
  // use now, so just don't bother with this test.
  //testInt32Throws(2147483648);

  let testUint32 = checkValue.bind(undefined, "uint32_t");
  let testUint32Throws = checkCantConvert.bind(undefined, "uint32_t");
  testUint32(1e100, 0);
  testUint32Throws(-1);
  // This probably should pass, but right now doubles fall into a different
  // code path where no error occurs.  ctypes is probably/hopefully declining in
  // use now, so just don't bother with this test.
  //testUint32Throws(-1 * Math.cos(Math.PI)); // -1.0 encoded as a double
  testUint32(0, 0);
  testUint32(1, 1);
  testUint32(2147483647, 2147483647);
  testUint32(2147483648, 2147483648);
  testUint32(4294967295, 4294967295);
  // This probably should pass, but right now doubles fall into a different
  // code path where no error occurs.  ctypes is probably/hopefully declining in
  // use now, so just don't bother with this test.
  //testUint32Throws(4294967296);
}

if (typeof ctypes === "object")
  test();