| 1 |
// Type conversion to number should use ECMA-style semantics. |
| 2 |
|
| 3 |
load(libdir + 'asserts.js'); |
| 4 |
|
| 5 |
function test() { |
| 6 |
function checkValue(type, provided, expected) { |
| 7 |
assertEq(ctypes[type](provided).value, expected, |
| 8 |
`ctypes.${type}(${provided}) contains unexpected value`); |
| 9 |
} |
| 10 |
|
| 11 |
function checkCantConvert(type, value) { |
| 12 |
var ctor = ctypes[type]; |
| 13 |
assertTypeErrorMessage(() => ctor(value), |
| 14 |
/can't convert the number/); |
| 15 |
} |
| 16 |
|
| 17 |
let testInt8 = checkValue.bind(undefined, "int8_t"); |
| 18 |
let testInt8Throws = checkCantConvert.bind(undefined, "int8_t"); |
| 19 |
testInt8(1e100, 0); |
| 20 |
testInt8Throws(-129); |
| 21 |
testInt8(-128, -128); |
| 22 |
testInt8(-1, -1); |
| 23 |
testInt8(0, 0); |
| 24 |
testInt8(1, 1); |
| 25 |
testInt8(127, 127); |
| 26 |
testInt8Throws(128); |
| 27 |
|
| 28 |
let testUint8 = checkValue.bind(undefined, "uint8_t"); |
| 29 |
let testUint8Throws = checkCantConvert.bind(undefined, "uint8_t"); |
| 30 |
testUint8(1e100, 0); |
| 31 |
testUint8Throws(-1); |
| 32 |
testUint8(0, 0); |
| 33 |
testUint8(1, 1); |
| 34 |
testUint8(127, 127); |
| 35 |
testUint8(128, 128); |
| 36 |
testUint8(255, 255); |
| 37 |
testUint8Throws(256); |
| 38 |
|
| 39 |
let testInt16 = checkValue.bind(undefined, "int16_t"); |
| 40 |
let testInt16Throws = checkCantConvert.bind(undefined, "int16_t"); |
| 41 |
testInt16(1e100, 0); |
| 42 |
testInt16Throws(-32769); |
| 43 |
testInt16(-32768, -32768); |
| 44 |
testInt16(-1, -1); |
| 45 |
testInt16(0, 0); |
| 46 |
testInt16(1, 1); |
| 47 |
testInt16(32767, 32767); |
| 48 |
testInt16Throws(32768); |
| 49 |
|
| 50 |
let testUint16 = checkValue.bind(undefined, "uint16_t"); |
| 51 |
let testUint16Throws = checkCantConvert.bind(undefined, "uint16_t"); |
| 52 |
testUint16(1e100, 0); |
| 53 |
testUint16Throws(-1); |
| 54 |
testUint16(0, 0); |
| 55 |
testUint16(1, 1); |
| 56 |
testUint16(32767, 32767); |
| 57 |
testUint16(32768, 32768); |
| 58 |
testUint16(65535, 65535); |
| 59 |
testUint16Throws(65536); |
| 60 |
|
| 61 |
let testInt32 = checkValue.bind(undefined, "int32_t"); |
| 62 |
let testInt32Throws = checkCantConvert.bind(undefined, "int32_t"); |
| 63 |
testInt32(1e100, 0); |
| 64 |
// This probably should pass, but right now doubles fall into a different |
| 65 |
// code path where no error occurs. ctypes is probably/hopefully declining in |
| 66 |
// use now, so just don't bother with this test. |
| 67 |
//testInt32Throws(-2147483649); |
| 68 |
testInt32(-2147483648, -2147483648); |
| 69 |
testInt32(-1, -1); |
| 70 |
testInt32(0, 0); |
| 71 |
testInt32(1, 1); |
| 72 |
testInt32(2147483647, 2147483647); |
| 73 |
// This probably should pass, but right now doubles fall into a different |
| 74 |
// code path where no error occurs. ctypes is probably/hopefully declining in |
| 75 |
// use now, so just don't bother with this test. |
| 76 |
//testInt32Throws(2147483648); |
| 77 |
|
| 78 |
let testUint32 = checkValue.bind(undefined, "uint32_t"); |
| 79 |
let testUint32Throws = checkCantConvert.bind(undefined, "uint32_t"); |
| 80 |
testUint32(1e100, 0); |
| 81 |
testUint32Throws(-1); |
| 82 |
// This probably should pass, but right now doubles fall into a different |
| 83 |
// code path where no error occurs. ctypes is probably/hopefully declining in |
| 84 |
// use now, so just don't bother with this test. |
| 85 |
//testUint32Throws(-1 * Math.cos(Math.PI)); // -1.0 encoded as a double |
| 86 |
testUint32(0, 0); |
| 87 |
testUint32(1, 1); |
| 88 |
testUint32(2147483647, 2147483647); |
| 89 |
testUint32(2147483648, 2147483648); |
| 90 |
testUint32(4294967295, 4294967295); |
| 91 |
// This probably should pass, but right now doubles fall into a different |
| 92 |
// code path where no error occurs. ctypes is probably/hopefully declining in |
| 93 |
// use now, so just don't bother with this test. |
| 94 |
//testUint32Throws(4294967296); |
| 95 |
} |
| 96 |
|
| 97 |
if (typeof ctypes === "object") |
| 98 |
test(); |