2 *******************************************************************************
\r
3 * Copyright (C) 2001-2007, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.dev.test.bidi;
\r
10 import com.ibm.icu.impl.Utility;
\r
11 import com.ibm.icu.text.Bidi;
\r
14 * Regression test for streaming mode
\r
16 * @author Lina Kemmel, Matitiahu Allouche
\r
19 public class TestStreaming extends BidiTest {
\r
21 static final int MAXPORTIONS = 10;
\r
23 static class TestCase {
\r
27 int[][] portionLens;
\r
30 public TestCase(String in, int ch, int[] np, int[][] lens, String[] msg) {
\r
33 this.nPortions = np;
\r
34 this.portionLens = lens;
\r
39 static final TestCase[] testCases = {
\r
40 new TestCase("123\n" +
\r
46 6, new int[] { 6, 6 },
\r
47 new int[][] {{ 6, 4, 6, 1, 6, 3}, { 4, 6, 6, 1, 6, 3 }},
\r
48 new String[] {"6, 4, 6, 1, 6, 3", "4, 6, 6, 1, 6, 3"}
\r
50 new TestCase("abcd\nfgh\r12345\n456",
\r
51 6, new int[] { 4, 4 },
\r
52 new int[][] {{ 6, 3, 6, 3 }, { 5, 4, 6, 3 }},
\r
53 new String[] {"6, 3, 6, 3", "5, 4, 6, 3"}
\r
55 new TestCase("abcd\nfgh\r12345\n45\r",
\r
56 6, new int[] { 4, 4 },
\r
57 new int[][] {{ 6, 3, 6, 3 }, { 5, 4, 6, 3 }},
\r
58 new String[] {"6, 3, 6, 3", "5, 4, 6, 3"}
\r
60 new TestCase("abcde\nfghi",
\r
61 10, new int[] { 1, 2 },
\r
62 new int[][] {{ 10 }, { 6, 4 }},
\r
63 new String[] {"10", "6, 4"}
\r
66 static final int MAXLOOPS = 20;
\r
67 static final byte[] paraLevels = { Bidi.LTR, Bidi.RTL };
\r
69 public void testStreaming()
\r
73 int srcLen, processedLen, chunk, len, nPortions, offset;
\r
74 int i, j, levelIndex;
\r
76 int nTests = testCases.length, nLevels = paraLevels.length;
\r
77 boolean mismatch, testOK = true;
\r
78 StringBuffer processedLenStr = new StringBuffer(MAXLOOPS * 5);
\r
80 logln("\nEntering TestStreaming\n");
\r
84 bidi.orderParagraphsLTR(true);
\r
86 for (levelIndex = 0; levelIndex < nLevels; levelIndex++) {
\r
87 for (i = 0; i < nTests; i++) {
\r
88 src = testCases[i].textIn;
\r
89 srcLen = src.length();
\r
90 chunk = testCases[i].chunk;
\r
91 nPortions = testCases[i].nPortions[levelIndex];
\r
92 level = paraLevels[levelIndex];
\r
93 processedLenStr.setLength(0);
\r
94 logln("Testing level " + level + ", case " + i);
\r
98 bidi.setReorderingOptions(Bidi.OPTION_STREAMING);
\r
99 for (j = 0; j < MAXPORTIONS && srcLen > 0; j++) {
\r
100 len = chunk < srcLen ? chunk : srcLen;
\r
101 offset = src.length() - srcLen;
\r
102 subsrc = src.substring(offset, offset + len);
\r
103 bidi.setPara(subsrc, level, null);
\r
105 processedLen = bidi.getProcessedLength();
\r
106 if (processedLen == 0) {
\r
107 bidi.setReorderingOptions(Bidi.OPTION_DEFAULT);
\r
111 bidi.setReorderingOptions(Bidi.OPTION_STREAMING);
\r
113 mismatch |= j >= nPortions ||
\r
114 processedLen != testCases[i].portionLens[levelIndex][j];
\r
116 processedLenStr.append(Integer.toString(processedLen) + " ");
\r
117 srcLen -= processedLen;
\r
120 if (mismatch || j != nPortions) {
\r
122 errln("\nProcessed lengths mismatch for" +
\r
123 "\n\tParagraph level = " + level +
\r
124 "\n\tInput string: " + Utility.escape(src) +
\r
125 "\n\tChunk = " + chunk +
\r
126 "\n\tActually processed portion lengths: { " +
\r
127 processedLenStr + " }" +
\r
128 "\n\tExpected portion lengths : { " +
\r
129 testCases[i].message[levelIndex] + " }\n");
\r
134 logln("\nBidi streaming test OK");
\r
136 logln("\nExiting TestStreaming\n");
\r
140 public static void main(String[] args) {
\r
142 new TestStreaming().run(args);
\r
144 catch (Exception e) {
\r
145 System.out.println(e);
\r