mirror of https://github.com/apache/poi.git
Reformat and add more tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1919238 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e2044c958b
commit
a971751238
|
@ -41,22 +41,20 @@ public final class NumberFormatter {
|
|||
private static final int T_UPPER_LETTER = 3;
|
||||
private static final int T_UPPER_ROMAN = 1;
|
||||
|
||||
public static String getNumber( int num, int style )
|
||||
{
|
||||
switch ( style )
|
||||
{
|
||||
case T_UPPER_ROMAN:
|
||||
return toRoman( num ).toUpperCase(Locale.ROOT);
|
||||
case T_LOWER_ROMAN:
|
||||
return toRoman( num );
|
||||
case T_UPPER_LETTER:
|
||||
return toLetters( num ).toUpperCase(Locale.ROOT);
|
||||
case T_LOWER_LETTER:
|
||||
return toLetters( num );
|
||||
case T_ARABIC:
|
||||
case T_ORDINAL:
|
||||
default:
|
||||
return String.valueOf( num );
|
||||
public static String getNumber( int num, int style ) {
|
||||
switch ( style ) {
|
||||
case T_UPPER_ROMAN:
|
||||
return toRoman( num ).toUpperCase(Locale.ROOT);
|
||||
case T_LOWER_ROMAN:
|
||||
return toRoman( num );
|
||||
case T_UPPER_LETTER:
|
||||
return toLetters( num ).toUpperCase(Locale.ROOT);
|
||||
case T_LOWER_LETTER:
|
||||
return toLetters( num );
|
||||
case T_ARABIC:
|
||||
case T_ORDINAL:
|
||||
default:
|
||||
return String.valueOf( num );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,19 +79,16 @@ public final class NumberFormatter {
|
|||
return new String(buf, charPos, (buf.length - charPos));
|
||||
}
|
||||
|
||||
private static String toRoman( int number )
|
||||
{
|
||||
private static String toRoman( int number ) {
|
||||
if ( number <= 0 )
|
||||
throw new IllegalArgumentException( "Unsupported number: " + number );
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for ( int i = 0; i < ROMAN_LETTERS.length; i++ )
|
||||
{
|
||||
for ( int i = 0; i < ROMAN_LETTERS.length; i++ ) {
|
||||
String letter = ROMAN_LETTERS[i];
|
||||
int value = ROMAN_VALUES[i];
|
||||
while ( number >= value )
|
||||
{
|
||||
while ( number >= value ) {
|
||||
number -= value;
|
||||
result.append( letter );
|
||||
}
|
||||
|
|
|
@ -17,13 +17,42 @@
|
|||
package org.apache.poi.hwpf.converter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestNumberFormatter {
|
||||
|
||||
@Test
|
||||
void testRoman() {
|
||||
void testArabicOrOrdinal() {
|
||||
assertEquals( "1", NumberFormatter.getNumber( 1, 5 ) );
|
||||
assertEquals( "2", NumberFormatter.getNumber( 2, 5 ) );
|
||||
assertEquals( "3", NumberFormatter.getNumber( 3, 5 ) );
|
||||
assertEquals( "4", NumberFormatter.getNumber( 4, 5 ) );
|
||||
assertEquals( "5", NumberFormatter.getNumber( 5, 5 ) );
|
||||
assertEquals( "6", NumberFormatter.getNumber( 6, 5 ) );
|
||||
assertEquals( "7", NumberFormatter.getNumber( 7, 5 ) );
|
||||
assertEquals( "8", NumberFormatter.getNumber( 8, 5 ) );
|
||||
assertEquals( "9", NumberFormatter.getNumber( 9, 5 ) );
|
||||
assertEquals( "10", NumberFormatter.getNumber( 10, 5 ) );
|
||||
|
||||
assertEquals( "1606", NumberFormatter.getNumber( 1606, 0 ) );
|
||||
assertEquals( "1910", NumberFormatter.getNumber( 1910, 0 ) );
|
||||
assertEquals( "1954", NumberFormatter.getNumber( 1954, 0 ) );
|
||||
|
||||
for ( int i = 1; i < 1000000; i++ ) {
|
||||
// make sure there is no exceptions
|
||||
assertEquals(Integer.toString(i), NumberFormatter.getNumber( i, 0 ));
|
||||
}
|
||||
|
||||
assertEquals( "0", NumberFormatter.getNumber( 0, 0 ) );
|
||||
assertEquals( "-1", NumberFormatter.getNumber( -1, 0 ) );
|
||||
|
||||
assertEquals( "1", NumberFormatter.getNumber( 1, 34 ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRomanLower() {
|
||||
assertEquals( "i", NumberFormatter.getNumber( 1, 2 ) );
|
||||
assertEquals( "ii", NumberFormatter.getNumber( 2, 2 ) );
|
||||
assertEquals( "iii", NumberFormatter.getNumber( 3, 2 ) );
|
||||
|
@ -38,10 +67,40 @@ public class TestNumberFormatter {
|
|||
assertEquals( "mdcvi", NumberFormatter.getNumber( 1606, 2 ) );
|
||||
assertEquals( "mcmx", NumberFormatter.getNumber( 1910, 2 ) );
|
||||
assertEquals( "mcmliv", NumberFormatter.getNumber( 1954, 2 ) );
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( 0, 2));
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( -1, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnglish() {
|
||||
void testRomanUpper() {
|
||||
assertEquals( "I", NumberFormatter.getNumber( 1, 1 ) );
|
||||
assertEquals( "II", NumberFormatter.getNumber( 2, 1 ) );
|
||||
assertEquals( "III", NumberFormatter.getNumber( 3, 1 ) );
|
||||
assertEquals( "IV", NumberFormatter.getNumber( 4, 1 ) );
|
||||
assertEquals( "V", NumberFormatter.getNumber( 5, 1 ) );
|
||||
assertEquals( "VI", NumberFormatter.getNumber( 6, 1 ) );
|
||||
assertEquals( "VII", NumberFormatter.getNumber( 7, 1 ) );
|
||||
assertEquals( "VIII", NumberFormatter.getNumber( 8, 1 ) );
|
||||
assertEquals( "IX", NumberFormatter.getNumber( 9, 1 ) );
|
||||
assertEquals( "X", NumberFormatter.getNumber( 10, 1 ) );
|
||||
|
||||
assertEquals( "MDCVI", NumberFormatter.getNumber( 1606, 1 ) );
|
||||
assertEquals( "MCMX", NumberFormatter.getNumber( 1910, 1 ) );
|
||||
assertEquals( "MCMLIV", NumberFormatter.getNumber( 1954, 1 ) );
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( 0, 1 ));
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( -1, 1 ));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnglishLower() {
|
||||
assertEquals( "a", NumberFormatter.getNumber( 1, 4 ) );
|
||||
assertEquals( "z", NumberFormatter.getNumber( 26, 4 ) );
|
||||
|
||||
|
@ -81,10 +140,62 @@ public class TestNumberFormatter {
|
|||
NumberFormatter.getNumber( 26 * 26 * 26 * 26 + 26 * 26 * 26
|
||||
+ 26 * 26 + 26, 4 ) );
|
||||
|
||||
for ( int i = 1; i < 1000000; i++ )
|
||||
{
|
||||
for ( int i = 1; i < 1000000; i++ ) {
|
||||
// make sure there is no exceptions
|
||||
NumberFormatter.getNumber( i, 4 );
|
||||
}
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( 0, 4 ));
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> NumberFormatter.getNumber( -1, 4 ));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnglishUpper() {
|
||||
assertEquals( "A", NumberFormatter.getNumber( 1, 3 ) );
|
||||
assertEquals( "Z", NumberFormatter.getNumber( 26, 3 ) );
|
||||
|
||||
assertEquals( "AA", NumberFormatter.getNumber( 26 + 1, 3 ) );
|
||||
assertEquals( "AZ", NumberFormatter.getNumber( 26 + 26, 3 ) );
|
||||
|
||||
assertEquals( "ZA", NumberFormatter.getNumber( 26 * 26 + 1, 3 ) );
|
||||
assertEquals( "ZZ", NumberFormatter.getNumber( 26 * 26 + 26, 3 ) );
|
||||
|
||||
assertEquals( "AAA",
|
||||
NumberFormatter.getNumber( 26 * 26 + 26 + 1, 3 ) );
|
||||
assertEquals( "AAZ",
|
||||
NumberFormatter.getNumber( 26 * 26 + 26 + 26, 3 ) );
|
||||
|
||||
assertEquals( "ABA",
|
||||
NumberFormatter.getNumber( 26 * 26 + 2 * 26 + 1, 3 ) );
|
||||
assertEquals( "AZA",
|
||||
NumberFormatter.getNumber( 26 * 26 + 26 * 26 + 1, 3 ) );
|
||||
|
||||
assertEquals( "AZZ",
|
||||
NumberFormatter.getNumber( 26 * 26 + 26 * 26 + 26, 3 ) );
|
||||
assertEquals( "BAA",
|
||||
NumberFormatter.getNumber( 2 * 26 * 26 + 26 + 1, 3 ) );
|
||||
assertEquals( "ZAA",
|
||||
NumberFormatter.getNumber( 26 * 26 * 26 + 26 + 1, 3 ) );
|
||||
assertEquals( "ZZZ",
|
||||
NumberFormatter.getNumber( 26 * 26 * 26 + 26 * 26 + 26, 3 ) );
|
||||
|
||||
assertEquals(
|
||||
"AAAA",
|
||||
NumberFormatter.getNumber( 26 * 26 * 26 + 26 * 26 + 26 + 1, 3 ) );
|
||||
assertEquals(
|
||||
"AZZZ",
|
||||
NumberFormatter.getNumber( 26 * 26 * 26 + 26 * 26 * 26 + 26 * 26 + 26, 3 ) );
|
||||
assertEquals(
|
||||
"ZZZZ",
|
||||
NumberFormatter.getNumber( 26 * 26 * 26 * 26 + 26 * 26 * 26
|
||||
+ 26 * 26 + 26, 3 ) );
|
||||
|
||||
for ( int i = 1; i < 1000000; i++ ) {
|
||||
// make sure there is no exceptions
|
||||
NumberFormatter.getNumber( i, 3 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue