2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.translit;
\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
17 * @summary Round trip test of Transliterator
\r
19 public class ReplaceableTest extends TestFmwk {
\r
21 public static void main(String[] args) throws Exception {
\r
22 new ReplaceableTest().run(args);
\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
41 void check(String transliteratorName, String test, String shouldProduceStyles) {
\r
42 TestReplaceable tr = new TestReplaceable(test, null);
\r
43 String original = tr.toString();
\r
46 if (transliteratorName.startsWith("*")) {
\r
47 transliteratorName = transliteratorName.substring(1);
\r
48 t = Transliterator.createFromRules("test", transliteratorName,
\r
49 Transliterator.FORWARD);
\r
51 t = Transliterator.getInstance(transliteratorName);
\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
59 logln("OK: " + transliteratorName + " ( " + original + " ) => " + tr.toString());
\r
62 if (!tr.hasMetaData() || tr.chars.hasMetaData()
\r
63 || tr.styles.hasMetaData()) {
\r
64 errln("Fail hasMetaData()");
\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
79 static class TestReplaceable implements Replaceable {
\r
80 ReplaceableString chars;
\r
81 ReplaceableString styles;
\r
83 static final char NO_STYLE = '_';
\r
85 static final char NO_STYLE_MARK = 0xFFFF;
\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
94 if (text.charAt(i) == NO_STYLE_MARK) {
\r
97 s.append((char) (i + '1'));
\r
101 this.styles = new ReplaceableString(s.toString());
\r
104 public String getStyles() {
\r
105 return styles.toString();
\r
108 public String toString() {
\r
109 return chars.toString() + "{" + styles.toString() + "}";
\r
112 public String substring(int start, int limit) {
\r
113 return chars.substring(start, limit);
\r
116 public int length() {
\r
117 return chars.length();
\r
120 public char charAt(int offset) {
\r
121 return chars.charAt(offset);
\r
124 public int char32At(int offset) {
\r
125 return chars.char32At(offset);
\r
128 public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {
\r
129 chars.getChars(srcStart, srcLimit, dst, dstStart);
\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
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
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
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
166 s.append(newStyle);
\r
169 styles.replace(start, limit, s.toString());
\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
177 public boolean hasMetaData() {
\r
181 static final boolean DEBUG = false;
\r
184 public void Test5789() {
\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