2 *******************************************************************************
3 * Copyright (C) 2001-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 package com.ibm.icu.dev.test.bidi;
10 import com.ibm.icu.impl.Utility;
11 import com.ibm.icu.text.Bidi;
14 * Regression test for streaming mode
16 * @author Lina Kemmel, Matitiahu Allouche
19 public class TestStreaming extends BidiTest {
21 static final int MAXPORTIONS = 10;
23 static class TestCase {
30 public TestCase(String in, int ch, int[] np, int[][] lens, String[] msg) {
34 this.portionLens = lens;
39 static final TestCase[] testCases = {
40 new TestCase("123\n" +
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"}
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"}
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"}
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"}
66 static final int MAXLOOPS = 20;
67 static final byte[] paraLevels = { Bidi.LTR, Bidi.RTL };
69 public void testStreaming()
73 int srcLen, processedLen, chunk, len, nPortions, offset;
76 int nTests = testCases.length, nLevels = paraLevels.length;
77 boolean mismatch, testOK = true;
78 StringBuffer processedLenStr = new StringBuffer(MAXLOOPS * 5);
80 logln("\nEntering TestStreaming\n");
84 bidi.orderParagraphsLTR(true);
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);
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);
105 processedLen = bidi.getProcessedLength();
106 if (processedLen == 0) {
107 bidi.setReorderingOptions(Bidi.OPTION_DEFAULT);
111 bidi.setReorderingOptions(Bidi.OPTION_STREAMING);
113 mismatch |= j >= nPortions ||
114 processedLen != testCases[i].portionLens[levelIndex][j];
116 processedLenStr.append(Integer.toString(processedLen) + " ");
117 srcLen -= processedLen;
120 if (mismatch || j != nPortions) {
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");
134 logln("\nBidi streaming test OK");
136 logln("\nExiting TestStreaming\n");
140 public static void main(String[] args) {
142 new TestStreaming().run(args);
144 catch (Exception e) {
145 System.out.println(e);