]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/translit/src/com/ibm/icu/dev/test/translit/ReplaceableTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / translit / src / com / ibm / icu / dev / test / translit / ReplaceableTest.java
1 /**\r
2  *******************************************************************************\r
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.translit;\r
8 \r
9 import com.ibm.icu.dev.test.TestFmwk;\r
10 import com.ibm.icu.impl.Utility;\r
11 import com.ibm.icu.text.Replaceable;\r
12 import com.ibm.icu.text.ReplaceableString;\r
13 import com.ibm.icu.text.Transliterator;\r
14 \r
15 /**\r
16  * @test\r
17  * @summary Round trip test of Transliterator\r
18  */\r
19 public class ReplaceableTest extends TestFmwk {\r
20     \r
21     public static void main(String[] args) throws Exception {\r
22         new ReplaceableTest().run(args);\r
23     }\r
24   \r
25     public void Test() {\r
26         check("Lower", "ABCD", "1234");\r
27         check("Upper", "abcd\u00DF", "123455"); // must map 00DF to SS\r
28         check("Title", "aBCD", "1234");\r
29         check("NFC", "A\u0300E\u0300", "13");\r
30         check("NFD", "\u00C0\u00C8", "1122");\r
31         check("*(x) > A $1 B", "wxy", "11223");\r
32         check("*(x)(y) > A $2 B $1 C $2 D", "wxyz", "113322334");\r
33         check("*(x)(y)(z) > A $3 B $2 C $1 D", "wxyzu", "114433225");\r
34         // TODO Revisit the following in 2.6 or later.\r
35         check("*x > a", "xyz", "223"); // expect "123"?\r
36         check("*x > a", "wxy", "113"); // expect "123"?\r
37         check("*x > a", "\uFFFFxy", "_33"); // expect "_23"?\r
38         check("*(x) > A $1 B", "\uFFFFxy", "__223");\r
39     }\r
40     \r
41     void check(String transliteratorName, String test, String shouldProduceStyles) {\r
42         TestReplaceable tr = new TestReplaceable(test, null);\r
43         String original = tr.toString();\r
44         \r
45         Transliterator t;\r
46         if (transliteratorName.startsWith("*")) {\r
47             transliteratorName = transliteratorName.substring(1);\r
48             t = Transliterator.createFromRules("test", transliteratorName,\r
49                                                Transliterator.FORWARD);\r
50         } else {\r
51             t = Transliterator.getInstance(transliteratorName);\r
52         }\r
53         t.transliterate(tr);\r
54         String newStyles = tr.getStyles();\r
55         if (!newStyles.equals(shouldProduceStyles)) {\r
56             errln("FAIL Styles: " + transliteratorName + " ( "\r
57                 + original + " ) => " + tr.toString() + "; should be {" + shouldProduceStyles + "}!");\r
58         } else {\r
59             logln("OK: " + transliteratorName + " ( " + original + " ) => " + tr.toString());\r
60         }\r
61         \r
62         if (!tr.hasMetaData() || tr.chars.hasMetaData() \r
63             || tr.styles.hasMetaData()) {\r
64             errln("Fail hasMetaData()");\r
65         }\r
66     }\r
67     \r
68 \r
69     /**\r
70      * This is a test class that simulates styled text.\r
71      * It associates a style number (0..65535) with each character,\r
72      * and maintains that style in the normal fashion:\r
73      * When setting text from raw string or characters,<br>\r
74      * Set the styles to the style of the first character replaced.<br>\r
75      * If no characters are replaced, use the style of the previous character.<br>\r
76      * If at start, use the following character<br>\r
77      * Otherwise use NO_STYLE.\r
78      */\r
79     static class TestReplaceable implements Replaceable {\r
80         ReplaceableString chars;\r
81         ReplaceableString styles;\r
82         \r
83         static final char NO_STYLE = '_';\r
84 \r
85         static final char NO_STYLE_MARK = 0xFFFF;\r
86         \r
87         TestReplaceable (String text, String styles) {\r
88             chars = new ReplaceableString(text);\r
89             StringBuffer s = new StringBuffer();\r
90             for (int i = 0; i < text.length(); ++i) {\r
91                 if (styles != null && i < styles.length()) {\r
92                     s.append(styles.charAt(i));\r
93                 } else {\r
94                     if (text.charAt(i) == NO_STYLE_MARK) {\r
95                         s.append(NO_STYLE);\r
96                     } else {\r
97                         s.append((char) (i + '1'));\r
98                     }\r
99                 }\r
100             }\r
101             this.styles = new ReplaceableString(s.toString());\r
102         }\r
103         \r
104         public String getStyles() {\r
105             return styles.toString();\r
106         }\r
107         \r
108         public String toString() {\r
109             return chars.toString() + "{" + styles.toString() + "}";\r
110         }\r
111 \r
112         public String substring(int start, int limit) {\r
113             return chars.substring(start, limit);\r
114         }\r
115 \r
116         public int length() {\r
117             return chars.length();\r
118         }\r
119 \r
120         public char charAt(int offset) {\r
121             return chars.charAt(offset);\r
122         }\r
123 \r
124         public int char32At(int offset) {\r
125             return chars.char32At(offset);\r
126         }\r
127 \r
128         public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {\r
129             chars.getChars(srcStart, srcLimit, dst, dstStart);\r
130         }\r
131 \r
132         public void replace(int start, int limit, String text) {\r
133             if (substring(start,limit).equals(text)) return; // NO ACTION!\r
134             if (DEBUG) System.out.print(Utility.escape(toString() + " -> replace(" + start +\r
135                                             "," + limit + "," + text) + ") -> ");\r
136             chars.replace(start, limit, text);\r
137             fixStyles(start, limit, text.length());\r
138             if (DEBUG) System.out.println(Utility.escape(toString()));\r
139         }\r
140         \r
141         public void replace(int start, int limit, char[] charArray,\r
142                             int charsStart, int charsLen) {\r
143             if (substring(start,limit).equals(new String(charArray, charsStart, charsLen-charsStart))) return; // NO ACTION!\r
144             this.chars.replace(start, limit, charArray, charsStart, charsLen);\r
145             fixStyles(start, limit, charsLen);\r
146         }\r
147 \r
148         void fixStyles(int start, int limit, int newLen) {\r
149             char newStyle = NO_STYLE;\r
150             if (start != limit && styles.charAt(start) != NO_STYLE) {\r
151                 newStyle = styles.charAt(start);\r
152             } else if (start > 0 && charAt(start-1) != NO_STYLE_MARK) {\r
153                 newStyle = styles.charAt(start-1);\r
154             } else if (limit < styles.length()) {\r
155                 newStyle = styles.charAt(limit);\r
156             }\r
157             // dumb implementation for now.\r
158             StringBuffer s = new StringBuffer();\r
159             for (int i = 0; i < newLen; ++i) {\r
160                 // this doesn't really handle an embedded NO_STYLE_MARK\r
161                 // in the middle of a long run of characters right -- but\r
162                 // that case shouldn't happen anyway\r
163                 if (charAt(start+i) == NO_STYLE_MARK) {\r
164                     s.append(NO_STYLE);\r
165                 } else {\r
166                     s.append(newStyle);\r
167                 }\r
168             }\r
169             styles.replace(start, limit, s.toString());\r
170         }\r
171 \r
172         public void copy(int start, int limit, int dest) {\r
173             chars.copy(start, limit, dest);\r
174             styles.copy(start, limit, dest);\r
175         }\r
176         \r
177         public boolean hasMetaData() {\r
178             return true;\r
179         }\r
180 \r
181         static final boolean DEBUG = false;\r
182     }\r
183     \r
184     public void Test5789() {\r
185         String rules =\r
186             "IETR > IET | \\' R; # (1) do split ietr between t and r\r\n" +\r
187             "I[EH] > I; # (2) friedrich";\r
188         Transliterator trans = Transliterator.createFromRules("foo", rules, Transliterator.FORWARD);\r
189         String result =  trans.transliterate("BLENKDIETRICH");\r
190         assertEquals("Rule breakage", "BLENKDIET'RICH", result);\r
191     }\r
192 }\r