1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.i18n;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.Vector;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.MultiMethodTest;
29cdf0e10cSrcweir import lib.Status;
30cdf0e10cSrcweir import lib.StatusException;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.i18n.Boundary;
33cdf0e10cSrcweir import com.sun.star.i18n.LineBreakHyphenationOptions;
34cdf0e10cSrcweir import com.sun.star.i18n.LineBreakResults;
35cdf0e10cSrcweir import com.sun.star.i18n.LineBreakUserOptions;
36cdf0e10cSrcweir import com.sun.star.i18n.ScriptType;
37cdf0e10cSrcweir import com.sun.star.i18n.WordType;
38cdf0e10cSrcweir import com.sun.star.i18n.XBreakIterator;
39cdf0e10cSrcweir import com.sun.star.lang.Locale;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /**
42cdf0e10cSrcweir * Testing <code>com.sun.star.i18n.XBreakIterator</code>
43cdf0e10cSrcweir * interface methods :
44cdf0e10cSrcweir * <ul>
45cdf0e10cSrcweir *  <li><code> nextCharacters()</code></li>
46cdf0e10cSrcweir *  <li><code> previousCharacters()</code></li>
47cdf0e10cSrcweir *  <li><code> nextWord()</code></li>
48cdf0e10cSrcweir *  <li><code> previousWord()</code></li>
49cdf0e10cSrcweir *  <li><code> getWordBoundary()</code></li>
50cdf0e10cSrcweir *  <li><code> getWordType()</code></li>
51cdf0e10cSrcweir *  <li><code> isBeginWord()</code></li>
52cdf0e10cSrcweir *  <li><code> isEndWord()</code></li>
53cdf0e10cSrcweir *  <li><code> beginOfSentence()</code></li>
54cdf0e10cSrcweir *  <li><code> endOfSentence()</code></li>
55cdf0e10cSrcweir *  <li><code> getLineBreak()</code></li>
56cdf0e10cSrcweir *  <li><code> beginOfScript()</code></li>
57cdf0e10cSrcweir *  <li><code> endOfScript()</code></li>
58cdf0e10cSrcweir *  <li><code> nextScript()</code></li>
59cdf0e10cSrcweir *  <li><code> previousScript()</code></li>
60cdf0e10cSrcweir *  <li><code> getScriptType()</code></li>
61cdf0e10cSrcweir *  <li><code> beginOfCharBlock()</code></li>
62cdf0e10cSrcweir *  <li><code> endOfCharBlock()</code></li>
63cdf0e10cSrcweir *  <li><code> nextCharBlock()</code></li>
64cdf0e10cSrcweir *  <li><code> previousCharBlock()</code></li>
65cdf0e10cSrcweir * </ul> <p>
66cdf0e10cSrcweir * This test needs the following object relations :
67cdf0e10cSrcweir * <ul>
68cdf0e10cSrcweir *  <li> <code>'Locale'</code>
69cdf0e10cSrcweir *   (of type <code>com.sun.star.lang.Locale</code>):
70cdf0e10cSrcweir *   this locale is used as locale argument for tested methods.
71cdf0e10cSrcweir *  </li>
72cdf0e10cSrcweir *  <li> <code>'UnicodeString'</code>
73cdf0e10cSrcweir *   (of type <code>String</code>): Unicode string which is passed
74cdf0e10cSrcweir *   to methods except 'CharacterBlock' methods.
75cdf0e10cSrcweir *  </li>
76cdf0e10cSrcweir * <ul> <p>
77cdf0e10cSrcweir * @see com.sun.star.i18n.XBreakIterator
78cdf0e10cSrcweir */
79cdf0e10cSrcweir public class _XBreakIterator extends MultiMethodTest {
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     public XBreakIterator oObj = null;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     Locale locale = null;
84cdf0e10cSrcweir     String UnicodeString = null;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     short wordType = WordType.ANYWORD_IGNOREWHITESPACES;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * Retrieves object relations.
90cdf0e10cSrcweir      * @throws StatusException If one of relations not found.
91cdf0e10cSrcweir      */
before()92cdf0e10cSrcweir     protected void before() {
93cdf0e10cSrcweir         locale = (Locale)tEnv.getObjRelation("Locale");
94cdf0e10cSrcweir         if (locale == null) {
95cdf0e10cSrcweir             throw new StatusException
96cdf0e10cSrcweir                 (Status.failed("Relation 'Locale' not found")) ;
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         UnicodeString = (String)tEnv.getObjRelation("UnicodeString");
100cdf0e10cSrcweir         if (UnicodeString == null) {
101cdf0e10cSrcweir             throw new StatusException(Status.failed
102cdf0e10cSrcweir                 ("Relation 'UnicodeString' not found")) ;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     /**
107cdf0e10cSrcweir      * Compares returned next character positions with expected values. <p>
108cdf0e10cSrcweir      *
109cdf0e10cSrcweir      * Has <b>OK</b> status if position after travel and traveled length
110cdf0e10cSrcweir      * has expected values.
111cdf0e10cSrcweir      */
_nextCharacters()112cdf0e10cSrcweir     public void _nextCharacters() {
113cdf0e10cSrcweir         short nCharacterIteratorMode =
114cdf0e10cSrcweir             com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         int strLength = UnicodeString.length();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         //Start from position : Travel ... chars :
119cdf0e10cSrcweir         // Actual position after : How many chars traveled
120cdf0e10cSrcweir         int[][] nextCharacters = {
121cdf0e10cSrcweir             { 1, 5000, strLength , strLength - 1 },
122cdf0e10cSrcweir             { 10, 6, 16, 6}};
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         boolean bRes = true;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         for(int i = 0; i < nextCharacters.length; i++) {
127cdf0e10cSrcweir             int[] lDone = new int[1];
128cdf0e10cSrcweir             long lRes = oObj.nextCharacters(UnicodeString, nextCharacters[i][0],
129cdf0e10cSrcweir                 locale, nCharacterIteratorMode, nextCharacters[i][1], lDone);
130cdf0e10cSrcweir             log.println("Expected result is: lRes = " + nextCharacters[i][2] +
131cdf0e10cSrcweir                         "; lDone = " + nextCharacters[i][3] );
132cdf0e10cSrcweir             log.println("Actual result is: lRes = " + lRes +
133cdf0e10cSrcweir                         "; lDone = " + lDone[0] );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             bRes = bRes && lRes == nextCharacters[i][2];
136cdf0e10cSrcweir             bRes = bRes && lDone[0] == nextCharacters[i][3];
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         tRes.tested("nextCharacters()", bRes);
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     /**
143cdf0e10cSrcweir      * Compares returned previous character positions with expected values. <p>
144cdf0e10cSrcweir      *
145cdf0e10cSrcweir      * Has <b>OK</b> status if position after travel and traveled length
146cdf0e10cSrcweir      * has expected values.
147cdf0e10cSrcweir      */
_previousCharacters()148cdf0e10cSrcweir     public void _previousCharacters() {
149cdf0e10cSrcweir         short nCharacterIteratorMode =
150cdf0e10cSrcweir             com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         //Start from position : Travel ... chars : Actual position after :
154cdf0e10cSrcweir         //How many chars traveled
155cdf0e10cSrcweir         int[][] previousCharacters = {
156cdf0e10cSrcweir             {5, 5000, 0, 5},
157cdf0e10cSrcweir             {10, 6, 4, 6}};
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         boolean bRes = true;
160cdf0e10cSrcweir         for(int i = 0; i < previousCharacters.length; i++) {
161cdf0e10cSrcweir             int[] lDone = new int[1];
162cdf0e10cSrcweir             int lRes = oObj.previousCharacters(UnicodeString,
163cdf0e10cSrcweir                 previousCharacters[i][0],
164cdf0e10cSrcweir                 locale, nCharacterIteratorMode,
165cdf0e10cSrcweir                 previousCharacters[i][1], lDone);
166cdf0e10cSrcweir             log.println("Expected result is: lRes = " + previousCharacters[i][2]
167cdf0e10cSrcweir                 + "; lDone = " + previousCharacters[i][3] );
168cdf0e10cSrcweir             log.println("Actual result is: lRes = " + lRes
169cdf0e10cSrcweir                 + "; lDone = " + lDone[0]);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             bRes = bRes && lRes == previousCharacters[i][2];
172cdf0e10cSrcweir             bRes = bRes && lDone[0] == previousCharacters[i][3];
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         tRes.tested("previousCharacters()", bRes);
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     Vector vBounds = new Vector();
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     /**
181cdf0e10cSrcweir     * Saves bounds of all returned words for the future tests. <p>
182cdf0e10cSrcweir     * Has <b>OK</b> status.
183cdf0e10cSrcweir     */
_nextWord()184cdf0e10cSrcweir     public void _nextWord() {
185cdf0e10cSrcweir         int i = 0;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         while( i < UnicodeString.length() - 1 ) {
188cdf0e10cSrcweir             Boundary bounds = oObj.nextWord
189cdf0e10cSrcweir                 (UnicodeString, i, locale, wordType);
190cdf0e10cSrcweir             if (bounds.endPos - bounds.startPos > 3) {
191cdf0e10cSrcweir                 vBounds.add( bounds );
192cdf0e10cSrcweir                 log.println("Word " + vBounds.size() + "("
193cdf0e10cSrcweir                     + bounds.startPos + "," + bounds.endPos + "): '" +
194cdf0e10cSrcweir                     UnicodeString.substring(bounds.startPos,
195cdf0e10cSrcweir                                             bounds.endPos) + "'");
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir             i = bounds.endPos - 1;
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir         log.println("In text there are " + vBounds.size()
200cdf0e10cSrcweir             + " words, if count from left to right");
201cdf0e10cSrcweir         tRes.tested("nextWord()", true);
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     /**
205cdf0e10cSrcweir     * Compares number of word bounds with number of word bounds saved
206cdf0e10cSrcweir     * by the method _nextWord().<p>
207cdf0e10cSrcweir     * Has <b>OK</b> status if number of word bounds are equal.
208cdf0e10cSrcweir     */
_previousWord()209cdf0e10cSrcweir     public void _previousWord() {
210cdf0e10cSrcweir         requiredMethod("nextWord()");
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         int i = UnicodeString.length() - 1;
213cdf0e10cSrcweir         Vector vPrevBounds = new Vector();
214cdf0e10cSrcweir         while( i > 0  ) {
215cdf0e10cSrcweir             Boundary bounds =
216cdf0e10cSrcweir                 oObj.previousWord(UnicodeString, i, locale, wordType);
217cdf0e10cSrcweir             if (bounds.endPos - bounds.startPos > 3) {
218cdf0e10cSrcweir                 vPrevBounds.add( bounds );
219cdf0e10cSrcweir                 log.println("Word " + vPrevBounds.size() + "("
220cdf0e10cSrcweir                     + bounds.startPos + "," + bounds.endPos + "): '"
221cdf0e10cSrcweir                     + UnicodeString.substring(bounds.startPos, bounds.endPos)
222cdf0e10cSrcweir                     + "'");
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir             i = bounds.startPos;
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir         log.println("In text there are " + vPrevBounds.size()
227cdf0e10cSrcweir             + " words, if count from right to left");
228cdf0e10cSrcweir         tRes.tested("previousWord()", vPrevBounds.size() == vBounds.size() );
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     /**
232cdf0e10cSrcweir      * For every word in array obtained by <code>nextWord</code> method test
233cdf0e10cSrcweir      * computes bounds of the word, passing its internal character position.<p>
234cdf0e10cSrcweir      *
235cdf0e10cSrcweir      * Has <b>OK</b> status if bounds calculated by <code>getWordBoundary()</code>
236cdf0e10cSrcweir      * method are the same as bounds obtained by <code>nextWord</code> method.
237cdf0e10cSrcweir      */
_getWordBoundary()238cdf0e10cSrcweir     public void _getWordBoundary() {
239cdf0e10cSrcweir         requiredMethod("nextWord()");
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         boolean bRes = true;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         for(int i = 0; i < vBounds.size(); i++) {
244cdf0e10cSrcweir             // calculate middle of the word
245cdf0e10cSrcweir             Boundary iBounds = (Boundary)vBounds.get(i);
246cdf0e10cSrcweir             int iPos = (iBounds.endPos - iBounds.startPos) / 2
247cdf0e10cSrcweir                         + iBounds.startPos;
248cdf0e10cSrcweir             Boundary bounds = oObj.getWordBoundary(UnicodeString, iPos,
249cdf0e10cSrcweir                 locale, wordType, true);
250cdf0e10cSrcweir             log.println("Expected result is: startPos = " + iBounds.startPos +
251cdf0e10cSrcweir                                  "; endPos = " + iBounds.endPos);
252cdf0e10cSrcweir             log.println("Actual result is: startPos = " + bounds.startPos
253cdf0e10cSrcweir                 + "; endPos = " + bounds.endPos + " Word is: '"
254cdf0e10cSrcweir                 + UnicodeString.substring(bounds.startPos, bounds.endPos) + "'");
255cdf0e10cSrcweir 
256cdf0e10cSrcweir             bRes = bRes && iBounds.startPos == bounds.startPos;
257cdf0e10cSrcweir             bRes = bRes && iBounds.endPos == bounds.endPos;
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         tRes.tested("getWordBoundary()", bRes);
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     /**
264cdf0e10cSrcweir      * For every word in array obtained by <code>nextWord</code> method test
265cdf0e10cSrcweir      * get its type, passing its internal character position.<p>
266cdf0e10cSrcweir      *
267cdf0e10cSrcweir      * Has <b>OK</b> status if every word has type <code>WordType.ANY_WORD</code>
268cdf0e10cSrcweir      */
_getWordType()269cdf0e10cSrcweir     public void _getWordType() {
270cdf0e10cSrcweir         requiredMethod("nextWord()");
271cdf0e10cSrcweir 
272cdf0e10cSrcweir         boolean bRes = true;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         for(int i = 0; i < vBounds.size(); i++) {
275cdf0e10cSrcweir             // calculate middle of the word
276cdf0e10cSrcweir             Boundary iBounds = (Boundary)vBounds.get(i);
277cdf0e10cSrcweir             int iPos = (iBounds.endPos - iBounds.startPos) / 2
278cdf0e10cSrcweir                         + iBounds.startPos;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir             short type = oObj.getWordType(UnicodeString, iPos, locale);
281cdf0e10cSrcweir 
282cdf0e10cSrcweir             bRes = bRes && type == WordType.ANY_WORD;
283cdf0e10cSrcweir         }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         tRes.tested("getWordType()", bRes);
286cdf0e10cSrcweir     }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     /**
289cdf0e10cSrcweir      * For every word in array obtained by <code>nextWord</code> method test
290cdf0e10cSrcweir      * tries to determine if the character at a position starts a word.
291cdf0e10cSrcweir      * First word starting position is passed, then internal character
292cdf0e10cSrcweir      * position is passed. <p>
293cdf0e10cSrcweir      * Has <b>OK</b> status if in the first case <code>true</code>
294cdf0e10cSrcweir      * returned and in the second - <code>false</code> for every word.
295cdf0e10cSrcweir      */
_isBeginWord()296cdf0e10cSrcweir     public void _isBeginWord() {
297cdf0e10cSrcweir         requiredMethod("nextWord()");
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         boolean bRes = true;
300cdf0e10cSrcweir 
301cdf0e10cSrcweir         for(int i = 0; i < vBounds.size(); i++) {
302cdf0e10cSrcweir             Boundary iBounds = (Boundary)vBounds.get(i);
303cdf0e10cSrcweir             boolean isBegin = oObj.isBeginWord(UnicodeString, iBounds.startPos,
304cdf0e10cSrcweir                                                locale, WordType.ANY_WORD);
305cdf0e10cSrcweir             bRes = bRes && isBegin;
306cdf0e10cSrcweir             boolean isNotBegin = !oObj.isBeginWord(UnicodeString,
307cdf0e10cSrcweir                     iBounds.startPos + 1, locale, WordType.ANY_WORD);
308cdf0e10cSrcweir             bRes = bRes && isNotBegin;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir             log.println("At position + " + iBounds.startPos
311cdf0e10cSrcweir                 + " isBeginWord? " + isBegin);
312cdf0e10cSrcweir             log.println("At position + " + (iBounds.startPos + 1)
313cdf0e10cSrcweir                 + " isBeginWord? " + !isNotBegin);
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir         tRes.tested("isBeginWord()", bRes);
317cdf0e10cSrcweir     }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir     /**
320cdf0e10cSrcweir      * For every word in array obtained by <code>nextWord</code> method test
321cdf0e10cSrcweir      * tries to determine if the character at a position ends a word.
322cdf0e10cSrcweir      * First word ending position is passed, then internal character
323cdf0e10cSrcweir      * position is passed. <p>
324cdf0e10cSrcweir      *
325cdf0e10cSrcweir      * Has <b>OK</b> status if in the first case <code>true</code>
326cdf0e10cSrcweir      * returned and in the second - <code>false</code> for every word.
327cdf0e10cSrcweir      */
_isEndWord()328cdf0e10cSrcweir     public void _isEndWord() {
329cdf0e10cSrcweir         requiredMethod("nextWord()");
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         boolean bRes = true;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         for(int i = 0; i < vBounds.size(); i++) {
334cdf0e10cSrcweir             Boundary iBounds = (Boundary)vBounds.get(i);
335cdf0e10cSrcweir             boolean isEnd = oObj.isEndWord(UnicodeString, iBounds.endPos,
336cdf0e10cSrcweir                 locale, WordType.ANY_WORD);
337cdf0e10cSrcweir             bRes = bRes && isEnd;
338cdf0e10cSrcweir             boolean isNotEnd = !oObj.isEndWord(UnicodeString,
339cdf0e10cSrcweir                 iBounds.endPos - 1, locale, WordType.ANY_WORD);
340cdf0e10cSrcweir             bRes = bRes && isNotEnd;
341cdf0e10cSrcweir 
342cdf0e10cSrcweir             log.println("At position + " + iBounds.endPos
343cdf0e10cSrcweir                 + " isEndWord? " + isEnd);
344cdf0e10cSrcweir             log.println("At position + " + (iBounds.endPos - 1)
345cdf0e10cSrcweir                 + " isEndWord? " + !isNotEnd);
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         tRes.tested("isEndWord()", bRes);
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     Vector vSentenceStart = new Vector();
352cdf0e10cSrcweir     /**
353cdf0e10cSrcweir      * Tries to find all sentences starting positions passing every character
354cdf0e10cSrcweir      * as position parameter and stores them. Then tries to pass invalid
355cdf0e10cSrcweir      * position parameters.
356cdf0e10cSrcweir      *
357cdf0e10cSrcweir      * Has <b>OK</b> status if -1 is returned for wrong position arguments.
358cdf0e10cSrcweir      */
_beginOfSentence()359cdf0e10cSrcweir     public void _beginOfSentence() {
360cdf0e10cSrcweir         int iPos = 0;
361cdf0e10cSrcweir         while( iPos < UnicodeString.length() ) {
362cdf0e10cSrcweir             Integer start = new Integer( oObj.beginOfSentence(UnicodeString,
363cdf0e10cSrcweir                 iPos, locale) );
364cdf0e10cSrcweir             if (start.intValue() >= 0 && !vSentenceStart.contains(start) ) {
365cdf0e10cSrcweir                 vSentenceStart.add( start );
366cdf0e10cSrcweir                 log.println("Sentence " + vSentenceStart.size()
367cdf0e10cSrcweir                     + " : start from position " + start);
368cdf0e10cSrcweir             }
369cdf0e10cSrcweir             iPos++;
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir         //test for invalid nStartPosition
373cdf0e10cSrcweir         boolean bRes = oObj.beginOfSentence(UnicodeString, -10, locale) == -1;
374cdf0e10cSrcweir         bRes &= oObj.beginOfSentence(UnicodeString,
375cdf0e10cSrcweir             UnicodeString.length() + 1, locale) == -1;
376cdf0e10cSrcweir 
377cdf0e10cSrcweir         if (!bRes) {
378cdf0e10cSrcweir             log.println("When invalid position, returned value isn't equal to -1");
379cdf0e10cSrcweir         }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir         tRes.tested("beginOfSentence()", bRes);
382cdf0e10cSrcweir     }
383cdf0e10cSrcweir 
384cdf0e10cSrcweir     /**
385cdf0e10cSrcweir      * For every sentence starting position found in
386cdf0e10cSrcweir      * <code>beginOfSentence()</code> test tries to compute end
387cdf0e10cSrcweir      * position of a sentence and checks that the end position is
388cdf0e10cSrcweir      * greater than starting.
389cdf0e10cSrcweir      * Then wrong position arguments are passed.
390cdf0e10cSrcweir      *
391cdf0e10cSrcweir      * Has <b>OK</b> status if the end position of every sentence
392cdf0e10cSrcweir      * greater than starting and -1 returned for invalid arguments.
393cdf0e10cSrcweir      */
_endOfSentence()394cdf0e10cSrcweir     public void _endOfSentence() {
395cdf0e10cSrcweir         boolean bRes = true;
396cdf0e10cSrcweir         for(int i = 0; i < vSentenceStart.size(); i++) {
397cdf0e10cSrcweir             int start = ((Integer)vSentenceStart.get(i)).intValue();
398cdf0e10cSrcweir             int end = oObj.endOfSentence(UnicodeString, start, locale);
399cdf0e10cSrcweir             bRes &= end > start;
400cdf0e10cSrcweir             log.println("Sentence " + i + " range is [" + start + ", "
401cdf0e10cSrcweir                 + end + "]");
402cdf0e10cSrcweir         }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir         //test for invalid nStartPosition
405cdf0e10cSrcweir         boolean bInvRes = oObj.endOfSentence(UnicodeString, -10, locale) == -1;
406cdf0e10cSrcweir         bInvRes &= oObj.endOfSentence(UnicodeString,
407cdf0e10cSrcweir             UnicodeString.length() + 1, locale) == -1;
408cdf0e10cSrcweir 
409cdf0e10cSrcweir         if (!bInvRes) {
410cdf0e10cSrcweir             log.println("When invalid position, returned value isn't equal to -1");
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir         tRes.tested("endOfSentence()", bRes && bInvRes);
414cdf0e10cSrcweir     }
415cdf0e10cSrcweir 
416cdf0e10cSrcweir     /**
417cdf0e10cSrcweir     * Tries to break a string in position other than 0 iterating characters
418cdf0e10cSrcweir     * from the string beginning (Hyphenation is not used for a while). <p>
419cdf0e10cSrcweir     *
420cdf0e10cSrcweir     * Has <b>OK</b> status if non-zero break position was found and it is
421cdf0e10cSrcweir     * less or equal than position we trying to break.
422cdf0e10cSrcweir     */
_getLineBreak()423cdf0e10cSrcweir     public void _getLineBreak() {
424cdf0e10cSrcweir         boolean bRes = true;
425cdf0e10cSrcweir         LineBreakResults lineBreakResults;
426cdf0e10cSrcweir         LineBreakHyphenationOptions lineBreakHyphenationOptions =
427cdf0e10cSrcweir             new LineBreakHyphenationOptions();
428cdf0e10cSrcweir         LineBreakUserOptions lineBreakUserOptions = new LineBreakUserOptions();
429cdf0e10cSrcweir 
430cdf0e10cSrcweir         lineBreakUserOptions.applyForbiddenRules = false;
431cdf0e10cSrcweir         lineBreakUserOptions.allowHyphenateEnglish = false;
432cdf0e10cSrcweir 
433cdf0e10cSrcweir         int breakPos = 0;
434cdf0e10cSrcweir         int pos = 0;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir         while(breakPos == 0 && pos < UnicodeString.length() ) {
437cdf0e10cSrcweir             lineBreakResults = oObj.getLineBreak(UnicodeString, pos,
438cdf0e10cSrcweir                 locale, 0, lineBreakHyphenationOptions, lineBreakUserOptions);
439cdf0e10cSrcweir             breakPos = lineBreakResults.breakIndex;
440cdf0e10cSrcweir             pos++;
441cdf0e10cSrcweir         }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir         // finally the position of break must be found in the middle and
444cdf0e10cSrcweir         // it must be before the break position specified
445cdf0e10cSrcweir         bRes = breakPos <= pos && breakPos > 0;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         if (!bRes) {
448cdf0e10cSrcweir             log.println("The last position was: " + pos
449cdf0e10cSrcweir                 + ", and the break position was: " + breakPos);
450cdf0e10cSrcweir         }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir         tRes.tested("getLineBreak()", bRes);
453cdf0e10cSrcweir     }
454cdf0e10cSrcweir 
455cdf0e10cSrcweir     // Asian type script
456cdf0e10cSrcweir     private static String katakana = new String(new char[] {0x30A1, 0x30A2}) ;
457cdf0e10cSrcweir     // Weak type script
458cdf0e10cSrcweir     private static String arrows = new String(new char[] {0x2190, 0x2191}) ;
459cdf0e10cSrcweir     // Complex type script
460cdf0e10cSrcweir     private static String arabic = new String(new char[] {0x0641, 0x0642}) ;
461cdf0e10cSrcweir 
462cdf0e10cSrcweir     /**
463cdf0e10cSrcweir     * Tries to find the begining of the nearest script specified
464cdf0e10cSrcweir     * relatively to position passed. <p>
465cdf0e10cSrcweir     * Has <b>OK</b> status if the starting position of script is returned.
466cdf0e10cSrcweir     */
_beginOfScript()467cdf0e10cSrcweir     public void _beginOfScript() {
468cdf0e10cSrcweir         String multiScript = "ab" + katakana  ;
469cdf0e10cSrcweir 
470cdf0e10cSrcweir         int pos = oObj.beginOfScript(multiScript, 3, ScriptType.ASIAN) ;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir         log.println("Position = " + pos) ;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir         tRes.tested("beginOfScript()", pos == 2) ;
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     /**
478cdf0e10cSrcweir     * Tries to find the end of the nearest script specified
479cdf0e10cSrcweir     * relatively to position passed. <p>
480cdf0e10cSrcweir     * Has <b>OK</b> status if the end position of script is returned.
481cdf0e10cSrcweir     */
_endOfScript()482cdf0e10cSrcweir     public void _endOfScript() {
483cdf0e10cSrcweir         String multiScript = "ab" + katakana + "cd" ;
484cdf0e10cSrcweir 
485cdf0e10cSrcweir         int pos = oObj.endOfScript(multiScript, 2, ScriptType.ASIAN) ;
486cdf0e10cSrcweir 
487cdf0e10cSrcweir         log.println("Position = " + pos) ;
488cdf0e10cSrcweir 
489cdf0e10cSrcweir         tRes.tested("endOfScript()", pos == 4) ;
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir     /**
493cdf0e10cSrcweir     * Tries to find the next script starting position specified
494cdf0e10cSrcweir     * relatively to position passed. <p>
495cdf0e10cSrcweir     * Has <b>OK</b> status if the appropriate position is returned.
496cdf0e10cSrcweir     */
_nextScript()497cdf0e10cSrcweir     public void _nextScript() {
498cdf0e10cSrcweir         String multiScript = "ab" + katakana + "cd"  ;
499cdf0e10cSrcweir 
500cdf0e10cSrcweir         int pos = oObj.nextScript(multiScript, 0, ScriptType.LATIN) ;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir         log.println("Position = " + pos) ;
503cdf0e10cSrcweir 
504cdf0e10cSrcweir         tRes.tested("nextScript()", pos == 4) ;
505cdf0e10cSrcweir     }
506cdf0e10cSrcweir 
507cdf0e10cSrcweir     /**
508cdf0e10cSrcweir     * Tries to find the previous script starting position specified
509cdf0e10cSrcweir     * relatively to position passed. <p>
510cdf0e10cSrcweir     * Has <b>OK</b> status if the appropriate position is returned.
511cdf0e10cSrcweir     */
_previousScript()512cdf0e10cSrcweir     public void _previousScript() {
513cdf0e10cSrcweir         String multiScript = "ab" + katakana + "cd"  ;
514cdf0e10cSrcweir 
515cdf0e10cSrcweir         int pos = oObj.previousScript(multiScript, 5, ScriptType.ASIAN) ;
516cdf0e10cSrcweir 
517cdf0e10cSrcweir         log.println("Position = " + pos) ;
518cdf0e10cSrcweir 
519cdf0e10cSrcweir         tRes.tested("previousScript()", pos == 2) ;
520cdf0e10cSrcweir     }
521cdf0e10cSrcweir 
522cdf0e10cSrcweir     /**
523cdf0e10cSrcweir     * Tries to determine script type (of all four types). <p>
524cdf0e10cSrcweir     * Has <b>OK</b> status if <code>LATIN</code> type returned
525cdf0e10cSrcweir     * for ACSII character, <code>ASIAN</code> for Katakana Unicode
526cdf0e10cSrcweir     * codepoints, <code>COMPLEX</code> for Arabic Unicode
527cdf0e10cSrcweir     * codepoints and <code>WEAK</code> for codepoints from Arrows
528cdf0e10cSrcweir     * Unicode block.
529cdf0e10cSrcweir     */
_getScriptType()530cdf0e10cSrcweir     public void _getScriptType() {
531cdf0e10cSrcweir         boolean res = true ;
532cdf0e10cSrcweir 
533cdf0e10cSrcweir         res &= oObj.getScriptType("abcd", 0) == ScriptType.LATIN ;
534cdf0e10cSrcweir         res &= oObj.getScriptType(katakana, 0) == ScriptType.ASIAN;
535cdf0e10cSrcweir         res &= oObj.getScriptType(arabic, 0) == ScriptType.COMPLEX ;
536cdf0e10cSrcweir         res &= oObj.getScriptType(arrows, 0) == ScriptType.WEAK ;
537cdf0e10cSrcweir 
538cdf0e10cSrcweir         tRes.tested("getScriptType()", res) ;
539cdf0e10cSrcweir     }
540cdf0e10cSrcweir 
541cdf0e10cSrcweir     boolean bCharBlockRes = true;
542cdf0e10cSrcweir 
getCharBlockType(int pos)543cdf0e10cSrcweir     protected short getCharBlockType(int pos) {
544cdf0e10cSrcweir         short i = 1;
545cdf0e10cSrcweir         short cType = 0;
546cdf0e10cSrcweir         while (i < 31) {
547cdf0e10cSrcweir             if (oObj.beginOfCharBlock(UnicodeString, pos, locale, i) != -1) {
548cdf0e10cSrcweir                 cType = i;
549cdf0e10cSrcweir                 i = 100;
550cdf0e10cSrcweir             }
551cdf0e10cSrcweir             i++;
552cdf0e10cSrcweir         }
553cdf0e10cSrcweir 
554cdf0e10cSrcweir         return cType;
555cdf0e10cSrcweir     }
556cdf0e10cSrcweir 
557cdf0e10cSrcweir     Vector vCharBlockBounds = new Vector();
558cdf0e10cSrcweir     Vector vCharBlockTypes = new Vector();
559cdf0e10cSrcweir 
560cdf0e10cSrcweir     /**
561cdf0e10cSrcweir      * Creates array of all char blocks with their boundaries and
562cdf0e10cSrcweir      * types using <code>beginOfCharBlock()</code> and
563cdf0e10cSrcweir      * <code>endOfCharBlock()</code> methods. <p>
564cdf0e10cSrcweir      *
565cdf0e10cSrcweir      * Has <b>OK</b> status if the end of each boundary is the same
566cdf0e10cSrcweir      * as start of the next one and if the start of the first block
567cdf0e10cSrcweir      * has position 0 and the end of the last block is at the end
568cdf0e10cSrcweir      * of the whole string.
569cdf0e10cSrcweir      */
_beginOfCharBlock()570cdf0e10cSrcweir     public void _beginOfCharBlock() {
571cdf0e10cSrcweir         int iPos = 0;
572cdf0e10cSrcweir 
573cdf0e10cSrcweir         while( iPos < UnicodeString.length() && iPos > -1) {
574cdf0e10cSrcweir             short charType = getCharBlockType(iPos);
575cdf0e10cSrcweir             int startPos = oObj.beginOfCharBlock(UnicodeString, iPos,
576cdf0e10cSrcweir                 locale, charType);
577cdf0e10cSrcweir             int endPos = oObj.endOfCharBlock(UnicodeString, iPos,
578cdf0e10cSrcweir                 locale, charType);
579cdf0e10cSrcweir             iPos = endPos;
580cdf0e10cSrcweir             vCharBlockBounds.add(new Boundary(startPos, endPos));
581cdf0e10cSrcweir             log.println("" + vCharBlockBounds.size() + "). Bounds: ["
582cdf0e10cSrcweir                 + startPos + "," + endPos + "]; Type = " + charType);
583cdf0e10cSrcweir             vCharBlockTypes.add(new Short(charType));
584cdf0e10cSrcweir         }
585cdf0e10cSrcweir 
586cdf0e10cSrcweir         for(int i = 0; i < vCharBlockBounds.size() - 1; i++) {
587cdf0e10cSrcweir             int endPos = ((Boundary)vCharBlockBounds.get(i)).endPos;
588cdf0e10cSrcweir             int startPos = ((Boundary)vCharBlockBounds.get(i + 1)).startPos;
589cdf0e10cSrcweir             bCharBlockRes &= endPos == startPos;
590cdf0e10cSrcweir         }
591cdf0e10cSrcweir 
592cdf0e10cSrcweir         log.println("Testing for no intersections : " + bCharBlockRes);
593cdf0e10cSrcweir         int startPos = ((Boundary)vCharBlockBounds.get(0)).startPos;
594cdf0e10cSrcweir         bCharBlockRes &= startPos == 0;
595cdf0e10cSrcweir         int endPos = ((Boundary)vCharBlockBounds.get
596cdf0e10cSrcweir             (vCharBlockBounds.size() - 1)).endPos;
597cdf0e10cSrcweir         bCharBlockRes &= endPos == UnicodeString.length();
598cdf0e10cSrcweir         log.println("Regions should starts with 0 and ends with "
599cdf0e10cSrcweir             + UnicodeString.length());
600cdf0e10cSrcweir 
601cdf0e10cSrcweir         tRes.tested("beginOfCharBlock()", bCharBlockRes);
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     /**
605cdf0e10cSrcweir      * Testing of this method is performed in <code>beginOfCharBlock()</code>
606cdf0e10cSrcweir      * method test. <p>
607cdf0e10cSrcweir      *
608cdf0e10cSrcweir      * Has the status same as <code>beginOfCharBlock()</code> method status.
609cdf0e10cSrcweir      */
_endOfCharBlock()610cdf0e10cSrcweir     public void _endOfCharBlock() {
611cdf0e10cSrcweir         requiredMethod("beginOfCharBlock()");
612cdf0e10cSrcweir         tRes.tested("endOfCharBlock()", bCharBlockRes);
613cdf0e10cSrcweir     }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir     /**
616cdf0e10cSrcweir      * For every character block obtained in <code>beginOfCharBlock()</code>
617cdf0e10cSrcweir      * method test (except the first) tries to find its starting position
618cdf0e10cSrcweir      * by mean of <code>nextCharBlock()</code> method passing as position
619cdf0e10cSrcweir      * argument the position before the start of a block. <p>
620cdf0e10cSrcweir      *
621cdf0e10cSrcweir      * Has <b>OK</b> status if the start of every block was found and it's
622cdf0e10cSrcweir      * equal to this block boundary start.
623cdf0e10cSrcweir      */
_nextCharBlock()624cdf0e10cSrcweir     public void _nextCharBlock() {
625cdf0e10cSrcweir         requiredMethod("beginOfCharBlock()");
626cdf0e10cSrcweir 
627cdf0e10cSrcweir         boolean bRes = true;
628cdf0e10cSrcweir         for(int i = 0; i < vCharBlockBounds.size(); i++) {
629cdf0e10cSrcweir             Boundary bounds = (Boundary)vCharBlockBounds.get(i);
630cdf0e10cSrcweir             Short type = (Short)vCharBlockTypes.get(i);
631cdf0e10cSrcweir             if (bounds.startPos - 1 < 0) continue;
632cdf0e10cSrcweir             int iPos = oObj.nextCharBlock(UnicodeString, bounds.startPos - 1,
633cdf0e10cSrcweir                 locale, type.shortValue());
634cdf0e10cSrcweir             if (iPos != bounds.startPos) {
635cdf0e10cSrcweir                 bRes = false;
636cdf0e10cSrcweir                 log.println("nextCharBlock(UnicodeString, "
637cdf0e10cSrcweir                     + (bounds.startPos - 1) + ", locale, " + type
638cdf0e10cSrcweir                     + ") should return " + bounds.startPos);
639cdf0e10cSrcweir                 log.println("... and actual value is " + iPos);
640cdf0e10cSrcweir             }
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir 
643cdf0e10cSrcweir         tRes.tested("nextCharBlock()", bRes);
644cdf0e10cSrcweir     }
645cdf0e10cSrcweir 
646cdf0e10cSrcweir     /**
647cdf0e10cSrcweir      * For every character block obtained in <code>beginOfCharBlock()</code>
648cdf0e10cSrcweir      * method test (except the first) tries to find its starting position
649cdf0e10cSrcweir      * by mean of <code>previousCharBlock()</code> method passing as position
650cdf0e10cSrcweir      * argument the position after the end of a block. <p>
651cdf0e10cSrcweir      *
652cdf0e10cSrcweir      * Has <b>OK</b> status if the start of every block was found and it's
653cdf0e10cSrcweir      * equal to this block boundary start.
654cdf0e10cSrcweir      */
_previousCharBlock()655cdf0e10cSrcweir     public void _previousCharBlock() {
656cdf0e10cSrcweir         requiredMethod("beginOfCharBlock()");
657cdf0e10cSrcweir 
658cdf0e10cSrcweir         boolean bRes = true;
659cdf0e10cSrcweir         for(int i = 0; i < vCharBlockBounds.size(); i++) {
660cdf0e10cSrcweir             Boundary bounds = (Boundary)vCharBlockBounds.get(i);
661cdf0e10cSrcweir             Short type = (Short)vCharBlockTypes.get(i);
662cdf0e10cSrcweir             int iPos = oObj.previousCharBlock(UnicodeString,
663cdf0e10cSrcweir                 bounds.endPos + 1, locale, type.shortValue());
664cdf0e10cSrcweir             if (iPos != bounds.startPos) {
665cdf0e10cSrcweir                 bRes = false;
666cdf0e10cSrcweir                 log.println("previousCharBlock(UnicodeString, "
667cdf0e10cSrcweir                     + (bounds.endPos + 1) + ", locale, " + type
668cdf0e10cSrcweir                     + ") should return " + bounds.startPos);
669cdf0e10cSrcweir                 log.println("... and actual value is " + iPos);
670cdf0e10cSrcweir             }
671cdf0e10cSrcweir         }
672cdf0e10cSrcweir 
673cdf0e10cSrcweir         tRes.tested("previousCharBlock()", bRes);
674cdf0e10cSrcweir     }
675cdf0e10cSrcweir 
676cdf0e10cSrcweir }
677cdf0e10cSrcweir 
678