/* * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ /** * @test * @bug 4504839 * @summary Basic tests for unsigned operations * @author Joseph D. Darcy */ import java.math.*; public class Unsigned { public static void main(String... args) { int errors = 0; errors += testUnsignedCompare(); if (errors > 0) { throw new RuntimeException(errors + "errors found in unsigned operations."); } } private static int testUnsignedCompare() { int errors = 0; long[] data = { 0L, 1L, 2L, 3L, 0x00000000_80000000L, 0x00000000_FFFFFFFFL, 0x00000001_00000000L, 0x80000000_00000000L, 0x80000000_00000001L, 0x80000000_00000002L, 0x80000000_00000003L, 0x80000000_80000000L, 0xFFFFFFFF_FFFFFFFEL, 0xFFFFFFFF_FFFFFFFFL, }; for(long i : data) { for(long j : data) { long libraryResult = Long.compareUnsigned(i, j); long libraryResultRev = Long.compareUnsigned(j, i); long localResult = compUnsigned(i, j); if (i == j) { if (libraryResult != 0) { errors++; System.err.printf("Value 0x%x did not compare as " + "an unsigned equal to itself; got %d%n", i, libraryResult); } } if (Long.signum(libraryResult) != Long.signum(localResult)) { errors++; System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" + "\texpected sign of %d, got %d%n", i, j, localResult, libraryResult); } if (Long.signum(libraryResult) != -Long.signum(libraryResultRev)) { errors++; System.err.printf("unsgnCmp(x, y) != - unsignedComp(y,x) for" + "\t0x%x and 0x%x, computed %d and %d%n", i, j, libraryResult, libraryResultRev); } } } return errors; } private static int compUnsigned(long x, long y) { BigInteger big_x = toUnsignedBigInt(x); BigInteger big_y = toUnsignedBigInt(y); return big_x.compareTo(big_y); } private static BigInteger toUnsignedBigInt(long x) { if (x >= 0) return BigInteger.valueOf(x); else { int upper = (int)(((long)x) >> 32); int lower = (int) x; BigInteger bi = // (upper << 32) + lower (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32). add(BigInteger.valueOf(Integer.toUnsignedLong(lower))); System.out.printf("%n\t%d%n\t%s%n", x, bi.toString()); return bi; // TODO } } }