]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/bidi/TestStreaming.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / bidi / TestStreaming.java
1 /*\r
2 *******************************************************************************\r
3 *   Copyright (C) 2001-2007, International Business Machines\r
4 *   Corporation and others.  All Rights Reserved.\r
5 *******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.test.bidi;\r
9 \r
10 import com.ibm.icu.impl.Utility;\r
11 import com.ibm.icu.text.Bidi;\r
12 \r
13 /**\r
14  * Regression test for streaming mode\r
15  *\r
16  * @author Lina Kemmel, Matitiahu Allouche\r
17  */\r
18 \r
19 public class TestStreaming extends BidiTest {\r
20 \r
21     static final int MAXPORTIONS = 10;\r
22 \r
23     static class TestCase {\r
24         String textIn;\r
25         int chunk;\r
26         int[] nPortions;\r
27         int[][] portionLens;\r
28         String[] message;\r
29 \r
30         public TestCase(String in, int ch, int[] np, int[][] lens, String[] msg) {\r
31             this.textIn = in;\r
32             this.chunk = ch;\r
33             this.nPortions = np;\r
34             this.portionLens = lens;\r
35             this.message = msg;\r
36         }\r
37     }\r
38 \r
39     static final TestCase[] testCases = {\r
40         new TestCase("123\n"    +\r
41                      "abc45\r"  +\r
42                      "67890\n"  +\r
43                      "\r"       +\r
44                      "02468\r"  +\r
45                      "ghi",\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
49         ),\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
54         ),\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
59         ),\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
64         )\r
65     };\r
66     static final int MAXLOOPS = 20;\r
67     static final byte[] paraLevels = { Bidi.LTR, Bidi.RTL };\r
68 \r
69     public void testStreaming()\r
70     {\r
71         String src, subsrc;\r
72         Bidi bidi;\r
73         int srcLen, processedLen, chunk, len, nPortions, offset;\r
74         int i, j, levelIndex;\r
75         byte level;\r
76         int nTests = testCases.length, nLevels = paraLevels.length;\r
77         boolean mismatch, testOK = true;\r
78         StringBuffer processedLenStr = new StringBuffer(MAXLOOPS * 5);\r
79 \r
80         logln("\nEntering TestStreaming\n");\r
81 \r
82         bidi = new Bidi();\r
83 \r
84         bidi.orderParagraphsLTR(true);\r
85 \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
95 \r
96                 mismatch = false;\r
97 \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
104 \r
105                     processedLen = bidi.getProcessedLength();\r
106                     if (processedLen == 0) {\r
107                         bidi.setReorderingOptions(Bidi.OPTION_DEFAULT);\r
108                         j--;\r
109                         continue;\r
110                     }\r
111                     bidi.setReorderingOptions(Bidi.OPTION_STREAMING);\r
112 \r
113                     mismatch |= j >= nPortions ||\r
114                                processedLen != testCases[i].portionLens[levelIndex][j];\r
115 \r
116                     processedLenStr.append(Integer.toString(processedLen) + " ");\r
117                     srcLen -= processedLen;\r
118                 }\r
119 \r
120                 if (mismatch || j != nPortions) {\r
121                     testOK = false;\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
130                 }\r
131             }\r
132         }\r
133         if (testOK) {\r
134             logln("\nBidi streaming test OK");\r
135         }\r
136         logln("\nExiting TestStreaming\n");\r
137     }\r
138 \r
139 \r
140     public static void main(String[] args) {\r
141         try {\r
142             new TestStreaming().run(args);\r
143         }\r
144         catch (Exception e) {\r
145             System.out.println(e);\r
146         }\r
147     }\r
148 \r
149 }\r