Path: blob/master/test/jdk/java/lang/Math/MultiplicationTests.java
41149 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main MultiplicationTests28* @bug 510093529* @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed)30* @key randomness31*/3233import java.math.BigInteger;34import jdk.test.lib.RandomFactory;3536public class MultiplicationTests {37private MultiplicationTests(){}3839// Number of random products to test.40private static final int COUNT = 1 << 16;4142// Initialize shared random number generator43private static java.util.Random rnd = RandomFactory.getRandom();4445// Calculate high 64 bits of 128 product using BigInteger.46private static long multiplyHighBigInt(long x, long y) {47return BigInteger.valueOf(x).multiply(BigInteger.valueOf(y))48.shiftRight(64).longValue();49}5051// Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y)52private static boolean check(long x, long y) {53long p1 = multiplyHighBigInt(x, y);54long p2 = Math.multiplyHigh(x, y);55if (p1 != p2) {56System.err.printf("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2);57return false;58} else {59return true;60}61}6263private static int testMultiplyHigh() {64int failures = 0;6566// check some boundary cases67long[][] v = new long[][]{68{0L, 0L},69{-1L, 0L},70{0L, -1L},71{1L, 0L},72{0L, 1L},73{-1L, -1L},74{-1L, 1L},75{1L, -1L},76{1L, 1L},77{Long.MAX_VALUE, Long.MAX_VALUE},78{Long.MAX_VALUE, -Long.MAX_VALUE},79{-Long.MAX_VALUE, Long.MAX_VALUE},80{Long.MAX_VALUE, Long.MIN_VALUE},81{Long.MIN_VALUE, Long.MAX_VALUE},82{Long.MIN_VALUE, Long.MIN_VALUE}83};8485for (long[] xy : v) {86if(!check(xy[0], xy[1])) {87failures++;88}89}9091// check some random values92for (int i = 0; i < COUNT; i++) {93if (!check(rnd.nextLong(), rnd.nextLong())) {94failures++;95}96}9798return failures;99}100101public static void main(String argv[]) {102int failures = testMultiplyHigh();103104if (failures > 0) {105System.err.println("Multiplication testing encountered "106+ failures + " failures.");107throw new RuntimeException();108} else {109System.out.println("MultiplicationTests succeeded");110}111}112}113114115