1 /* 2 * Copyright (c) 2016, 2017, 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 */ 23 24 /* 25 * @test 26 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main MultiplicationTests 29 * @bug 5100935 30 * @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed) 31 * @key randomness 32 */ 33 package test.java.lang.StrictMath; 34 35 import java.math.BigInteger; 36 // Android-removed: main(), randomness 37 // import jdk.test.lib.RandomFactory; 38 39 // Android-added: testng. 40 import org.testng.Assert; 41 import org.testng.annotations.Test; 42 import static org.testng.Assert.fail; 43 44 public class MultiplicationTests { MultiplicationTests()45 private MultiplicationTests(){} 46 47 // BEGIN Android-removed: randomness, main(). 48 /* 49 // Number of random products to test. 50 private static final int COUNT = 1 << 16; 51 52 // Initialize shared random number generator 53 private static java.util.Random rnd = RandomFactory.getRandom(); 54 */ 55 // END Android-removed: randomness, main(). 56 57 // Calculate high 64 bits of 128 product using BigInteger. multiplyHighBigInt(long x, long y)58 private static long multiplyHighBigInt(long x, long y) { 59 return BigInteger.valueOf(x).multiply(BigInteger.valueOf(y)) 60 .shiftRight(64).longValue(); 61 } 62 63 // Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y) check(long x, long y)64 private static boolean check(long x, long y) { 65 long p1 = multiplyHighBigInt(x, y); 66 long p2 = StrictMath.multiplyHigh(x, y); 67 if (p1 != p2) { 68 // Android-changed: use testng fail(). 69 //System.err.printf("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2); 70 fail(String.format("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2)); 71 return false; 72 } else { 73 return true; 74 } 75 } 76 77 // Android-changed: testng, return void instead of int. 78 @Test testMultiplyHigh()79 public static void testMultiplyHigh() { 80 int failures = 0; 81 82 // check some boundary cases 83 long[][] v = new long[][]{ 84 {0L, 0L}, 85 {-1L, 0L}, 86 {0L, -1L}, 87 {1L, 0L}, 88 {0L, 1L}, 89 {-1L, -1L}, 90 {-1L, 1L}, 91 {1L, -1L}, 92 {1L, 1L}, 93 {Long.MAX_VALUE, Long.MAX_VALUE}, 94 {Long.MAX_VALUE, -Long.MAX_VALUE}, 95 {-Long.MAX_VALUE, Long.MAX_VALUE}, 96 {Long.MAX_VALUE, Long.MIN_VALUE}, 97 {Long.MIN_VALUE, Long.MAX_VALUE}, 98 {Long.MIN_VALUE, Long.MIN_VALUE} 99 }; 100 101 for (long[] xy : v) { 102 if(!check(xy[0], xy[1])) { 103 failures++; 104 } 105 } 106 107 // Android-added: assert error counter. 108 Assert.assertEquals(failures, 0); 109 110 // BEGIN Android-removed: error counter, randomness, main(). 111 /* 112 // check some random values 113 for (int i = 0; i < COUNT; i++) { 114 if (!check(rnd.nextLong(), rnd.nextLong())) { 115 failures++; 116 } 117 } 118 119 return failures; 120 */ 121 // END Android-removed: error counter, randomness, main(). 122 } 123 124 // BEGIN Android-removed: error counter, randomness, main(). 125 /* 126 public static void main(String argv[]) { 127 int failures = testMultiplyHigh(); 128 129 if (failures > 0) { 130 System.err.println("Multiplication testing encountered " 131 + failures + " failures."); 132 throw new RuntimeException(); 133 } else { 134 System.out.println("MultiplicationTests succeeded"); 135 } 136 } 137 */ 138 }