001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v1.8.1 : rbbi : RBBIAPITest
010: * Source File: $ICU4CRoot/source/test/intltest/rbbiapts.cpp
011: **/package com.ibm.icu.dev.test.rbbi;
012:
013: import com.ibm.icu.text.BreakIterator;
014: import com.ibm.icu.text.RuleBasedBreakIterator;
015: import java.util.Locale;
016: import java.text.CharacterIterator;
017: import java.text.StringCharacterIterator;
018:
019: /**
020: * API Test the RuleBasedBreakIterator class
021: */
022: public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
023:
024: public static void main(String[] args) throws Exception {
025: new RBBIAPITest().run(args);
026: }
027:
028: /**
029: * Tests clone() and equals() methods of RuleBasedBreakIterator
030: **/
031: public void TestCloneEquals() {
032: RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator
033: .getCharacterInstance(Locale.getDefault());
034: RuleBasedBreakIterator biequal = (RuleBasedBreakIterator) BreakIterator
035: .getCharacterInstance(Locale.getDefault());
036: RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator
037: .getCharacterInstance(Locale.getDefault());
038: RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator
039: .getWordInstance(Locale.getDefault());
040:
041: String testString = "Testing word break iterators's clone() and equals()";
042: bi1.setText(testString);
043: bi2.setText(testString);
044: biequal.setText(testString);
045:
046: bi3.setText("hello");
047: logln("Testing equals()");
048: logln("Testing == and !=");
049: if (!bi1.equals(biequal) || bi1.equals(bi2) || bi1.equals(bi3))
050: errln("ERROR:1 RBBI's == and !- operator failed.");
051: if (bi2.equals(biequal) || bi2.equals(bi1)
052: || biequal.equals(bi3))
053: errln("ERROR:2 RBBI's == and != operator failed.");
054: logln("Testing clone()");
055: RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1
056: .clone();
057: RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2
058: .clone();
059: if (!bi1clone.equals(bi1) || !bi1clone.equals(biequal)
060: || bi1clone.equals(bi3) || bi1clone.equals(bi2))
061: errln("ERROR:1 RBBI's clone() method failed");
062:
063: if (bi2clone.equals(bi1) || bi2clone.equals(biequal)
064: || bi2clone.equals(bi3) || !bi2clone.equals(bi2))
065: errln("ERROR:2 RBBI's clone() method failed");
066:
067: if (!bi1.getText().equals(bi1clone.getText())
068: || !bi2clone.getText().equals(bi2.getText())
069: || bi2clone.equals(bi1clone))
070: errln("ERROR: RBBI's clone() method failed");
071: }
072:
073: /**
074: * Tests toString() method of RuleBasedBreakIterator
075: **/
076: public void TestToString() {
077: RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator
078: .getCharacterInstance(Locale.getDefault());
079: RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator
080: .getWordInstance(Locale.getDefault());
081: logln("Testing toString()");
082: bi1.setText("Hello there");
083: RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) bi1
084: .clone();
085: String temp = bi1.toString();
086: String temp2 = bi2.toString();
087: String temp3 = bi3.toString();
088: if (temp2.equals(temp3) || temp.equals(temp2)
089: || !temp.equals(temp3))
090: errln("ERROR: error in toString() method");
091: }
092:
093: /**
094: * Tests the method hashCode() of RuleBasedBreakIterator
095: **/
096: public void TestHashCode() {
097: RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator
098: .getCharacterInstance(Locale.getDefault());
099: RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator
100: .getCharacterInstance(Locale.getDefault());
101: RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator
102: .getWordInstance(Locale.getDefault());
103: logln("Testing hashCode()");
104: bi1.setText("Hash code");
105: bi2.setText("Hash code");
106: bi3.setText("Hash code");
107: RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1
108: .clone();
109: RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2
110: .clone();
111: if (bi1.hashCode() != bi1clone.hashCode()
112: || bi1.hashCode() != bi3.hashCode()
113: || bi1clone.hashCode() != bi3.hashCode()
114: || bi2.hashCode() != bi2clone.hashCode())
115: errln("ERROR: identical objects have different hashcodes");
116:
117: if (bi1.hashCode() == bi2.hashCode()
118: || bi2.hashCode() == bi3.hashCode()
119: || bi1clone.hashCode() == bi2clone.hashCode()
120: || bi1clone.hashCode() == bi2.hashCode())
121: errln("ERROR: different objects have same hashcodes");
122: }
123:
124: /**
125: * Tests the methods getText() and setText() of RuleBasedBreakIterator
126: **/
127: public void TestGetSetText() {
128: logln("Testing getText setText ");
129: String str1 = "first string.";
130: String str2 = "Second string.";
131: //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
132: RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator
133: .getWordInstance(Locale.getDefault());
134: CharacterIterator text1 = new StringCharacterIterator(str1);
135: //CharacterIterator text1Clone = (CharacterIterator) text1.clone();
136: //CharacterIterator text2 = new StringCharacterIterator(str2);
137: wordIter1.setText(str1);
138: if (!wordIter1.getText().equals(text1))
139: errln("ERROR:1 error in setText or getText ");
140: if (wordIter1.current() != 0)
141: errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is"
142: + wordIter1.current() + "\n");
143: wordIter1.next(2);
144: wordIter1.setText(str2);
145: if (wordIter1.current() != 0)
146: errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
147: + wordIter1.current() + "\n");
148: //ICU4J has remove the method adoptText
149: /*
150: charIter1.adoptText(text1Clone);
151: if (wordIter1.getText() == charIter1.getText()
152: || wordIter1.getText() != text2
153: || charIter1.getText() != text1)
154: errln((UnicodeString) "ERROR:2 error is getText or setText()");
155:
156: RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
157: rb.adoptText(text1);
158: if (rb.getText() != text1)
159: errln((UnicodeString) "ERROR:1 error in adoptText ");
160: rb.adoptText(text2);
161: if (rb.getText() != text2)
162: errln((UnicodeString) "ERROR:2 error in adoptText ");
163: */
164: }
165:
166: /**
167: * Testing the methods first(), next(), next(int) and following() of RuleBasedBreakIterator
168: **/
169: public void TestFirstNextFollowing() {
170: int p, q;
171: String testString = "This is a word break. Isn't it? 2.25";
172: logln("Testing first() and next(), following() with custom rules");
173: logln("testing word iterator - string :- \"" + testString
174: + "\"\n");
175: RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator
176: .getWordInstance(Locale.getDefault());
177: wordIter1.setText(testString);
178: p = wordIter1.first();
179: if (p != 0)
180: errln("ERROR: first() returned" + p + "instead of 0");
181: q = wordIter1.next(9);
182: doTest(testString, p, q, 20, "This is a word break");
183: p = q;
184: q = wordIter1.next();
185: doTest(testString, p, q, 21, ".");
186: p = q;
187: q = wordIter1.next(3);
188: doTest(testString, p, q, 28, " Isn't ");
189: p = q;
190: q = wordIter1.next(2);
191: doTest(testString, p, q, 31, "it?");
192: q = wordIter1.following(2);
193: doTest(testString, 2, q, 4, "is");
194: q = wordIter1.following(22);
195: doTest(testString, 22, q, 27, "Isn't");
196: wordIter1.last();
197: p = wordIter1.next();
198: q = wordIter1.following(wordIter1.last());
199: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
200: errln("ERROR: next()/following() at last position returned #"
201: + p
202: + " and "
203: + q
204: + " instead of"
205: + testString.length() + "\n");
206: RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator
207: .getCharacterInstance(Locale.getDefault());
208: testString = "Write hindi here. \u092d\u093e\u0930\u0301 \u0938\u0941\u0902\u0926\u0930 \u0939\u094c\u0964";
209: logln("testing char iter - string:- \"" + testString + "\"");
210: charIter1.setText(testString);
211: p = charIter1.first();
212: if (p != 0)
213: errln("ERROR: first() returned" + p + "instead of 0");
214: q = charIter1.next();
215: doTest(testString, p, q, 1, "W");
216: p = q;
217: q = charIter1.next(4);
218: doTest(testString, p, q, 5, "rite");
219: p = q;
220: q = charIter1.next(12);
221: doTest(testString, p, q, 17, " hindi here.");
222: p = q;
223: q = charIter1.next(-6);
224: doTest(testString, p, q, 11, " here.");
225: p = q;
226: q = charIter1.next(6);
227: doTest(testString, p, q, 17, " here.");
228: // hindi starts here
229: p = q;
230: q = charIter1.next(4);
231: doTest(testString, p, q, 22, " \u092d\u093e\u0930\u0301"); // Nonsense, but compatible between old and new rules.
232: p = q;
233: q = charIter1.next(2);
234: doTest(testString, p, q, 26, " \u0938\u0941\u0902");
235:
236: q = charIter1.following(24);
237: doTest(testString, 24, q, 26, "\u0941\u0902");
238: q = charIter1.following(20);
239: doTest(testString, 20, q, 22, "\u0930\u0301");
240: p = charIter1.following(charIter1.last());
241: q = charIter1.next(charIter1.last());
242: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
243: errln("ERROR: following()/next() at last position returned #"
244: + p
245: + " and "
246: + q
247: + " instead of"
248: + testString.length());
249: testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
250: RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator
251: .getSentenceInstance(Locale.getDefault());
252: logln("testing sentence iter - String:- \"" + testString + "\"");
253: sentIter1.setText(testString);
254: p = sentIter1.first();
255: if (p != 0)
256: errln("ERROR: first() returned" + p + "instead of 0");
257: q = sentIter1.next();
258: doTest(testString, p, q, 7, "Hello! ");
259: p = q;
260: q = sentIter1.next(2);
261: doTest(testString, p, q, 31, "how are you? I'am fine. ");
262: p = q;
263: q = sentIter1.next(-2);
264: doTest(testString, p, q, 7, "how are you? I'am fine. ");
265: p = q;
266: q = sentIter1.next(4);
267: doTest(testString, p, q, 60,
268: "how are you? I'am fine. Thankyou. How are you doing? ");
269: p = q;
270: q = sentIter1.next();
271: doTest(testString, p, q, 83, "This costs $20,00,000.");
272: q = sentIter1.following(1);
273: doTest(testString, 1, q, 7, "ello! ");
274: q = sentIter1.following(10);
275: doTest(testString, 10, q, 20, " are you? ");
276: q = sentIter1.following(20);
277: doTest(testString, 20, q, 31, "I'am fine. ");
278: p = sentIter1.following(sentIter1.last());
279: q = sentIter1.next(sentIter1.last());
280: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
281: errln("ERROR: following()/next() at last position returned #"
282: + p
283: + " and "
284: + q
285: + " instead of"
286: + testString.length());
287: testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000.";
288: logln("(UnicodeString)testing line iter - String:- \""
289: + testString + "\"");
290: RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator
291: .getLineInstance(Locale.getDefault());
292: lineIter1.setText(testString);
293: p = lineIter1.first();
294: if (p != 0)
295: errln("ERROR: first() returned" + p + "instead of 0");
296: q = lineIter1.next();
297: doTest(testString, p, q, 7, "Hello! ");
298: p = q;
299: p = q;
300: q = lineIter1.next(4);
301: doTest(testString, p, q, 20, "how\r\n (are)\r ");
302: p = q;
303: q = lineIter1.next(-4);
304: doTest(testString, p, q, 7, "how\r\n (are)\r ");
305: p = q;
306: q = lineIter1.next(6);
307: doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am ");
308: p = q;
309: q = lineIter1.next();
310: doTest(testString, p, q, 36, "fine- ");
311: p = q;
312: q = lineIter1.next(2);
313: doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar ");
314: q = lineIter1.following(60);
315: doTest(testString, 60, q, 64, "re, ");
316: q = lineIter1.following(1);
317: doTest(testString, 1, q, 7, "ello! ");
318: q = lineIter1.following(10);
319: doTest(testString, 10, q, 12, "\r\n");
320: q = lineIter1.following(20);
321: doTest(testString, 20, q, 25, "you? ");
322: p = lineIter1.following(lineIter1.last());
323: q = lineIter1.next(lineIter1.last());
324: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
325: errln("ERROR: following()/next() at last position returned #"
326: + p
327: + " and "
328: + q
329: + " instead of"
330: + testString.length());
331: }
332:
333: /**
334: * Testing the methods lastt(), previous(), and preceding() of RuleBasedBreakIterator
335: **/
336: public void TestLastPreviousPreceding() {
337: int p, q;
338: String testString = "This is a word break. Isn't it? 2.25 dollars";
339: logln("Testing last(),previous(), preceding() with custom rules");
340: logln("testing word iteration for string \"" + testString
341: + "\"");
342: RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator
343: .getWordInstance(Locale.getDefault());
344: wordIter1.setText(testString);
345: p = wordIter1.last();
346: if (p != testString.length()) {
347: errln("ERROR: first() returned" + p + "instead of"
348: + testString.length());
349: }
350: q = wordIter1.previous();
351: doTest(testString, p, q, 37, "dollars");
352: p = q;
353: q = wordIter1.previous();
354: doTest(testString, p, q, 36, " ");
355: q = wordIter1.preceding(25);
356: doTest(testString, 25, q, 22, "Isn");
357: p = q;
358: q = wordIter1.previous();
359: doTest(testString, p, q, 21, " ");
360: q = wordIter1.preceding(20);
361: doTest(testString, 20, q, 15, "break");
362: p = wordIter1.preceding(wordIter1.first());
363: if (p != BreakIterator.DONE)
364: errln("ERROR: preceding() at starting position returned #"
365: + p + " instead of 0");
366: testString = "Write hindi here. \u092d\u093e\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 \u0939\u0301\u0964";
367: logln("testing character iteration for string \" " + testString
368: + "\" \n");
369: RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator
370: .getCharacterInstance(Locale.getDefault());
371: charIter1.setText(testString);
372: p = charIter1.last();
373: if (p != testString.length())
374: errln("ERROR: first() returned" + p + "instead of"
375: + testString.length());
376: q = charIter1.previous();
377: doTest(testString, p, q, 31, "\u0964");
378: p = q;
379: q = charIter1.previous();
380: doTest(testString, p, q, 29, "\u0939\u0301");
381: q = charIter1.preceding(26);
382: doTest(testString, 26, q, 23, "\u0938\u0941\u0902");
383: q = charIter1.preceding(16);
384: doTest(testString, 16, q, 15, "e");
385: p = q;
386: q = charIter1.previous();
387: doTest(testString, p, q, 14, "r");
388: charIter1.first();
389: p = charIter1.previous();
390: q = charIter1.preceding(charIter1.first());
391: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
392: errln("ERROR: previous()/preceding() at starting position returned #"
393: + p + " and " + q + " instead of 0\n");
394: testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
395: logln("testing sentence iter - String:- \"" + testString + "\"");
396: RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator
397: .getSentenceInstance(Locale.getDefault());
398: sentIter1.setText(testString);
399: p = sentIter1.last();
400: if (p != testString.length())
401: errln("ERROR: last() returned" + p + "instead of "
402: + testString.length());
403: q = sentIter1.previous();
404: doTest(testString, p, q, 60, "This costs $20,00,000.");
405: p = q;
406: q = sentIter1.previous();
407: doTest(testString, p, q, 41, "How are you doing? ");
408: q = sentIter1.preceding(40);
409: doTest(testString, 40, q, 31, "Thankyou.");
410: q = sentIter1.preceding(25);
411: doTest(testString, 25, q, 20, "I'am ");
412: sentIter1.first();
413: p = sentIter1.previous();
414: q = sentIter1.preceding(sentIter1.first());
415: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
416: errln("ERROR: previous()/preceding() at starting position returned #"
417: + p + " and " + q + " instead of 0\n");
418: testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000.";
419: logln("testing line iter - String:- \"" + testString + "\"");
420: RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator
421: .getLineInstance(Locale.getDefault());
422: lineIter1.setText(testString);
423: p = lineIter1.last();
424: if (p != testString.length())
425: errln("ERROR: last() returned" + p + "instead of "
426: + testString.length());
427: q = lineIter1.previous();
428: doTest(testString, p, q, 72, "$20,00,000.");
429: p = q;
430: q = lineIter1.previous();
431: doTest(testString, p, q, 66, "costs ");
432: q = lineIter1.preceding(40);
433: doTest(testString, 40, q, 31, "Thankyou.");
434: q = lineIter1.preceding(25);
435: doTest(testString, 25, q, 20, "I'am ");
436: lineIter1.first();
437: p = lineIter1.previous();
438: q = lineIter1.preceding(sentIter1.first());
439: if (p != BreakIterator.DONE || q != BreakIterator.DONE)
440: errln("ERROR: previous()/preceding() at starting position returned #"
441: + p + " and " + q + " instead of 0\n");
442: }
443:
444: /**
445: * Tests the method IsBoundary() of RuleBasedBreakIterator
446: **/
447: public void TestIsBoundary() {
448: String testString1 = "Write here. \u092d\u0301\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 a\u0301u";
449: RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator
450: .getCharacterInstance(Locale.getDefault());
451: charIter1.setText(testString1);
452: int bounds1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14,
453: 15, 16, 17, 20, 21, 22, 23, 25, 26 };
454: doBoundaryTest(charIter1, testString1, bounds1);
455: RuleBasedBreakIterator wordIter2 = (RuleBasedBreakIterator) BreakIterator
456: .getWordInstance(Locale.getDefault());
457: wordIter2.setText(testString1);
458: int bounds2[] = { 0, 5, 6, 10, 11, 12, 16, 17, 22, 23, 26 };
459: doBoundaryTest(wordIter2, testString1, bounds2);
460: }
461:
462: //---------------------------------------------
463: //Internal subroutines
464: //---------------------------------------------
465:
466: /* Internal subroutine used by TestIsBoundary() */
467: public void doBoundaryTest(BreakIterator bi, String text,
468: int[] boundaries) {
469: logln("testIsBoundary():");
470: int p = 0;
471: boolean isB;
472: for (int i = 0; i < text.length(); i++) {
473: isB = bi.isBoundary(i);
474: logln("bi.isBoundary(" + i + ") -> " + isB);
475: if (i == boundaries[p]) {
476: if (!isB)
477: errln("Wrong result from isBoundary() for " + i
478: + ": expected true, got false");
479: p++;
480: } else {
481: if (isB)
482: errln("Wrong result from isBoundary() for " + i
483: + ": expected false, got true");
484: }
485: }
486: }
487:
488: /*Internal subroutine used for comparision of expected and acquired results */
489: public void doTest(String testString, int start, int gotoffset,
490: int expectedOffset, String expectedString) {
491: String selected;
492: String expected = expectedString;
493: if (gotoffset != expectedOffset)
494: errln("ERROR:****returned #" + gotoffset + " instead of #"
495: + expectedOffset);
496: if (start <= gotoffset) {
497: selected = testString.substring(start, gotoffset);
498: } else {
499: selected = testString.substring(gotoffset, start);
500: }
501: if (!selected.equals(expected))
502: errln("ERROR:****selected \"" + selected
503: + "\" instead of \"" + expected + "\"");
504: else
505: logln("****selected \"" + selected + "\"");
506: }
507: }
|