Source Code Cross Referenced for CollationMonkeyTest.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » collator » 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.collator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *******************************************************************************
003:         * Copyright (C) 2002-2005, International Business Machines Corporation and    *
004:         * others. All Rights Reserved.                                                *
005:         *******************************************************************************
006:         */
007:
008:        /** 
009:         * Port From:   ICU4C v2.1 : collate/CollationMonkeyTest
010:         * Source File: $ICU4CRoot/source/test/intltest/mnkytst.cpp
011:         **/package com.ibm.icu.dev.test.collator;
012:
013:        import com.ibm.icu.dev.test.*;
014:        import com.ibm.icu.text.*;
015:        import java.util.Random;
016:        import java.util.Locale;
017:
018:        /**
019:         * CollationMonkeyTest is a third level test class.  This tests the random 
020:         * substrings of the default test strings to verify if the compare and 
021:         * sort key algorithm works correctly.  For example, any string is always
022:         * less than the string itself appended with any character.
023:         */
024:
025:        public class CollationMonkeyTest extends TestFmwk {
026:
027:            private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
028:
029:            public static void main(String[] args) throws Exception {
030:                new CollationMonkeyTest().run(args);
031:            }
032:
033:            public void TestCollationKey() {
034:                if (source.length() == 0) {
035:                    errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
036:                    return;
037:                }
038:                Collator myCollator;
039:                try {
040:                    myCollator = Collator.getInstance(new Locale("en", "US"));
041:                } catch (Exception e) {
042:                    warnln("ERROR: in creation of collator of ENGLISH locale");
043:                    return;
044:                }
045:
046:                Random rand = createRandom(); // use test framework's random seed
047:                int s = rand.nextInt(0x7fff) % source.length();
048:                int t = rand.nextInt(0x7fff) % source.length();
049:                int slen = Math.abs(rand.nextInt(0x7fff) % source.length()
050:                        - source.length())
051:                        % source.length();
052:                int tlen = Math.abs(rand.nextInt(0x7fff) % source.length()
053:                        - source.length())
054:                        % source.length();
055:                String subs = source.substring(Math.min(s, slen), Math.min(s
056:                        + slen, source.length()));
057:                String subt = source.substring(Math.min(t, tlen), Math.min(t
058:                        + tlen, source.length()));
059:
060:                CollationKey collationKey1, collationKey2;
061:
062:                myCollator.setStrength(Collator.TERTIARY);
063:                collationKey1 = myCollator.getCollationKey(subs);
064:                collationKey2 = myCollator.getCollationKey(subt);
065:                int result = collationKey1.compareTo(collationKey2); // Tertiary
066:                int revResult = collationKey2.compareTo(collationKey1); // Tertiary
067:                report(subs, subt, result, revResult);
068:
069:                myCollator.setStrength(Collator.SECONDARY);
070:                collationKey1 = myCollator.getCollationKey(subs);
071:                collationKey2 = myCollator.getCollationKey(subt);
072:                result = collationKey1.compareTo(collationKey2); // Secondary
073:                revResult = collationKey2.compareTo(collationKey1); // Secondary
074:                report(subs, subt, result, revResult);
075:
076:                myCollator.setStrength(Collator.PRIMARY);
077:                collationKey1 = myCollator.getCollationKey(subs);
078:                collationKey2 = myCollator.getCollationKey(subt);
079:                result = collationKey1.compareTo(collationKey2); // Primary
080:                revResult = collationKey2.compareTo(collationKey1); // Primary
081:                report(subs, subt, result, revResult);
082:
083:                String msg = "";
084:                String addOne = subs + String.valueOf(0xE000);
085:
086:                collationKey1 = myCollator.getCollationKey(subs);
087:                collationKey2 = myCollator.getCollationKey(addOne);
088:                result = collationKey1.compareTo(collationKey2);
089:                if (result != -1) {
090:                    msg += "CollationKey(";
091:                    msg += subs;
092:                    msg += ") .LT. CollationKey(";
093:                    msg += addOne;
094:                    msg += ") Failed.";
095:                    errln(msg);
096:                }
097:
098:                msg = "";
099:                result = collationKey2.compareTo(collationKey1);
100:                if (result != 1) {
101:                    msg += "CollationKey(";
102:                    msg += addOne;
103:                    msg += ") .GT. CollationKey(";
104:                    msg += subs;
105:                    msg += ") Failed.";
106:                    errln(msg);
107:                }
108:            }
109:
110:            // perform monkey tests using Collator.compare
111:            public void TestCompare() {
112:                if (source.length() == 0) {
113:                    errln("CollationMonkeyTest.TestCompare(): source is empty - ICU_DATA not set or data missing?");
114:                    return;
115:                }
116:
117:                Collator myCollator;
118:                try {
119:                    myCollator = Collator.getInstance(new Locale("en", "US"));
120:                } catch (Exception e) {
121:                    warnln("ERROR: in creation of collator of ENGLISH locale");
122:                    return;
123:                }
124:
125:                /* Seed the random-number generator with current time so that
126:                 * the numbers will be different every time we run.
127:                 */
128:
129:                Random rand = createRandom(); // use test framework's random seed
130:                int s = rand.nextInt(0x7fff) % source.length();
131:                int t = rand.nextInt(0x7fff) % source.length();
132:                int slen = Math.abs(rand.nextInt(0x7fff) % source.length()
133:                        - source.length())
134:                        % source.length();
135:                int tlen = Math.abs(rand.nextInt(0x7fff) % source.length()
136:                        - source.length())
137:                        % source.length();
138:                String subs = source.substring(Math.min(s, slen), Math.min(s
139:                        + slen, source.length()));
140:                String subt = source.substring(Math.min(t, tlen), Math.min(t
141:                        + tlen, source.length()));
142:
143:                myCollator.setStrength(Collator.TERTIARY);
144:                int result = myCollator.compare(subs, subt); // Tertiary
145:                int revResult = myCollator.compare(subt, subs); // Tertiary
146:                report(subs, subt, result, revResult);
147:
148:                myCollator.setStrength(Collator.SECONDARY);
149:                result = myCollator.compare(subs, subt); // Secondary
150:                revResult = myCollator.compare(subt, subs); // Secondary
151:                report(subs, subt, result, revResult);
152:
153:                myCollator.setStrength(Collator.PRIMARY);
154:                result = myCollator.compare(subs, subt); // Primary
155:                revResult = myCollator.compare(subt, subs); // Primary
156:                report(subs, subt, result, revResult);
157:
158:                String msg = "";
159:                String addOne = subs + String.valueOf(0xE000);
160:
161:                result = myCollator.compare(subs, addOne);
162:                if (result != -1) {
163:                    msg += "Test : ";
164:                    msg += subs;
165:                    msg += " .LT. ";
166:                    msg += addOne;
167:                    msg += " Failed.";
168:                    errln(msg);
169:                }
170:
171:                msg = "";
172:                result = myCollator.compare(addOne, subs);
173:                if (result != 1) {
174:                    msg += "Test : ";
175:                    msg += addOne;
176:                    msg += " .GT. ";
177:                    msg += subs;
178:                    msg += " Failed.";
179:                    errln(msg);
180:                }
181:            }
182:
183:            void report(String s, String t, int result, int revResult) {
184:                if (revResult != -result) {
185:                    String msg = "";
186:                    msg += s;
187:                    msg += " and ";
188:                    msg += t;
189:                    msg += " round trip comparison failed";
190:                    msg += " (result " + result + ", reverse Result "
191:                            + revResult + ")";
192:                    errln(msg);
193:                }
194:            }
195:
196:            public void TestRules() {
197:                String testSourceCases[] = { "\u0061\u0062\u007a",
198:                        "\u0061\u0062\u007a", };
199:
200:                String testTargetCases[] = { "\u0061\u0062\u00e4",
201:                        "\u0061\u0062\u0061\u0308", };
202:
203:                int i = 0;
204:                logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
205:                Collator col = Collator.getInstance(new Locale("en", "US"));
206:                String baseRules = ((RuleBasedCollator) col).getRules();
207:                String newRules = " & z < ";
208:                newRules = baseRules + newRules + String.valueOf(0x00e4);
209:                RuleBasedCollator myCollation = null;
210:                try {
211:                    myCollation = new RuleBasedCollator(newRules);
212:                } catch (Exception e) {
213:                    warnln("Demo Test 1 Table Collation object creation failed.");
214:                    return;
215:                }
216:
217:                for (i = 0; i < 2; i++) {
218:                    doTest(myCollation, testSourceCases[i], testTargetCases[i],
219:                            -1);
220:                }
221:                logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
222:                newRules = "";
223:                newRules = baseRules + " & z < a" + String.valueOf(0x0308);
224:                try {
225:                    myCollation = new RuleBasedCollator(newRules);
226:                } catch (Exception e) {
227:                    errln("Demo Test 1 Table Collation object creation failed.");
228:                    return;
229:                }
230:                for (i = 0; i < 2; i++) {
231:                    doTest(myCollation, testSourceCases[i], testTargetCases[i],
232:                            -1);
233:                }
234:            }
235:
236:            void doTest(RuleBasedCollator myCollation, String mysource,
237:                    String target, int result) {
238:                int compareResult = myCollation.compare(source, target);
239:                CollationKey sortKey1, sortKey2;
240:
241:                try {
242:                    sortKey1 = myCollation.getCollationKey(source);
243:                    sortKey2 = myCollation.getCollationKey(target);
244:                } catch (Exception e) {
245:                    errln("SortKey generation Failed.\n");
246:                    return;
247:                }
248:                int keyResult = sortKey1.compareTo(sortKey2);
249:                reportCResult(mysource, target, sortKey1, sortKey2,
250:                        compareResult, keyResult, compareResult, result);
251:            }
252:
253:            public void reportCResult(String source, String target,
254:                    CollationKey sourceKey, CollationKey targetKey,
255:                    int compareResult, int keyResult, int incResult,
256:                    int expectedResult) {
257:                if (expectedResult < -1 || expectedResult > 1) {
258:                    errln("***** invalid call to reportCResult ****");
259:                    return;
260:                }
261:                boolean ok1 = (compareResult == expectedResult);
262:                boolean ok2 = (keyResult == expectedResult);
263:                boolean ok3 = (incResult == expectedResult);
264:                if (ok1 && ok2 && ok3 && !isVerbose()) {
265:                    return;
266:                } else {
267:                    String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
268:                    String msg2 = "\", \"";
269:                    String msg3 = "\") returned ";
270:                    String msg4 = "; expected ";
271:                    String sExpect = new String("");
272:                    String sResult = new String("");
273:                    sResult = appendCompareResult(compareResult, sResult);
274:                    sExpect = appendCompareResult(expectedResult, sExpect);
275:                    if (ok1) {
276:                        logln(msg1 + source + msg2 + target + msg3 + sResult);
277:                    } else {
278:                        errln(msg1 + source + msg2 + target + msg3 + sResult
279:                                + msg4 + sExpect);
280:                    }
281:                    msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
282:                    msg2 = "\").compareTo(key(\"";
283:                    msg3 = "\")) returned ";
284:                    sResult = appendCompareResult(keyResult, sResult);
285:                    if (ok2) {
286:                        logln(msg1 + source + msg2 + target + msg3 + sResult);
287:                    } else {
288:                        errln(msg1 + source + msg2 + target + msg3 + sResult
289:                                + msg4 + sExpect);
290:                        msg1 = "  ";
291:                        msg2 = " vs. ";
292:                        errln(msg1 + prettify(sourceKey) + msg2
293:                                + prettify(targetKey));
294:                    }
295:                    msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
296:                    msg2 = "\", \"";
297:                    msg3 = "\") returned ";
298:                    sResult = appendCompareResult(incResult, sResult);
299:                    if (ok3) {
300:                        logln(msg1 + source + msg2 + target + msg3 + sResult);
301:                    } else {
302:                        errln(msg1 + source + msg2 + target + msg3 + sResult
303:                                + msg4 + sExpect);
304:                    }
305:                }
306:            }
307:
308:            String appendCompareResult(int result, String target) {
309:                if (result == -1) { //LESS
310:                    target += "LESS";
311:                } else if (result == 0) { //EQUAL
312:                    target += "EQUAL";
313:                } else if (result == 1) { //GREATER
314:                    target += "GREATER";
315:                } else {
316:                    String huh = "?";
317:                    target += huh + result;
318:                }
319:                return target;
320:            }
321:
322:            String prettify(CollationKey sourceKey) {
323:                int i;
324:                byte[] bytes = sourceKey.toByteArray();
325:                String target = "[";
326:
327:                for (i = 0; i < bytes.length; i++) {
328:                    target += Integer.toHexString(bytes[i]);
329:                    target += " ";
330:                }
331:                target += "]";
332:                return target;
333:            }
334:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.