Skip to content

Commit ec334db

Browse files
committed
Added assertions to testVariableLength and samples
1 parent 1892eca commit ec334db

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/java.base/share/classes/java/util/HexFormat.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,7 @@ public boolean equals(Object o) {
993993
}
994994

995995
/**
996-
* Returns a hashcode for this {@code HexFormat} that is consistent with
997-
* {@link #equals(Object) equals}.
996+
* Returns a hashcode for this {@code HexFormat}.
998997
*
999998
* @return a hashcode for this {@code HexFormat}
1000999
*/

test/jdk/java/util/HexFormat/HexFormatTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,27 @@ private static String escapeNL(String string) {
383383
static void testVariableLength() {
384384
HexFormat hex = HexFormat.of();
385385

386+
String allHex = "fedcba9876543210";
386387
final long orig = 0xfedcba9876543210L;
387388
for (int digits = 0; digits <= 16; digits++) {
388389
String s = hex.toHexDigits(orig, digits);
389390
long actual = hex.fromHexDigitsToLong(s, 0, digits);
390391
System.out.printf(" digits: %2d, formatted: \"%s\", parsed as: 0x%016xL%n",
391392
digits, s, actual);
393+
Assert.assertEquals(s, allHex.substring(16 - digits, 16));
394+
long expected = (digits < 16) ? orig & ~(0xffffffffffffffffL << (4 * digits)) : orig;
395+
Assert.assertEquals(actual, expected);
392396
}
393397
}
394398

399+
/**
400+
* Example code from the HexFormat javadoc.
401+
* Showing simple usage of the API using "assert" to express the correct results
402+
* when shown in the javadoc.
403+
* The additional TestNG asserts verify the correctness of the same code.
404+
*/
395405
@Test
396406
private static void samples() {
397-
398407
{
399408
// Primitive formatting and parsing.
400409
HexFormat hex = HexFormat.of();
@@ -406,24 +415,30 @@ private static void samples() {
406415
int byteVal = hex.fromHexDigits(byteStr);
407416
assert(byteStr.equals("7f"));
408417
assert(b == byteVal);
418+
Assert.assertTrue(byteStr.equals("7f"));
419+
Assert.assertTrue(b == byteVal);
420+
409421

410422
char c = 'A';
411423
String charStr = hex.toHexDigits(c);
412424
System.out.println(" " + charStr);
413425
int charVal = hex.fromHexDigits(charStr);
414426
assert(c == charVal);
427+
Assert.assertTrue(c == charVal);
415428

416429
int i = 12345;
417430
String intStr = hex.toHexDigits(i);
418431
System.out.println(" " + intStr);
419432
int intVal = hex.fromHexDigits(intStr);
420433
assert(i == intVal);
434+
Assert.assertTrue(i == intVal);
421435

422436
long l = Long.MAX_VALUE;
423437
String longStr = hex.toHexDigits(l, 16);
424438
long longVal = hex.fromHexDigitsToLong(longStr, 0, 16);
425439
System.out.println(" " + longStr + ", " + longVal);
426440
assert(l == longVal);
441+
Assert.assertTrue(l == longVal);
427442
}
428443

429444
{
@@ -436,6 +451,7 @@ private static void samples() {
436451
byte[] parsed = formatFingerprint.parseHex(str);
437452
System.out.println(" Parsed: " + Arrays.toString(parsed));
438453
assert(Arrays.equals(bytes, parsed));
454+
Assert.assertTrue(Arrays.equals(bytes, parsed));
439455
}
440456

441457
{
@@ -448,6 +464,7 @@ private static void samples() {
448464
byte[] parsed = commaFormat.parseHex(str);
449465
System.out.println(" Parsed: " + Arrays.toString(parsed));
450466
assert(Arrays.equals(bytes, parsed));
467+
Assert.assertTrue(Arrays.equals(bytes, parsed));
451468
}
452469
{
453470
// Text formatting
@@ -459,6 +476,7 @@ private static void samples() {
459476
byte[] parsed = commaFormat.parseHex(str);
460477
System.out.println(" Parsed: " + Arrays.toString(parsed));
461478
assert(Arrays.equals(bytes, parsed));
479+
Assert.assertTrue(Arrays.equals(bytes, parsed));
462480
}
463481
}
464482
}

0 commit comments

Comments
 (0)