Path: blob/master/test/jdk/java/lang/Integer/Unsigned.java
41149 views
/*1* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4504839 4215269 632207426* @summary Basic tests for unsigned operations.27* @author Joseph D. Darcy28*/29public class Unsigned {30public static void main(String... args) {31int errors = 0;3233errors += testRoundtrip();34errors += testByteToUnsignedInt();35errors += testShortToUnsignedInt();36errors += testUnsignedCompare();37errors += testToUnsignedLong();38errors += testToStringUnsigned();39errors += testParseUnsignedInt();40errors += testDivideAndRemainder();4142if (errors > 0) {43throw new RuntimeException(errors + " errors found in unsigned operations.");44}45}4647private static int testRoundtrip() {48int errors = 0;4950int[] data = {-1, 0, 1};5152for(int datum : data) {53if (Integer.parseUnsignedInt(Integer.toBinaryString(datum), 2) != datum) {54errors++;55System.err.println("Bad binary roundtrip conversion of " + datum);56}5758if (Integer.parseUnsignedInt(Integer.toOctalString(datum), 8) != datum) {59errors++;60System.err.println("Bad octal roundtrip conversion of " + datum);61}6263if (Integer.parseUnsignedInt(Integer.toHexString(datum), 16) != datum) {64errors++;65System.err.println("Bad hex roundtrip conversion of " + datum);66}67}68return errors;69}7071private static int testByteToUnsignedInt() {72int errors = 0;7374for(int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {75byte datum = (byte) i;76int ui = Byte.toUnsignedInt(datum);7778if ( (ui & (~0xff)) != 0 ||79((byte)ui != datum )) {80errors++;81System.err.printf("Bad conversion of byte %d to unsigned int %d%n",82datum, ui);83}84}85return errors;86}8788private static int testShortToUnsignedInt() {89int errors = 0;9091for(int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; i++) {92short datum = (short) i;93int ui = Short.toUnsignedInt(datum);9495if ( (ui & (~0xffff)) != 0 ||96((short)ui != datum )) {97errors++;98System.err.printf("Bad conversion of short %d to unsigned int %d%n",99datum, ui);100}101}102return errors;103}104105private static int testUnsignedCompare() {106int errors = 0;107108int[] data = {1090,1101,1112,1123,1130x8000_0000,1140x8000_0001,1150x8000_0002,1160x8000_0003,1170xFFFF_FFFE,1180xFFFF_FFFF,119};120121for(int i : data) {122for(int j : data) {123int libraryResult = Integer.compareUnsigned(i, j);124int libraryResultRev = Integer.compareUnsigned(j, i);125int localResult = compUnsigned(i, j);126127if (i == j) {128if (libraryResult != 0) {129errors++;130System.err.printf("Value 0x%x did not compare as " +131"an unsigned value equal to itself; got %d%n",132i, libraryResult);133}134}135136if (Integer.signum(libraryResult) != Integer.signum(localResult)) {137errors++;138System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" +139"\texpected sign of %d, got %d%n",140i, j, localResult, libraryResult);141}142143if (Integer.signum(libraryResult) !=144-Integer.signum(libraryResultRev)) {145errors++;146System.err.printf("signum(compareUnsigned(x, y)) != -signum(compareUnsigned(y,x))" +147" for \t0x%x and 0x%x, computed %d and %d%n",148i, j, libraryResult, libraryResultRev);149}150}151}152153return errors;154}155156/**157* Straightforward compare unsigned algorithm.158*/159private static int compUnsigned(int x, int y) {160int sign_x = x & Integer.MIN_VALUE;161int sign_y = y & Integer.MIN_VALUE;162163int mant_x = x & (~Integer.MIN_VALUE);164int mant_y = y & (~Integer.MIN_VALUE);165166if (sign_x == sign_y)167return Integer.compare(mant_x, mant_y);168else {169if (sign_x == 0)170return -1; // sign x is 0, sign y is 1 => (x < y)171else172return 1; // sign x is 1, sign y is 0 => (x > y)173}174}175176private static int testToUnsignedLong() {177int errors = 0;178179int[] data = {1800,1811,1822,1833,1840x1234_5678,1850x8000_0000,1860x8000_0001,1870x8000_0002,1880x8000_0003,1890x8765_4321,1900xFFFF_FFFE,1910xFFFF_FFFF,192};193194for(int datum : data) {195long result = Integer.toUnsignedLong(datum);196197// High-order bits should be zero198if ((result & 0xffff_ffff_0000_0000L) != 0L) {199errors++;200System.err.printf("High bits set converting 0x%x to 0x%x%n",201datum, result);202}203204// Lower-order bits should be equal to datum.205int lowOrder = (int)(result & 0x0000_0000_ffff_ffffL);206if (lowOrder != datum ) {207errors++;208System.err.printf("Low bits not preserved converting 0x%x to 0x%x%n",209datum, result);210}211}212return errors;213}214215private static int testToStringUnsigned() {216int errors = 0;217218int[] data = {2190,2201,2212,2223,22399999,224100000,225999999,226100000,227999999999,2281000000000,2290x1234_5678,2300x8000_0000,2310x8000_0001,2320x8000_0002,2330x8000_0003,2340x8765_4321,2350xFFFF_FFFE,2360xFFFF_FFFF,237};238239for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {240for(int datum : data) {241String result1 = Integer.toUnsignedString(datum, radix);242String result2 = Long.toString(Integer.toUnsignedLong(datum), radix);243244if (!result1.equals(result2)) {245errors++;246System.err.printf("Unexpected string difference converting 0x%x:" +247"\t%s %s%n",248datum, result1, result2);249}250251if (radix == 10) {252String result3 = Integer.toUnsignedString(datum);253if (!result2.equals(result3)) {254errors++;255System.err.printf("Unexpected string difference converting 0x%x:" +256"\t%s %s%n",257datum, result3, result2);258}259}260261int parseResult = Integer.parseUnsignedInt(result1, radix);262263if (parseResult != datum) {264errors++;265System.err.printf("Bad roundtrip conversion of %d in base %d" +266"\tconverting back ''%s'' resulted in %d%n",267datum, radix, result1, parseResult);268}269}270}271272return errors;273}274275private static final long MAX_UNSIGNED_INT = Integer.toUnsignedLong(0xffff_ffff);276277private static int testParseUnsignedInt() {278int errors = 0;279280// Values include those between signed Integer.MAX_VALUE and281// unsignted int MAX_VALUE.282long[] inRange = {2830L,2841L,28510L,2862147483646L, // MAX_VALUE - 12872147483647L, // MAX_VALUE2882147483648L, // MAX_VALUE + 1289290MAX_UNSIGNED_INT - 1L,291MAX_UNSIGNED_INT,292};293294for(long value : inRange) {295for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {296String longString = Long.toString(value, radix);297int intResult = Integer.parseUnsignedInt(longString, radix);298299if (Integer.toUnsignedLong(intResult) != value) {300errors++;301System.err.printf("Bad roundtrip conversion of %d in base %d" +302"\tconverting back ''%s'' resulted in %d%n",303value, radix, longString, intResult);304}305306// test offset based parse method307intResult = Integer.parseUnsignedInt("prefix" + longString + "suffix",308"prefix".length(), "prefix".length() + longString.length(), radix);309310if (Integer.toUnsignedLong(intResult) != value) {311errors++;312System.err.printf("Bad roundtrip conversion of %d in base %d" +313"\tconverting back ''%s'' resulted in %d%n",314value, radix, longString, intResult);315}316}317}318319String[] outOfRange = {320null,321"",322"-1",323Long.toString(MAX_UNSIGNED_INT + 1L),324Long.toString(Long.MAX_VALUE)325};326327for(String s : outOfRange) {328try {329int result = Integer.parseUnsignedInt(s);330errors++; // Should not reach here331System.err.printf("Unexpected got %d from an unsigned conversion of %s",332result, s);333} catch(NumberFormatException nfe) {334; // Correct result335}336}337338return errors;339}340341private static int testDivideAndRemainder() {342int errors = 0;343344long[] inRange = {3450L,3461L,3472L,3482147483646L, // MAX_VALUE - 13492147483647L, // MAX_VALUE3502147483648L, // MAX_VALUE + 1351352MAX_UNSIGNED_INT - 1L,353MAX_UNSIGNED_INT,354};355356for(long dividend : inRange) {357for(long divisor : inRange) {358int quotient;359long longQuotient;360361int remainder;362long longRemainder;363364if (divisor == 0) {365try {366quotient = Integer.divideUnsigned((int) dividend, (int) divisor);367errors++;368} catch(ArithmeticException ea) {369; // Expected370}371372try {373remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);374errors++;375} catch(ArithmeticException ea) {376; // Expected377}378} else {379quotient = Integer.divideUnsigned((int) dividend, (int) divisor);380longQuotient = dividend / divisor;381382if (quotient != (int)longQuotient) {383errors++;384System.err.printf("Unexpected unsigned divide result %s on %s/%s%n",385Integer.toUnsignedString(quotient),386Integer.toUnsignedString((int) dividend),387Integer.toUnsignedString((int) divisor));388}389390remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);391longRemainder = dividend % divisor;392393if (remainder != (int)longRemainder) {394errors++;395System.err.printf("Unexpected unsigned remainder result %s on %s%%%s%n",396Integer.toUnsignedString(remainder),397Integer.toUnsignedString((int) dividend),398Integer.toUnsignedString((int) divisor));399}400}401}402}403404return errors;405}406}407408409