Bug 1447668 - Add a test. r=jorendorff
authorJeff Walden <jwalden@mit.edu>
Tue, 10 Apr 2018 15:47:25 -0700
changeset 554791 2ce54fbea7dc53831e822d3af55871a4117b9629
parent 554790 893a4487b58644284f8eace41f7604d370eacc93
child 554793 b3cc8963e8718dbd40761f14664f45320c258bbd
push id2165
push userffxbld-merge
push dateMon, 14 Oct 2019 16:30:58 +0000
treeherdermozilla-release@0eae18af659f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs1447668
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1447668 - Add a test. r=jorendorff MANUAL PUSH: Splinter patch reviewed in the pre-phabricator days, using Lando would require posting to phabricator, generating redundant-with-history reviewspam for jorendorff, me clicking through a bunch of nonsense about it "not being reviewed yadda yadda" to Lando it, &c. Doin' it old-school...
js/src/jit-test/tests/ctypes/conversion-to-number.js
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/ctypes/conversion-to-number.js
@@ -0,0 +1,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();