0001: /*
0002: *******************************************************************************
0003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
0004: * others. All Rights Reserved. *
0005: *******************************************************************************
0006: */
0007:
0008: package com.ibm.icu.dev.test.lang;
0009:
0010: import com.ibm.icu.dev.test.TestFmwk;
0011: import com.ibm.icu.dev.test.UTF16Util;
0012: import com.ibm.icu.lang.UCharacter;
0013: import com.ibm.icu.text.UTF16;
0014: import com.ibm.icu.text.ReplaceableString;
0015: import com.ibm.icu.impl.Utility;
0016:
0017: /**
0018: * Testing class for UTF16
0019: * @author Syn Wee Quek
0020: * @since feb 09 2001
0021: */
0022: public final class UTF16Test extends TestFmwk {
0023: // constructor ===================================================
0024:
0025: /**
0026: * Constructor
0027: */
0028: public UTF16Test() {
0029: }
0030:
0031: // public methods ================================================
0032:
0033: /**
0034: * Testing UTF16 class methods append
0035: */
0036: public void TestAppend() {
0037: StringBuffer strbuff = new StringBuffer("this is a string ");
0038: char array[] = new char[UCharacter.MAX_VALUE >> 2];
0039: int strsize = strbuff.length();
0040: int arraysize = strsize;
0041:
0042: Utility.getChars(strbuff, 0, strsize, array, 0);
0043: for (int i = 1; i < UCharacter.MAX_VALUE; i += 100) {
0044: UTF16.append(strbuff, i);
0045: arraysize = UTF16.append(array, arraysize, i);
0046:
0047: String arraystr = new String(array, 0, arraysize);
0048: if (!arraystr.equals(strbuff.toString())) {
0049: errln("FAIL Comparing char array append and string append "
0050: + "with 0x" + Integer.toHexString(i));
0051: }
0052:
0053: // this is to cater for the combination of 0xDBXX 0xDC50 which
0054: // forms a supplementary character
0055: if (i == 0xDC51) {
0056: strsize--;
0057: }
0058:
0059: if (UTF16.countCodePoint(strbuff) != strsize + (i / 100)
0060: + 1) {
0061: errln("FAIL Counting code points in string appended with "
0062: + " 0x" + Integer.toHexString(i));
0063: break;
0064: }
0065: }
0066:
0067: // coverage for new 1.5 - cover only so no real test
0068: strbuff = new StringBuffer();
0069: UTF16.appendCodePoint(strbuff, 0x10000);
0070: if (strbuff.length() != 2) {
0071: errln("fail appendCodePoint");
0072: }
0073: }
0074:
0075: /**
0076: * Testing UTF16 class methods bounds
0077: */
0078: public void TestBounds() {
0079: StringBuffer strbuff =
0080: //0 12345 6 7 8 9
0081: new StringBuffer("\udc000123\ud800\udc00\ud801\udc01\ud802");
0082: String str = strbuff.toString();
0083: char array[] = str.toCharArray();
0084: int boundtype[] = { UTF16.SINGLE_CHAR_BOUNDARY,
0085: UTF16.SINGLE_CHAR_BOUNDARY, UTF16.SINGLE_CHAR_BOUNDARY,
0086: UTF16.SINGLE_CHAR_BOUNDARY, UTF16.SINGLE_CHAR_BOUNDARY,
0087: UTF16.LEAD_SURROGATE_BOUNDARY,
0088: UTF16.TRAIL_SURROGATE_BOUNDARY,
0089: UTF16.LEAD_SURROGATE_BOUNDARY,
0090: UTF16.TRAIL_SURROGATE_BOUNDARY,
0091: UTF16.SINGLE_CHAR_BOUNDARY };
0092: int length = str.length();
0093: for (int i = 0; i < length; i++) {
0094: if (UTF16.bounds(str, i) != boundtype[i]) {
0095: errln("FAIL checking bound type at index " + i);
0096: }
0097: if (UTF16.bounds(strbuff, i) != boundtype[i]) {
0098: errln("FAIL checking bound type at index " + i);
0099: }
0100: if (UTF16.bounds(array, 0, length, i) != boundtype[i]) {
0101: errln("FAIL checking bound type at index " + i);
0102: }
0103: }
0104: // does not straddle between supplementary character
0105: int start = 4;
0106: int limit = 9;
0107: int subboundtype1[] = { UTF16.SINGLE_CHAR_BOUNDARY,
0108: UTF16.LEAD_SURROGATE_BOUNDARY,
0109: UTF16.TRAIL_SURROGATE_BOUNDARY,
0110: UTF16.LEAD_SURROGATE_BOUNDARY,
0111: UTF16.TRAIL_SURROGATE_BOUNDARY };
0112: try {
0113: UTF16.bounds(array, start, limit, -1);
0114: errln("FAIL Out of bounds index in bounds should fail");
0115: } catch (Exception e) {
0116: // getting rid of warnings
0117: System.out.print("");
0118: }
0119:
0120: for (int i = 0; i < limit - start; i++) {
0121: if (UTF16.bounds(array, start, limit, i) != subboundtype1[i]) {
0122: errln("FAILED Subarray bounds in [" + start + ", "
0123: + limit + "] expected " + subboundtype1[i]
0124: + " at offset " + i);
0125: }
0126: }
0127:
0128: // starts from the mid of a supplementary character
0129: int subboundtype2[] = { UTF16.SINGLE_CHAR_BOUNDARY,
0130: UTF16.LEAD_SURROGATE_BOUNDARY,
0131: UTF16.TRAIL_SURROGATE_BOUNDARY };
0132:
0133: start = 6;
0134: limit = 9;
0135: for (int i = 0; i < limit - start; i++) {
0136: if (UTF16.bounds(array, start, limit, i) != subboundtype2[i]) {
0137: errln("FAILED Subarray bounds in [" + start + ", "
0138: + limit + "] expected " + subboundtype2[i]
0139: + " at offset " + i);
0140: }
0141: }
0142:
0143: // ends in the mid of a supplementary character
0144: int subboundtype3[] = { UTF16.LEAD_SURROGATE_BOUNDARY,
0145: UTF16.TRAIL_SURROGATE_BOUNDARY,
0146: UTF16.SINGLE_CHAR_BOUNDARY };
0147: start = 5;
0148: limit = 8;
0149: for (int i = 0; i < limit - start; i++) {
0150: if (UTF16.bounds(array, start, limit, i) != subboundtype3[i]) {
0151: errln("FAILED Subarray bounds in [" + start + ", "
0152: + limit + "] expected " + subboundtype3[i]
0153: + " at offset " + i);
0154: }
0155: }
0156: }
0157:
0158: /**
0159: * Testing UTF16 class methods charAt and charAtCodePoint
0160: */
0161: public void TestCharAt() {
0162: StringBuffer strbuff = new StringBuffer(
0163: "12345\ud800\udc0167890\ud800\udc02");
0164: if (UTF16.charAt(strbuff, 0) != '1'
0165: || UTF16.charAt(strbuff, 2) != '3'
0166: || UTF16.charAt(strbuff, 5) != 0x10001
0167: || UTF16.charAt(strbuff, 6) != 0x10001
0168: || UTF16.charAt(strbuff, 12) != 0x10002
0169: || UTF16.charAt(strbuff, 13) != 0x10002) {
0170: errln("FAIL Getting character from string buffer error");
0171: }
0172: String str = strbuff.toString();
0173: if (UTF16.charAt(str, 0) != '1' || UTF16.charAt(str, 2) != '3'
0174: || UTF16.charAt(str, 5) != 0x10001
0175: || UTF16.charAt(str, 6) != 0x10001
0176: || UTF16.charAt(str, 12) != 0x10002
0177: || UTF16.charAt(str, 13) != 0x10002) {
0178: errln("FAIL Getting character from string error");
0179: }
0180: char array[] = str.toCharArray();
0181: int start = 0;
0182: int limit = str.length();
0183: if (UTF16.charAt(array, start, limit, 0) != '1'
0184: || UTF16.charAt(array, start, limit, 2) != '3'
0185: || UTF16.charAt(array, start, limit, 5) != 0x10001
0186: || UTF16.charAt(array, start, limit, 6) != 0x10001
0187: || UTF16.charAt(array, start, limit, 12) != 0x10002
0188: || UTF16.charAt(array, start, limit, 13) != 0x10002) {
0189: errln("FAIL Getting character from array error");
0190: }
0191: // check the sub array here.
0192: start = 6;
0193: limit = 13;
0194: try {
0195: UTF16.charAt(array, start, limit, -1);
0196: errln("FAIL out of bounds error expected");
0197: } catch (Exception e) {
0198: System.out.print("");
0199: }
0200: try {
0201: UTF16.charAt(array, start, limit, 8);
0202: errln("FAIL out of bounds error expected");
0203: } catch (Exception e) {
0204: System.out.print("");
0205: }
0206: if (UTF16.charAt(array, start, limit, 0) != 0xdc01) {
0207: errln("FAIL Expected result in subarray 0xdc01");
0208: }
0209: if (UTF16.charAt(array, start, limit, 6) != 0xd800) {
0210: errln("FAIL Expected result in subarray 0xd800");
0211: }
0212: ReplaceableString replaceable = new ReplaceableString(str);
0213: if (UTF16.charAt(replaceable, 0) != '1'
0214: || UTF16.charAt(replaceable, 2) != '3'
0215: || UTF16.charAt(replaceable, 5) != 0x10001
0216: || UTF16.charAt(replaceable, 6) != 0x10001
0217: || UTF16.charAt(replaceable, 12) != 0x10002
0218: || UTF16.charAt(replaceable, 13) != 0x10002) {
0219: errln("FAIL Getting character from replaceable error");
0220: }
0221: }
0222:
0223: /**
0224: * Testing UTF16 class methods countCodePoint
0225: */
0226: public void TestCountCodePoint() {
0227: StringBuffer strbuff = new StringBuffer("");
0228: char array[] = null;
0229: if (UTF16.countCodePoint(strbuff) != 0
0230: || UTF16.countCodePoint("") != 0
0231: || UTF16.countCodePoint(array, 0, 0) != 0) {
0232: errln("FAIL Counting code points for empty strings");
0233: }
0234:
0235: strbuff = new StringBuffer("this is a string ");
0236: String str = strbuff.toString();
0237: array = str.toCharArray();
0238: int size = str.length();
0239:
0240: if (UTF16.countCodePoint(array, 0, 0) != 0) {
0241: errln("FAIL Counting code points for 0 offset array");
0242: }
0243:
0244: if (UTF16.countCodePoint(str) != size
0245: || UTF16.countCodePoint(strbuff) != size
0246: || UTF16.countCodePoint(array, 0, size) != size) {
0247: errln("FAIL Counting code points");
0248: }
0249:
0250: UTF16.append(strbuff, 0x10000);
0251: str = strbuff.toString();
0252: array = str.toCharArray();
0253: if (UTF16.countCodePoint(str) != size + 1
0254: || UTF16.countCodePoint(strbuff) != size + 1
0255: || UTF16.countCodePoint(array, 0, size + 1) != size + 1
0256: || UTF16.countCodePoint(array, 0, size + 2) != size + 1) {
0257: errln("FAIL Counting code points");
0258: }
0259: UTF16.append(strbuff, 0x61);
0260: str = strbuff.toString();
0261: array = str.toCharArray();
0262: if (UTF16.countCodePoint(str) != size + 2
0263: || UTF16.countCodePoint(strbuff) != size + 2
0264: || UTF16.countCodePoint(array, 0, size + 1) != size + 1
0265: || UTF16.countCodePoint(array, 0, size + 2) != size + 1
0266: || UTF16.countCodePoint(array, 0, size + 3) != size + 2) {
0267: errln("FAIL Counting code points");
0268: }
0269: }
0270:
0271: /**
0272: * Testing UTF16 class methods delete
0273: */
0274: public void TestDelete() { //01234567890123456
0275: StringBuffer strbuff = new StringBuffer("these are strings");
0276: int size = strbuff.length();
0277: char array[] = strbuff.toString().toCharArray();
0278:
0279: UTF16.delete(strbuff, 3);
0280: UTF16.delete(strbuff, 3);
0281: UTF16.delete(strbuff, 3);
0282: UTF16.delete(strbuff, 3);
0283: UTF16.delete(strbuff, 3);
0284: UTF16.delete(strbuff, 3);
0285: try {
0286: UTF16.delete(strbuff, strbuff.length());
0287: errln("FAIL deleting out of bounds character should fail");
0288: } catch (Exception e) {
0289: System.out.print("");
0290: }
0291: UTF16.delete(strbuff, strbuff.length() - 1);
0292: if (!strbuff.toString().equals("the string")) {
0293: errln("FAIL expected result after deleting characters is "
0294: + "\"the string\"");
0295: }
0296:
0297: size = UTF16.delete(array, size, 3);
0298: size = UTF16.delete(array, size, 3);
0299: size = UTF16.delete(array, size, 3);
0300: size = UTF16.delete(array, size, 3);
0301: size = UTF16.delete(array, size, 3);
0302: size = UTF16.delete(array, size, 3);
0303: try {
0304: UTF16.delete(array, size, size);
0305: errln("FAIL deleting out of bounds character should fail");
0306: } catch (Exception e) {
0307: System.out.print("");
0308: }
0309: size = UTF16.delete(array, size, size - 1);
0310: String str = new String(array, 0, size);
0311: if (!str.equals("the string")) {
0312: errln("FAIL expected result after deleting characters is "
0313: + "\"the string\"");
0314: }
0315: //012345678 9 01 2 3 4
0316: strbuff = new StringBuffer(
0317: "string: \ud800\udc00 \ud801\udc01 \ud801\udc01");
0318: size = strbuff.length();
0319: array = strbuff.toString().toCharArray();
0320:
0321: UTF16.delete(strbuff, 8);
0322: UTF16.delete(strbuff, 8);
0323: UTF16.delete(strbuff, 9);
0324: UTF16.delete(strbuff, 8);
0325: UTF16.delete(strbuff, 9);
0326: UTF16.delete(strbuff, 6);
0327: UTF16.delete(strbuff, 6);
0328: if (!strbuff.toString().equals("string")) {
0329: errln("FAIL expected result after deleting characters is \"string\"");
0330: }
0331:
0332: size = UTF16.delete(array, size, 8);
0333: size = UTF16.delete(array, size, 8);
0334: size = UTF16.delete(array, size, 9);
0335: size = UTF16.delete(array, size, 8);
0336: size = UTF16.delete(array, size, 9);
0337: size = UTF16.delete(array, size, 6);
0338: size = UTF16.delete(array, size, 6);
0339: str = new String(array, 0, size);
0340: if (!str.equals("string")) {
0341: errln("FAIL expected result after deleting characters is \"string\"");
0342: }
0343: }
0344:
0345: /**
0346: * Testing findOffsetFromCodePoint and findCodePointOffset
0347: */
0348: public void TestfindOffset() {
0349: // jitterbug 47
0350: String str = "a\uD800\uDC00b";
0351: StringBuffer strbuff = new StringBuffer(str);
0352: char array[] = str.toCharArray();
0353: int limit = str.length();
0354: if (UTF16.findCodePointOffset(str, 0) != 0
0355: || UTF16.findOffsetFromCodePoint(str, 0) != 0
0356: || UTF16.findCodePointOffset(strbuff, 0) != 0
0357: || UTF16.findOffsetFromCodePoint(strbuff, 0) != 0
0358: || UTF16.findCodePointOffset(array, 0, limit, 0) != 0
0359: || UTF16.findOffsetFromCodePoint(array, 0, limit, 0) != 0) {
0360: errln("FAIL Getting the first codepoint offset to a string with "
0361: + "supplementary characters");
0362: }
0363: if (UTF16.findCodePointOffset(str, 1) != 1
0364: || UTF16.findOffsetFromCodePoint(str, 1) != 1
0365: || UTF16.findCodePointOffset(strbuff, 1) != 1
0366: || UTF16.findOffsetFromCodePoint(strbuff, 1) != 1
0367: || UTF16.findCodePointOffset(array, 0, limit, 1) != 1
0368: || UTF16.findOffsetFromCodePoint(array, 0, limit, 1) != 1) {
0369: errln("FAIL Getting the second codepoint offset to a string with "
0370: + "supplementary characters");
0371: }
0372: if (UTF16.findCodePointOffset(str, 2) != 1
0373: || UTF16.findOffsetFromCodePoint(str, 2) != 3
0374: || UTF16.findCodePointOffset(strbuff, 2) != 1
0375: || UTF16.findOffsetFromCodePoint(strbuff, 2) != 3
0376: || UTF16.findCodePointOffset(array, 0, limit, 2) != 1
0377: || UTF16.findOffsetFromCodePoint(array, 0, limit, 2) != 3) {
0378: errln("FAIL Getting the third codepoint offset to a string with "
0379: + "supplementary characters");
0380: }
0381: if (UTF16.findCodePointOffset(str, 3) != 2
0382: || UTF16.findOffsetFromCodePoint(str, 3) != 4
0383: || UTF16.findCodePointOffset(strbuff, 3) != 2
0384: || UTF16.findOffsetFromCodePoint(strbuff, 3) != 4
0385: || UTF16.findCodePointOffset(array, 0, limit, 3) != 2
0386: || UTF16.findOffsetFromCodePoint(array, 0, limit, 3) != 4) {
0387: errln("FAIL Getting the last codepoint offset to a string with "
0388: + "supplementary characters");
0389: }
0390: if (UTF16.findCodePointOffset(str, 4) != 3
0391: || UTF16.findCodePointOffset(strbuff, 4) != 3
0392: || UTF16.findCodePointOffset(array, 0, limit, 4) != 3) {
0393: errln("FAIL Getting the length offset to a string with "
0394: + "supplementary characters");
0395: }
0396: try {
0397: UTF16.findCodePointOffset(str, 5);
0398: errln("FAIL Getting the a non-existence codepoint to a string "
0399: + "with supplementary characters");
0400: } catch (Exception e) {
0401: // this is a success
0402: logln("Passed out of bounds codepoint offset");
0403: }
0404: try {
0405: UTF16.findOffsetFromCodePoint(str, 4);
0406: errln("FAIL Getting the a non-existence codepoint to a string "
0407: + "with supplementary characters");
0408: } catch (Exception e) {
0409: // this is a success
0410: logln("Passed out of bounds codepoint offset");
0411: }
0412: try {
0413: UTF16.findCodePointOffset(strbuff, 5);
0414: errln("FAIL Getting the a non-existence codepoint to a string "
0415: + "with supplementary characters");
0416: } catch (Exception e) {
0417: // this is a success
0418: logln("Passed out of bounds codepoint offset");
0419: }
0420: try {
0421: UTF16.findOffsetFromCodePoint(strbuff, 4);
0422: errln("FAIL Getting the a non-existence codepoint to a string "
0423: + "with supplementary characters");
0424: } catch (Exception e) {
0425: // this is a success
0426: logln("Passed out of bounds codepoint offset");
0427: }
0428: try {
0429: UTF16.findCodePointOffset(array, 0, limit, 5);
0430: errln("FAIL Getting the a non-existence codepoint to a string "
0431: + "with supplementary characters");
0432: } catch (Exception e) {
0433: // this is a success
0434: logln("Passed out of bounds codepoint offset");
0435: }
0436: try {
0437: UTF16.findOffsetFromCodePoint(array, 0, limit, 4);
0438: errln("FAIL Getting the a non-existence codepoint to a string "
0439: + "with supplementary characters");
0440: } catch (Exception e) {
0441: // this is a success
0442: logln("Passed out of bounds codepoint offset");
0443: }
0444:
0445: if (UTF16.findCodePointOffset(array, 1, 3, 0) != 0
0446: || UTF16.findOffsetFromCodePoint(array, 1, 3, 0) != 0
0447: || UTF16.findCodePointOffset(array, 1, 3, 1) != 0
0448: || UTF16.findCodePointOffset(array, 1, 3, 2) != 1
0449: || UTF16.findOffsetFromCodePoint(array, 1, 3, 1) != 2) {
0450: errln("FAIL Getting valid codepoint offset in sub array");
0451: }
0452: }
0453:
0454: /**
0455: * Testing UTF16 class methods getCharCount, *Surrogate
0456: */
0457: public void TestGetCharCountSurrogate() {
0458: if (UTF16.getCharCount(0x61) != 1
0459: || UTF16.getCharCount(0x10000) != 2) {
0460: errln("FAIL getCharCount result failure");
0461: }
0462: if (UTF16.getLeadSurrogate(0x61) != 0
0463: || UTF16.getTrailSurrogate(0x61) != 0x61
0464: || UTF16.isLeadSurrogate((char) 0x61)
0465: || UTF16.isTrailSurrogate((char) 0x61)
0466: || UTF16.getLeadSurrogate(0x10000) != 0xd800
0467: || UTF16.getTrailSurrogate(0x10000) != 0xdc00
0468: || UTF16.isLeadSurrogate((char) 0xd800) != true
0469: || UTF16.isTrailSurrogate((char) 0xd800)
0470: || UTF16.isLeadSurrogate((char) 0xdc00)
0471: || UTF16.isTrailSurrogate((char) 0xdc00) != true) {
0472: errln("FAIL *Surrogate result failure");
0473: }
0474:
0475: if (UTF16.isSurrogate((char) 0x61)
0476: || !UTF16.isSurrogate((char) 0xd800)
0477: || !UTF16.isSurrogate((char) 0xdc00)) {
0478: errln("FAIL isSurrogate result failure");
0479: }
0480: }
0481:
0482: /**
0483: * Testing UTF16 class method insert
0484: */
0485: public void TestInsert() {
0486: StringBuffer strbuff = new StringBuffer("0123456789");
0487: char array[] = new char[128];
0488: Utility.getChars(strbuff, 0, strbuff.length(), array, 0);
0489: int length = 10;
0490: UTF16.insert(strbuff, 5, 't');
0491: UTF16.insert(strbuff, 5, 's');
0492: UTF16.insert(strbuff, 5, 'e');
0493: UTF16.insert(strbuff, 5, 't');
0494: if (!(strbuff.toString().equals("01234test56789"))) {
0495: errln("FAIL inserting \"test\"");
0496: }
0497: length = UTF16.insert(array, length, 5, 't');
0498: length = UTF16.insert(array, length, 5, 's');
0499: length = UTF16.insert(array, length, 5, 'e');
0500: length = UTF16.insert(array, length, 5, 't');
0501: String str = new String(array, 0, length);
0502: if (!(str.equals("01234test56789"))) {
0503: errln("FAIL inserting \"test\"");
0504: }
0505: UTF16.insert(strbuff, 0, 0x10000);
0506: UTF16.insert(strbuff, 11, 0x10000);
0507: UTF16.insert(strbuff, strbuff.length(), 0x10000);
0508: if (!(strbuff.toString()
0509: .equals("\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) {
0510: errln("FAIL inserting supplementary characters");
0511: }
0512: length = UTF16.insert(array, length, 0, 0x10000);
0513: length = UTF16.insert(array, length, 11, 0x10000);
0514: length = UTF16.insert(array, length, length, 0x10000);
0515: str = new String(array, 0, length);
0516: if (!(str
0517: .equals("\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) {
0518: errln("FAIL inserting supplementary characters");
0519: }
0520:
0521: try {
0522: UTF16.insert(strbuff, -1, 0);
0523: errln("FAIL invalid insertion offset");
0524: } catch (Exception e) {
0525: System.out.print("");
0526: }
0527: try {
0528: UTF16.insert(strbuff, 64, 0);
0529: errln("FAIL invalid insertion offset");
0530: } catch (Exception e) {
0531: System.out.print("");
0532: }
0533: try {
0534: UTF16.insert(array, length, -1, 0);
0535: errln("FAIL invalid insertion offset");
0536: } catch (Exception e) {
0537: System.out.print("");
0538: }
0539: try {
0540: UTF16.insert(array, length, 64, 0);
0541: errln("FAIL invalid insertion offset");
0542: } catch (Exception e) {
0543: System.out.print("");
0544: }
0545: try {
0546: // exceeded array size
0547: UTF16.insert(array, array.length, 64, 0);
0548: errln("FAIL invalid insertion offset");
0549: } catch (Exception e) {
0550: System.out.print("");
0551: }
0552: }
0553:
0554: /*
0555: * Testing moveCodePointOffset APIs
0556: */
0557:
0558: //
0559: // checkMoveCodePointOffset
0560: // Run a single test case through each of the moveCodePointOffset() functions.
0561: // Parameters -
0562: // s The string to work in.
0563: // startIdx The starting position within the string.
0564: // amount The number of code points to move.
0565: // expectedResult The string index after the move, or -1 if the
0566: // function should throw an exception.
0567: private void checkMoveCodePointOffset(String s, int startIdx,
0568: int amount, int expectedResult) {
0569: // Test with the String flavor of moveCodePointOffset
0570: try {
0571: int result = UTF16.moveCodePointOffset(s, startIdx, amount);
0572: if (result != expectedResult) {
0573: errln("FAIL: UTF16.moveCodePointOffset(String \""
0574: + s
0575: + "\", "
0576: + startIdx
0577: + ", "
0578: + amount
0579: + ")"
0580: + " returned "
0581: + result
0582: + ", expected result was "
0583: + (expectedResult == -1 ? "exception" : Integer
0584: .toString(expectedResult)));
0585: }
0586: } catch (IndexOutOfBoundsException e) {
0587: if (expectedResult != -1) {
0588: errln("FAIL: UTF16.moveCodePointOffset(String \"" + s
0589: + "\", " + startIdx + ", " + amount + ")"
0590: + " returned exception"
0591: + ", expected result was " + expectedResult);
0592: }
0593: }
0594:
0595: // Test with the StringBuffer flavor of moveCodePointOffset
0596: StringBuffer sb = new StringBuffer(s);
0597: try {
0598: int result = UTF16
0599: .moveCodePointOffset(sb, startIdx, amount);
0600: if (result != expectedResult) {
0601: errln("FAIL: UTF16.moveCodePointOffset(StringBuffer \""
0602: + s
0603: + "\", "
0604: + startIdx
0605: + ", "
0606: + amount
0607: + ")"
0608: + " returned "
0609: + result
0610: + ", expected result was "
0611: + (expectedResult == -1 ? "exception" : Integer
0612: .toString(expectedResult)));
0613: }
0614: } catch (IndexOutOfBoundsException e) {
0615: if (expectedResult != -1) {
0616: errln("FAIL: UTF16.moveCodePointOffset(StringBuffer \""
0617: + s + "\", " + startIdx + ", " + amount + ")"
0618: + " returned exception"
0619: + ", expected result was " + expectedResult);
0620: }
0621: }
0622:
0623: // Test with the char[] flavor of moveCodePointOffset
0624: char ca[] = s.toCharArray();
0625: try {
0626: int result = UTF16.moveCodePointOffset(ca, 0, s.length(),
0627: startIdx, amount);
0628: if (result != expectedResult) {
0629: errln("FAIL: UTF16.moveCodePointOffset(char[] \""
0630: + s
0631: + "\", 0, "
0632: + s.length()
0633: + ", "
0634: + startIdx
0635: + ", "
0636: + amount
0637: + ")"
0638: + " returned "
0639: + result
0640: + ", expected result was "
0641: + (expectedResult == -1 ? "exception" : Integer
0642: .toString(expectedResult)));
0643: }
0644: } catch (IndexOutOfBoundsException e) {
0645: if (expectedResult != -1) {
0646: errln("FAIL: UTF16.moveCodePointOffset(char[] \"" + s
0647: + "\", 0, " + s.length() + ", " + startIdx
0648: + ", " + amount + ")" + " returned exception"
0649: + ", expected result was " + expectedResult);
0650: }
0651: }
0652:
0653: // Put the test string into the interior of a char array,
0654: // run test on the subsection of the array.
0655: char ca2[] = new char[s.length() + 2];
0656: ca2[0] = (char) 0xd800;
0657: ca2[s.length() + 1] = (char) 0xd8ff;
0658: s.getChars(0, s.length(), ca2, 1);
0659: try {
0660: int result = UTF16.moveCodePointOffset(ca2, 1,
0661: s.length() + 1, startIdx, amount);
0662: if (result != expectedResult) {
0663: errln("UTF16.moveCodePointOffset(char[] \""
0664: + "."
0665: + s
0666: + ".\", 1, "
0667: + (s.length() + 1)
0668: + ", "
0669: + startIdx
0670: + ", "
0671: + amount
0672: + ")"
0673: + " returned "
0674: + result
0675: + ", expected result was "
0676: + (expectedResult == -1 ? "exception" : Integer
0677: .toString(expectedResult)));
0678: }
0679: } catch (IndexOutOfBoundsException e) {
0680: if (expectedResult != -1) {
0681: errln("UTF16.moveCodePointOffset(char[] \"" + "." + s
0682: + ".\", 1, " + (s.length() + 1) + ", "
0683: + startIdx + ", " + amount + ")"
0684: + " returned exception"
0685: + ", expected result was " + expectedResult);
0686: }
0687: }
0688:
0689: }
0690:
0691: public void TestMoveCodePointOffset() {
0692: // checkMoveCodePointOffset(String, startIndex, amount, expected ); expected=-1 for exception.
0693:
0694: // No Supplementary chars
0695: checkMoveCodePointOffset("abc", 1, 1, 2);
0696: checkMoveCodePointOffset("abc", 1, -1, 0);
0697: checkMoveCodePointOffset("abc", 1, -2, -1);
0698: checkMoveCodePointOffset("abc", 1, 2, 3);
0699: checkMoveCodePointOffset("abc", 1, 3, -1);
0700: checkMoveCodePointOffset("abc", 1, 0, 1);
0701:
0702: checkMoveCodePointOffset("abc", 3, 0, 3);
0703: checkMoveCodePointOffset("abc", 4, 0, -1);
0704: checkMoveCodePointOffset("abc", 0, 0, 0);
0705: checkMoveCodePointOffset("abc", -1, 0, -1);
0706:
0707: checkMoveCodePointOffset("", 0, 0, 0);
0708: checkMoveCodePointOffset("", 0, -1, -1);
0709: checkMoveCodePointOffset("", 0, 1, -1);
0710:
0711: checkMoveCodePointOffset("a", 0, 0, 0);
0712: checkMoveCodePointOffset("a", 1, 0, 1);
0713: checkMoveCodePointOffset("a", 0, 1, 1);
0714: checkMoveCodePointOffset("a", 1, -1, 0);
0715:
0716: // Supplementary in middle of string
0717: checkMoveCodePointOffset("a\ud800\udc00b", 0, 1, 1);
0718: checkMoveCodePointOffset("a\ud800\udc00b", 0, 2, 3);
0719: checkMoveCodePointOffset("a\ud800\udc00b", 0, 3, 4);
0720: checkMoveCodePointOffset("a\ud800\udc00b", 0, 4, -1);
0721:
0722: checkMoveCodePointOffset("a\ud800\udc00b", 4, -1, 3);
0723: checkMoveCodePointOffset("a\ud800\udc00b", 4, -2, 1);
0724: checkMoveCodePointOffset("a\ud800\udc00b", 4, -3, 0);
0725: checkMoveCodePointOffset("a\ud800\udc00b", 4, -4, -1);
0726:
0727: // Supplementary at start of string
0728: checkMoveCodePointOffset("\ud800\udc00ab", 0, 1, 2);
0729: checkMoveCodePointOffset("\ud800\udc00ab", 1, 1, 2);
0730: checkMoveCodePointOffset("\ud800\udc00ab", 2, 1, 3);
0731: checkMoveCodePointOffset("\ud800\udc00ab", 2, -1, 0);
0732: checkMoveCodePointOffset("\ud800\udc00ab", 1, -1, 0);
0733: checkMoveCodePointOffset("\ud800\udc00ab", 0, -1, -1);
0734:
0735: // Supplementary at end of string
0736: checkMoveCodePointOffset("ab\ud800\udc00", 1, 1, 2);
0737: checkMoveCodePointOffset("ab\ud800\udc00", 2, 1, 4);
0738: checkMoveCodePointOffset("ab\ud800\udc00", 3, 1, 4);
0739: checkMoveCodePointOffset("ab\ud800\udc00", 4, 1, -1);
0740:
0741: checkMoveCodePointOffset("ab\ud800\udc00", 5, -2, -1);
0742: checkMoveCodePointOffset("ab\ud800\udc00", 4, -1, 2);
0743: checkMoveCodePointOffset("ab\ud800\udc00", 3, -1, 2);
0744: checkMoveCodePointOffset("ab\ud800\udc00", 2, -1, 1);
0745: checkMoveCodePointOffset("ab\ud800\udc00", 1, -1, 0);
0746:
0747: // Unpaired surrogate in middle
0748: checkMoveCodePointOffset("a\ud800b", 0, 1, 1);
0749: checkMoveCodePointOffset("a\ud800b", 1, 1, 2);
0750: checkMoveCodePointOffset("a\ud800b", 2, 1, 3);
0751:
0752: checkMoveCodePointOffset("a\udc00b", 0, 1, 1);
0753: checkMoveCodePointOffset("a\udc00b", 1, 1, 2);
0754: checkMoveCodePointOffset("a\udc00b", 2, 1, 3);
0755:
0756: checkMoveCodePointOffset("a\udc00\ud800b", 0, 1, 1);
0757: checkMoveCodePointOffset("a\udc00\ud800b", 1, 1, 2);
0758: checkMoveCodePointOffset("a\udc00\ud800b", 2, 1, 3);
0759: checkMoveCodePointOffset("a\udc00\ud800b", 3, 1, 4);
0760:
0761: checkMoveCodePointOffset("a\ud800b", 1, -1, 0);
0762: checkMoveCodePointOffset("a\ud800b", 2, -1, 1);
0763: checkMoveCodePointOffset("a\ud800b", 3, -1, 2);
0764:
0765: checkMoveCodePointOffset("a\udc00b", 1, -1, 0);
0766: checkMoveCodePointOffset("a\udc00b", 2, -1, 1);
0767: checkMoveCodePointOffset("a\udc00b", 3, -1, 2);
0768:
0769: checkMoveCodePointOffset("a\udc00\ud800b", 1, -1, 0);
0770: checkMoveCodePointOffset("a\udc00\ud800b", 2, -1, 1);
0771: checkMoveCodePointOffset("a\udc00\ud800b", 3, -1, 2);
0772: checkMoveCodePointOffset("a\udc00\ud800b", 4, -1, 3);
0773:
0774: // Unpaired surrogate at start
0775: checkMoveCodePointOffset("\udc00ab", 0, 1, 1);
0776: checkMoveCodePointOffset("\ud800ab", 0, 2, 2);
0777: checkMoveCodePointOffset("\ud800\ud800ab", 0, 3, 3);
0778: checkMoveCodePointOffset("\udc00\udc00ab", 0, 4, 4);
0779:
0780: checkMoveCodePointOffset("\udc00ab", 2, -1, 1);
0781: checkMoveCodePointOffset("\ud800ab", 1, -1, 0);
0782: checkMoveCodePointOffset("\ud800ab", 1, -2, -1);
0783: checkMoveCodePointOffset("\ud800\ud800ab", 2, -1, 1);
0784: checkMoveCodePointOffset("\udc00\udc00ab", 2, -2, 0);
0785: checkMoveCodePointOffset("\udc00\udc00ab", 2, -3, -1);
0786:
0787: // Unpaired surrogate at end
0788: checkMoveCodePointOffset("ab\udc00\udc00ab", 3, 1, 4);
0789: checkMoveCodePointOffset("ab\udc00\udc00ab", 2, 1, 3);
0790: checkMoveCodePointOffset("ab\udc00\udc00ab", 1, 1, 2);
0791:
0792: checkMoveCodePointOffset("ab\udc00\udc00ab", 4, -1, 3);
0793: checkMoveCodePointOffset("ab\udc00\udc00ab", 3, -1, 2);
0794: checkMoveCodePointOffset("ab\udc00\udc00ab", 2, -1, 1);
0795:
0796: //01234567890 1 2 3 45678901234
0797: String str = new String(
0798: "0123456789\ud800\udc00\ud801\udc010123456789");
0799: int move1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12, 14, 14,
0800: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
0801: int move2[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 14, 15, 15,
0802: 16, 17, 18, 19, 20, 21, 22, 23, 24, -1 };
0803: int move3[] = { 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 15, 16,
0804: 16, 17, 18, 19, 20, 21, 22, 23, 24, -1, -1 };
0805: int size = str.length();
0806: for (int i = 0; i < size; i++) {
0807: checkMoveCodePointOffset(str, i, 1, move1[i]);
0808: checkMoveCodePointOffset(str, i, 2, move2[i]);
0809: checkMoveCodePointOffset(str, i, 3, move3[i]);
0810: }
0811:
0812: char strarray[] = str.toCharArray();
0813: if (UTF16.moveCodePointOffset(strarray, 9, 13, 0, 2) != 3) {
0814: errln("FAIL: Moving offset 0 by 2 codepoint in subarray [9, 13] "
0815: + "expected result 3");
0816: }
0817: if (UTF16.moveCodePointOffset(strarray, 9, 13, 1, 2) != 4) {
0818: errln("FAIL: Moving offset 1 by 2 codepoint in subarray [9, 13] "
0819: + "expected result 4");
0820: }
0821: if (UTF16.moveCodePointOffset(strarray, 11, 14, 0, 2) != 3) {
0822: errln("FAIL: Moving offset 0 by 2 codepoint in subarray [11, 14] "
0823: + "expected result 3");
0824: }
0825: }
0826:
0827: /**
0828: * Testing UTF16 class methods setCharAt
0829: */
0830: public void TestSetCharAt() {
0831: StringBuffer strbuff = new StringBuffer("012345");
0832: char array[] = new char[128];
0833: Utility.getChars(strbuff, 0, strbuff.length(), array, 0);
0834: int length = 6;
0835: for (int i = 0; i < length; i++) {
0836: UTF16.setCharAt(strbuff, i, '0');
0837: UTF16.setCharAt(array, length, i, '0');
0838: }
0839: String str = new String(array, 0, length);
0840: if (!(strbuff.toString().equals("000000"))
0841: || !(str.equals("000000"))) {
0842: errln("FAIL: setChar to '0' failed");
0843: }
0844: UTF16.setCharAt(strbuff, 0, 0x10000);
0845: UTF16.setCharAt(strbuff, 4, 0x10000);
0846: UTF16.setCharAt(strbuff, 7, 0x10000);
0847: if (!(strbuff.toString()
0848: .equals("\ud800\udc0000\ud800\udc000\ud800\udc00"))) {
0849: errln("FAIL: setChar to 0x10000 failed");
0850: }
0851: length = UTF16.setCharAt(array, length, 0, 0x10000);
0852: length = UTF16.setCharAt(array, length, 4, 0x10000);
0853: length = UTF16.setCharAt(array, length, 7, 0x10000);
0854: str = new String(array, 0, length);
0855: if (!(str.equals("\ud800\udc0000\ud800\udc000\ud800\udc00"))) {
0856: errln("FAIL: setChar to 0x10000 failed");
0857: }
0858: UTF16.setCharAt(strbuff, 0, '0');
0859: UTF16.setCharAt(strbuff, 1, '1');
0860: UTF16.setCharAt(strbuff, 2, '2');
0861: UTF16.setCharAt(strbuff, 4, '3');
0862: UTF16.setCharAt(strbuff, 4, '4');
0863: UTF16.setCharAt(strbuff, 5, '5');
0864: if (!strbuff.toString().equals("012345")) {
0865: errln("Fail converting supplementaries in StringBuffer to BMP "
0866: + "characters");
0867: }
0868: length = UTF16.setCharAt(array, length, 0, '0');
0869: length = UTF16.setCharAt(array, length, 1, '1');
0870: length = UTF16.setCharAt(array, length, 2, '2');
0871: length = UTF16.setCharAt(array, length, 4, '3');
0872: length = UTF16.setCharAt(array, length, 4, '4');
0873: length = UTF16.setCharAt(array, length, 5, '5');
0874: str = new String(array, 0, length);
0875: if (!str.equals("012345")) {
0876: errln("Fail converting supplementaries in array to BMP "
0877: + "characters");
0878: }
0879: try {
0880: UTF16.setCharAt(strbuff, -1, 0);
0881: errln("FAIL: setting character at invalid offset");
0882: } catch (Exception e) {
0883: System.out.print("");
0884: }
0885: try {
0886: UTF16.setCharAt(array, length, -1, 0);
0887: errln("FAIL: setting character at invalid offset");
0888: } catch (Exception e) {
0889: System.out.print("");
0890: }
0891: try {
0892: UTF16.setCharAt(strbuff, length, 0);
0893: errln("FAIL: setting character at invalid offset");
0894: } catch (Exception e) {
0895: System.out.print("");
0896: }
0897: try {
0898: UTF16.setCharAt(array, length, length, 0);
0899: errln("FAIL: setting character at invalid offset");
0900: } catch (Exception e) {
0901: System.out.print("");
0902: }
0903: }
0904:
0905: /**
0906: * Testing UTF16 valueof APIs
0907: */
0908: public void TestValueOf() {
0909: if (UCharacter.getCodePoint('\ud800', '\udc00') != 0x10000) {
0910: errln("FAIL: getCodePoint('\ud800','\udc00')");
0911: }
0912: if (!UTF16.valueOf(0x61).equals("a")
0913: || !UTF16.valueOf(0x10000).equals("\ud800\udc00")) {
0914: errln("FAIL: valueof(char32)");
0915: }
0916: String str = new String("01234\ud800\udc0056789");
0917: StringBuffer strbuff = new StringBuffer(str);
0918: char array[] = str.toCharArray();
0919: int length = str.length();
0920:
0921: String expected[] = { "0", "1", "2", "3", "4", "\ud800\udc00",
0922: "\ud800\udc00", "5", "6", "7", "8", "9" };
0923: for (int i = 0; i < length; i++) {
0924: if (!UTF16.valueOf(str, i).equals(expected[i])
0925: || !UTF16.valueOf(strbuff, i).equals(expected[i])
0926: || !UTF16.valueOf(array, 0, length, i).equals(
0927: expected[i])) {
0928: errln("FAIL: valueOf() expected " + expected[i]);
0929: }
0930: }
0931: try {
0932: UTF16.valueOf(str, -1);
0933: errln("FAIL: out of bounds error expected");
0934: } catch (Exception e) {
0935: System.out.print("");
0936: }
0937: try {
0938: UTF16.valueOf(strbuff, -1);
0939: errln("FAIL: out of bounds error expected");
0940: } catch (Exception e) {
0941: System.out.print("");
0942: }
0943: try {
0944: UTF16.valueOf(array, 0, length, -1);
0945: errln("FAIL: out of bounds error expected");
0946: } catch (Exception e) {
0947: System.out.print("");
0948: }
0949: try {
0950: UTF16.valueOf(str, length);
0951: errln("FAIL: out of bounds error expected");
0952: } catch (Exception e) {
0953: System.out.print("");
0954: }
0955: try {
0956: UTF16.valueOf(strbuff, length);
0957: errln("FAIL: out of bounds error expected");
0958: } catch (Exception e) {
0959: System.out.print("");
0960: }
0961: try {
0962: UTF16.valueOf(array, 0, length, length);
0963: errln("FAIL: out of bounds error expected");
0964: } catch (Exception e) {
0965: System.out.print("");
0966: }
0967: if (!UTF16.valueOf(array, 6, length, 0).equals("\udc00")
0968: || !UTF16.valueOf(array, 0, 6, 5).equals("\ud800")) {
0969: errln("FAIL: error getting partial supplementary character");
0970: }
0971: try {
0972: UTF16.valueOf(array, 3, 5, -1);
0973: errln("FAIL: out of bounds error expected");
0974: } catch (Exception e) {
0975: System.out.print("");
0976: }
0977: try {
0978: UTF16.valueOf(array, 3, 5, 3);
0979: errln("FAIL: out of bounds error expected");
0980: } catch (Exception e) {
0981: System.out.print("");
0982: }
0983: }
0984:
0985: public void TestIndexOf() {
0986: //012345678901234567890123456789012345
0987: String test1 = "test test ttest tetest testesteststt";
0988: String test2 = "test";
0989: int testChar1 = 0x74;
0990: int testChar2 = 0x20402;
0991: // int testChar3 = 0xdc02;
0992: // int testChar4 = 0xd841;
0993: String test3 = "\ud841\udc02\u0071\udc02\ud841\u0071\ud841\udc02\u0071\u0072\ud841\udc02\u0071\ud841\udc02\u0071\udc02\ud841\u0073";
0994: String test4 = UCharacter.toString(testChar2);
0995:
0996: if (UTF16.indexOf(test1, test2) != 0
0997: || UTF16.indexOf(test1, test2, 0) != 0) {
0998: errln("indexOf failed: expected to find '" + test2
0999: + "' at position 0 in text '" + test1 + "'");
1000: }
1001: if (UTF16.indexOf(test1, testChar1) != 0
1002: || UTF16.indexOf(test1, testChar1, 0) != 0) {
1003: errln("indexOf failed: expected to find 0x"
1004: + Integer.toHexString(testChar1)
1005: + " at position 0 in text '" + test1 + "'");
1006: }
1007: if (UTF16.indexOf(test3, testChar2) != 0
1008: || UTF16.indexOf(test3, testChar2, 0) != 0) {
1009: errln("indexOf failed: expected to find 0x"
1010: + Integer.toHexString(testChar2)
1011: + " at position 0 in text '" + Utility.hex(test3)
1012: + "'");
1013: }
1014: String test5 = "\ud841\ud841\udc02";
1015: if (UTF16.indexOf(test5, testChar2) != 1
1016: || UTF16.indexOf(test5, testChar2, 0) != 1) {
1017: errln("indexOf failed: expected to find 0x"
1018: + Integer.toHexString(testChar2)
1019: + " at position 0 in text '" + Utility.hex(test3)
1020: + "'");
1021: }
1022: if (UTF16.lastIndexOf(test1, test2) != 29
1023: || UTF16.lastIndexOf(test1, test2, test1.length()) != 29) {
1024: errln("lastIndexOf failed: expected to find '" + test2
1025: + "' at position 29 in text '" + test1 + "'");
1026: }
1027: if (UTF16.lastIndexOf(test1, testChar1) != 35
1028: || UTF16.lastIndexOf(test1, testChar1, test1.length()) != 35) {
1029: errln("lastIndexOf failed: expected to find 0x"
1030: + Integer.toHexString(testChar1)
1031: + " at position 35 in text '" + test1 + "'");
1032: }
1033: if (UTF16.lastIndexOf(test3, testChar2) != 13
1034: || UTF16.lastIndexOf(test3, testChar2, test3.length()) != 13) {
1035: errln("indexOf failed: expected to find 0x"
1036: + Integer.toHexString(testChar2)
1037: + " at position 13 in text '" + Utility.hex(test3)
1038: + "'");
1039: }
1040: int occurrences = 0;
1041: for (int startPos = 0; startPos != -1
1042: && startPos < test1.length();) {
1043: startPos = UTF16.indexOf(test1, test2, startPos);
1044: if (startPos >= 0) {
1045: ++occurrences;
1046: startPos += 4;
1047: }
1048: }
1049: if (occurrences != 6) {
1050: errln("indexOf failed: expected to find 6 occurrences, found "
1051: + occurrences);
1052: }
1053:
1054: occurrences = 0;
1055: for (int startPos = 10; startPos != -1
1056: && startPos < test1.length();) {
1057: startPos = UTF16.indexOf(test1, test2, startPos);
1058: if (startPos >= 0) {
1059: ++occurrences;
1060: startPos += 4;
1061: }
1062: }
1063: if (occurrences != 4) {
1064: errln("indexOf with starting offset failed: expected to find 4 occurrences, found "
1065: + occurrences);
1066: }
1067:
1068: occurrences = 0;
1069: for (int startPos = 0; startPos != -1
1070: && startPos < test3.length();) {
1071: startPos = UTF16.indexOf(test3, test4, startPos);
1072: if (startPos != -1) {
1073: ++occurrences;
1074: startPos += 2;
1075: }
1076: }
1077: if (occurrences != 4) {
1078: errln("indexOf failed: expected to find 4 occurrences, found "
1079: + occurrences);
1080: }
1081:
1082: occurrences = 0;
1083: for (int startPos = 10; startPos != -1
1084: && startPos < test3.length();) {
1085: startPos = UTF16.indexOf(test3, test4, startPos);
1086: if (startPos != -1) {
1087: ++occurrences;
1088: startPos += 2;
1089: }
1090: }
1091: if (occurrences != 2) {
1092: errln("indexOf failed: expected to find 2 occurrences, found "
1093: + occurrences);
1094: }
1095:
1096: occurrences = 0;
1097: for (int startPos = 0; startPos != -1
1098: && startPos < test1.length();) {
1099: startPos = UTF16.indexOf(test1, testChar1, startPos);
1100: if (startPos != -1) {
1101: ++occurrences;
1102: startPos += 1;
1103: }
1104: }
1105: if (occurrences != 16) {
1106: errln("indexOf with character failed: expected to find 16 occurrences, found "
1107: + occurrences);
1108: }
1109:
1110: occurrences = 0;
1111: for (int startPos = 10; startPos != -1
1112: && startPos < test1.length();) {
1113: startPos = UTF16.indexOf(test1, testChar1, startPos);
1114: if (startPos != -1) {
1115: ++occurrences;
1116: startPos += 1;
1117: }
1118: }
1119: if (occurrences != 12) {
1120: errln("indexOf with character & start offset failed: expected to find 12 occurrences, found "
1121: + occurrences);
1122: }
1123:
1124: occurrences = 0;
1125: for (int startPos = 0; startPos != -1
1126: && startPos < test3.length();) {
1127: startPos = UTF16.indexOf(test3, testChar2, startPos);
1128: if (startPos != -1) {
1129: ++occurrences;
1130: startPos += 1;
1131: }
1132: }
1133: if (occurrences != 4) {
1134: errln("indexOf failed: expected to find 4 occurrences, found "
1135: + occurrences);
1136: }
1137:
1138: occurrences = 0;
1139: for (int startPos = 5; startPos != -1
1140: && startPos < test3.length();) {
1141: startPos = UTF16.indexOf(test3, testChar2, startPos);
1142: if (startPos != -1) {
1143: ++occurrences;
1144: startPos += 1;
1145: }
1146: }
1147: if (occurrences != 3) {
1148: errln("indexOf with character & start & end offsets failed: expected to find 2 occurrences, found "
1149: + occurrences);
1150: }
1151: occurrences = 0;
1152: for (int startPos = 32; startPos != -1;) {
1153: startPos = UTF16.lastIndexOf(test1, test2, startPos);
1154: if (startPos != -1) {
1155: ++occurrences;
1156: startPos -= 5;
1157: }
1158: }
1159: if (occurrences != 6) {
1160: errln("lastIndexOf with starting and ending offsets failed: expected to find 4 occurrences, found "
1161: + occurrences);
1162: }
1163: occurrences = 0;
1164: for (int startPos = 32; startPos != -1;) {
1165: startPos = UTF16.lastIndexOf(test1, testChar1, startPos);
1166: if (startPos != -1) {
1167: ++occurrences;
1168: startPos -= 5;
1169: }
1170: }
1171: if (occurrences != 7) {
1172: errln("lastIndexOf with character & start & end offsets failed: expected to find 11 occurrences, found "
1173: + occurrences);
1174: }
1175:
1176: //testing UChar32
1177: occurrences = 0;
1178: for (int startPos = test3.length(); startPos != -1;) {
1179: startPos = UTF16
1180: .lastIndexOf(test3, testChar2, startPos - 5);
1181: if (startPos != -1) {
1182: ++occurrences;
1183: }
1184: }
1185: if (occurrences != 3) {
1186: errln("lastIndexOf with character & start & end offsets failed: expected to find 3 occurrences, found "
1187: + occurrences);
1188: }
1189:
1190: // testing supplementary
1191: for (int i = 0; i < INDEXOF_SUPPLEMENTARY_CHAR_.length; i++) {
1192: int ch = INDEXOF_SUPPLEMENTARY_CHAR_[i];
1193: for (int j = 0; j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length; j++) {
1194: int index = 0;
1195: int expected = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j];
1196: if (j > 0) {
1197: index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j - 1] + 1;
1198: }
1199: if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch,
1200: index) != expected
1201: || UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_,
1202: UCharacter.toString(ch), index) != expected) {
1203: errln("Failed finding index for supplementary 0x"
1204: + Integer.toHexString(ch));
1205: }
1206: index = INDEXOF_SUPPLEMENTARY_STRING_.length();
1207: if (j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length - 1) {
1208: index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j + 1] - 1;
1209: }
1210: if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_,
1211: ch, index) != expected
1212: || UTF16.lastIndexOf(
1213: INDEXOF_SUPPLEMENTARY_STRING_,
1214: UCharacter.toString(ch), index) != expected) {
1215: errln("Failed finding last index for supplementary 0x"
1216: + Integer.toHexString(ch));
1217: }
1218: }
1219: }
1220:
1221: for (int i = 0; i < INDEXOF_SUPPLEMENTARY_STR_INDEX_.length; i++) {
1222: int index = 0;
1223: int expected = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i];
1224: if (i > 0) {
1225: index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i - 1] + 1;
1226: }
1227: if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_,
1228: INDEXOF_SUPPLEMENTARY_STR_, index) != expected) {
1229: errln("Failed finding index for supplementary string "
1230: + hex(INDEXOF_SUPPLEMENTARY_STRING_));
1231: }
1232: index = INDEXOF_SUPPLEMENTARY_STRING_.length();
1233: if (i < INDEXOF_SUPPLEMENTARY_STR_INDEX_.length - 1) {
1234: index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i + 1] - 1;
1235: }
1236: if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_,
1237: INDEXOF_SUPPLEMENTARY_STR_, index) != expected) {
1238: errln("Failed finding last index for supplementary string "
1239: + hex(INDEXOF_SUPPLEMENTARY_STRING_));
1240: }
1241: }
1242: }
1243:
1244: public void TestReplace() {
1245: String test1 = "One potato, two potato, three potato, four\n";
1246: String test2 = "potato";
1247: String test3 = "MISSISSIPPI";
1248:
1249: String result = UTF16.replace(test1, test2, test3);
1250: String expectedValue = "One MISSISSIPPI, two MISSISSIPPI, three MISSISSIPPI, four\n";
1251: if (!result.equals(expectedValue)) {
1252: errln("findAndReplace failed: expected \"" + expectedValue
1253: + "\", got \"" + test1 + "\".");
1254: }
1255: result = UTF16.replace(test1, test3, test2);
1256: expectedValue = test1;
1257: if (!result.equals(expectedValue)) {
1258: errln("findAndReplace failed: expected \"" + expectedValue
1259: + "\", got \"" + test1 + "\".");
1260: }
1261:
1262: result = UTF16.replace(test1, ',', 'e');
1263: expectedValue = "One potatoe two potatoe three potatoe four\n";
1264: if (!result.equals(expectedValue)) {
1265: errln("findAndReplace failed: expected \"" + expectedValue
1266: + "\", got \"" + test1 + "\".");
1267: }
1268:
1269: result = UTF16.replace(test1, ',', 0x10000);
1270: expectedValue = "One potato\ud800\udc00 two potato\ud800\udc00 three potato\ud800\udc00 four\n";
1271: if (!result.equals(expectedValue)) {
1272: errln("findAndReplace failed: expected \"" + expectedValue
1273: + "\", got \"" + test1 + "\".");
1274: }
1275:
1276: result = UTF16.replace(test1, "potato",
1277: "\ud800\udc00\ud801\udc01");
1278: expectedValue = "One \ud800\udc00\ud801\udc01, two \ud800\udc00\ud801\udc01, three \ud800\udc00\ud801\udc01, four\n";
1279: if (!result.equals(expectedValue)) {
1280: errln("findAndReplace failed: expected \"" + expectedValue
1281: + "\", got \"" + test1 + "\".");
1282: }
1283:
1284: String test4 = "\ud800\ud800\udc00\ud800\udc00\udc00\ud800\ud800\udc00\ud800\udc00\udc00";
1285: result = UTF16.replace(test4, 0xd800, 'A');
1286: expectedValue = "A\ud800\udc00\ud800\udc00\udc00A\ud800\udc00\ud800\udc00\udc00";
1287: if (!result.equals(expectedValue)) {
1288: errln("findAndReplace failed: expected \"" + expectedValue
1289: + "\", got \"" + test1 + "\".");
1290: }
1291:
1292: result = UTF16.replace(test4, 0xdC00, 'A');
1293: expectedValue = "\ud800\ud800\udc00\ud800\udc00A\ud800\ud800\udc00\ud800\udc00A";
1294: if (!result.equals(expectedValue)) {
1295: errln("findAndReplace failed: expected \"" + expectedValue
1296: + "\", got \"" + test1 + "\".");
1297: }
1298:
1299: result = UTF16.replace(test4, 0x10000, 'A');
1300: expectedValue = "\ud800AA\udc00\ud800AA\udc00";
1301: if (!result.equals(expectedValue)) {
1302: errln("findAndReplace failed: expected \"" + expectedValue
1303: + "\", got \"" + test1 + "\".");
1304: }
1305: }
1306:
1307: public void TestReverse() {
1308: StringBuffer test = new StringBuffer(
1309: "backwards words say to used I");
1310:
1311: StringBuffer result = UTF16.reverse(test);
1312: if (!result.toString().equals("I desu ot yas sdrow sdrawkcab")) {
1313: errln("reverse() failed: Expected \"I desu ot yas sdrow sdrawkcab\",\n got \""
1314: + result + "\"");
1315: }
1316: StringBuffer testbuffer = new StringBuffer();
1317: UTF16.append(testbuffer, 0x2f999);
1318: UTF16.append(testbuffer, 0x1d15f);
1319: UTF16.append(testbuffer, 0x00c4);
1320: UTF16.append(testbuffer, 0x1ed0);
1321: result = UTF16.reverse(testbuffer);
1322: if (result.charAt(0) != 0x1ed0 || result.charAt(1) != 0xc4
1323: || UTF16.charAt(result, 2) != 0x1d15f
1324: || UTF16.charAt(result, 4) != 0x2f999) {
1325: errln("reverse() failed with supplementary characters");
1326: }
1327: }
1328:
1329: /**
1330: * Testing the setter and getter apis for StringComparator
1331: */
1332: public void TestStringComparator() {
1333: UTF16.StringComparator compare = new UTF16.StringComparator();
1334: if (compare.getCodePointCompare() != false) {
1335: errln("Default string comparator should be code unit compare");
1336: }
1337: if (compare.getIgnoreCase() != false) {
1338: errln("Default string comparator should be case sensitive compare");
1339: }
1340: if (compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
1341: errln("Default string comparator should have fold case default compare");
1342: }
1343: compare.setCodePointCompare(true);
1344: if (compare.getCodePointCompare() != true) {
1345: errln("Error setting code point compare");
1346: }
1347: compare.setCodePointCompare(false);
1348: if (compare.getCodePointCompare() != false) {
1349: errln("Error setting code point compare");
1350: }
1351: compare.setIgnoreCase(true,
1352: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1353: if (compare.getIgnoreCase() != true
1354: || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
1355: errln("Error setting ignore case and options");
1356: }
1357: compare.setIgnoreCase(false,
1358: UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
1359: if (compare.getIgnoreCase() != false
1360: || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) {
1361: errln("Error setting ignore case and options");
1362: }
1363: compare.setIgnoreCase(true,
1364: UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
1365: if (compare.getIgnoreCase() != true
1366: || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) {
1367: errln("Error setting ignore case and options");
1368: }
1369: compare.setIgnoreCase(false,
1370: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1371: if (compare.getIgnoreCase() != false
1372: || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
1373: errln("Error setting ignore case and options");
1374: }
1375: }
1376:
1377: public void TestCodePointCompare() {
1378: // these strings are in ascending order
1379: String str[] = { "\u0061", "\u20ac\ud801",
1380: "\u20ac\ud800\udc00", "\ud800", "\ud800\uff61",
1381: "\udfff", "\uff61\udfff", "\uff61\ud800\udc02",
1382: "\ud800\udc02", "\ud84d\udc56" };
1383: UTF16.StringComparator cpcompare = new UTF16.StringComparator(
1384: true, false, UTF16.StringComparator.FOLD_CASE_DEFAULT);
1385: UTF16.StringComparator cucompare = new UTF16.StringComparator();
1386: for (int i = 0; i < str.length - 1; ++i) {
1387: if (cpcompare.compare(str[i], str[i + 1]) >= 0) {
1388: errln("error: compare() in code point order fails for string "
1389: + Utility.hex(str[i])
1390: + " and "
1391: + Utility.hex(str[i + 1]));
1392: }
1393: // test code unit compare
1394: if (cucompare.compare(str[i], str[i + 1]) != str[i]
1395: .compareTo(str[i + 1])) {
1396: errln("error: compare() in code unit order fails for string "
1397: + Utility.hex(str[i])
1398: + " and "
1399: + Utility.hex(str[i + 1]));
1400: }
1401: }
1402: }
1403:
1404: public void TestCaseCompare() {
1405: String mixed = "\u0061\u0042\u0131\u03a3\u00df\ufb03\ud93f\udfff";
1406: String otherDefault = "\u0041\u0062\u0131\u03c3\u0073\u0053\u0046\u0066\u0049\ud93f\udfff";
1407: String otherExcludeSpecialI = "\u0041\u0062\u0131\u03c3\u0053\u0073\u0066\u0046\u0069\ud93f\udfff";
1408: String different = "\u0041\u0062\u0131\u03c3\u0073\u0053\u0046\u0066\u0049\ud93f\udffd";
1409:
1410: UTF16.StringComparator compare = new UTF16.StringComparator();
1411: compare.setIgnoreCase(true,
1412: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1413: // test u_strcasecmp()
1414: int result = compare.compare(mixed, otherDefault);
1415: if (result != 0) {
1416: errln("error: default compare(mixed, other) = " + result
1417: + " instead of 0");
1418: }
1419:
1420: // test u_strcasecmp() - exclude special i
1421: compare.setIgnoreCase(true,
1422: UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
1423: result = compare.compare(mixed, otherExcludeSpecialI);
1424: if (result != 0) {
1425: errln("error: exclude_i compare(mixed, other) = " + result
1426: + " instead of 0");
1427: }
1428:
1429: // test u_strcasecmp()
1430: compare.setIgnoreCase(true,
1431: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1432: result = compare.compare(mixed, different);
1433: if (result <= 0) {
1434: errln("error: default compare(mixed, different) = "
1435: + result + " instead of positive");
1436: }
1437:
1438: // test substrings - stop before the sharp s (U+00df)
1439: compare.setIgnoreCase(true,
1440: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1441: result = compare.compare(mixed.substring(0, 4), different
1442: .substring(0, 4));
1443: if (result != 0) {
1444: errln("error: default compare(mixed substring, different substring) = "
1445: + result + " instead of 0");
1446: }
1447: // test substrings - stop in the middle of the sharp s (U+00df)
1448: compare.setIgnoreCase(true,
1449: UTF16.StringComparator.FOLD_CASE_DEFAULT);
1450: result = compare.compare(mixed.substring(0, 5), different
1451: .substring(0, 5));
1452: if (result <= 0) {
1453: errln("error: default compare(mixed substring, different substring) = "
1454: + result + " instead of positive");
1455: }
1456: }
1457:
1458: public void TestHasMoreCodePointsThan() {
1459: String str = "\u0061\u0062\ud800\udc00\ud801\udc01\u0063\ud802\u0064"
1460: + "\udc03\u0065\u0066\ud804\udc04\ud805\udc05\u0067";
1461: int length = str.length();
1462: while (length >= 0) {
1463: for (int i = 0; i <= length; ++i) {
1464: String s = str.substring(0, i);
1465: for (int number = -1; number <= ((length - i) + 2); ++number) {
1466: boolean flag = UTF16.hasMoreCodePointsThan(s,
1467: number);
1468: if (flag != (UTF16.countCodePoint(s) > number)) {
1469: errln("hasMoreCodePointsThan(" + Utility.hex(s)
1470: + ", " + number + ") = " + flag
1471: + " is wrong");
1472: }
1473: }
1474: }
1475: --length;
1476: }
1477:
1478: // testing for null bad input
1479: for (length = -1; length <= 1; ++length) {
1480: for (int i = 0; i <= length; ++i) {
1481: for (int number = -2; number <= 2; ++number) {
1482: boolean flag = UTF16.hasMoreCodePointsThan(
1483: (String) null, number);
1484: if (flag != (UTF16.countCodePoint((String) null) > number)) {
1485: errln("hasMoreCodePointsThan(null, " + number
1486: + ") = " + flag + " is wrong");
1487: }
1488: }
1489: }
1490: }
1491:
1492: length = str.length();
1493: while (length >= 0) {
1494: for (int i = 0; i <= length; ++i) {
1495: StringBuffer s = new StringBuffer(str.substring(0, i));
1496: for (int number = -1; number <= ((length - i) + 2); ++number) {
1497: boolean flag = UTF16.hasMoreCodePointsThan(s,
1498: number);
1499: if (flag != (UTF16.countCodePoint(s) > number)) {
1500: errln("hasMoreCodePointsThan(" + Utility.hex(s)
1501: + ", " + number + ") = " + flag
1502: + " is wrong");
1503: }
1504: }
1505: }
1506: --length;
1507: }
1508:
1509: // testing for null bad input
1510: for (length = -1; length <= 1; ++length) {
1511: for (int i = 0; i <= length; ++i) {
1512: for (int number = -2; number <= 2; ++number) {
1513: boolean flag = UTF16.hasMoreCodePointsThan(
1514: (StringBuffer) null, number);
1515: if (flag != (UTF16
1516: .countCodePoint((StringBuffer) null) > number)) {
1517: errln("hasMoreCodePointsThan(null, " + number
1518: + ") = " + flag + " is wrong");
1519: }
1520: }
1521: }
1522: }
1523:
1524: char strarray[] = str.toCharArray();
1525: while (length >= 0) {
1526: for (int limit = 0; limit <= length; ++limit) {
1527: for (int start = 0; start <= limit; ++start) {
1528: for (int number = -1; number <= ((limit - start) + 2); ++number) {
1529: boolean flag = UTF16.hasMoreCodePointsThan(
1530: strarray, start, limit, number);
1531: if (flag != (UTF16.countCodePoint(strarray,
1532: start, limit) > number)) {
1533: errln("hasMoreCodePointsThan("
1534: + Utility.hex(str.substring(start,
1535: limit)) + ", " + start
1536: + ", " + limit + ", " + number
1537: + ") = " + flag + " is wrong");
1538: }
1539: }
1540: }
1541: }
1542: --length;
1543: }
1544:
1545: // testing for null bad input
1546: for (length = -1; length <= 1; ++length) {
1547: for (int i = 0; i <= length; ++i) {
1548: for (int number = -2; number <= 2; ++number) {
1549: boolean flag = UTF16.hasMoreCodePointsThan(
1550: (StringBuffer) null, number);
1551: if (flag != (UTF16
1552: .countCodePoint((StringBuffer) null) > number)) {
1553: errln("hasMoreCodePointsThan(null, " + number
1554: + ") = " + flag + " is wrong");
1555: }
1556: }
1557: }
1558: }
1559:
1560: // bad input
1561: try {
1562: UTF16.hasMoreCodePointsThan(strarray, -2, -1, 5);
1563: errln("hasMoreCodePointsThan(chararray) with negative indexes has to throw an exception");
1564: } catch (Exception e) {
1565: logln("PASS: UTF16.hasMoreCodePointsThan failed as expected");
1566: }
1567: try {
1568: UTF16.hasMoreCodePointsThan(strarray, 5, 2, 5);
1569: errln("hasMoreCodePointsThan(chararray) with limit less than start index has to throw an exception");
1570: } catch (Exception e) {
1571: logln("PASS: UTF16.hasMoreCodePointsThan failed as expected");
1572: }
1573: try {
1574: if (UTF16.hasMoreCodePointsThan(strarray, -2, 2, 5)) {
1575: errln("hasMoreCodePointsThan(chararray) with negative start indexes can't return true");
1576: }
1577: } catch (Exception e) {
1578: }
1579: }
1580:
1581: public void TestNewString() {
1582: final int[] codePoints = {
1583: UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
1584: UCharacter.MAX_LOW_SURROGATE),
1585: UCharacter.toCodePoint(UCharacter.MAX_HIGH_SURROGATE,
1586: UCharacter.MIN_LOW_SURROGATE),
1587: UCharacter.MAX_HIGH_SURROGATE, 'A', -1, };
1588:
1589: final String cpString = "" + UCharacter.MIN_HIGH_SURROGATE
1590: + UCharacter.MAX_LOW_SURROGATE
1591: + UCharacter.MAX_HIGH_SURROGATE
1592: + UCharacter.MIN_LOW_SURROGATE
1593: + UCharacter.MAX_HIGH_SURROGATE + 'A';
1594:
1595: final int[][] tests = { { 0, 1, 0, 2 }, { 0, 2, 0, 4 },
1596: { 1, 1, 2, 2 }, { 1, 2, 2, 3 }, { 1, 3, 2, 4 },
1597: { 2, 2, 4, 2 }, { 2, 3, 0, -1 }, { 4, 5, 0, -1 },
1598: { 3, -1, 0, -1 } };
1599:
1600: for (int i = 0; i < tests.length; ++i) {
1601: int[] t = tests[i];
1602: int s = t[0];
1603: int c = t[1];
1604: int rs = t[2];
1605: int rc = t[3];
1606:
1607: Exception e = null;
1608: try {
1609: String str = UTF16.newString(codePoints, s, c);
1610: if (rc == -1
1611: || !str.equals(cpString.substring(rs, rs + rc))) {
1612: errln("failed codePoints iter: " + i + " start: "
1613: + s + " len: " + c);
1614: }
1615: continue;
1616: } catch (IndexOutOfBoundsException e1) {
1617: e = e1;
1618: } catch (IllegalArgumentException e2) {
1619: e = e2;
1620: }
1621: if (rc != -1) {
1622: errln(e.getMessage());
1623: }
1624: }
1625: }
1626:
1627: public static void main(String[] arg) {
1628: try {
1629: UTF16Test test = new UTF16Test();
1630: test.run(arg);
1631: // test.TestCaseCompare();
1632: } catch (Exception e) {
1633: e.printStackTrace();
1634: }
1635: }
1636:
1637: // private data members ----------------------------------------------
1638:
1639: private final static String INDEXOF_SUPPLEMENTARY_STRING_ = "\ud841\udc02\u0071\udc02\ud841\u0071\ud841\udc02\u0071\u0072"
1640: + "\ud841\udc02\u0071\ud841\udc02\u0071\udc02\ud841\u0073";
1641: private final static int INDEXOF_SUPPLEMENTARY_CHAR_[] = { 0x71,
1642: 0xd841, 0xdc02,
1643: UTF16Util.getRawSupplementary((char) 0xd841, (char) 0xdc02) };
1644: private final static int INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[][] = {
1645: { 2, 5, 8, 12, 15 }, { 4, 17 }, { 3, 16 }, { 0, 6, 10, 13 } };
1646: private final static String INDEXOF_SUPPLEMENTARY_STR_ = "\udc02\ud841";
1647: private final static int INDEXOF_SUPPLEMENTARY_STR_INDEX_[] = { 3,
1648: 16 };
1649:
1650: // private methods ---------------------------------------------------
1651: }
|