1/**
2*******************************************************************************
3* Copyright (C) 1999-2010, International Business Machines Corporation and    *
4* others. All Rights Reserved.                                                *
5*******************************************************************************
6*/
7
8package com.ibm.icu.dev.test.lang;
9
10import com.ibm.icu.dev.test.TestFmwk;
11import com.ibm.icu.lang.UScript;
12import com.ibm.icu.lang.UScriptRun;
13
14public class TestUScriptRun extends TestFmwk
15{
16    public TestUScriptRun()
17    {
18        // nothing
19    }
20
21    public static void main(String[] args) throws Exception {
22        new TestUScriptRun().run(args);
23    }
24
25    private static final class RunTestData
26    {
27        String runText;
28        int    runScript;
29
30        public RunTestData(String theText, int theScriptCode)
31        {
32            runText   = theText;
33            runScript = theScriptCode;
34        }
35    }
36
37    private static final RunTestData[][] m_testData = {
38        {
39            new RunTestData("\u0020\u0946\u0939\u093F\u0928\u094D\u0926\u0940\u0020", UScript.DEVANAGARI),
40            new RunTestData("\u0627\u0644\u0639\u0631\u0628\u064A\u0629\u0020", UScript.ARABIC),
41            new RunTestData("\u0420\u0443\u0441\u0441\u043A\u0438\u0439\u0020", UScript.CYRILLIC),
42            new RunTestData("English (", UScript.LATIN),
43            new RunTestData("\u0E44\u0E17\u0E22", UScript.THAI),
44            new RunTestData(") ", UScript.LATIN),
45            new RunTestData("\u6F22\u5B75", UScript.HAN),
46            new RunTestData("\u3068\u3072\u3089\u304C\u306A\u3068", UScript.HIRAGANA),
47            new RunTestData("\u30AB\u30BF\u30AB\u30CA", UScript.KATAKANA),
48            new RunTestData("\uD801\uDC00\uD801\uDC01\uD801\uDC02\uD801\uDC03", UScript.DESERET),
49        },
50        {
51            new RunTestData("((((((((((abc))))))))))", UScript.LATIN)
52        }
53    };
54
55    private static final String padding = "This string is used for padding...";
56
57    private void CheckScriptRuns(UScriptRun scriptRun, int[] runStarts, RunTestData[] testData)
58    {
59        int run, runStart, runLimit;
60        int runScript;
61
62        /* iterate over all the runs */
63        run = 0;
64        while (scriptRun.next()) {
65            runStart  = scriptRun.getScriptStart();
66            runLimit  = scriptRun.getScriptLimit();
67            runScript = scriptRun.getScriptCode();
68
69            if (runStart != runStarts[run]) {
70                errln("Incorrect start offset for run " + run + ": expected " + runStarts[run] + ", got " + runStart);
71            }
72
73            if (runLimit != runStarts[run + 1]) {
74                errln("Incorrect limit offset for run " + run + ": expected " + runStarts[run + 1] + ", got " + runLimit);
75            }
76
77            if (runScript != testData[run].runScript) {
78                errln("Incorrect script for run " + run + ": expected \"" + UScript.getName(testData[run].runScript) + "\", got \"" + UScript.getName(runScript) + "\"");
79            }
80
81            run += 1;
82
83            /* stop when we've seen all the runs we expect to see */
84            if (run >= testData.length) {
85                break;
86            }
87        }
88
89        /* Complain if we didn't see then number of runs we expected */
90        if (run != testData.length) {
91            errln("Incorrect number of runs: expected " + testData.length + ", got " + run);
92        }
93    }
94
95    public void TestContstruction()
96    {
97        UScriptRun scriptRun = null;
98        char[] nullChars  = null, dummyChars  = {'d', 'u', 'm', 'm', 'y'};
99        String nullString = null, dummyString = new String(dummyChars);
100
101        try {
102            scriptRun = new UScriptRun(nullString, 0, 100);
103            errln("new UScriptRun(nullString, 0, 100) did not produce an IllegalArgumentException!");
104        } catch (IllegalArgumentException iae) {
105            logln("PASS: UScriptRun failed as expected");
106        }
107
108        try {
109            scriptRun = new UScriptRun(nullString, 100, 0);
110            errln("new UScriptRun(nullString, 100, 0) did not produce an IllegalArgumentException!");
111        } catch (IllegalArgumentException iae) {
112            logln("PASS: UScriptRun failed as expected");
113        }
114
115        try {
116            scriptRun = new UScriptRun(nullString, 0, -100);
117            errln("new UScriptRun(nullString, 0, -100) did not produce an IllegalArgumentException!");
118        } catch (IllegalArgumentException iae) {
119            logln("PASS: UScriptRun failed as expected");
120        }
121
122        try {
123            scriptRun = new UScriptRun(nullString, -100, 0);
124            errln("new UScriptRun(nullString, -100, 0) did not produce an IllegalArgumentException!");
125        } catch (IllegalArgumentException iae) {
126            logln("PASS: UScriptRun failed as expected");
127        }
128
129        try {
130            scriptRun = new UScriptRun(nullChars, 0, 100);
131            errln("new UScriptRun(nullChars, 0, 100) did not produce an IllegalArgumentException!");
132        } catch (IllegalArgumentException iae) {
133            logln("PASS: UScriptRun failed as expected");
134        }
135
136        try {
137            scriptRun = new UScriptRun(nullChars, 100, 0);
138            errln("new UScriptRun(nullChars, 100, 0) did not produce an IllegalArgumentException!");
139        } catch (IllegalArgumentException iae) {
140            logln("PASS: UScriptRun failed as expected");
141        }
142
143        try {
144            scriptRun = new UScriptRun(nullChars, 0, -100);
145            errln("new UScriptRun(nullChars, 0, -100) did not produce an IllegalArgumentException!");
146        } catch (IllegalArgumentException iae) {
147            logln("PASS: UScriptRun failed as expected");
148        }
149
150        try {
151            scriptRun = new UScriptRun(nullChars, -100, 0);
152            errln("new UScriptRun(nullChars, -100, 0) did not produce an IllegalArgumentException!");
153        } catch (IllegalArgumentException iae) {
154            logln("PASS: UScriptRun failed as expected");
155        }
156
157        try {
158            scriptRun = new UScriptRun(dummyString, 0, 6);
159            errln("new UScriptRun(dummyString, 0, 6) did not produce an IllegalArgumentException!");
160        } catch (IllegalArgumentException iae) {
161            logln("PASS: UScriptRun failed as expected");
162        }
163
164        try {
165            scriptRun = new UScriptRun(dummyString, 6, 0);
166            errln("new UScriptRun(dummy, 6, 0) did not produce an IllegalArgumentException!");
167        }catch (IllegalArgumentException iae) {
168            logln("PASS: UScriptRun failed as expected");
169        }
170
171        try {
172            scriptRun = new UScriptRun(dummyString, 0, -100);
173            errln("new UScriptRun(dummyString, 0, -100) did not produce an IllegalArgumentException!");
174        } catch (IllegalArgumentException iae) {
175            logln("PASS: UScriptRun failed as expected");
176        }
177
178        try {
179            scriptRun = new UScriptRun(dummyString, -100, 0);
180            errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!");
181        } catch (IllegalArgumentException iae) {
182            logln("PASS: UScriptRun failed as expected");
183        }
184
185        try {
186            scriptRun = new UScriptRun(dummyChars, 0, 6);
187            errln("new UScriptRun(dummyChars, 0, 6) did not produce an IllegalArgumentException!");
188        } catch (IllegalArgumentException iae) {
189            logln("PASS: UScriptRun failed as expected");
190        }
191
192        try {
193            scriptRun = new UScriptRun(dummyChars, 6, 0);
194            errln("new UScriptRun(dummyChars, 6, 0) did not produce an IllegalArgumentException!");
195        }catch (IllegalArgumentException iae) {
196            logln("PASS: UScriptRun failed as expected");
197        }
198
199        try {
200            scriptRun = new UScriptRun(dummyChars, 0, -100);
201            errln("new UScriptRun(dummyChars, 0, -100) did not produce an IllegalArgumentException!");
202        } catch (IllegalArgumentException iae) {
203            logln("PASS: UScriptRun failed as expected");
204        }
205
206        try {
207            scriptRun = new UScriptRun(dummyChars, -100, 0);
208            errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!");
209        } catch (IllegalArgumentException iae) {
210            logln("PASS: UScriptRun failed as expected");
211        }
212        if(scriptRun!=null){
213            errln("Did not get the expected Exception");
214        }
215    }
216
217    public void TestReset()
218    {
219        UScriptRun scriptRun = null;
220        char[] dummy = {'d', 'u', 'm', 'm', 'y'};
221
222        try {
223            scriptRun = new UScriptRun();
224        } catch (IllegalArgumentException iae) {
225            errln("new UScriptRun() produced an IllegalArgumentException!");
226        }
227
228        try {
229            scriptRun.reset(0, 100);
230            errln("scriptRun.reset(0, 100) did not produce an IllegalArgumentException!");
231        } catch (IllegalArgumentException iae) {
232            logln("PASS: scriptRun.reset failed as expected");
233        }
234
235        try {
236            scriptRun.reset(100, 0);
237            errln("scriptRun.reset(100, 0) did not produce an IllegalArgumentException!");
238        } catch (IllegalArgumentException iae) {
239            logln("PASS: scriptRun.reset failed as expected");
240        }
241
242        try {
243            scriptRun.reset(0, -100);
244            errln("scriptRun.reset(0, -100) did not produce an IllegalArgumentException!");
245        } catch (IllegalArgumentException iae) {
246            logln("PASS: scriptRun.reset failed as expected");
247        }
248
249        try {
250            scriptRun.reset(-100, 0);
251            errln("scriptRun.reset(-100, 0) did not produce an IllegalArgumentException!");
252        } catch (IllegalArgumentException iae) {
253            logln("PASS: scriptRun.reset failed as expected");
254        }
255
256        try {
257            scriptRun.reset(dummy, 0, 6);
258            errln("scriptRun.reset(dummy, 0, 6) did not produce an IllegalArgumentException!");
259        } catch (IllegalArgumentException iae) {
260            logln("PASS: scriptRun.reset failed as expected");
261        }
262
263        try {
264            scriptRun.reset(dummy, 6, 0);
265            errln("scriptRun.reset(dummy, 6, 0) did not produce an IllegalArgumentException!");
266        }catch (IllegalArgumentException iae) {
267            logln("PASS: scriptRun.reset failed as expected");
268        }
269
270        try {
271            scriptRun.reset(dummy, 0, -100);
272            errln("scriptRun.reset(dummy, 0, -100) did not produce an IllegalArgumentException!");
273        } catch (IllegalArgumentException iae) {
274            logln("PASS: scriptRun.reset failed as expected");
275        }
276
277        try {
278            scriptRun.reset(dummy, -100, 0);
279            errln("scriptRun.reset(dummy, -100, 0) did not produce an IllegalArgumentException!");
280        } catch (IllegalArgumentException iae) {
281            logln("PASS: scriptRun.reset failed as expected");
282        }
283
284        try {
285            scriptRun.reset(dummy, 0, dummy.length);
286        } catch (IllegalArgumentException iae) {
287            errln("scriptRun.reset(dummy, 0, dummy.length) produced an IllegalArgumentException!");
288        }
289
290
291        try {
292            scriptRun.reset(0, 6);
293            errln("scriptRun.reset(0, 6) did not produce an IllegalArgumentException!");
294        } catch (IllegalArgumentException iae) {
295            logln("PASS: scriptRun.reset failed as expected");
296        }
297
298        try {
299            scriptRun.reset(6, 0);
300            errln("scriptRun.reset(6, 0) did not produce an IllegalArgumentException!");
301        } catch (IllegalArgumentException iae) {
302            logln("PASS: scriptRun.reset failed as expected");
303        }
304
305        try {
306            scriptRun.reset(dummy, 0, dummy.length);
307            scriptRun.reset();
308        } catch(IllegalArgumentException iae){
309            errln("scriptRun.reset() produced an IllegalArgumentException!");
310        }
311
312        try {
313            scriptRun.reset((char[]) null);
314        } catch(IllegalArgumentException iae){
315            errln("scriptRun.reset((char[])null) produced an IllegalArgumentException!");
316        }
317
318        try {
319            scriptRun.reset((String) null);
320        } catch(IllegalArgumentException iae){
321            errln("scriptRun.reset((String)null) produced an IllegalArgumentException!");
322        }
323    }
324
325    public void TestRuns()
326    {
327        for (int i = 0; i < m_testData.length; i += 1) {
328            RunTestData[] test = m_testData[i];
329            int stringLimit = 0;
330            int[] runStarts = new int[test.length + 1];
331            String testString = "";
332            UScriptRun scriptRun = null;
333
334            /*
335             * Fill in the test string and the runStarts array.
336             */
337            for (int run = 0; run < test.length; run += 1) {
338                runStarts[run] = stringLimit;
339                stringLimit += test[run].runText.length();
340                testString  += test[run].runText;
341            }
342
343            /* The limit of the last run */
344            runStarts[test.length] = stringLimit;
345
346            try {
347                scriptRun = new UScriptRun(testString);
348                CheckScriptRuns(scriptRun, runStarts, test);
349            } catch (IllegalArgumentException iae) {
350                errln("new UScriptRun(testString) produced an IllegalArgumentException!");
351            }
352
353            try {
354                scriptRun.reset();
355                CheckScriptRuns(scriptRun, runStarts, test);
356            } catch (IllegalArgumentException iae) {
357                errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!");
358            }
359
360            try {
361                scriptRun = new UScriptRun(testString.toCharArray());
362                CheckScriptRuns(scriptRun, runStarts, test);
363            } catch (IllegalArgumentException iae) {
364                errln("new UScriptRun(testString.toCharArray()) produced an IllegalArgumentException!");
365            }
366
367            try {
368                scriptRun.reset();
369                CheckScriptRuns(scriptRun, runStarts, test);
370            } catch (IllegalArgumentException iae) {
371                errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!");
372            }
373
374            try {
375                scriptRun = new UScriptRun();
376
377                if (scriptRun.next()) {
378                    errln("scriptRun.next() on an empty UScriptRun returned true!");
379                }
380            } catch (IllegalArgumentException iae) {
381                errln("new UScriptRun() produced an IllegalArgumentException!");
382            }
383
384            try {
385                scriptRun.reset(testString, 0, testString.length());
386                CheckScriptRuns(scriptRun, runStarts, test);
387            } catch (IllegalArgumentException iae) {
388                errln("scriptRun.reset(testString, 0, testString.length) produced an IllegalArgumentException!");
389            }
390
391            try {
392                scriptRun.reset(testString.toCharArray(), 0, testString.length());
393                CheckScriptRuns(scriptRun, runStarts, test);
394            } catch (IllegalArgumentException iae) {
395                errln("scriptRun.reset(testString.toCharArray(), 0, testString.length) produced an IllegalArgumentException!");
396            }
397
398            String paddedTestString = padding + testString + padding;
399            int startOffset = padding.length();
400            int count = testString.length();
401
402            for (int run = 0; run < runStarts.length; run += 1) {
403                runStarts[run] += startOffset;
404            }
405
406            try {
407                scriptRun.reset(paddedTestString, startOffset, count);
408                CheckScriptRuns(scriptRun, runStarts, test);
409            } catch (IllegalArgumentException iae) {
410                errln("scriptRun.reset(paddedTestString, startOffset, count) produced an IllegalArgumentException!");
411            }
412
413            try {
414                scriptRun.reset(paddedTestString.toCharArray(), startOffset, count);
415                CheckScriptRuns(scriptRun, runStarts, test);
416            } catch (IllegalArgumentException iae) {
417                errln("scriptRun.reset(paddedTestString.toCharArray(), startOffset, count) produced an IllegalArgumentException!");
418            }
419
420            /* Tests "public final void reset()" */
421            // Tests when "while (stackIsNotEmpty())" is true
422            try{
423                UScriptRun usr = new UScriptRun((String)null);
424                usr.reset();
425            } catch (Exception e){
426                errln("scriptRun.reset() was not suppose to produce an exception.");
427            }
428        }
429    }
430}
431