/* * Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 4504839 4215269 6322074 8030814 * @summary Basic tests for unsigned operations * @author Joseph D. Darcy
*/
for(long i : data) { for(long j : data) { long libraryResult = Long.compareUnsigned(i, j); long libraryResultRev = Long.compareUnsigned(j, i); long localResult = compUnsigned(i, j);
if (i == j) { if (libraryResult != 0) {
errors++;
System.err.printf("Value 0x%x did not compare as " + "an unsigned equal to itself; got %d%n",
i, libraryResult);
}
}
if (Long.signum(libraryResult) != Long.signum(localResult)) {
errors++;
System.err.printf("Unsigned compare of 0x%x to 0x%x%n:" + "\texpected sign of %d, got %d%n",
i, j, localResult, libraryResult);
}
if (Long.signum(libraryResult) !=
-Long.signum(libraryResultRev)) {
errors++;
System.err.printf("signum(compareUnsigned(x, y)) != -signum(compareUnsigned(y,x))" + " for \t0x%x and 0x%x, computed %d and %d%n",
i, j, libraryResult, libraryResultRev);
}
}
}
long parseResult = Long.parseUnsignedLong(result1, radix);
if (parseResult != datum) {
errors++;
System.err.printf("Bad roundtrip conversion of %d in base %d" + "\tconverting back ''%s'' resulted in %d%n",
datum, radix, result1, parseResult);
}
}
}
return errors;
}
privatestaticint testParseUnsignedLong() { int errors = 0; long maxUnsignedInt = Integer.toUnsignedLong(0xffff_ffff);
// Values include those between signed Long.MAX_VALUE and // unsignted Long MAX_VALUE.
BigInteger[] inRange = {
BigInteger.valueOf(0L),
BigInteger.valueOf(1L),
BigInteger.valueOf(10L),
BigInteger.valueOf(2147483646L), // Integer.MAX_VALUE - 1
BigInteger.valueOf(2147483647L), // Integer.MAX_VALUE
BigInteger.valueOf(2147483648L), // Integer.MAX_VALUE + 1
if (!toUnsignedBigInt(longResult).equals(value)) {
errors++;
System.err.printf("Bad roundtrip conversion of %d in base %d" + "\tconverting back ''%s'' resulted in %d%n",
value, radix, bigString, longResult);
}
// test offset based parse method
longResult = Long.parseUnsignedLong("prefix" + bigString + "suffix", "prefix".length(), "prefix".length() + bigString.length(), radix);
if (!toUnsignedBigInt(longResult).equals(value)) {
errors++;
System.err.printf("Bad roundtrip conversion of %d in base %d" + "\tconverting back ''%s'' resulted in %d%n",
value, radix, bigString, longResult);
}
}
}
for(String s : outOfRange) { try { long result = Long.parseUnsignedLong(s);
errors++; // Should not reach here
System.err.printf("Unexpected got %d from an unsigned conversion of %s",
result, s);
} catch(NumberFormatException nfe) {
; // Correct result
}
}
// test case known at one time to fail
errors += testUnsignedOverflow("1234567890abcdef1", 16, true);
// largest value with guard = 91 = 13*7; radix = 13
errors += testUnsignedOverflow("196a78a44c3bba320c", 13, false);
// smallest value with guard = 92 = 23*2*2; radix = 23
errors += testUnsignedOverflow("137060c6g1c1dg0", 23, false);
// guard in [92,98]: no overflow
// one less than smallest guard value to overflow: guard = 99 = 11*3*3, radix = 33
errors += testUnsignedOverflow("b1w8p7j5q9r6f", 33, false);
// smallest guard value to overflow: guard = 99 = 11*3*3, radix = 33
errors += testUnsignedOverflow("b1w8p7j5q9r6g", 33, true);
// test overflow of overflow
BigInteger maxUnsignedLong =
BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE); for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
BigInteger quotient = maxUnsignedLong.divide(BigInteger.valueOf(radix)); for (int addend = 2; addend <= radix; addend++) {
BigInteger b = quotient.multiply(BigInteger.valueOf(radix + addend));
errors += testUnsignedOverflow(b.toString(radix), radix, b.compareTo(maxUnsignedLong) > 0);
}
}
return errors;
}
// test for missing or unexpected unsigned overflow exception privatestaticint testUnsignedOverflow(String s, int radix, boolean exception) { int errors = 0; long result; try {
result = Long.parseUnsignedLong(s, radix); if (exception) {
System.err.printf("Unexpected result %d for Long.parseUnsignedLong(%s,%d)\n",
result, s, radix);
errors++;
}
} catch (NumberFormatException nfe) { if (!exception) {
System.err.printf("Unexpected exception %s for Long.parseUnsignedLong(%s,%d)\n",
nfe.toString(), s, radix);
errors++;
}
} return errors;
}
if (remainder != longRemainder.longValue()) {
errors++;
System.err.printf("Unexpected unsigned remainder result %s on %s%%%s%n", Long.toUnsignedString(remainder), Long.toUnsignedString(dividend.longValue()), Long.toUnsignedString(divisor.longValue()));
}
}
}
}
return errors;
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.16Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.