Home | History | Annotate | Download | only in bidi
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 /*
      3 *******************************************************************************
      4 *   Copyright (C) 2001-2013, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *******************************************************************************
      7 */
      8 
      9 package android.icu.dev.test.bidi;
     10 
     11 import java.util.Arrays;
     12 
     13 import android.icu.impl.Utility;
     14 import android.icu.text.Bidi;
     15 import org.junit.runner.RunWith;
     16 import android.icu.junit.IcuTestFmwkRunner;
     17 
     18 /**
     19  * Regression test for the basic "inverse" Bidi mode.
     20  *
     21  * ported from C by Lina Kemmel, Matitiahu Allouche
     22  */
     23 
     24 @RunWith(IcuTestFmwkRunner.class)
     25 public class TestInverse extends BidiTest {
     26 
     27     private int countRoundtrips = 0;
     28     private int countNonRoundtrips = 0;
     29 
     30     static final String[] testCases = {
     31         "\u006c\u0061\u0028\u0074\u0069\u006e\u0020\u05d0\u05d1\u0029\u05d2\u05d3",
     32         "\u006c\u0061\u0074\u0020\u05d0\u05d1\u05d2\u0020\u0031\u0032\u0033",
     33         "\u006c\u0061\u0074\u0020\u05d0\u0028\u05d1\u05d2\u0020\u0031\u0029\u0032\u0033",
     34         "\u0031\u0032\u0033\u0020\u05d0\u05d1\u05d2\u0020\u0034\u0035\u0036",
     35         "\u0061\u0062\u0020\u0061\u0062\u0020\u0661\u0662"
     36     };
     37 
     38     public void testInverse() {
     39         Bidi bidi;
     40         int i;
     41 
     42         logln("\nEntering TestInverse\n");
     43         bidi = new Bidi();
     44         log("inverse Bidi: testInverse(L) with " + testCases.length +
     45             " test cases ---\n");
     46         for(i = 0; i < testCases.length; ++i) {
     47             logln("Testing case " + i);
     48             _testInverseBidi(bidi, testCases[i], Bidi.DIRECTION_LEFT_TO_RIGHT);
     49         }
     50 
     51         log("inverse Bidi: testInverse(R) with " + testCases.length +
     52             " test cases ---\n");
     53         for (i = 0; i < testCases.length; ++i) {
     54             logln("Testing case " + i);
     55             _testInverseBidi(bidi, testCases[i], Bidi.DIRECTION_RIGHT_TO_LEFT);
     56         }
     57 
     58         _testManyInverseBidi(bidi, Bidi.DIRECTION_LEFT_TO_RIGHT);
     59         _testManyInverseBidi(bidi, Bidi.DIRECTION_RIGHT_TO_LEFT);
     60 
     61         logln("inverse Bidi: rountrips: " + countRoundtrips +
     62               "   non-roundtrips: " + countNonRoundtrips);
     63 
     64         _testWriteReverse();
     65 
     66         _testManyAddedPoints();
     67 
     68         _testMisc();
     69 
     70         logln("\nExiting TestInverse\n");
     71     }
     72 
     73     private static final char[][] repeatSegments = {
     74         { 0x61, 0x62 },     /* L */
     75         { 0x5d0, 0x5d1 },   /* R */
     76         { 0x627, 0x628 },   /* AL */
     77         { 0x31, 0x32 },     /* EN */
     78         { 0x661, 0x662 },   /* AN */
     79         { 0x20, 0x20 }      /* WS (N) */
     80     };
     81     private static final int COUNT_REPEAT_SEGMENTS = 6;
     82 
     83     private void _testManyInverseBidi(Bidi bidi, int direction) {
     84         char[] text = { 0, 0, 0x20, 0, 0, 0x20, 0, 0 };
     85         int i, j, k;
     86 
     87         log("inverse Bidi: testManyInverseBiDi(" +
     88             (direction == Bidi.DIRECTION_LEFT_TO_RIGHT ? 'L' : 'R') +
     89             ") - test permutations of text snippets ---\n");
     90         for (i = 0; i < COUNT_REPEAT_SEGMENTS; ++i) {
     91             text[0] = repeatSegments[i][0];
     92             text[1] = repeatSegments[i][1];
     93             for (j = 0; j < COUNT_REPEAT_SEGMENTS; ++j) {
     94                 text[3] = repeatSegments[j][0];
     95                 text[4] = repeatSegments[j][1];
     96                 for (k = 0; k < COUNT_REPEAT_SEGMENTS; ++k) {
     97                     text[6] = repeatSegments[k][0];
     98                     text[7] = repeatSegments[k][1];
     99 
    100                     log("inverse Bidi: testManyInverseBiDi()[" +
    101                         i + " " + j + " " + k + "]\n");
    102                     _testInverseBidi(bidi, new String(text), direction);
    103                 }
    104             }
    105         }
    106     }
    107 
    108     private void _testInverseBidi(Bidi bidi, String src, int direction) {
    109         String visualLTR, logicalDest, visualDest;
    110         try {
    111             if (direction == Bidi.DIRECTION_LEFT_TO_RIGHT) {
    112                 log("inverse Bidi: testInverse(L)\n");
    113 
    114                 /* convert visual to logical */
    115                 bidi.setInverse(true);
    116                 if (!bidi.isInverse()) {
    117                     err("Error while doing setInverse(true)\n");
    118                 }
    119                 bidi.setPara(src, Bidi.LTR, null);
    120                 if (!Arrays.equals(src.toCharArray(), bidi.getText())) {
    121                     err("Wrong value returned by getText\n");
    122                 }
    123                 if (!src.equals(bidi.getTextAsString())) {
    124                     err("Wrong value returned by getTextAsString\n");
    125                 }
    126                 logicalDest = bidi.writeReordered(Bidi.DO_MIRRORING |
    127                                                   Bidi.INSERT_LRM_FOR_NUMERIC);
    128                 log("  v ");
    129                 printUnicode(src.toCharArray(), bidi.getLevels());
    130                 log("\n");
    131 
    132                 /* convert back to visual LTR */
    133                 bidi.setInverse(false);
    134                 if (bidi.isInverse()) {
    135                     err("Error while doing setInverse(false)\n");
    136                 }
    137                 bidi.setPara(logicalDest, Bidi.LTR, null);
    138                 visualDest = bidi.writeReordered(Bidi.DO_MIRRORING |
    139                                                  Bidi.REMOVE_BIDI_CONTROLS);
    140             } else {
    141                 logln("inverse Bidi: testInverse(R)\n");
    142 
    143                 /* reverse visual from RTL to LTR */
    144                 visualLTR = Bidi.writeReverse(src, 0);
    145                 log("  vr");
    146                 printUnicode(src.toCharArray(), null);
    147                 log("\n");
    148 
    149                 /* convert visual RTL to logical */
    150                 bidi.setInverse(true);
    151                 bidi.setPara(visualLTR, Bidi.LTR, null);
    152                 logicalDest = bidi.writeReordered(Bidi.DO_MIRRORING |
    153                                                   Bidi.INSERT_LRM_FOR_NUMERIC);
    154                 log("  vl");
    155                 printUnicode(visualLTR.toCharArray(), bidi.getLevels());
    156                 log("\n");
    157 
    158                 /* convert back to visual RTL */
    159                 bidi.setInverse(false);
    160                 bidi.setPara(logicalDest, Bidi.LTR, null);
    161                 visualDest = bidi.writeReordered(Bidi.DO_MIRRORING |
    162                              Bidi.REMOVE_BIDI_CONTROLS | Bidi.OUTPUT_REVERSE);
    163             }
    164             log("  l ");
    165             printUnicode(logicalDest.toCharArray(), bidi.getLevels());
    166             log("\n");
    167             log("  v ");
    168             printUnicode(visualDest.toCharArray(), null);
    169             log("\n");
    170         } catch (Exception e) {
    171             errln("\ninverse Bidi: *** failed");
    172             errln("   error message: " + e.getMessage());
    173             e.printStackTrace();
    174             visualDest = null;
    175         }
    176 
    177         /* check and print results */
    178         if (src.equals(visualDest)) {
    179             ++countRoundtrips;
    180             log(" + roundtripped\n");
    181         } else {
    182             ++countNonRoundtrips;
    183             log(" * did not roundtrip\n");
    184         }
    185     }
    186 
    187     private void _testWriteReverse() {
    188         /* U+064e and U+0650 are combining marks (Mn) */
    189         final String
    190             forward = "\u200f\u0627\u064e\u0650\u0020\u0028\u0031\u0029",
    191             reverseKeepCombining =
    192                 "\u0029\u0031\u0028\u0020\u0627\u064e\u0650\u200f",
    193             reverseRemoveControlsKeepCombiningDoMirror =
    194                 "\u0028\u0031\u0029\u0020\u0627\u064e\u0650";
    195 
    196         String reverse;
    197 
    198         /* test Bidi.writeReverse() with "interesting" options */
    199         try {
    200             reverse = Bidi.writeReverse(forward, Bidi.KEEP_BASE_COMBINING);
    201         } catch (Exception e) {
    202             errln("Failure in Bidi.writeReverse(KEEP_BASE_COMBINING)");
    203             reverse = null;
    204         }
    205         assertEquals("\nFailure in " + getClass().toString() +
    206                      " in Bidi.writeReverse", reverseKeepCombining,
    207                      reverse, forward, null, "KEEP_BASE_COMBINING", null);
    208 
    209         try {
    210             reverse = Bidi.writeReverse(forward, Bidi.REMOVE_BIDI_CONTROLS |
    211                                         Bidi.DO_MIRRORING | Bidi.KEEP_BASE_COMBINING);
    212         } catch (Exception e) {
    213             errln("Failure in Bidi.writeReverse(KEEP_BASE_COMBINING)");
    214         }
    215         assertEquals("\nFailure in " + getClass().toString() +
    216                      " in Bidi.writeReverse",
    217                      reverseRemoveControlsKeepCombiningDoMirror,
    218                      reverse, forward, null,
    219                      "REMOVE_BIDI_CONTROLS|DO_MIRRORING|KEEP_BASE_COMBINING",
    220                      null);
    221     }
    222 
    223     private void printUnicode(char[] chars, byte[] levels) {
    224         int i;
    225 
    226         log("{ ");
    227         for (i = 0; i < chars.length; ++i) {
    228             log("0x" + Utility.hex(chars[i]));
    229             if (levels != null) {
    230                 log("." + levels[i]);
    231             }
    232             log("   ");
    233         }
    234         log(" }");
    235     }
    236 
    237     private void _testManyAddedPoints() {
    238         Bidi bidi = new Bidi();
    239         char[] text = new char[90];
    240         for (int i = 0; i < text.length; i+=3) {
    241             text[i] = 'a';
    242             text[i+1] = '\u05d0';
    243             text[i+2] = '3';
    244         }
    245         bidi.setReorderingMode(Bidi.REORDER_INVERSE_LIKE_DIRECT);
    246         bidi.setReorderingOptions(Bidi.OPTION_INSERT_MARKS);
    247         bidi.setPara(text, Bidi.LTR, null);
    248         String out = bidi.writeReordered(0);
    249         char[] expected = new char[120];
    250         for (int i = 0; i < expected.length; i+=4) {
    251             expected[i] = 'a';
    252             expected[i+1] = '\u05d0';
    253             expected[i+2] = '\u200e';
    254             expected[i+3] = '3';
    255         }
    256         assertEquals("\nInvalid output with many added points",
    257                      new String(expected), out);
    258     }
    259 
    260     private void _testMisc() {
    261         Bidi bidi = new Bidi();
    262         bidi.setInverse(true);
    263         bidi.setPara("   ", Bidi.RTL, null);
    264         String out = bidi.writeReordered(Bidi.OUTPUT_REVERSE | Bidi.INSERT_LRM_FOR_NUMERIC);
    265         assertEquals("\nInvalid output with RLM at both sides",
    266                      "\u200f   \u200f", out);
    267     }
    268 
    269 
    270     public static void main(String[] args) {
    271         try {
    272             new TestInverse().run(args);
    273         }
    274         catch (Exception e) {
    275             System.out.println(e);
    276         }
    277     }
    278 
    279 }
    280