]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/bidi/TestStreaming.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / bidi / TestStreaming.java
1 /*
2 *******************************************************************************
3 *   Copyright (C) 2001-2013, 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.impl.Utility;
11 import com.ibm.icu.text.Bidi;
12
13 /**
14  * Regression test for streaming mode
15  *
16  * @author Lina Kemmel, Matitiahu Allouche
17  */
18
19 public class TestStreaming extends BidiTest {
20
21     static final int MAXPORTIONS = 10;
22
23     static class TestCase {
24         String textIn;
25         int chunk;
26         int[] nPortions;
27         int[][] portionLens;
28         String[] message;
29
30         public TestCase(String in, int ch, int[] np, int[][] lens, String[] msg) {
31             this.textIn = in;
32             this.chunk = ch;
33             this.nPortions = np;
34             this.portionLens = lens;
35             this.message = msg;
36         }
37     }
38
39     static final TestCase[] testCases = {
40         new TestCase("123\n"    +
41                      "abc45\r"  +
42                      "67890\n"  +
43                      "\r"       +
44                      "02468\r"  +
45                      "ghi",
46             6, new int[] { 6, 6 },
47             new int[][] {{ 4, 6, 6, 1, 6, 3}, { 4, 6, 6, 1, 6, 3 }},
48             new String[] {"4, 6, 6, 1, 6, 3",  "4, 6, 6, 1, 6, 3"}
49         ),
50         new TestCase("abcd\nfgh\r12345\n456",
51             6, new int[] { 4, 4 },
52             new int[][] {{ 5, 4, 6, 3 }, { 5, 4, 6, 3 }},
53             new String[] {"5, 4, 6, 3",   "5, 4, 6, 3"}
54         ),
55         new TestCase("abcd\nfgh\r12345\n45\r",
56             6, new int[] { 4, 4 },
57             new int[][] {{ 5, 4, 6, 3 }, { 5, 4, 6, 3 }},
58             new String[] {"5, 4, 6, 3",   "5, 4, 6, 3"}
59         ),
60         new TestCase("abcde\nfghi",
61             10, new int[] { 2, 2 },
62             new int[][] {{ 6, 4 }, { 6, 4 }},
63             new String[] {"6, 4",   "6, 4"}
64         )
65     };
66     static final int MAXLOOPS = 20;
67     static final byte[] paraLevels = { Bidi.LTR, Bidi.RTL };
68
69     public void testStreaming()
70     {
71         String src, subsrc;
72         Bidi bidi;
73         int srcLen, processedLen, chunk, len, nPortions, offset;
74         int i, j, levelIndex;
75         byte level;
76         int nTests = testCases.length, nLevels = paraLevels.length;
77         boolean mismatch, testOK = true;
78         StringBuffer processedLenStr = new StringBuffer(MAXLOOPS * 5);
79
80         logln("\nEntering TestStreaming\n");
81
82         bidi = new Bidi();
83
84         bidi.orderParagraphsLTR(true);
85
86         for (levelIndex = 0; levelIndex < nLevels; levelIndex++) {
87             for (i = 0; i < nTests; i++) {
88                 src = testCases[i].textIn;
89                 srcLen = src.length();
90                 chunk = testCases[i].chunk;
91                 nPortions = testCases[i].nPortions[levelIndex];
92                 level = paraLevels[levelIndex];
93                 processedLenStr.setLength(0);
94                 logln("Testing level " + level + ", case " + i);
95
96                 mismatch = false;
97
98                 bidi.setReorderingOptions(Bidi.OPTION_STREAMING);
99                 for (j = 0; j < MAXPORTIONS && srcLen > 0; j++) {
100                     len = chunk < srcLen ? chunk : srcLen;
101                     offset = src.length() - srcLen;
102                     subsrc = src.substring(offset, offset + len);
103                     bidi.setPara(subsrc, level, null);
104
105                     processedLen = bidi.getProcessedLength();
106                     if (processedLen == 0) {
107                         bidi.setReorderingOptions(Bidi.OPTION_DEFAULT);
108                         j--;
109                         continue;
110                     }
111                     bidi.setReorderingOptions(Bidi.OPTION_STREAMING);
112
113                     mismatch |= j >= nPortions ||
114                                processedLen != testCases[i].portionLens[levelIndex][j];
115
116                     processedLenStr.append(Integer.toString(processedLen) + " ");
117                     srcLen -= processedLen;
118                 }
119
120                 if (mismatch || j != nPortions) {
121                     testOK = false;
122                     errln("\nProcessed lengths mismatch for" +
123                           "\n\tParagraph level = " + level +
124                           "\n\tInput string: " + Utility.escape(src) +
125                           "\n\tChunk = " + chunk +
126                           "\n\tActually processed portion lengths: { " +
127                                 processedLenStr + " }" +
128                           "\n\tExpected portion lengths          : { " +
129                                 testCases[i].message[levelIndex] + " }\n");
130                 }
131             }
132         }
133         if (testOK) {
134             logln("\nBidi streaming test OK");
135         }
136         logln("\nExiting TestStreaming\n");
137     }
138
139
140     public static void main(String[] args) {
141         try {
142             new TestStreaming().run(args);
143         }
144         catch (Exception e) {
145             System.out.println(e);
146         }
147     }
148
149 }