diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java index 2d10ade88..3b3128beb 100644 --- a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java @@ -34,9 +34,16 @@ import org.junit.Test; */ public class CharUtilsTest { + private static final char CHAR_COPY = '\u00a9'; private static final Character CHARACTER_A = new Character('A'); private static final Character CHARACTER_B = new Character('B'); - private static final char CHAR_COPY = '\u00a9'; + + @Test + public void testCompare() { + assertTrue(CharUtils.compare('a', 'b') < 0); + assertTrue(CharUtils.compare('c', 'c') == 0); + assertTrue(CharUtils.compare('c', 'a') > 0); + } @Test public void testConstructor() { @@ -48,36 +55,144 @@ public class CharUtilsTest { assertFalse(Modifier.isFinal(BooleanUtils.class.getModifiers())); } - @SuppressWarnings("deprecation") // intentional test of deprecated method @Test - public void testToCharacterObject_char() { - assertEquals(new Character('a'), CharUtils.toCharacterObject('a')); - assertSame(CharUtils.toCharacterObject('a'), CharUtils.toCharacterObject('a')); + public void testIsAscii_char() { + assertTrue(CharUtils.isAscii('a')); + assertTrue(CharUtils.isAscii('A')); + assertTrue(CharUtils.isAscii('3')); + assertTrue(CharUtils.isAscii('-')); + assertTrue(CharUtils.isAscii('\n')); + assertFalse(CharUtils.isAscii(CHAR_COPY)); - for (int i = 0; i < 128; i++) { - final Character ch = CharUtils.toCharacterObject((char) i); - final Character ch2 = CharUtils.toCharacterObject((char) i); - assertSame(ch, ch2); - assertEquals(i, ch.charValue()); + for (int i = 0; i < 255; i++) { + assertEquals(i < 128, CharUtils.isAscii((char) i)); } - for (int i = 128; i < 196; i++) { - final Character ch = CharUtils.toCharacterObject((char) i); - final Character ch2 = CharUtils.toCharacterObject((char) i); - assertEquals(ch, ch2); - assertTrue(ch != ch2); - assertEquals(i, ch.charValue()); - assertEquals(i, ch2.charValue()); - } - assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject('a')); } @Test - public void testToCharacterObject_String() { - assertNull(CharUtils.toCharacterObject(null)); - assertNull(CharUtils.toCharacterObject("")); - assertEquals(new Character('a'), CharUtils.toCharacterObject("a")); - assertEquals(new Character('a'), CharUtils.toCharacterObject("abc")); - assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject("a")); + public void testIsAsciiAlpha_char() { + assertTrue(CharUtils.isAsciiAlpha('a')); + assertTrue(CharUtils.isAsciiAlpha('A')); + assertFalse(CharUtils.isAsciiAlpha('3')); + assertFalse(CharUtils.isAsciiAlpha('-')); + assertFalse(CharUtils.isAsciiAlpha('\n')); + assertFalse(CharUtils.isAsciiAlpha(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')) { + assertTrue(CharUtils.isAsciiAlpha((char) i)); + } else { + assertFalse(CharUtils.isAsciiAlpha((char) i)); + } + } + } + + @Test + public void testIsAsciiAlphaLower_char() { + assertTrue(CharUtils.isAsciiAlphaLower('a')); + assertFalse(CharUtils.isAsciiAlphaLower('A')); + assertFalse(CharUtils.isAsciiAlphaLower('3')); + assertFalse(CharUtils.isAsciiAlphaLower('-')); + assertFalse(CharUtils.isAsciiAlphaLower('\n')); + assertFalse(CharUtils.isAsciiAlphaLower(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 'a' && i <= 'z') { + assertTrue(CharUtils.isAsciiAlphaLower((char) i)); + } else { + assertFalse(CharUtils.isAsciiAlphaLower((char) i)); + } + } + } + + @Test + public void testIsAsciiAlphanumeric_char() { + assertTrue(CharUtils.isAsciiAlphanumeric('a')); + assertTrue(CharUtils.isAsciiAlphanumeric('A')); + assertTrue(CharUtils.isAsciiAlphanumeric('3')); + assertFalse(CharUtils.isAsciiAlphanumeric('-')); + assertFalse(CharUtils.isAsciiAlphanumeric('\n')); + assertFalse(CharUtils.isAsciiAlphanumeric(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9')) { + assertTrue(CharUtils.isAsciiAlphanumeric((char) i)); + } else { + assertFalse(CharUtils.isAsciiAlphanumeric((char) i)); + } + } + } + + @Test + public void testIsAsciiAlphaUpper_char() { + assertFalse(CharUtils.isAsciiAlphaUpper('a')); + assertTrue(CharUtils.isAsciiAlphaUpper('A')); + assertFalse(CharUtils.isAsciiAlphaUpper('3')); + assertFalse(CharUtils.isAsciiAlphaUpper('-')); + assertFalse(CharUtils.isAsciiAlphaUpper('\n')); + assertFalse(CharUtils.isAsciiAlphaUpper(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 'A' && i <= 'Z') { + assertTrue(CharUtils.isAsciiAlphaUpper((char) i)); + } else { + assertFalse(CharUtils.isAsciiAlphaUpper((char) i)); + } + } + } + + @Test + public void testIsAsciiControl_char() { + assertFalse(CharUtils.isAsciiControl('a')); + assertFalse(CharUtils.isAsciiControl('A')); + assertFalse(CharUtils.isAsciiControl('3')); + assertFalse(CharUtils.isAsciiControl('-')); + assertTrue(CharUtils.isAsciiControl('\n')); + assertFalse(CharUtils.isAsciiControl(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i < 32 || i == 127) { + assertTrue(CharUtils.isAsciiControl((char) i)); + } else { + assertFalse(CharUtils.isAsciiControl((char) i)); + } + } + } + + @Test + public void testIsAsciiNumeric_char() { + assertFalse(CharUtils.isAsciiNumeric('a')); + assertFalse(CharUtils.isAsciiNumeric('A')); + assertTrue(CharUtils.isAsciiNumeric('3')); + assertFalse(CharUtils.isAsciiNumeric('-')); + assertFalse(CharUtils.isAsciiNumeric('\n')); + assertFalse(CharUtils.isAsciiNumeric(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= '0' && i <= '9') { + assertTrue(CharUtils.isAsciiNumeric((char) i)); + } else { + assertFalse(CharUtils.isAsciiNumeric((char) i)); + } + } + } + + @Test + public void testIsAsciiPrintable_char() { + assertTrue(CharUtils.isAsciiPrintable('a')); + assertTrue(CharUtils.isAsciiPrintable('A')); + assertTrue(CharUtils.isAsciiPrintable('3')); + assertTrue(CharUtils.isAsciiPrintable('-')); + assertFalse(CharUtils.isAsciiPrintable('\n')); + assertFalse(CharUtils.isAsciiPrintable(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 32 && i <= 126) { + assertTrue(CharUtils.isAsciiPrintable((char) i)); + } else { + assertFalse(CharUtils.isAsciiPrintable((char) i)); + } + } } @Test @@ -119,6 +234,38 @@ public class CharUtilsTest { assertEquals('X', CharUtils.toChar((String) null, 'X')); } + @SuppressWarnings("deprecation") // intentional test of deprecated method + @Test + public void testToCharacterObject_char() { + assertEquals(new Character('a'), CharUtils.toCharacterObject('a')); + assertSame(CharUtils.toCharacterObject('a'), CharUtils.toCharacterObject('a')); + + for (int i = 0; i < 128; i++) { + final Character ch = CharUtils.toCharacterObject((char) i); + final Character ch2 = CharUtils.toCharacterObject((char) i); + assertSame(ch, ch2); + assertEquals(i, ch.charValue()); + } + for (int i = 128; i < 196; i++) { + final Character ch = CharUtils.toCharacterObject((char) i); + final Character ch2 = CharUtils.toCharacterObject((char) i); + assertEquals(ch, ch2); + assertTrue(ch != ch2); + assertEquals(i, ch.charValue()); + assertEquals(i, ch2.charValue()); + } + assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject('a')); + } + + @Test + public void testToCharacterObject_String() { + assertNull(CharUtils.toCharacterObject(null)); + assertNull(CharUtils.toCharacterObject("")); + assertEquals(new Character('a'), CharUtils.toCharacterObject("a")); + assertEquals(new Character('a'), CharUtils.toCharacterObject("abc")); + assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject("a")); + } + @Test public void testToIntValue_char() { assertEquals(0, CharUtils.toIntValue('0')); @@ -217,151 +364,4 @@ public class CharUtilsTest { assertNull(CharUtils.unicodeEscaped(null)); assertEquals("\\u0041", CharUtils.unicodeEscaped(CHARACTER_A)); } - - @Test - public void testIsAscii_char() { - assertTrue(CharUtils.isAscii('a')); - assertTrue(CharUtils.isAscii('A')); - assertTrue(CharUtils.isAscii('3')); - assertTrue(CharUtils.isAscii('-')); - assertTrue(CharUtils.isAscii('\n')); - assertFalse(CharUtils.isAscii(CHAR_COPY)); - - for (int i = 0; i < 255; i++) { - assertEquals(i < 128, CharUtils.isAscii((char) i)); - } - } - - @Test - public void testIsAsciiPrintable_char() { - assertTrue(CharUtils.isAsciiPrintable('a')); - assertTrue(CharUtils.isAsciiPrintable('A')); - assertTrue(CharUtils.isAsciiPrintable('3')); - assertTrue(CharUtils.isAsciiPrintable('-')); - assertFalse(CharUtils.isAsciiPrintable('\n')); - assertFalse(CharUtils.isAsciiPrintable(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if (i >= 32 && i <= 126) { - assertTrue(CharUtils.isAsciiPrintable((char) i)); - } else { - assertFalse(CharUtils.isAsciiPrintable((char) i)); - } - } - } - - @Test - public void testIsAsciiControl_char() { - assertFalse(CharUtils.isAsciiControl('a')); - assertFalse(CharUtils.isAsciiControl('A')); - assertFalse(CharUtils.isAsciiControl('3')); - assertFalse(CharUtils.isAsciiControl('-')); - assertTrue(CharUtils.isAsciiControl('\n')); - assertFalse(CharUtils.isAsciiControl(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if (i < 32 || i == 127) { - assertTrue(CharUtils.isAsciiControl((char) i)); - } else { - assertFalse(CharUtils.isAsciiControl((char) i)); - } - } - } - - @Test - public void testIsAsciiAlpha_char() { - assertTrue(CharUtils.isAsciiAlpha('a')); - assertTrue(CharUtils.isAsciiAlpha('A')); - assertFalse(CharUtils.isAsciiAlpha('3')); - assertFalse(CharUtils.isAsciiAlpha('-')); - assertFalse(CharUtils.isAsciiAlpha('\n')); - assertFalse(CharUtils.isAsciiAlpha(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')) { - assertTrue(CharUtils.isAsciiAlpha((char) i)); - } else { - assertFalse(CharUtils.isAsciiAlpha((char) i)); - } - } - } - - @Test - public void testIsAsciiAlphaUpper_char() { - assertFalse(CharUtils.isAsciiAlphaUpper('a')); - assertTrue(CharUtils.isAsciiAlphaUpper('A')); - assertFalse(CharUtils.isAsciiAlphaUpper('3')); - assertFalse(CharUtils.isAsciiAlphaUpper('-')); - assertFalse(CharUtils.isAsciiAlphaUpper('\n')); - assertFalse(CharUtils.isAsciiAlphaUpper(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if (i >= 'A' && i <= 'Z') { - assertTrue(CharUtils.isAsciiAlphaUpper((char) i)); - } else { - assertFalse(CharUtils.isAsciiAlphaUpper((char) i)); - } - } - } - - @Test - public void testIsAsciiAlphaLower_char() { - assertTrue(CharUtils.isAsciiAlphaLower('a')); - assertFalse(CharUtils.isAsciiAlphaLower('A')); - assertFalse(CharUtils.isAsciiAlphaLower('3')); - assertFalse(CharUtils.isAsciiAlphaLower('-')); - assertFalse(CharUtils.isAsciiAlphaLower('\n')); - assertFalse(CharUtils.isAsciiAlphaLower(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if (i >= 'a' && i <= 'z') { - assertTrue(CharUtils.isAsciiAlphaLower((char) i)); - } else { - assertFalse(CharUtils.isAsciiAlphaLower((char) i)); - } - } - } - - @Test - public void testIsAsciiNumeric_char() { - assertFalse(CharUtils.isAsciiNumeric('a')); - assertFalse(CharUtils.isAsciiNumeric('A')); - assertTrue(CharUtils.isAsciiNumeric('3')); - assertFalse(CharUtils.isAsciiNumeric('-')); - assertFalse(CharUtils.isAsciiNumeric('\n')); - assertFalse(CharUtils.isAsciiNumeric(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if (i >= '0' && i <= '9') { - assertTrue(CharUtils.isAsciiNumeric((char) i)); - } else { - assertFalse(CharUtils.isAsciiNumeric((char) i)); - } - } - } - - @Test - public void testIsAsciiAlphanumeric_char() { - assertTrue(CharUtils.isAsciiAlphanumeric('a')); - assertTrue(CharUtils.isAsciiAlphanumeric('A')); - assertTrue(CharUtils.isAsciiAlphanumeric('3')); - assertFalse(CharUtils.isAsciiAlphanumeric('-')); - assertFalse(CharUtils.isAsciiAlphanumeric('\n')); - assertFalse(CharUtils.isAsciiAlphanumeric(CHAR_COPY)); - - for (int i = 0; i < 196; i++) { - if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9')) { - assertTrue(CharUtils.isAsciiAlphanumeric((char) i)); - } else { - assertFalse(CharUtils.isAsciiAlphanumeric((char) i)); - } - } - } - - @Test - public void testCompare() { - assertTrue(CharUtils.compare('a', 'b') < 0); - assertTrue(CharUtils.compare('c', 'c') == 0); - assertTrue(CharUtils.compare('c', 'a') > 0); - } }