Tue, 16 Aug 2016 21:41:28 -0700
Added tag jdk8u102-b33 for changeset 36e3c21b5fca
attila@1065 | 1 | /* |
sundar@1482 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@1065 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@1482 | 4 | * |
attila@1065 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@1065 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@1065 | 7 | * published by the Free Software Foundation. |
sundar@1482 | 8 | * |
attila@1065 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@1065 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@1065 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@1065 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@1065 | 13 | * accompanied this code). |
sundar@1482 | 14 | * |
attila@1065 | 15 | * You should have received a copy of the GNU General Public License version |
attila@1065 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@1065 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@1482 | 18 | * |
attila@1065 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@1065 | 20 | * or visit www.oracle.com if you need additional information or have any |
attila@1065 | 21 | * questions. |
attila@1065 | 22 | */ |
attila@1065 | 23 | |
attila@1065 | 24 | /** |
attila@1065 | 25 | * JDK-8058610: must not let long operations overflow |
attila@1065 | 26 | * |
attila@1065 | 27 | * @test |
attila@1065 | 28 | * @run |
attila@1065 | 29 | */ |
attila@1065 | 30 | |
attila@1065 | 31 | function mul(x) { |
attila@1065 | 32 | return x.foo * x.bar; |
attila@1065 | 33 | } |
attila@1065 | 34 | print("=== mul ===") |
attila@1065 | 35 | print(mul({foo: 2147483647, bar: 2147483647})); // 2^31 |
attila@1065 | 36 | print(mul({foo: 17179869184, bar: 2147483647})); // 2^34 |
attila@1065 | 37 | |
attila@1065 | 38 | function self_mul(x) { |
attila@1065 | 39 | return x.foo *= x.bar; |
attila@1065 | 40 | } |
attila@1065 | 41 | print("=== self_mul ===") |
attila@1065 | 42 | print(self_mul({foo: 2147483647, bar: 2147483647})); // 2^31 |
attila@1065 | 43 | print(self_mul({foo: 17179869184, bar: 2147483647})); // 2^34 |
attila@1065 | 44 | |
attila@1065 | 45 | // We'll need to use this function to obtain long values larger in |
attila@1065 | 46 | // magnitude than those precisely representable in a double (2^53), |
attila@1065 | 47 | // as Nashorn's parser will reify such literals as a double. For |
attila@1065 | 48 | // overflow on add and sub we need (2^63)-1. |
attila@1065 | 49 | var parseLong = Java.type("java.lang.Long").parseLong; |
attila@1065 | 50 | |
attila@1065 | 51 | function sub(x) { |
attila@1065 | 52 | return x.foo - x.bar; |
attila@1065 | 53 | } |
attila@1065 | 54 | print("=== sub ===") |
attila@1065 | 55 | print(sub({foo: 2147483647, bar: -2147483647})); // 2^31 |
attila@1065 | 56 | print(sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 |
attila@1065 | 57 | |
attila@1065 | 58 | function self_sub(x) { |
attila@1065 | 59 | return x.foo -= x.bar; |
attila@1065 | 60 | } |
attila@1065 | 61 | print("=== self_sub ===") |
attila@1065 | 62 | print(self_sub({foo: 2147483647, bar: -2147483647})); // 2^31 |
attila@1065 | 63 | print(self_sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 |
attila@1065 | 64 | |
attila@1065 | 65 | function add(x) { |
attila@1065 | 66 | return x.foo + x.bar; |
attila@1065 | 67 | } |
attila@1065 | 68 | print("=== add ===") |
attila@1065 | 69 | print(add({foo: 2147483647, bar: 2147483647})); // 2^31 |
attila@1065 | 70 | print(add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1 |
attila@1065 | 71 | |
attila@1065 | 72 | function self_add(x) { |
attila@1065 | 73 | return x.foo += x.bar; |
attila@1065 | 74 | } |
attila@1065 | 75 | print("=== self_add ===") |
attila@1065 | 76 | print(self_add({foo: 2147483647, bar: 2147483647})); // 2^31 |
attila@1065 | 77 | print(self_add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1 |