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