blob: 68956e2f03344a5cd4257de8285349dcd8f190ac [file] [log] [blame]
<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<script>
description("Exercise WebIDL type conversions.");
quiet = true;
evalAndLog("converter = window.internals.typeConversions()");
debug("");
function verifyAttribute(attribute)
{
shouldBeTrue("'" + attribute + "' in converter", quiet);
shouldBeTrue("typeof converter." + attribute + " === 'number'", quiet);
evalAndLog("converter." + attribute + " = 123", quiet);
shouldBe("converter." + attribute, "123", quiet);
evalAndLog("converter." + attribute + " = 0", quiet);
shouldBe("converter." + attribute, "0", quiet);
}
function convert(attribute, value, expected)
{
if (arguments.length < 3) {
expected = value;
}
evalAndLog("converter." + attribute + " = " + value);
shouldBe("converter." + attribute, expected);
}
function convertThrows(attribute, value)
{
shouldThrow("converter." + attribute + " = " + value);
}
function testNonNumericToNumeric(attribute)
{
convert(attribute, "false", "0");
convert(attribute, "true", "1");
convert(attribute, "''", "0");
convert(attribute, "'1'", "1");
convert(attribute, "'abc'", "0");
convert(attribute, "null", "0");
convert(attribute, "undefined", "0");
}
function testNonNumericToNumericEnforceRange(attribute)
{
convert(attribute, "false", "0");
convert(attribute, "true", "1");
convert(attribute, "''", "0");
convert(attribute, "'1'", "1");
convertThrows(attribute, "'abc'");
convert(attribute, "null", "0");
convertThrows(attribute, "undefined");
}
type = "testLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000");
convert(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000", "-0x80000000");
convert(type, "0xFFFFFFFF", "-1");
convert(type, "-0x80000000");
convert(type, "-0x80000001", "0x7FFFFFFF");
convert(type, "0xDCBA9876543210", "0x76543210");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000");
convert(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convertThrows(type, "0x80000000");
convertThrows(type, "0xFFFFFFFF");
convert(type, "-0x80000000");
convertThrows(type, "-0x80000001");
convertThrows(type, "0xDCBA9876543210");
convertThrows(type, "Number.MAX_VALUE");
convert(type, "Number.MIN_VALUE", "0");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testUnsignedLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1", "0xFFFFFFFF");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80", "0xFFFFFF80");
convert(type, "-0x81", "0xFFFFFF7F");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000", "0xFFFF8000");
convert(type, "-0x8001", "0xFFFF7FFF");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convert(type, "-0x80000000", "0x80000000");
convert(type, "-0x80000001", "0x7FFFFFFF");
convert(type, "0xDCBA9876543210", "0x76543210");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "0xFFFFFFFF");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeUnsignedLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convertThrows(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convertThrows(type, "-0x80");
convertThrows(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convertThrows(type, "-0x8000");
convertThrows(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convertThrows(type, "-0x80000000");
convertThrows(type, "-0x80000001");
convertThrows(type, "0xDCBA9876543210");
convertThrows(type, "Number.MAX_VALUE");
convert(type, "Number.MIN_VALUE", "0");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convertThrows(type, "-1.99");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testLongLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000");
convert(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convert(type, "-0x80000000");
convert(type, "-0x80000001");
convert(type, "-Math.pow(2, 53) + 1");
convert(type, "-Math.pow(2, 53)");
convert(type, "Math.pow(2, 53) - 1");
convert(type, "Math.pow(2, 53)");
convert(type, "0xDCBA9876543210");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeLongLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000");
convert(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convert(type, "-0x80000000");
convert(type, "-0x80000001");
convert(type, "-Math.pow(2, 53) + 1");
convertThrows(type, "-Math.pow(2, 53)");
convert(type, "Math.pow(2, 53) - 1");
convertThrows(type, "Math.pow(2, 53) + 1");
convertThrows(type, "0xDCBA9876543210");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convertThrows(type, "Number.MAX_VALUE");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN", "0");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testUnsignedLongLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1", "0xFFFFFFFFFFFFFFFF");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80", "0xFFFFFFFFFFFFFF80");
convert(type, "-0x81", "0xFFFFFFFFFFFFFF7F");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "-0x8000", "0xFFFFFFFFFFFF8000");
convert(type, "-0x8001", "0xFFFFFFFFFFFF7FFF");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convert(type, "-0x80000000", "0xFFFFFFFF80000000");
convert(type, "-0x80000001", "0xFFFFFFFF7FFFFFFF");
convert(type, "-Math.pow(2, 53) + 1", "Math.pow(2, 64) - Math.pow(2, 53)");
convert(type, "-Math.pow(2, 53)", "Math.pow(2, 64) - Math.pow(2, 53)");
convert(type, "Math.pow(2, 53) - 1");
convert(type, "Math.pow(2, 53)");
convert(type, "0xDCBA9876543210");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "0xFFFFFFFFFFFFFFFF");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeUnsignedLongLong";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convertThrows(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convertThrows(type, "-0x80");
convertThrows(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convertThrows(type, "-0x8000");
convertThrows(type, "-0x8001");
convert(type, "0x7FFFFFFF");
convert(type, "0x80000000");
convert(type, "0xFFFFFFFF");
convertThrows(type, "-0x80000000");
convertThrows(type, "-0x80000001");
convertThrows(type, "-Math.pow(2, 53) + 1");
convertThrows(type, "-Math.pow(2, 53)");
convert(type, "Math.pow(2, 53) - 1");
convertThrows(type, "Math.pow(2, 53)");
convertThrows(type, "0xDCBA9876543210");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convertThrows(type, "Number.MAX_VALUE");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "1.99", "1");
convertThrows(type, "-1.99");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testByte";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80", "-0x80");
convert(type, "-0x7F");
convert(type, "-0x80");
convert(type, "-0x81", "0x7F");
convert(type, "0x1234", "0x34");
convert(type, "0xDCBA9876543210", "0x10");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convert(type, "4660.99", "0x34");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeByte";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convertThrows(type, "0x80");
convert(type, "-0x7F");
convert(type, "-0x80");
convertThrows(type, "-0x81");
convertThrows(type, "0x1234");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convertThrows(type, "Number.MAX_VALUE");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convertThrows(type, "4660.99");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testOctet";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1", "0xFF");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "0x100", "0");
convert(type, "0x101", "1");
convert(type, "-0x80", "0x80");
convert(type, "-0x81", "0x7F");
convert(type, "0x1234", "0x34");
convert(type, "0xDCBA9876543210", "0x10");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "0xFF");
convert(type, "4660.99", "0x34");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeOctet";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convertThrows(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convertThrows(type, "0x100");
convertThrows(type, "0x101");
convertThrows(type, "-0x80");
convertThrows(type, "-0x81");
convertThrows(type, "0x1234");
convertThrows(type, "Number.MAX_VALUE");
convert(type, "Number.MIN_VALUE", "0");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convertThrows(type, "-1.99");
convertThrows(type, "4660.99");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testShort";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000", "-0x8000");
convert(type, "0xFFFF", "-1");
convert(type, "-0x8000");
convert(type, "-0x8001", "0x7FFF");
convert(type, "0xDCBA9876543210", "0x3210");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeShort";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80");
convert(type, "-0x81");
convert(type, "0x7FFF");
convertThrows(type, "0x8000");
convertThrows(type, "0xFFFF");
convert(type, "-0x8000");
convertThrows(type, "-0x8001");
convertThrows(type, "0xDCBA9876543210");
convertThrows(type, "Number.MAX_VALUE");
convert(type, "Number.MIN_VALUE", "0");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "-1");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testUnsignedShort";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convert(type, "-1", "0xFFFF");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convert(type, "-0x80", "0xFF80");
convert(type, "-0x81", "0xFF7F");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convert(type, "0x10000", "0");
convert(type, "-0x8000", "0x8000");
convert(type, "-0x8001", "0x7FFF");
convert(type, "0xDCBA9876543210", "0x3210");
convert(type, "Number.MAX_VALUE", "0");
convert(type, "Number.MIN_VALUE", "0");
convert(type, "-Number.MAX_VALUE", "0");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convert(type, "-1.99", "0xFFFF");
convert(type, "Infinity", "0");
convert(type, "-Infinity", "0");
convert(type, "NaN", "0");
testNonNumericToNumeric(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
type = "testEnforceRangeUnsignedShort";
verifyAttribute(type);
convert(type, "0");
convert(type, "-0", "0");
convertThrows(type, "-1");
convert(type, "1");
convert(type, "0x7F");
convert(type, "0x80");
convert(type, "0xFF");
convertThrows(type, "-0x80");
convertThrows(type, "-0x81");
convert(type, "0x7FFF");
convert(type, "0x8000");
convert(type, "0xFFFF");
convertThrows(type, "0x10000");
convertThrows(type, "-0x8000");
convertThrows(type, "-0x8001");
convert(type, "0x3210");
convertThrows(type, "Number.MAX_VALUE");
convert(type, "Number.MIN_VALUE", "0");
convertThrows(type, "-Number.MAX_VALUE");
convert(type, "-Number.MIN_VALUE", "0");
convert(type, "1.99", "1");
convertThrows(type, "-1.99");
convertThrows(type, "Infinity");
convertThrows(type, "-Infinity");
convertThrows(type, "NaN");
testNonNumericToNumericEnforceRange(type);
convertThrows(type, "{valueOf:function(){throw new Error('custom');}}");
debug("");
evalAndLog("converter.testByteString = '!@#123ABCabc\\x00\\x80\\xFF\\r\\n\\t'");
shouldBeEqualToString("converter.testByteString", "!@#123ABCabc\x00\x80\xFF\r\n\t");
shouldThrow("converter.testByteString = '\\u0100'");
shouldThrow("converter.testByteString = {toString: function() { throw Error(); }}");
shouldBeEqualToString("converter.testByteString", "!@#123ABCabc\x00\x80\xFF\r\n\t");
["true", "123", "null", "undefined"].forEach(function(value) {
evalAndLog("converter.testByteString = " + value);
shouldBeEqualToString("converter.testByteString", value);
});
shouldNotThrow("converter.setTestByteString('abc')");
shouldNotThrow("converter.setTestByteStringDefaultNull('abc')");
shouldThrow("converter.setTestByteString('\\u0100')");
shouldThrow("converter.setTestByteStringDefaultNull('\\u0100')");
shouldThrow("converter.setTestByteString()");
shouldNotThrow("converter.setTestByteStringDefaultNull()");
shouldBeEqualToString("converter.testByteString", "");
evalAndLog("converter.testUSVString = '!@#123ABCabc\\x00\\x80\\xFF\\r\\n\\t'");
shouldBeEqualToString("converter.testUSVString", "!@#123ABCabc\x00\x80\xFF\r\n\t");
evalAndLog("converter.testUSVString = '\\u0100'");
shouldBeEqualToString("converter.testUSVString", "\u0100");
shouldThrow("converter.testUSVString = {toString: function() { throw Error(); }}");
shouldBeEqualToString("converter.testUSVString", "\u0100");
[{input: "\uD800", expected: "\uFFFD"},
{input: "\uDC00", expected: "\uFFFD"},
{input: "\uD800\u0000", expected: "\uFFFD\u0000"},
{input: "\uDC00\u0000", expected: "\uFFFD\u0000"},
{input: "\uDC00\uD800", expected: "\uFFFD\uFFFD"},
{input: "\uD834\uDD1E", expected: "\uD834\uDD1E"},
].forEach(function(test) {
evalAndLog("converter.testUSVString = " + JSON.stringify(test.input));
shouldBeEqualToString("converter.testUSVString", test.expected);
});
["true", "123", "null", "undefined"].forEach(function(value) {
evalAndLog("converter.testUSVString = " + value);
shouldBeEqualToString("converter.testUSVString", value);
});
shouldNotThrow("converter.setTestUSVString('abc')");
shouldNotThrow("converter.setTestUSVStringDefaultNull('abc')");
shouldNotThrow("converter.setTestUSVString('\\u0100')");
shouldNotThrow("converter.setTestUSVStringDefaultNull('\\u0100')");
shouldThrow("converter.setTestUSVString()");
shouldNotThrow("converter.setTestUSVStringDefaultNull()");
shouldBeEqualToString("converter.testUSVString", "");
</script>