test/jdk/java/lang/Integer/Unsigned.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 26462 jdk/test/java/lang/Integer/Unsigned.java@d6d34934be12
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     1
/*
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     4
 *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     8
 *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    13
 * accompanied this code).
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    14
 *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    18
 *
14856
92a1bcf46888 8004982: JDK8 source with GPL header errors
katleman
parents: 11672
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
92a1bcf46888 8004982: JDK8 source with GPL header errors
katleman
parents: 11672
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
92a1bcf46888 8004982: JDK8 source with GPL header errors
katleman
parents: 11672
diff changeset
    21
 * questions.
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    22
 */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    23
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    24
/*
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    25
 * @test
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    26
 * @bug 4504839 4215269 6322074
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    27
 * @summary Basic tests for unsigned operations.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    28
 * @author Joseph D. Darcy
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    29
 */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    30
public class Unsigned {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    31
    public static void main(String... args) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    32
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    33
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    34
        errors += testRoundtrip();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    35
        errors += testByteToUnsignedInt();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    36
        errors += testShortToUnsignedInt();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    37
        errors += testUnsignedCompare();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    38
        errors += testToUnsignedLong();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    39
        errors += testToStringUnsigned();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    40
        errors += testParseUnsignedInt();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    41
        errors += testDivideAndRemainder();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    42
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    43
        if (errors > 0) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    44
            throw new RuntimeException(errors + " errors found in unsigned operations.");
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    45
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    46
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    47
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    48
    private static int testRoundtrip() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    49
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    50
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    51
        int[] data = {-1, 0, 1};
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    52
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    53
        for(int datum : data) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    54
            if (Integer.parseUnsignedInt(Integer.toBinaryString(datum), 2) != datum) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    55
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    56
                System.err.println("Bad binary roundtrip conversion of " + datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    57
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    58
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    59
            if (Integer.parseUnsignedInt(Integer.toOctalString(datum), 8) != datum) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    60
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    61
                System.err.println("Bad octal roundtrip conversion of " + datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    62
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    63
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    64
            if (Integer.parseUnsignedInt(Integer.toHexString(datum), 16) != datum) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    65
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    66
                System.err.println("Bad hex roundtrip conversion of " + datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    67
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    68
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    69
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    70
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    71
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    72
    private static int testByteToUnsignedInt() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    73
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    74
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    75
        for(int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    76
            byte datum = (byte) i;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    77
            int ui = Byte.toUnsignedInt(datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    78
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    79
            if ( (ui & (~0xff)) != 0 ||
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    80
                 ((byte)ui != datum )) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    81
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    82
                System.err.printf("Bad conversion of byte %d to unsigned int %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    83
                                  datum, ui);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    84
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    85
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    86
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    87
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    88
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    89
    private static int testShortToUnsignedInt() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    90
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    91
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    92
        for(int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; i++) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    93
            short datum = (short) i;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    94
            int ui = Short.toUnsignedInt(datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    95
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    96
            if ( (ui & (~0xffff)) != 0 ||
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    97
                 ((short)ui != datum )) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    98
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
    99
                System.err.printf("Bad conversion of short %d to unsigned int %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   100
                                  datum, ui);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   101
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   102
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   103
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   104
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   105
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   106
    private static int testUnsignedCompare() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   107
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   108
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   109
        int[] data = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   110
            0,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   111
            1,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   112
            2,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   113
            3,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   114
            0x8000_0000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   115
            0x8000_0001,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   116
            0x8000_0002,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   117
            0x8000_0003,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   118
            0xFFFF_FFFE,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   119
            0xFFFF_FFFF,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   120
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   121
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   122
        for(int i : data) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   123
            for(int j : data) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   124
                int libraryResult    = Integer.compareUnsigned(i, j);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   125
                int libraryResultRev = Integer.compareUnsigned(j, i);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   126
                int localResult      = compUnsigned(i, j);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   127
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   128
                if (i == j) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   129
                    if (libraryResult != 0) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   130
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   131
                        System.err.printf("Value 0x%x did not compare as " +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   132
                                          "an unsigned value equal to itself; got %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   133
                                          i, libraryResult);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   134
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   135
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   136
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   137
                if (Integer.signum(libraryResult) != Integer.signum(localResult)) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   138
                    errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   139
                    System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   140
                                      "\texpected sign of %d, got %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   141
                                      i, j, localResult, libraryResult);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   142
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   143
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   144
                if (Integer.signum(libraryResult) !=
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   145
                    -Integer.signum(libraryResultRev)) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   146
                    errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   147
                    System.err.printf("signum(compareUnsigned(x, y)) != -signum(compareUnsigned(y,x))" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   148
                                      " for \t0x%x and 0x%x, computed %d and %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   149
                                      i, j, libraryResult, libraryResultRev);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   150
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   151
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   152
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   153
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   154
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   155
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   156
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   157
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   158
     * Straightforward compare unsigned algorithm.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   159
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   160
    private static int compUnsigned(int x, int y) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   161
        int sign_x = x & Integer.MIN_VALUE;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   162
        int sign_y = y & Integer.MIN_VALUE;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   163
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   164
        int mant_x  = x & (~Integer.MIN_VALUE);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   165
        int mant_y  = y & (~Integer.MIN_VALUE);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   166
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   167
        if (sign_x == sign_y)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   168
            return Integer.compare(mant_x, mant_y);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   169
        else {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   170
            if (sign_x == 0)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   171
                return -1; // sign x is 0, sign y is 1 => (x < y)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   172
            else
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   173
                return 1; //  sign x is 1, sign y is 0 => (x > y)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   174
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   175
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   176
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   177
    private static int testToUnsignedLong() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   178
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   179
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   180
        int[] data = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   181
            0,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   182
            1,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   183
            2,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   184
            3,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   185
            0x1234_5678,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   186
            0x8000_0000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   187
            0x8000_0001,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   188
            0x8000_0002,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   189
            0x8000_0003,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   190
            0x8765_4321,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   191
            0xFFFF_FFFE,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   192
            0xFFFF_FFFF,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   193
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   194
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   195
        for(int datum : data) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   196
            long result = Integer.toUnsignedLong(datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   197
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   198
            // High-order bits should be zero
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   199
            if ((result & 0xffff_ffff_0000_0000L) != 0L) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   200
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   201
                System.err.printf("High bits set converting 0x%x to 0x%x%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   202
                                  datum, result);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   203
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   204
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   205
            // Lower-order bits should be equal to datum.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   206
            int lowOrder = (int)(result & 0x0000_0000_ffff_ffffL);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   207
            if (lowOrder != datum ) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   208
                errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   209
                System.err.printf("Low bits not preserved converting 0x%x to 0x%x%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   210
                                  datum, result);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   211
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   212
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   213
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   214
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   215
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   216
    private static int testToStringUnsigned() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   217
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   218
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   219
        int[] data = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   220
            0,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   221
            1,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   222
            2,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   223
            3,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   224
            99999,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   225
            100000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   226
            999999,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   227
            100000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   228
            999999999,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   229
            1000000000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   230
            0x1234_5678,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   231
            0x8000_0000,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   232
            0x8000_0001,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   233
            0x8000_0002,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   234
            0x8000_0003,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   235
            0x8765_4321,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   236
            0xFFFF_FFFE,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   237
            0xFFFF_FFFF,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   238
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   239
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   240
        for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   241
            for(int datum : data) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   242
                String result1 = Integer.toUnsignedString(datum, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   243
                String result2 = Long.toString(Integer.toUnsignedLong(datum), radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   244
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   245
                if (!result1.equals(result2)) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   246
                    errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   247
                    System.err.printf("Unexpected string difference converting 0x%x:" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   248
                                      "\t%s %s%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   249
                                      datum, result1, result2);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   250
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   251
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   252
                if (radix == 10) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   253
                    String result3 = Integer.toUnsignedString(datum);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   254
                    if (!result2.equals(result3)) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   255
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   256
                        System.err.printf("Unexpected string difference converting 0x%x:" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   257
                                          "\t%s %s%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   258
                                          datum, result3, result2);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   259
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   260
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   261
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   262
                int parseResult = Integer.parseUnsignedInt(result1, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   263
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   264
                if (parseResult != datum) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   265
                    errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   266
                        System.err.printf("Bad roundtrip conversion of %d in base %d" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   267
                                          "\tconverting back ''%s'' resulted in %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   268
                                          datum, radix, result1,  parseResult);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   269
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   270
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   271
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   272
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   273
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   274
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   275
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   276
    private static final long MAX_UNSIGNED_INT = Integer.toUnsignedLong(0xffff_ffff);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   277
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   278
    private static int testParseUnsignedInt() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   279
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   280
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   281
        // Values include those between signed Integer.MAX_VALUE and
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   282
        // unsignted int MAX_VALUE.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   283
        long[] inRange = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   284
            0L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   285
            1L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   286
            10L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   287
            2147483646L,   // MAX_VALUE - 1
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   288
            2147483647L,   // MAX_VALUE
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   289
            2147483648L,   // MAX_VALUE + 1
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   290
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   291
            MAX_UNSIGNED_INT - 1L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   292
            MAX_UNSIGNED_INT,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   293
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   294
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   295
        for(long value : inRange) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   296
            for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   297
                String longString = Long.toString(value, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   298
                int intResult = Integer.parseUnsignedInt(longString, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   299
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   300
                if (Integer.toUnsignedLong(intResult) != value) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   301
                    errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   302
                    System.err.printf("Bad roundtrip conversion of %d in base %d" +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   303
                                      "\tconverting back ''%s'' resulted in %d%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   304
                                      value, radix, longString,  intResult);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   305
                }
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   306
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   307
                // test offset based parse method
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   308
                intResult = Integer.parseUnsignedInt("prefix" + longString + "suffix",
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   309
                        "prefix".length(), "prefix".length() + longString.length(), radix);
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   310
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   311
                if (Integer.toUnsignedLong(intResult) != value) {
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   312
                    errors++;
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   313
                    System.err.printf("Bad roundtrip conversion of %d in base %d" +
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   314
                            "\tconverting back ''%s'' resulted in %d%n",
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   315
                            value, radix, longString,  intResult);
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 14856
diff changeset
   316
                }
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   317
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   318
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   319
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   320
        String[] outOfRange = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   321
            null,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   322
            "",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   323
            "-1",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   324
            Long.toString(MAX_UNSIGNED_INT + 1L),
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   325
            Long.toString(Long.MAX_VALUE)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   326
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   327
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   328
        for(String s : outOfRange) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   329
            try {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   330
                int result = Integer.parseUnsignedInt(s);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   331
                errors++; // Should not reach here
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   332
                System.err.printf("Unexpected got %d from an unsigned conversion of %s",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   333
                                  result, s);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   334
            } catch(NumberFormatException nfe) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   335
                ; // Correct result
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   336
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   337
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   338
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   339
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   340
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   341
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   342
    private static int testDivideAndRemainder() {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   343
        int errors = 0;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   344
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   345
        long[] inRange = {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   346
            0L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   347
            1L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   348
            2L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   349
            2147483646L,   // MAX_VALUE - 1
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   350
            2147483647L,   // MAX_VALUE
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   351
            2147483648L,   // MAX_VALUE + 1
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   352
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   353
            MAX_UNSIGNED_INT - 1L,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   354
            MAX_UNSIGNED_INT,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   355
        };
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   356
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   357
        for(long dividend : inRange) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   358
            for(long divisor : inRange) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   359
                int quotient;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   360
                long longQuotient;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   361
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   362
                int remainder;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   363
                long longRemainder;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   364
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   365
                if (divisor == 0) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   366
                    try {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   367
                        quotient = Integer.divideUnsigned((int) dividend, (int) divisor);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   368
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   369
                    } catch(ArithmeticException ea) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   370
                        ; // Expected
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   371
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   372
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   373
                    try {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   374
                        remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   375
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   376
                    } catch(ArithmeticException ea) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   377
                        ; // Expected
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   378
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   379
                } else {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   380
                    quotient = Integer.divideUnsigned((int) dividend, (int) divisor);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   381
                    longQuotient = dividend / divisor;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   382
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   383
                    if (quotient != (int)longQuotient) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   384
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   385
                        System.err.printf("Unexpected unsigned divide result %s on %s/%s%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   386
                                          Integer.toUnsignedString(quotient),
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   387
                                          Integer.toUnsignedString((int) dividend),
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   388
                                          Integer.toUnsignedString((int) divisor));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   389
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   390
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   391
                    remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   392
                    longRemainder = dividend % divisor;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   393
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   394
                    if (remainder != (int)longRemainder) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   395
                        errors++;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   396
                        System.err.printf("Unexpected unsigned remainder result %s on %s%%%s%n",
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   397
                                          Integer.toUnsignedString(remainder),
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   398
                                          Integer.toUnsignedString((int) dividend),
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   399
                                          Integer.toUnsignedString((int) divisor));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   400
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   401
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   402
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   403
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   404
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   405
        return errors;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   406
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
diff changeset
   407
}