]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/rbbi/RBBIAPITest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / rbbi / RBBIAPITest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 /** 
9  * Port From:   ICU4C v1.8.1 : rbbi : RBBIAPITest
10  * Source File: $ICU4CRoot/source/test/intltest/rbbiapts.cpp
11  **/
12
13 package com.ibm.icu.dev.test.rbbi;
14
15 import java.text.CharacterIterator;
16 import java.text.StringCharacterIterator;
17 import java.util.Locale;
18
19 import com.ibm.icu.text.BreakIterator;
20 import com.ibm.icu.text.RuleBasedBreakIterator;
21
22 /**
23  * API Test the RuleBasedBreakIterator class
24  */
25 public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
26     
27     public static void main(String[] args) throws Exception {
28         new RBBIAPITest().run(args);
29     }
30     
31     /**
32      * Tests clone() and equals() methods of RuleBasedBreakIterator         
33      **/
34     public void TestCloneEquals() {
35         RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
36         RuleBasedBreakIterator biequal = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
37         RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
38         RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 
39
40         String testString = "Testing word break iterators's clone() and equals()";
41         bi1.setText(testString);
42         bi2.setText(testString);
43         biequal.setText(testString);
44         
45         bi3.setText("hello");
46         logln("Testing equals()");
47         logln("Testing == and !=");
48         if (!bi1.equals(biequal) || bi1.equals(bi2) || bi1.equals(bi3))
49             errln("ERROR:1 RBBI's == and !- operator failed.");
50         if (bi2.equals(biequal) || bi2.equals(bi1) || biequal.equals(bi3))
51             errln("ERROR:2 RBBI's == and != operator  failed.");
52         logln("Testing clone()");
53         RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
54         RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
55         if (!bi1clone.equals(bi1)
56             || !bi1clone.equals(biequal)
57             || bi1clone.equals(bi3)
58             || bi1clone.equals(bi2))
59             errln("ERROR:1 RBBI's clone() method failed");
60
61         if (bi2clone.equals(bi1)
62             || bi2clone.equals(biequal)
63             || bi2clone.equals(bi3)
64             || !bi2clone.equals(bi2))
65             errln("ERROR:2 RBBI's clone() method failed");
66
67         if (!bi1.getText().equals(bi1clone.getText())
68             || !bi2clone.getText().equals(bi2.getText())
69             || bi2clone.equals(bi1clone))
70             errln("ERROR: RBBI's clone() method failed");
71     }
72     
73     /**
74      * Tests toString() method of RuleBasedBreakIterator
75      **/
76     public void TestToString() {
77         RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
78         RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
79         logln("Testing toString()");
80         bi1.setText("Hello there");
81         RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) bi1.clone();
82         String temp = bi1.toString();
83         String temp2 = bi2.toString();
84         String temp3 = bi3.toString();
85         if (temp2.equals(temp3) || temp.equals(temp2) || !temp.equals(temp3))
86             errln("ERROR: error in toString() method");
87     }
88     
89     /**
90      * Tests the method hashCode() of RuleBasedBreakIterator
91      **/
92     public void TestHashCode() {
93         RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
94         RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
95         RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
96         logln("Testing hashCode()");
97         bi1.setText("Hash code");
98         bi2.setText("Hash code");
99         bi3.setText("Hash code");
100         RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
101         RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
102         if (bi1.hashCode() != bi1clone.hashCode()
103             || bi1.hashCode() != bi3.hashCode()
104             || bi1clone.hashCode() != bi3.hashCode()
105             || bi2.hashCode() != bi2clone.hashCode())
106             errln("ERROR: identical objects have different hashcodes");
107         
108         if (bi1.hashCode() == bi2.hashCode()
109             || bi2.hashCode() == bi3.hashCode()
110             || bi1clone.hashCode() == bi2clone.hashCode()
111             || bi1clone.hashCode() == bi2.hashCode())
112             errln("ERROR: different objects have same hashcodes");
113     }
114     
115     /**
116       * Tests the methods getText() and setText() of RuleBasedBreakIterator
117       **/
118     public void TestGetSetText() {
119         logln("Testing getText setText ");
120         String str1 = "first string.";
121         String str2 = "Second string.";
122         //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
123         RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 
124         CharacterIterator text1 = new StringCharacterIterator(str1);
125         //CharacterIterator text1Clone = (CharacterIterator) text1.clone();
126         //CharacterIterator text2 = new StringCharacterIterator(str2);
127         wordIter1.setText(str1);
128         if (!wordIter1.getText().equals(text1))
129             errln("ERROR:1 error in setText or getText ");
130         if (wordIter1.current() != 0)
131             errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is"
132                    + wordIter1.current() + "\n"); 
133         wordIter1.next(2);
134         wordIter1.setText(str2);
135         if (wordIter1.current() != 0)
136             errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
137                     + wordIter1.current() + "\n"); 
138         //ICU4J has remove the method adoptText
139         /*
140         charIter1.adoptText(text1Clone);
141         if (wordIter1.getText() == charIter1.getText()
142             || wordIter1.getText() != text2
143             || charIter1.getText() != text1)
144             errln((UnicodeString) "ERROR:2 error is getText or setText()");
145         
146         RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
147         rb.adoptText(text1);
148         if (rb.getText() != text1)
149             errln((UnicodeString) "ERROR:1 error in adoptText ");
150         rb.adoptText(text2);
151         if (rb.getText() != text2)
152             errln((UnicodeString) "ERROR:2 error in adoptText ");
153         */
154     }
155     
156     /**
157       * Testing the methods first(), next(), next(int) and following() of RuleBasedBreakIterator
158       *   TODO:  Most of this test should be retired, rule behavior is much better covered by
159       *          TestExtended, which is also easier to understand and maintain.
160       **/
161     public void TestFirstNextFollowing() {
162         int p, q;
163         String testString = "This is a word break. Isn't it? 2.25";
164         logln("Testing first() and next(), following() with custom rules");
165         logln("testing word iterator - string :- \"" + testString + "\"\n");
166         RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
167         wordIter1.setText(testString);
168         p = wordIter1.first();
169         if (p != 0)
170             errln("ERROR: first() returned" + p + "instead of 0");
171         q = wordIter1.next(9);
172         doTest(testString, p, q, 20, "This is a word break");
173         p = q;
174         q = wordIter1.next();
175         doTest(testString, p, q, 21, ".");
176         p = q;
177         q = wordIter1.next(3);
178         doTest(testString, p, q, 28, " Isn't ");
179         p = q;
180         q = wordIter1.next(2);
181         doTest(testString, p, q, 31, "it?");
182         q = wordIter1.following(2);
183         doTest(testString, 2, q, 4, "is");
184         q = wordIter1.following(22);
185         doTest(testString, 22, q, 27, "Isn't");
186         wordIter1.last();
187         p = wordIter1.next();
188         q = wordIter1.following(wordIter1.last());
189         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
190             errln("ERROR: next()/following() at last position returned #"
191                     + p + " and " + q + " instead of" + testString.length() + "\n"); 
192         RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 
193         testString = "Write hindi here. "; 
194         logln("testing char iter - string:- \"" + testString + "\"");
195         charIter1.setText(testString);
196         p = charIter1.first();
197         if (p != 0)
198             errln("ERROR: first() returned" + p + "instead of 0");
199         q = charIter1.next();
200         doTest(testString, p, q, 1, "W");
201         p = q;
202         q = charIter1.next(4);
203         doTest(testString, p, q, 5, "rite");
204         p = q;
205         q = charIter1.next(12);
206         doTest(testString, p, q, 17, " hindi here.");
207         p = q;
208         q = charIter1.next(-6);
209         doTest(testString, p, q, 11, " here.");
210         p = q;
211         q = charIter1.next(6);
212         doTest(testString, p, q, 17, " here.");
213         p = charIter1.following(charIter1.last());
214         q = charIter1.next(charIter1.last());
215         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
216             errln("ERROR: following()/next() at last position returned #"
217                     + p + " and " + q + " instead of" + testString.length()); 
218         testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This  costs $20,00,000."; 
219         RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault()); 
220         logln("testing sentence iter - String:- \"" + testString + "\"");
221         sentIter1.setText(testString);
222         p = sentIter1.first();
223         if (p != 0)
224             errln("ERROR: first() returned" + p + "instead of 0");
225         q = sentIter1.next();
226         doTest(testString, p, q, 7, "Hello! ");
227         p = q;
228         q = sentIter1.next(2);
229         doTest(testString, p, q, 31, "how are you? I'am fine. ");
230         p = q;
231         q = sentIter1.next(-2);
232         doTest(testString, p, q, 7, "how are you? I'am fine. ");
233         p = q;
234         q = sentIter1.next(4);
235         doTest(testString, p, q, 60, "how are you? I'am fine. Thankyou. How are you doing? ");
236         p = q;
237         q = sentIter1.next();
238         doTest(testString, p, q, 83, "This  costs $20,00,000.");
239         q = sentIter1.following(1);
240         doTest(testString, 1, q, 7, "ello! ");
241         q = sentIter1.following(10);
242         doTest(testString, 10, q, 20, " are you? ");
243         q = sentIter1.following(20);
244         doTest(testString, 20, q, 31, "I'am fine. ");
245         p = sentIter1.following(sentIter1.last());
246         q = sentIter1.next(sentIter1.last());
247         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
248             errln("ERROR: following()/next() at last position returned #"
249                     + p + " and " + q + " instead of" + testString.length()); 
250         testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000."; 
251         logln("(UnicodeString)testing line iter - String:- \"" + testString + "\"");
252         RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault()); 
253         lineIter1.setText(testString);
254         p = lineIter1.first();
255         if (p != 0)
256             errln("ERROR: first() returned" + p + "instead of 0");
257         q = lineIter1.next();
258         doTest(testString, p, q, 7, "Hello! ");
259         p = q;
260         p = q;
261         q = lineIter1.next(4);
262         doTest(testString, p, q, 20, "how\r\n (are)\r ");
263         p = q;
264         q = lineIter1.next(-4);
265         doTest(testString, p, q, 7, "how\r\n (are)\r ");
266         p = q;
267         q = lineIter1.next(6);
268         doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am ");
269         p = q;
270         q = lineIter1.next();
271         doTest(testString, p, q, 36, "fine- ");
272         p = q;
273         q = lineIter1.next(2);
274         doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar ");
275         q = lineIter1.following(60);
276         doTest(testString, 60, q, 64, "re, ");
277         q = lineIter1.following(1);
278         doTest(testString, 1, q, 7, "ello! ");
279         q = lineIter1.following(10);
280         doTest(testString, 10, q, 12, "\r\n");
281         q = lineIter1.following(20);
282         doTest(testString, 20, q, 25, "you? ");
283         p = lineIter1.following(lineIter1.last());
284         q = lineIter1.next(lineIter1.last());
285         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
286             errln("ERROR: following()/next() at last position returned #"
287                     + p + " and " + q + " instead of" + testString.length()); 
288     }
289     
290     /**
291      * Testing the methods lastt(), previous(), and preceding() of RuleBasedBreakIterator
292      **/
293     public void TestLastPreviousPreceding() {
294         int p, q;
295         String testString = "This is a word break. Isn't it? 2.25 dollars";
296         logln("Testing last(),previous(), preceding() with custom rules");
297         logln("testing word iteration for string \"" + testString + "\"");
298         RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 
299         wordIter1.setText(testString);
300         p = wordIter1.last();
301         if (p != testString.length()) {
302             errln("ERROR: first() returned" + p + "instead of" + testString.length());
303         }
304         q = wordIter1.previous();
305         doTest(testString, p, q, 37, "dollars");
306         p = q;
307         q = wordIter1.previous();
308         doTest(testString, p, q, 36, " ");
309         q = wordIter1.preceding(25);
310         doTest(testString, 25, q, 22, "Isn");
311         p = q;
312         q = wordIter1.previous();
313         doTest(testString, p, q, 21, " ");
314         q = wordIter1.preceding(20);
315         doTest(testString, 20, q, 15, "break");
316         p = wordIter1.preceding(wordIter1.first());
317         if (p != BreakIterator.DONE)
318             errln("ERROR: preceding()  at starting position returned #" + p + " instead of 0");
319         testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This  costs $20,00,000."; 
320         logln("testing sentence iter - String:- \"" + testString + "\"");
321         RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault()); 
322         sentIter1.setText(testString);
323         p = sentIter1.last();
324         if (p != testString.length())
325             errln("ERROR: last() returned" + p + "instead of " + testString.length());
326         q = sentIter1.previous();
327         doTest(testString, p, q, 60, "This  costs $20,00,000.");
328         p = q;
329         q = sentIter1.previous();
330         doTest(testString, p, q, 41, "How are you doing? ");
331         q = sentIter1.preceding(40);
332         doTest(testString, 40, q, 31, "Thankyou.");
333         q = sentIter1.preceding(25);
334         doTest(testString, 25, q, 20, "I'am ");
335         sentIter1.first();
336         p = sentIter1.previous();
337         q = sentIter1.preceding(sentIter1.first());
338         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
339             errln("ERROR: previous()/preceding() at starting position returned #"
340                     + p + " and " + q + " instead of 0\n"); 
341         testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000."; 
342         logln("testing line iter - String:- \"" + testString + "\"");
343         RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault());
344         lineIter1.setText(testString);
345         p = lineIter1.last();
346         if (p != testString.length())
347             errln("ERROR: last() returned" + p + "instead of " + testString.length());
348         q = lineIter1.previous();
349         doTest(testString, p, q, 72, "$20,00,000.");
350         p = q;
351         q = lineIter1.previous();
352         doTest(testString, p, q, 66, "costs ");
353         q = lineIter1.preceding(40);
354         doTest(testString, 40, q, 31, "Thankyou.");
355         q = lineIter1.preceding(25);
356         doTest(testString, 25, q, 20, "I'am ");
357         lineIter1.first();
358         p = lineIter1.previous();
359         q = lineIter1.preceding(sentIter1.first());
360         if (p != BreakIterator.DONE || q != BreakIterator.DONE)
361             errln("ERROR: previous()/preceding() at starting position returned #"
362                     + p + " and " + q + " instead of 0\n");
363     }
364     
365     /**
366      * Tests the method IsBoundary() of RuleBasedBreakIterator
367      **/
368     public void TestIsBoundary() {
369         String testString1 = "Write here. \u092d\u0301\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 a\u0301u";
370         RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
371         charIter1.setText(testString1);
372         int bounds1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 25, 26};
373         doBoundaryTest(charIter1, testString1, bounds1);
374         RuleBasedBreakIterator wordIter2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
375         wordIter2.setText(testString1);
376         int bounds2[] = {0, 5, 6, 10, 11, 12, 16, 17, 22, 23, 26};
377         doBoundaryTest(wordIter2, testString1, bounds2);
378     }
379     
380     //---------------------------------------------
381     //Internal subroutines
382     //---------------------------------------------
383     
384     /* Internal subroutine used by TestIsBoundary() */ 
385     public void doBoundaryTest(BreakIterator bi, String text, int[] boundaries) {
386         logln("testIsBoundary():");
387         int p = 0;
388         boolean isB;
389         for (int i = 0; i < text.length(); i++) {
390             isB = bi.isBoundary(i);
391             logln("bi.isBoundary(" + i + ") -> " + isB);
392             if (i == boundaries[p]) {
393                 if (!isB)
394                     errln("Wrong result from isBoundary() for " + i + ": expected true, got false");
395                 p++;
396             } else {
397                 if (isB)
398                     errln("Wrong result from isBoundary() for " + i + ": expected false, got true");
399             }
400         }
401     }
402     
403     /*Internal subroutine used for comparision of expected and acquired results */
404     public void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) {
405         String selected;
406         String expected = expectedString;
407         if (gotoffset != expectedOffset)
408             errln("ERROR:****returned #" + gotoffset + " instead of #" + expectedOffset);
409         if (start <= gotoffset) {
410             selected = testString.substring(start, gotoffset);
411         } else {
412             selected = testString.substring(gotoffset, start);
413         }
414         if (!selected.equals(expected))
415             errln("ERROR:****selected \"" + selected + "\" instead of \"" + expected + "\"");
416         else
417             logln("****selected \"" + selected + "\"");
418     }
419 }