• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /*
4  *******************************************************************************
5  * Copyright (C) 2006-2011, International Business Machines Corporation and    *
6  * others. All Rights Reserved.                                                *
7  *******************************************************************************
8  */
9 
10 package com.ibm.icu.tests;
11 
12 import java.text.CharacterIterator;
13 import java.text.StringCharacterIterator;
14 import java.util.Locale;
15 
16 import com.ibm.icu.text.BreakIterator;
17 import com.ibm.icu.util.ULocale;
18 
19 public class BreakIteratorTest extends ICUTestCase {
20     // ICU behaves a bit differently with this text, but the tested values aren't
21     // affected.  If Java changes behavior they might need to change.
22     private static final String text = "Mr. and Mrs. Mumblety-Peg paid $35.97 for a new 12\" cockatoo. " +
23         "When they got home they both cooed \"Isn't it lovely?\" and sighed softly. " +
24         "\"Let's name it u\u0308\u5098!\" they said with glee.";
25     private static int pos = text.indexOf("sn't");
26     private static BreakIterator cbr;
27     private static BreakIterator wbr;
28     private static BreakIterator lbr;
29     private static BreakIterator sbr;
30 
31     static {
32         cbr = BreakIterator.getCharacterInstance();
33         cbr.setText(text);
34         wbr = BreakIterator.getWordInstance();
35         wbr.setText(text);
36         lbr = BreakIterator.getLineInstance();
37         lbr.setText(text);
38         sbr = BreakIterator.getSentenceInstance();
39         sbr.setText(text);
40 
41         // diagnostic
42         //              dump(cbr);
43         //              dump(wbr);
44         //              dump(lbr);
45         //              dump(sbr);
46     }
47 
48     //      private static void dump(BreakIterator bi) {
49     //              for (int ix = bi.first(), lim = text.length(); ix != lim;) {
50     //                      int nx = bi.next();
51     //                      if (nx < 0) nx = lim;
52     //                      System.out.println(Integer.toString(ix) + ": " + text.substring(ix, nx));
53     //                      ix = nx;
54     //              }
55     //      }
56 
57     /*
58      * Test method for 'com.ibm.icu.text.BreakIterator.hashCode()'
59      */
testHashCode()60     public void testHashCode() {
61         BreakIterator br = BreakIterator.getWordInstance();
62         br.setText(text);
63         BreakIterator brne = BreakIterator.getWordInstance();
64         brne.setText(text + "X");
65         wbr.first();
66         testEHCS(br, wbr, brne);
67     }
68 
69     /*
70      * Test method for 'com.ibm.icu.text.BreakIterator.BreakIterator(BreakIterator)'
71      */
testBreakIterator()72     public void testBreakIterator() {
73         // implicitly tested everywhere
74     }
75 
76     /*
77      * Test method for 'com.ibm.icu.text.BreakIterator.first()'
78      */
testFirst()79     public void testFirst() {
80         assertEquals(0, cbr.first());
81         assertEquals(0, wbr.first());
82         assertEquals(0, lbr.first());
83         assertEquals(0, sbr.first());
84     }
85 
86     /*
87      * Test method for 'com.ibm.icu.text.BreakIterator.last()'
88      */
testLast()89     public void testLast() {
90         assertEquals(text.length(), cbr.last());
91         assertEquals(text.length(), wbr.last());
92         assertEquals(text.length(), lbr.last());
93         assertEquals(text.length(), sbr.last());
94     }
95 
96     /*
97      * Test method for 'com.ibm.icu.text.BreakIterator.next(int)'
98      */
testNextInt()99     public void testNextInt() {
100         cbr.first();
101         wbr.first();
102         lbr.first();
103         sbr.first();
104         assertEquals(2, cbr.next(2));
105         assertEquals(3, wbr.next(2));
106         assertEquals(8, lbr.next(2));
107         assertEquals(62, sbr.next(2));
108 
109         cbr.last();
110         wbr.last();
111         lbr.last();
112         sbr.last();
113         assertEquals(174, cbr.next(-2));
114         assertEquals(171, wbr.next(-2));
115         assertEquals(166, lbr.next(-2));
116         assertEquals(135, sbr.next(-2));
117     }
118 
119     /*
120      * Test method for 'com.ibm.icu.text.BreakIterator.next()'
121      */
testNext()122     public void testNext() {
123         cbr.first();
124         wbr.first();
125         lbr.first();
126         sbr.first();
127         assertEquals(1, cbr.next());
128         assertEquals(2, wbr.next());
129         assertEquals(4, lbr.next());
130         assertEquals(13, sbr.next());
131     }
132 
133     /*
134      * Test method for 'com.ibm.icu.text.BreakIterator.previous()'
135      */
testPrevious()136     public void testPrevious() {
137         cbr.last();
138         wbr.last();
139         lbr.last();
140         sbr.last();
141         assertEquals(175, cbr.previous());
142         assertEquals(175, wbr.previous());
143         assertEquals(171, lbr.previous());
144         assertEquals(156, sbr.previous());
145     }
146 
147     /*
148      * Test method for 'com.ibm.icu.text.BreakIterator.following(int)'
149      */
testFollowing()150     public void testFollowing() {
151         assertEquals(100, cbr.following(pos));
152         assertEquals(103, wbr.following(pos));
153         assertEquals(104, lbr.following(pos));
154         assertEquals(116, sbr.following(pos));
155     }
156 
157     /*
158      * Test method for 'com.ibm.icu.text.BreakIterator.preceding(int)'
159      */
testPreceding()160     public void testPreceding() {
161         assertEquals(98, cbr.preceding(pos));
162         assertEquals(98, wbr.preceding(pos));
163         assertEquals(97, lbr.preceding(pos));
164         assertEquals(62, sbr.preceding(pos));
165     }
166 
167     /*
168      * Test method for 'com.ibm.icu.text.BreakIterator.isBoundary(int)'
169      */
testIsBoundary()170     public void testIsBoundary() {
171         assertTrue(cbr.isBoundary(pos));
172         assertFalse(wbr.isBoundary(pos));
173         assertFalse(lbr.isBoundary(pos));
174         assertFalse(sbr.isBoundary(pos));
175 
176     }
177 
178     /*
179      * Test method for 'com.ibm.icu.text.BreakIterator.current()'
180      */
testCurrent()181     public void testCurrent() {
182         cbr.following(pos);
183         wbr.following(pos);
184         lbr.following(pos);
185         sbr.following(pos);
186         assertEquals(100, cbr.current());
187         assertEquals(103, wbr.current());
188         assertEquals(104, lbr.current());
189         assertEquals(116, sbr.current());
190     }
191 
192     /*
193      * Test method for 'com.ibm.icu.text.BreakIterator.getText()'
194      */
testGetText()195     public void testGetText() {
196         CharacterIterator ci = cbr.getText();
197         StringBuffer buf = new StringBuffer(ci.getEndIndex() - ci.getBeginIndex());
198         for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
199             buf.append(c);
200         }
201         String result = buf.toString();
202         assertEquals(text, result);
203     }
204 
205     /*
206      * Test method for 'com.ibm.icu.text.BreakIterator.setText(String)'
207      */
testSetTextString()208     public void testSetTextString() {
209         // implicitly tested
210     }
211 
212     /*
213      * Test method for 'com.ibm.icu.text.BreakIterator.setText(CharacterIterator)'
214      */
testSetTextCharacterIterator()215     public void testSetTextCharacterIterator() {
216         CharacterIterator ci = new StringCharacterIterator(text, pos);
217         BreakIterator bi = BreakIterator.getWordInstance();
218         bi.setText(ci);
219         assertEquals(2, bi.next());
220     }
221 
222     /*
223      * Test method for 'com.ibm.icu.text.BreakIterator.getWordInstance()'
224      */
testGetWordInstance()225     public void testGetWordInstance() {
226         // implicitly tested
227     }
228 
229     /*
230      * Test method for 'com.ibm.icu.text.BreakIterator.getWordInstance(Locale)'
231      */
testGetWordInstanceLocale()232     public void testGetWordInstanceLocale() {
233         assertNotNull(BreakIterator.getWordInstance(Locale.JAPAN));
234     }
235 
236     /*
237      * Test method for 'com.ibm.icu.text.BreakIterator.getWordInstance(ULocale)'
238      */
testGetWordInstanceULocale()239     public void testGetWordInstanceULocale() {
240         assertNotNull(BreakIterator.getWordInstance(ULocale.JAPAN));
241     }
242 
243     /*
244      * Test method for 'com.ibm.icu.text.BreakIterator.getLineInstance()'
245      */
testGetLineInstance()246     public void testGetLineInstance() {
247         // implicitly tested
248     }
249 
250     /*
251      * Test method for 'com.ibm.icu.text.BreakIterator.getLineInstance(Locale)'
252      */
testGetLineInstanceLocale()253     public void testGetLineInstanceLocale() {
254         assertNotNull(BreakIterator.getLineInstance(Locale.JAPAN));
255     }
256 
257     /*
258      * Test method for 'com.ibm.icu.text.BreakIterator.getLineInstance(ULocale)'
259      */
testGetLineInstanceULocale()260     public void testGetLineInstanceULocale() {
261         assertNotNull(BreakIterator.getLineInstance(ULocale.JAPAN));
262     }
263 
264     /*
265      * Test method for 'com.ibm.icu.text.BreakIterator.getCharacterInstance()'
266      */
testGetCharacterInstance()267     public void testGetCharacterInstance() {
268         // implicitly tested
269     }
270 
271     /*
272      * Test method for 'com.ibm.icu.text.BreakIterator.getCharacterInstance(Locale)'
273      */
testGetCharacterInstanceLocale()274     public void testGetCharacterInstanceLocale() {
275         assertNotNull(BreakIterator.getCharacterInstance(Locale.JAPAN));
276     }
277 
278     /*
279      * Test method for 'com.ibm.icu.text.BreakIterator.getCharacterInstance(ULocale)'
280      */
testGetCharacterInstanceULocale()281     public void testGetCharacterInstanceULocale() {
282         assertNotNull(BreakIterator.getCharacterInstance(ULocale.JAPAN));
283     }
284 
285     /*
286      * Test method for 'com.ibm.icu.text.BreakIterator.getSentenceInstance()'
287      */
testGetSentenceInstance()288     public void testGetSentenceInstance() {
289         // implicitly tested
290     }
291 
292     /*
293      * Test method for 'com.ibm.icu.text.BreakIterator.getSentenceInstance(Locale)'
294      */
testGetSentenceInstanceLocale()295     public void testGetSentenceInstanceLocale() {
296         assertNotNull(BreakIterator.getSentenceInstance(Locale.JAPAN));
297     }
298 
299     /*
300      * Test method for 'com.ibm.icu.text.BreakIterator.getSentenceInstance(ULocale)'
301      */
testGetSentenceInstanceULocale()302     public void testGetSentenceInstanceULocale() {
303         assertNotNull(BreakIterator.getSentenceInstance(ULocale.JAPAN));
304     }
305 
306     /*
307      * Test method for 'com.ibm.icu.text.BreakIterator.getTitleInstance()'
308      */
testGetTitleInstance()309     public void testGetTitleInstance() {
310         // not implemented
311     }
312 
313     /*
314      * Test method for 'com.ibm.icu.text.BreakIterator.getTitleInstance(Locale)'
315      */
testGetTitleInstanceLocale()316     public void testGetTitleInstanceLocale() {
317         // not implemented
318     }
319 
320     /*
321      * Test method for 'com.ibm.icu.text.BreakIterator.getTitleInstance(ULocale)'
322      */
testGetTitleInstanceULocale()323     public void testGetTitleInstanceULocale() {
324         // not implemented
325     }
326 
327     /*
328      * Test method for 'com.ibm.icu.text.BreakIterator.getAvailableLocales()'
329      */
testGetAvailableLocales()330     public void testGetAvailableLocales() {
331         assertNotNull(BreakIterator.getAvailableLocales());
332     }
333 
334     /*
335      * Test method for 'com.ibm.icu.text.BreakIterator.getAvailableULocales()'
336      */
testGetAvailableULocales()337     public void testGetAvailableULocales() {
338         assertNotNull(BreakIterator.getAvailableULocales());
339     }
340 
341     /*
342      * Test method for 'com.ibm.icu.text.BreakIterator.toString()'
343      */
testToString()344     public void testToString() {
345         assertNotNull(cbr.toString());
346     }
347 
348     /*
349      * Test method for 'com.ibm.icu.text.BreakIterator.clone()'
350      */
testClone()351     public void testClone() {
352         // see testHashCode
353     }
354 
355     /*
356      * Test method for 'com.ibm.icu.text.BreakIterator.equals(Object)'
357      */
testEqualsObject()358     public void testEqualsObject() {
359         // see testHashCode
360     }
361 }
362