test/script/basic/JDK-8144020.js

Tue, 21 Mar 2017 13:41:57 -0700

author
asaha
date
Tue, 21 Mar 2017 13:41:57 -0700
changeset 2160
1df40fe54cd6
parent 1720
c09b105e7be5
permissions
-rw-r--r--

Merge

hannesw@1720 1 /*
hannesw@1720 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
hannesw@1720 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
hannesw@1720 4 *
hannesw@1720 5 * This code is free software; you can redistribute it and/or modify it
hannesw@1720 6 * under the terms of the GNU General Public License version 2 only, as
hannesw@1720 7 * published by the Free Software Foundation.
hannesw@1720 8 *
hannesw@1720 9 * This code is distributed in the hope that it will be useful, but WITHOUT
hannesw@1720 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
hannesw@1720 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
hannesw@1720 12 * version 2 for more details (a copy is included in the LICENSE file that
hannesw@1720 13 * accompanied this code).
hannesw@1720 14 *
hannesw@1720 15 * You should have received a copy of the GNU General Public License version
hannesw@1720 16 * 2 along with this work; if not, write to the Free Software Foundation,
hannesw@1720 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
hannesw@1720 18 *
hannesw@1720 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
hannesw@1720 20 * or visit www.oracle.com if you need additional information or have any
hannesw@1720 21 * questions.
hannesw@1720 22 */
hannesw@1720 23
hannesw@1720 24 /**
hannesw@1720 25 * JDK-8144020: Remove long as an internal numeric type
hannesw@1720 26 *
hannesw@1720 27 * @test
hannesw@1720 28 * @run
hannesw@1720 29 */
hannesw@1720 30
hannesw@1720 31 var LongProvider = Java.type("jdk.nashorn.test.models.LongProvider");
hannesw@1720 32 var Long = Java.type("java.lang.Long");
hannesw@1720 33 var LongClass = Long.class;
hannesw@1720 34 var Integer = Java.type("java.lang.Integer");
hannesw@1720 35 var Double = Java.type("java.lang.Double");
hannesw@1720 36
hannesw@1720 37 var INT = "3";
hannesw@1720 38 var DOUBLE = "5.5";
hannesw@1720 39 var MAX_LONG = "9223372036854775807";
hannesw@1720 40 var MIN_LONG = "-9223372036854775808";
hannesw@1720 41 var BIG_LONG = "281474976710655"; // can be represented as double
hannesw@1720 42 var NEG_LONG = "-281474976710656"; // can be represented as double
hannesw@1720 43 var SMALL_LONG = "13";
hannesw@1720 44
hannesw@1720 45 // Make sure we can pass longs from and to Java without losing precision
hannesw@1720 46 LongProvider.checkLong(LongProvider.getLong(MAX_LONG), MAX_LONG);
hannesw@1720 47 LongProvider.checkLong(LongProvider.getLong(MIN_LONG), MIN_LONG);
hannesw@1720 48 LongProvider.checkLong(LongProvider.getLong(BIG_LONG), BIG_LONG);
hannesw@1720 49 LongProvider.checkLong(LongProvider.getLong(NEG_LONG), NEG_LONG);
hannesw@1720 50 LongProvider.checkLong(LongProvider.getLong(SMALL_LONG), SMALL_LONG);
hannesw@1720 51
hannesw@1720 52 // a polymorphic function that can return various number types
hannesw@1720 53 function getNumber(str) {
hannesw@1720 54 switch (str) {
hannesw@1720 55 case INT: return +INT;
hannesw@1720 56 case DOUBLE: return +DOUBLE;
hannesw@1720 57 default: return Long.parseLong(str);
hannesw@1720 58 }
hannesw@1720 59 }
hannesw@1720 60
hannesw@1720 61 function compareValue(n, str) {
hannesw@1720 62 switch (str) {
hannesw@1720 63 case INT: return Integer.compare(n, Integer.parseInt(str) == 0);
hannesw@1720 64 case DOUBLE: return Double.compare(n, Double.parseDouble(str) == 0);
hannesw@1720 65 default: return Long.compare(n, Long.parseLong(str) == 0);
hannesw@1720 66 }
hannesw@1720 67 }
hannesw@1720 68
hannesw@1720 69 // Call a a function with a sequence of values. The purpose of this is that we can handle
hannesw@1720 70 // longs without losing precision in the presence of optimistic deoptimization, cached callsites, etc.
hannesw@1720 71 function testSequence(fn, values) {
hannesw@1720 72 for (var i in values) {
hannesw@1720 73 fn(values[i]);
hannesw@1720 74 }
hannesw@1720 75 }
hannesw@1720 76
hannesw@1720 77 // We need to use "fresh" (unlinked and un-deoptimized) functions for each of the test runs.
hannesw@1720 78 testSequence(function(str) {
hannesw@1720 79 var n = getNumber(str);
hannesw@1720 80 Assert.assertTrue(compareValue(n, str));
hannesw@1720 81 }, [INT, BIG_LONG, MIN_LONG]);
hannesw@1720 82
hannesw@1720 83 testSequence(function(str) {
hannesw@1720 84 var n = getNumber(str);
hannesw@1720 85 Assert.assertTrue(compareValue(n, str));
hannesw@1720 86 }, [INT, MAX_LONG]);
hannesw@1720 87
hannesw@1720 88 testSequence(function(str) {
hannesw@1720 89 var n = getNumber(str);
hannesw@1720 90 Assert.assertTrue(compareValue(n, str));
hannesw@1720 91 }, [INT, DOUBLE, NEG_LONG]);
hannesw@1720 92
hannesw@1720 93 testSequence(function(str) {
hannesw@1720 94 var n = getNumber(str);
hannesw@1720 95 Assert.assertTrue(compareValue(n, str));
hannesw@1720 96 }, [DOUBLE, MAX_LONG]);
hannesw@1720 97
hannesw@1720 98 testSequence(function(str) {
hannesw@1720 99 var n = getNumber(str);
hannesw@1720 100 Assert.assertTrue(compareValue(n, str));
hannesw@1720 101 }, [DOUBLE, SMALL_LONG, MAX_LONG]);
hannesw@1720 102
hannesw@1720 103 testSequence(function(str) {
hannesw@1720 104 var n = getNumber(str);
hannesw@1720 105 Assert.assertTrue(compareValue(n, str));
hannesw@1720 106 }, [INT, DOUBLE, NEG_LONG, MAX_LONG]);
hannesw@1720 107
hannesw@1720 108 testSequence(function(str) {
hannesw@1720 109 var n = getNumber(str);
hannesw@1720 110 Assert.assertTrue(compareValue(n, str));
hannesw@1720 111 }, [DOUBLE, MAX_LONG, DOUBLE, INT]);
hannesw@1720 112
hannesw@1720 113 // Make sure long arrays make it through Java.from and Java.to without losing precision
hannesw@1720 114 var longArrayType = Java.type("long[]");
hannesw@1720 115 for (var i = 0; i < 3; i++) {
hannesw@1720 116 LongProvider.checkLongArray(Java.to(Java.from(LongProvider.getLongArray(i)), longArrayType), i);
hannesw@1720 117 }
hannesw@1720 118
hannesw@1720 119 l = Long.parseLong(BIG_LONG);
hannesw@1720 120 Assert.assertTrue(l >>> 8 === 0xffffff);
hannesw@1720 121 Assert.assertTrue(l << 8 === -0x100);
hannesw@1720 122 Assert.assertTrue(l + 1 === 0x1000000000000);
hannesw@1720 123 Assert.assertTrue(l - 1 === 0xfffffffffffe);
hannesw@1720 124
hannesw@1720 125 Assert.assertEquals(LongProvider.getLong(MAX_LONG).getClass(), LongClass);
hannesw@1720 126 Assert.assertEquals(LongProvider.getLong(MIN_LONG).getClass(), LongClass);

mercurial