undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
168
test/jdk/java/lang/Integer/BitTwiddle.java
Normal file
168
test/jdk/java/lang/Integer/BitTwiddle.java
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2018, 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
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.RandomFactory
|
||||
* @run main BitTwiddle
|
||||
* @bug 4495754 8078672
|
||||
* @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed)
|
||||
* @author Josh Bloch
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.util.Random;
|
||||
import jdk.test.lib.RandomFactory;
|
||||
import static java.lang.Integer.*;
|
||||
|
||||
public class BitTwiddle {
|
||||
private static final int N = 1000; // # of repetitions per test
|
||||
|
||||
public static void main(String args[]) {
|
||||
Random rnd = RandomFactory.getRandom();
|
||||
|
||||
if (highestOneBit(0) != 0)
|
||||
throw new RuntimeException("a");
|
||||
if (highestOneBit(-1) != MIN_VALUE)
|
||||
throw new RuntimeException("b");
|
||||
if (highestOneBit(1) != 1)
|
||||
throw new RuntimeException("c");
|
||||
|
||||
if (lowestOneBit(0) != 0)
|
||||
throw new RuntimeException("d");
|
||||
if (lowestOneBit(-1) != 1)
|
||||
throw new RuntimeException("e");
|
||||
if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
|
||||
throw new RuntimeException("f");
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
|
||||
String expected = new StringBuilder()
|
||||
.append(leftpad(toBinaryString(x), 32))
|
||||
.reverse().toString();
|
||||
|
||||
String actual = leftpad(toBinaryString(reverse(x)), 32);
|
||||
|
||||
if (!expected.equals(actual)) {
|
||||
throw new RuntimeException("reverse: \n" +
|
||||
expected + " \n" + actual);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
|
||||
throw new RuntimeException("g: " + toHexString(x));
|
||||
}
|
||||
|
||||
if (numberOfLeadingZeros(0) != SIZE)
|
||||
throw new RuntimeException("h");
|
||||
if (numberOfLeadingZeros(-1) != 0)
|
||||
throw new RuntimeException("i");
|
||||
if (numberOfLeadingZeros(1) != (SIZE - 1))
|
||||
throw new RuntimeException("j");
|
||||
if (numberOfLeadingZeros(Integer.MAX_VALUE) != 1)
|
||||
throw new RuntimeException("lzmax");
|
||||
|
||||
if (numberOfTrailingZeros(0) != SIZE)
|
||||
throw new RuntimeException("k");
|
||||
if (numberOfTrailingZeros(1) != 0)
|
||||
throw new RuntimeException("l");
|
||||
if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
|
||||
throw new RuntimeException("m");
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
|
||||
throw new RuntimeException("n: " + toHexString(x));
|
||||
}
|
||||
for (int i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {
|
||||
if (numberOfLeadingZeros(i) != r ||
|
||||
numberOfTrailingZeros(i) != (SIZE - r - 1) ||
|
||||
numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {
|
||||
throw new RuntimeException("lzx: " + toHexString(i));
|
||||
}
|
||||
}
|
||||
|
||||
if (bitCount(0) != 0)
|
||||
throw new RuntimeException("o");
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
int pow2 = 1 << i;
|
||||
if (bitCount(pow2) != 1)
|
||||
throw new RuntimeException("p: " + i);
|
||||
if (bitCount(pow2 -1) != i)
|
||||
throw new RuntimeException("q: " + i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
if (bitCount(x) != bitCount(reverse(x)))
|
||||
throw new RuntimeException("r: " + toHexString(x));
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
int dist = rnd.nextInt();
|
||||
if (bitCount(x) != bitCount(rotateRight(x, dist)))
|
||||
throw new RuntimeException("s: " + toHexString(x) +
|
||||
toHexString(dist));
|
||||
if (bitCount(x) != bitCount(rotateLeft(x, dist)))
|
||||
throw new RuntimeException("t: " + toHexString(x) +
|
||||
toHexString(dist));
|
||||
if (rotateRight(x, dist) != rotateLeft(x, -dist))
|
||||
throw new RuntimeException("u: " + toHexString(x) +
|
||||
toHexString(dist));
|
||||
if (rotateRight(x, -dist) != rotateLeft(x, dist))
|
||||
throw new RuntimeException("v: " + toHexString(x) +
|
||||
toHexString(dist));
|
||||
}
|
||||
|
||||
if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
|
||||
|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
|
||||
throw new RuntimeException("w");
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
|
||||
if (signum(x) != sign)
|
||||
throw new RuntimeException("x: " + toHexString(x));
|
||||
}
|
||||
|
||||
if(reverseBytes(0xaabbccdd) != 0xddccbbaa)
|
||||
throw new RuntimeException("y");
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int x = rnd.nextInt();
|
||||
if (bitCount(x) != bitCount(reverseBytes(x)))
|
||||
throw new RuntimeException("z: " + toHexString(x));
|
||||
}
|
||||
}
|
||||
|
||||
private static final String ZEROS = "0".repeat(32);
|
||||
private static String leftpad(String s, int width) {
|
||||
return ZEROS.substring(0, width - s.length()) + s;
|
||||
}
|
||||
}
|
||||
100
test/jdk/java/lang/Integer/Decode.java
Normal file
100
test/jdk/java/lang/Integer/Decode.java
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2025, 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 4136371 5017980 6576055
|
||||
* @summary Test Integer.decode method
|
||||
*/
|
||||
|
||||
/**
|
||||
* There are six methods in java.lang.Integer which transform strings
|
||||
* into an int or Integer value:
|
||||
*
|
||||
* public Integer(String s)
|
||||
* public static Integer decode(String nm)
|
||||
* public static int parseInteger(String s, int radix)
|
||||
* public static int parseInteger(String s)
|
||||
* public static Integer valueOf(String s, int radix)
|
||||
* public static Integer valueOf(String s)
|
||||
*
|
||||
* The other five methods are tested elsewhere.
|
||||
*/
|
||||
public class Decode {
|
||||
|
||||
private static void check(String val, int expected) {
|
||||
int n = (Integer.decode(val)).intValue();
|
||||
if (n != expected)
|
||||
throw new RuntimeException("Integer.decode failed. String:" +
|
||||
val + " Result:" + n);
|
||||
}
|
||||
|
||||
private static void checkFailure(String val, String message) {
|
||||
try {
|
||||
int n = (Integer.decode(val)).intValue();
|
||||
throw new RuntimeException(message);
|
||||
} catch (NumberFormatException e) { /* Okay */}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
check(new String(""+Integer.MIN_VALUE), Integer.MIN_VALUE);
|
||||
check(new String(""+Integer.MAX_VALUE), Integer.MAX_VALUE);
|
||||
|
||||
check("10", 10);
|
||||
check("0x10", 16);
|
||||
check("0X10", 16);
|
||||
check("010", 8);
|
||||
check("#10", 16);
|
||||
|
||||
check("+10", 10);
|
||||
check("+0x10", 16);
|
||||
check("+0X10", 16);
|
||||
check("+010", 8);
|
||||
check("+#10", 16);
|
||||
|
||||
check("-10", -10);
|
||||
check("-0x10", -16);
|
||||
check("-0X10", -16);
|
||||
check("-010", -8);
|
||||
check("-#10", -16);
|
||||
|
||||
check(Long.toString(Integer.MIN_VALUE), Integer.MIN_VALUE);
|
||||
check(Long.toString(Integer.MAX_VALUE), Integer.MAX_VALUE);
|
||||
|
||||
checkFailure("0x-10", "Integer.decode allows negative sign in wrong position.");
|
||||
checkFailure("0x+10", "Integer.decode allows positive sign in wrong position.");
|
||||
|
||||
checkFailure("+", "Raw plus sign allowed.");
|
||||
checkFailure("-", "Raw minus sign allowed.");
|
||||
|
||||
checkFailure(Long.toString((long)Integer.MIN_VALUE - 1L), "Out of range");
|
||||
checkFailure(Long.toString((long)Integer.MAX_VALUE + 1L), "Out of range");
|
||||
|
||||
checkFailure("", "Empty String");
|
||||
|
||||
try {
|
||||
Integer.decode(null);
|
||||
throw new RuntimeException("Integer.decode(null) expected to throw NPE");
|
||||
} catch (NullPointerException npe) {/* Okay */}
|
||||
}
|
||||
}
|
||||
35
test/jdk/java/lang/Integer/GetInteger.java
Normal file
35
test/jdk/java/lang/Integer/GetInteger.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 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 4252315
|
||||
* @summary test Integer.getInteger method with empty key
|
||||
*/
|
||||
|
||||
public class GetInteger {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Integer.getInteger("", 1);
|
||||
Integer.getInteger(null, 1);
|
||||
}
|
||||
}
|
||||
172
test/jdk/java/lang/Integer/ParsingTest.java
Normal file
172
test/jdk/java/lang/Integer/ParsingTest.java
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2025, 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 5017980 6576055 8041972 8055251
|
||||
* @summary Test parsing methods
|
||||
*/
|
||||
|
||||
import java.lang.IndexOutOfBoundsException;
|
||||
import java.lang.NullPointerException;
|
||||
import java.lang.RuntimeException;
|
||||
|
||||
/**
|
||||
* There are seven methods in java.lang.Integer which transform strings
|
||||
* into an int or Integer value:
|
||||
*
|
||||
* public Integer(String s)
|
||||
* public static Integer decode(String nm)
|
||||
* public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
|
||||
* public static int parseInt(String s, int radix)
|
||||
* public static int parseInt(String s)
|
||||
* public static Integer valueOf(String s, int radix)
|
||||
* public static Integer valueOf(String s)
|
||||
*
|
||||
* Besides decode, all the methods and constructor call down into
|
||||
* parseInt(CharSequence, int, int, int) to do the actual work. Therefore, the
|
||||
* behavior of parseInt(CharSequence, int, int, int) will be tested here.
|
||||
*
|
||||
*/
|
||||
|
||||
public class ParsingTest {
|
||||
|
||||
public static void main(String... argv) {
|
||||
check(+100, "+100");
|
||||
check(-100, "-100");
|
||||
|
||||
check(0, "+0");
|
||||
check(0, "-0");
|
||||
check(0, "+00000");
|
||||
check(0, "-00000");
|
||||
|
||||
check(0, "0");
|
||||
check(1, "1");
|
||||
check(9, "9");
|
||||
|
||||
checkFailure("");
|
||||
checkFailure("\u0000");
|
||||
checkFailure("\u002f");
|
||||
checkFailure("+");
|
||||
checkFailure("-");
|
||||
checkFailure("++");
|
||||
checkFailure("+-");
|
||||
checkFailure("-+");
|
||||
checkFailure("--");
|
||||
checkFailure("++100");
|
||||
checkFailure("--100");
|
||||
checkFailure("+-6");
|
||||
checkFailure("-+6");
|
||||
checkFailure("*100");
|
||||
|
||||
// check offset based methods
|
||||
check(0, "+00000", 0, 6, 10);
|
||||
check(0, "-00000", 0, 6, 10);
|
||||
check(0, "test-00000", 4, 10, 10);
|
||||
check(-12345, "test-12345", 4, 10, 10);
|
||||
check(12345, "xx12345yy", 2, 7, 10);
|
||||
check(15, "xxFyy", 2, 3, 16);
|
||||
|
||||
checkNumberFormatException("", 0, 0, 10);
|
||||
checkNumberFormatException("+-6", 0, 3, 10);
|
||||
checkNumberFormatException("1000000", 7, 7, 10);
|
||||
checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);
|
||||
checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);
|
||||
|
||||
checkIndexOutOfBoundsException("1000000", 10, 4, 10);
|
||||
checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);
|
||||
checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);
|
||||
checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);
|
||||
checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);
|
||||
checkIndexOutOfBoundsException("-1", 0, 3, 10);
|
||||
checkIndexOutOfBoundsException("-1", 2, 3, 10);
|
||||
checkIndexOutOfBoundsException("-1", -1, 2, 10);
|
||||
|
||||
checkNull(0, 1, 10);
|
||||
checkNull(-1, 0, 10);
|
||||
checkNull(0, 0, 10);
|
||||
checkNull(0, -1, 10);
|
||||
checkNull(-1, -1, -1);
|
||||
}
|
||||
|
||||
private static void check(int expected, String val) {
|
||||
int n = Integer.parseInt(val);
|
||||
if (n != expected)
|
||||
throw new RuntimeException("Integer.parseInt failed. String:" +
|
||||
val + " Result:" + n);
|
||||
}
|
||||
|
||||
private static void checkFailure(String val) {
|
||||
int n = 0;
|
||||
try {
|
||||
n = Integer.parseInt(val);
|
||||
System.err.println("parseInt(" + val + ") incorrectly returned " + n);
|
||||
throw new RuntimeException();
|
||||
} catch (NumberFormatException nfe) {
|
||||
; // Expected
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkNumberFormatException(String val, int start, int end, int radix) {
|
||||
int n = 0;
|
||||
try {
|
||||
n = Integer.parseInt(val, start, end, radix);
|
||||
System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +
|
||||
") incorrectly returned " + n);
|
||||
throw new RuntimeException();
|
||||
} catch (NumberFormatException nfe) {
|
||||
; // Expected
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {
|
||||
int n = 0;
|
||||
try {
|
||||
n = Integer.parseInt(val, start, end, radix);
|
||||
System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +
|
||||
") incorrectly returned " + n);
|
||||
throw new RuntimeException();
|
||||
} catch (IndexOutOfBoundsException ioob) {
|
||||
; // Expected
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkNull(int start, int end, int radix) {
|
||||
int n = 0;
|
||||
try {
|
||||
n = Integer.parseInt(null, start, end, radix);
|
||||
System.err.println("parseInt(null, " + start + ", " + end + ", " + radix +
|
||||
") incorrectly returned " + n);
|
||||
throw new RuntimeException();
|
||||
} catch (NullPointerException npe) {
|
||||
; // Expected
|
||||
}
|
||||
}
|
||||
|
||||
private static void check(int expected, String val, int start, int end, int radix) {
|
||||
int n = Integer.parseInt(val, start, end, radix);
|
||||
if (n != expected)
|
||||
throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" +
|
||||
val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n);
|
||||
}
|
||||
}
|
||||
89
test/jdk/java/lang/Integer/ToString.java
Normal file
89
test/jdk/java/lang/Integer/ToString.java
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2023, 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 8136500 8310929
|
||||
* @summary Test Integer.toString method for both compact and non-compact strings
|
||||
* @run junit/othervm -XX:+CompactStrings ToString
|
||||
* @run junit/othervm -XX:-CompactStrings ToString
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ToString {
|
||||
|
||||
@Test
|
||||
public void testBase10() {
|
||||
test("-2147483648", Integer.MIN_VALUE);
|
||||
test("2147483647", Integer.MAX_VALUE);
|
||||
test("0", 0);
|
||||
|
||||
// Wiggle around the exponentially increasing base.
|
||||
final int LIMIT = (1 << 15);
|
||||
int base = 10000;
|
||||
while (base < Integer.MAX_VALUE / 10) {
|
||||
for (int d = -LIMIT; d < LIMIT; d++) {
|
||||
int c = base + d;
|
||||
if (c > 0) {
|
||||
buildAndTest(c);
|
||||
}
|
||||
}
|
||||
base *= 10;
|
||||
}
|
||||
|
||||
for (int c = 1; c < LIMIT; c++) {
|
||||
buildAndTest(Integer.MAX_VALUE - LIMIT + c);
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildAndTest(int c) {
|
||||
if (c <= 0) {
|
||||
throw new IllegalArgumentException("Test bug: can only handle positives, " + c);
|
||||
}
|
||||
|
||||
StringBuilder sbN = new StringBuilder();
|
||||
StringBuilder sbP = new StringBuilder();
|
||||
|
||||
int t = c;
|
||||
while (t > 0) {
|
||||
char digit = (char) ('0' + (t % 10));
|
||||
sbN.append(digit);
|
||||
sbP.append(digit);
|
||||
t = t / 10;
|
||||
}
|
||||
|
||||
sbN.append("-");
|
||||
sbN.reverse();
|
||||
sbP.reverse();
|
||||
|
||||
test(sbN.toString(), -c);
|
||||
test(sbP.toString(), c);
|
||||
}
|
||||
|
||||
private static void test(String expected, int value) {
|
||||
assertEquals(expected, Integer.toString(value));
|
||||
}
|
||||
}
|
||||
406
test/jdk/java/lang/Integer/Unsigned.java
Normal file
406
test/jdk/java/lang/Integer/Unsigned.java
Normal file
|
|
@ -0,0 +1,406 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2025, 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
|
||||
* @summary Basic tests for unsigned operations.
|
||||
*/
|
||||
public class Unsigned {
|
||||
public static void main(String... args) {
|
||||
int errors = 0;
|
||||
|
||||
errors += testRoundtrip();
|
||||
errors += testByteToUnsignedInt();
|
||||
errors += testShortToUnsignedInt();
|
||||
errors += testUnsignedCompare();
|
||||
errors += testToUnsignedLong();
|
||||
errors += testToStringUnsigned();
|
||||
errors += testParseUnsignedInt();
|
||||
errors += testDivideAndRemainder();
|
||||
|
||||
if (errors > 0) {
|
||||
throw new RuntimeException(errors + " errors found in unsigned operations.");
|
||||
}
|
||||
}
|
||||
|
||||
private static int testRoundtrip() {
|
||||
int errors = 0;
|
||||
|
||||
int[] data = {-1, 0, 1};
|
||||
|
||||
for(int datum : data) {
|
||||
if (Integer.parseUnsignedInt(Integer.toBinaryString(datum), 2) != datum) {
|
||||
errors++;
|
||||
System.err.println("Bad binary roundtrip conversion of " + datum);
|
||||
}
|
||||
|
||||
if (Integer.parseUnsignedInt(Integer.toOctalString(datum), 8) != datum) {
|
||||
errors++;
|
||||
System.err.println("Bad octal roundtrip conversion of " + datum);
|
||||
}
|
||||
|
||||
if (Integer.parseUnsignedInt(Integer.toHexString(datum), 16) != datum) {
|
||||
errors++;
|
||||
System.err.println("Bad hex roundtrip conversion of " + datum);
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
private static int testByteToUnsignedInt() {
|
||||
int errors = 0;
|
||||
|
||||
for(int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
|
||||
byte datum = (byte) i;
|
||||
int ui = Byte.toUnsignedInt(datum);
|
||||
|
||||
if ( (ui & (~0xff)) != 0 ||
|
||||
((byte)ui != datum )) {
|
||||
errors++;
|
||||
System.err.printf("Bad conversion of byte %d to unsigned int %d%n",
|
||||
datum, ui);
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
private static int testShortToUnsignedInt() {
|
||||
int errors = 0;
|
||||
|
||||
for(int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; i++) {
|
||||
short datum = (short) i;
|
||||
int ui = Short.toUnsignedInt(datum);
|
||||
|
||||
if ( (ui & (~0xffff)) != 0 ||
|
||||
((short)ui != datum )) {
|
||||
errors++;
|
||||
System.err.printf("Bad conversion of short %d to unsigned int %d%n",
|
||||
datum, ui);
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
private static int testUnsignedCompare() {
|
||||
int errors = 0;
|
||||
|
||||
int[] data = {
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
0x8000_0000,
|
||||
0x8000_0001,
|
||||
0x8000_0002,
|
||||
0x8000_0003,
|
||||
0xFFFF_FFFE,
|
||||
0xFFFF_FFFF,
|
||||
};
|
||||
|
||||
for(int i : data) {
|
||||
for(int j : data) {
|
||||
int libraryResult = Integer.compareUnsigned(i, j);
|
||||
int libraryResultRev = Integer.compareUnsigned(j, i);
|
||||
int localResult = compUnsigned(i, j);
|
||||
|
||||
if (i == j) {
|
||||
if (libraryResult != 0) {
|
||||
errors++;
|
||||
System.err.printf("Value 0x%x did not compare as " +
|
||||
"an unsigned value equal to itself; got %d%n",
|
||||
i, libraryResult);
|
||||
}
|
||||
}
|
||||
|
||||
if (Integer.signum(libraryResult) != Integer.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 (Integer.signum(libraryResult) !=
|
||||
-Integer.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Straightforward compare unsigned algorithm.
|
||||
*/
|
||||
private static int compUnsigned(int x, int y) {
|
||||
int sign_x = x & Integer.MIN_VALUE;
|
||||
int sign_y = y & Integer.MIN_VALUE;
|
||||
|
||||
int mant_x = x & (~Integer.MIN_VALUE);
|
||||
int mant_y = y & (~Integer.MIN_VALUE);
|
||||
|
||||
if (sign_x == sign_y)
|
||||
return Integer.compare(mant_x, mant_y);
|
||||
else {
|
||||
if (sign_x == 0)
|
||||
return -1; // sign x is 0, sign y is 1 => (x < y)
|
||||
else
|
||||
return 1; // sign x is 1, sign y is 0 => (x > y)
|
||||
}
|
||||
}
|
||||
|
||||
private static int testToUnsignedLong() {
|
||||
int errors = 0;
|
||||
|
||||
int[] data = {
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
0x1234_5678,
|
||||
0x8000_0000,
|
||||
0x8000_0001,
|
||||
0x8000_0002,
|
||||
0x8000_0003,
|
||||
0x8765_4321,
|
||||
0xFFFF_FFFE,
|
||||
0xFFFF_FFFF,
|
||||
};
|
||||
|
||||
for(int datum : data) {
|
||||
long result = Integer.toUnsignedLong(datum);
|
||||
|
||||
// High-order bits should be zero
|
||||
if ((result & 0xffff_ffff_0000_0000L) != 0L) {
|
||||
errors++;
|
||||
System.err.printf("High bits set converting 0x%x to 0x%x%n",
|
||||
datum, result);
|
||||
}
|
||||
|
||||
// Lower-order bits should be equal to datum.
|
||||
int lowOrder = (int)(result & 0x0000_0000_ffff_ffffL);
|
||||
if (lowOrder != datum ) {
|
||||
errors++;
|
||||
System.err.printf("Low bits not preserved converting 0x%x to 0x%x%n",
|
||||
datum, result);
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
private static int testToStringUnsigned() {
|
||||
int errors = 0;
|
||||
|
||||
int[] data = {
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
99999,
|
||||
100000,
|
||||
999999,
|
||||
100000,
|
||||
999999999,
|
||||
1000000000,
|
||||
0x1234_5678,
|
||||
0x8000_0000,
|
||||
0x8000_0001,
|
||||
0x8000_0002,
|
||||
0x8000_0003,
|
||||
0x8765_4321,
|
||||
0xFFFF_FFFE,
|
||||
0xFFFF_FFFF,
|
||||
};
|
||||
|
||||
for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
|
||||
for(int datum : data) {
|
||||
String result1 = Integer.toUnsignedString(datum, radix);
|
||||
String result2 = Long.toString(Integer.toUnsignedLong(datum), radix);
|
||||
|
||||
if (!result1.equals(result2)) {
|
||||
errors++;
|
||||
System.err.printf("Unexpected string difference converting 0x%x:" +
|
||||
"\t%s %s%n",
|
||||
datum, result1, result2);
|
||||
}
|
||||
|
||||
if (radix == 10) {
|
||||
String result3 = Integer.toUnsignedString(datum);
|
||||
if (!result2.equals(result3)) {
|
||||
errors++;
|
||||
System.err.printf("Unexpected string difference converting 0x%x:" +
|
||||
"\t%s %s%n",
|
||||
datum, result3, result2);
|
||||
}
|
||||
}
|
||||
|
||||
int parseResult = Integer.parseUnsignedInt(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;
|
||||
}
|
||||
|
||||
private static final long MAX_UNSIGNED_INT = Integer.toUnsignedLong(0xffff_ffff);
|
||||
|
||||
private static int testParseUnsignedInt() {
|
||||
int errors = 0;
|
||||
|
||||
// Values include those between signed Integer.MAX_VALUE and
|
||||
// unsignted int MAX_VALUE.
|
||||
long[] inRange = {
|
||||
0L,
|
||||
1L,
|
||||
10L,
|
||||
2147483646L, // MAX_VALUE - 1
|
||||
2147483647L, // MAX_VALUE
|
||||
2147483648L, // MAX_VALUE + 1
|
||||
|
||||
MAX_UNSIGNED_INT - 1L,
|
||||
MAX_UNSIGNED_INT,
|
||||
};
|
||||
|
||||
for(long value : inRange) {
|
||||
for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
|
||||
String longString = Long.toString(value, radix);
|
||||
int intResult = Integer.parseUnsignedInt(longString, radix);
|
||||
|
||||
if (Integer.toUnsignedLong(intResult) != value) {
|
||||
errors++;
|
||||
System.err.printf("Bad roundtrip conversion of %d in base %d" +
|
||||
"\tconverting back ''%s'' resulted in %d%n",
|
||||
value, radix, longString, intResult);
|
||||
}
|
||||
|
||||
// test offset based parse method
|
||||
intResult = Integer.parseUnsignedInt("prefix" + longString + "suffix",
|
||||
"prefix".length(), "prefix".length() + longString.length(), radix);
|
||||
|
||||
if (Integer.toUnsignedLong(intResult) != value) {
|
||||
errors++;
|
||||
System.err.printf("Bad roundtrip conversion of %d in base %d" +
|
||||
"\tconverting back ''%s'' resulted in %d%n",
|
||||
value, radix, longString, intResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String[] outOfRange = {
|
||||
null,
|
||||
"",
|
||||
"-1",
|
||||
Long.toString(MAX_UNSIGNED_INT + 1L),
|
||||
Long.toString(Long.MAX_VALUE)
|
||||
};
|
||||
|
||||
for(String s : outOfRange) {
|
||||
try {
|
||||
int result = Integer.parseUnsignedInt(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
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
private static int testDivideAndRemainder() {
|
||||
int errors = 0;
|
||||
|
||||
long[] inRange = {
|
||||
0L,
|
||||
1L,
|
||||
2L,
|
||||
2147483646L, // MAX_VALUE - 1
|
||||
2147483647L, // MAX_VALUE
|
||||
2147483648L, // MAX_VALUE + 1
|
||||
|
||||
MAX_UNSIGNED_INT - 1L,
|
||||
MAX_UNSIGNED_INT,
|
||||
};
|
||||
|
||||
for(long dividend : inRange) {
|
||||
for(long divisor : inRange) {
|
||||
int quotient;
|
||||
long longQuotient;
|
||||
|
||||
int remainder;
|
||||
long longRemainder;
|
||||
|
||||
if (divisor == 0) {
|
||||
try {
|
||||
quotient = Integer.divideUnsigned((int) dividend, (int) divisor);
|
||||
errors++;
|
||||
} catch(ArithmeticException ea) {
|
||||
; // Expected
|
||||
}
|
||||
|
||||
try {
|
||||
remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);
|
||||
errors++;
|
||||
} catch(ArithmeticException ea) {
|
||||
; // Expected
|
||||
}
|
||||
} else {
|
||||
quotient = Integer.divideUnsigned((int) dividend, (int) divisor);
|
||||
longQuotient = dividend / divisor;
|
||||
|
||||
if (quotient != (int)longQuotient) {
|
||||
errors++;
|
||||
System.err.printf("Unexpected unsigned divide result %s on %s/%s%n",
|
||||
Integer.toUnsignedString(quotient),
|
||||
Integer.toUnsignedString((int) dividend),
|
||||
Integer.toUnsignedString((int) divisor));
|
||||
}
|
||||
|
||||
remainder = Integer.remainderUnsigned((int) dividend, (int) divisor);
|
||||
longRemainder = dividend % divisor;
|
||||
|
||||
if (remainder != (int)longRemainder) {
|
||||
errors++;
|
||||
System.err.printf("Unexpected unsigned remainder result %s on %s%%%s%n",
|
||||
Integer.toUnsignedString(remainder),
|
||||
Integer.toUnsignedString((int) dividend),
|
||||
Integer.toUnsignedString((int) divisor));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
56
test/jdk/java/lang/Integer/ValueOf.java
Normal file
56
test/jdk/java/lang/Integer/ValueOf.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2018, 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 6807702
|
||||
* @summary Basic test for Integer.valueOf
|
||||
* @run main ValueOf
|
||||
* @run main/othervm -esa -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox
|
||||
* -XX:AutoBoxCacheMax=20000 ValueOf
|
||||
*/
|
||||
|
||||
public class ValueOf {
|
||||
|
||||
// test Integer.valueOf over this range (inclusive)
|
||||
private static final int TEST_LOW = -1024;
|
||||
private static final int TEST_HIGH = 24576;
|
||||
|
||||
public static void main(String[] args) {
|
||||
int i = TEST_LOW;
|
||||
while (i <= TEST_HIGH) {
|
||||
// check that valueOf stores i
|
||||
if (Integer.valueOf(i).intValue() != i)
|
||||
throw new RuntimeException();
|
||||
|
||||
// check that the same object is returned for integral values
|
||||
// in the range -128 to 127 (inclusive)
|
||||
if (i >= -128 && i <= 127) {
|
||||
if (Integer.valueOf(i) != Integer.valueOf(i))
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue