1 /*
   2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4504839
  27  * @summary Basic tests for unsigned operations
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.math.*;
  32 
  33 public class Unsigned {
  34     public static void main(String... args) {
  35         int errors = 0;
  36 
  37         errors += testUnsignedCompare();
  38 
  39         if (errors > 0) {
  40             throw new RuntimeException(errors + "errors found in unsigned operations.");
  41         }
  42     }
  43 
  44     private static int testUnsignedCompare() {
  45         int errors = 0;
  46         
  47         long[] data = {
  48             0L,
  49             1L,
  50             2L,
  51             3L,
  52             0x00000000_80000000L,
  53             0x00000000_FFFFFFFFL,
  54             0x00000001_00000000L,
  55             0x80000000_00000000L,
  56             0x80000000_00000001L,
  57             0x80000000_00000002L,
  58             0x80000000_00000003L,
  59             0x80000000_80000000L,
  60             0xFFFFFFFF_FFFFFFFEL,
  61             0xFFFFFFFF_FFFFFFFFL,
  62         };
  63 
  64         for(long i : data) {
  65             for(long j : data) {
  66                 long libraryResult    = Long.compareUnsigned(i, j);
  67                 long libraryResultRev = Long.compareUnsigned(j, i);
  68                 long localResult      = compUnsigned(i, j);
  69                 
  70                 if (i == j) {
  71                     if (libraryResult != 0) {
  72                         errors++;
  73                         System.err.printf("Value 0x%x did not compare as " +
  74                                           "an unsigned equal to itself; got %d%n",
  75                                           i, libraryResult);
  76                     }
  77                 }
  78 
  79                    if (Long.signum(libraryResult) != Long.signum(localResult)) {
  80                        errors++;
  81                        System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" +
  82                                          "\texpected sign of %d, got %d%n",
  83                                          i, j, localResult, libraryResult);
  84                    }
  85 
  86                 if (Long.signum(libraryResult) !=
  87                     -Long.signum(libraryResultRev)) {
  88                     errors++;
  89                     System.err.printf("unsgnCmp(x, y) != - unsignedComp(y,x) for" +
  90                                       "\t0x%x and 0x%x, computed %d and %d%n",
  91                                       i, j, libraryResult, libraryResultRev);
  92                 }
  93             }
  94         }
  95 
  96         return errors;
  97     }
  98 
  99     private static int compUnsigned(long x, long y) {
 100         BigInteger big_x = toUnsignedBigInt(x);
 101         BigInteger big_y = toUnsignedBigInt(y);
 102 
 103         return big_x.compareTo(big_y);
 104     }
 105 
 106     private static BigInteger toUnsignedBigInt(long x) {
 107         if (x >= 0)
 108             return BigInteger.valueOf(x);
 109         else {
 110             int upper = (int)(((long)x) >> 32);
 111             int lower = (int) x;
 112 
 113             BigInteger bi = // (upper << 32) + lower
 114                 (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
 115                 add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
 116 
 117             System.out.printf("%n\t%d%n\t%s%n", x, bi.toString());
 118             return bi; // TODO
 119         }
 120     }
 121 }