]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/bidi/TestContext.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / bidi / TestContext.java
1 /*
2 *******************************************************************************
3 *   Copyright (C) 2007-2011, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 */
7
8 package com.ibm.icu.dev.test.bidi;
9
10 import com.ibm.icu.text.Bidi;
11
12 /**
13  * Regression test for doing transformations in context
14  *
15  * @author Matitiahu Allouche
16  */
17
18 public class TestContext extends BidiTest {
19
20     private class ContextCase {
21         String prologue;
22         String source;
23         String epilogue;
24         String expected;
25         byte paraLevel;
26
27         ContextCase(String pro, String src, String epi, String exp, byte lev) {
28             prologue = pro;
29             source = src;
30             epilogue = epi;
31             expected = exp;
32             paraLevel = lev;
33         }
34     };
35
36     private final ContextCase[] contextData = {
37         /*00*/  new ContextCase("", "", "", "", Bidi.LTR),
38         /*01*/  new ContextCase("", ".-=JKL-+*", "", ".-=LKJ-+*", Bidi.LTR),
39         /*02*/  new ContextCase(" ", ".-=JKL-+*", " ", ".-=LKJ-+*", Bidi.LTR),
40         /*03*/  new ContextCase("a", ".-=JKL-+*", "b", ".-=LKJ-+*", Bidi.LTR),
41         /*04*/  new ContextCase("D", ".-=JKL-+*", "", "LKJ=-.-+*", Bidi.LTR),
42         /*05*/  new ContextCase("", ".-=JKL-+*", " D", ".-=*+-LKJ", Bidi.LTR),
43         /*06*/  new ContextCase("", ".-=JKL-+*", " 2", ".-=*+-LKJ", Bidi.LTR),
44         /*07*/  new ContextCase("", ".-=JKL-+*", " 7", ".-=*+-LKJ", Bidi.LTR),
45         /*08*/  new ContextCase(" G 1", ".-=JKL-+*", " H", "*+-LKJ=-.", Bidi.LTR),
46         /*09*/  new ContextCase("7", ".-=JKL-+*", " H", ".-=*+-LKJ", Bidi.LTR),
47         /*10*/  new ContextCase("", ".-=abc-+*", "", "*+-abc=-.", Bidi.RTL),
48         /*11*/  new ContextCase(" ", ".-=abc-+*", " ", "*+-abc=-.", Bidi.RTL),
49         /*12*/  new ContextCase("D", ".-=abc-+*", "G", "*+-abc=-.", Bidi.RTL),
50         /*13*/  new ContextCase("x", ".-=abc-+*", "", "*+-.-=abc", Bidi.RTL),
51         /*14*/  new ContextCase("", ".-=abc-+*", " y", "abc-+*=-.", Bidi.RTL),
52         /*15*/  new ContextCase("", ".-=abc-+*", " 2", "abc-+*=-.", Bidi.RTL),
53         /*16*/  new ContextCase(" x 1", ".-=abc-+*", " 2", ".-=abc-+*", Bidi.RTL),
54         /*17*/  new ContextCase(" x 7", ".-=abc-+*", " 8", "*+-.-=abc", Bidi.RTL),
55         /*18*/  new ContextCase("x|", ".-=abc-+*", " 8", "*+-abc=-.", Bidi.RTL),
56         /*19*/  new ContextCase("G|y", ".-=abc-+*", " 8", "*+-.-=abc", Bidi.RTL),
57         /*20*/  new ContextCase("", ".-=", "", ".-=", Bidi.LEVEL_DEFAULT_LTR),
58         /*21*/  new ContextCase("D", ".-=", "", "=-.", Bidi.LEVEL_DEFAULT_LTR),
59         /*22*/  new ContextCase("G", ".-=", "", "=-.", Bidi.LEVEL_DEFAULT_LTR),
60         /*23*/  new ContextCase("xG", ".-=", "", ".-=", Bidi.LEVEL_DEFAULT_LTR),
61         /*24*/  new ContextCase("x|G", ".-=", "", "=-.", Bidi.LEVEL_DEFAULT_LTR),
62         /*25*/  new ContextCase("x|G", ".-=|-+*", "", "=-.|-+*", Bidi.LEVEL_DEFAULT_LTR),
63     };
64     private final int CONTEXT_COUNT = contextData.length;
65
66     public void testContext()
67     {
68         String prologue, epilogue, src, dest;
69         Bidi bidi = new Bidi();
70         int tc;
71         ContextCase cc;
72
73         logln("\nEntering TestContext\n");
74
75         bidi.orderParagraphsLTR(true);
76
77         for (tc = 0; tc < CONTEXT_COUNT; tc++) {
78             cc = contextData[tc];
79             prologue = pseudoToU16(cc.prologue);
80             epilogue = pseudoToU16(cc.epilogue);
81             /* in the call below, prologue and epilogue are swapped to show
82                that the next call will override this call */
83             bidi.setContext(epilogue, prologue);
84             bidi.setContext(prologue, epilogue);
85             src = pseudoToU16(cc.source);
86             bidi.setPara(src, cc.paraLevel, null);
87             dest = bidi.writeReordered(Bidi.DO_MIRRORING);
88             dest = u16ToPseudo(dest);
89             assertEquals("\nActual and expected output mismatch on case "+tc+"." +
90                          "\nPrologue:           " + cc.prologue +
91                          "\nInput:              " + cc.source +
92                          "\nEpilogue:           " + cc.epilogue +
93                          "\nParagraph level:    " + Byte.toString(bidi.getParaLevel()) + "\n",
94                          cc.expected, dest);
95         }
96
97         logln("\nExiting TestContext\n");
98     }
99
100     public static void main(String[] args) {
101         try {
102             new TestContext().run(args);
103         }
104         catch (Exception e) {
105             System.out.println(e);
106         }
107     }
108 }