classlib: fix corner case for Character.forDigit (#769)

This commit is contained in:
Ivan Hetman 2023-10-08 12:48:59 +03:00 committed by GitHub
parent a493d003e5
commit 2d91f539c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -363,7 +363,7 @@ public class TCharacter extends TObject implements TComparable<TCharacter> {
}
public static char forDigit(int digit, int radix) {
if (radix < MIN_RADIX || radix > MAX_RADIX || digit >= radix) {
if (radix < MIN_RADIX || radix > MAX_RADIX || digit < 0 || digit >= radix) {
return '\0';
}
return digit < 10 ? (char) ('0' + digit) : (char) ('a' + digit - 10);

View File

@ -77,4 +77,15 @@ public class CharacterTest {
assertEquals((char) 12405, Character.reverseBytes((char) 30000));
assertEquals((char) -12150, Character.reverseBytes((char) -30000));
}
@Test
public void characterForDigit() {
assertEquals('\0', Character.forDigit(-1, 5));
assertEquals('\0', Character.forDigit(6, 5));
assertEquals('\0', Character.forDigit(1, 1));
assertEquals('\0', Character.forDigit(1, 37));
assertEquals('a', Character.forDigit(10, 11));
assertEquals('\0', Character.forDigit(10, 10));
assertEquals('5', Character.forDigit(5, 6));
}
}