1 /*
   2  * Copyright (c) 2009, 2012, 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 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 4215269 6322074
  27  * @summary Basic tests for unsigned operations.
  28  * @author Joseph D. Darcy
  29  */
  30 public class Unsigned {
  31     public static void main(String... args) {
  32         int errors = 0;
  33         
  34         errors += testRoundtrip();
  35         errors += testByteToUnsignedInt();
  36         errors += testShortToUnsignedInt();
  37         errors += testUnsignedCompare();
  38         errors += testToUnsignedLong();
  39         errors += testToStringUnsigned();
  40         errors += testParseUnsignedInt();
  41 
  42         if (errors > 0) {
  43             throw new RuntimeException(errors + " errors found in unsigned operations.");
  44         }
  45     }
  46 
  47     private static int testRoundtrip() {
  48         int errors = 0;
  49         
  50         int[] data = {-1, 0, 1};
  51 
  52         for(int datum : data) {
  53             if (Integer.parseUnsignedInt(Integer.toBinaryString(datum), 2) != datum) {
  54                 errors++;
  55                 System.err.println("Bad binary roundtrip conversion of " + datum);
  56             }
  57 
  58             if (Integer.parseUnsignedInt(Integer.toOctalString(datum), 8) != datum) {
  59                 errors++;
  60                 System.err.println("Bad octal roundtrip conversion of " + datum);
  61             }
  62 
  63             if (Integer.parseUnsignedInt(Integer.toHexString(datum), 16) != datum) {
  64                 errors++;
  65                 System.err.println("Bad hex roundtrip conversion of " + datum);
  66             }
  67         }
  68         return errors;
  69     }
  70 
  71     private static int testByteToUnsignedInt() {
  72         int errors = 0;
  73 
  74         for(int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
  75             byte datum = (byte) i;
  76             int ui = Byte.toUnsignedInt(datum);
  77             
  78             if ( (ui & (~0xff)) != 0 ||
  79                  ((byte)ui != datum )) {
  80                 errors++;
  81                 System.err.printf("Bad conversion of byte %d to unsigned int %d%n",
  82                                   datum, ui);
  83             }
  84         }
  85         return errors;
  86     }
  87 
  88     private static int testShortToUnsignedInt() {
  89         int errors = 0;
  90 
  91         for(int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; i++) {
  92             short datum = (short) i;
  93             int ui = Short.toUnsignedInt(datum);
  94             
  95             if ( (ui & (~0xffff)) != 0 ||
  96                  ((short)ui != datum )) {
  97                 errors++;
  98                 System.err.printf("Bad conversion of short %d to unsigned int %d%n",
  99                                   datum, ui);
 100             }
 101         }
 102         return errors;
 103     }
 104 
 105     private static int testUnsignedCompare() {
 106         int errors = 0;
 107         
 108         int[] data = {
 109             0,
 110             1,
 111             2,
 112             3,
 113             0x8000_0000,
 114             0x8000_0001,
 115             0x8000_0002,
 116             0x8000_0003,
 117             0xFFFF_FFFE,
 118             0xFFFF_FFFF,
 119         };
 120 
 121         for(int i : data) {
 122             for(int j : data) {
 123                 int libraryResult    = Integer.compareUnsigned(i, j);
 124                 int libraryResultRev = Integer.compareUnsigned(j, i);
 125                 int localResult      = compUnsigned(i, j);
 126                 
 127                 if (i == j) {
 128                     if (libraryResult != 0) {
 129                         errors++;
 130                         System.err.printf("Value 0x%x did not compare as " +
 131                                           "an unsigned value equal to itself; got %d%n",
 132                                           i, libraryResult);
 133                     }
 134                 }
 135 
 136                 if (Integer.signum(libraryResult) != Integer.signum(localResult)) {
 137                     errors++;
 138                     System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" +
 139                                       "\texpected sign of %d, got %d%n",
 140                                       i, j, localResult, libraryResult);
 141                 }
 142 
 143                 if (Integer.signum(libraryResult) !=
 144                     -Integer.signum(libraryResultRev)) {
 145                     errors++;
 146                     System.err.printf("signum(compareUnsigned(x, y)) != -signum(compareUnsigned(y,x))" +
 147                                       " for \t0x%x and 0x%x, computed %d and %d%n",
 148                                       i, j, libraryResult, libraryResultRev);
 149                 }
 150             }
 151         }
 152 
 153         return errors;
 154     }
 155 
 156     /**
 157      * Straightforward compare unsigned algorithm.
 158      */
 159     private static int compUnsigned(int x, int y) {
 160         int sign_x = x & Integer.MIN_VALUE;
 161         int sign_y = y & Integer.MIN_VALUE;
 162 
 163         int mant_x  = x & (~Integer.MIN_VALUE);
 164         int mant_y  = y & (~Integer.MIN_VALUE);
 165 
 166         if (sign_x == sign_y)
 167             return Integer.compare(mant_x, mant_y);
 168         else {
 169             if (sign_x == 0)
 170                 return -1; // sign x is 0, sign y is 1 => (x < y)
 171             else
 172                 return 1; //  sign x is 1, sign y is 0 => (x > y)
 173         }
 174     }
 175 
 176     private static int testToUnsignedLong() {
 177         int errors = 0;
 178 
 179         int[] data = {
 180             0,
 181             1,
 182             2,
 183             3,
 184             0x1234_5678,
 185             0x8000_0000,
 186             0x8000_0001,
 187             0x8000_0002,
 188             0x8000_0003,
 189             0x8765_4321,
 190             0xFFFF_FFFE,
 191             0xFFFF_FFFF,
 192         };
 193 
 194         for(int datum : data) {
 195             long result = Integer.toUnsignedLong(datum);
 196             
 197             // High-order bits should be zero
 198             if ((result & 0xffff_ffff_0000_0000L) != 0L) {
 199                 errors++;
 200                 System.err.printf("High bits set converting 0x%x to 0x%x%n",
 201                                   datum, result);
 202             }
 203 
 204             // Lower-order bits should be equal to datum.
 205             int lowOrder = (int)(result & 0x0000_0000_ffff_ffffL);
 206             if (lowOrder != datum ) {
 207                 errors++;
 208                 System.err.printf("Low bits not preserved converting 0x%x to 0x%x%n",
 209                                   datum, result);
 210             }
 211         }
 212         return errors;
 213     }
 214 
 215     private static int testToStringUnsigned() {
 216         int errors = 0;
 217 
 218         int[] data = {
 219             0,
 220             1,
 221             2,
 222             3,
 223             99999,
 224             100000,
 225             999999,
 226             100000,
 227             999999999,
 228             1000000000,
 229             0x1234_5678,
 230             0x8000_0000,
 231             0x8000_0001,
 232             0x8000_0002,
 233             0x8000_0003,
 234             0x8765_4321,
 235             0xFFFF_FFFE,
 236             0xFFFF_FFFF,
 237         };
 238 
 239         for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
 240             for(int datum : data) {
 241                 String result1 = Integer.toUnsignedString(datum, radix);
 242                 String result2 = Long.toString(Integer.toUnsignedLong(datum), radix);
 243 
 244                 if (!result1.equals(result2)) {
 245                     errors++;
 246                     System.err.printf("Unexpected string difference converting 0x%x:" +
 247                                       "\t%s %s%n",
 248                                       datum, result1, result2);
 249                 }
 250 
 251                 if (radix == 10) {
 252                     String result3 = Integer.toUnsignedString(datum);
 253                     if (!result2.equals(result3)) {
 254                         errors++;
 255                         System.err.printf("Unexpected string difference converting 0x%x:" +
 256                                           "\t%s %s%n",
 257                                           datum, result3, result2);
 258                     }
 259                 }
 260 
 261                 int parseResult = Integer.parseUnsignedInt(result1, radix);
 262 
 263                 if (parseResult != datum) {
 264                     errors++;
 265                         System.err.printf("Bad roundtrip conversion of %d in base %d" +
 266                                           "\tconverting back ''%s'' resulted in %d%n",
 267                                           datum, radix, result1,  parseResult);
 268                 }
 269             }
 270         }
 271 
 272         return errors;
 273     }
 274 
 275     private static int testParseUnsignedInt() {
 276         int errors = 0;
 277         long maxUnsignedInt = Integer.toUnsignedLong(0xffff_ffff);
 278 
 279         // Values include those between signed Integer.MAX_VALUE and
 280         // unsignted int MAX_VALUE.
 281         long[] inRange = {
 282             0L,
 283             1L,
 284             10L,
 285             2147483646L,   // MAX_VALUE - 1
 286             2147483647L,   // MAX_VALUE
 287             2147483648L,   // MAX_VALUE + 1
 288             
 289             maxUnsignedInt - 1L,
 290             maxUnsignedInt,
 291         };
 292 
 293         for(long value : inRange) {
 294             for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
 295                 String longString = Long.toString(value, radix);
 296                 int intResult = Integer.parseUnsignedInt(longString, radix);
 297 
 298                 if (Integer.toUnsignedLong(intResult) != value) {
 299                     errors++;
 300                     System.err.printf("Bad roundtrip conversion of %d in base %d" +
 301                                       "\tconverting back ''%s'' resulted in %d%n",
 302                                       value, radix, longString,  intResult);
 303                 }
 304             }
 305         }
 306 
 307         String[] outOfRange = {
 308             null,
 309             "",
 310             "-1",
 311             Long.toString(maxUnsignedInt + 1L),
 312             Long.toString(Long.MAX_VALUE)
 313         };
 314 
 315         for(String s : outOfRange) {
 316             try {
 317                 int result = Integer.parseUnsignedInt(s);
 318                 errors++; // Should not reach here
 319                 System.err.printf("Unexpected got %d from an unsigned conversion of %s",
 320                                   result, s);
 321             } catch(NumberFormatException nfe) {
 322                 ; // Correct result
 323             }
 324         }
 325 
 326         return errors;
 327     }
 328 }