hannesw@1720: /* hannesw@1720: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. hannesw@1720: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. hannesw@1720: * hannesw@1720: * This code is free software; you can redistribute it and/or modify it hannesw@1720: * under the terms of the GNU General Public License version 2 only, as hannesw@1720: * published by the Free Software Foundation. hannesw@1720: * hannesw@1720: * This code is distributed in the hope that it will be useful, but WITHOUT hannesw@1720: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or hannesw@1720: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License hannesw@1720: * version 2 for more details (a copy is included in the LICENSE file that hannesw@1720: * accompanied this code). hannesw@1720: * hannesw@1720: * You should have received a copy of the GNU General Public License version hannesw@1720: * 2 along with this work; if not, write to the Free Software Foundation, hannesw@1720: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. hannesw@1720: * hannesw@1720: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA hannesw@1720: * or visit www.oracle.com if you need additional information or have any hannesw@1720: * questions. hannesw@1720: */ hannesw@1720: hannesw@1720: /** hannesw@1720: * JDK-8144020: Remove long as an internal numeric type hannesw@1720: * hannesw@1720: * @test hannesw@1720: * @run hannesw@1720: */ hannesw@1720: hannesw@1720: var LongProvider = Java.type("jdk.nashorn.test.models.LongProvider"); hannesw@1720: var Long = Java.type("java.lang.Long"); hannesw@1720: var LongClass = Long.class; hannesw@1720: var Integer = Java.type("java.lang.Integer"); hannesw@1720: var Double = Java.type("java.lang.Double"); hannesw@1720: hannesw@1720: var INT = "3"; hannesw@1720: var DOUBLE = "5.5"; hannesw@1720: var MAX_LONG = "9223372036854775807"; hannesw@1720: var MIN_LONG = "-9223372036854775808"; hannesw@1720: var BIG_LONG = "281474976710655"; // can be represented as double hannesw@1720: var NEG_LONG = "-281474976710656"; // can be represented as double hannesw@1720: var SMALL_LONG = "13"; hannesw@1720: hannesw@1720: // Make sure we can pass longs from and to Java without losing precision hannesw@1720: LongProvider.checkLong(LongProvider.getLong(MAX_LONG), MAX_LONG); hannesw@1720: LongProvider.checkLong(LongProvider.getLong(MIN_LONG), MIN_LONG); hannesw@1720: LongProvider.checkLong(LongProvider.getLong(BIG_LONG), BIG_LONG); hannesw@1720: LongProvider.checkLong(LongProvider.getLong(NEG_LONG), NEG_LONG); hannesw@1720: LongProvider.checkLong(LongProvider.getLong(SMALL_LONG), SMALL_LONG); hannesw@1720: hannesw@1720: // a polymorphic function that can return various number types hannesw@1720: function getNumber(str) { hannesw@1720: switch (str) { hannesw@1720: case INT: return +INT; hannesw@1720: case DOUBLE: return +DOUBLE; hannesw@1720: default: return Long.parseLong(str); hannesw@1720: } hannesw@1720: } hannesw@1720: hannesw@1720: function compareValue(n, str) { hannesw@1720: switch (str) { hannesw@1720: case INT: return Integer.compare(n, Integer.parseInt(str) == 0); hannesw@1720: case DOUBLE: return Double.compare(n, Double.parseDouble(str) == 0); hannesw@1720: default: return Long.compare(n, Long.parseLong(str) == 0); hannesw@1720: } hannesw@1720: } hannesw@1720: hannesw@1720: // Call a a function with a sequence of values. The purpose of this is that we can handle hannesw@1720: // longs without losing precision in the presence of optimistic deoptimization, cached callsites, etc. hannesw@1720: function testSequence(fn, values) { hannesw@1720: for (var i in values) { hannesw@1720: fn(values[i]); hannesw@1720: } hannesw@1720: } hannesw@1720: hannesw@1720: // We need to use "fresh" (unlinked and un-deoptimized) functions for each of the test runs. hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [INT, BIG_LONG, MIN_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [INT, MAX_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [INT, DOUBLE, NEG_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [DOUBLE, MAX_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [DOUBLE, SMALL_LONG, MAX_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [INT, DOUBLE, NEG_LONG, MAX_LONG]); hannesw@1720: hannesw@1720: testSequence(function(str) { hannesw@1720: var n = getNumber(str); hannesw@1720: Assert.assertTrue(compareValue(n, str)); hannesw@1720: }, [DOUBLE, MAX_LONG, DOUBLE, INT]); hannesw@1720: hannesw@1720: // Make sure long arrays make it through Java.from and Java.to without losing precision hannesw@1720: var longArrayType = Java.type("long[]"); hannesw@1720: for (var i = 0; i < 3; i++) { hannesw@1720: LongProvider.checkLongArray(Java.to(Java.from(LongProvider.getLongArray(i)), longArrayType), i); hannesw@1720: } hannesw@1720: hannesw@1720: l = Long.parseLong(BIG_LONG); hannesw@1720: Assert.assertTrue(l >>> 8 === 0xffffff); hannesw@1720: Assert.assertTrue(l << 8 === -0x100); hannesw@1720: Assert.assertTrue(l + 1 === 0x1000000000000); hannesw@1720: Assert.assertTrue(l - 1 === 0xfffffffffffe); hannesw@1720: hannesw@1720: Assert.assertEquals(LongProvider.getLong(MAX_LONG).getClass(), LongClass); hannesw@1720: Assert.assertEquals(LongProvider.getLong(MIN_LONG).getClass(), LongClass);