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