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