]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/bidi/TestCompatibility.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / bidi / TestCompatibility.java
1 //##header J2SE15
2 //#if defined(FOUNDATION10) || defined(J2SE13)
3 //#else
4 /*
5 *******************************************************************************
6 *   Copyright (C) 2007-2009, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *******************************************************************************
9 */
10
11 package com.ibm.icu.dev.test.bidi;
12
13 import com.ibm.icu.text.Bidi;
14 import java.text.AttributedString;
15 import java.awt.font.TextAttribute;
16 import java.text.AttributedCharacterIterator;
17 import java.awt.font.NumericShaper;
18
19 /**
20  * Regression test for java.text.Bidi compatibility
21  *
22  * @author Matitiahu Allouche
23  */
24
25 public class TestCompatibility extends BidiTest {
26
27     void compareBidi(Bidi bidi, java.text.Bidi jbidi)
28     {
29         byte paraLevel = bidi.getParaLevel();
30         if (bidi.baseIsLeftToRight() != jbidi.baseIsLeftToRight()) {
31             errln("Discrepancy in baseIsLeftToRight for case " +
32                   "(level=" + paraLevel + "): " +
33                   u16ToPseudo(bidi.getTextAsString()) +
34                   "\n    bidi: " + bidi.baseIsLeftToRight() +
35                   "\n   jbidi: " + jbidi.baseIsLeftToRight());
36         }
37         if (bidi.getBaseLevel() != jbidi.getBaseLevel()) {
38             errln("Discrepancy in getBaseLevel for case " +
39                   "(level=" + paraLevel + "): " +
40                   u16ToPseudo(bidi.getTextAsString()) +
41                   "\n    bidi: " + bidi.getBaseLevel() +
42                   "\n   jbidi: " + jbidi.getBaseLevel());
43         }
44         if (bidi.getLength() != jbidi.getLength()) {
45             errln("Discrepancy in getLength for case " +
46                   "(level=" + paraLevel + "): " +
47                   u16ToPseudo(bidi.getTextAsString()) +
48                   "\n    bidi: " + bidi.getLength() +
49                   "\n   jbidi: " + jbidi.getLength());
50         }
51         int len = bidi.getLength();
52         for (int i = 0; i < len; i++) {
53             if (bidi.getLevelAt(i) != jbidi.getLevelAt(i)) {
54                 errln("Discrepancy in getLevelAt for offset " + i +
55                       " of case " +
56                       "(level=" + paraLevel + "): " +
57                       u16ToPseudo(bidi.getTextAsString()) +
58                       "\n    bidi: " + bidi.getLevelAt(i) +
59                       "\n   jbidi: " + jbidi.getLevelAt(i));
60             }
61         }
62         if (bidi.getRunCount() != jbidi.getRunCount()) {
63             if (!(len == 0 && jbidi.getRunCount() == 1)) {
64                 errln("Discrepancy in getRunCount for case " +
65                       "(level=" + paraLevel + "): " +
66                       u16ToPseudo(bidi.getTextAsString()) +
67                       "\n    bidi: " + bidi.getRunCount() +
68                       "\n   jbidi: " + jbidi.getRunCount());
69             }
70         }
71         int runCount = bidi.getRunCount();
72         for (int i = 0; i < runCount; i++) {
73             if (bidi.getRunLevel(i) != jbidi.getRunLevel(i)) {
74                 errln("Discrepancy in getRunLevel for run " + i +
75                       " of case " +
76                       "(level=" + paraLevel + "): " +
77                       u16ToPseudo(bidi.getTextAsString()) +
78                       "\n    bidi: " + bidi.getRunLevel(i) +
79                       "\n   jbidi: " + jbidi.getRunLevel(i));
80             }
81             if (bidi.getRunLimit(i) != jbidi.getRunLimit(i)) {
82                 errln("Discrepancy in getRunLimit for run " + i +
83                       " of case " +
84                       "(level=" + paraLevel + "): " +
85                       u16ToPseudo(bidi.getTextAsString()) +
86                       "\n    bidi: " + bidi.getRunLimit(i) +
87                       "\n   jbidi: " + jbidi.getRunLimit(i));
88             }
89             if (bidi.getRunStart(i) != jbidi.getRunStart(i)) {
90                 errln("Discrepancy in getRunStart for run " + i +
91                       " of case " +
92                       "(level=" + paraLevel + "): " +
93                       u16ToPseudo(bidi.getTextAsString()) +
94                       "\n    bidi: " + bidi.getRunStart(i) +
95                       "\n   jbidi: " + jbidi.getRunStart(i));
96             }
97         }
98         if (bidi.isLeftToRight() != jbidi.isLeftToRight()) {
99             errln("Discrepancy in isLeftToRight for case " +
100                   "(level=" + paraLevel + "): " +
101                   u16ToPseudo(bidi.getTextAsString()) +
102                   "\n    bidi: " + bidi.isLeftToRight() +
103                   "\n   jbidi: " + jbidi.isLeftToRight());
104         }
105         if (bidi.isMixed() != jbidi.isMixed()) {
106             errln("Discrepancy in isMixed for case " +
107                   "(level=" + paraLevel + "): " +
108                   u16ToPseudo(bidi.getTextAsString()) +
109                   "\n    bidi: " + bidi.isMixed() +
110                   "\n   jbidi: " + jbidi.isMixed());
111         }
112         if (bidi.isRightToLeft() != jbidi.isRightToLeft()) {
113             errln("Discrepancy in isRightToLeft for case " +
114                   "(level=" + paraLevel + "): " +
115                   u16ToPseudo(bidi.getTextAsString()) +
116                   "\n    bidi: " + bidi.isRightToLeft() +
117                   "\n   jbidi: " + jbidi.isRightToLeft());
118         }
119         char[] text = bidi.getText();
120         if (Bidi.requiresBidi(text, 0, text.length) !=
121             java.text.Bidi.requiresBidi(text, 0, text.length)) {
122             errln("Discrepancy in requiresBidi for case " +
123                   u16ToPseudo(bidi.getTextAsString()) +
124                   "\n    bidi: " + Bidi.requiresBidi(text, 0, text.length) +
125                   "\n   jbidi: " + java.text.Bidi.requiresBidi(text, 0, text.length));
126         }
127         /* skip the next test, since the toString implementation are
128          * not compatible
129         if (!bidi.toString().equals(jbidi.toString())) {
130             errln("Discrepancy in toString for case " +
131                   "(level=" + paraLevel + "): " +
132                   u16ToPseudo(bidi.getTextAsString() +
133                   "\n    bidi: " + bidi.toString() +
134                   "\n   jbidi: " + jbidi.toString()));
135         }
136          */
137     }
138
139     public void testCompatibility()
140     {
141         // This test case does not work well on Java 1.4/1.4.1 environment,
142         // because of insufficient Bidi implementation in these versions.
143         String javaVersion = System.getProperty("java.version");
144         if (javaVersion.startsWith("1.4.0") || javaVersion.startsWith("1.4.1")) {
145             logln("\nSkipping TestCompatibility.  The test case is known to fail on Java "
146                     + javaVersion + "\n");
147             return;
148         }
149         logln("\nEntering TestCompatibility\n");
150         /* check constant field values */
151         int val;
152         val = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
153         val = Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT;
154         val = Bidi.DIRECTION_LEFT_TO_RIGHT;
155         val = Bidi.DIRECTION_RIGHT_TO_LEFT;
156         logln("last val = " + val);
157
158         String[] data = {"",
159                          /* the following 2 cases are skipped, because
160                           * java.text.Bidi has bugs which cause discrepancies
161                          "abc",
162                          "ABC",
163                           */
164                          "abc def",
165                          "ABC DEF",
166                          "abc 123 def",
167                          "ABC 123 DEF",
168                          "abc DEF ghi",
169                          "abc DEF 123 xyz",
170                          "abc GHIJ 12345 def KLM"
171                         };
172         int dataCnt = data.length;
173         Bidi bidi;
174         java.text.Bidi jbidi;
175         for (int i = 0; i < dataCnt; i++) {
176             String src = pseudoToU16(data[i]);
177             bidi = new Bidi(src, Bidi.DIRECTION_LEFT_TO_RIGHT);
178             jbidi = new java.text.Bidi(src, java.text.Bidi.DIRECTION_LEFT_TO_RIGHT);
179             compareBidi(bidi, jbidi);
180             bidi = new Bidi(src, Bidi.DIRECTION_RIGHT_TO_LEFT);
181             jbidi = new java.text.Bidi(src, java.text.Bidi.DIRECTION_RIGHT_TO_LEFT);
182             compareBidi(bidi, jbidi);
183             char[] chars = src.toCharArray();
184             bidi = new Bidi(chars, 0, null, 0, chars.length, Bidi.DIRECTION_LEFT_TO_RIGHT);
185             jbidi = new java.text.Bidi(chars, 0, null, 0, chars.length, java.text.Bidi.DIRECTION_LEFT_TO_RIGHT);
186             compareBidi(bidi, jbidi);
187         }
188         /* check bogus flags */
189         bidi = new Bidi("abc", 999);
190         assertEquals("\nDirection should be LTR", Bidi.LTR, bidi.getDirection());
191         /* check constructor with overriding embeddings */
192         bidi = new Bidi(new char[] { 's', 's', 's' }, 0,
193                         new byte[] {(byte) -7, (byte) -2, (byte) -3 },
194                         0, 3, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
195         jbidi = new java.text.Bidi(new char[] { 's', 's', 's' }, 0,
196                         new byte[] {(byte) -7, (byte) -2, (byte) -3 },
197                         0, 3, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
198
199         AttributedString as = new AttributedString("HEBREW 123 english MOREHEB");
200         as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
201         as.addAttribute(TextAttribute.NUMERIC_SHAPING, NumericShaper.getShaper(NumericShaper.ARABIC));
202         as.addAttribute(TextAttribute.BIDI_EMBEDDING, new Integer(1), 0, 26);
203         as.addAttribute(TextAttribute.BIDI_EMBEDDING, new Integer(-1), 0, 6);
204         as.addAttribute(TextAttribute.BIDI_EMBEDDING, new Integer(-1), 19, 26);
205         AttributedCharacterIterator aci = as.getIterator();
206         bidi = new Bidi(aci);
207         jbidi = new java.text.Bidi(aci);
208         compareBidi(bidi, jbidi);
209         String out = bidi.writeReordered(0);
210         logln("Output #1 of Bidi(AttributedCharacterIterator): " + out);
211
212         as = new AttributedString("HEBREW 123 english MOREHEB");
213         as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
214         as.addAttribute(TextAttribute.BIDI_EMBEDDING, new Integer(0), 0, 26);
215         aci = as.getIterator();
216         bidi = new Bidi(aci);
217         jbidi = new java.text.Bidi(aci);
218         compareBidi(bidi, jbidi);
219         out = bidi.writeReordered(0);
220         logln("Output #2 of Bidi(AttributedCharacterIterator): " + out);
221
222         as = new AttributedString("HEBREW 123 english MOREHEB");
223         aci = as.getIterator();
224         bidi = new Bidi(aci);
225         jbidi = new java.text.Bidi(aci);
226         compareBidi(bidi, jbidi);
227         out = bidi.writeReordered(0);
228         logln("Output #3 of Bidi(AttributedCharacterIterator): " + out);
229
230         char[] text = "abc==(123)==>def".toCharArray();
231         bidi = new Bidi(text, 3, null, 0, 10, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
232         jbidi = new java.text.Bidi(text, 3, null, 0, 10, java.text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
233         compareBidi(bidi, jbidi);
234         out = bidi.writeReordered(0);
235         logln("Output of Bidi(abc==(123)==>def,3,null,0,10, DEFAULT_LTR): " + out);
236         bidi = new Bidi(text, 3, null, 0, 10, Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
237         jbidi = new java.text.Bidi(text, 3, null, 0, 10, java.text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
238         compareBidi(bidi, jbidi);
239         out = bidi.writeReordered(0);
240         logln("Output of Bidi(abc==(123)==>def,3,null,0,10, DEFAULT_RTL): " + out);
241         byte[] levels = new byte[] {0,0,0,-1,-1,-1,0,0,0,0};
242         bidi = new Bidi(text, 3, levels, 0, 10, Bidi.DIRECTION_LEFT_TO_RIGHT);
243         jbidi = new java.text.Bidi(text, 3, levels, 0, 10, java.text.Bidi.DIRECTION_LEFT_TO_RIGHT);
244         compareBidi(bidi, jbidi);
245         out = bidi.writeReordered(0);
246         logln("Output of Bidi(abc==(123)==>def,3,levels,0,10, LTR): " + out);
247         bidi = new Bidi(text, 3, levels, 0, 10, Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
248         jbidi = new java.text.Bidi(text, 3, levels, 0, 10, java.text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
249         compareBidi(bidi, jbidi);
250         out = bidi.writeReordered(0);
251         logln("Output of Bidi(abc==(123)==>def,3,levels,0,10, DEFAULT_RTL): " + out);
252
253         /* test reorderVisually */
254         byte[] myLevels = new byte[] {1,2,0,1,2,1,2,0,1,2};
255         Character[] objects = new Character[10];
256         levels = new byte[objects.length];
257         for (int i = 0; i < objects.length; i++) {
258             objects[i] = new Character((char)('a'+i));
259             levels[i] = myLevels[i];
260         }
261         Bidi.reorderVisually(levels, 3, objects, 3, 7);
262         String strbidi = "";
263         for (int i = 0; i < objects.length; i++) {
264             strbidi += objects[i].toString();
265         }
266         for (int i = 0; i < objects.length; i++) {
267             objects[i] = new Character((char)('a'+i));
268             levels[i] = myLevels[i];
269         }
270         java.text.Bidi.reorderVisually(levels, 3, objects, 3, 7);
271         String strjbidi = "";
272         for (int i = 0; i < objects.length; i++) {
273             strjbidi += objects[i].toString();
274         }
275         if (!strjbidi.equals(strbidi)) {
276             errln("Discrepancy in reorderVisually " +
277                   "\n      bidi: " + strbidi +
278                   "\n     jbidi: " + strjbidi);
279         } else {
280             logln("Correct match in reorderVisually " +
281                   "\n      bidi: " + strbidi +
282                   "\n     jbidi: " + strjbidi);
283         }
284
285         logln("\nExiting TestCompatibility\n");
286     }
287
288     public static void main(String[] args) {
289         try {
290             new TestCompatibility().run(args);
291         }
292         catch (Exception e) {
293             System.out.println(e);
294         }
295     }
296 }
297 //#endif