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