Source Code Cross Referenced for TestUCharacterIterator.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » iterator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.dev.test.iterator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *******************************************************************************
003:         * Copyright (C) 2001-2006, International Business Machines Corporation and    *
004:         * others. All Rights Reserved.                                                *
005:         *******************************************************************************
006:         */package com.ibm.icu.dev.test.iterator;
007:
008:        import com.ibm.icu.dev.test.TestFmwk;
009:        import com.ibm.icu.text.UCharacterIterator;
010:        import com.ibm.icu.text.UTF16;
011:        import com.ibm.icu.text.ReplaceableString;
012:        import java.text.CharacterIterator;
013:        import java.text.StringCharacterIterator;
014:
015:        /**
016:         * @author ram
017:         *
018:         * To change this generated comment edit the template variable "typecomment":
019:         * Window>Preferences>Java>Templates.
020:         * To enable and disable the creation of type comments go to
021:         * Window>Preferences>Java>Code Generation.
022:         */
023:        public class TestUCharacterIterator extends TestFmwk {
024:
025:            // constructor -----------------------------------------------------
026:
027:            /**
028:             * Constructor
029:             */
030:            public TestUCharacterIterator() {
031:            }
032:
033:            // public methods --------------------------------------------------
034:
035:            /**
036:             * Testing cloning
037:             */
038:            public void TestClone() throws CloneNotSupportedException {
039:                UCharacterIterator iterator = UCharacterIterator
040:                        .getInstance("testing");
041:                UCharacterIterator cloned = (UCharacterIterator) iterator
042:                        .clone();
043:                int completed = 0;
044:                while (completed != UCharacterIterator.DONE) {
045:                    completed = iterator.next();
046:                    if (completed != cloned.next()) {
047:                        errln("Cloned operation failed");
048:                    }
049:                }
050:            }
051:
052:            public void getText(UCharacterIterator iterator, String result) {
053:                /* test getText */
054:                char[] buf = new char[1];
055:                for (;;) {
056:                    try {
057:                        iterator.getText(buf);
058:                        break;
059:                    } catch (IndexOutOfBoundsException e) {
060:                        buf = new char[iterator.getLength()];
061:                    }
062:                }
063:                if (result.compareTo(new String(buf, 0, iterator.getLength())) != 0) {
064:                    errln("getText failed for iterator");
065:                }
066:            }
067:
068:            /**
069:             * Testing iteration
070:             */
071:            public void TestIteration() {
072:                UCharacterIterator iterator = UCharacterIterator
073:                        .getInstance(ITERATION_STRING_);
074:                UCharacterIterator iterator2 = UCharacterIterator
075:                        .getInstance(ITERATION_STRING_);
076:                iterator.setToStart();
077:                if (iterator.current() != ITERATION_STRING_.charAt(0)) {
078:                    errln("Iterator failed retrieving first character");
079:                }
080:                iterator.setToLimit();
081:                if (iterator.previous() != ITERATION_STRING_
082:                        .charAt(ITERATION_STRING_.length() - 1)) {
083:                    errln("Iterator failed retrieving last character");
084:                }
085:                if (iterator.getLength() != ITERATION_STRING_.length()) {
086:                    errln("Iterator failed determining begin and end index");
087:                }
088:                iterator2.setIndex(0);
089:                iterator.setIndex(0);
090:                int ch = 0;
091:                while (ch != UCharacterIterator.DONE) {
092:                    int index = iterator2.getIndex();
093:                    ch = iterator2.nextCodePoint();
094:                    if (index != ITERATION_SUPPLEMENTARY_INDEX) {
095:                        if (ch != (int) iterator.next()
096:                                && ch != UCharacterIterator.DONE) {
097:                            errln("Error mismatch in next() and nextCodePoint()");
098:                        }
099:                    } else {
100:                        if (UTF16.getLeadSurrogate(ch) != iterator.next()
101:                                || UTF16.getTrailSurrogate(ch) != iterator
102:                                        .next()) {
103:                            errln("Error mismatch in next and nextCodePoint for "
104:                                    + "supplementary characters");
105:                        }
106:                    }
107:                }
108:                iterator.setIndex(ITERATION_STRING_.length());
109:                iterator2.setIndex(ITERATION_STRING_.length());
110:                while (ch != UCharacterIterator.DONE) {
111:                    int index = iterator2.getIndex();
112:                    ch = iterator2.previousCodePoint();
113:                    if (index != ITERATION_SUPPLEMENTARY_INDEX) {
114:                        if (ch != (int) iterator.previous()
115:                                && ch != UCharacterIterator.DONE) {
116:                            errln("Error mismatch in previous() and "
117:                                    + "previousCodePoint()");
118:                        }
119:                    } else {
120:                        if (UTF16.getLeadSurrogate(ch) != iterator.previous()
121:                                || UTF16.getTrailSurrogate(ch) != iterator
122:                                        .previous()) {
123:                            errln("Error mismatch in previous and "
124:                                    + "previousCodePoint for supplementary characters");
125:                        }
126:                    }
127:                }
128:            }
129:
130:            public static void main(String[] arg) {
131:                try {
132:                    TestUCharacterIterator test = new TestUCharacterIterator();
133:                    test.run(arg);
134:                } catch (Exception e) {
135:                    e.printStackTrace();
136:                }
137:            }
138:
139:            //Tests for new API for utf-16 support 
140:            public void TestIterationUChar32() {
141:                String text = "\u0061\u0062\ud841\udc02\u20ac\ud7ff\ud842\udc06\ud801\udc00\u0061";
142:                int c;
143:                int i;
144:                {
145:                    UCharacterIterator iter = UCharacterIterator
146:                            .getInstance(text);
147:
148:                    String iterText = iter.getText();
149:                    if (!iterText.equals(text))
150:                        errln("iter.getText() failed");
151:
152:                    iter.setIndex(1);
153:                    if (iter.currentCodePoint() != UTF16.charAt(text, 1))
154:                        errln("Iterator didn't start out in the right place.");
155:
156:                    iter.setToStart();
157:                    c = iter.currentCodePoint();
158:                    i = 0;
159:                    i = iter.moveCodePointIndex(1);
160:                    c = iter.currentCodePoint();
161:                    if (c != UTF16.charAt(text, 1) || i != 1)
162:                        errln("moveCodePointIndex(1) didn't work correctly expected "
163:                                + hex(c)
164:                                + " got "
165:                                + hex(UTF16.charAt(text, 1))
166:                                + " i= " + i);
167:
168:                    i = iter.moveCodePointIndex(2);
169:                    c = iter.currentCodePoint();
170:                    if (c != UTF16.charAt(text, 4) || i != 4)
171:                        errln("moveCodePointIndex(2) didn't work correctly expected "
172:                                + hex(c)
173:                                + " got "
174:                                + hex(UTF16.charAt(text, 4))
175:                                + " i= " + i);
176:
177:                    i = iter.moveCodePointIndex(-2);
178:                    c = iter.currentCodePoint();
179:                    if (c != UTF16.charAt(text, 1) || i != 1)
180:                        errln("moveCodePointIndex(-2) didn't work correctly expected "
181:                                + hex(c)
182:                                + " got "
183:                                + hex(UTF16.charAt(text, 1))
184:                                + " i= " + i);
185:
186:                    iter.setToLimit();
187:                    i = iter.moveCodePointIndex(-2);
188:                    c = iter.currentCodePoint();
189:                    if (c != UTF16.charAt(text, (text.length() - 3))
190:                            || i != (text.length() - 3))
191:                        errln("moveCodePointIndex(-2) didn't work correctly expected "
192:                                + hex(c)
193:                                + " got "
194:                                + hex(UTF16.charAt(text, (text.length() - 3)))
195:                                + " i= " + i);
196:
197:                    iter.setToStart();
198:                    c = iter.currentCodePoint();
199:                    i = 0;
200:
201:                    //testing first32PostInc, nextCodePointPostInc, setTostart
202:                    i = 0;
203:                    iter.setToStart();
204:                    c = iter.next();
205:                    if (c != UTF16.charAt(text, i))
206:                        errln("first32PostInc failed.  Expected->"
207:                                + hex(UTF16.charAt(text, i)) + " Got-> "
208:                                + hex(c));
209:                    if (iter.getIndex() != UTF16.getCharCount(c) + i)
210:                        errln("getIndex() after first32PostInc() failed");
211:
212:                    iter.setToStart();
213:                    i = 0;
214:                    if (iter.getIndex() != 0)
215:                        errln("setToStart failed");
216:
217:                    logln("Testing forward iteration...");
218:                    do {
219:                        if (c != UCharacterIterator.DONE)
220:                            c = iter.nextCodePoint();
221:
222:                        if (c != UTF16.charAt(text, i))
223:                            errln("Character mismatch at position " + i
224:                                    + ", iterator has " + hex(c)
225:                                    + ", string has "
226:                                    + hex(UTF16.charAt(text, i)));
227:
228:                        i += UTF16.getCharCount(c);
229:                        if (iter.getIndex() != i)
230:                            errln("getIndex() aftr nextCodePointPostInc() isn't working right");
231:                        c = iter.currentCodePoint();
232:                        if (c != UCharacterIterator.DONE
233:                                && c != UTF16.charAt(text, i))
234:                            errln("current() after nextCodePointPostInc() isn't working right");
235:
236:                    } while (c != UCharacterIterator.DONE);
237:                    c = iter.nextCodePoint();
238:                    if (c != UCharacterIterator.DONE)
239:                        errln("nextCodePointPostInc() didn't return DONE at the beginning");
240:
241:                }
242:            }
243:
244:            class UCharIterator {
245:
246:                public UCharIterator(int[] src, int len, int index) {
247:
248:                    s = src;
249:                    length = len;
250:                    i = index;
251:                }
252:
253:                public int current() {
254:                    if (i < length) {
255:                        return s[i];
256:                    } else {
257:                        return -1;
258:                    }
259:                }
260:
261:                public int next() {
262:                    if (i < length) {
263:                        return s[i++];
264:                    } else {
265:                        return -1;
266:                    }
267:                }
268:
269:                public int previous() {
270:                    if (i > 0) {
271:                        return s[--i];
272:                    } else {
273:                        return -1;
274:                    }
275:                }
276:
277:                public int getIndex() {
278:                    return i;
279:                }
280:
281:                private int[] s;
282:                private int length, i;
283:            }
284:
285:            // src and expect strings
286:            private final char src[] = { UTF16.getLeadSurrogate(0x2f999),
287:                    UTF16.getTrailSurrogate(0x2f999),
288:                    UTF16.getLeadSurrogate(0x1d15f),
289:                    UTF16.getTrailSurrogate(0x1d15f), 0xc4, 0x1ed0 };
290:
291:            public void TestPreviousNext() {
292:                // iterators
293:                UCharacterIterator iter1 = UCharacterIterator
294:                        .getInstance(new ReplaceableString(new String(src)));
295:                UCharacterIterator iter2 = UCharacterIterator
296:                        .getInstance(src/*char array*/);
297:                UCharacterIterator iter3 = UCharacterIterator
298:                        .getInstance(new StringCharacterIterator(
299:                                new String(src)));
300:                UCharacterIterator iter4 = UCharacterIterator
301:                        .getInstance(new StringBuffer(new String(src)));
302:                previousNext(iter1);
303:                previousNext(iter2);
304:                previousNext(iter3);
305:                previousNext(iter4);
306:                getText(iter1, new String(src));
307:                getText(iter2, new String(src));
308:                getText(iter3, new String(src));
309:                /* getCharacterIterator */
310:                CharacterIterator citer1 = iter1.getCharacterIterator();
311:                CharacterIterator citer2 = iter2.getCharacterIterator();
312:                CharacterIterator citer3 = iter3.getCharacterIterator();
313:                if (citer1.first() != iter1.current()) {
314:                    errln("getCharacterIterator for iter1 failed");
315:                }
316:                if (citer2.first() != iter2.current()) {
317:                    errln("getCharacterIterator for iter2 failed");
318:                }
319:                if (citer3.first() != iter3.current()) {
320:                    errln("getCharacterIterator for iter3 failed");
321:                }
322:                /* Test clone()  && moveIndex()*/
323:                try {
324:                    UCharacterIterator clone1 = (UCharacterIterator) iter1
325:                            .clone();
326:                    UCharacterIterator clone2 = (UCharacterIterator) iter2
327:                            .clone();
328:                    UCharacterIterator clone3 = (UCharacterIterator) iter3
329:                            .clone();
330:                    if (clone1.moveIndex(3) != iter1.moveIndex(3)) {
331:                        errln("moveIndex for iter1 failed");
332:                    }
333:                    if (clone2.moveIndex(3) != iter2.moveIndex(3)) {
334:                        errln("moveIndex for iter2 failed");
335:                    }
336:                    if (clone3.moveIndex(3) != iter3.moveIndex(3)) {
337:                        errln("moveIndex for iter1 failed");
338:                    }
339:                } catch (Exception e) {
340:                    errln("could not clone the iterator");
341:                }
342:            }
343:
344:            public void previousNext(UCharacterIterator iter) {
345:
346:                int expect[] = { 0x2f999, 0x1d15f, 0xc4, 0x1ed0 };
347:
348:                // expected src indexes corresponding to expect indexes
349:                int expectIndex[] = { 0, 0, 1, 1, 2, 3, 4 //needed 
350:                };
351:
352:                // initial indexes into the src and expect strings
353:
354:                final int SRC_MIDDLE = 4;
355:                final int EXPECT_MIDDLE = 2;
356:
357:                // movement vector
358:                // - for previous(), 0 for current(), + for next()
359:                // not const so that we can terminate it below for the error message
360:                String moves = "0+0+0--0-0-+++0--+++++++0--------";
361:
362:                UCharIterator iter32 = new UCharIterator(expect, expect.length,
363:                        EXPECT_MIDDLE);
364:
365:                int c1, c2;
366:                char m;
367:
368:                // initially set the indexes into the middle of the strings
369:                iter.setIndex(SRC_MIDDLE);
370:
371:                // move around and compare the iteration code points with
372:                // the expected ones
373:                int movesIndex = 0;
374:                while (movesIndex < moves.length()) {
375:                    m = moves.charAt(movesIndex++);
376:                    if (m == '-') {
377:                        c1 = iter.previousCodePoint();
378:                        c2 = iter32.previous();
379:                    } else if (m == '0') {
380:                        c1 = iter.currentCodePoint();
381:                        c2 = iter32.current();
382:                    } else {// m=='+' 
383:                        c1 = iter.nextCodePoint();
384:                        c2 = iter32.next();
385:                    }
386:
387:                    // compare results
388:                    if (c1 != c2) {
389:                        // copy the moves until the current (m) move, and terminate
390:                        String history = moves.substring(0, movesIndex);
391:                        errln("error: mismatch in Normalizer iteration at "
392:                                + history + ": " + "got c1= " + hex(c1)
393:                                + " != expected c2= " + hex(c2));
394:                        break;
395:                    }
396:
397:                    // compare indexes
398:                    if (expectIndex[iter.getIndex()] != iter32.getIndex()) {
399:                        // copy the moves until the current (m) move, and terminate
400:                        String history = moves.substring(0, movesIndex);
401:                        errln("error: index mismatch in Normalizer iteration at "
402:                                + history
403:                                + " : "
404:                                + "Normalizer index "
405:                                + iter.getIndex()
406:                                + " expected "
407:                                + expectIndex[iter32.getIndex()]);
408:                        break;
409:                    }
410:                }
411:            }
412:
413:            public void TestUCharacterIteratorWrapper() {
414:                String source = "asdfasdfjoiuyoiuy2341235679886765";
415:                UCharacterIterator it = UCharacterIterator.getInstance(source);
416:                CharacterIterator wrap_ci = it.getCharacterIterator();
417:                CharacterIterator ci = new StringCharacterIterator(source);
418:                wrap_ci.setIndex(10);
419:                ci.setIndex(10);
420:                String moves = "0+0+0--0-0-+++0--+++++++0--------++++0000----0-";
421:                int c1, c2;
422:                char m;
423:                int movesIndex = 0;
424:
425:                while (movesIndex < moves.length()) {
426:                    m = moves.charAt(movesIndex++);
427:                    if (m == '-') {
428:                        c1 = wrap_ci.previous();
429:                        c2 = ci.previous();
430:                    } else if (m == '0') {
431:                        c1 = wrap_ci.current();
432:                        c2 = ci.current();
433:                    } else {// m=='+' 
434:                        c1 = wrap_ci.next();
435:                        c2 = ci.next();
436:                    }
437:
438:                    // compare results
439:                    if (c1 != c2) {
440:                        // copy the moves until the current (m) move, and terminate
441:                        String history = moves.substring(0, movesIndex);
442:                        errln("error: mismatch in Normalizer iteration at "
443:                                + history + ": " + "got c1= " + hex(c1)
444:                                + " != expected c2= " + hex(c2));
445:                        break;
446:                    }
447:
448:                    // compare indexes
449:                    if (wrap_ci.getIndex() != ci.getIndex()) {
450:                        // copy the moves until the current (m) move, and terminate
451:                        String history = moves.substring(0, movesIndex);
452:                        errln("error: index mismatch in Normalizer iteration at "
453:                                + history
454:                                + " : "
455:                                + "Normalizer index "
456:                                + wrap_ci.getIndex()
457:                                + " expected "
458:                                + ci.getIndex());
459:                        break;
460:                    }
461:                }
462:                if (ci.first() != wrap_ci.first()) {
463:                    errln("CharacterIteratorWrapper.first() failed. expected: "
464:                            + ci.first() + " got: " + wrap_ci.first());
465:                }
466:                if (ci.last() != wrap_ci.last()) {
467:                    errln("CharacterIteratorWrapper.last() failed expected: "
468:                            + ci.last() + " got: " + wrap_ci.last());
469:                }
470:                if (ci.getBeginIndex() != wrap_ci.getBeginIndex()) {
471:                    errln("CharacterIteratorWrapper.getBeginIndex() failed expected: "
472:                            + ci.getBeginIndex()
473:                            + " got: "
474:                            + wrap_ci.getBeginIndex());
475:                }
476:                if (ci.getEndIndex() != wrap_ci.getEndIndex()) {
477:                    errln("CharacterIteratorWrapper.getEndIndex() failed expected: "
478:                            + ci.getEndIndex()
479:                            + " got: "
480:                            + wrap_ci.getEndIndex());
481:                }
482:                try {
483:                    CharacterIterator cloneWCI = (CharacterIterator) wrap_ci
484:                            .clone();
485:                    if (wrap_ci.getIndex() != cloneWCI.getIndex()) {
486:                        errln("CharacterIteratorWrapper.clone() failed expected: "
487:                                + wrap_ci.getIndex()
488:                                + " got: "
489:                                + cloneWCI.getIndex());
490:                    }
491:                } catch (Exception e) {
492:                    errln("CharacterIterator.clone() failed");
493:                }
494:            }
495:
496:            // private data members ---------------------------------------------
497:
498:            private static final String ITERATION_STRING_ = "Testing 1 2 3 \ud800\udc00 456";
499:            private static final int ITERATION_SUPPLEMENTARY_INDEX = 14;
500:
501:            public void TestJitterbug1952() {
502:                //test previous code point
503:                char[] src = new char[] { '\uDC00', '\uD800', '\uDC01',
504:                        '\uD802', '\uDC02', '\uDC03' };
505:                UCharacterIterator iter = UCharacterIterator.getInstance(src);
506:                iter.setIndex(1);
507:                int ch;
508:                // this should never go into a infinite loop
509:                // if it does then we have a problem
510:                while ((ch = iter.previousCodePoint()) != UCharacterIterator.DONE) {
511:                    if (ch != 0xDc00) {
512:                        errln("iter.previousCodePoint() failed");
513:                    }
514:                }
515:                iter.setIndex(5);
516:                while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
517:                    if (ch != 0xDC03) {
518:                        errln("iter.nextCodePoint() failed");
519:                    }
520:                }
521:            }
522:
523:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.