001: /**
002: *******************************************************************************
003: * Copyright (C) 1999-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test.lang;
007:
008: import com.ibm.icu.lang.UScript;
009: import com.ibm.icu.lang.UScriptRun;
010: import com.ibm.icu.dev.test.TestFmwk;
011:
012: public class TestUScriptRun extends TestFmwk {
013: public TestUScriptRun() {
014: // nothing
015: }
016:
017: public static void main(String[] args) throws Exception {
018: new TestUScriptRun().run(args);
019: }
020:
021: private static final class RunTestData {
022: String runText;
023: int runScript;
024:
025: public RunTestData(String theText, int theScriptCode) {
026: runText = theText;
027: runScript = theScriptCode;
028: }
029: }
030:
031: private static final RunTestData[][] testData = {
032: {
033: new RunTestData(
034: "\u0020\u0946\u0939\u093F\u0928\u094D\u0926\u0940\u0020",
035: UScript.DEVANAGARI),
036: new RunTestData(
037: "\u0627\u0644\u0639\u0631\u0628\u064A\u0629\u0020",
038: UScript.ARABIC),
039: new RunTestData(
040: "\u0420\u0443\u0441\u0441\u043A\u0438\u0439\u0020",
041: UScript.CYRILLIC),
042: new RunTestData("English (", UScript.LATIN),
043: new RunTestData("\u0E44\u0E17\u0E22", UScript.THAI),
044: new RunTestData(") ", UScript.LATIN),
045: new RunTestData("\u6F22\u5B75", UScript.HAN),
046: new RunTestData(
047: "\u3068\u3072\u3089\u304C\u306A\u3068",
048: UScript.HIRAGANA),
049: new RunTestData("\u30AB\u30BF\u30AB\u30CA",
050: UScript.KATAKANA),
051: new RunTestData(
052: "\uD801\uDC00\uD801\uDC01\uD801\uDC02\uD801\uDC03",
053: UScript.DESERET), },
054: { new RunTestData("((((((((((abc))))))))))", UScript.LATIN) } };
055:
056: private static final String padding = "This string is used for padding...";
057:
058: private void CheckScriptRuns(UScriptRun scriptRun, int[] runStarts,
059: RunTestData[] testData) {
060: int run, runStart, runLimit;
061: int runScript;
062:
063: /* iterate over all the runs */
064: run = 0;
065: while (scriptRun.next()) {
066: runStart = scriptRun.getScriptStart();
067: runLimit = scriptRun.getScriptLimit();
068: runScript = scriptRun.getScriptCode();
069:
070: if (runStart != runStarts[run]) {
071: errln("Incorrect start offset for run " + run
072: + ": expected " + runStarts[run] + ", got "
073: + runStart);
074: }
075:
076: if (runLimit != runStarts[run + 1]) {
077: errln("Incorrect limit offset for run " + run
078: + ": expected " + runStarts[run + 1] + ", got "
079: + runLimit);
080: }
081:
082: if (runScript != testData[run].runScript) {
083: errln("Incorrect script for run " + run
084: + ": expected \""
085: + UScript.getName(testData[run].runScript)
086: + "\", got \"" + UScript.getName(runScript)
087: + "\"");
088: }
089:
090: run += 1;
091:
092: /* stop when we've seen all the runs we expect to see */
093: if (run >= testData.length) {
094: break;
095: }
096: }
097:
098: /* Complain if we didn't see then number of runs we expected */
099: if (run != testData.length) {
100: errln("Incorrect number of runs: expected "
101: + testData.length + ", got " + run);
102: }
103: }
104:
105: public void TestContstruction() {
106: UScriptRun scriptRun = null;
107: char[] nullChars = null, dummyChars = { 'd', 'u', 'm', 'm', 'y' };
108: String nullString = null, dummyString = new String(dummyChars);
109:
110: try {
111: scriptRun = new UScriptRun(nullString, 0, 100);
112: errln("new UScriptRun(nullString, 0, 100) did not produce an IllegalArgumentException!");
113: } catch (IllegalArgumentException iae) {
114: logln("PASS: UScriptRun failed as expected");
115: }
116:
117: try {
118: scriptRun = new UScriptRun(nullString, 100, 0);
119: errln("new UScriptRun(nullString, 100, 0) did not produce an IllegalArgumentException!");
120: } catch (IllegalArgumentException iae) {
121: logln("PASS: UScriptRun failed as expected");
122: }
123:
124: try {
125: scriptRun = new UScriptRun(nullString, 0, -100);
126: errln("new UScriptRun(nullString, 0, -100) did not produce an IllegalArgumentException!");
127: } catch (IllegalArgumentException iae) {
128: logln("PASS: UScriptRun failed as expected");
129: }
130:
131: try {
132: scriptRun = new UScriptRun(nullString, -100, 0);
133: errln("new UScriptRun(nullString, -100, 0) did not produce an IllegalArgumentException!");
134: } catch (IllegalArgumentException iae) {
135: logln("PASS: UScriptRun failed as expected");
136: }
137:
138: try {
139: scriptRun = new UScriptRun(nullChars, 0, 100);
140: errln("new UScriptRun(nullChars, 0, 100) did not produce an IllegalArgumentException!");
141: } catch (IllegalArgumentException iae) {
142: logln("PASS: UScriptRun failed as expected");
143: }
144:
145: try {
146: scriptRun = new UScriptRun(nullChars, 100, 0);
147: errln("new UScriptRun(nullChars, 100, 0) did not produce an IllegalArgumentException!");
148: } catch (IllegalArgumentException iae) {
149: logln("PASS: UScriptRun failed as expected");
150: }
151:
152: try {
153: scriptRun = new UScriptRun(nullChars, 0, -100);
154: errln("new UScriptRun(nullChars, 0, -100) did not produce an IllegalArgumentException!");
155: } catch (IllegalArgumentException iae) {
156: logln("PASS: UScriptRun failed as expected");
157: }
158:
159: try {
160: scriptRun = new UScriptRun(nullChars, -100, 0);
161: errln("new UScriptRun(nullChars, -100, 0) did not produce an IllegalArgumentException!");
162: } catch (IllegalArgumentException iae) {
163: logln("PASS: UScriptRun failed as expected");
164: }
165:
166: try {
167: scriptRun = new UScriptRun(dummyString, 0, 6);
168: errln("new UScriptRun(dummyString, 0, 6) did not produce an IllegalArgumentException!");
169: } catch (IllegalArgumentException iae) {
170: logln("PASS: UScriptRun failed as expected");
171: }
172:
173: try {
174: scriptRun = new UScriptRun(dummyString, 6, 0);
175: errln("new UScriptRun(dummy, 6, 0) did not produce an IllegalArgumentException!");
176: } catch (IllegalArgumentException iae) {
177: logln("PASS: UScriptRun failed as expected");
178: }
179:
180: try {
181: scriptRun = new UScriptRun(dummyString, 0, -100);
182: errln("new UScriptRun(dummyString, 0, -100) did not produce an IllegalArgumentException!");
183: } catch (IllegalArgumentException iae) {
184: logln("PASS: UScriptRun failed as expected");
185: }
186:
187: try {
188: scriptRun = new UScriptRun(dummyString, -100, 0);
189: errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!");
190: } catch (IllegalArgumentException iae) {
191: logln("PASS: UScriptRun failed as expected");
192: }
193:
194: try {
195: scriptRun = new UScriptRun(dummyChars, 0, 6);
196: errln("new UScriptRun(dummyChars, 0, 6) did not produce an IllegalArgumentException!");
197: } catch (IllegalArgumentException iae) {
198: logln("PASS: UScriptRun failed as expected");
199: }
200:
201: try {
202: scriptRun = new UScriptRun(dummyChars, 6, 0);
203: errln("new UScriptRun(dummyChars, 6, 0) did not produce an IllegalArgumentException!");
204: } catch (IllegalArgumentException iae) {
205: logln("PASS: UScriptRun failed as expected");
206: }
207:
208: try {
209: scriptRun = new UScriptRun(dummyChars, 0, -100);
210: errln("new UScriptRun(dummyChars, 0, -100) did not produce an IllegalArgumentException!");
211: } catch (IllegalArgumentException iae) {
212: logln("PASS: UScriptRun failed as expected");
213: }
214:
215: try {
216: scriptRun = new UScriptRun(dummyChars, -100, 0);
217: errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!");
218: } catch (IllegalArgumentException iae) {
219: logln("PASS: UScriptRun failed as expected");
220: }
221: if (scriptRun != null) {
222: errln("Did not get the expected Exception");
223: }
224: }
225:
226: public void TestReset() {
227: UScriptRun scriptRun = null;
228: char[] dummy = { 'd', 'u', 'm', 'm', 'y' };
229:
230: try {
231: scriptRun = new UScriptRun();
232: } catch (IllegalArgumentException iae) {
233: errln("new UScriptRun() produced an IllegalArgumentException!");
234: }
235:
236: try {
237: scriptRun.reset(0, 100);
238: errln("scriptRun.reset(0, 100) did not produce an IllegalArgumentException!");
239: } catch (IllegalArgumentException iae) {
240: logln("PASS: scriptRun.reset failed as expected");
241: }
242:
243: try {
244: scriptRun.reset(100, 0);
245: errln("scriptRun.reset(100, 0) did not produce an IllegalArgumentException!");
246: } catch (IllegalArgumentException iae) {
247: logln("PASS: scriptRun.reset failed as expected");
248: }
249:
250: try {
251: scriptRun.reset(0, -100);
252: errln("scriptRun.reset(0, -100) did not produce an IllegalArgumentException!");
253: } catch (IllegalArgumentException iae) {
254: logln("PASS: scriptRun.reset failed as expected");
255: }
256:
257: try {
258: scriptRun.reset(-100, 0);
259: errln("scriptRun.reset(-100, 0) did not produce an IllegalArgumentException!");
260: } catch (IllegalArgumentException iae) {
261: logln("PASS: scriptRun.reset failed as expected");
262: }
263:
264: try {
265: scriptRun.reset(dummy, 0, 6);
266: errln("scriptRun.reset(dummy, 0, 6) did not produce an IllegalArgumentException!");
267: } catch (IllegalArgumentException iae) {
268: logln("PASS: scriptRun.reset failed as expected");
269: }
270:
271: try {
272: scriptRun.reset(dummy, 6, 0);
273: errln("scriptRun.reset(dummy, 6, 0) did not produce an IllegalArgumentException!");
274: } catch (IllegalArgumentException iae) {
275: logln("PASS: scriptRun.reset failed as expected");
276: }
277:
278: try {
279: scriptRun.reset(dummy, 0, -100);
280: errln("scriptRun.reset(dummy, 0, -100) did not produce an IllegalArgumentException!");
281: } catch (IllegalArgumentException iae) {
282: logln("PASS: scriptRun.reset failed as expected");
283: }
284:
285: try {
286: scriptRun.reset(dummy, -100, 0);
287: errln("scriptRun.reset(dummy, -100, 0) did not produce an IllegalArgumentException!");
288: } catch (IllegalArgumentException iae) {
289: logln("PASS: scriptRun.reset failed as expected");
290: }
291:
292: try {
293: scriptRun.reset(dummy, 0, dummy.length);
294: } catch (IllegalArgumentException iae) {
295: errln("scriptRun.reset(dummy, 0, dummy.length) produced an IllegalArgumentException!");
296: }
297:
298: try {
299: scriptRun.reset(0, 6);
300: errln("scriptRun.reset(0, 6) did not produce an IllegalArgumentException!");
301: } catch (IllegalArgumentException iae) {
302: logln("PASS: scriptRun.reset failed as expected");
303: }
304:
305: try {
306: scriptRun.reset(6, 0);
307: errln("scriptRun.reset(6, 0) did not produce an IllegalArgumentException!");
308: } catch (IllegalArgumentException iae) {
309: logln("PASS: scriptRun.reset failed as expected");
310: }
311: }
312:
313: public void TestRuns() {
314: for (int i = 0; i < testData.length; i += 1) {
315: RunTestData[] test = testData[i];
316: int stringLimit = 0;
317: int[] runStarts = new int[test.length + 1];
318: String testString = "";
319: UScriptRun scriptRun = null;
320:
321: /*
322: * Fill in the test string and the runStarts array.
323: */
324: for (int run = 0; run < test.length; run += 1) {
325: runStarts[run] = stringLimit;
326: stringLimit += test[run].runText.length();
327: testString += test[run].runText;
328: }
329:
330: /* The limit of the last run */
331: runStarts[test.length] = stringLimit;
332:
333: try {
334: scriptRun = new UScriptRun(testString);
335: CheckScriptRuns(scriptRun, runStarts, test);
336: } catch (IllegalArgumentException iae) {
337: errln("new UScriptRun(testString) produced an IllegalArgumentException!");
338: }
339:
340: try {
341: scriptRun.reset();
342: CheckScriptRuns(scriptRun, runStarts, test);
343: } catch (IllegalArgumentException iae) {
344: errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!");
345: }
346:
347: try {
348: scriptRun = new UScriptRun(testString.toCharArray());
349: CheckScriptRuns(scriptRun, runStarts, test);
350: } catch (IllegalArgumentException iae) {
351: errln("new UScriptRun(testString.toCharArray()) produced an IllegalArgumentException!");
352: }
353:
354: try {
355: scriptRun.reset();
356: CheckScriptRuns(scriptRun, runStarts, test);
357: } catch (IllegalArgumentException iae) {
358: errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!");
359: }
360:
361: try {
362: scriptRun = new UScriptRun();
363:
364: if (scriptRun.next()) {
365: errln("scriptRun.next() on an empty UScriptRun returned true!");
366: }
367: } catch (IllegalArgumentException iae) {
368: errln("new UScriptRun() produced an IllegalArgumentException!");
369: }
370:
371: try {
372: scriptRun.reset(testString, 0, testString.length());
373: CheckScriptRuns(scriptRun, runStarts, test);
374: } catch (IllegalArgumentException iae) {
375: errln("scriptRun.reset(testString, 0, testString.length) produced an IllegalArgumentException!");
376: }
377:
378: try {
379: scriptRun.reset(testString.toCharArray(), 0, testString
380: .length());
381: CheckScriptRuns(scriptRun, runStarts, test);
382: } catch (IllegalArgumentException iae) {
383: errln("scriptRun.reset(testString.toCharArray(), 0, testString.length) produced an IllegalArgumentException!");
384: }
385:
386: String paddedTestString = padding + testString + padding;
387: int startOffset = padding.length();
388: int count = testString.length();
389:
390: for (int run = 0; run < runStarts.length; run += 1) {
391: runStarts[run] += startOffset;
392: }
393:
394: try {
395: scriptRun.reset(paddedTestString, startOffset, count);
396: CheckScriptRuns(scriptRun, runStarts, test);
397: } catch (IllegalArgumentException iae) {
398: errln("scriptRun.reset(paddedTestString, startOffset, count) produced an IllegalArgumentException!");
399: }
400:
401: try {
402: scriptRun.reset(paddedTestString.toCharArray(),
403: startOffset, count);
404: CheckScriptRuns(scriptRun, runStarts, test);
405: } catch (IllegalArgumentException iae) {
406: errln("scriptRun.reset(paddedTestString.toCharArray(), startOffset, count) produced an IllegalArgumentException!");
407: }
408: }
409: }
410: }
|