/* * Copyright (c) 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.
*/
@DataProvider(name = "HexStringsThrowing")
Object[][] HexStringsThrowing() { returnnew Object[][]{
{"0", ":", "", ""}, // wrong string length
{"01:", ":", "", ""}, // wrong string length
{"01:0", ":", "", ""}, // wrong string length
{"0", ",", "", ""}, // wrong length and separator
{"01:", ",", "", ""}, // wrong length and separator
{"01:0", ",", "", ""}, // wrong length and separator
{"01:00", ",", "", ""}, // wrong separator
{"00]", ",", "[", "]"}, // missing prefix
{"[00", ",", "[", "]"}, // missing suffix
{"]", ",", "[", "]"}, // missing prefix
{"[", ",", "[", "]"}, // missing suffix
{"00", ",", "abc", ""}, // Prefix longer than string
{"01", ",", "", "def"}, // Suffix longer than string
{"abc00,", ",", "abc", ""}, // Prefix and delim but not another value
{"01def,", ",", "", "def"}, // Suffix and delim but not another value
};
}
@DataProvider(name = "BadBytesThrowing")
Object[][] badBytesThrowing() { returnnew Object[][]{
{newbyte[1], 0, 2}, // bad toIndex
{newbyte[1], 1, 2}, // bad fromIndex + toIndex
{newbyte[1], -1, 2}, // bad fromIndex
{newbyte[1], -1, 1}, // bad fromIndex
{newbyte[1], 0, -1}, // bad toIndex
{newbyte[1], 1, -1}, // bad toIndex
};
}
@DataProvider(name = "BadParseHexThrowing")
Object[][] badParseHexThrowing() { returnnew Object[][]{
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad fromIndex
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad fromIndex
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"76543210", 0, 7, IllegalArgumentException.class}, // odd number of digits
{"zz00", 0, 4, IllegalArgumentException.class}, // non-hex digits
{"00zz", 0, 4, IllegalArgumentException.class}, // non-hex digits
};
}
@DataProvider(name = "BadFromHexDigitsThrowing")
Object[][] badHexDigitsThrowing() { returnnew Object[][]{
{"a", 0, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, 2, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"a", -1, 2, IndexOutOfBoundsException.class}, // bad toIndex
{"b", -1, 1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
{"a", 0, -1, IndexOutOfBoundsException.class}, // bad toIndex
{"b", 1, -1, IndexOutOfBoundsException.class}, // bad fromIndex + toIndex
};
}
staticbyte[] genBytes(int origin, int len) { byte[] bytes = newbyte[len]; for (int i = 0; i < len; i++)
bytes[i] = (byte) (origin + i); return bytes;
}
@Test staticvoid testToHex() {
HexFormat hex = HexFormat.of(); for (int i = 0; i < 32; i++) { char c = hex.toLowHexDigit((byte)i);
String expected = Integer.toHexString(i & 0xf);
assertEquals(c, expected.charAt(0), "toHex formatting");
}
}
@Test staticvoid testToHexDigits() {
HexFormat hex = HexFormat.of(); for (int i = 0; i < 256; i++) {
String actual = hex.toHexDigits((byte)i); int expected = HexFormat.fromHexDigits(actual);
assertEquals(expected, i, "fromHexDigits");
assertEquals(actual.charAt(0), hex.toHighHexDigit((byte)i), "first char mismatch");
assertEquals(actual.charAt(1), hex.toLowHexDigit((byte)i), "second char mismatch");
}
}
@Test staticvoid testIsHexDigit() { for (int i = 0; i < 0x3ff; i++) { boolean actual = HexFormat.isHexDigit(i); boolean expected = Character.digit(i, 16) >= 0;
assertEquals(actual, expected, "isHexDigit: " + i);
}
}
@Test staticvoid testFromHexDigit() {
String chars = "0123456789ABCDEF0123456789abcdef"; for (int i = 0; i < chars.length(); i++) { int v = HexFormat.fromHexDigit(chars.charAt(i));
assertEquals(v, i & 0xf, "fromHex decode");
}
}
@Test staticvoid testFromHexInvalid() { for (int i = 0; i < 65536; i++) { char ch = (char)i; if (ch > 0xff || Character.digit(ch, 16) < 0) {
assertFalse(HexFormat.isHexDigit(ch), "isHexDigit incorrect for '" + ch + "' = " + i);
expectThrows(NumberFormatException.class,
() -> HexFormat.fromHexDigit(ch));
}
}
}
@Test staticvoid testAppendHexByteWithStringBuilder() {
HexFormat hex = HexFormat.of();
StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) {
sb.setLength(0);
StringBuilder sb1 = hex.toHexDigits(sb, (byte)i);
assertSame(sb1, sb, "toHexDigits returned different StringBuilder");
assertEquals(sb.length(), 2, "wrong length after append: " + i);
assertEquals(sb.charAt(0), hex.toHighHexDigit((byte)i), "MSB converted wrong");
assertEquals(sb.charAt(1), hex.toLowHexDigit((byte)i), "LSB converted wrong");
assertEquals(HexFormat.fromHexDigits(sb), i, "hex.format(sb, byte) wrong");
}
}
@Test staticvoid testAppendHexByteWithCharBuffer() {
HexFormat hex = HexFormat.of();
CharBuffer cb = CharBuffer.allocate(256); for (int i = 1; i <= 128; i++) {
CharBuffer cb1 = hex.toHexDigits(cb, (byte)i);
assertTrue(cb1 == cb);
assertEquals(cb.position(), i * 2);
}
assertEquals(cb.remaining(), 0);
}
@Test staticvoid testAppendHexByteWithCharArrayWriter() {
HexFormat hex = HexFormat.of();
CharArrayWriter caw = new CharArrayWriter(); for (int i = 1; i <= 128; i++) {
CharArrayWriter caw1 = hex.toHexDigits(caw, (byte)i);
assertTrue(caw1 == caw);
assertEquals(caw.size(), i * 2);
}
}
// Verify IAE for strings that are too long for the target primitive type // or the number of requested digits is too large.
@Test staticvoid wrongNumberDigits() {
assertThrows(IllegalArgumentException.class,
() -> HexFormat.fromHexDigits("9876543210"));
assertThrows(IllegalArgumentException.class,
() -> HexFormat.fromHexDigits("9876543210", 0, 9));
assertThrows(IllegalArgumentException.class,
() -> HexFormat.fromHexDigitsToLong("98765432109876543210"));
assertThrows(IllegalArgumentException.class,
() -> HexFormat.fromHexDigitsToLong("98765432109876543210", 0, 17));
}
if (expected.length > 1) { // check prefix and suffix is present for each hex pair for (int i = 0; i < expected.length; i++) { int valueChars = prefix.length() + 2 + suffix.length(); int offset = i * (valueChars + delimiter.length());
String value = res.substring(offset, offset + valueChars);
assertTrue(value.startsWith(prefix), "wrong prefix");
assertTrue(value.endsWith(suffix), "wrong suffix");
// Check case of digits
String cc = value.substring(prefix.length(), prefix.length() + 2);
assertEquals(cc,
(uppercase) ? cc.toUpperCase(Locale.ROOT) : cc.toLowerCase(Locale.ROOT), "Case mismatch"); if (i < expected.length - 1 && !delimiter.isEmpty()) { // Check the delimiter is present for each pair except the last
assertEquals(res.substring(offset + valueChars,
offset + valueChars + delimiter.length()), delimiter);
}
}
}
}
assertTrue(actual.equals(actual)); // equals self
assertFalse(actual.equals(null)); // never equals null
}
@Test(dataProvider="HexFormattersParsers") staticvoid testZeroLength(String delimiter, String prefix, String suffix, boolean uppercase,
HexFormat hex) { // Test formatting of zero length byte arrays, should produce no output
StringBuilder sb = new StringBuilder();
assertEquals(hex.formatHex(newbyte[0]), "", "Zero length");
assertEquals(hex.formatHex(newbyte[0], 0, 0), "", "Zero length");
hex.formatHex(sb, newbyte[0]);
assertEquals(sb.length(), 0, "length should not change");
hex.formatHex(sb, newbyte[0], 0, 0);
assertEquals(sb.length(), 0, "length should not change");
@Test(dataProvider="HexFormattersParsers") staticvoid testOOME(String delimiter, String prefix, String suffix, boolean uppercase,
HexFormat hex) { // compute the size of byte array that will exceed the buffer long valueChars = prefix.length() + 2 + suffix.length(); long stride = valueChars + delimiter.length(); long max = Integer.MAX_VALUE & 0xFFFFFFFFL; long len = max / stride; long remainder = max - ((len - 1) * stride); if (remainder > valueChars) {
len++;
remainder -= valueChars;
} try { byte[] bytes = newbyte[(int) len];
Throwable ex = expectThrows(OutOfMemoryError.class,
() -> hex.formatHex(bytes));
System.out.println("ex: " + ex);
} catch (OutOfMemoryError oome) {
System.out.printf("OOME: total mem: %08x, free mem: %08x, max mem: %08x%n",
Runtime.getRuntime().totalMemory(),
Runtime.getRuntime().freeMemory(),
Runtime.getRuntime().maxMemory()); thrownew SkipException("Insufficient Memory to test OOME");
}
}
/** * Example code from the HexFormat javadoc. * Showing simple usage of the API using "assert" to express the correct results * when shown in the javadoc. * The additional TestNG asserts verify the correctness of the same code.
*/
@Test privatestaticvoid samples() {
{ // Primitive formatting and parsing.
HexFormat hex = HexFormat.of();
/** * A test implementation of Appendable that throws IOException on all methods.
*/ staticclass ThrowingAppendable implements Appendable {
@Override public Appendable append(CharSequence csq) throws IOException { thrownew IOException(".append(CharSequence) always throws");
}
@Override public Appendable append(CharSequence csq, int start, int end) throws IOException { thrownew IOException(".append(CharSequence, start, end) always throws");
}
@Override public Appendable append(char c) throws IOException { thrownew IOException(".append(char) always throws");
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.14 Sekunden
(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 und die Messung sind noch experimentell.