From 77c134dc3866116f0de81c1b8fbd36a201051f8f Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Thu, 11 Mar 2004 16:51:51 +0000 Subject: [PATCH] ICU-3630 cover jsr 204 APIs where possible, also jb 3523 after a fashion X-SVN-Rev: 14686 --- .../com/ibm/icu/dev/test/lang/UTF16Test.java | 63 ++++++++++++++++++- icu4j/src/com/ibm/icu/text/UTF16.java | 14 +++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java b/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java index 7926b63f123..f642bdd8343 100755 --- a/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java +++ b/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java,v $ -* $Date: 2002/10/28 21:59:23 $ -* $Revision: 1.20 $ +* $Date: 2004/03/11 16:51:50 $ +* $Revision: 1.21 $ * ******************************************************************************* */ @@ -1439,7 +1439,64 @@ public final class UTF16Test extends TestFmwk e.printStackTrace(); } } - + + public void TestNewString() { + final int[] codePoints = { + UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE, UCharacter.MAX_LOW_SURROGATE), + UCharacter.toCodePoint(UCharacter.MAX_HIGH_SURROGATE, UCharacter.MIN_LOW_SURROGATE), + UCharacter.MAX_HIGH_SURROGATE, + 'A', + -1, + }; + + final String cpString = "" + + UCharacter.MIN_HIGH_SURROGATE + + UCharacter.MAX_LOW_SURROGATE + + UCharacter.MAX_HIGH_SURROGATE + + UCharacter.MIN_LOW_SURROGATE + + UCharacter.MAX_HIGH_SURROGATE + + 'A'; + + final int[][] tests = { + { 0, 1, 0, 2 }, + { 0, 2, 0, 4 }, + { 1, 1, 2, 2 }, + { 1, 2, 2, 3 }, + { 1, 3, 2, 4 }, + { 2, 2, 4, 2 }, + { 2, 3, 0, -1 }, + { 4, 5, 0, -1 }, + { 3, -1, 0, -1 } + }; + + for (int i = 0; i < tests.length; ++i) { + int[] t = tests[i]; + int s = t[0]; + int c = t[1]; + int rs = t[2]; + int rc = t[3]; + + Exception e = null; + try { + String str = UTF16.newString(codePoints, s, c); + if (rc == -1 || !str.equals(cpString.substring(rs, rs+rc))) { + errln("failed codePoints iter: " + i + " start: " + s + " len: " + c); + } + continue; + } + catch (IndexOutOfBoundsException e1) { + e = e1; + } + catch (IllegalArgumentException e2) { + e = e2; + } + if (rc != -1) { + errln(e.getMessage()); + } + } + } + + // private data members ---------------------------------------------- private final static String INDEXOF_SUPPLEMENTARY_STRING_ = diff --git a/icu4j/src/com/ibm/icu/text/UTF16.java b/icu4j/src/com/ibm/icu/text/UTF16.java index 6d1abd63f02..d53c30fbac7 100755 --- a/icu4j/src/com/ibm/icu/text/UTF16.java +++ b/icu4j/src/com/ibm/icu/text/UTF16.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/UTF16.java,v $ -* $Date: 2004/03/11 07:23:29 $ -* $Revision: 1.35 $ +* $Date: 2004/03/11 16:51:51 $ +* $Revision: 1.36 $ * ******************************************************************************* */ @@ -2299,16 +2299,20 @@ public final class UTF16 } /** - * Cover JDK 1.5 API. Create String from an array of codePoints. + * Cover JDK 1.5 API. Create a String from an array of codePoints. * @param codePoints the code array * @param offset the start of the text in the code point array * @param count the number of code points * @return a String representing the code points between offset and count * @throws IllegalArgumentException if an invalid code point is encountered - * @throws IndexOutOfBoundsException if the offset or count are out of bounds. + * @throws IndexOutOfBoundsException if the offset or count are out of bounds. + * @draft ICU 3.0 */ public static String newString(int[] codePoints, int offset, int count) { - char[] chars = new char[codePoints.length]; + if (count < 0) { + throw new IllegalArgumentException(); + } + char[] chars = new char[count]; int w = 0; for (int r = offset, e = offset + count; r < e; ++r) { int cp = codePoints[r];