Index: /luni/src/test/java/tests/api/java/lang/StringTest.java =================================================================== --- /luni/src/test/java/tests/api/java/lang/StringTest.java (revision 388931) +++ /luni/src/test/java/tests/api/java/lang/StringTest.java (working copy) @@ -170,17 +170,183 @@ assertTrue("Created incorrect string", new String(sb) .equals("HelloWorld")); } + + /** + * @tests java.lang.String#String(java.lang.StringBuilder) + */ + public void test_ConstructorLjava_lang_StringBuilder() { + StringBuilder sb = new StringBuilder(32); + sb.append("HelloWorld"); + assertEquals("HelloWorld", new String(sb)); + + try { + new String((StringBuilder)null); + fail("No NPE"); + } catch (NullPointerException e) { + } + } + + /** + * @tests java.lang.String#String(int[],int,int) + */ + public void test_Constructor$III() { + assertEquals("HelloWorld", new String(new int[] {'H','e','l','l','o','W','o','r','l','d'}, 0, 10)); + assertEquals("Hello", new String(new int[] {'H','e','l','l','o','W','o','r','l','d'}, 0, 5)); + assertEquals("World", new String(new int[] {'H','e','l','l','o','W','o','r','l','d'}, 5, 5)); + assertEquals("", new String(new int[] {'H','e','l','l','o','W','o','r','l','d'}, 5, 0)); + + assertEquals("\uD800\uDC00", new String(new int[] {0x010000}, 0, 1)); + assertEquals("\uD800\uDC00a\uDBFF\uDFFF", new String(new int[] {0x010000, 'a', 0x010FFFF}, 0, 3)); + + try { + new String((int[])null, 0, 1); + fail("No NPE"); + } catch (NullPointerException e) { + } + + try { + new String(new int[] {'a','b'}, -1, 2); + fail("No IOOBE, negative offset"); + } catch (IndexOutOfBoundsException e) { + } + + try { + new String(new int[] {'a','b'}, 0, -1); + fail("No IOOBE, negative count"); + } catch (IndexOutOfBoundsException e) { + } + + try { + new String(new int[] {'a','b'}, 0, -1); + fail("No IOOBE, negative count"); + } catch (IndexOutOfBoundsException e) { + } + + try { + new String(new int[] {'a','b'}, 0, 3); + fail("No IOOBE, too large"); + } catch (IndexOutOfBoundsException e) { + } + } /** - * @tests java.lang.String#charAt(int) - */ - public void test_charAtI() { - // Test for method char java.lang.String.charAt(int) - assertTrue("Incorrect character returned", hw1.charAt(5) == 'W' - && (hw1.charAt(1) != 'Z')); - } + * @tests java.lang.String#charAt(int) + */ + public void test_charAtI() { + // Test for method char java.lang.String.charAt(int) + assertTrue("Incorrect character returned", hw1.charAt(5) == 'W' + && (hw1.charAt(1) != 'Z')); + } + + /** + * @tests java.lang.String.codePointAt(int) + */ + public void test_codePointAtI() { + String s = new String("abc"); + assertEquals('a', s.codePointAt(0)); + assertEquals('b', s.codePointAt(1)); + assertEquals('c', s.codePointAt(2)); + + s = new String("\uD800\uDC00"); + assertEquals(0x10000, s.codePointAt(0)); + assertEquals('\uDC00', s.codePointAt(1)); + + s = "abc"; + try { + s.codePointAt(-1); + fail("No IOOBE on negative index."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointAt(s.length()); + fail("No IOOBE on index equal to length."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointAt(s.length() + 1); + fail("No IOOBE on index greater than length."); + } catch (IndexOutOfBoundsException e) { + + } + } + + /** + * @tests java.lang.String.codePointBefore(int) + */ + public void test_codePointBeforeI() { + String s = new String("abc"); + assertEquals('a', s.codePointBefore(1)); + assertEquals('b', s.codePointBefore(2)); + assertEquals('c', s.codePointBefore(3)); + + s = new String("\uD800\uDC00"); + assertEquals(0x10000, s.codePointBefore(2)); + assertEquals('\uD800', s.codePointBefore(1)); + + s = "abc"; + + try { + s.codePointBefore(0); + fail("No IOOBE on zero index."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointBefore(-1); + fail("No IOOBE on negative index."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointBefore(s.length() + 1); + fail("No IOOBE on index greater than length."); + } catch (IndexOutOfBoundsException e) { + + } + } + + /** + * @tests java.lang.String.codePointCount(int, int) + */ + public void test_codePointCountII() { + assertEquals(1, new String("\uD800\uDC00").codePointCount(0, 2)); + assertEquals(1, new String("\uD800\uDC01").codePointCount(0, 2)); + assertEquals(1, new String("\uD801\uDC01").codePointCount(0, 2)); + assertEquals(1, new String("\uDBFF\uDFFF").codePointCount(0, 2)); + + assertEquals(3, new String("a\uD800\uDC00b").codePointCount(0, 4)); + assertEquals(4, new String("a\uD800\uDC00b\uD800").codePointCount(0, 5)); + + String s = "abc"; + try { + s.codePointCount(-1, 2); + fail("No IOOBE for negative begin index."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointCount(0, 4); + fail("No IOOBE for end index that's too large."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + s.codePointCount(3, 2); + fail("No IOOBE for begin index larger than end index."); + } catch (IndexOutOfBoundsException e) { + + } + } - /** + /** * @tests java.lang.String#compareTo(java.lang.Object) */ public void test_compareToLjava_lang_Object() { @@ -278,6 +444,54 @@ String s3 = s1.concat(s2); assertTrue("should not be identical", s2 != s3); } + + /** + * @tests java.lang.String#contentEquals(StringBuffer) + */ + public void test_contentEqualsLjava_lang_StringBuffer() { + String s = "abc"; + assertTrue(s.contentEquals(new StringBuffer("abc"))); + assertFalse(s.contentEquals(new StringBuffer("def"))); + assertFalse(s.contentEquals(new StringBuffer("ghij"))); + + try { + s.contentEquals((StringBuffer)null); + fail("No NPE"); + } catch (NullPointerException e) { + } + } + + /** + * @tests java.lang.String#contentEquals(CharSequence) + */ + public void test_contentEqualsLjava_lang_CharSequence() { + String s = "abc"; + assertTrue(s.contentEquals((CharSequence)new StringBuffer("abc"))); + assertFalse(s.contentEquals((CharSequence)new StringBuffer("def"))); + assertFalse(s.contentEquals((CharSequence)new StringBuffer("ghij"))); + + try { + s.contentEquals((CharSequence)null); + fail("No NPE"); + } catch (NullPointerException e) { + } + } + + /** + * @tests java.lang.String#contains(CharSequence) + */ + public void test_containsLjava_lang_CharSequence() { + String s = "abcdefghijklmnopqrstuvwxyz"; + assertTrue(s.contains((CharSequence)new StringBuffer("abc"))); + assertTrue(s.contains((CharSequence)new StringBuffer("def"))); + assertFalse(s.contains((CharSequence)new StringBuffer("ac"))); + + try { + s.contentEquals((CharSequence)null); + fail("No NPE"); + } catch (NullPointerException e) { + } + } /** * @tests java.lang.String#copyValueOf(char[]) @@ -611,6 +825,71 @@ } /** + * @tests java.lang.String.offsetByCodePoints(int, int)' + */ + public void test_offsetByCodePointsII() { + int result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 2); + assertEquals(3, result); + + result = new String("abcd").offsetByCodePoints(3, -1); + assertEquals(2, result); + + result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 3); + assertEquals(4, result); + + result = new String("a\uD800\uDC00b").offsetByCodePoints(3, -1); + assertEquals(1, result); + + result = new String("a\uD800\uDC00b").offsetByCodePoints(3, 0); + assertEquals(3, result); + + result = new String("\uD800\uDC00bc").offsetByCodePoints(3, 0); + assertEquals(3, result); + + result = new String("a\uDC00bc").offsetByCodePoints(3, -1); + assertEquals(2, result); + + result = new String("a\uD800bc").offsetByCodePoints(3, -1); + assertEquals(2, result); + + String sb = "abc"; + try { + sb.offsetByCodePoints(-1, 1); + fail("No IOOBE for negative index."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + sb.offsetByCodePoints(0, 4); + fail("No IOOBE for offset that's too large."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + sb.offsetByCodePoints(3, -4); + fail("No IOOBE for offset that's too small."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + sb.offsetByCodePoints(3, 1); + fail("No IOOBE for index that's too large."); + } catch (IndexOutOfBoundsException e) { + + } + + try { + sb.offsetByCodePoints(4, -1); + fail("No IOOBE for index that's too large."); + } catch (IndexOutOfBoundsException e) { + + } + } + + /** * @tests java.lang.String#regionMatches(int, java.lang.String, int, int) */ public void test_regionMatchesILjava_lang_StringII() {