]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / charset / src / com / ibm / icu / dev / test / charset / TestCharset.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 2006-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 *\r
7 *******************************************************************************\r
8 */\r
9 \r
10 package com.ibm.icu.dev.test.charset;\r
11 \r
12 import java.nio.BufferOverflowException;\r
13 import java.nio.ByteBuffer;\r
14 import java.nio.CharBuffer;\r
15 import java.nio.charset.CharacterCodingException;\r
16 import java.nio.charset.Charset;\r
17 import java.nio.charset.CharsetDecoder;\r
18 import java.nio.charset.CharsetEncoder;\r
19 import java.nio.charset.CoderResult;\r
20 import java.nio.charset.CodingErrorAction;\r
21 import java.nio.charset.UnsupportedCharsetException;\r
22 import java.nio.charset.spi.CharsetProvider;\r
23 import java.util.ArrayList;\r
24 import java.util.Iterator;\r
25 import java.util.MissingResourceException;\r
26 import java.util.Set;\r
27 import java.util.SortedMap;\r
28 \r
29 import com.ibm.icu.charset.CharsetCallback;\r
30 import com.ibm.icu.charset.CharsetDecoderICU;\r
31 import com.ibm.icu.charset.CharsetEncoderICU;\r
32 import com.ibm.icu.charset.CharsetICU;\r
33 import com.ibm.icu.charset.CharsetProviderICU;\r
34 import com.ibm.icu.dev.test.TestFmwk;\r
35 import com.ibm.icu.text.UTF16;\r
36 \r
37 public class TestCharset extends TestFmwk {\r
38     private String m_encoding = "UTF-16";\r
39     CharsetDecoder m_decoder = null;\r
40     CharsetEncoder m_encoder = null;\r
41     Charset m_charset =null;\r
42     static final String unistr = "abcd\ud800\udc00\u1234\u00a5\u3000\r\n";\r
43     static final byte[] byteStr ={   \r
44             (byte) 0x00,(byte) 'a',\r
45             (byte) 0x00,(byte) 'b',\r
46             (byte) 0x00,(byte) 'c',\r
47             (byte) 0x00,(byte) 'd',\r
48             (byte) 0xd8,(byte) 0x00,\r
49             (byte) 0xdc,(byte) 0x00,\r
50             (byte) 0x12,(byte) 0x34,\r
51             (byte) 0x00,(byte) 0xa5,\r
52             (byte) 0x30,(byte) 0x00,\r
53             (byte) 0x00,(byte) 0x0d,\r
54             (byte) 0x00,(byte) 0x0a };\r
55     static final byte[] expectedByteStr ={\r
56         (byte) 0xfe,(byte) 0xff,\r
57         (byte) 0x00,(byte) 'a',\r
58         (byte) 0x00,(byte) 'b',\r
59         (byte) 0x00,(byte) 'c',\r
60         (byte) 0x00,(byte) 'd',\r
61         (byte) 0xd8,(byte) 0x00,\r
62         (byte) 0xdc,(byte) 0x00,\r
63         (byte) 0x12,(byte) 0x34,\r
64         (byte) 0x00,(byte) 0xa5,\r
65         (byte) 0x30,(byte) 0x00,\r
66         (byte) 0x00,(byte) 0x0d,\r
67         (byte) 0x00,(byte) 0x0a };\r
68     \r
69     protected void init(){\r
70         try{\r
71             if ("UTF-16".equals(m_encoding)) {\r
72                 int x = 2;\r
73                 x++;\r
74             }\r
75             CharsetProviderICU provider = new CharsetProviderICU();\r
76             //Charset charset = CharsetICU.forName(encoding);\r
77             m_charset = provider.charsetForName(m_encoding);\r
78             m_decoder = (CharsetDecoder) m_charset.newDecoder();\r
79             m_encoder = (CharsetEncoder) m_charset.newEncoder();   \r
80         }catch(MissingResourceException ex){\r
81             warnln("Could not load charset data");\r
82         }\r
83     }\r
84     \r
85     public static void main(String[] args) throws Exception {\r
86         new TestCharset().run(args);\r
87     }\r
88     public void TestUTF16Converter(){\r
89         CharsetProvider icu = new CharsetProviderICU();\r
90         Charset cs1 = icu.charsetForName("UTF-16BE");\r
91         CharsetEncoder e1 = cs1.newEncoder();\r
92         CharsetDecoder d1 = cs1.newDecoder();\r
93         \r
94         Charset cs2 = icu.charsetForName("UTF-16LE");\r
95         CharsetEncoder e2 = cs2.newEncoder();\r
96         CharsetDecoder d2 = cs2.newDecoder();\r
97         \r
98         for(int i=0x0000; i<0x10FFFF; i+=0xFF){\r
99             CharBuffer us = CharBuffer.allocate(0xFF*2);\r
100             ByteBuffer bs1 = ByteBuffer.allocate(0xFF*8);\r
101             ByteBuffer bs2 = ByteBuffer.allocate(0xFF*8);\r
102             for(int j=0;j<0xFF; j++){\r
103                 int c = i+j;\r
104               \r
105                 if((c>=0xd800&&c<=0xdFFF)||c>0x10FFFF){\r
106                     continue;\r
107                 }\r
108 \r
109                 if(c>0xFFFF){\r
110                     char lead = UTF16.getLeadSurrogate(c);\r
111                     char trail = UTF16.getTrailSurrogate(c);\r
112                     if(!UTF16.isLeadSurrogate(lead)){\r
113                         errln("lead is not lead!"+lead+" for cp: \\U"+Integer.toHexString(c));\r
114                         continue;\r
115                     }\r
116                     if(!UTF16.isTrailSurrogate(trail)){\r
117                         errln("trail is not trail!"+trail);\r
118                         continue;\r
119                     }\r
120                     us.put(lead);\r
121                     us.put(trail);\r
122                     bs1.put((byte)(lead>>8));\r
123                     bs1.put((byte)(lead&0xFF));\r
124                     bs1.put((byte)(trail>>8));\r
125                     bs1.put((byte)(trail&0xFF));\r
126                     \r
127                     bs2.put((byte)(lead&0xFF));\r
128                     bs2.put((byte)(lead>>8));\r
129                     bs2.put((byte)(trail&0xFF));\r
130                     bs2.put((byte)(trail>>8));\r
131                 }else{\r
132 \r
133                     if(c<0xFF){\r
134                         bs1.put((byte)0x00);\r
135                         bs1.put((byte)(c));\r
136                         bs2.put((byte)(c));\r
137                         bs2.put((byte)0x00);\r
138                     }else{\r
139                         bs1.put((byte)(c>>8));\r
140                         bs1.put((byte)(c&0xFF));\r
141                         \r
142                         bs2.put((byte)(c&0xFF));\r
143                         bs2.put((byte)(c>>8));\r
144                     }\r
145                     us.put((char)c);\r
146                 }\r
147             }\r
148             \r
149             \r
150             us.limit(us.position());\r
151             us.position(0);\r
152             if(us.length()==0){\r
153                 continue;\r
154             }\r
155             \r
156 \r
157             bs1.limit(bs1.position());\r
158             bs1.position(0);\r
159             ByteBuffer newBS = ByteBuffer.allocate(bs1.capacity());\r
160             //newBS.put((byte)0xFE);\r
161             //newBS.put((byte)0xFF);\r
162             newBS.put(bs1);    \r
163             bs1.position(0);\r
164             smBufDecode(d1, "UTF-16", bs1, us);\r
165             smBufEncode(e1, "UTF-16", us, newBS);\r
166             \r
167             bs2.limit(bs2.position());\r
168             bs2.position(0);\r
169             newBS.clear();\r
170             //newBS.put((byte)0xFF);\r
171             //newBS.put((byte)0xFE);\r
172             newBS.put(bs2);     \r
173             bs2.position(0);\r
174             smBufDecode(d2, "UTF16-LE", bs2, us);\r
175             smBufEncode(e2, "UTF-16LE", us, newBS);\r
176             \r
177         }\r
178     }\r
179     public void TestUTF32Converter(){\r
180         CharsetProvider icu = new CharsetProviderICU();\r
181         Charset cs1 = icu.charsetForName("UTF-32BE");\r
182         CharsetEncoder e1 = cs1.newEncoder();\r
183         CharsetDecoder d1 = cs1.newDecoder();\r
184         \r
185         Charset cs2 = icu.charsetForName("UTF-32LE");\r
186         CharsetEncoder e2 = cs2.newEncoder();\r
187         CharsetDecoder d2 = cs2.newDecoder();\r
188         \r
189         for(int i=0x000; i<0x10FFFF; i+=0xFF){\r
190             CharBuffer us = CharBuffer.allocate(0xFF*2);\r
191             ByteBuffer bs1 = ByteBuffer.allocate(0xFF*8);\r
192             ByteBuffer bs2 = ByteBuffer.allocate(0xFF*8);\r
193             for(int j=0;j<0xFF; j++){\r
194                 int c = i+j;\r
195               \r
196                 if((c>=0xd800&&c<=0xdFFF)||c>0x10FFFF){\r
197                     continue;\r
198                 }\r
199 \r
200                 if(c>0xFFFF){\r
201                     char lead = UTF16.getLeadSurrogate(c);\r
202                     char trail = UTF16.getTrailSurrogate(c);\r
203 \r
204                     us.put(lead);\r
205                     us.put(trail);\r
206                 }else{\r
207                     us.put((char)c);\r
208                 }\r
209                 bs1.put((byte) (c >>> 24));\r
210                 bs1.put((byte) (c >>> 16)); \r
211                 bs1.put((byte) (c >>> 8)); \r
212                 bs1.put((byte) (c & 0xFF));       \r
213                                 \r
214                 bs2.put((byte) (c & 0xFF));  \r
215                 bs2.put((byte) (c >>> 8));\r
216                 bs2.put((byte) (c >>> 16)); \r
217                 bs2.put((byte) (c >>> 24));\r
218             }\r
219             bs1.limit(bs1.position());\r
220             bs1.position(0);\r
221             bs2.limit(bs2.position());\r
222             bs2.position(0);\r
223             us.limit(us.position());\r
224             us.position(0);\r
225             if(us.length()==0){\r
226                 continue;\r
227             }\r
228              \r
229 \r
230             ByteBuffer newBS = ByteBuffer.allocate(bs1.capacity());\r
231             \r
232             newBS.put((byte)0x00);\r
233             newBS.put((byte)0x00);\r
234             newBS.put((byte)0xFE);\r
235             newBS.put((byte)0xFF);\r
236             \r
237             newBS.put(bs1);\r
238             bs1.position(0);\r
239             smBufDecode(d1, "UTF-32", bs1, us);\r
240             smBufEncode(e1, "UTF-32", us, newBS);\r
241             \r
242             \r
243             newBS.clear();\r
244             \r
245             newBS.put((byte)0xFF);\r
246             newBS.put((byte)0xFE);\r
247             newBS.put((byte)0x00);\r
248             newBS.put((byte)0x00);\r
249             \r
250             newBS.put(bs2);    \r
251             bs2.position(0);\r
252             smBufDecode(d2, "UTF-32LE", bs2, us);\r
253             smBufEncode(e2, "UTF-32LE", us, newBS);\r
254 \r
255         }\r
256     }\r
257     public void TestASCIIConverter() {\r
258         runTestASCIIBasedConverter("ASCII", 0x80);\r
259     }    \r
260     public void Test88591Converter() {\r
261         runTestASCIIBasedConverter("iso-8859-1", 0x100);\r
262     }\r
263     public void runTestASCIIBasedConverter(String converter, int limit){\r
264         CharsetProvider icu = new CharsetProviderICU();\r
265         Charset icuChar = icu.charsetForName(converter);\r
266         CharsetEncoder encoder = icuChar.newEncoder();\r
267         CharsetDecoder decoder = icuChar.newDecoder();\r
268         CoderResult cr;\r
269 \r
270         /* test with and without array-backed buffers */ \r
271         \r
272         byte[] bytes = new byte[0x10000];\r
273         char[] chars = new char[0x10000];\r
274         for (int j = 0; j <= 0xffff; j++) {\r
275             bytes[j] = (byte) j;\r
276             chars[j] = (char) j;\r
277         }\r
278 \r
279         boolean fail = false;\r
280         boolean arrays = false;\r
281         boolean decoding = false;\r
282         int i;\r
283         \r
284         // 0 thru limit - 1\r
285         ByteBuffer bs = ByteBuffer.wrap(bytes, 0, limit);\r
286         CharBuffer us = CharBuffer.wrap(chars, 0, limit);\r
287         smBufDecode(decoder, converter, bs, us, true);\r
288         smBufDecode(decoder, converter, bs, us, false);\r
289         smBufEncode(encoder, converter, us, bs, true);\r
290         smBufEncode(encoder, converter, us, bs, false);\r
291         for (i = 0; i < limit; i++) {\r
292             bs = ByteBuffer.wrap(bytes, i, 1).slice();\r
293             us = CharBuffer.wrap(chars, i, 1).slice();\r
294             try {\r
295                 decoding = true;\r
296                 arrays = true;\r
297                 smBufDecode(decoder, converter, bs, us, true, false, true);\r
298                 \r
299                 decoding = true;\r
300                 arrays = false;\r
301                 smBufDecode(decoder, converter, bs, us, true, false, false);\r
302                 \r
303                 decoding = false;\r
304                 arrays = true;\r
305                 smBufEncode(encoder, converter, us, bs, true, false, true);\r
306                 \r
307                 decoding = false;\r
308                 arrays = false;\r
309                 smBufEncode(encoder, converter, us, bs, true, false, false);\r
310                 \r
311             } catch (Exception ex) {\r
312                 errln("Failed to fail to " + (decoding ? "decode" : "encode") + " 0x"\r
313                         + Integer.toHexString(i) + (arrays ? " with arrays" : " without arrays") + " in " + converter);\r
314                 return;\r
315             }\r
316         }\r
317         \r
318         // decode limit thru 255\r
319         for (i = limit; i <= 0xff; i++) {\r
320             bs = ByteBuffer.wrap(bytes, i, 1).slice();\r
321             us = CharBuffer.wrap(chars, i, 1).slice();\r
322             try {\r
323                 smBufDecode(decoder, converter, bs, us, true, false, true);\r
324                 fail = true;\r
325                 arrays = true;\r
326                 break;\r
327             } catch (Exception ex) {\r
328             }\r
329             try {\r
330                 smBufDecode(decoder, converter, bs, us, true, false, false);\r
331                 fail = true;\r
332                 arrays = false;\r
333                 break;\r
334             } catch (Exception ex) {\r
335             }\r
336         }\r
337         if (fail) {\r
338             errln("Failed to fail to decode 0x" + Integer.toHexString(i)\r
339                     + (arrays ? " with arrays" : " without arrays") + " in " + converter);\r
340             return;\r
341         }\r
342         \r
343         // encode limit thru 0xffff, skipping through much of the 1ff to feff range to save\r
344         // time (it would take too much time to test every possible case)\r
345         for (i = limit; i <= 0xffff; i = ((i>=0x1ff && i<0xfeff) ? i+0xfd : i+1)) {\r
346             bs = ByteBuffer.wrap(bytes, i, 1).slice();\r
347             us = CharBuffer.wrap(chars, i, 1).slice();\r
348             try {\r
349                 smBufEncode(encoder, converter, us, bs, true, false, true);\r
350                 fail = true;\r
351                 arrays = true;\r
352                 break;\r
353             } catch (Exception ex) {\r
354             }\r
355             try {\r
356                 smBufEncode(encoder, converter, us, bs, true, false, false);\r
357                 fail = true;\r
358                 arrays = false;\r
359                 break;\r
360             } catch (Exception ex) {\r
361             }\r
362         }\r
363         if (fail) {\r
364             errln("Failed to fail to encode 0x" + Integer.toHexString(i)\r
365                     + (arrays ? " with arrays" : " without arrays") + " in " + converter);\r
366             return;\r
367         }\r
368         \r
369         // test overflow / underflow edge cases\r
370         outer: for (int n = 1; n <= 3; n++) {\r
371             for (int m = 0; m < n; m++) {\r
372                 // expecting underflow\r
373                 try {\r
374                     bs = ByteBuffer.wrap(bytes, 'a', m).slice();\r
375                     us = CharBuffer.wrap(chars, 'a', m).slice();\r
376                     smBufDecode(decoder, converter, bs, us, true, false, true);\r
377                     smBufDecode(decoder, converter, bs, us, true, false, false);\r
378                     smBufEncode(encoder, converter, us, bs, true, false, true);\r
379                     smBufEncode(encoder, converter, us, bs, true, false, false);\r
380                     bs = ByteBuffer.wrap(bytes, 'a', m).slice();\r
381                     us = CharBuffer.wrap(chars, 'a', n).slice();\r
382                     smBufDecode(decoder, converter, bs, us, true, false, true, m);\r
383                     smBufDecode(decoder, converter, bs, us, true, false, false, m);\r
384                     bs = ByteBuffer.wrap(bytes, 'a', n).slice();\r
385                     us = CharBuffer.wrap(chars, 'a', m).slice();\r
386                     smBufEncode(encoder, converter, us, bs, true, false, true, m);\r
387                     smBufEncode(encoder, converter, us, bs, true, false, false, m);\r
388                     bs = ByteBuffer.wrap(bytes, 'a', n).slice();\r
389                     us = CharBuffer.wrap(chars, 'a', n).slice();\r
390                     smBufDecode(decoder, converter, bs, us, true, false, true);\r
391                     smBufDecode(decoder, converter, bs, us, true, false, false);\r
392                     smBufEncode(encoder, converter, us, bs, true, false, true);\r
393                     smBufEncode(encoder, converter, us, bs, true, false, false);\r
394                 } catch (Exception ex) {\r
395                     fail = true;\r
396                     break outer;\r
397                 }\r
398                 \r
399                 // expecting overflow\r
400                 try {\r
401                     bs = ByteBuffer.wrap(bytes, 'a', n).slice();\r
402                     us = CharBuffer.wrap(chars, 'a', m).slice();\r
403                     smBufDecode(decoder, converter, bs, us, true, false, true);\r
404                     fail = true;\r
405                     break;\r
406                 } catch (Exception ex) {\r
407                     if (!(ex instanceof BufferOverflowException)) {\r
408                         fail = true;\r
409                         break outer;\r
410                     }\r
411                 }\r
412                 try {\r
413                     bs = ByteBuffer.wrap(bytes, 'a', n).slice();\r
414                     us = CharBuffer.wrap(chars, 'a', m).slice();\r
415                     smBufDecode(decoder, converter, bs, us, true, false, false);\r
416                     fail = true;\r
417                 } catch (Exception ex) {\r
418                     if (!(ex instanceof BufferOverflowException)) {\r
419                         fail = true;\r
420                         break outer;\r
421                     }\r
422                 }\r
423                 try {\r
424                     bs = ByteBuffer.wrap(bytes, 'a', m).slice();\r
425                     us = CharBuffer.wrap(chars, 'a', n).slice();\r
426                     smBufEncode(encoder, converter, us, bs, true, false, true);\r
427                     fail = true;\r
428                 } catch (Exception ex) {\r
429                     if (!(ex instanceof BufferOverflowException)) {\r
430                         fail = true;\r
431                         break outer;\r
432                     }\r
433                 }\r
434                 try {\r
435                     bs = ByteBuffer.wrap(bytes, 'a', m).slice();\r
436                     us = CharBuffer.wrap(chars, 'a', n).slice();\r
437                     smBufEncode(encoder, converter, us, bs, true, false, false);\r
438                     fail = true;\r
439                 } catch (Exception ex) {\r
440                     if (!(ex instanceof BufferOverflowException)) {\r
441                         fail = true;\r
442                         break outer;\r
443                     }\r
444                 }\r
445             }\r
446         }\r
447         if (fail) {\r
448             errln("Incorrect result in " + converter + " for underflow / overflow edge cases");\r
449             return;\r
450         }\r
451         \r
452         // test surrogate combinations in encoding\r
453         String lead = "\ud888";\r
454         String trail = "\udc88";\r
455         String norm = "a";\r
456         String ext = "\u0275"; // theta\r
457         String end = "";\r
458         bs = ByteBuffer.wrap(new byte[] { 0 });\r
459         String[] input = new String[] { //\r
460                 lead + lead,   // malf(1)\r
461                 lead + trail,  // unmap(2)\r
462                 lead + norm,   // malf(1)\r
463                 lead + ext,    // malf(1)\r
464                 lead + end,    // malf(1)\r
465                 trail + norm,  // malf(1)\r
466                 trail + end,   // malf(1)\r
467                 ext   + norm,  // unmap(1)\r
468                 ext   + end,   // unmap(1)\r
469         };\r
470         CoderResult[] result = new CoderResult[] {\r
471                 CoderResult.malformedForLength(1),\r
472                 CoderResult.unmappableForLength(2),\r
473                 CoderResult.malformedForLength(1),\r
474                 CoderResult.malformedForLength(1),\r
475                 CoderResult.malformedForLength(1),\r
476                 CoderResult.malformedForLength(1),\r
477                 CoderResult.malformedForLength(1),\r
478                 CoderResult.unmappableForLength(1),\r
479                 CoderResult.unmappableForLength(1),\r
480         };\r
481         \r
482         for (int index = 0; index < input.length; index++) {\r
483             CharBuffer source = CharBuffer.wrap(input[index]);\r
484             cr = encoder.encode(source, bs, true);\r
485             bs.rewind();\r
486             encoder.reset();\r
487 \r
488             // if cr != results[x]\r
489             if (!((cr.isUnderflow() && result[index].isUnderflow())\r
490                     || (cr.isOverflow() && result[index].isOverflow())\r
491                     || (cr.isMalformed() && result[index].isMalformed())\r
492                     || (cr.isUnmappable() && result[index].isUnmappable()))\r
493                     || (cr.isError() && cr.length() != result[index].length())) {\r
494                 errln("Incorrect result in " + converter + " for \"" + input[index] + "\"" + ", expected: " + result[index] + ", received:  " + cr);\r
495                 break;\r
496             }\r
497 \r
498             source = CharBuffer.wrap(input[index].toCharArray());\r
499             cr = encoder.encode(source, bs, true);\r
500             bs.rewind();\r
501             encoder.reset();\r
502 \r
503             // if cr != results[x]\r
504             if (!((cr.isUnderflow() && result[index].isUnderflow())\r
505                     || (cr.isOverflow() && result[index].isOverflow())\r
506                     || (cr.isMalformed() && result[index].isMalformed())\r
507                     || (cr.isUnmappable() && result[index].isUnmappable()))\r
508                     || (cr.isError() && cr.length() != result[index].length())) {\r
509                 errln("Incorrect result in " + converter + " for \"" + input[index] + "\"" + ", expected: " + result[index] + ", received:  " + cr);\r
510                 break;\r
511             }\r
512         }\r
513     }\r
514     public void TestUTF8Converter() {\r
515         String converter = "UTF-8";\r
516         CharsetProvider icu = new CharsetProviderICU();\r
517         Charset icuChar = icu.charsetForName(converter);\r
518         CharsetEncoder encoder = icuChar.newEncoder();\r
519         CharsetDecoder decoder = icuChar.newDecoder();\r
520         ByteBuffer bs;\r
521         CharBuffer us;\r
522         CoderResult cr;\r
523 \r
524         \r
525         int[] size = new int[] { 1<<7, 1<<11, 1<<16 }; // # of 1,2,3 byte combinations\r
526         byte[] bytes = new byte[size[0] + size[1]*2 + size[2]*3];\r
527         char[] chars = new char[size[0] + size[1] + size[2]];\r
528         int i = 0;\r
529         int x, y;\r
530         \r
531         // 0 to 1 << 7 (1 byters)\r
532         for (; i < size[0]; i++) {\r
533             bytes[i] = (byte) i;\r
534             chars[i] = (char) i;\r
535             bs = ByteBuffer.wrap(bytes, i, 1).slice();\r
536             us = CharBuffer.wrap(chars, i, 1).slice();\r
537             try {\r
538                 smBufDecode(decoder, converter, bs, us, true, false, true);\r
539                 smBufDecode(decoder, converter, bs, us, true, false, false);\r
540                 smBufEncode(encoder, converter, us, bs, true, false, true);\r
541                 smBufEncode(encoder, converter, us, bs, true, false, false);\r
542             } catch (Exception ex) {\r
543                 errln("Incorrect result in " + converter + " for 0x"\r
544                         + Integer.toHexString(i));\r
545                 break;\r
546             }\r
547         }\r
548 \r
549         // 1 << 7 to 1 << 11 (2 byters)\r
550         for (; i < size[1]; i++) {\r
551             x = size[0] + i*2;\r
552             y = size[0] + i;\r
553             bytes[x + 0] = (byte) (0xc0 | ((i >> 6) & 0x1f));\r
554             bytes[x + 1] = (byte) (0x80 | ((i >> 0) & 0x3f));\r
555             chars[y] = (char) i;\r
556             bs = ByteBuffer.wrap(bytes, x, 2).slice();\r
557             us = CharBuffer.wrap(chars, y, 1).slice();\r
558             try {\r
559                 smBufDecode(decoder, converter, bs, us, true, false, true);\r
560                 smBufDecode(decoder, converter, bs, us, true, false, false);\r
561                 smBufEncode(encoder, converter, us, bs, true, false, true);\r
562                 smBufEncode(encoder, converter, us, bs, true, false, false);\r
563             } catch (Exception ex) {\r
564                 errln("Incorrect result in " + converter + " for 0x"\r
565                         + Integer.toHexString(i));\r
566                 break;\r
567             }\r
568         }\r
569 \r
570         // 1 << 11 to 1 << 16 (3 byters and surrogates)\r
571         for (; i < size[2]; i++) {\r
572             x = size[0] + size[1] * 2 + i * 3;\r
573             y = size[0] + size[1] + i;\r
574             bytes[x + 0] = (byte) (0xe0 | ((i >> 12) & 0x0f));\r
575             bytes[x + 1] = (byte) (0x80 | ((i >> 6) & 0x3f));\r
576             bytes[x + 2] = (byte) (0x80 | ((i >> 0) & 0x3f));\r
577             chars[y] = (char) i;\r
578             if (!UTF16.isSurrogate((char)i)) {\r
579                 bs = ByteBuffer.wrap(bytes, x, 3).slice();\r
580                 us = CharBuffer.wrap(chars, y, 1).slice();\r
581                 try {\r
582                     smBufDecode(decoder, converter, bs, us, true, false, true);\r
583                     smBufDecode(decoder, converter, bs, us, true, false, false);\r
584                     smBufEncode(encoder, converter, us, bs, true, false, true);\r
585                     smBufEncode(encoder, converter, us, bs, true, false, false);\r
586                 } catch (Exception ex) {\r
587                     errln("Incorrect result in " + converter + " for 0x"\r
588                             + Integer.toHexString(i));\r
589                     break;\r
590                 }\r
591             } else {\r
592                 bs = ByteBuffer.wrap(bytes, x, 3).slice();\r
593                 us = CharBuffer.wrap(chars, y, 1).slice();\r
594                 \r
595                 decoder.reset();\r
596                 cr = decoder.decode(bs, us, true);\r
597                 bs.rewind();\r
598                 us.rewind();\r
599                 if (!cr.isMalformed() || cr.length() != 3) {\r
600                     errln("Incorrect result in " + converter + " decoder for 0x"\r
601                             + Integer.toHexString(i) + " received " + cr);\r
602                     break;\r
603                 }\r
604                 encoder.reset();\r
605                 cr = encoder.encode(us, bs, true);\r
606                 bs.rewind();\r
607                 us.rewind();\r
608                 if (!cr.isMalformed() || cr.length() != 1) {\r
609                     errln("Incorrect result in " + converter + " encoder for 0x"\r
610                             + Integer.toHexString(i) + " received " + cr);\r
611                     break;\r
612                 }\r
613                 \r
614                 bs = ByteBuffer.wrap(bytes, x, 3).slice();\r
615                 us = CharBuffer.wrap(new String(chars, y, 1));\r
616                 \r
617                 decoder.reset();\r
618                 cr = decoder.decode(bs, us, true);\r
619                 bs.rewind();\r
620                 us.rewind();\r
621                 if (!cr.isMalformed() || cr.length() != 3) {\r
622                     errln("Incorrect result in " + converter + " decoder for 0x"\r
623                             + Integer.toHexString(i) + " received " + cr);\r
624                     break;\r
625                 }\r
626                 encoder.reset();\r
627                 cr = encoder.encode(us, bs, true);\r
628                 bs.rewind();\r
629                 us.rewind();\r
630                 if (!cr.isMalformed() || cr.length() != 1) {\r
631                     errln("Incorrect result in " + converter + " encoder for 0x"\r
632                             + Integer.toHexString(i) + " received " + cr);\r
633                     break;\r
634                 }\r
635                 \r
636                 \r
637             }\r
638         }\r
639         if (true)\r
640             return;\r
641     }\r
642     \r
643     public void TestHZ() {\r
644         /* test input */\r
645         char[] in = new char[] {\r
646                 0x3000, 0x3001, 0x3002, 0x00B7, 0x02C9, 0x02C7, 0x00A8, 0x3003, 0x3005, 0x2014,\r
647                 0xFF5E, 0x2016, 0x2026, 0x007E, 0x997C, 0x70B3, 0x75C5, 0x5E76, 0x73BB, 0x83E0,\r
648                 0x64AD, 0x62E8, 0x94B5, 0x000A, 0x6CE2, 0x535A, 0x52C3, 0x640F, 0x94C2, 0x7B94,\r
649                 0x4F2F, 0x5E1B, 0x8236, 0x000A, 0x8116, 0x818A, 0x6E24, 0x6CCA, 0x9A73, 0x6355,\r
650                 0x535C, 0x54FA, 0x8865, 0x000A, 0x57E0, 0x4E0D, 0x5E03, 0x6B65, 0x7C3F, 0x90E8,\r
651                 0x6016, 0x248F, 0x2490, 0x000A, 0x2491, 0x2492, 0x2493, 0x2494, 0x2495, 0x2496,\r
652                 0x2497, 0x2498, 0x2499, 0x000A, 0x249A, 0x249B, 0x2474, 0x2475, 0x2476, 0x2477,\r
653                 0x2478, 0x2479, 0x247A, 0x000A, 0x247B, 0x247C, 0x247D, 0x247E, 0x247F, 0x2480,\r
654                 0x2481, 0x2482, 0x2483, 0x000A, 0x0041, 0x0043, 0x0044, 0x0045, 0x0046, 0x007E,\r
655                 0x0048, 0x0049, 0x004A, 0x000A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,\r
656                 0x0051, 0x0052, 0x0053, 0x000A, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,\r
657                 0x005A, 0x005B, 0x005C, 0x000A\r
658           };\r
659         \r
660         String converter = "HZ";\r
661         CharsetProvider icu = new CharsetProviderICU();\r
662         Charset icuChar = icu.charsetForName(converter);\r
663         CharsetEncoder encoder = icuChar.newEncoder();\r
664         CharsetDecoder decoder = icuChar.newDecoder();\r
665         try {\r
666             CharBuffer start = CharBuffer.wrap(in);\r
667             ByteBuffer bytes = encoder.encode(start);\r
668             CharBuffer finish = decoder.decode(bytes);\r
669             \r
670             if (!equals(start, finish)) {\r
671                 errln(converter + " roundtrip test failed: start does not match finish");\r
672                 \r
673                 char[] finishArray = new char[finish.limit()];\r
674                 for (int i=0; i<finishArray.length; i++)\r
675                     finishArray[i] = finish.get(i);\r
676                 \r
677                 logln("start:  " + hex(in));\r
678                 logln("finish: " + hex(finishArray));\r
679             }\r
680         } catch (CharacterCodingException ex) {\r
681             errln(converter + " roundtrip test failed: " + ex.getMessage());\r
682             ex.printStackTrace(System.err);\r
683         }\r
684         \r
685         /* For better code coverage */\r
686         CoderResult result = CoderResult.UNDERFLOW;\r
687         byte byteout[] = {\r
688                 (byte)0x7e, (byte)0x7d, (byte)0x41,\r
689                 (byte)0x7e, (byte)0x7b, (byte)0x21,\r
690         };\r
691         char charin[] = {\r
692                 (char)0x0041, (char)0x0042, (char)0x3000\r
693         };\r
694         ByteBuffer bb = ByteBuffer.wrap(byteout);\r
695         CharBuffer cb = CharBuffer.wrap(charin);\r
696         int testLoopSize = 5;\r
697         int bbLimits[] = { 0, 1, 3, 4, 6};\r
698         int bbPositions[] = { 0, 0, 0, 3, 3 };\r
699         int ccPositions[] = { 0, 0, 0, 2, 2 };\r
700         for (int i = 0; i < testLoopSize; i++) {\r
701             encoder.reset();\r
702             bb.limit(bbLimits[i]);\r
703             bb.position(bbPositions[i]);\r
704             cb.position(ccPositions[i]);\r
705             result = encoder.encode(cb, bb, true);\r
706             \r
707             if (i < 3) {\r
708                 if (!result.isOverflow()) {\r
709                     errln("Overflow buffer error should have occurred while encoding HZ (" + i + ")");\r
710                 }\r
711             } else {\r
712                 if (result.isError()) {\r
713                     errln("Error should not have occurred while encoding HZ.(" + i + ")");\r
714                 }\r
715             }\r
716         }\r
717     }\r
718 \r
719     public void TestUTF8Surrogates() {\r
720         byte[][] in = new byte[][] {\r
721             { (byte)0x61, },\r
722             { (byte)0xc2, (byte)0x80, },\r
723             { (byte)0xe0, (byte)0xa0, (byte)0x80, },\r
724             { (byte)0xf0, (byte)0x90, (byte)0x80, (byte)0x80, },\r
725             { (byte)0xf4, (byte)0x84, (byte)0x8c, (byte)0xa1, },\r
726             { (byte)0xf0, (byte)0x90, (byte)0x90, (byte)0x81, },\r
727         };\r
728 \r
729         /* expected test results */\r
730         char[][] results = new char[][] {\r
731             /* number of bytes read, code point */\r
732             { '\u0061', },\r
733             { '\u0080', },\r
734             { '\u0800', },\r
735             { '\ud800', '\udc00', },      //  10000\r
736             { '\udbd0', '\udf21', },      // 104321\r
737             { '\ud801', '\udc01', },      //  10401\r
738         };\r
739 \r
740         /* error test input */\r
741         byte[][] in2 = new byte[][] {\r
742             { (byte)0x61, },\r
743             { (byte)0xc0, (byte)0x80,                                     /* illegal non-shortest form */\r
744             (byte)0xe0, (byte)0x80, (byte)0x80,                           /* illegal non-shortest form */\r
745             (byte)0xf0, (byte)0x80, (byte)0x80, (byte)0x80,               /* illegal non-shortest form */\r
746             (byte)0xc0, (byte)0xc0,                                       /* illegal trail byte */\r
747             (byte)0xf4, (byte)0x90, (byte)0x80, (byte)0x80,               /* 0x110000 out of range */\r
748             (byte)0xf8, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80,   /* too long */\r
749             (byte)0xfe,                                                   /* illegal byte altogether */\r
750             (byte)0x62, },\r
751         };\r
752 \r
753         /* expected error test results */\r
754         char[][] results2 = new char[][] {\r
755             /* number of bytes read, code point */\r
756             { '\u0062', },\r
757             { '\u0062', },\r
758         };\r
759         \r
760         String converter = "UTF-8";\r
761         CharsetProvider icu = new CharsetProviderICU();\r
762         Charset icuChar = icu.charsetForName(converter);\r
763         CharsetDecoder decoder = icuChar.newDecoder();\r
764         \r
765         int i;\r
766         try {\r
767             for (i = 0; i < in.length; i++) {\r
768                 ByteBuffer source = ByteBuffer.wrap(in[i]);\r
769                 CharBuffer expected = CharBuffer.wrap(results[i]);\r
770                 smBufDecode(decoder, converter, source, expected, true, false,\r
771                         true);\r
772                 smBufDecode(decoder, converter, source, expected, true, false,\r
773                         false);\r
774             }\r
775         } catch (Exception ex) {\r
776             errln("Incorrect result in " + converter);\r
777         }\r
778         try {\r
779             for (i = 0; i < in2.length; i++) {\r
780                 ByteBuffer source = ByteBuffer.wrap(in2[i]);\r
781                 CharBuffer expected = CharBuffer.wrap(results2[i]);\r
782                 decoder.onMalformedInput(CodingErrorAction.IGNORE);\r
783                 smBufDecode(decoder, converter, source, expected, true, false,\r
784                         true);\r
785                 smBufDecode(decoder, converter, source, expected, true, false,\r
786                         false);\r
787             }\r
788         } catch (Exception ex) {\r
789             errln("Incorrect result in " + converter);\r
790         }\r
791     }\r
792     \r
793     public void TestSurrogateBehavior() {\r
794         CharsetProviderICU icu = new CharsetProviderICU();\r
795         \r
796         // get all the converters into an array\r
797         Object[] converters = CharsetProviderICU.getAvailableNames();\r
798         \r
799         String norm = "a";\r
800         String ext = "\u0275"; // theta\r
801         String lead = "\ud835";\r
802         String trail = "\udd04";\r
803         // lead + trail = \U1d504 (fraktur capital A)\r
804         \r
805         String input = \r
806                         // error    position\r
807                 ext     // unmap(1) 1\r
808                 + lead  // under    1  \r
809                 + lead  // malf(1)  2\r
810                 + trail // unmap(2) 4\r
811                 + trail // malf(1)  5\r
812                 + ext   // unmap(1) 6\r
813                 + norm  // unmap(1) 7\r
814         ;\r
815         CoderResult[] results = new CoderResult[] {\r
816                 CoderResult.unmappableForLength(1), // or underflow\r
817                 CoderResult.UNDERFLOW,\r
818                 CoderResult.malformedForLength(1),\r
819                 CoderResult.unmappableForLength(2), // or underflow\r
820                 CoderResult.malformedForLength(1),\r
821                 CoderResult.unmappableForLength(1), // or underflow\r
822                 CoderResult.unmappableForLength(1), // or underflow\r
823         };\r
824         int[] positions = new int[] { 1,1,2,4,5,6,7 };\r
825         int n = positions.length;\r
826         \r
827         int badcount = 0;\r
828         int goodcount = 0;\r
829         int[] uhohindices = new int[n];\r
830         int[] badposindices = new int[n];\r
831         int[] malfindices = new int[n];\r
832         int[] unmapindices = new int[n];\r
833         ArrayList pass = new ArrayList();\r
834         ArrayList exempt = new ArrayList();\r
835         \r
836         outer: for (int conv=0; conv<converters.length; conv++) {\r
837             String converter = (String)converters[conv];\r
838             if (converter.equals("x-IMAP-mailbox-name") || converter.equals("UTF-7") || converter.equals("CESU-8") || converter.equals("BOCU-1") ||\r
839                     converter.equals("x-LMBCS-1")) {\r
840                 exempt.add(converter);\r
841                 continue;\r
842             }\r
843             \r
844             boolean currentlybad = false;\r
845             Charset icuChar = icu.charsetForName(converter);\r
846             CharsetEncoder encoder = icuChar.newEncoder();\r
847             CoderResult cr;\r
848                 \r
849             CharBuffer source = CharBuffer.wrap(input);\r
850             ByteBuffer target = ByteBuffer.allocate(30);\r
851             ByteBuffer expected = null;\r
852             try {\r
853                 encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);\r
854                 encoder.onMalformedInput(CodingErrorAction.IGNORE);\r
855                 expected = encoder.encode(CharBuffer.wrap(ext + lead + trail + ext + norm));\r
856                 encoder.reset();\r
857             } catch (CharacterCodingException ex) {\r
858                 errln("Unexpected CharacterCodingException: " + ex.getMessage());\r
859                 return;\r
860             } catch (RuntimeException ex) {\r
861                 if (!currentlybad) {currentlybad = true; badcount++; logln(""); }\r
862                 errln(converter + " " + ex.getClass().getName() + ": " + ex.getMessage());\r
863                 continue outer;\r
864             }\r
865             \r
866             encoder.onUnmappableCharacter(CodingErrorAction.REPORT);\r
867             encoder.onMalformedInput(CodingErrorAction.REPORT);\r
868             for (int i=0; i<n; i++) {\r
869                 source.limit(i+1);\r
870                 cr = encoder.encode(source, target, i == n - 1);\r
871                 if (!(equals(cr, results[i])\r
872                         || (results[i].isUnmappable() && cr.isUnderflow()) // mappability depends on the converter\r
873                     )) {\r
874                     if (!currentlybad) {currentlybad = true; badcount++; logln(""); }\r
875                     if (results[i].isMalformed() && cr.isMalformed()) {\r
876                         malfindices[i]++;\r
877                     } else if (results[i].isUnmappable() && cr.isUnmappable()) {\r
878                         unmapindices[i]++;\r
879                     } else {\r
880                         uhohindices[i]++;\r
881                     }\r
882                     errln("(index=" + i + ") " + converter + " Received: " + cr + " Expected: " + results[i]);\r
883                 }\r
884                 if (source.position() != positions[i]) {\r
885                     if (!currentlybad) {currentlybad = true; badcount++; logln(""); }\r
886                     badposindices[i]++;\r
887                     errln("(index=" + i + ") " + converter + " Received: " + source.position() + " Expected: " + positions[i]);\r
888                 }\r
889                     \r
890             }\r
891             encoder.reset();\r
892             \r
893             //System.out.println("\n" + hex(target.array()));\r
894             //System.out.println(hex(expected.array()) + "\n" + expected.limit());\r
895             if (!(equals(target, expected, expected.limit()) && target.position() == expected.limit())) {\r
896                 if (!currentlybad) {currentlybad = true; badcount++; logln(""); }\r
897                 errln(converter + " Received: \"" + hex(target.array()) + "\" Expected: \"" + hex(expected.array()) + "\"");\r
898             }\r
899             \r
900             if (!currentlybad) {\r
901                 goodcount++;\r
902                 pass.add(converter);\r
903             }\r
904         }\r
905         \r
906         logln("\n" + badcount + " / " + (converters.length - exempt.size()) + "   (" + goodcount + " good, " + badcount + " bad)");\r
907         log("index\t"); for (int i=0; i<n; i++) log(i + "\t"); logln("");\r
908         log("unmap\t"); for (int i=0; i<n; i++) log(unmapindices[i] + "\t"); logln("");\r
909         log("malf \t"); for (int i=0; i<n; i++) log(malfindices[i] + "\t"); logln("");\r
910         log("pos  \t"); for (int i=0; i<n; i++) log(badposindices[i] + "\t"); logln("");\r
911         log("uhoh \t"); for (int i=0; i<n; i++) log(uhohindices[i] + "\t"); logln("");\r
912         logln("");\r
913         log("The few that passed: "); for (int i=0; i<pass.size(); i++) log(pass.get(i) + ", "); logln(""); \r
914         log("The few that are exempt: "); for (int i=0; i<exempt.size(); i++) log(exempt.get(i) + ", "); logln(""); \r
915     }\r
916     \r
917 //    public void TestCharsetCallback() {\r
918 //        String currentTest = "initialization";\r
919 //        try {\r
920 //            Class[] params;\r
921 //            \r
922 //            // get the classes\r
923 //            Class CharsetCallback = Class.forName("com.ibm.icu.charset.CharsetCallback");\r
924 //            Class Decoder = Class.forName("com.ibm.icu.charset.CharsetCallback$Decoder");\r
925 //            Class Encoder = Class.forName("com.ibm.icu.charset.CharsetCallback$Encoder");\r
926 //            \r
927 //            // set up encoderCall\r
928 //            params = new Class[] {CharsetEncoderICU.class, Object.class, \r
929 //                    CharBuffer.class, ByteBuffer.class, IntBuffer.class, \r
930 //                    char[].class, int.class, int.class, CoderResult.class };\r
931 //            Method encoderCall = Encoder.getDeclaredMethod("call", params);\r
932 //            \r
933 //            // set up decoderCall\r
934 //            params = new Class[] {CharsetDecoderICU.class, Object.class, \r
935 //                    ByteBuffer.class, CharBuffer.class, IntBuffer.class,\r
936 //                    char[].class, int.class, CoderResult.class};\r
937 //            Method decoderCall = Decoder.getDeclaredMethod("call", params);\r
938 //            \r
939 //            // get relevant fields\r
940 //            Object SUB_STOP_ON_ILLEGAL = getFieldValue(CharsetCallback, "SUB_STOP_ON_ILLEGAL", null);\r
941 //            \r
942 //            // set up a few arguments\r
943 //            CharsetProvider provider = new CharsetProviderICU();\r
944 //            Charset charset = provider.charsetForName("UTF-8");\r
945 //            CharsetEncoderICU encoder = (CharsetEncoderICU)charset.newEncoder();\r
946 //            CharsetDecoderICU decoder = (CharsetDecoderICU)charset.newDecoder();\r
947 //            CharBuffer chars = CharBuffer.allocate(10);\r
948 //            chars.put('o');\r
949 //            chars.put('k');\r
950 //            ByteBuffer bytes = ByteBuffer.allocate(10);\r
951 //            bytes.put((byte)'o');\r
952 //            bytes.put((byte)'k');\r
953 //            IntBuffer offsets = IntBuffer.allocate(10);\r
954 //            offsets.put(0);\r
955 //            offsets.put(1);\r
956 //            char[] buffer = null;\r
957 //            Integer length = new Integer(2);\r
958 //            Integer cp = new Integer(0);\r
959 //            CoderResult unmap = CoderResult.unmappableForLength(2);\r
960 //            CoderResult malf = CoderResult.malformedForLength(2);\r
961 //            CoderResult under = CoderResult.UNDERFLOW;\r
962 //            \r
963 //            // set up error arrays\r
964 //            Integer invalidCharLength = new Integer(1);\r
965 //            Byte subChar1 = new Byte((byte)0);\r
966 //            Byte subChar1_alternate = new Byte((byte)1); // for TO_U_CALLBACK_SUBSTITUTE\r
967 //            \r
968 //            // set up chars and bytes backups and expected values for certain cases\r
969 //            CharBuffer charsBackup = bufferCopy(chars);\r
970 //            ByteBuffer bytesBackup = bufferCopy(bytes);\r
971 //            IntBuffer offsetsBackup = bufferCopy(offsets);\r
972 //            CharBuffer encoderCharsExpected = bufferCopy(chars);\r
973 //            ByteBuffer encoderBytesExpected = bufferCopy(bytes);\r
974 //            IntBuffer encoderOffsetsExpected = bufferCopy(offsets);\r
975 //            CharBuffer decoderCharsExpected1 = bufferCopy(chars);\r
976 //            CharBuffer decoderCharsExpected2 = bufferCopy(chars);\r
977 //            IntBuffer decoderOffsetsExpected1 = bufferCopy(offsets);\r
978 //            IntBuffer decoderOffsetsExpected2 = bufferCopy(offsets);\r
979 //            \r
980 //            // initialize fields to obtain expected data\r
981 //            setFieldValue(CharsetDecoderICU.class, "invalidCharLength", decoder, invalidCharLength);\r
982 //            setFieldValue(CharsetICU.class, "subChar1", ((CharsetICU) decoder.charset()), subChar1);\r
983 //            \r
984 //            // run cbFromUWriteSub\r
985 //            Method cbFromUWriteSub = CharsetEncoderICU.class.getDeclaredMethod("cbFromUWriteSub", new Class[] { CharsetEncoderICU.class, CharBuffer.class, ByteBuffer.class, IntBuffer.class});\r
986 //            cbFromUWriteSub.setAccessible(true);\r
987 //            CoderResult encoderResultExpected = (CoderResult)cbFromUWriteSub.invoke(encoder, new Object[] {encoder, encoderCharsExpected, encoderBytesExpected, encoderOffsetsExpected});\r
988 //            \r
989 //            // run toUWriteUChars with normal data\r
990 //            Method toUWriteUChars = CharsetDecoderICU.class.getDeclaredMethod("toUWriteUChars", new Class[] { CharsetDecoderICU.class, char[].class, int.class, int.class, CharBuffer.class, IntBuffer.class, int.class});\r
991 //            toUWriteUChars.setAccessible(true);\r
992 //            CoderResult decoderResultExpected1 = (CoderResult)toUWriteUChars.invoke(decoder, new Object[] {decoder, new char[] {0xFFFD}, new Integer(0), new Integer(1), decoderCharsExpected1, decoderOffsetsExpected1, new Integer(bytes.position())});\r
993 //            \r
994 //            // reset certain fields\r
995 //            setFieldValue(CharsetDecoderICU.class, "invalidCharLength", decoder, invalidCharLength);\r
996 //            setFieldValue(CharsetICU.class, "subChar1", ((CharsetICU) decoder.charset()), subChar1_alternate);\r
997 //            \r
998 //            // run toUWriteUChars again\r
999 //            CoderResult decoderResultExpected2 = (CoderResult)toUWriteUChars.invoke(decoder, new Object[] {decoder, new char[] {0x1A}, new Integer(0), new Integer(1), decoderCharsExpected2, decoderOffsetsExpected2, new Integer(bytes.position())});\r
1000 //            \r
1001 //            // begin creating the tests array\r
1002 //            ArrayList tests = new ArrayList();\r
1003 //            \r
1004 //            // create tests for FROM_U_CALLBACK_SKIP   0\r
1005 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SKIP", new Object[] { encoder, null, chars, bytes, offsets, buffer, length, cp, null }, under, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1006 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SKIP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, malf }, malf, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1007 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SKIP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, unmap }, under, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1008 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SKIP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL + "xx", chars, bytes, offsets, buffer, length, cp, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1009 //            \r
1010 //            // create tests for TO_U_CALLBACK_SKIP    4\r
1011 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SKIP", new Object[] { decoder, null, bytes, chars, offsets, buffer, length, null }, under, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1012 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SKIP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL, bytes, chars, offsets, buffer, length, malf }, malf, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1013 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SKIP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL, bytes, chars, offsets, buffer, length, unmap }, under, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1014 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SKIP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL + "xx", bytes, chars, offsets, buffer, length, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1015 //            \r
1016 //            // create tests for FROM_U_CALLBACK_STOP   8\r
1017 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_STOP", new Object[] { encoder, null, chars, bytes, offsets, buffer, length, cp, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1018 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_STOP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, malf }, malf, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1019 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_STOP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, unmap }, unmap, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1020 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_STOP", new Object[] { encoder, SUB_STOP_ON_ILLEGAL + "xx", chars, bytes, offsets, buffer, length, cp, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1021 //            \r
1022 //            // create tests for TO_U_CALLBACK_STOP   12\r
1023 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_STOP", new Object[] { decoder, null, bytes, chars, offsets, buffer, length, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1024 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_STOP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL, bytes, chars, offsets, buffer, length, malf }, malf, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1025 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_STOP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL, bytes, chars, offsets, buffer, length, unmap }, unmap, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1026 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_STOP", new Object[] { decoder, SUB_STOP_ON_ILLEGAL + "xx", bytes, chars, offsets, buffer, length, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { invalidCharLength, subChar1 }});\r
1027 //            \r
1028 //            // create tests for FROM_U_CALLBACK_SUBSTITUTE  16\r
1029 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SUBSTITUTE", new Object[] { encoder, null, chars, bytes, offsets, buffer, length, cp, null }, encoderResultExpected, encoderCharsExpected, encoderBytesExpected, encoderOffsetsExpected, new Object[] { }});\r
1030 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SUBSTITUTE", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, malf }, malf, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1031 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SUBSTITUTE", new Object[] { encoder, SUB_STOP_ON_ILLEGAL, chars, bytes, offsets, buffer, length, cp, unmap }, encoderResultExpected, encoderCharsExpected, encoderBytesExpected, encoderOffsetsExpected, new Object[] { }});\r
1032 //            tests.add(new Object[] {encoderCall, "FROM_U_CALLBACK_SUBSTITUTE", new Object[] { encoder, SUB_STOP_ON_ILLEGAL + "xx", chars, bytes, offsets, buffer, length, cp, null }, null, charsBackup, bytesBackup, offsetsBackup, new Object[] { }});\r
1033 //            \r
1034 //            // create tests for TO_U_CALLBACK_SUBSTITUTE   20\r
1035 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SUBSTITUTE", new Object[] { decoder, null, bytes, chars, offsets, buffer, length, null }, decoderResultExpected1, decoderCharsExpected1, bytesBackup, decoderOffsetsExpected1, new Object[] { invalidCharLength, subChar1 }});\r
1036 //            tests.add(new Object[] {decoderCall, "TO_U_CALLBACK_SUBSTITUTE", new Object[] { decoder, null, bytes, chars, offsets, buffer, length, null }, decoderResultExpected2, decoderCharsExpected2, bytesBackup, decoderOffsetsExpected2, new Object[] { invalidCharLength, subChar1_alternate }});\r
1037 //            \r
1038 //            Iterator iter = tests.iterator();\r
1039 //            for (int i=0; iter.hasNext(); i++) {\r
1040 //                // get the data out of the map\r
1041 //                Object[] next = (Object[])iter.next();\r
1042 //                \r
1043 //                Method method = (Method)next[0];\r
1044 //                String fieldName = (String)next[1];\r
1045 //                Object field = getFieldValue(CharsetCallback, fieldName, null);\r
1046 //                Object[] args = (Object[])next[2];\r
1047 //                CoderResult expected = (CoderResult)next[3];\r
1048 //                CharBuffer charsExpected = (CharBuffer)next[4];\r
1049 //                ByteBuffer bytesExpected = (ByteBuffer)next[5];\r
1050 //                IntBuffer offsetsExpected = (IntBuffer)next[6];\r
1051 //                \r
1052 //                // set up error arrays and certain fields\r
1053 //                Object[] values = (Object[])next[7];\r
1054 //                if (method == decoderCall) {\r
1055 //                    decoder.reset();\r
1056 //                    setFieldValue(CharsetDecoderICU.class, "invalidCharLength", decoder, values[0]);\r
1057 //                    setFieldValue(CharsetICU.class, "subChar1", ((CharsetICU) decoder.charset()), values[1]);\r
1058 //                } else if (method == encoderCall) {\r
1059 //                    encoder.reset();\r
1060 //                }\r
1061 //                \r
1062 //                try {\r
1063 //                    // invoke the method\r
1064 //                    CoderResult actual = (CoderResult)method.invoke(field, args);\r
1065 //                    \r
1066 //                    // if expected != actual\r
1067 //                    if (!coderResultsEqual(expected, actual)) {\r
1068 //                        // case #i refers to the index in the arraylist tests\r
1069 //                        errln(fieldName + " failed to return the correct result for case #" + i + ".");\r
1070 //                    }\r
1071 //                    // if the expected buffers != actual buffers\r
1072 //                    else if (!(buffersEqual(chars, charsExpected) && \r
1073 //                            buffersEqual(bytes, bytesExpected) &&\r
1074 //                            buffersEqual(offsets, offsetsExpected))) {\r
1075 //                        // case #i refers to the index in the arraylist tests\r
1076 //                        errln(fieldName + " did not perform the correct operation on the buffers for case #" + i + ".");\r
1077 //                    }\r
1078 //                } catch (InvocationTargetException ex)  {\r
1079 //                    // case #i refers to the index in the arraylist tests\r
1080 //                    errln(fieldName + " threw an exception for case #" + i + ": " + ex.getCause());\r
1081 //                    //ex.getCause().printStackTrace();\r
1082 //                }\r
1083 //                \r
1084 //                // reset the buffers\r
1085 //                System.arraycopy(bytesBackup.array(), 0, bytes.array(), 0, 10);\r
1086 //                System.arraycopy(charsBackup.array(), 0, chars.array(), 0, 10);\r
1087 //                System.arraycopy(offsetsBackup.array(), 0, offsets.array(), 0, 10);\r
1088 //                bytes.position(bytesBackup.position());\r
1089 //                chars.position(charsBackup.position());\r
1090 //                offsets.position(offsetsBackup.position());\r
1091 //            }\r
1092 //            \r
1093 //        } catch (Exception ex) {\r
1094 //            errln("TestCharsetCallback skipped due to " + ex.toString());\r
1095 //            ex.printStackTrace();\r
1096 //        }\r
1097 //    }\r
1098 //    \r
1099 //    private Object getFieldValue(Class c, String name, Object instance) throws Exception {\r
1100 //        Field field = c.getDeclaredField(name);\r
1101 //        field.setAccessible(true);\r
1102 //        return field.get(instance);\r
1103 //    }\r
1104 //    private void setFieldValue(Class c, String name, Object instance, Object value) throws Exception {\r
1105 //        Field field = c.getDeclaredField(name);\r
1106 //        field.setAccessible(true);\r
1107 //        if (value instanceof Boolean)\r
1108 //            field.setBoolean(instance, ((Boolean)value).booleanValue());\r
1109 //        else if (value instanceof Byte)\r
1110 //            field.setByte(instance, ((Byte)value).byteValue());\r
1111 //        else if (value instanceof Character)\r
1112 //            field.setChar(instance, ((Character)value).charValue());\r
1113 //        else if (value instanceof Double)\r
1114 //            field.setDouble(instance, ((Double)value).doubleValue());\r
1115 //        else if (value instanceof Float)\r
1116 //            field.setFloat(instance, ((Float)value).floatValue());\r
1117 //        else if (value instanceof Integer)\r
1118 //            field.setInt(instance, ((Integer)value).intValue());\r
1119 //        else if (value instanceof Long)\r
1120 //            field.setLong(instance, ((Long)value).longValue());\r
1121 //        else if (value instanceof Short)\r
1122 //            field.setShort(instance, ((Short)value).shortValue());\r
1123 //        else\r
1124 //            field.set(instance, value);\r
1125 //    }\r
1126 //    private boolean coderResultsEqual(CoderResult a, CoderResult b) {\r
1127 //        if (a == null && b == null)\r
1128 //            return true;\r
1129 //        if (a == null || b == null)\r
1130 //            return false;\r
1131 //        if ((a.isUnderflow() && b.isUnderflow()) || (a.isOverflow() && b.isOverflow()))\r
1132 //            return true;\r
1133 //        if (a.length() != b.length())\r
1134 //            return false;\r
1135 //        if ((a.isMalformed() && b.isMalformed()) || (a.isUnmappable() && b.isUnmappable()))\r
1136 //            return true;\r
1137 //        return false;\r
1138 //    }\r
1139 //    private boolean buffersEqual(ByteBuffer a, ByteBuffer b) {\r
1140 //        if (a.position() != b.position())\r
1141 //            return false;\r
1142 //        int limit = a.position();\r
1143 //        for (int i=0; i<limit; i++)\r
1144 //            if (a.get(i) != b.get(i))\r
1145 //                return false;\r
1146 //        return true;\r
1147 //    }\r
1148 //    private boolean buffersEqual(CharBuffer a, CharBuffer b) {\r
1149 //        if (a.position() != b.position())\r
1150 //            return false;\r
1151 //        int limit = a.position();\r
1152 //        for (int i=0; i<limit; i++)\r
1153 //            if (a.get(i) != b.get(i))\r
1154 //                return false;\r
1155 //        return true;\r
1156 //    }\r
1157 //    private boolean buffersEqual(IntBuffer a, IntBuffer b) {\r
1158 //        if (a.position() != b.position())\r
1159 //            return false;\r
1160 //        int limit = a.position();\r
1161 //        for (int i=0; i<limit; i++)\r
1162 //            if (a.get(i) != b.get(i))\r
1163 //                return false;\r
1164 //        return true;\r
1165 //    }\r
1166 //    private ByteBuffer bufferCopy(ByteBuffer src) {\r
1167 //        ByteBuffer dest = ByteBuffer.allocate(src.limit());\r
1168 //        System.arraycopy(src.array(), 0, dest.array(), 0, src.limit());\r
1169 //        dest.position(src.position());\r
1170 //        return dest;\r
1171 //    }\r
1172 //    private CharBuffer bufferCopy(CharBuffer src) {\r
1173 //        CharBuffer dest = CharBuffer.allocate(src.limit());\r
1174 //        System.arraycopy(src.array(), 0, dest.array(), 0, src.limit());\r
1175 //        dest.position(src.position());\r
1176 //        return dest;\r
1177 //    }\r
1178 //    private IntBuffer bufferCopy(IntBuffer src) {\r
1179 //        IntBuffer dest = IntBuffer.allocate(src.limit());\r
1180 //        System.arraycopy(src.array(), 0, dest.array(), 0, src.limit());\r
1181 //        dest.position(src.position());\r
1182 //        return dest;\r
1183 //    }\r
1184     \r
1185 \r
1186     public void TestAPISemantics(/*String encoding*/) \r
1187                 throws Exception {\r
1188         int rc;\r
1189         ByteBuffer byes = ByteBuffer.wrap(byteStr);\r
1190         CharBuffer uniVal = CharBuffer.wrap(unistr);\r
1191         ByteBuffer expected = ByteBuffer.wrap(expectedByteStr);\r
1192         \r
1193         rc = 0;\r
1194         if(m_decoder==null){\r
1195             warnln("Could not load decoder.");\r
1196             return;\r
1197         }\r
1198         m_decoder.reset();\r
1199         /* Convert the whole buffer to Unicode */\r
1200         try {\r
1201             CharBuffer chars = CharBuffer.allocate(unistr.length());\r
1202             CoderResult result = m_decoder.decode(byes, chars, false);\r
1203 \r
1204             if (result.isError()) {\r
1205                 errln("ToChars encountered Error");\r
1206                 rc = 1;\r
1207             }\r
1208             if (result.isOverflow()) {\r
1209                 errln("ToChars encountered overflow exception");\r
1210                 rc = 1;\r
1211             }\r
1212             if (!equals(chars, unistr)) {\r
1213                 errln("ToChars does not match");\r
1214                 printchars(chars);\r
1215                 errln("Expected : ");\r
1216                 printchars(unistr);\r
1217                 rc = 2;\r
1218             }\r
1219 \r
1220         } catch (Exception e) {\r
1221             errln("ToChars - exception in buffer");\r
1222             rc = 5;\r
1223         }\r
1224 \r
1225         /* Convert single bytes to Unicode */\r
1226         try {\r
1227             CharBuffer chars = CharBuffer.allocate(unistr.length());\r
1228             ByteBuffer b = ByteBuffer.wrap(byteStr);\r
1229             m_decoder.reset();\r
1230             CoderResult result=null;\r
1231             for (int i = 1; i <= byteStr.length; i++) {\r
1232                 b.limit(i);\r
1233                 result = m_decoder.decode(b, chars, false);\r
1234                 if(result.isOverflow()){\r
1235                     errln("ToChars single threw an overflow exception");\r
1236                 }\r
1237                 if (result.isError()) {\r
1238                     errln("ToChars single the result is an error "+result.toString());\r
1239                 } \r
1240             }\r
1241             if (unistr.length() != (chars.limit())) {\r
1242                 errln("ToChars single len does not match");\r
1243                 rc = 3;\r
1244             }\r
1245             if (!equals(chars, unistr)) {\r
1246                 errln("ToChars single does not match");\r
1247                 printchars(chars);\r
1248                 rc = 4;\r
1249             }\r
1250         } catch (Exception e) {\r
1251             errln("ToChars - exception in single");\r
1252             //e.printStackTrace();\r
1253             rc = 6;\r
1254         }\r
1255 \r
1256         /* Convert the buffer one at a time to Unicode */\r
1257         try {\r
1258             CharBuffer chars = CharBuffer.allocate(unistr.length());\r
1259             m_decoder.reset();\r
1260             byes.rewind();\r
1261             for (int i = 1; i <= byteStr.length; i++) {\r
1262                 byes.limit(i);\r
1263                 CoderResult result = m_decoder.decode(byes, chars, false);\r
1264                 if (result.isError()) {\r
1265                     errln("Error while decoding: "+result.toString());\r
1266                 }\r
1267                 if(result.isOverflow()){\r
1268                     errln("ToChars Simple threw an overflow exception");\r
1269                 }\r
1270             }\r
1271             if (chars.limit() != unistr.length()) {\r
1272                 errln("ToChars Simple buffer len does not match");\r
1273                 rc = 7;\r
1274             }\r
1275             if (!equals(chars, unistr)) {\r
1276                 errln("ToChars Simple buffer does not match");\r
1277                 printchars(chars);\r
1278                 err(" Expected : ");\r
1279                 printchars(unistr);\r
1280                 rc = 8;\r
1281             }\r
1282         } catch (Exception e) {\r
1283             errln("ToChars - exception in single buffer");\r
1284             //e.printStackTrace(System.err);\r
1285             rc = 9;\r
1286         }\r
1287         if (rc != 0) {\r
1288             errln("Test Simple ToChars for encoding : FAILED");\r
1289         }\r
1290 \r
1291         rc = 0;\r
1292         /* Convert the whole buffer from unicode */\r
1293         try {\r
1294             ByteBuffer bytes = ByteBuffer.allocate(expectedByteStr.length);\r
1295             m_encoder.reset();\r
1296             CoderResult result = m_encoder.encode(uniVal, bytes, false);\r
1297             if (result.isError()) {\r
1298                 errln("FromChars reported error: " + result.toString());\r
1299                 rc = 1;\r
1300             }\r
1301             if(result.isOverflow()){\r
1302                 errln("FromChars threw an overflow exception");\r
1303             }\r
1304             bytes.position(0);\r
1305             if (!bytes.equals(expected)) {\r
1306                 errln("FromChars does not match");\r
1307                 printbytes(bytes);\r
1308                 printbytes(expected);\r
1309                 rc = 2;\r
1310             }\r
1311         } catch (Exception e) {\r
1312             errln("FromChars - exception in buffer");\r
1313             //e.printStackTrace(System.err);\r
1314             rc = 5;\r
1315         }\r
1316 \r
1317         /* Convert the buffer one char at a time to unicode */\r
1318         try {\r
1319             ByteBuffer bytes = ByteBuffer.allocate(expectedByteStr.length);\r
1320             CharBuffer c = CharBuffer.wrap(unistr);\r
1321             m_encoder.reset();\r
1322             CoderResult result= null;\r
1323             for (int i = 1; i <= unistr.length(); i++) {\r
1324                 c.limit(i);\r
1325                 result = m_encoder.encode(c, bytes, false);\r
1326                 if(result.isOverflow()){\r
1327                     errln("FromChars single threw an overflow exception");\r
1328                 }\r
1329                 if(result.isError()){\r
1330                     errln("FromChars single threw an error: "+ result.toString());\r
1331                 }\r
1332             }\r
1333             if (expectedByteStr.length != bytes.limit()) {\r
1334                 errln("FromChars single len does not match");\r
1335                 rc = 3;\r
1336             }\r
1337 \r
1338             bytes.position(0);\r
1339             if (!bytes.equals(expected)) {\r
1340                 errln("FromChars single does not match");\r
1341                 printbytes(bytes);\r
1342                 printbytes(expected);\r
1343                 rc = 4;\r
1344             }\r
1345 \r
1346         } catch (Exception e) {\r
1347             errln("FromChars - exception in single");\r
1348             //e.printStackTrace(System.err);\r
1349             rc = 6;\r
1350         }\r
1351 \r
1352         /* Convert one char at a time to unicode */\r
1353         try {\r
1354             ByteBuffer bytes = ByteBuffer.allocate(expectedByteStr.length);\r
1355             m_encoder.reset();\r
1356             char[] temp = unistr.toCharArray();\r
1357             CoderResult result=null;\r
1358             for (int i = 0; i <= temp.length; i++) {\r
1359                 uniVal.limit(i);\r
1360                 result = m_encoder.encode(uniVal, bytes, false);\r
1361                 if(result.isOverflow()){\r
1362                     errln("FromChars simple threw an overflow exception");\r
1363                 }\r
1364                 if(result.isError()){\r
1365                     errln("FromChars simple threw an error: "+ result.toString());\r
1366                 }\r
1367             }\r
1368             if (bytes.limit() != expectedByteStr.length) {\r
1369                 errln("FromChars Simple len does not match");\r
1370                 rc = 7;\r
1371             }\r
1372             if (!bytes.equals(byes)) {\r
1373                 errln("FromChars Simple does not match");\r
1374                 printbytes(bytes);\r
1375                 printbytes(byes);\r
1376                 rc = 8;\r
1377             }\r
1378         } catch (Exception e) {\r
1379             errln("FromChars - exception in single buffer");\r
1380             //e.printStackTrace(System.err);\r
1381             rc = 9;\r
1382         }\r
1383         if (rc != 0) {\r
1384             errln("Test Simple FromChars " + m_encoding + " --FAILED");\r
1385         }\r
1386     }\r
1387 \r
1388     void printchars(CharBuffer buf) {\r
1389         int i;\r
1390         char[] chars = new char[buf.limit()];\r
1391         //save the current position\r
1392         int pos = buf.position();\r
1393         buf.position(0);\r
1394         buf.get(chars);\r
1395         //reset to old position\r
1396         buf.position(pos);\r
1397         for (i = 0; i < chars.length; i++) {\r
1398             err(hex(chars[i]) + " ");\r
1399         }\r
1400         errln("");\r
1401     }\r
1402     void printchars(String str) {\r
1403         char[] chars = str.toCharArray();\r
1404         for (int i = 0; i < chars.length; i++) {\r
1405             err(hex(chars[i]) + " ");\r
1406         }\r
1407         errln("");\r
1408     }\r
1409     void printbytes(ByteBuffer buf) {\r
1410         int i;\r
1411         byte[] bytes = new byte[buf.limit()];\r
1412         //save the current position\r
1413         int pos = buf.position();\r
1414         buf.position(0);\r
1415         buf.get(bytes);\r
1416         //reset to old position\r
1417         buf.position(pos);\r
1418         for (i = 0; i < bytes.length; i++) {\r
1419             System.out.print(hex(bytes[i]) + " ");\r
1420         }\r
1421         errln("");\r
1422     }\r
1423 \r
1424     public boolean equals(CoderResult a, CoderResult b) {\r
1425         return (a.isUnderflow() && b.isUnderflow())\r
1426                 || (a.isOverflow() && b.isOverflow())\r
1427                 || (a.isMalformed() && b.isMalformed() && a.length() == b.length())\r
1428                 || (a.isUnmappable() && b.isUnmappable() && a.length() == b.length());\r
1429     }\r
1430     public boolean equals(CharBuffer buf, String str) {\r
1431         return equals(buf, str.toCharArray());\r
1432     }\r
1433     public boolean equals(CharBuffer buf, CharBuffer str) {\r
1434         if (buf.limit() != str.limit())\r
1435             return false;\r
1436         int limit = buf.limit();\r
1437         for (int i = 0; i < limit; i++)\r
1438             if (buf.get(i) != str.get(i))\r
1439                 return false;\r
1440         return true;\r
1441     }\r
1442     public boolean equals(CharBuffer buf, CharBuffer str, int limit) {\r
1443         if (limit > buf.limit() || limit > str.limit())\r
1444             return false;\r
1445         for (int i = 0; i < limit; i++)\r
1446             if (buf.get(i) != str.get(i))\r
1447                 return false;\r
1448         return true;\r
1449     }\r
1450     public boolean equals(CharBuffer buf, char[] compareTo) {\r
1451         char[] chars = new char[buf.limit()];\r
1452         //save the current position\r
1453         int pos = buf.position();\r
1454         buf.position(0);\r
1455         buf.get(chars);\r
1456         //reset to old position\r
1457         buf.position(pos);\r
1458         return equals(chars, compareTo);\r
1459     }\r
1460 \r
1461     public boolean equals(char[] chars, char[] compareTo) {\r
1462         if (chars.length != compareTo.length) {\r
1463             errln(\r
1464                 "Length does not match chars: "\r
1465                     + chars.length\r
1466                     + " compareTo: "\r
1467                     + compareTo.length);\r
1468             return false;\r
1469         } else {\r
1470             boolean result = true;\r
1471             for (int i = 0; i < chars.length; i++) {\r
1472                 if (chars[i] != compareTo[i]) {    \r
1473                     logln(\r
1474                         "Got: "\r
1475                             + hex(chars[i])\r
1476                             + " Expected: "\r
1477                             + hex(compareTo[i])\r
1478                             + " At: "\r
1479                             + i);\r
1480                     result = false;\r
1481                 }\r
1482             }\r
1483             return result;\r
1484         }\r
1485     }\r
1486 \r
1487     public boolean equals(ByteBuffer buf, byte[] compareTo) {\r
1488         byte[] chars = new byte[buf.limit()];\r
1489         //save the current position\r
1490         int pos = buf.position();\r
1491         buf.position(0);\r
1492         buf.get(chars);\r
1493         //reset to old position\r
1494         buf.position(pos);\r
1495         return equals(chars, compareTo);\r
1496     }\r
1497     public boolean equals(ByteBuffer buf, ByteBuffer compareTo) {\r
1498         if (buf.limit() != compareTo.limit())\r
1499             return false;\r
1500         int limit = buf.limit();\r
1501         for (int i = 0; i < limit; i++)\r
1502             if (buf.get(i) != compareTo.get(i))\r
1503                 return false;\r
1504         return true;\r
1505     }\r
1506     public boolean equals(ByteBuffer buf, ByteBuffer compareTo, int limit) {\r
1507         if (limit > buf.limit() || limit > compareTo.limit())\r
1508             return false;\r
1509         for (int i = 0; i < limit; i++)\r
1510             if (buf.get(i) != compareTo.get(i))\r
1511                 return false;\r
1512         return true;\r
1513     }\r
1514     public boolean equals(byte[] chars, byte[] compareTo) {\r
1515         if (false/*chars.length != compareTo.length*/) {\r
1516             errln(\r
1517                 "Length does not match chars: "\r
1518                     + chars.length\r
1519                     + " compareTo: "\r
1520                     + compareTo.length);\r
1521             return false;\r
1522         } else {\r
1523             boolean result = true;\r
1524             for (int i = 0; i < chars.length; i++) {\r
1525                 if (chars[i] != compareTo[i]) {\r
1526                     logln(\r
1527                         "Got: "\r
1528                             + hex(chars[i])\r
1529                             + " Expected: "\r
1530                             + hex(compareTo[i])\r
1531                             + " At: "\r
1532                             + i);\r
1533                     result = false;\r
1534                 }\r
1535             }\r
1536             return result;\r
1537         }\r
1538     }\r
1539 \r
1540 //  TODO\r
1541   /*\r
1542     public void TestCallback(String encoding) throws Exception {\r
1543         \r
1544         byte[] gbSource =\r
1545             {\r
1546                 (byte) 0x81,\r
1547                 (byte) 0x36,\r
1548                 (byte) 0xDE,\r
1549                 (byte) 0x36,\r
1550                 (byte) 0x81,\r
1551                 (byte) 0x36,\r
1552                 (byte) 0xDE,\r
1553                 (byte) 0x37,\r
1554                 (byte) 0x81,\r
1555                 (byte) 0x36,\r
1556                 (byte) 0xDE,\r
1557                 (byte) 0x38,\r
1558                 (byte) 0xe3,\r
1559                 (byte) 0x32,\r
1560                 (byte) 0x9a,\r
1561                 (byte) 0x36 };\r
1562 \r
1563         char[] subChars = { 'P', 'I' };\r
1564 \r
1565         decoder.reset();\r
1566 \r
1567         decoder.replaceWith(new String(subChars));\r
1568         ByteBuffer mySource = ByteBuffer.wrap(gbSource);\r
1569         CharBuffer myTarget = CharBuffer.allocate(5);\r
1570 \r
1571         decoder.decode(mySource, myTarget, true);\r
1572         char[] expectedResult =\r
1573             { '\u22A6', '\u22A7', '\u22A8', '\u0050', '\u0049', };\r
1574 \r
1575         if (!equals(myTarget, new String(expectedResult))) {\r
1576             errln("Test callback GB18030 to Unicode : FAILED");\r
1577         }\r
1578         \r
1579     }\r
1580 */\r
1581     public void TestCanConvert(/*String encoding*/)throws Exception {\r
1582         char[] mySource = { \r
1583             '\ud800', '\udc00',/*surrogate pair */\r
1584             '\u22A6','\u22A7','\u22A8','\u22A9','\u22AA',\r
1585             '\u22AB','\u22AC','\u22AD','\u22AE','\u22AF',\r
1586             '\u22B0','\u22B1','\u22B2','\u22B3','\u22B4',\r
1587             '\ud800','\udc00',/*surrogate pair */\r
1588             '\u22B5','\u22B6','\u22B7','\u22B8','\u22B9',\r
1589             '\u22BA','\u22BB','\u22BC','\u22BD','\u22BE' \r
1590             };\r
1591         if(m_encoder==null){\r
1592             warnln("Could not load encoder.");\r
1593             return;\r
1594         }\r
1595         m_encoder.reset();\r
1596         if (!m_encoder.canEncode(new String(mySource))) {\r
1597             errln("Test canConvert() " + m_encoding + " failed. "+m_encoder);\r
1598         }\r
1599 \r
1600     }\r
1601     public void TestAvailableCharsets() {\r
1602         SortedMap map = Charset.availableCharsets();\r
1603         Set keySet = map.keySet();\r
1604         Iterator iter = keySet.iterator();\r
1605         while(iter.hasNext()){\r
1606             logln("Charset name: "+iter.next().toString());\r
1607         }\r
1608         Object[] charsets = CharsetProviderICU.getAvailableNames();\r
1609         int mapSize = map.size();\r
1610         if(mapSize < charsets.length){\r
1611             errln("Charset.availableCharsets() returned a number less than the number returned by icu. ICU: " + charsets.length\r
1612                     + " JDK: " + mapSize);\r
1613         }\r
1614         logln("Total Number of chasets = " + map.size());\r
1615     }\r
1616     /* ticket 5580 */\r
1617     public void TestJavaCanonicalNameOnAvailableCharsets() {\r
1618         CharsetProviderICU provider = new CharsetProviderICU();\r
1619         Iterator allCharsets = provider.charsets();\r
1620         String errorMessage = null;\r
1621         \r
1622         while (allCharsets.hasNext()) {\r
1623             Charset _chset = (Charset)allCharsets.next();\r
1624             Charset chset = Charset.forName(_chset.name());\r
1625             \r
1626             if (!chset.name().equals(_chset.name())) {\r
1627                 if (errorMessage == null) {\r
1628                     errorMessage = new String("Error: Charset.forName( " + _chset.name() + " ) returned " + chset + " instead of " + _chset);\r
1629                 } else {\r
1630                     errorMessage = errorMessage + "\nError: Charset.forName( " + _chset.name() + " ) returned " + chset + " instead of " + _chset;\r
1631                 }\r
1632             }\r
1633         }\r
1634         \r
1635         if (errorMessage != null) {\r
1636             errln(errorMessage);\r
1637         }\r
1638     }\r
1639     \r
1640     public void TestWindows936(){\r
1641         CharsetProviderICU icu = new CharsetProviderICU();\r
1642         Charset cs = icu.charsetForName("windows-936-2000");\r
1643         String canonicalName = cs.name();\r
1644         if(!canonicalName.equals("GBK")){\r
1645             errln("Did not get the expected canonical name. Got: "+canonicalName); //get the canonical name\r
1646         }\r
1647     }\r
1648     \r
1649     public void TestICUAvailableCharsets() {\r
1650         CharsetProviderICU icu = new CharsetProviderICU();\r
1651         Object[] charsets = CharsetProviderICU.getAvailableNames();\r
1652         for(int i=0;i<charsets.length;i++){\r
1653             Charset cs = icu.charsetForName((String)charsets[i]);\r
1654             try{\r
1655                 CharsetEncoder encoder = cs.newEncoder();\r
1656                 if(encoder!=null){\r
1657                     logln("Creation of encoder succeeded. "+cs.toString());\r
1658                 }\r
1659             }catch(Exception ex){\r
1660                 errln("Could not instantiate encoder for "+charsets[i]+". Error: "+ex.toString());\r
1661             }\r
1662             try{\r
1663                 CharsetDecoder decoder = cs.newDecoder();\r
1664                 if(decoder!=null){\r
1665                     logln("Creation of decoder succeeded. "+cs.toString());\r
1666                 }\r
1667             }catch(Exception ex){\r
1668                 errln("Could not instantiate decoder for "+charsets[i]+". Error: "+ex.toString());\r
1669             }\r
1670         }\r
1671     }\r
1672     /* jitterbug 4312 */\r
1673     public void TestUnsupportedCharset(){\r
1674         CharsetProvider icu = new CharsetProviderICU();\r
1675         Charset icuChar = icu.charsetForName("impossible");\r
1676         if(icuChar != null){\r
1677             errln("ICU does not conform to the spec");\r
1678         }\r
1679     }\r
1680 \r
1681 \r
1682     public void TestEncoderCreation(){\r
1683         try{\r
1684             Charset cs = Charset.forName("GB_2312-80");\r
1685             CharsetEncoder enc = cs.newEncoder();\r
1686             if(enc!=null && (enc instanceof CharsetEncoderICU) ){\r
1687                 logln("Successfully created the encoder: "+ enc);\r
1688             }else{\r
1689                 errln("Error creating charset encoder.");\r
1690             }\r
1691         }catch(Exception e){\r
1692             warnln("Error creating charset encoder."+ e.toString());\r
1693            // e.printStackTrace();\r
1694         }\r
1695         try{\r
1696             Charset cs = Charset.forName("x-ibm-971_P100-1995");\r
1697             CharsetEncoder enc = cs.newEncoder();\r
1698             if(enc!=null && (enc instanceof CharsetEncoderICU) ){\r
1699                 logln("Successfully created the encoder: "+ enc);\r
1700             }else{\r
1701                 errln("Error creating charset encoder.");\r
1702             }\r
1703         }catch(Exception e){\r
1704             warnln("Error creating charset encoder."+ e.toString());\r
1705         }\r
1706     }\r
1707     public void TestSubBytes(){\r
1708         try{\r
1709             //create utf-8 decoder\r
1710             CharsetDecoder decoder = new CharsetProviderICU().charsetForName("utf-8").newDecoder();\r
1711     \r
1712             //create a valid byte array, which can be decoded to " buffer"\r
1713             byte[] unibytes = new byte[] { 0x0020, 0x0062, 0x0075, 0x0066, 0x0066, 0x0065, 0x0072 };\r
1714     \r
1715             ByteBuffer buffer = ByteBuffer.allocate(20);\r
1716     \r
1717             //add a evil byte to make the byte buffer be malformed input\r
1718             buffer.put((byte)0xd8);\r
1719     \r
1720             //put the valid byte array\r
1721             buffer.put(unibytes);\r
1722     \r
1723             //reset postion\r
1724             buffer.flip();  \r
1725             \r
1726             decoder.onMalformedInput(CodingErrorAction.REPLACE);\r
1727             CharBuffer out = decoder.decode(buffer);\r
1728             String expected = "\ufffd buffer";\r
1729             if(!expected.equals(new String(out.array()))){\r
1730                 errln("Did not get the expected result for substitution chars. Got: "+\r
1731                        new String(out.array()) + "("+ hex(out.array())+")");\r
1732             }\r
1733             logln("Output: "+  new String(out.array()) + "("+ hex(out.array())+")");\r
1734         }catch (CharacterCodingException ex){\r
1735             errln("Unexpected exception: "+ex.toString());\r
1736         }\r
1737     }\r
1738     /*\r
1739     public void TestImplFlushFailure(){\r
1740    \r
1741        try{\r
1742            CharBuffer in = CharBuffer.wrap("\u3005\u3006\u3007\u30FC\u2015\u2010\uFF0F");\r
1743            CharsetEncoder encoder = new CharsetProviderICU().charsetForName("iso-2022-jp").newEncoder();\r
1744            ByteBuffer out = ByteBuffer.allocate(30);\r
1745            encoder.encode(in, out, true);\r
1746            encoder.flush(out);\r
1747            if(out.position()!= 20){\r
1748                errln("Did not get the expected position from flush");\r
1749            }\r
1750            \r
1751        }catch (Exception ex){\r
1752            errln("Could not create encoder for  iso-2022-jp exception: "+ex.toString());\r
1753        } \r
1754     }\r
1755    */\r
1756     public void TestISO88591() {\r
1757        \r
1758         Charset cs = new CharsetProviderICU().charsetForName("iso-8859-1");\r
1759         if(cs!=null){\r
1760             CharsetEncoder encoder = cs.newEncoder();\r
1761             if(encoder!=null){\r
1762                 encoder.canEncode("\uc2a3");\r
1763             }else{\r
1764                 errln("Could not create encoder for iso-8859-1");\r
1765             }\r
1766         }else{\r
1767             errln("Could not create Charset for iso-8859-1");\r
1768         }\r
1769         \r
1770     }\r
1771     public void TestUTF8Encode() {\r
1772         CharsetEncoder encoderICU = new CharsetProviderICU().charsetForName("utf-8").newEncoder();\r
1773         ByteBuffer out = ByteBuffer.allocate(30);\r
1774         CoderResult result = encoderICU.encode(CharBuffer.wrap("\ud800"), out, true);\r
1775         \r
1776         if (result.isMalformed()) {\r
1777             logln("\\ud800 is malformed for ICU4JNI utf-8 encoder");\r
1778         } else if (result.isUnderflow()) {\r
1779             errln("\\ud800 is OK for ICU4JNI utf-8 encoder");\r
1780         }\r
1781 \r
1782         CharsetEncoder encoderJDK = Charset.forName("utf-8").newEncoder();\r
1783         result = encoderJDK.encode(CharBuffer.wrap("\ud800"), ByteBuffer\r
1784                 .allocate(10), true);\r
1785         if (result.isUnderflow()) {\r
1786             errln("\\ud800 is OK for JDK utf-8 encoder");\r
1787         } else if (result.isMalformed()) {\r
1788             logln("\\ud800 is malformed for JDK utf-8 encoder");\r
1789         }\r
1790     }\r
1791 \r
1792 /*    private void printCB(CharBuffer buf){\r
1793         buf.rewind();\r
1794         while(buf.hasRemaining()){\r
1795             System.out.println(hex(buf.get()));\r
1796         }\r
1797         buf.rewind();\r
1798     }\r
1799 */\r
1800     public void TestUTF8() throws CharacterCodingException{\r
1801            try{\r
1802                CharsetEncoder encoderICU = new CharsetProviderICU().charsetForName("utf-8").newEncoder();\r
1803                encoderICU.encode(CharBuffer.wrap("\ud800"));\r
1804                errln("\\ud800 is OK for ICU4JNI utf-8 encoder");\r
1805            }catch (Exception e) {\r
1806                logln("\\ud800 is malformed for JDK utf-8 encoder");\r
1807               //e.printStackTrace();\r
1808            }\r
1809            \r
1810            CharsetEncoder encoderJDK = Charset.forName("utf-8").newEncoder();\r
1811            try {\r
1812                encoderJDK.encode(CharBuffer.wrap("\ud800"));\r
1813                errln("\\ud800 is OK for JDK utf-8 encoder");\r
1814            } catch (Exception e) {\r
1815                logln("\\ud800 is malformed for JDK utf-8 encoder");\r
1816                //e.printStackTrace();\r
1817            }         \r
1818     }\r
1819     \r
1820     public void TestUTF16Bom(){\r
1821 \r
1822         Charset cs = (new CharsetProviderICU()).charsetForName("UTF-16");\r
1823         char[] in = new char[] { 0x1122, 0x2211, 0x3344, 0x4433,\r
1824                                 0x5566, 0x6655, 0x7788, 0x8877, 0x9900 };\r
1825         CharBuffer inBuf = CharBuffer.allocate(in.length);\r
1826         inBuf.put(in);\r
1827         CharsetEncoder encoder = cs.newEncoder();\r
1828         ByteBuffer outBuf = ByteBuffer.allocate(in.length*2+2);\r
1829         inBuf.rewind();\r
1830         encoder.encode(inBuf, outBuf, true);\r
1831         outBuf.rewind();\r
1832         if(outBuf.get(0)!= (byte)0xFE && outBuf.get(1)!= (byte)0xFF){\r
1833             errln("The UTF16 encoder did not appended bom. Length returned: " + outBuf.remaining());\r
1834         }\r
1835         while(outBuf.hasRemaining()){\r
1836             logln("0x"+hex(outBuf.get()));\r
1837         }\r
1838         CharsetDecoder decoder = cs.newDecoder();\r
1839         outBuf.rewind();\r
1840         CharBuffer rt = CharBuffer.allocate(in.length);\r
1841         CoderResult cr = decoder.decode(outBuf, rt, true);\r
1842         if(cr.isError()){\r
1843             errln("Decoding with BOM failed. Error: "+ cr.toString());\r
1844         }\r
1845         equals(rt, in);\r
1846         {\r
1847             rt.clear();\r
1848             outBuf.rewind();\r
1849             Charset utf16 = Charset.forName("UTF-16");\r
1850             CharsetDecoder dc = utf16.newDecoder();\r
1851             cr = dc.decode(outBuf, rt, true);\r
1852             equals(rt, in);\r
1853         }\r
1854     }\r
1855      \r
1856     private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target,\r
1857             boolean throwException, boolean flush) throws BufferOverflowException, Exception {\r
1858         smBufDecode(decoder, encoding, source, target, throwException, flush, true);\r
1859     }\r
1860 \r
1861     private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target,\r
1862             boolean throwException, boolean flush, boolean backedByArray) throws BufferOverflowException, Exception {\r
1863         smBufDecode(decoder, encoding, source, target, throwException, flush, backedByArray, -1);\r
1864     }\r
1865 \r
1866     private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target,\r
1867             boolean throwException, boolean flush, boolean backedByArray, int targetLimit)\r
1868             throws BufferOverflowException, Exception {\r
1869         ByteBuffer mySource;\r
1870         CharBuffer myTarget;\r
1871         if (backedByArray) {\r
1872             mySource = ByteBuffer.allocate(source.capacity());\r
1873             myTarget = CharBuffer.allocate(target.capacity());\r
1874         } else {\r
1875             // this does not guarantee by any means that mySource and myTarget\r
1876             // are not backed by arrays\r
1877             mySource = ByteBuffer.allocateDirect(source.capacity());\r
1878             myTarget = ByteBuffer.allocateDirect(target.capacity() * 2).asCharBuffer();\r
1879         }\r
1880         mySource.position(source.position());\r
1881         for (int i = source.position(); i < source.limit(); i++)\r
1882             mySource.put(i, source.get(i));\r
1883 \r
1884         {\r
1885             decoder.reset();\r
1886             myTarget.limit(target.limit());\r
1887             mySource.limit(source.limit());\r
1888             mySource.position(source.position());\r
1889             CoderResult result = CoderResult.UNDERFLOW;\r
1890             result = decoder.decode(mySource, myTarget, true);\r
1891             if (flush) {\r
1892                 result = decoder.flush(myTarget);\r
1893             }\r
1894             if (result.isError()) {\r
1895                 if (throwException) {\r
1896                     throw new Exception();\r
1897                 }\r
1898                 errln("Test complete buffers while decoding failed. " + result.toString());\r
1899                 return;\r
1900             }\r
1901             if (result.isOverflow()) {\r
1902                 if (throwException) {\r
1903                     throw new BufferOverflowException();\r
1904                 }\r
1905                 errln("Test complete buffers while decoding threw overflow exception");\r
1906                 return;\r
1907             }\r
1908             myTarget.limit(myTarget.position());\r
1909             myTarget.position(0);\r
1910             target.position(0);\r
1911             if (result.isUnderflow() && !equals(myTarget, target, targetLimit)) {\r
1912                 errln(" Test complete buffers while decoding  " + encoding + " TO Unicode--failed");\r
1913             }\r
1914         }\r
1915         if (isQuick()) {\r
1916             return;\r
1917         }\r
1918         {\r
1919             decoder.reset();\r
1920             myTarget.limit(target.position());\r
1921             mySource.limit(source.position());\r
1922             mySource.position(source.position());\r
1923             myTarget.clear();\r
1924             myTarget.position(0);\r
1925 \r
1926             int inputLen = mySource.remaining();\r
1927 \r
1928             CoderResult result = CoderResult.UNDERFLOW;\r
1929             for (int i = 1; i <= inputLen; i++) {\r
1930                 mySource.limit(i);\r
1931                 if (i == inputLen) {\r
1932                     result = decoder.decode(mySource, myTarget, true);\r
1933                 } else {\r
1934                     result = decoder.decode(mySource, myTarget, false);\r
1935                 }\r
1936                 if (result.isError()) {\r
1937                     errln("Test small input buffers while decoding failed. " + result.toString());\r
1938                     break;\r
1939                 }\r
1940                 if (result.isOverflow()) {\r
1941                     if (throwException) {\r
1942                         throw new BufferOverflowException();\r
1943                     }\r
1944                     errln("Test small input buffers while decoding threw overflow exception");\r
1945                     break;\r
1946                 }\r
1947 \r
1948             }\r
1949             if (result.isUnderflow() && !equals(myTarget, target, targetLimit)) {\r
1950                 errln("Test small input buffers while decoding " + encoding + " TO Unicode--failed");\r
1951             }\r
1952         }\r
1953         {\r
1954             decoder.reset();\r
1955             myTarget.limit(0);\r
1956             mySource.limit(0);\r
1957             mySource.position(source.position());\r
1958             myTarget.clear();\r
1959             while (true) {\r
1960                 CoderResult result = decoder.decode(mySource, myTarget, false);\r
1961                 if (result.isUnderflow()) {\r
1962                     if (mySource.limit() < source.limit())\r
1963                         mySource.limit(mySource.limit() + 1);\r
1964                 } else if (result.isOverflow()) {\r
1965                     if (myTarget.limit() < target.limit())\r
1966                         myTarget.limit(myTarget.limit() + 1);\r
1967                     else\r
1968                         break;\r
1969                 } else /*if (result.isError())*/ {\r
1970                     errln("Test small output buffers while decoding " + result.toString());\r
1971                 }\r
1972                 if (mySource.position() == mySource.limit()) {\r
1973                     result = decoder.decode(mySource, myTarget, true);\r
1974                     if (result.isError()) {\r
1975                         errln("Test small output buffers while decoding " + result.toString());\r
1976                     }\r
1977                     result = decoder.flush(myTarget);\r
1978                     if (result.isError()) {\r
1979                         errln("Test small output buffers while decoding " + result.toString());\r
1980                     }\r
1981                     break;\r
1982                 }\r
1983             }\r
1984 \r
1985             if (!equals(myTarget, target, targetLimit)) {\r
1986                 errln("Test small output buffers " + encoding + " TO Unicode failed");\r
1987             }\r
1988         }\r
1989     }\r
1990 \r
1991     private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target,\r
1992             boolean throwException, boolean flush) throws Exception, BufferOverflowException {\r
1993         smBufEncode(encoder, encoding, source, target, throwException, flush, true);\r
1994     }\r
1995 \r
1996     private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target,\r
1997             boolean throwException, boolean flush, boolean backedByArray) throws Exception, BufferOverflowException {\r
1998         smBufEncode(encoder, encoding, source, target, throwException, flush, true, -1);\r
1999     }\r
2000 \r
2001     private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target,\r
2002             boolean throwException, boolean flush, boolean backedByArray, int targetLimit) throws Exception,\r
2003             BufferOverflowException {\r
2004         logln("Running smBufEncode for " + encoding + " with class " + encoder);\r
2005 \r
2006         CharBuffer mySource;\r
2007         ByteBuffer myTarget;\r
2008         if (backedByArray) {\r
2009             mySource = CharBuffer.allocate(source.capacity());\r
2010             myTarget = ByteBuffer.allocate(target.capacity());\r
2011         } else {\r
2012             mySource = ByteBuffer.allocateDirect(source.capacity() * 2).asCharBuffer();\r
2013             myTarget = ByteBuffer.allocateDirect(target.capacity());\r
2014         }\r
2015         mySource.position(source.position());\r
2016         for (int i = source.position(); i < source.limit(); i++)\r
2017             mySource.put(i, source.get(i));\r
2018 \r
2019         myTarget.clear();\r
2020         {\r
2021             logln("Running tests on small input buffers for " + encoding);\r
2022             encoder.reset();\r
2023             myTarget.limit(target.limit());\r
2024             mySource.limit(source.limit());\r
2025             mySource.position(source.position());\r
2026             CoderResult result = null;\r
2027 \r
2028             result = encoder.encode(mySource, myTarget, true);\r
2029             if (flush) {\r
2030                 result = encoder.flush(myTarget);\r
2031             }\r
2032 \r
2033             if (result.isError()) {\r
2034                 if (throwException) {\r
2035                     throw new Exception();\r
2036                 }\r
2037                 errln("Test complete while encoding failed. " + result.toString());\r
2038             }\r
2039             if (result.isOverflow()) {\r
2040                 if (throwException) {\r
2041                     throw new BufferOverflowException();\r
2042                 }\r
2043                 errln("Test complete while encoding threw overflow exception");\r
2044             }\r
2045             if (!equals(myTarget, target, targetLimit)) {\r
2046                 errln("Test complete buffers while encoding for " + encoding + " failed");\r
2047 \r
2048             } else {\r
2049                 logln("Tests complete buffers for " + encoding + " passed");\r
2050             }\r
2051         }\r
2052         if (isQuick()) {\r
2053             return;\r
2054         }\r
2055         {\r
2056             logln("Running tests on small input buffers for " + encoding);\r
2057             encoder.reset();\r
2058             myTarget.clear();\r
2059             myTarget.limit(target.limit());\r
2060             mySource.limit(source.limit());\r
2061             mySource.position(source.position());\r
2062             int inputLen = mySource.limit();\r
2063             CoderResult result = null;\r
2064             for (int i = 1; i <= inputLen; i++) {\r
2065                 mySource.limit(i);\r
2066                 result = encoder.encode(mySource, myTarget, false);\r
2067                 if (result.isError()) {\r
2068                     errln("Test small input buffers while encoding failed. " + result.toString());\r
2069                 }\r
2070                 if (result.isOverflow()) {\r
2071                     if (throwException) {\r
2072                         throw new BufferOverflowException();\r
2073                     }\r
2074                     errln("Test small input buffers while encoding threw overflow exception");\r
2075                 }\r
2076             }\r
2077             if (!equals(myTarget, target, targetLimit)) {\r
2078                 errln("Test small input buffers " + encoding + " From Unicode failed");\r
2079             } else {\r
2080                 logln("Tests on small input buffers for " + encoding + " passed");\r
2081             }\r
2082         }\r
2083         {\r
2084             logln("Running tests on small output buffers for " + encoding);\r
2085             encoder.reset();\r
2086             myTarget.clear();\r
2087             myTarget.limit(target.limit());\r
2088             mySource.limit(source.limit());\r
2089             mySource.position(source.position());\r
2090             mySource.position(0);\r
2091             myTarget.position(0);\r
2092 \r
2093             logln("myTarget.limit: " + myTarget.limit() + " myTarget.capcity: " + myTarget.capacity());\r
2094 \r
2095             while (true) {\r
2096                 int pos = myTarget.position();\r
2097 \r
2098                 CoderResult result = encoder.encode(mySource, myTarget, false);\r
2099                 logln("myTarget.Position: " + pos + " myTarget.limit: " + myTarget.limit());\r
2100                 logln("mySource.position: " + mySource.position() + " mySource.limit: " + mySource.limit());\r
2101 \r
2102                 if (result.isError()) {\r
2103                     errln("Test small output buffers while encoding " + result.toString());\r
2104                 }\r
2105                 if (mySource.position() == mySource.limit()) {\r
2106                     result = encoder.encode(mySource, myTarget, true);\r
2107                     if (result.isError()) {\r
2108                         errln("Test small output buffers while encoding " + result.toString());\r
2109                     }\r
2110 \r
2111                     myTarget.limit(myTarget.capacity());\r
2112                     result = encoder.flush(myTarget);\r
2113                     if (result.isError()) {\r
2114                         errln("Test small output buffers while encoding " + result.toString());\r
2115                     }\r
2116                     break;\r
2117                 }\r
2118             }\r
2119             if (!equals(myTarget, target, targetLimit)) {\r
2120                 errln("Test small output buffers " + encoding + " From Unicode failed.");\r
2121             }\r
2122             logln("Tests on small output buffers for " + encoding + " passed");\r
2123         }\r
2124     }\r
2125 \r
2126     public void convertAllTest(ByteBuffer bSource, CharBuffer uSource) throws Exception {\r
2127         {\r
2128             try {\r
2129                 m_decoder.reset();\r
2130                 ByteBuffer mySource = bSource.duplicate();\r
2131                 CharBuffer myTarget = m_decoder.decode(mySource);\r
2132                 if (!equals(myTarget, uSource)) {\r
2133                     errln(\r
2134                         "--Test convertAll() "\r
2135                             + m_encoding\r
2136                             + " to Unicode  --FAILED");\r
2137                 }\r
2138             } catch (Exception e) {\r
2139                 //e.printStackTrace();\r
2140                 errln(e.getMessage());\r
2141             }\r
2142         }\r
2143         {\r
2144             try {\r
2145                 m_encoder.reset();\r
2146                 CharBuffer mySource = CharBuffer.wrap(uSource);\r
2147                 ByteBuffer myTarget = m_encoder.encode(mySource);\r
2148                 if (!equals(myTarget, bSource)) {\r
2149                     errln(\r
2150                         "--Test convertAll() "\r
2151                             + m_encoding\r
2152                             + " to Unicode  --FAILED");\r
2153                 }\r
2154             } catch (Exception e) {\r
2155                 //e.printStackTrace();\r
2156                 errln("encoder.encode() failed "+ e.getMessage()+" "+e.toString());\r
2157             }\r
2158         }\r
2159 \r
2160     }\r
2161     //TODO\r
2162     /*\r
2163     public void TestString(ByteBuffer bSource, CharBuffer uSource) throws Exception {\r
2164         try {\r
2165             {\r
2166                 String source = uSource.toString();\r
2167                 byte[] target = source.getBytes(m_encoding);\r
2168                 if (!equals(target, bSource.array())) {\r
2169                     errln("encode using string API failed");\r
2170                 }\r
2171             }\r
2172             {\r
2173 \r
2174                 String target = new String(bSource.array(), m_encoding);\r
2175                 if (!equals(uSource, target.toCharArray())) {\r
2176                     errln("decode using string API failed");\r
2177                 }\r
2178             }\r
2179         } catch (Exception e) {\r
2180             //e.printStackTrace();\r
2181             errln(e.getMessage());\r
2182         }\r
2183     }\r
2184 \r
2185     /*private void fromUnicodeTest() throws Exception {\r
2186         \r
2187         logln("Loaded Charset: " + charset.getClass().toString());\r
2188         logln("Loaded CharsetEncoder: " + encoder.getClass().toString());\r
2189         logln("Loaded CharsetDecoder: " + decoder.getClass().toString());\r
2190         \r
2191         ByteBuffer myTarget = ByteBuffer.allocate(gbSource.length);\r
2192         logln("Created ByteBuffer of length: " + uSource.length);\r
2193         CharBuffer mySource = CharBuffer.wrap(uSource);\r
2194         logln("Wrapped ByteBuffer with CharBuffer  ");\r
2195         encoder.reset();\r
2196         logln("Test Unicode to " + encoding );\r
2197         encoder.encode(mySource, myTarget, true);\r
2198         if (!equals(myTarget, gbSource)) {\r
2199             errln("--Test Unicode to " + encoding + ": FAILED");\r
2200         } \r
2201         logln("Test Unicode to " + encoding +" passed");\r
2202     }\r
2203 \r
2204     public void TestToUnicode( ) throws Exception {\r
2205         \r
2206         logln("Loaded Charset: " + charset.getClass().toString());\r
2207         logln("Loaded CharsetEncoder: " + encoder.getClass().toString());\r
2208         logln("Loaded CharsetDecoder: " + decoder.getClass().toString());\r
2209         \r
2210         CharBuffer myTarget = CharBuffer.allocate(uSource.length);\r
2211         ByteBuffer mySource = ByteBuffer.wrap(getByteArray(gbSource));\r
2212         decoder.reset();\r
2213         CoderResult result = decoder.decode(mySource, myTarget, true);\r
2214         if (result.isError()) {\r
2215             errln("Test ToUnicode -- FAILED");\r
2216         }\r
2217         if (!equals(myTarget, uSource)) {\r
2218             errln("--Test " + encoding + " to Unicode :FAILED");\r
2219         }\r
2220     }\r
2221 \r
2222     public static byte[] getByteArray(char[] source) {\r
2223         byte[] target = new byte[source.length];\r
2224         int i = source.length;\r
2225         for (; --i >= 0;) {\r
2226             target[i] = (byte) source[i];\r
2227         }\r
2228         return target;\r
2229     }\r
2230     /*\r
2231     private void smBufCharset(Charset charset) {\r
2232         try {\r
2233             ByteBuffer bTarget = charset.encode(CharBuffer.wrap(uSource));\r
2234             CharBuffer uTarget =\r
2235                 charset.decode(ByteBuffer.wrap(getByteArray(gbSource)));\r
2236 \r
2237             if (!equals(uTarget, uSource)) {\r
2238                 errln("Test " + charset.toString() + " to Unicode :FAILED");\r
2239             }\r
2240             if (!equals(bTarget, gbSource)) {\r
2241                 errln("Test " + charset.toString() + " from Unicode :FAILED");\r
2242             }\r
2243         } catch (Exception ex) {\r
2244             errln("Encountered exception in smBufCharset");\r
2245         }\r
2246     }\r
2247     \r
2248     public void TestMultithreaded() throws Exception {\r
2249         final Charset cs = Charset.forName(encoding);\r
2250         if (cs == charset) {\r
2251             errln("The objects are equal");\r
2252         }\r
2253         smBufCharset(cs);\r
2254         try {\r
2255             final Thread t1 = new Thread() {\r
2256                 public void run() {\r
2257                     // commented out since the mehtods on\r
2258                     // Charset API are supposed to be thread\r
2259                     // safe ... to test it we dont sync\r
2260             \r
2261                     // synchronized(charset){\r
2262                    while (!interrupted()) {\r
2263                         try {\r
2264                             smBufCharset(cs);\r
2265                         } catch (UnsupportedCharsetException ueEx) {\r
2266                             errln(ueEx.toString());\r
2267                         }\r
2268                     }\r
2269 \r
2270                     // }\r
2271                 }\r
2272             };\r
2273             final Thread t2 = new Thread() {\r
2274                 public void run() {\r
2275                         // synchronized(charset){\r
2276                     while (!interrupted()) {\r
2277                         try {\r
2278                             smBufCharset(cs);\r
2279                         } catch (UnsupportedCharsetException ueEx) {\r
2280                             errln(ueEx.toString());\r
2281                         }\r
2282                     }\r
2283 \r
2284                     //}\r
2285                 }\r
2286             };\r
2287             t1.start();\r
2288             t2.start();\r
2289             int i = 0;\r
2290             for (;;) {\r
2291                 if (i > 1000000000) {\r
2292                     try {\r
2293                         t1.interrupt();\r
2294                     } catch (Exception e) {\r
2295                     }\r
2296                     try {\r
2297                         t2.interrupt();\r
2298                     } catch (Exception e) {\r
2299                     }\r
2300                     break;\r
2301                 }\r
2302                 i++;\r
2303             }\r
2304         } catch (Exception e) {\r
2305             throw e;\r
2306         }\r
2307     }\r
2308 \r
2309     public void TestSynchronizedMultithreaded() throws Exception {\r
2310         // Methods on CharsetDecoder and CharsetEncoder classes\r
2311         // are inherently unsafe if accessed by multiple concurrent\r
2312         // thread so we synchronize them\r
2313         final Charset charset = Charset.forName(encoding);\r
2314         final CharsetDecoder decoder = charset.newDecoder();\r
2315         final CharsetEncoder encoder = charset.newEncoder();\r
2316         try {\r
2317             final Thread t1 = new Thread() {\r
2318                 public void run() {\r
2319                     while (!interrupted()) {\r
2320                         try {\r
2321                             synchronized (encoder) {\r
2322                                 smBufEncode(encoder, encoding);\r
2323                             }\r
2324                             synchronized (decoder) {\r
2325                                 smBufDecode(decoder, encoding);\r
2326                             }\r
2327                         } catch (UnsupportedCharsetException ueEx) {\r
2328                             errln(ueEx.toString());\r
2329                         }\r
2330                     }\r
2331 \r
2332                 }\r
2333             };\r
2334             final Thread t2 = new Thread() {\r
2335                 public void run() {\r
2336                     while (!interrupted()) {\r
2337                         try {\r
2338                             synchronized (encoder) {\r
2339                                 smBufEncode(encoder, encoding);\r
2340                             }\r
2341                             synchronized (decoder) {\r
2342                                 smBufDecode(decoder, encoding);\r
2343                             }\r
2344                         } catch (UnsupportedCharsetException ueEx) {\r
2345                             errln(ueEx.toString());\r
2346                         }\r
2347                     }\r
2348                 }\r
2349             };\r
2350             t1.start();\r
2351             t2.start();\r
2352             int i = 0;\r
2353             for (;;) {\r
2354                 if (i > 1000000000) {\r
2355                     try {\r
2356                         t1.interrupt();\r
2357                     } catch (Exception e) {\r
2358                     }\r
2359                     try {\r
2360                         t2.interrupt();\r
2361                     } catch (Exception e) {\r
2362                     }\r
2363                     break;\r
2364                 }\r
2365                 i++;\r
2366             }\r
2367         } catch (Exception e) {\r
2368             throw e;\r
2369         }\r
2370     }\r
2371     */\r
2372     \r
2373     public void TestMBCS(){      \r
2374         {\r
2375             // Encoder: from Unicode conversion\r
2376             CharsetEncoder encoderICU = new CharsetProviderICU().charsetForName("ibm-971").newEncoder();\r
2377             ByteBuffer out = ByteBuffer.allocate(6);\r
2378             encoderICU.onUnmappableCharacter(CodingErrorAction.REPLACE);\r
2379             CoderResult result = encoderICU.encode(CharBuffer.wrap("\u0131\u0061\u00a1"), out, true);\r
2380             if(!result.isError()){\r
2381                 byte[] expected = {(byte)0xA9, (byte)0xA5, (byte)0xAF, (byte)0xFE, (byte)0xA2, (byte)0xAE};\r
2382                 if(!equals(expected, out.array())){\r
2383                     errln("Did not get the expected result for substitution bytes. Got: "+\r
2384                            hex(out.array()));\r
2385                 }\r
2386                 logln("Output: "+  hex(out.array()));\r
2387             }else{\r
2388                 errln("Encode operation failed for encoder: "+encoderICU.toString());\r
2389             }\r
2390         }\r
2391         {\r
2392             // Decoder: to Unicode conversion\r
2393             CharsetDecoder decoderICU = new CharsetProviderICU().charsetForName("ibm-971").newDecoder();\r
2394             CharBuffer out = CharBuffer.allocate(3);\r
2395             decoderICU.onMalformedInput(CodingErrorAction.REPLACE);\r
2396             CoderResult result = decoderICU.decode(ByteBuffer.wrap(new byte[] { (byte)0xA2, (byte)0xAE, (byte)0x12, (byte)0x34, (byte)0xEF, (byte)0xDC }), out, true);\r
2397             if(!result.isError()){\r
2398                 char[] expected = {'\u00a1', '\ufffd', '\u6676'};\r
2399                 if(!equals(expected, out.array())){\r
2400                     errln("Did not get the expected result for substitution chars. Got: "+\r
2401                            hex(out.array()));\r
2402                 }\r
2403                 logln("Output: "+  hex(out.array()));\r
2404             }else{\r
2405                 errln("Decode operation failed for encoder: "+decoderICU.toString());\r
2406             }\r
2407         }\r
2408     }\r
2409     \r
2410     public void TestJB4897(){\r
2411         CharsetProviderICU provider = new CharsetProviderICU();\r
2412         Charset charset = provider.charsetForName("x-abracadabra");  \r
2413         if(charset!=null && charset.canEncode()== true){\r
2414             errln("provider.charsetForName() does not validate the charset names" );\r
2415         }\r
2416     }\r
2417 \r
2418     public void TestJB5027() {\r
2419         CharsetProviderICU provider= new CharsetProviderICU();\r
2420 \r
2421         Charset fake = provider.charsetForName("doesNotExist");\r
2422         if(fake != null){\r
2423             errln("\"doesNotExist\" returned " + fake);\r
2424         }\r
2425         Charset xfake = provider.charsetForName("x-doesNotExist");\r
2426         if(xfake!=null){\r
2427             errln("\"x-doesNotExist\" returned " + xfake);\r
2428         }\r
2429     }\r
2430     //test to make sure that number of aliases and canonical names are in the charsets that are in\r
2431     public void TestAllNames() {\r
2432         \r
2433         CharsetProviderICU provider= new CharsetProviderICU();\r
2434         Object[] available = CharsetProviderICU.getAvailableNames();\r
2435         for(int i=0; i<available.length;i++){\r
2436             try{\r
2437                 String canon  = CharsetProviderICU.getICUCanonicalName((String)available[i]);\r
2438 \r
2439                 // ',' is not allowed by Java's charset name checker\r
2440                 if(canon.indexOf(',')>=0){\r
2441                     continue;\r
2442                 }\r
2443                 Charset cs = provider.charsetForName((String)available[i]);\r
2444               \r
2445                 Object[] javaAliases =  cs.aliases().toArray();\r
2446                 //seach for ICU canonical name in javaAliases\r
2447                 boolean inAliasList = false;\r
2448                 for(int j=0; j<javaAliases.length; j++){\r
2449                     String java = (String) javaAliases[j];\r
2450                     if(java.equals(canon)){\r
2451                         logln("javaAlias: " + java + " canon: " + canon);\r
2452                         inAliasList = true;\r
2453                     }\r
2454                 }\r
2455                 if(inAliasList == false){\r
2456                     errln("Could not find ICU canonical name: "+canon+ " for java canonical name: "+ available[i]+ " "+ i);\r
2457                 }\r
2458             }catch(UnsupportedCharsetException ex){\r
2459                 errln("could no load charset "+ available[i]+" "+ex.getMessage());\r
2460                 continue;\r
2461             }\r
2462         }\r
2463     }\r
2464     public void TestDecoderImplFlush() {\r
2465         CharsetProviderICU provider = new CharsetProviderICU();\r
2466         Charset ics = provider.charsetForName("UTF-16");\r
2467         Charset jcs = Charset.forName("UTF-16"); // Java's UTF-16 charset\r
2468         execDecoder(jcs);\r
2469         execDecoder(ics);\r
2470     }\r
2471     public void TestEncoderImplFlush() {\r
2472         CharsetProviderICU provider = new CharsetProviderICU();\r
2473         Charset ics = provider.charsetForName("UTF-16");\r
2474         Charset jcs = Charset.forName("UTF-16"); // Java's UTF-16 charset\r
2475         execEncoder(jcs);\r
2476         execEncoder(ics);\r
2477     }\r
2478     private void execDecoder(Charset cs){\r
2479         CharsetDecoder decoder = cs.newDecoder();\r
2480         decoder.onMalformedInput(CodingErrorAction.REPORT);\r
2481         decoder.onUnmappableCharacter(CodingErrorAction.REPORT);\r
2482         CharBuffer out = CharBuffer.allocate(10);\r
2483         CoderResult result = decoder.decode(ByteBuffer.wrap(new byte[] { -1,\r
2484                 -2, 32, 0, 98 }), out, false);\r
2485         result = decoder.decode(ByteBuffer.wrap(new byte[] { 98 }), out, true);\r
2486 \r
2487         logln(cs.getClass().toString()+ ":" +result.toString());\r
2488         try {\r
2489             result = decoder.flush(out);\r
2490             logln(cs.getClass().toString()+ ":" +result.toString());\r
2491         } catch (Exception e) {\r
2492             errln(e.getMessage()+" "+cs.getClass().toString());\r
2493         }\r
2494     }\r
2495     private void execEncoder(Charset cs){\r
2496         CharsetEncoder encoder = cs.newEncoder();\r
2497         encoder.onMalformedInput(CodingErrorAction.REPORT);\r
2498         encoder.onUnmappableCharacter(CodingErrorAction.REPORT);\r
2499         ByteBuffer out = ByteBuffer.allocate(10);\r
2500         CoderResult result = encoder.encode(CharBuffer.wrap(new char[] { '\uFFFF',\r
2501                 '\u2345', 32, 98 }), out, false);\r
2502         logln(cs.getClass().toString()+ ":" +result.toString());\r
2503         result = encoder.encode(CharBuffer.wrap(new char[] { 98 }), out, true);\r
2504 \r
2505         logln(cs.getClass().toString()+ ":" +result.toString());\r
2506         try {\r
2507             result = encoder.flush(out);\r
2508             logln(cs.getClass().toString()+ ":" +result.toString());\r
2509         } catch (Exception e) {\r
2510             errln(e.getMessage()+" "+cs.getClass().toString());\r
2511         }\r
2512     }\r
2513     public void TestDecodeMalformed() {\r
2514         CharsetProviderICU provider = new CharsetProviderICU();\r
2515         Charset ics = provider.charsetForName("UTF-16BE");\r
2516         //Use SUN's charset\r
2517         Charset jcs = Charset.forName("UTF-16");\r
2518         CoderResult ir = execMalformed(ics);\r
2519         CoderResult jr = execMalformed(jcs);\r
2520         if(ir!=jr){\r
2521             errln("ICU's decoder did not return the same result as Sun. ICU: "+ir.toString()+" Sun: "+jr.toString());\r
2522         }\r
2523     }\r
2524     private CoderResult execMalformed(Charset cs){\r
2525         CharsetDecoder decoder = cs.newDecoder();\r
2526         decoder.onMalformedInput(CodingErrorAction.IGNORE);\r
2527         decoder.onUnmappableCharacter(CodingErrorAction.REPORT);\r
2528         ByteBuffer in = ByteBuffer.wrap(new byte[] { 0x00, 0x41, 0x00, 0x42, 0x01 });\r
2529         CharBuffer out = CharBuffer.allocate(3);\r
2530         return decoder.decode(in, out, true);\r
2531     }\r
2532     \r
2533     public void TestJavaUTF16Decoder(){\r
2534         CharsetProviderICU provider = new CharsetProviderICU();\r
2535         Charset ics = provider.charsetForName("UTF-16BE");\r
2536         //Use SUN's charset\r
2537         Charset jcs = Charset.forName("UTF-16");\r
2538         Exception ie = execConvertAll(ics);\r
2539         Exception je = execConvertAll(jcs);\r
2540         if(ie!=je){\r
2541             errln("ICU's decoder did not return the same result as Sun. ICU: "+ie.toString()+" Sun: "+je.toString());\r
2542         }\r
2543     }\r
2544     private Exception execConvertAll(Charset cs){\r
2545         ByteBuffer in = ByteBuffer.allocate(400);\r
2546         int i=0;\r
2547         while(in.position()!=in.capacity()){\r
2548             in.put((byte)0xD8);\r
2549             in.put((byte)i);\r
2550             in.put((byte)0xDC);\r
2551             in.put((byte)i);\r
2552             i++;\r
2553         }\r
2554         in.limit(in.position());\r
2555         in.position(0);\r
2556         CharsetDecoder decoder = cs.newDecoder();\r
2557         decoder.onMalformedInput(CodingErrorAction.IGNORE);\r
2558         decoder.onUnmappableCharacter(CodingErrorAction.REPORT);\r
2559         try{\r
2560             CharBuffer out = decoder.decode(in);\r
2561             if(out!=null){\r
2562                 logln(cs.toString()+" encoing succeeded as expected!");\r
2563             }\r
2564         }catch ( Exception ex){\r
2565             errln("Did not get expected exception for encoding: "+cs.toString());\r
2566             return ex;\r
2567         }\r
2568         return null;\r
2569     }\r
2570     public void TestUTF32BOM(){\r
2571 \r
2572         Charset cs = (new CharsetProviderICU()).charsetForName("UTF-32");\r
2573         char[] in = new char[] { 0xd800, 0xdc00, \r
2574                                  0xd801, 0xdc01,\r
2575                                  0xdbff, 0xdfff, \r
2576                                  0xd900, 0xdd00, \r
2577                                  0x0000, 0x0041,\r
2578                                  0x0000, 0x0042,\r
2579                                  0x0000, 0x0043};\r
2580         \r
2581         CharBuffer inBuf = CharBuffer.allocate(in.length);\r
2582         inBuf.put(in);\r
2583         CharsetEncoder encoder = cs.newEncoder();\r
2584         ByteBuffer outBuf = ByteBuffer.allocate(in.length*4+4);\r
2585         inBuf.rewind();\r
2586         encoder.encode(inBuf, outBuf, true);\r
2587         outBuf.rewind();\r
2588         if(outBuf.get(0)!= (byte)0x00 && outBuf.get(1)!= (byte)0x00 && \r
2589                 outBuf.get(2)!= (byte)0xFF && outBuf.get(3)!= (byte)0xFE){\r
2590             errln("The UTF32 encoder did not appended bom. Length returned: " + outBuf.remaining());\r
2591         }\r
2592         while(outBuf.hasRemaining()){\r
2593             logln("0x"+hex(outBuf.get()));\r
2594         }\r
2595         CharsetDecoder decoder = cs.newDecoder();\r
2596         outBuf.limit(outBuf.position());\r
2597         outBuf.rewind();\r
2598         CharBuffer rt = CharBuffer.allocate(in.length);\r
2599         CoderResult cr = decoder.decode(outBuf, rt, true);\r
2600         if(cr.isError()){\r
2601             errln("Decoding with BOM failed. Error: "+ cr.toString());\r
2602         }\r
2603         equals(rt, in);\r
2604         try{\r
2605             rt.clear();\r
2606             outBuf.rewind();\r
2607             Charset utf16 = Charset.forName("UTF-32");\r
2608             CharsetDecoder dc = utf16.newDecoder();\r
2609             cr = dc.decode(outBuf, rt, true);\r
2610             equals(rt, in);\r
2611         }catch(UnsupportedCharsetException ex){\r
2612             // swallow the expection.\r
2613         }\r
2614     }\r
2615     \r
2616     /*\r
2617      *  Michael Ow\r
2618      *  Modified 070424\r
2619      */\r
2620     /*The following two methods provides the option of exceptions when Decoding \r
2621      * and Encoding if needed for testing purposes.\r
2622      */\r
2623     private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target) {\r
2624         smBufDecode(decoder, encoding, source, target, true);\r
2625     }\r
2626     private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target, boolean backedByArray) {\r
2627         try {\r
2628             smBufDecode(decoder, encoding, source, target, false, false, backedByArray);\r
2629         }    \r
2630         catch (Exception ex) {           \r
2631             System.out.println("!exception!");\r
2632         }\r
2633     }\r
2634     private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target)  {\r
2635         smBufEncode(encoder, encoding, source, target, true);\r
2636     }\r
2637     private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target, boolean backedByArray)  {\r
2638         try {\r
2639             smBufEncode(encoder, encoding, source, target, false, false); \r
2640         }\r
2641         catch (Exception ex) {\r
2642             System.out.println("!exception!");\r
2643         }\r
2644     }\r
2645     //Test CharsetICUProvider\r
2646     public void TestNullCanonicalName() {\r
2647         String enc = null;\r
2648         String canonicalName = CharsetProviderICU.getICUCanonicalName(enc);\r
2649         \r
2650         if (canonicalName != null) {\r
2651             errln("getICUCanonicalName return a non-null string for given null string");\r
2652         }\r
2653     }\r
2654     public void TestGetAllNames() {\r
2655         String[] names = null;\r
2656         \r
2657         names = CharsetProviderICU.getAllNames();\r
2658         \r
2659         if (names == null) {\r
2660             errln("getAllNames returned a null string.");\r
2661         }\r
2662     }\r
2663     //Test CharsetICU\r
2664     public void TestCharsetContains() {\r
2665         boolean test;\r
2666         \r
2667         CharsetProvider provider = new CharsetProviderICU();     \r
2668         Charset cs1 = provider.charsetForName("UTF-32");\r
2669         Charset cs2 = null;\r
2670         \r
2671         test = cs1.contains(cs2);\r
2672         \r
2673         if (test != false) {\r
2674             errln("Charset.contains returned true for a null charset.");\r
2675         }\r
2676         \r
2677         cs2 = CharsetICU.forNameICU("UTF-32");\r
2678         \r
2679         test = cs1.contains(cs2);\r
2680         \r
2681         if (test != true) {\r
2682             errln("Charset.contains returned false for an identical charset.");\r
2683         }\r
2684         \r
2685         cs2 = provider.charsetForName("UTF-8");\r
2686         \r
2687         test = cs1.contains(cs2);\r
2688         \r
2689         if (test != false) {\r
2690             errln("Charset.contains returned true for a different charset.");\r
2691         }\r
2692     }\r
2693     public void TestCharsetICUNullCharsetName() {\r
2694         String charsetName = null;\r
2695         \r
2696         try {\r
2697             CharsetICU.forNameICU(charsetName);\r
2698             errln("CharsetICU.forName should have thown an exception after getting a null charsetName.");\r
2699         }\r
2700         catch(Exception ex) {          \r
2701         }\r
2702     }\r
2703     \r
2704     //Test CharsetASCII\r
2705     public void TestCharsetASCIIOverFlow() {\r
2706         int byteBufferLimit;\r
2707         int charBufferLimit;\r
2708         \r
2709         CharsetProvider provider = new CharsetProviderICU();\r
2710         Charset cs = provider.charsetForName("ASCII");        \r
2711         CharsetEncoder encoder = cs.newEncoder();\r
2712         CharsetDecoder decoder = cs.newDecoder();\r
2713         \r
2714         CharBuffer charBuffer = CharBuffer.allocate(0x90);\r
2715         ByteBuffer byteBuffer = ByteBuffer.allocate(0x90);\r
2716         \r
2717         CharBuffer charBufferTest = CharBuffer.allocate(0xb0);\r
2718         ByteBuffer byteBufferTest = ByteBuffer.allocate(0xb0);\r
2719         \r
2720         for(int j=0;j<=0x7f; j++){\r
2721            charBuffer.put((char)j);\r
2722            byteBuffer.put((byte)j);\r
2723         }\r
2724         \r
2725         byteBuffer.limit(byteBufferLimit = byteBuffer.position());\r
2726         byteBuffer.position(0);\r
2727         charBuffer.limit(charBufferLimit = charBuffer.position());\r
2728         charBuffer.position(0);\r
2729         \r
2730         //test for overflow\r
2731         byteBufferTest.limit(byteBufferLimit - 5);\r
2732         byteBufferTest.position(0);\r
2733         charBufferTest.limit(charBufferLimit - 5);\r
2734         charBufferTest.position(0);\r
2735         try {\r
2736             smBufDecode(decoder, "ASCII", byteBuffer, charBufferTest, true, false);\r
2737             errln("Overflow exception while decoding ASCII should have been thrown.");\r
2738         }\r
2739         catch(Exception ex) {\r
2740         }\r
2741         try {\r
2742             smBufEncode(encoder, "ASCII", charBuffer, byteBufferTest, true, false);\r
2743             errln("Overflow exception while encoding ASCII should have been thrown.");\r
2744         }\r
2745         catch (Exception ex) {\r
2746         }\r
2747         \r
2748         // For better code coverage\r
2749         /* For better code coverage */\r
2750         byte byteout[] = {\r
2751                 (byte)0x01\r
2752         };\r
2753         char charin[] = {\r
2754                 (char)0x0001, (char)0x0002\r
2755         };\r
2756         ByteBuffer bb = ByteBuffer.wrap(byteout);\r
2757         CharBuffer cb = CharBuffer.wrap(charin);\r
2758         CharBuffer cb2 = CharBuffer.wrap(cb.subSequence(0, 2));\r
2759         encoder.reset();\r
2760         if (!(encoder.encode(cb2, bb, true)).isOverflow()) {\r
2761             errln("Overflow error while encoding ASCII should have occurred.");\r
2762         }\r
2763     }\r
2764     //Test CharsetUTF7\r
2765     public void TestCharsetUTF7() {\r
2766         CoderResult result = CoderResult.UNDERFLOW;\r
2767         CharsetProvider provider = new CharsetProviderICU();\r
2768         Charset cs = provider.charsetForName("UTF-7");        \r
2769         CharsetEncoder encoder = cs.newEncoder();\r
2770         CharsetDecoder decoder = cs.newDecoder();\r
2771         \r
2772         CharBuffer us = CharBuffer.allocate(0x100);\r
2773         ByteBuffer bs = ByteBuffer.allocate(0x100);\r
2774         \r
2775         /* Unicode :  A<not equal to Alpha Lamda>. */\r
2776         /* UTF7: AImIDkQ. */\r
2777         us.put((char)0x41); us.put((char)0x2262); us.put((char)0x391); us.put((char)0x39B); us.put((char)0x2e);\r
2778         bs.put((byte)0x41); bs.put((byte)0x2b); bs.put((byte)0x49); bs.put((byte)0x6d); \r
2779         bs.put((byte)0x49); bs.put((byte)0x44); bs.put((byte)0x6b); bs.put((byte)0x51); \r
2780         bs.put((byte)0x4f); bs.put((byte)0x62); bs.put((byte)0x2e);\r
2781         \r
2782         bs.limit(bs.position());\r
2783         bs.position(0);\r
2784         us.limit(us.position());\r
2785         us.position(0);\r
2786 \r
2787         smBufDecode(decoder, "UTF-7", bs, us);\r
2788         smBufEncode(encoder, "UTF-7", us, bs);\r
2789         \r
2790         /* ticket 6151 */\r
2791         CharBuffer smallus = CharBuffer.allocate(1);\r
2792         ByteBuffer bigbs = ByteBuffer.allocate(3);\r
2793         bigbs.put((byte)0x41); bigbs.put((byte)0x41); bigbs.put((byte)0x41);\r
2794         bigbs.position(0);\r
2795         try {\r
2796             smBufDecode(decoder, "UTF-7-DE-Overflow", bigbs, smallus, true, false);\r
2797             errln("Buffer Overflow exception should have been thrown while decoding UTF-7.");\r
2798         } catch (Exception ex) {\r
2799         }\r
2800         \r
2801         //The rest of the code in this method is to provide better code coverage\r
2802         CharBuffer ccus = CharBuffer.allocate(0x10);\r
2803         ByteBuffer ccbs = ByteBuffer.allocate(0x10);\r
2804         \r
2805         //start of charset decoder code coverage code\r
2806         //test for accurate illegal and control character checking\r
2807         ccbs.put((byte)0x0D); ccbs.put((byte)0x05);\r
2808         ccus.put((char)0x0000);\r
2809         \r
2810         ccbs.limit(ccbs.position());\r
2811         ccbs.position(0);\r
2812         ccus.limit(ccus.position());\r
2813         ccus.position(0);\r
2814 \r
2815         try {\r
2816             smBufDecode(decoder, "UTF-7-CC-DE-1", ccbs, ccus, true, false);\r
2817             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2818         }\r
2819         catch (Exception ex) {\r
2820         }\r
2821         \r
2822         ccbs.clear();\r
2823         ccus.clear();\r
2824         \r
2825         //test for illegal base64 character\r
2826         ccbs.put((byte)0x2b); ccbs.put((byte)0xff);\r
2827         ccus.put((char)0x0000);\r
2828         \r
2829         ccbs.limit(ccbs.position());\r
2830         ccbs.position(0);\r
2831         ccus.limit(ccus.position());\r
2832         ccus.position(0);\r
2833         \r
2834         try {\r
2835             smBufDecode(decoder, "UTF-7-CC-DE-2", ccbs, ccus, true, false);\r
2836             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2837         }\r
2838         catch (Exception ex) {\r
2839         }\r
2840         \r
2841         ccbs.clear();\r
2842         ccus.clear();\r
2843         \r
2844         //test for illegal order of the base64 character sequence\r
2845         ccbs.put((byte)0x2b); ccbs.put((byte)0x2d); ccbs.put((byte)0x2b); ccbs.put((byte)0x49); ccbs.put((byte)0x2d);\r
2846         ccus.put((char)0x0000); ccus.put((char)0x0000);\r
2847         \r
2848         ccbs.limit(ccbs.position());\r
2849         ccbs.position(0);\r
2850         ccus.limit(ccus.position());\r
2851         ccus.position(0);\r
2852         \r
2853         try {\r
2854             smBufDecode(decoder, "UTF-7-CC-DE-3", ccbs, ccus, true, false);\r
2855             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2856         }\r
2857         catch (Exception ex) {\r
2858         }\r
2859         \r
2860         ccbs.clear();\r
2861         ccus.clear();\r
2862         \r
2863         //test for illegal order of the base64 character sequence \r
2864         ccbs.put((byte)0x2b); ccbs.put((byte)0x0a); ccbs.put((byte)0x09);\r
2865         ccus.put((char)0x0000);\r
2866         \r
2867         ccbs.limit(ccbs.position());\r
2868         ccbs.position(0);\r
2869         ccus.limit(ccus.position());\r
2870         ccus.position(0);\r
2871         \r
2872         try {\r
2873             smBufDecode(decoder, "UTF-7-CC-DE-4", ccbs, ccus, true, false);\r
2874             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2875         }\r
2876         catch (Exception ex) {\r
2877         }\r
2878         \r
2879         ccbs.clear();\r
2880         ccus.clear();\r
2881         \r
2882         //test for illegal order of the base64 character sequence\r
2883         ccbs.put((byte)0x2b); ccbs.put((byte)0x49); ccbs.put((byte)0x0a);\r
2884         ccus.put((char)0x0000);\r
2885         \r
2886         ccbs.limit(ccbs.position());\r
2887         ccbs.position(0);\r
2888         ccus.limit(ccus.position());\r
2889         ccus.position(0);\r
2890         \r
2891         try {\r
2892             smBufDecode(decoder, "UTF-7-CC-DE-5", ccbs, ccus, true, false);\r
2893             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2894         }\r
2895         catch (Exception ex) {\r
2896         }\r
2897         \r
2898         ccbs.clear();\r
2899         ccus.clear();\r
2900         \r
2901         //test for illegal order of the base64 character sequence\r
2902         ccbs.put((byte)0x2b); ccbs.put((byte)0x00);\r
2903         ccus.put((char)0x0000);\r
2904         \r
2905         ccbs.limit(ccbs.position());\r
2906         ccbs.position(0);\r
2907         ccus.limit(ccus.position());\r
2908         ccus.position(0);\r
2909         \r
2910         try {\r
2911             smBufDecode(decoder, "UTF-7-CC-DE-6", ccbs, ccus, true, false);\r
2912             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2913         }\r
2914         catch (Exception ex) {\r
2915         }\r
2916         \r
2917         ccbs.clear();\r
2918         ccus.clear();\r
2919         \r
2920         //test for overflow buffer error\r
2921         ccbs.put((byte)0x2b); ccbs.put((byte)0x49);\r
2922         \r
2923         ccbs.limit(ccbs.position());\r
2924         ccbs.position(0);\r
2925         ccus.limit(0);\r
2926         ccus.position(0);\r
2927         \r
2928         try {\r
2929             smBufDecode(decoder, "UTF-7-CC-DE-7", ccbs, ccus, true, false);\r
2930             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2931         }\r
2932         catch (Exception ex) {\r
2933         }\r
2934         \r
2935         ccbs.clear();\r
2936         ccus.clear();\r
2937         \r
2938         //test for overflow buffer error\r
2939         ccbs.put((byte)0x0c); ccbs.put((byte)0x0c);\r
2940         \r
2941         ccbs.limit(ccbs.position());\r
2942         ccbs.position(0);\r
2943         ccus.limit(0);\r
2944         ccus.position(0);\r
2945         \r
2946         try {\r
2947             smBufDecode(decoder, "UTF-7-CC-DE-8", ccbs, ccus, true, false);\r
2948             errln("Exception while decoding UTF-7 code coverage test should have been thrown.");\r
2949         }\r
2950         catch (Exception ex) {\r
2951         }\r
2952         //end of charset decoder code coverage code\r
2953         \r
2954         //start of charset encoder code coverage code\r
2955         ccbs.clear();\r
2956         ccus.clear();\r
2957         //test for overflow buffer error\r
2958         ccus.put((char)0x002b);\r
2959         ccbs.put((byte)0x2b); \r
2960         \r
2961         ccbs.limit(ccbs.position());\r
2962         ccbs.position(0);\r
2963         ccus.limit(ccus.position());\r
2964         ccus.position(0);\r
2965         \r
2966         try {\r
2967             smBufEncode(encoder, "UTF-7-CC-EN-1", ccus, ccbs, true, false);\r
2968             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
2969         }\r
2970         catch (Exception ex) {\r
2971         }\r
2972         \r
2973         ccbs.clear();\r
2974         ccus.clear();\r
2975         \r
2976         //test for overflow buffer error\r
2977         ccus.put((char)0x002b); ccus.put((char)0x2262);\r
2978         ccbs.put((byte)0x2b); ccbs.put((byte)0x2d); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
2979         \r
2980         ccbs.limit(ccbs.position());\r
2981         ccbs.position(0);\r
2982         ccus.limit(ccus.position());\r
2983         ccus.position(0);\r
2984         \r
2985         try {\r
2986             smBufEncode(encoder, "UTF-7-CC-EN-2", ccus, ccbs, true, false);\r
2987             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
2988         }\r
2989         catch (Exception ex) {\r
2990         } \r
2991         \r
2992         ccbs.clear();\r
2993         ccus.clear();\r
2994         \r
2995         //test for overflow buffer error\r
2996         ccus.put((char)0x2262); ccus.put((char)0x0049);\r
2997         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
2998         ccbs.limit(ccbs.position());\r
2999         ccbs.position(0);\r
3000         ccus.limit(ccus.position());\r
3001         ccus.position(0);\r
3002         \r
3003         try {\r
3004             smBufEncode(encoder, "UTF-7-CC-EN-3", ccus, ccbs, true, false);\r
3005             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3006         }\r
3007         catch (Exception ex) {\r
3008         }  \r
3009         \r
3010         ccbs.clear();\r
3011         ccus.clear();\r
3012         \r
3013         //test for overflow buffer error\r
3014         ccus.put((char)0x2262); ccus.put((char)0x0395);\r
3015         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3016         ccbs.limit(ccbs.position());\r
3017         ccbs.position(0);\r
3018         ccus.limit(ccus.position());\r
3019         ccus.position(0);\r
3020         \r
3021         try {\r
3022             smBufEncode(encoder, "UTF-7-CC-EN-4", ccus, ccbs, true, false);\r
3023             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3024         }\r
3025         catch (Exception ex) {\r
3026         }  \r
3027         \r
3028         ccbs.clear();\r
3029         ccus.clear();\r
3030         \r
3031         //test for overflow buffer error\r
3032         ccus.put((char)0x2262); ccus.put((char)0x0395);\r
3033         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3034         ccbs.limit(ccbs.position());\r
3035         ccbs.position(0);\r
3036         ccus.limit(ccus.position());\r
3037         ccus.position(0);\r
3038         \r
3039         try {\r
3040             smBufEncode(encoder, "UTF-7-CC-EN-5", ccus, ccbs, true, false);\r
3041             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3042         }\r
3043         catch (Exception ex) {\r
3044         }  \r
3045         \r
3046         ccbs.clear();\r
3047         ccus.clear();\r
3048         \r
3049         //test for overflow buffer error\r
3050         ccus.put((char)0x2262); ccus.put((char)0x0395); ccus.put((char)0x0391);\r
3051         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3052         ccbs.limit(ccbs.position());\r
3053         ccbs.position(0);\r
3054         ccus.limit(ccus.position());\r
3055         ccus.position(0);\r
3056         \r
3057         try {\r
3058             smBufEncode(encoder, "UTF-7-CC-EN-6", ccus, ccbs, true, false);\r
3059             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3060         }\r
3061         catch (Exception ex) {\r
3062         }  \r
3063         \r
3064         ccbs.clear();\r
3065         ccus.clear();\r
3066         \r
3067         //test for overflow buffer error\r
3068         ccus.put((char)0x2262); ccus.put((char)0x0395); ccus.put((char)0x0391);\r
3069         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); \r
3070         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3071         ccbs.limit(ccbs.position());\r
3072         ccbs.position(0);\r
3073         ccus.limit(ccus.position());\r
3074         ccus.position(0);\r
3075         \r
3076         try {\r
3077             smBufEncode(encoder, "UTF-7-CC-EN-7", ccus, ccbs, true, false);\r
3078             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3079         }\r
3080         catch (Exception ex) {\r
3081         }  \r
3082         \r
3083         ccbs.clear();\r
3084         ccus.clear();\r
3085         \r
3086         //test for overflow buffer error\r
3087         ccus.put((char)0x0049); ccus.put((char)0x0048);\r
3088         ccbs.put((byte)0x00); \r
3089         ccbs.limit(ccbs.position());\r
3090         ccbs.position(0);\r
3091         ccus.limit(ccus.position());\r
3092         ccus.position(0);\r
3093         \r
3094         try {\r
3095             smBufEncode(encoder, "UTF-7-CC-EN-8", ccus, ccbs, true, false);\r
3096             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3097         }\r
3098         catch (Exception ex) {\r
3099         } \r
3100         \r
3101         ccbs.clear();\r
3102         ccus.clear();\r
3103         \r
3104         //test for overflow buffer error\r
3105         ccus.put((char)0x2262);\r
3106         ccbs.put((byte)0x00);\r
3107         ccbs.limit(ccbs.position());\r
3108         ccbs.position(0);\r
3109         ccus.limit(ccus.position());\r
3110         ccus.position(0);\r
3111         \r
3112         try {\r
3113             smBufEncode(encoder, "UTF-7-CC-EN-9", ccus, ccbs, true, false);\r
3114             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3115         }\r
3116         catch (Exception ex) {\r
3117         } \r
3118         \r
3119         ccbs.clear();\r
3120         ccus.clear();\r
3121         \r
3122         //test for overflow buffer error\r
3123         ccus.put((char)0x2262); ccus.put((char)0x0049);\r
3124         ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3125         ccbs.limit(ccbs.position());\r
3126         ccbs.position(0);\r
3127         ccus.limit(ccus.position());\r
3128         ccus.position(0);\r
3129         \r
3130         try {\r
3131             smBufEncode(encoder, "UTF-7-CC-EN-10", ccus, ccbs, true, false);\r
3132             errln("Exception while encoding UTF-7 code coverage test should have been thrown.");\r
3133         }\r
3134         catch (Exception ex) {\r
3135         }  \r
3136         \r
3137         ccbs.clear();\r
3138         ccus.clear();\r
3139         \r
3140         //test for overflow buffer error\r
3141         ccus.put((char)0x2262);\r
3142         ccbs.put((byte)0x2b); ccbs.put((byte)0x49); ccbs.put((byte)0x6d); ccbs.put((byte)0x49);\r
3143         \r
3144         ccbs.limit(ccbs.position());\r
3145         ccbs.position(0);\r
3146         ccus.limit(ccus.position());\r
3147         ccus.position(0);\r
3148         try {\r
3149             smBufEncode(encoder, "UTF-7-CC-EN-11", ccus, ccbs, false, true);\r
3150         } catch (Exception ex) {\r
3151             errln("Exception while encoding UTF-7 code coverage test should not have been thrown.");\r
3152         }\r
3153         \r
3154         ccbs.clear();\r
3155         ccus.clear();\r
3156         \r
3157         //test for overflow buffer error\r
3158         encoder.reset();\r
3159         ccus.put((char)0x3980); ccus.put((char)0x2715);\r
3160         ccbs.put((byte)0x2b); ccbs.put((byte)0x4f); ccbs.put((byte)0x59);\r
3161         \r
3162         ccbs.limit(ccbs.position());\r
3163         ccbs.position(0);\r
3164         ccus.limit(ccus.position());\r
3165         ccus.position(0);\r
3166         \r
3167         result = encoder.encode(ccus, ccbs, true);\r
3168         result = encoder.flush(ccbs);\r
3169         if (!result.isOverflow()) {\r
3170             errln("Overflow buffer while encoding UTF-7 should have occurred.");\r
3171         }\r
3172         //end of charset encoder code coverage code\r
3173     }\r
3174     //Test Charset ISCII\r
3175     public void TestCharsetISCII() {\r
3176         CharsetProvider provider = new CharsetProviderICU();\r
3177         Charset cs = provider.charsetForName("ISCII,version=0");        \r
3178         CharsetEncoder encoder = cs.newEncoder();\r
3179         CharsetDecoder decoder = cs.newDecoder();\r
3180         \r
3181         CharBuffer us = CharBuffer.allocate(0x100);\r
3182         ByteBuffer bs = ByteBuffer.allocate(0x100);\r
3183         ByteBuffer bsr = ByteBuffer.allocate(0x100);\r
3184         \r
3185         //test full range of Devanagari\r
3186         us.put((char)0x0901); us.put((char)0x0902); us.put((char)0x0903); us.put((char)0x0905); us.put((char)0x0906); us.put((char)0x0907);\r
3187         us.put((char)0x0908); us.put((char)0x0909); us.put((char)0x090A); us.put((char)0x090B); us.put((char)0x090E); us.put((char)0x090F);\r
3188         us.put((char)0x0910); us.put((char)0x090D); us.put((char)0x0912); us.put((char)0x0913); us.put((char)0x0914); us.put((char)0x0911);\r
3189         us.put((char)0x0915); us.put((char)0x0916); us.put((char)0x0917); us.put((char)0x0918); us.put((char)0x0919); us.put((char)0x091A);\r
3190         us.put((char)0x091B); us.put((char)0x091C); us.put((char)0x091D); us.put((char)0x091E); us.put((char)0x091F); us.put((char)0x0920);\r
3191         us.put((char)0x0921); us.put((char)0x0922); us.put((char)0x0923); us.put((char)0x0924); us.put((char)0x0925); us.put((char)0x0926); \r
3192         us.put((char)0x0927); us.put((char)0x0928); us.put((char)0x0929); us.put((char)0x092A); us.put((char)0x092B); us.put((char)0x092C); \r
3193         us.put((char)0x092D); us.put((char)0x092E); us.put((char)0x092F); us.put((char)0x095F); us.put((char)0x0930); us.put((char)0x0931); \r
3194         us.put((char)0x0932); us.put((char)0x0933); us.put((char)0x0934); us.put((char)0x0935); us.put((char)0x0936); us.put((char)0x0937); \r
3195         us.put((char)0x0938); us.put((char)0x0939); us.put((char)0x200D); us.put((char)0x093E); us.put((char)0x093F); us.put((char)0x0940); \r
3196         us.put((char)0x0941); us.put((char)0x0942); us.put((char)0x0943); us.put((char)0x0946); us.put((char)0x0947); us.put((char)0x0948); \r
3197         us.put((char)0x0945); us.put((char)0x094A); us.put((char)0x094B); us.put((char)0x094C); us.put((char)0x0949); us.put((char)0x094D); \r
3198         us.put((char)0x093D); us.put((char)0x0966); us.put((char)0x0967); us.put((char)0x0968); us.put((char)0x0969); us.put((char)0x096A); \r
3199         us.put((char)0x096B); us.put((char)0x096C); us.put((char)0x096D); us.put((char)0x096E); us.put((char)0x096F); \r
3200         \r
3201         bs.put((byte)0xEF); bs.put((byte)0x42);\r
3202         bs.put((byte)0xA1); bs.put((byte)0xA2); bs.put((byte)0xA3); bs.put((byte)0xA4); bs.put((byte)0xA5); bs.put((byte)0xA6);\r
3203         bs.put((byte)0xA7); bs.put((byte)0xA8); bs.put((byte)0xA9); bs.put((byte)0xAA); bs.put((byte)0xAB); bs.put((byte)0xAC); \r
3204         bs.put((byte)0xAD); bs.put((byte)0xAE); bs.put((byte)0xAF); bs.put((byte)0xB0); bs.put((byte)0xB1); bs.put((byte)0xB2); \r
3205         bs.put((byte)0xB3); bs.put((byte)0xB4); bs.put((byte)0xB5); bs.put((byte)0xB6); bs.put((byte)0xB7); bs.put((byte)0xB8); \r
3206         bs.put((byte)0xB9); bs.put((byte)0xBA); bs.put((byte)0xBB); bs.put((byte)0xBC); bs.put((byte)0xBD); bs.put((byte)0xBE); \r
3207         bs.put((byte)0xBF); bs.put((byte)0xC0); bs.put((byte)0xC1); bs.put((byte)0xC2); bs.put((byte)0xC3); bs.put((byte)0xC4); \r
3208         bs.put((byte)0xC5); bs.put((byte)0xC6); bs.put((byte)0xC7); bs.put((byte)0xC8); bs.put((byte)0xC9); bs.put((byte)0xCA); \r
3209         bs.put((byte)0xCB); bs.put((byte)0xCC); bs.put((byte)0xCD); bs.put((byte)0xCE); bs.put((byte)0xCF); bs.put((byte)0xD0); \r
3210         bs.put((byte)0xD1); bs.put((byte)0xD2); bs.put((byte)0xD3); bs.put((byte)0xD4); bs.put((byte)0xD5); bs.put((byte)0xD6); \r
3211         bs.put((byte)0xD7); bs.put((byte)0xD8); bs.put((byte)0xD9); bs.put((byte)0xDA); bs.put((byte)0xDB); bs.put((byte)0xDC); \r
3212         bs.put((byte)0xDD); bs.put((byte)0xDE); bs.put((byte)0xDF); bs.put((byte)0xE0); bs.put((byte)0xE1); bs.put((byte)0xE2); \r
3213         bs.put((byte)0xE3); bs.put((byte)0xE4); bs.put((byte)0xE5); bs.put((byte)0xE6); bs.put((byte)0xE7); bs.put((byte)0xE8); \r
3214         bs.put((byte)0xEA); bs.put((byte)0xE9); bs.put((byte)0xF1); bs.put((byte)0xF2); bs.put((byte)0xF3); bs.put((byte)0xF4); \r
3215         bs.put((byte)0xF5); bs.put((byte)0xF6); bs.put((byte)0xF7); bs.put((byte)0xF8); bs.put((byte)0xF9); bs.put((byte)0xFA); \r
3216         \r
3217         bsr.put((byte)0xA1); bsr.put((byte)0xA2); bsr.put((byte)0xA3); bsr.put((byte)0xA4); bsr.put((byte)0xA5); bsr.put((byte)0xA6);\r
3218         bsr.put((byte)0xA7); bsr.put((byte)0xA8); bsr.put((byte)0xA9); bsr.put((byte)0xAA); bsr.put((byte)0xAB); bsr.put((byte)0xAC); \r
3219         bsr.put((byte)0xAD); bsr.put((byte)0xAE); bsr.put((byte)0xAF); bsr.put((byte)0xB0); bsr.put((byte)0xB1); bsr.put((byte)0xB2); \r
3220         bsr.put((byte)0xB3); bsr.put((byte)0xB4); bsr.put((byte)0xB5); bsr.put((byte)0xB6); bsr.put((byte)0xB7); bsr.put((byte)0xB8); \r
3221         bsr.put((byte)0xB9); bsr.put((byte)0xBA); bsr.put((byte)0xBB); bsr.put((byte)0xBC); bsr.put((byte)0xBD); bsr.put((byte)0xBE); \r
3222         bsr.put((byte)0xBF); bsr.put((byte)0xC0); bsr.put((byte)0xC1); bsr.put((byte)0xC2); bsr.put((byte)0xC3); bsr.put((byte)0xC4); \r
3223         bsr.put((byte)0xC5); bsr.put((byte)0xC6); bsr.put((byte)0xC7); bsr.put((byte)0xC8); bsr.put((byte)0xC9); bsr.put((byte)0xCA); \r
3224         bsr.put((byte)0xCB); bsr.put((byte)0xCC); bsr.put((byte)0xCD); bsr.put((byte)0xCE); bsr.put((byte)0xCF); bsr.put((byte)0xD0); \r
3225         bsr.put((byte)0xD1); bsr.put((byte)0xD2); bsr.put((byte)0xD3); bsr.put((byte)0xD4); bsr.put((byte)0xD5); bsr.put((byte)0xD6); \r
3226         bsr.put((byte)0xD7); bsr.put((byte)0xD8); bsr.put((byte)0xD9); bsr.put((byte)0xDA); bsr.put((byte)0xDB); bsr.put((byte)0xDC); \r
3227         bsr.put((byte)0xDD); bsr.put((byte)0xDE); bsr.put((byte)0xDF); bsr.put((byte)0xE0); bsr.put((byte)0xE1); bsr.put((byte)0xE2); \r
3228         bsr.put((byte)0xE3); bsr.put((byte)0xE4); bsr.put((byte)0xE5); bsr.put((byte)0xE6); bsr.put((byte)0xE7); bsr.put((byte)0xE8); \r
3229         bsr.put((byte)0xEA); bsr.put((byte)0xE9); bsr.put((byte)0xF1); bsr.put((byte)0xF2); bsr.put((byte)0xF3); bsr.put((byte)0xF4); \r
3230         bsr.put((byte)0xF5); bsr.put((byte)0xF6); bsr.put((byte)0xF7); bsr.put((byte)0xF8); bsr.put((byte)0xF9); bsr.put((byte)0xFA); \r
3231         \r
3232         //test Soft Halant\r
3233         us.put((char)0x0915); us.put((char)0x094d); us.put((char)0x200D);\r
3234         bs.put((byte)0xB3); bs.put((byte)0xE8); bs.put((byte)0xE9);\r
3235         bsr.put((byte)0xB3); bsr.put((byte)0xE8); bsr.put((byte)0xE9);\r
3236         \r
3237         //test explicit halant\r
3238         us.put((char)0x0915); us.put((char)0x094D); us.put((char)0x200C);\r
3239         bs.put((byte)0xB3); bs.put((byte)0xE8); bs.put((byte)0xE8);\r
3240         bsr.put((byte)0xB3); bsr.put((byte)0xE8); bsr.put((byte)0xE8);\r
3241         \r
3242         //test double danda\r
3243         us.put((char)0x0965); \r
3244         bs.put((byte)0xEA); bs.put((byte)0xEA); \r
3245         bsr.put((byte)0xEA); bsr.put((byte)0xEA); \r
3246         \r
3247         //test ASCII\r
3248         us.put((char)0x1B); us.put((char)0x24); us.put((char)0x29); us.put((char)0x47); us.put((char)0x0E); us.put((char)0x23);\r
3249         us.put((char)0x21); us.put((char)0x23); us.put((char)0x22); us.put((char)0x23); us.put((char)0x23); us.put((char)0x23);\r
3250         us.put((char)0x24); us.put((char)0x23); us.put((char)0x25); us.put((char)0x23); us.put((char)0x26); us.put((char)0x23);\r
3251         us.put((char)0x27); us.put((char)0x23); us.put((char)0x28); us.put((char)0x23); us.put((char)0x29); us.put((char)0x23);\r
3252         us.put((char)0x2A); us.put((char)0x23); us.put((char)0x2B); us.put((char)0x0F); us.put((char)0x2F); us.put((char)0x2A);\r
3253         \r
3254         bs.put((byte)0x1B); bs.put((byte)0x24); bs.put((byte)0x29); bs.put((byte)0x47); bs.put((byte)0x0E); bs.put((byte)0x23);\r
3255         bs.put((byte)0x21); bs.put((byte)0x23); bs.put((byte)0x22); bs.put((byte)0x23); bs.put((byte)0x23); bs.put((byte)0x23);\r
3256         bs.put((byte)0x24); bs.put((byte)0x23); bs.put((byte)0x25); bs.put((byte)0x23); bs.put((byte)0x26); bs.put((byte)0x23);\r
3257         bs.put((byte)0x27); bs.put((byte)0x23); bs.put((byte)0x28); bs.put((byte)0x23); bs.put((byte)0x29); bs.put((byte)0x23);\r
3258         bs.put((byte)0x2A); bs.put((byte)0x23); bs.put((byte)0x2B); bs.put((byte)0x0F); bs.put((byte)0x2F); bs.put((byte)0x2A);\r
3259         \r
3260         bsr.put((byte)0x1B); bsr.put((byte)0x24); bsr.put((byte)0x29); bsr.put((byte)0x47); bsr.put((byte)0x0E); bsr.put((byte)0x23);\r
3261         bsr.put((byte)0x21); bsr.put((byte)0x23); bsr.put((byte)0x22); bsr.put((byte)0x23); bsr.put((byte)0x23); bsr.put((byte)0x23);\r
3262         bsr.put((byte)0x24); bsr.put((byte)0x23); bsr.put((byte)0x25); bsr.put((byte)0x23); bsr.put((byte)0x26); bsr.put((byte)0x23);\r
3263         bsr.put((byte)0x27); bsr.put((byte)0x23); bsr.put((byte)0x28); bsr.put((byte)0x23); bsr.put((byte)0x29); bsr.put((byte)0x23);\r
3264         bsr.put((byte)0x2A); bsr.put((byte)0x23); bsr.put((byte)0x2B); bsr.put((byte)0x0F); bsr.put((byte)0x2F); bsr.put((byte)0x2A);\r
3265         \r
3266         //test from Lotus\r
3267         //Some of the Lotus ISCII code points have been changed or commented out.\r
3268         us.put((char)0x0061); us.put((char)0x0915); us.put((char)0x000D); us.put((char)0x000A); us.put((char)0x0996); us.put((char)0x0043);\r
3269         us.put((char)0x0930); us.put((char)0x094D); us.put((char)0x200D); us.put((char)0x0901); us.put((char)0x000D); us.put((char)0x000A);\r
3270         us.put((char)0x0905); us.put((char)0x0985); us.put((char)0x0043); us.put((char)0x0915); us.put((char)0x0921); us.put((char)0x002B);\r
3271         us.put((char)0x095F); \r
3272         bs.put((byte)0x61); bs.put((byte)0xB3);\r
3273         bs.put((byte)0x0D); bs.put((byte)0x0A); \r
3274         bs.put((byte)0xEF); bs.put((byte)0x42); \r
3275         bs.put((byte)0xEF); bs.put((byte)0x43); bs.put((byte)0xB4); bs.put((byte)0x43);\r
3276         bs.put((byte)0xEF); bs.put((byte)0x42); bs.put((byte)0xCF); bs.put((byte)0xE8); bs.put((byte)0xE9); bs.put((byte)0xA1); bs.put((byte)0x0D); bs.put((byte)0x0A); bs.put((byte)0xEF); bs.put((byte)0x42);\r
3277         bs.put((byte)0xA4); bs.put((byte)0xEF); bs.put((byte)0x43); bs.put((byte)0xA4); bs.put((byte)0x43); bs.put((byte)0xEF);\r
3278         bs.put((byte)0x42); bs.put((byte)0xB3); bs.put((byte)0xBF); bs.put((byte)0x2B);\r
3279         bs.put((byte)0xCE);\r
3280         bsr.put((byte)0x61); bsr.put((byte)0xEF); bsr.put((byte)0x42); bsr.put((byte)0xEF); bsr.put((byte)0x30); bsr.put((byte)0xB3);\r
3281         bsr.put((byte)0x0D); bsr.put((byte)0x0A); bsr.put((byte)0xEF); bsr.put((byte)0x43); bsr.put((byte)0xB4); bsr.put((byte)0x43);\r
3282         bsr.put((byte)0xEF); bsr.put((byte)0x42); bsr.put((byte)0xCF); bsr.put((byte)0xE8); bsr.put((byte)0xD9); bsr.put((byte)0xEF);\r
3283         bsr.put((byte)0x42); bsr.put((byte)0xA1); bsr.put((byte)0x0D); bsr.put((byte)0x0A); bsr.put((byte)0xEF); bsr.put((byte)0x42);\r
3284         bsr.put((byte)0xA4); bsr.put((byte)0xEF); bsr.put((byte)0x43); bsr.put((byte)0xA4); bsr.put((byte)0x43); bsr.put((byte)0xEF);\r
3285         bsr.put((byte)0x42); bsr.put((byte)0xB3); bsr.put((byte)0xBF); bsr.put((byte)0x2B); bsr.put((byte)0xEF); bsr.put((byte)0x42);\r
3286         bsr.put((byte)0xCE);\r
3287         //end of test from Lotus\r
3288         \r
3289         //tamil range\r
3290         us.put((char)0x0B86); us.put((char)0x0B87); us.put((char)0x0B88);\r
3291         bs.put((byte)0xEF); bs.put((byte)0x44); bs.put((byte)0xA5); bs.put((byte)0xA6); bs.put((byte)0xA7);\r
3292         bsr.put((byte)0xEF); bsr.put((byte)0x44); bsr.put((byte)0xA5); bsr.put((byte)0xA6); bsr.put((byte)0xA7);\r
3293         \r
3294         //telugu range\r
3295         us.put((char)0x0C05); us.put((char)0x0C02); us.put((char)0x0C03); us.put((char)0x0C31);\r
3296         bs.put((byte)0xEF); bs.put((byte)0x45); bs.put((byte)0xA4); bs.put((byte)0xA2); bs.put((byte)0xA3); bs.put((byte)0xD0);\r
3297         bsr.put((byte)0xEF); bsr.put((byte)0x45); bsr.put((byte)0xA4); bsr.put((byte)0xA2); bsr.put((byte)0xA3); bsr.put((byte)0xD0);\r
3298         \r
3299         //kannada range\r
3300         us.put((char)0x0C85); us.put((char)0x0C82); us.put((char)0x0C83);\r
3301         bs.put((byte)0xEF); bs.put((byte)0x48); bs.put((byte)0xA4); bs.put((byte)0xA2); bs.put((byte)0xA3);\r
3302         bsr.put((byte)0xEF); bsr.put((byte)0x48); bsr.put((byte)0xA4); bsr.put((byte)0xA2); bsr.put((byte)0xA3);  \r
3303         \r
3304         //test Abbr sign and Anudatta\r
3305         us.put((char)0x0970); us.put((char)0x0952); us.put((char)0x0960); us.put((char)0x0944); us.put((char)0x090C); us.put((char)0x0962);\r
3306         us.put((char)0x0961); us.put((char)0x0963); us.put((char)0x0950); us.put((char)0x093D); us.put((char)0x0958); us.put((char)0x0959);\r
3307         us.put((char)0x095A); us.put((char)0x095B); us.put((char)0x095C); us.put((char)0x095D); us.put((char)0x095E); us.put((char)0x0020);\r
3308         us.put((char)0x094D); us.put((char)0x0930); us.put((char)0x0000); us.put((char)0x00A0); \r
3309         bs.put((byte)0xEF); bs.put((byte)0x42); bs.put((byte)0xF0); bs.put((byte)0xBF); bs.put((byte)0xF0); bs.put((byte)0xB8);\r
3310         bs.put((byte)0xAA); bs.put((byte)0xE9); bs.put((byte)0xDF); bs.put((byte)0xE9); bs.put((byte)0xA6); bs.put((byte)0xE9);\r
3311         bs.put((byte)0xDB); bs.put((byte)0xE9); bs.put((byte)0xA7); bs.put((byte)0xE9); bs.put((byte)0xDC); bs.put((byte)0xE9);\r
3312         bs.put((byte)0xA1); bs.put((byte)0xE9); bs.put((byte)0xEA); bs.put((byte)0xE9); bs.put((byte)0xB3); bs.put((byte)0xE9);\r
3313         bs.put((byte)0xB4); bs.put((byte)0xE9); bs.put((byte)0xB5); bs.put((byte)0xE9); bs.put((byte)0xBA); bs.put((byte)0xE9);\r
3314         bs.put((byte)0xBF); bs.put((byte)0xE9); bs.put((byte)0xC0); bs.put((byte)0xE9); bs.put((byte)0xC9); bs.put((byte)0xE9);\r
3315         bs.put((byte)0x20); bs.put((byte)0xE8); bs.put((byte)0xCF); bs.put((byte)0x00); bs.put((byte)0xA0); \r
3316         //bs.put((byte)0xEF); bs.put((byte)0x30); \r
3317         bsr.put((byte)0xEF); bsr.put((byte)0x42); bsr.put((byte)0xF0); bsr.put((byte)0xBF); bsr.put((byte)0xF0); bsr.put((byte)0xB8);\r
3318         bsr.put((byte)0xAA); bsr.put((byte)0xE9); bsr.put((byte)0xDF); bsr.put((byte)0xE9); bsr.put((byte)0xA6); bsr.put((byte)0xE9);\r
3319         bsr.put((byte)0xDB); bsr.put((byte)0xE9); bsr.put((byte)0xA7); bsr.put((byte)0xE9); bsr.put((byte)0xDC); bsr.put((byte)0xE9);\r
3320         bsr.put((byte)0xA1); bsr.put((byte)0xE9); bsr.put((byte)0xEA); bsr.put((byte)0xE9); bsr.put((byte)0xB3); bsr.put((byte)0xE9);\r
3321         bsr.put((byte)0xB4); bsr.put((byte)0xE9); bsr.put((byte)0xB5); bsr.put((byte)0xE9); bsr.put((byte)0xBA); bsr.put((byte)0xE9);\r
3322         bsr.put((byte)0xBF); bsr.put((byte)0xE9); bsr.put((byte)0xC0); bsr.put((byte)0xE9); bsr.put((byte)0xC9); bsr.put((byte)0xE9);\r
3323         bsr.put((byte)0xD9); bsr.put((byte)0xE8); bsr.put((byte)0xCF); bsr.put((byte)0x00); bsr.put((byte)0xA0);  \r
3324         \r
3325         bs.limit(bs.position());\r
3326         bs.position(0);\r
3327         us.limit(us.position());\r
3328         us.position(0);\r
3329         bsr.limit(bsr.position());\r
3330         bsr.position(0);\r
3331         \r
3332         //round trip test\r
3333         try {\r
3334             smBufDecode(decoder, "ISCII-part1", bsr, us, false, true);\r
3335             smBufEncode(encoder, "ISCII-part2", us, bs); \r
3336             smBufDecode(decoder, "ISCII-part3", bs, us, false, true);\r
3337         } catch (Exception ex) {\r
3338             errln("ISCII round trip test failed.");\r
3339         }\r
3340         \r
3341         //Test new characters in the ISCII charset\r
3342         encoder = provider.charsetForName("ISCII,version=0").newEncoder();\r
3343         decoder = provider.charsetForName("ISCII,version=0").newDecoder();\r
3344         char u_pts[] = {\r
3345                 /* DEV */ (char)0x0904,\r
3346                 /* PNJ */ (char)0x0A01, (char)0x0A03, (char)0x0A33, (char)0x0A70\r
3347             };\r
3348         byte b_pts[] = {\r
3349                                 (byte)0xef, (byte)0x42,\r
3350                 /* DEV */ (byte)0xa4, (byte)0xe0,\r
3351                 /* PNJ */ (byte)0xef, (byte)0x4b, (byte)0xa1, (byte)0xa3, (byte)0xd2, (byte)0xf0, (byte)0xbf\r
3352             };\r
3353         us = CharBuffer.allocate(u_pts.length);\r
3354         bs = ByteBuffer.allocate(b_pts.length);\r
3355         us.put(u_pts);\r
3356         bs.put(b_pts);\r
3357         \r
3358         bs.limit(bs.position());\r
3359         bs.position(0);\r
3360         us.limit(us.position());\r
3361         us.position(0);\r
3362         \r
3363         try {\r
3364             smBufDecode(decoder, "ISCII-update", bs, us, true, true);         \r
3365             bs.position(0);\r
3366             us.position(0);\r
3367             smBufEncode(encoder, "ISCII-update", us, bs, true, true);\r
3368         } catch (Exception ex) {\r
3369             errln("Error occurred while encoding/decoding ISCII with the new characters.");\r
3370         }\r
3371         \r
3372         //The rest of the code in this method is to provide better code coverage\r
3373         CharBuffer ccus = CharBuffer.allocate(0x10);\r
3374         ByteBuffer ccbs = ByteBuffer.allocate(0x10);\r
3375         \r
3376         //start of charset decoder code coverage code\r
3377         //test overflow buffer\r
3378         ccbs.put((byte)0x49);\r
3379         \r
3380         ccbs.limit(ccbs.position());\r
3381         ccbs.position(0);\r
3382         ccus.limit(0);\r
3383         ccus.position(0);\r
3384         \r
3385         try {\r
3386             smBufDecode(decoder, "ISCII-CC-DE-1", ccbs, ccus, true, false);\r
3387             errln("Exception while decoding ISCII should have been thrown.");\r
3388         }\r
3389         catch (Exception ex) {\r
3390         }\r
3391         \r
3392         ccbs.clear();\r
3393         ccus.clear();\r
3394         \r
3395         //test atr overflow buffer\r
3396         ccbs.put((byte)0xEF); ccbs.put((byte)0x40); ccbs.put((byte)0xEF); ccbs.put((byte)0x20);\r
3397         ccus.put((char)0x00);\r
3398         \r
3399         ccbs.limit(ccbs.position());\r
3400         ccbs.position(0);\r
3401         ccus.limit(ccus.position());\r
3402         ccus.position(0);\r
3403         \r
3404         try {\r
3405             smBufDecode(decoder, "ISCII-CC-DE-2", ccbs, ccus, true, false);\r
3406             errln("Exception while decoding ISCII should have been thrown.");\r
3407         }\r
3408         catch (Exception ex) {\r
3409         }\r
3410         \r
3411         //end of charset decoder code coverage code\r
3412         \r
3413         ccbs.clear();\r
3414         ccus.clear();\r
3415       \r
3416         //start of charset encoder code coverage code\r
3417         //test ascii overflow buffer\r
3418         ccus.put((char)0x41);\r
3419         \r
3420         ccus.limit(ccus.position());\r
3421         ccus.position(0);\r
3422         ccbs.limit(0);\r
3423         ccbs.position(0);\r
3424            \r
3425         try {\r
3426             smBufEncode(encoder, "ISCII-CC-EN-1", ccus, ccbs, true, false);\r
3427             errln("Exception while encoding ISCII should have been thrown.");\r
3428         }\r
3429         catch (Exception ex) {\r
3430         }\r
3431         \r
3432         ccbs.clear();\r
3433         ccus.clear();\r
3434         \r
3435         //test ascii overflow buffer\r
3436         ccus.put((char)0x0A); ccus.put((char)0x0043);\r
3437         ccbs.put((byte)0x00); ccbs.put((byte)0x00);\r
3438         \r
3439         ccus.limit(ccus.position());\r
3440         ccus.position(0);\r
3441         ccbs.limit(ccbs.position());\r
3442         ccbs.position(0);\r
3443            \r
3444         try {\r
3445             smBufEncode(encoder, "ISCII-CC-EN-2", ccus, ccbs, true, false);\r
3446             errln("Exception while encoding ISCII should have been thrown.");\r
3447         }\r
3448         catch (Exception ex) {\r
3449         }\r
3450         \r
3451         ccbs.clear();\r
3452         ccus.clear();\r
3453         \r
3454         //test surrogate malform\r
3455         ccus.put((char)0x06E3);\r
3456         ccbs.put((byte)0x00);\r
3457         \r
3458         ccus.limit(ccus.position());\r
3459         ccus.position(0);\r
3460         ccbs.limit(ccbs.position());\r
3461         ccbs.position(0);\r
3462            \r
3463         try {\r
3464             smBufEncode(encoder, "ISCII-CC-EN-3", ccus, ccbs, true, false);\r
3465             errln("Exception while encoding ISCII should have been thrown.");\r
3466         }\r
3467         catch (Exception ex) {\r
3468         }\r
3469         \r
3470         ccbs.clear();\r
3471         ccus.clear();\r
3472         \r
3473         //test surrogate malform\r
3474         ccus.put((char)0xD801); ccus.put((char)0xDD01);\r
3475         ccbs.put((byte)0x00);\r
3476         \r
3477         ccus.limit(ccus.position());\r
3478         ccus.position(0);\r
3479         ccbs.limit(ccbs.position());\r
3480         ccbs.position(0);\r
3481            \r
3482         try {\r
3483             smBufEncode(encoder, "ISCII-CC-EN-4", ccus, ccbs, true, false);\r
3484             errln("Exception while encoding ISCII should have been thrown.");\r
3485         }\r
3486         catch (Exception ex) {\r
3487         }\r
3488         \r
3489         ccbs.clear();\r
3490         ccus.clear();\r
3491         \r
3492         //test trail surrogate malform\r
3493         ccus.put((char)0xDD01); \r
3494         ccbs.put((byte)0x00);\r
3495         \r
3496         ccus.limit(ccus.position());\r
3497         ccus.position(0);\r
3498         ccbs.limit(ccbs.position());\r
3499         ccbs.position(0);\r
3500            \r
3501         try {\r
3502             smBufEncode(encoder, "ISCII-CC-EN-5", ccus, ccbs, true, false);\r
3503             errln("Exception while encoding ISCII should have been thrown.");\r
3504         }\r
3505         catch (Exception ex) {\r
3506         }\r
3507         \r
3508         ccbs.clear();\r
3509         ccus.clear();\r
3510         \r
3511         //test lead surrogates malform\r
3512         ccus.put((char)0xD801); ccus.put((char)0xD802); \r
3513         ccbs.put((byte)0x00);\r
3514         \r
3515         ccus.limit(ccus.position());\r
3516         ccus.position(0);\r
3517         ccbs.limit(ccbs.position());\r
3518         ccbs.position(0);\r
3519            \r
3520         try {\r
3521             smBufEncode(encoder, "ISCII-CC-EN-6", ccus, ccbs, true, false);\r
3522             errln("Exception while encoding ISCII should have been thrown.");\r
3523         }\r
3524         catch (Exception ex) {\r
3525         }\r
3526         \r
3527         ccus.clear();\r
3528         ccbs.clear();\r
3529         \r
3530         //test overflow buffer\r
3531         ccus.put((char)0x0901); \r
3532         ccbs.put((byte)0x00);\r
3533         \r
3534         ccus.limit(ccus.position());\r
3535         ccus.position(0);\r
3536         ccbs.limit(ccbs.position());\r
3537         ccbs.position(0);\r
3538            \r
3539         cs = provider.charsetForName("ISCII,version=0");\r
3540         encoder = cs.newEncoder();\r
3541         \r
3542         try {\r
3543             smBufEncode(encoder, "ISCII-CC-EN-7", ccus, ccbs, true, false);\r
3544             errln("Exception while encoding ISCII should have been thrown.");\r
3545         }\r
3546         catch (Exception ex) {\r
3547         }\r
3548         //end of charset encoder code coverage code\r
3549     }\r
3550     \r
3551     //Test for the IMAP Charset\r
3552     public void TestCharsetIMAP() {\r
3553         CharsetProvider provider = new CharsetProviderICU();\r
3554         Charset cs = provider.charsetForName("IMAP-mailbox-name");        \r
3555         CharsetEncoder encoder = cs.newEncoder();\r
3556         CharsetDecoder decoder = cs.newDecoder();\r
3557         \r
3558         CharBuffer us = CharBuffer.allocate(0x20);\r
3559         ByteBuffer bs = ByteBuffer.allocate(0x20);\r
3560         \r
3561         us.put((char)0x00A3); us.put((char)0x2020); us.put((char)0x41);\r
3562         \r
3563         bs.put((byte)0x26); bs.put((byte)0x41); bs.put((byte)0x4B); bs.put((byte)0x4D); bs.put((byte)0x67); bs.put((byte)0x49);\r
3564         bs.put((byte)0x41); bs.put((byte)0x2D); bs.put((byte)0x41);\r
3565         \r
3566         \r
3567         bs.limit(bs.position());\r
3568         bs.position(0);\r
3569         us.limit(us.position());\r
3570         us.position(0);\r
3571 \r
3572         smBufDecode(decoder, "IMAP", bs, us);\r
3573         smBufEncode(encoder, "IMAP", us, bs);\r
3574         \r
3575         //the rest of the code in this method is for better code coverage\r
3576         us.clear();\r
3577         bs.clear();\r
3578         \r
3579         //start of charset encoder code coverage\r
3580         //test buffer overflow\r
3581         us.put((char)0x0026); us.put((char)0x17A9); \r
3582         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
3583         \r
3584         bs.limit(bs.position());\r
3585         bs.position(0);\r
3586         us.limit(us.position());\r
3587         us.position(0);\r
3588         \r
3589         try {\r
3590             smBufEncode(encoder, "IMAP-EN-1", us, bs, true, false);\r
3591             errln("Exception while encoding IMAP (1) should have been thrown.");\r
3592         } catch(Exception ex) {\r
3593         }\r
3594         \r
3595         us.clear();\r
3596         bs.clear();\r
3597         \r
3598         //test buffer overflow\r
3599         us.put((char)0x17A9); us.put((char)0x0941);\r
3600         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
3601         \r
3602         bs.limit(bs.position());\r
3603         bs.position(0);\r
3604         us.limit(us.position());\r
3605         us.position(0);\r
3606         \r
3607         try {\r
3608             smBufEncode(encoder, "IMAP-EN-2", us, bs, true, false);\r
3609             errln("Exception while encoding IMAP (2) should have been thrown.");\r
3610         } catch(Exception ex) {\r
3611         }\r
3612         \r
3613         us.clear();\r
3614         bs.clear();\r
3615         \r
3616         //test buffer overflow\r
3617         us.put((char)0x17A9); us.put((char)0x0941);\r
3618         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);   \r
3619         \r
3620         bs.limit(bs.position());\r
3621         bs.position(0);\r
3622         us.limit(us.position());\r
3623         us.position(0);\r
3624         \r
3625         try {\r
3626             smBufEncode(encoder, "IMAP-EN-3", us, bs, true, false);\r
3627             errln("Exception while encoding IMAP (3) should have been thrown.");\r
3628         } catch(Exception ex) {\r
3629         }\r
3630         \r
3631         us.clear();\r
3632         bs.clear();\r
3633         \r
3634         //test buffer overflow\r
3635         us.put((char)0x17A9); us.put((char)0x0941); us.put((char)0x0955);\r
3636         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);      \r
3637         bs.put((byte)0x00);\r
3638         \r
3639         bs.limit(bs.position());\r
3640         bs.position(0);\r
3641         us.limit(us.position());\r
3642         us.position(0);\r
3643         \r
3644         try {\r
3645             smBufEncode(encoder, "IMAP-EN-4", us, bs, true, false);\r
3646             errln("Exception while encoding IMAP (4) should have been thrown.");\r
3647         } catch(Exception ex) {\r
3648         }\r
3649         \r
3650         us.clear();\r
3651         bs.clear();\r
3652         \r
3653         //test buffer overflow\r
3654         us.put((char)0x17A9); us.put((char)0x0941); us.put((char)0x0955);\r
3655         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);  \r
3656         bs.put((byte)0x00); bs.put((byte)0x00); \r
3657         \r
3658         bs.limit(bs.position());\r
3659         bs.position(0);\r
3660         us.limit(us.position());\r
3661         us.position(0);\r
3662         \r
3663         try {\r
3664             smBufEncode(encoder, "IMAP-EN-5", us, bs, true, false);\r
3665             errln("Exception while encoding IMAP (5) should have been thrown.");\r
3666         } catch(Exception ex) {\r
3667         }\r
3668         \r
3669         us.clear();\r
3670         bs.clear();\r
3671         \r
3672         //test buffer overflow\r
3673         us.put((char)0x17A9); us.put((char)0x0941); us.put((char)0x0955); us.put((char)0x0970);\r
3674         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);  \r
3675         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
3676         \r
3677         bs.limit(bs.position());\r
3678         bs.position(0);\r
3679         us.limit(us.position());\r
3680         us.position(0);\r
3681         \r
3682         try {\r
3683             smBufEncode(encoder, "IMAP-EN-6", us, bs, true, false);\r
3684             errln("Exception while encoding IMAP (6) should have been thrown.");\r
3685         } catch(Exception ex) {\r
3686         }\r
3687         \r
3688         us.clear();\r
3689         bs.clear();\r
3690         \r
3691         //test buffer overflow\r
3692         us.put((char)0x17A9); us.put((char)0x0941);\r
3693         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);  bs.put((byte)0x00); bs.put((byte)0x00);\r
3694         bs.put((byte)0x00); \r
3695         \r
3696         bs.limit(bs.position());\r
3697         bs.position(0);\r
3698         us.limit(us.position());\r
3699         us.position(0);\r
3700         \r
3701         try {\r
3702             smBufEncode(encoder, "IMAP-EN-7", us, bs, true, true);\r
3703             errln("Exception while encoding IMAP (7) should have been thrown.");\r
3704         } catch(Exception ex) {\r
3705         }\r
3706         \r
3707         us.clear();\r
3708         bs.clear();\r
3709         \r
3710         //test flushing\r
3711         us.put((char)0x17A9); us.put((char)0x0941); \r
3712         bs.put((byte)0x26); bs.put((byte)0x46); bs.put((byte)0x36); bs.put((byte)0x6b);  bs.put((byte)0x4a); bs.put((byte)0x51);\r
3713         bs.put((byte)0x51); bs.put((byte)0x2d);\r
3714         \r
3715         bs.limit(bs.position());\r
3716         bs.position(0);\r
3717         us.limit(us.position());\r
3718         us.position(0);\r
3719         \r
3720         try {\r
3721             smBufEncode(encoder, "IMAP-EN-8", us, bs, true, true);\r
3722         } catch(Exception ex) {\r
3723             errln("Exception while encoding IMAP (8) should not have been thrown.");\r
3724         }\r
3725         \r
3726         us = CharBuffer.allocate(0x08);\r
3727         bs = ByteBuffer.allocate(0x08);\r
3728         \r
3729         //test flushing buffer overflow\r
3730         us.put((char)0x0061);\r
3731         bs.put((byte)0x61); bs.put((byte)0x00);\r
3732         \r
3733         bs.limit(bs.position());\r
3734         bs.position(0);\r
3735         us.limit(us.position());\r
3736         us.position(0);\r
3737         \r
3738         try {\r
3739             smBufEncode(encoder, "IMAP-EN-9", us, bs, true, true);\r
3740         } catch(Exception ex) {\r
3741             errln("Exception while encoding IMAP (9) should not have been thrown.");\r
3742         }\r
3743         //end of charset encoder code coverage\r
3744         \r
3745         us = CharBuffer.allocate(0x10);\r
3746         bs = ByteBuffer.allocate(0x10);\r
3747         \r
3748         //start of charset decoder code coverage\r
3749         //test malform case 2\r
3750         us.put((char)0x0000); us.put((char)0x0000); \r
3751         bs.put((byte)0x26); bs.put((byte)0x41); bs.put((byte)0x43); bs.put((byte)0x41);  \r
3752         \r
3753         bs.limit(bs.position());\r
3754         bs.position(0);\r
3755         us.limit(us.position());\r
3756         us.position(0);\r
3757         \r
3758         try {\r
3759             smBufDecode(decoder, "IMAP-DE-1", bs, us, true, false);\r
3760             errln("Exception while decoding IMAP (1) should have been thrown.");\r
3761         } catch(Exception ex) {\r
3762         }\r
3763         \r
3764         us.clear();\r
3765         bs.clear();\r
3766         \r
3767         //test malform case 5\r
3768         us.put((char)0x0000); us.put((char)0x0000); us.put((char)0x0000);\r
3769         bs.put((byte)0x26); bs.put((byte)0x41); bs.put((byte)0x41); bs.put((byte)0x41); \r
3770         bs.put((byte)0x41); bs.put((byte)0x49); bs.put((byte)0x41);  \r
3771         \r
3772         bs.limit(bs.position());\r
3773         bs.position(0);\r
3774         us.limit(us.position());\r
3775         us.position(0);\r
3776         \r
3777         try {\r
3778             smBufDecode(decoder, "IMAP-DE-2", bs, us, true, false);\r
3779             errln("Exception while decoding IMAP (2) should have been thrown.");\r
3780         } catch(Exception ex) {\r
3781         }\r
3782         \r
3783         us.clear();\r
3784         bs.clear();\r
3785         \r
3786         //test malform case 7\r
3787         us.put((char)0x0000); us.put((char)0x0000); us.put((char)0x0000); us.put((char)0x0000);\r
3788         bs.put((byte)0x26); bs.put((byte)0x41); bs.put((byte)0x41); bs.put((byte)0x41); \r
3789         bs.put((byte)0x41); bs.put((byte)0x41); bs.put((byte)0x41); bs.put((byte)0x42); \r
3790         bs.put((byte)0x41);  \r
3791         \r
3792         bs.limit(bs.position());\r
3793         bs.position(0);\r
3794         us.limit(us.position());\r
3795         us.position(0);\r
3796         \r
3797         try {\r
3798             smBufDecode(decoder, "IMAP-DE-3", bs, us, true, false);\r
3799             errln("Exception while decoding IMAP (3) should have been thrown.");\r
3800         } catch(Exception ex) {\r
3801         }\r
3802         //end of charset decoder coder coverage  \r
3803     }\r
3804     \r
3805     //Test for charset UTF32LE to provide better code coverage\r
3806     public void TestCharsetUTF32LE() {\r
3807         CoderResult result = CoderResult.UNDERFLOW;\r
3808         CharsetProvider provider = new CharsetProviderICU();\r
3809         Charset cs = provider.charsetForName("UTF-32LE");        \r
3810         CharsetEncoder encoder = cs.newEncoder();\r
3811         //CharsetDecoder decoder = cs.newDecoder();\r
3812         \r
3813         CharBuffer us = CharBuffer.allocate(0x10);\r
3814         ByteBuffer bs = ByteBuffer.allocate(0x10);\r
3815         \r
3816         \r
3817         //test malform surrogate\r
3818         us.put((char)0xD901);\r
3819         bs.put((byte)0x00);\r
3820         \r
3821         bs.limit(bs.position());\r
3822         bs.position(0);\r
3823         us.limit(us.position());\r
3824         us.position(0);\r
3825         \r
3826         try {\r
3827             smBufEncode(encoder, "UTF32LE-EN-1", us, bs, true, false);\r
3828             errln("Exception while encoding UTF32LE (1) should have been thrown.");\r
3829         } catch (Exception ex) {\r
3830         }\r
3831         \r
3832         bs.clear();\r
3833         us.clear();\r
3834         \r
3835         //test malform surrogate\r
3836         us.put((char)0xD901); us.put((char)0xD902);\r
3837         bs.put((byte)0x00);\r
3838         \r
3839         bs.limit(bs.position());\r
3840         bs.position(0);\r
3841         us.limit(us.position());\r
3842         us.position(0);\r
3843         \r
3844         result = encoder.encode(us, bs, true);\r
3845         \r
3846         if (!result.isError() && !result.isOverflow()) {\r
3847             errln("Error while encoding UTF32LE (2) should have occurred.");\r
3848         }\r
3849         \r
3850         bs.clear();\r
3851         us.clear();\r
3852         \r
3853         //test overflow trail surrogate\r
3854         us.put((char)0xDD01); us.put((char)0xDD0E); us.put((char)0xDD0E);\r
3855         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
3856         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); \r
3857         \r
3858         bs.limit(bs.position());\r
3859         bs.position(0);\r
3860         us.limit(us.position());\r
3861         us.position(0);\r
3862         \r
3863         result = encoder.encode(us, bs, true);\r
3864         \r
3865         if (!result.isError() && !result.isOverflow()) {\r
3866             errln("Error while encoding UTF32LE (3) should have occurred.");\r
3867         }\r
3868         \r
3869         bs.clear();\r
3870         us.clear();\r
3871         \r
3872         //test malform lead surrogate\r
3873         us.put((char)0xD90D); us.put((char)0xD90E);\r
3874         bs.put((byte)0x00); \r
3875         \r
3876         bs.limit(bs.position());\r
3877         bs.position(0);\r
3878         us.limit(us.position());\r
3879         us.position(0);\r
3880         \r
3881         try {\r
3882             smBufEncode(encoder, "UTF32LE-EN-4", us, bs, true, false);\r
3883             errln("Exception while encoding UTF32LE (4) should have been thrown.");\r
3884         } catch (Exception ex) {\r
3885         }\r
3886         \r
3887         bs.clear();\r
3888         us.clear();\r
3889         \r
3890         //test overflow buffer\r
3891         us.put((char)0x0061);\r
3892         bs.put((byte)0x00); \r
3893         \r
3894         bs.limit(bs.position());\r
3895         bs.position(0);\r
3896         us.limit(us.position());\r
3897         us.position(0);\r
3898         \r
3899         try {\r
3900             smBufEncode(encoder, "UTF32LE-EN-5", us, bs, true, false);\r
3901             errln("Exception while encoding UTF32LE (5) should have been thrown.");\r
3902         } catch (Exception ex) {\r
3903         }\r
3904         \r
3905         bs.clear();\r
3906         us.clear();\r
3907         \r
3908         //test malform trail surrogate\r
3909         us.put((char)0xDD01);\r
3910         bs.put((byte)0x00); \r
3911         \r
3912         bs.limit(bs.position());\r
3913         bs.position(0);\r
3914         us.limit(us.position());\r
3915         us.position(0);\r
3916         \r
3917         try {\r
3918             smBufEncode(encoder, "UTF32LE-EN-6", us, bs, true, false);\r
3919             errln("Exception while encoding UTF32LE (6) should have been thrown.");\r
3920         } catch (Exception ex) {\r
3921         }\r
3922     }\r
3923 \r
3924     //Test for charset UTF16LE to provide better code coverage\r
3925     public void TestCharsetUTF16LE() {\r
3926         CoderResult result = CoderResult.UNDERFLOW;\r
3927         CharsetProvider provider = new CharsetProviderICU();\r
3928         Charset cs = provider.charsetForName("UTF-16LE");        \r
3929         CharsetEncoder encoder = cs.newEncoder();\r
3930         //CharsetDecoder decoder = cs.newDecoder();\r
3931         \r
3932         // Test for malform and change fromUChar32 for next call\r
3933         char u_pts1[] = {\r
3934                 (char)0xD805, \r
3935                 (char)0xDC01, (char)0xDC02, (char)0xDC03,\r
3936                 (char)0xD901, (char)0xD902\r
3937                 };\r
3938         byte b_pts1[] = {\r
3939                 (byte)0x00, \r
3940                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00\r
3941                 };\r
3942         \r
3943         CharBuffer us = CharBuffer.allocate(u_pts1.length);\r
3944         ByteBuffer bs = ByteBuffer.allocate(b_pts1.length);\r
3945         \r
3946         us.put(u_pts1);\r
3947         bs.put(b_pts1);\r
3948         \r
3949         us.limit(1);\r
3950         us.position(0);\r
3951         bs.limit(1);\r
3952         bs.position(0);\r
3953        \r
3954         result = encoder.encode(us, bs, true);\r
3955         \r
3956         if (!result.isMalformed()) {\r
3957             // LE should not output BOM, so this should be malformed \r
3958             errln("Malformed while encoding UTF-16LE (1) should have occured.");\r
3959         }\r
3960         \r
3961         // Test for malform surrogate from previous buffer\r
3962         us.limit(4);\r
3963         us.position(1);\r
3964         bs.limit(7);\r
3965         bs.position(1);\r
3966         \r
3967         result = encoder.encode(us, bs, true);\r
3968         \r
3969         if (!result.isMalformed()) {\r
3970             errln("Error while encoding UTF-16LE (2) should have occured.");\r
3971         }       \r
3972         \r
3973         // Test for malform trail surrogate\r
3974         encoder.reset();\r
3975         \r
3976         us.limit(1);\r
3977         us.position(0);\r
3978         bs.limit(1);\r
3979         bs.position(0);\r
3980        \r
3981         result = encoder.encode(us, bs, true);    \r
3982         \r
3983         us.limit(6);\r
3984         us.position(4);\r
3985         bs.limit(4);\r
3986         bs.position(1);\r
3987         \r
3988         result = encoder.encode(us, bs, true);\r
3989         \r
3990         if (!result.isMalformed()) {\r
3991             errln("Error while encoding UTF-16LE (3) should have occured.");\r
3992         }          \r
3993     }\r
3994     \r
3995     //provide better code coverage for the generic charset UTF32\r
3996     public void TestCharsetUTF32() {\r
3997         CoderResult result = CoderResult.UNDERFLOW;\r
3998         CharsetProvider provider = new CharsetProviderICU();\r
3999         Charset cs = provider.charsetForName("UTF-32");        \r
4000         CharsetDecoder decoder = cs.newDecoder();\r
4001         CharsetEncoder encoder = cs.newEncoder();\r
4002         \r
4003         //start of decoding code coverage\r
4004         char us_array[] = {\r
4005                 0x0000, 0x0000, 0x0000, 0x0000,\r
4006             };\r
4007         \r
4008         byte bs_array1[] = {\r
4009                 (byte)0x00, (byte)0x00, (byte)0xFE, (byte)0xFF,\r
4010                 (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x43,\r
4011                 (byte)0xFF, (byte)0xFE, (byte)0x00, (byte)0x00,\r
4012                 (byte)0x43, (byte)0x04, (byte)0x00, (byte)0x00,\r
4013             };\r
4014         \r
4015         byte bs_array2[] = {\r
4016                 (byte)0xFF, (byte)0xFE, (byte)0x00, (byte)0x00,\r
4017                 (byte)0x43, (byte)0x04, (byte)0x00, (byte)0x00,\r
4018             };\r
4019         \r
4020         CharBuffer us = CharBuffer.allocate(us_array.length);\r
4021         ByteBuffer bs = ByteBuffer.allocate(bs_array1.length);\r
4022         \r
4023         us.put(us_array);\r
4024         bs.put(bs_array1);\r
4025         \r
4026         us.limit(us.position());\r
4027         us.position(0);\r
4028         bs.limit(bs.position());\r
4029         bs.position(0);\r
4030             \r
4031         try {\r
4032             smBufDecode(decoder, "UTF32-DE-1", bs, us, true, false);\r
4033             errln("Malform exception while decoding UTF32 charset (1) should have been thrown.");\r
4034         } catch (Exception ex) {\r
4035         }\r
4036         \r
4037         decoder = cs.newDecoder();\r
4038         \r
4039         bs = ByteBuffer.allocate(bs_array2.length);\r
4040         bs.put(bs_array2);\r
4041         \r
4042         us.limit(4);\r
4043         us.position(0);\r
4044         bs.limit(bs.position());\r
4045         bs.position(0);\r
4046             \r
4047         try {\r
4048             smBufDecode(decoder, "UTF32-DE-2", bs, us, true, false);\r
4049         } catch (Exception ex) {\r
4050             // should recognize little endian BOM\r
4051             errln("Exception while decoding UTF32 charset (2) should not have been thrown.");\r
4052         }\r
4053         \r
4054         //Test malform exception\r
4055         bs.clear();\r
4056         us.clear();\r
4057         \r
4058         bs.put((byte)0x00); bs.put((byte)0xFE); bs.put((byte)0xFF); bs.put((byte)0x00); bs.put((byte)0x00);\r
4059         us.put((char)0x0000);\r
4060         \r
4061         us.limit(us.position());\r
4062         us.position(0);\r
4063         bs.limit(bs.position());\r
4064         bs.position(0);\r
4065         \r
4066         try {\r
4067             smBufDecode(decoder, "UTF32-DE-3", bs, us, true, false);\r
4068             errln("Malform exception while decoding UTF32 charset (3) should have been thrown.");\r
4069         } catch (Exception ex) {\r
4070         }\r
4071         \r
4072         //Test BOM testing\r
4073         bs.clear();\r
4074         us.clear();\r
4075         \r
4076         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0xFF); bs.put((byte)0xFE); \r
4077         us.put((char)0x0000);\r
4078         \r
4079         us.limit(us.position());\r
4080         us.position(0);\r
4081         bs.limit(bs.position());\r
4082         bs.position(0);\r
4083         \r
4084         try {\r
4085             smBufDecode(decoder, "UTF32-DE-4", bs, us, true, false);\r
4086         } catch (Exception ex) {\r
4087             // should recognize big endian BOM\r
4088             errln("Exception while decoding UTF32 charset (4) should not have been thrown.");\r
4089         }\r
4090         //end of decoding code coverage\r
4091         \r
4092         //start of encoding code coverage\r
4093         us = CharBuffer.allocate(0x10);\r
4094         bs = ByteBuffer.allocate(0x10);\r
4095         \r
4096         //test wite BOM overflow error\r
4097         us.put((char)0xDC01);\r
4098         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4099         \r
4100         us.limit(us.position());\r
4101         us.position(0);\r
4102         bs.limit(bs.position());\r
4103         bs.position(0);\r
4104         \r
4105         result = encoder.encode(us, bs, true);\r
4106         // must try to output BOM first for UTF-32 (not UTF-32BE or UTF-32LE)\r
4107         if (!result.isOverflow()) {\r
4108             errln("Buffer overflow error while encoding UTF32 charset (1) should have occurred."); \r
4109         }\r
4110         \r
4111         us.clear();\r
4112         bs.clear();\r
4113         \r
4114         //test malform surrogate and store value in fromChar32\r
4115         us.put((char)0xD801); us.put((char)0xD802);\r
4116         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4117         \r
4118         us.limit(us.position());\r
4119         us.position(0);\r
4120         bs.limit(bs.position());\r
4121         bs.position(0);\r
4122         \r
4123         result = encoder.encode(us, bs, true);\r
4124         if (!result.isMalformed()) {\r
4125             errln("Malformed error while encoding UTF32 charset (2) should have occurred.");\r
4126         }    \r
4127         \r
4128         us.clear();\r
4129         bs.clear();\r
4130         \r
4131         //test malform surrogate\r
4132         us.put((char)0x0000); us.put((char)0xD902);\r
4133         \r
4134         us.limit(us.position());\r
4135         us.position(0);\r
4136         bs.limit(bs.position());\r
4137         bs.position(0);\r
4138         \r
4139         result = encoder.encode(us, bs, true);\r
4140         if (!result.isOverflow()) {\r
4141             errln("Overflow error while encoding UTF32 charset (3) should have occurred.");\r
4142         } \r
4143         \r
4144         us.clear();\r
4145         bs.clear();\r
4146         \r
4147         //test malform surrogate\r
4148         encoder.reset();\r
4149         us.put((char)0xD801);\r
4150         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4151    \r
4152         us.limit(us.position());\r
4153         us.position(0);\r
4154         bs.limit(bs.position());\r
4155         bs.position(0);\r
4156         \r
4157         result = encoder.encode(us, bs, true);\r
4158         if (!result.isMalformed()) {\r
4159             errln("Malform error while encoding UTF32 charset (4) should have occurred.");\r
4160         } \r
4161         \r
4162         us.clear();\r
4163         bs.clear();\r
4164         \r
4165         //test overflow surrogate\r
4166         us.put((char)0x0000); us.put((char)0xDDE1); us.put((char)0xD915); us.put((char)0xDDF2);\r
4167         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); \r
4168    \r
4169         us.limit(us.position());\r
4170         us.position(0);\r
4171         bs.limit(bs.position());\r
4172         bs.position(0);\r
4173         \r
4174         result = encoder.encode(us, bs, true);\r
4175         if (!result.isOverflow()) {\r
4176             errln("Overflow error while encoding UTF32 charset (5) should have occurred.");\r
4177         } \r
4178         \r
4179         us.clear();\r
4180         bs.clear();\r
4181         \r
4182         //test malform surrogate\r
4183         encoder.reset();\r
4184         us.put((char)0xDDE1);\r
4185         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4186    \r
4187         us.limit(us.position());\r
4188         us.position(0);\r
4189         bs.limit(bs.position());\r
4190         bs.position(0);\r
4191         \r
4192         result = encoder.encode(us, bs, true);\r
4193         if (!result.isMalformed()) {\r
4194             errln("Malform error while encoding UTF32 charset (6) should have occurred.");\r
4195         } \r
4196         //end of encoding code coverage\r
4197     }\r
4198     \r
4199     //this method provides better code coverage decoding UTF32 LE/BE\r
4200     public void TestDecodeUTF32LEBE() {\r
4201         CoderResult result = CoderResult.UNDERFLOW;\r
4202         CharsetProvider provider = new CharsetProviderICU();       \r
4203         CharsetDecoder decoder;\r
4204         CharBuffer us = CharBuffer.allocate(0x10);\r
4205         ByteBuffer bs = ByteBuffer.allocate(0x10);\r
4206         \r
4207         //decode UTF32LE\r
4208         decoder = provider.charsetForName("UTF-32LE").newDecoder();\r
4209         //test overflow buffer\r
4210         bs.put((byte)0x41); bs.put((byte)0xFF); bs.put((byte)0x01); bs.put((byte)0x00);\r
4211         us.put((char)0x0000);\r
4212         \r
4213         us.limit(us.position());\r
4214         us.position(0);\r
4215         bs.limit(bs.position());\r
4216         bs.position(0);\r
4217         \r
4218         try {\r
4219             smBufDecode(decoder, "UTF-32LE", bs, us, true, false);\r
4220             errln("Overflow exception while decoding UTF32LE (1) should have been thrown.");\r
4221         } catch (Exception ex) {\r
4222         }\r
4223         // test overflow buffer handling in CharsetDecoderICU\r
4224         bs.position(0);\r
4225         us.position(0);\r
4226         decoder.reset();\r
4227         result = decoder.decode(bs, us, true);\r
4228         if (result.isOverflow()) {\r
4229             result = decoder.decode(bs, us, true);\r
4230             if (!result.isOverflow()) {\r
4231                 errln("Overflow buffer error while decoding UTF32LE should have occurred.");\r
4232             }\r
4233         } else {\r
4234             errln("Overflow buffer error while decoding UTF32LE should have occurred.");\r
4235         }\r
4236         \r
4237         us.clear();\r
4238         bs.clear();\r
4239         //test malform buffer\r
4240         bs.put((byte)0x02); bs.put((byte)0xD9); bs.put((byte)0x00); bs.put((byte)0x00);\r
4241         us.put((char)0x0000);\r
4242         \r
4243         us.limit(us.position());\r
4244         us.position(0);\r
4245         bs.limit(bs.position());\r
4246         bs.position(0);\r
4247         \r
4248         try {\r
4249             smBufDecode(decoder, "UTF-32LE", bs, us, true, false);\r
4250             errln("Malform exception while decoding UTF32LE (2) should have been thrown.");\r
4251         } catch (Exception ex) {\r
4252         }\r
4253         \r
4254         us.clear();\r
4255         bs.clear();\r
4256         //test malform buffer\r
4257         bs.put((byte)0xFF); bs.put((byte)0xFE); bs.put((byte)0x00); bs.put((byte)0x00);\r
4258         bs.put((byte)0xFF); bs.put((byte)0xDF); bs.put((byte)0x10); \r
4259         us.put((char)0x0000);\r
4260         \r
4261         us.limit(us.position());\r
4262         us.position(0);\r
4263         bs.limit(bs.position());\r
4264         bs.position(0);\r
4265         \r
4266         try {\r
4267             // must flush in order to exhibit malformed behavior\r
4268             smBufDecode(decoder, "UTF-32LE", bs, us, true, true);\r
4269             errln("Malform exception while decoding UTF32LE (3) should have been thrown.");\r
4270         } catch (Exception ex) {\r
4271         }\r
4272         \r
4273         us.clear();\r
4274         bs.clear();\r
4275         //test malform buffer\r
4276         bs.put((byte)0xFF); bs.put((byte)0xFE); bs.put((byte)0x00); bs.put((byte)0x00);\r
4277         bs.put((byte)0x02); bs.put((byte)0xD9); bs.put((byte)0x00); bs.put((byte)0x00);\r
4278         us.put((char)0x0000);\r
4279         \r
4280         us.limit(us.position());\r
4281         us.position(0);\r
4282         bs.limit(bs.position());\r
4283         bs.position(0);\r
4284         \r
4285         try {\r
4286             smBufDecode(decoder, "UTF-32LE", bs, us, true, false);\r
4287             errln("Malform exception while decoding UTF32LE (4) should have been thrown.");\r
4288         } catch (Exception ex) {\r
4289         }\r
4290         \r
4291         us.clear();\r
4292         bs.clear();\r
4293         //test overflow buffer\r
4294         bs.put((byte)0xFF); bs.put((byte)0xFE); bs.put((byte)0x00); bs.put((byte)0x00);\r
4295         bs.put((byte)0xDD); bs.put((byte)0xFF); bs.put((byte)0x10); bs.put((byte)0x00);\r
4296         us.put((char)0x0000);\r
4297         \r
4298         us.limit(us.position());\r
4299         us.position(0);\r
4300         bs.limit(bs.position());\r
4301         bs.position(0);\r
4302         \r
4303         try {\r
4304             smBufDecode(decoder, "UTF-32LE", bs, us, true, false);\r
4305             errln("Overflow exception while decoding UTF32LE (5) should have been thrown.");\r
4306         } catch (Exception ex) {\r
4307         }\r
4308         //end of decode UTF32LE\r
4309         \r
4310         bs.clear();\r
4311         us.clear();\r
4312         \r
4313         //decode UTF32BE\r
4314         decoder = provider.charsetForName("UTF-32BE").newDecoder();\r
4315         //test overflow buffer\r
4316         bs.put((byte)0x00); bs.put((byte)0x01); bs.put((byte)0xFF); bs.put((byte)0x41);\r
4317         us.put((char)0x0000);\r
4318         \r
4319         us.limit(us.position());\r
4320         us.position(0);\r
4321         bs.limit(bs.position());\r
4322         bs.position(0);\r
4323         \r
4324         try {\r
4325             smBufDecode(decoder, "UTF-32BE", bs, us, true, false);\r
4326             errln("Overflow exception while decoding UTF32BE (1) should have been thrown.");\r
4327         } catch (Exception ex) {\r
4328         }\r
4329         \r
4330         bs.clear();\r
4331         us.clear();\r
4332         //test malform buffer\r
4333         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0xD9); bs.put((byte)0x02);\r
4334         us.put((char)0x0000);\r
4335         \r
4336         us.limit(us.position());\r
4337         us.position(0);\r
4338         bs.limit(bs.position());\r
4339         bs.position(0);\r
4340         \r
4341         try {\r
4342             smBufDecode(decoder, "UTF-32BE", bs, us, true, false);\r
4343             errln("Malform exception while decoding UTF32BE (2) should have been thrown.");\r
4344         } catch (Exception ex) {\r
4345         }\r
4346         \r
4347         bs.clear();\r
4348         us.clear();\r
4349         //test malform buffer\r
4350         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0xFE); bs.put((byte)0xFF);\r
4351         bs.put((byte)0x10); bs.put((byte)0xFF); bs.put((byte)0xDF);\r
4352         us.put((char)0x0000);\r
4353         \r
4354         us.limit(us.position());\r
4355         us.position(0);\r
4356         bs.limit(bs.position());\r
4357         bs.position(0);\r
4358         \r
4359         try {\r
4360             // must flush to exhibit malformed behavior\r
4361             smBufDecode(decoder, "UTF-32BE", bs, us, true, true);\r
4362             errln("Malform exception while decoding UTF32BE (3) should have been thrown.");\r
4363         } catch (Exception ex) {\r
4364         }\r
4365         \r
4366         bs.clear();\r
4367         us.clear();\r
4368         //test overflow buffer\r
4369         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0xFE); bs.put((byte)0xFF);\r
4370         bs.put((byte)0x00); bs.put((byte)0x10); bs.put((byte)0xFF); bs.put((byte)0xDD);\r
4371         us.put((char)0x0000);\r
4372         \r
4373         us.limit(us.position());\r
4374         us.position(0);\r
4375         bs.limit(bs.position());\r
4376         bs.position(0);\r
4377         \r
4378         try {\r
4379             smBufDecode(decoder, "UTF-32BE", bs, us, true, false);\r
4380             errln("Overflow exception while decoding UTF32BE (4) should have been thrown.");\r
4381         } catch (Exception ex) {\r
4382         }\r
4383         \r
4384         bs.clear();\r
4385         us.clear();\r
4386         //test malform buffer\r
4387         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0xFE); \r
4388         us.put((char)0x0000);\r
4389         \r
4390         us.limit(us.position());\r
4391         us.position(0);\r
4392         bs.limit(bs.position());\r
4393         bs.position(0);\r
4394         \r
4395         try {\r
4396             // must flush to exhibit malformed behavior\r
4397             smBufDecode(decoder, "UTF-32BE", bs, us, true, true);\r
4398             errln("Malform exception while decoding UTF32BE (5) should have been thrown.");\r
4399         } catch (Exception ex) {\r
4400         }\r
4401         //end of decode UTF32BE\r
4402     }\r
4403     \r
4404     //provide better code coverage for UTF8\r
4405     public void TestCharsetUTF8() {\r
4406         CoderResult result = CoderResult.UNDERFLOW;\r
4407         CharsetProvider provider = new CharsetProviderICU();       \r
4408         CharsetDecoder decoder = provider.charsetForName("UTF-8").newDecoder();\r
4409         CharsetEncoder encoder = provider.charsetForName("UTF-8").newEncoder();\r
4410         \r
4411         CharBuffer us = CharBuffer.allocate(0x10);\r
4412         ByteBuffer bs = ByteBuffer.allocate(0x10);\r
4413         ByteBuffer bs2;\r
4414         CharBuffer us2;\r
4415         int limit_us;\r
4416         int limit_bs;\r
4417         \r
4418         //encode and decode using read only buffer\r
4419         encoder.reset();\r
4420         decoder.reset();\r
4421         us.put((char)0x0041); us.put((char)0x0081); us.put((char)0xEF65); us.put((char)0xD902);\r
4422         bs.put((byte)0x41); bs.put((byte)0xc2); bs.put((byte)0x81); bs.put((byte)0xee); bs.put((byte)0xbd); bs.put((byte)0xa5);\r
4423         bs.put((byte)0x00); \r
4424         limit_us = us.position();\r
4425         limit_bs = bs.position();\r
4426         \r
4427         us.limit(limit_us);\r
4428         us.position(0);\r
4429         bs.limit(limit_bs);\r
4430         bs.position(0);\r
4431         bs2 = bs.asReadOnlyBuffer();\r
4432         us2 = us.asReadOnlyBuffer();\r
4433         \r
4434         result = decoder.decode(bs2, us, true);\r
4435         if (!result.isUnderflow() || !equals(us, us2)) {\r
4436             errln("Error while decoding UTF-8 (1) should not have occured.");\r
4437         }\r
4438         \r
4439         us2.limit(limit_us);\r
4440         us2.position(0);\r
4441         bs.limit(limit_bs);\r
4442         bs.position(0);\r
4443         \r
4444         result = encoder.encode(us2, bs, true); \r
4445         if (!result.isUnderflow() || !equals(bs, bs2)) {\r
4446             errln("Error while encoding UTF-8 (1) should not have occured.");\r
4447         }  \r
4448         \r
4449         us.clear();\r
4450         bs.clear();\r
4451         \r
4452         //test overflow buffer while encoding\r
4453         //readonly buffer\r
4454         encoder.reset();\r
4455         us.put((char)0x0081); us.put((char)0xEF65); \r
4456         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4457         limit_us = us.position();\r
4458         us2 = us.asReadOnlyBuffer();\r
4459         us2.limit(limit_us);\r
4460         us2.position(0);\r
4461         bs.limit(1);\r
4462         bs.position(0);\r
4463         result = encoder.encode(us2, bs, true);\r
4464         if (!result.isOverflow()) {\r
4465             errln("Overflow Error should have occured while encoding UTF-8 (2).");\r
4466         }\r
4467         \r
4468         encoder.reset();\r
4469         \r
4470         us2.limit(limit_us);\r
4471         us2.position(1);\r
4472         bs.limit(1);\r
4473         bs.position(0);\r
4474         result = encoder.encode(us2, bs, true);\r
4475         if (!result.isOverflow()) {\r
4476             errln("Overflow Error should have occured while encoding UTF-8 (3).");\r
4477         }\r
4478         \r
4479         encoder.reset();\r
4480         \r
4481         us2.limit(limit_us);\r
4482         us2.position(1);\r
4483         bs.limit(2);\r
4484         bs.position(0);\r
4485         result = encoder.encode(us2, bs, true);\r
4486         if (!result.isOverflow()) {\r
4487             errln("Overflow Error should have occured while encoding UTF-8 (4).");\r
4488         }\r
4489         \r
4490         encoder.reset();\r
4491         \r
4492         us2.limit(limit_us);\r
4493         us2.position(0);\r
4494         bs.limit(2);\r
4495         bs.position(0);\r
4496         result = encoder.encode(us2, bs, true);\r
4497         if (!result.isOverflow()) {\r
4498             errln("Overflow Error should have occured while encoding UTF-8 (5).");\r
4499         }   \r
4500         \r
4501         //not readonly buffer\r
4502         encoder.reset();\r
4503         \r
4504         us.limit(limit_us);\r
4505         us.position(0);\r
4506         bs.limit(1);\r
4507         bs.position(0);\r
4508         result = encoder.encode(us, bs, true);\r
4509         if (!result.isOverflow()) {\r
4510             errln("Overflow Error should have occured while encoding UTF-8 (6).");\r
4511         }\r
4512         \r
4513         encoder.reset();\r
4514         \r
4515         us.limit(limit_us);\r
4516         us.position(0);\r
4517         bs.limit(3);\r
4518         bs.position(0);\r
4519         result = encoder.encode(us, bs, true);\r
4520         if (!result.isOverflow()) {\r
4521             errln("Overflow Error should have occured while encoding UTF-8 (7).");\r
4522         }\r
4523         \r
4524         encoder.reset();\r
4525         \r
4526         us.limit(limit_us);\r
4527         us.position(1);\r
4528         bs.limit(2);\r
4529         bs.position(0);\r
4530         result = encoder.encode(us, bs, true);\r
4531         if (!result.isOverflow()) {\r
4532             errln("Overflow Error should have occured while encoding UTF-8 (8).");\r
4533         }   \r
4534         \r
4535         encoder.reset();\r
4536         \r
4537         us.limit(limit_us + 1);\r
4538         us.position(1);\r
4539         bs.limit(3);\r
4540         bs.position(0);\r
4541         result = encoder.encode(us, bs, true);\r
4542         if (!result.isOverflow()) {\r
4543             errln("Overflow Error should have occured while encoding UTF-8 (9).");\r
4544         }  \r
4545         \r
4546         us.clear();\r
4547         bs.clear();\r
4548         \r
4549         //test encoding 4 byte characters\r
4550         encoder.reset();\r
4551         us.put((char)0xD902); us.put((char)0xDD02); us.put((char)0x0041);\r
4552         bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00); bs.put((byte)0x00);\r
4553         limit_us = us.position();\r
4554         us2 = us.asReadOnlyBuffer();\r
4555         us2.limit(limit_us);\r
4556         us2.position(0);\r
4557         bs.limit(1);\r
4558         bs.position(0);\r
4559         result = encoder.encode(us2, bs, true);\r
4560         if (!result.isOverflow()) {\r
4561             errln("Overflow Error should have occured while encoding UTF-8 (10).");\r
4562         }\r
4563         \r
4564         encoder.reset();\r
4565         \r
4566         us2.limit(limit_us);\r
4567         us2.position(0);\r
4568         bs.limit(2);\r
4569         bs.position(0);\r
4570         result = encoder.encode(us2, bs, true);\r
4571         if (!result.isOverflow()) {\r
4572             errln("Overflow Error should have occured while encoding UTF-8 (11).");\r
4573         }\r
4574         \r
4575         encoder.reset();\r
4576         \r
4577         us2.limit(limit_us);\r
4578         us2.position(0);\r
4579         bs.limit(3);\r
4580         bs.position(0);\r
4581         result = encoder.encode(us2, bs, true);\r
4582         if (!result.isOverflow()) {\r
4583             errln("Overflow Error should have occured while encoding UTF-8 (12).");\r
4584         }\r
4585         \r
4586         encoder.reset();\r
4587         \r
4588         us2.limit(limit_us);\r
4589         us2.position(0);\r
4590         bs.limit(4);\r
4591         bs.position(0);\r
4592         result = encoder.encode(us2, bs, true);\r
4593         if (!result.isOverflow()) {\r
4594             errln("Overflow Error should have occured while encoding UTF-8 (13).");\r
4595         }\r
4596         \r
4597         us.clear();\r
4598         bs.clear();\r
4599         \r
4600         //decoding code coverage\r
4601         //test malform error\r
4602         decoder.reset();\r
4603         bs.put((byte)0xC0); bs.put((byte)0xC0);\r
4604         us.put((char)0x0000);\r
4605         bs2 = bs.asReadOnlyBuffer();\r
4606         \r
4607         us.limit(1);\r
4608         us.position(0);\r
4609         bs2.limit(1);\r
4610         bs2.position(0);\r
4611         \r
4612         result = decoder.decode(bs2, us, true);\r
4613         result = decoder.flush(us);\r
4614         if (!result.isMalformed()) {\r
4615             errln("Malform error should have occurred while decoding UTF-8 (1).");\r
4616         }    \r
4617         \r
4618         us.limit(1);\r
4619         us.position(0);\r
4620         bs2.limit(1);\r
4621         bs2.position(0);\r
4622         \r
4623         decoder.reset();\r
4624         \r
4625         result = decoder.decode(bs2, us, true);\r
4626         us.limit(1);\r
4627         us.position(0);\r
4628         bs2.limit(2);\r
4629         bs2.position(0);\r
4630         result = decoder.decode(bs2, us, true);\r
4631         if (!result.isMalformed()) {\r
4632             errln("Malform error should have occurred while decoding UTF-8 (2).");\r
4633         }  \r
4634         \r
4635         us.clear();\r
4636         bs.clear();\r
4637         \r
4638         //test overflow buffer\r
4639         bs.put((byte)0x01); bs.put((byte)0x41);\r
4640         us.put((char)0x0000);\r
4641         bs2 = bs.asReadOnlyBuffer();\r
4642         us.limit(1);\r
4643         us.position(0);\r
4644         bs2.limit(2);\r
4645         bs2.position(0);\r
4646         \r
4647         result = decoder.decode(bs2, us, true);\r
4648         if (!result.isOverflow()) {\r
4649             errln("Overflow error should have occurred while decoding UTF-8 (3).");\r
4650         }\r
4651         \r
4652         us.clear();\r
4653         bs.clear();\r
4654         \r
4655         //test malform string\r
4656         decoder.reset();\r
4657         bs.put((byte)0xF5); bs.put((byte)0xB4); bs.put((byte)0x8A); bs.put((byte)0x8C);\r
4658         us.put((char)0x0000);\r
4659         bs2 = bs.asReadOnlyBuffer();\r
4660         us.limit(1);\r
4661         us.position(0);\r
4662         bs2.limit(4);\r
4663         bs2.position(0);\r
4664         \r
4665         result = decoder.decode(bs2, us, true);\r
4666         if (!result.isMalformed()) {\r
4667             errln("Malform error should have occurred while decoding UTF-8 (4).");\r
4668         }\r
4669         \r
4670         bs.clear();\r
4671         \r
4672         //test overflow\r
4673         decoder.reset();\r
4674         bs.put((byte)0xF3); bs.put((byte)0xB4); bs.put((byte)0x8A); bs.put((byte)0x8C);\r
4675         bs2 = bs.asReadOnlyBuffer();\r
4676         us.limit(1);\r
4677         us.position(0);\r
4678         bs2.limit(4);\r
4679         bs2.position(0);\r
4680         \r
4681         result = decoder.decode(bs2, us, true);\r
4682         if (!result.isOverflow()) {\r
4683             errln("Overflow error should have occurred while decoding UTF-8 (5).");\r
4684         }\r
4685         \r
4686         //test overflow\r
4687         decoder.reset();\r
4688         us.limit(2);\r
4689         us.position(0);\r
4690         bs2.limit(5);\r
4691         bs2.position(0);\r
4692         \r
4693         result = decoder.decode(bs2, us, true);\r
4694         if (!result.isOverflow()) {\r
4695             errln("Overflow error should have occurred while decoding UTF-8 (5).");\r
4696         }\r
4697         \r
4698         //test overflow\r
4699         decoder.reset();\r
4700         us.limit(1);\r
4701         us.position(0);\r
4702         bs.limit(5);\r
4703         bs.position(0);\r
4704         \r
4705         result = decoder.decode(bs, us, true);\r
4706         if (!result.isOverflow()) {\r
4707             errln("Overflow error should have occurred while decoding UTF-8 (6).");\r
4708         }\r
4709       \r
4710         bs.clear();\r
4711         \r
4712         //test overflow\r
4713         decoder.reset();\r
4714         bs.put((byte)0x41); bs.put((byte)0x42);\r
4715         us.limit(1);\r
4716         us.position(0);\r
4717         bs.limit(2);\r
4718         bs.position(0);\r
4719         \r
4720         result = decoder.decode(bs, us, true);\r
4721         if (!result.isOverflow()) {\r
4722             errln("Overflow error should have occurred while decoding UTF-8 (7).");\r
4723         }\r
4724         \r
4725     }\r
4726     \r
4727     //provide better code coverage for Charset UTF16\r
4728     public void TestCharsetUTF16() {\r
4729         CoderResult result = CoderResult.UNDERFLOW;\r
4730         CharsetProvider provider = new CharsetProviderICU();       \r
4731         CharsetDecoder decoder = provider.charsetForName("UTF-16").newDecoder();\r
4732         CharsetEncoder encoder = provider.charsetForName("UTF-16").newEncoder();\r
4733         \r
4734         CharBuffer us = CharBuffer.allocate(0x10);\r
4735         ByteBuffer bs = ByteBuffer.allocate(0x10);\r
4736         \r
4737         //test flush buffer and malform string\r
4738         bs.put((byte)0xFF); \r
4739         us.put((char)0x0000);\r
4740         \r
4741         us.limit(us.position());\r
4742         us.position(0);\r
4743         bs.limit(bs.position());\r
4744         bs.position(0);\r
4745         \r
4746         result = decoder.decode(bs, us, true);\r
4747         result = decoder.flush(us);\r
4748         if (!result.isMalformed()) {\r
4749             errln("Malform error while decoding UTF-16 should have occurred.");\r
4750         }\r
4751         \r
4752         us.clear();\r
4753         bs.clear();\r
4754         \r
4755         us.put((char)0xD902); us.put((char)0xDD01); us.put((char)0x0041);\r
4756         \r
4757         us.limit(1);\r
4758         us.position(0);\r
4759         bs.limit(4);\r
4760         bs.position(0);\r
4761         \r
4762         result = encoder.encode(us, bs, true);\r
4763         us.limit(3);\r
4764         us.position(0);\r
4765         bs.limit(3);\r
4766         bs.position(0);\r
4767         result = encoder.encode(us, bs, true);\r
4768         if (!result.isOverflow()) {\r
4769             errln("Overflow buffer while encoding UTF-16 should have occurred.");\r
4770         }   \r
4771         \r
4772         us.clear();\r
4773         bs.clear();\r
4774         \r
4775         //test overflow buffer\r
4776         decoder.reset();\r
4777         decoder = provider.charsetForName("UTF-16BE").newDecoder();\r
4778         \r
4779         bs.put((byte)0xFF); bs.put((byte)0xFE); bs.put((byte)0x41);\r
4780         \r
4781         us.limit(0);\r
4782         us.position(0);\r
4783         bs.limit(3);\r
4784         bs.position(0);\r
4785         \r
4786         result = decoder.decode(bs, us, true);\r
4787         if (!result.isOverflow()) {\r
4788             errln("Overflow buffer while decoding UTF-16 should have occurred.");\r
4789         }        \r
4790     }\r
4791     \r
4792     //provide better code coverage for Charset ISO-2022-KR\r
4793     public void TestCharsetISO2022KR() {\r
4794         CoderResult result = CoderResult.UNDERFLOW;\r
4795         CharsetProvider provider = new CharsetProviderICU();       \r
4796         CharsetDecoder decoder = provider.charsetForName("ISO-2022-KR").newDecoder();\r
4797         \r
4798         byte bytearray[] = {\r
4799                 (byte)0x1b, (byte)0x24, (byte)0x29, (byte)0x43, (byte)0x41, (byte)0x42,\r
4800         };\r
4801         char chararray[] = {\r
4802                 (char)0x0041\r
4803         };\r
4804         ByteBuffer bb = ByteBuffer.wrap(bytearray);\r
4805         CharBuffer cb = CharBuffer.wrap(chararray);\r
4806         \r
4807         result = decoder.decode(bb, cb, true);\r
4808         \r
4809         if (!result.isOverflow()) {\r
4810             errln("Overflow buffer while decoding ISO-2022-KR should have occurred.");\r
4811         }\r
4812     }\r
4813     \r
4814     //provide better code coverage for Charset ISO-2022-JP\r
4815     public void TestCharsetISO2022JP() {\r
4816         CoderResult result = CoderResult.UNDERFLOW;\r
4817         CharsetProvider provider = new CharsetProviderICU();       \r
4818         CharsetDecoder decoder = provider.charsetForName("ISO-2022-JP-2").newDecoder();\r
4819         \r
4820         byte bytearray[] = {\r
4821                 (byte)0x1b, (byte)0x24, (byte)0x28, (byte)0x44, (byte)0x0A, (byte)0x41,\r
4822         };\r
4823         char chararray[] = {\r
4824                 (char)0x000A\r
4825         };\r
4826         ByteBuffer bb = ByteBuffer.wrap(bytearray);\r
4827         CharBuffer cb = CharBuffer.wrap(chararray);\r
4828         \r
4829         result = decoder.decode(bb, cb, true);\r
4830         \r
4831         if (!result.isOverflow()) {\r
4832             errln("Overflow buffer while decoding ISO-2022-KR should have occurred.");\r
4833         }\r
4834     }\r
4835     \r
4836     //provide better code coverage for Charset ASCII\r
4837     public void TestCharsetASCII() {\r
4838         CoderResult result = CoderResult.UNDERFLOW;\r
4839         CharsetProvider provider = new CharsetProviderICU();       \r
4840         CharsetDecoder decoder = provider.charsetForName("US-ASCII").newDecoder();\r
4841         \r
4842         byte bytearray[] = {\r
4843                 (byte)0x41\r
4844         };\r
4845         char chararray[] = {\r
4846                 (char)0x0041\r
4847         };\r
4848         \r
4849         ByteBuffer bb = ByteBuffer.wrap(bytearray);\r
4850         CharBuffer cb = CharBuffer.wrap(chararray);\r
4851         \r
4852         result = decoder.decode(bb, cb, true);\r
4853         result = decoder.flush(cb);\r
4854         \r
4855         if (result.isError()) {\r
4856             errln("Error occurred while decoding US-ASCII.");\r
4857         }\r
4858     }\r
4859     \r
4860     // provide better code coverage for Charset Callbacks\r
4861     /* Different aspects of callbacks are being tested including using different context available */\r
4862     public void TestCharsetCallbacks() {\r
4863         CoderResult result = CoderResult.UNDERFLOW;\r
4864         CharsetProvider provider = new CharsetProviderICU();       \r
4865         CharsetEncoder encoder = provider.charsetForName("iso-2022-jp").newEncoder();\r
4866         CharsetDecoder decoder = provider.charsetForName("iso-2022-jp").newDecoder();\r
4867         \r
4868         String context3[] = {\r
4869                 "i",\r
4870                 "J"\r
4871         };\r
4872         \r
4873         // Testing encoder escape callback\r
4874         String context1[] = {\r
4875                 "J",\r
4876                 "C",\r
4877                 "D",\r
4878                 null\r
4879         };\r
4880         char chararray[] = {\r
4881                 (char)0xd122\r
4882         };\r
4883         ByteBuffer bb = ByteBuffer.allocate(20);\r
4884         CharBuffer cb = CharBuffer.wrap(chararray);\r
4885         \r
4886         ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.OVERFLOW, CharsetCallback.FROM_U_CALLBACK_ESCAPE, null);  // This callback is not valid.\r
4887         for (int i = 0; i < context1.length; i++) {\r
4888             encoder.reset();\r
4889             cb.position(0);\r
4890             bb.position(0);\r
4891             ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_ESCAPE, context1[i]); // This callback is valid.\r
4892             \r
4893             result = encoder.encode(cb, bb, true);\r
4894             if (result.isError()) {\r
4895                 errln("Error occurred while testing of callbacks for ISO-2022-JP encoder.");\r
4896             }\r
4897         }\r
4898         \r
4899         // Testing encoder skip callback\r
4900         for (int i = 0; i < context3.length; i++) {\r
4901             encoder.reset();\r
4902             cb.position(0);\r
4903             bb.position(0);\r
4904             ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_SKIP, context3[i]); \r
4905             \r
4906             result = encoder.encode(cb, bb, true);\r
4907             if (result.isError() && i == 0) {\r
4908                 errln("Error occurred while testing of callbacks for ISO-2022-JP encoder.");\r
4909             }\r
4910         }\r
4911         \r
4912         // Testing encoder sub callback\r
4913         for (int i = 0; i < context3.length; i++) {\r
4914             encoder.reset();\r
4915             cb.position(0);\r
4916             bb.position(0);\r
4917             ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_SUBSTITUTE, context3[i]); \r
4918             \r
4919             result = encoder.encode(cb, bb, true);\r
4920             if (result.isError() && i == 0) {\r
4921                 errln("Error occurred while testing of callbacks for ISO-2022-JP encoder.");\r
4922             }\r
4923         }\r
4924         \r
4925         // Testing decoder escape callback\r
4926         String context2[] = {\r
4927                 "X",\r
4928                 "C",\r
4929                 "D",\r
4930                 null\r
4931         };\r
4932         byte bytearray[] = {\r
4933                 (byte)0x1b, (byte)0x2e, (byte)0x43\r
4934         };\r
4935         bb = ByteBuffer.wrap(bytearray);\r
4936         cb = CharBuffer.allocate(20);\r
4937         \r
4938         ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.OVERFLOW, CharsetCallback.TO_U_CALLBACK_ESCAPE, null);  // This callback is not valid.\r
4939         for (int i = 0; i < context2.length; i++) {\r
4940             decoder.reset();\r
4941             cb.position(0);\r
4942             bb.position(0);\r
4943             ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.malformedForLength(1), CharsetCallback.TO_U_CALLBACK_ESCAPE, context2[i]); // This callback is valid.\r
4944             \r
4945             result = decoder.decode(bb, cb, true);\r
4946             if (result.isError()) {\r
4947                 errln("Error occurred while testing of callbacks for ISO-2022-JP decoder.");\r
4948             }\r
4949         }\r
4950         \r
4951         // Testing decoder skip callback\r
4952         for (int i = 0; i < context3.length; i++) {\r
4953             decoder.reset();\r
4954             cb.position(0);\r
4955             bb.position(0);\r
4956             ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.malformedForLength(1), CharsetCallback.TO_U_CALLBACK_SKIP, context3[i]);\r
4957             result = decoder.decode(bb, cb, true);\r
4958             if (!result.isError()) {\r
4959                 errln("Error occurred while testing of callbacks for ISO-2022-JP decoder should have occurred.");\r
4960             }\r
4961         }\r
4962     }\r
4963     \r
4964     // Testing invalid input exceptions\r
4965     public void TestInvalidInput() {\r
4966         CharsetProvider provider = new CharsetProviderICU();\r
4967         Charset charset = provider.charsetForName("iso-2022-jp");\r
4968         CharsetEncoder encoder = charset.newEncoder();\r
4969         CharsetDecoder decoder = charset.newDecoder();\r
4970         \r
4971         try {\r
4972             encoder.encode(CharBuffer.allocate(10), null, true);\r
4973             errln("Illegal argument exception should have been thrown due to null target.");\r
4974         } catch (Exception ex) {\r
4975         }\r
4976         \r
4977         try {\r
4978             decoder.decode(ByteBuffer.allocate(10), null, true);\r
4979             errln("Illegal argument exception should have been thrown due to null target.");\r
4980         } catch (Exception ex) {\r
4981         }\r
4982     }\r
4983     \r
4984     // Test java canonical names\r
4985     public void TestGetICUJavaCanonicalNames() {\r
4986         // Ambiguous charset name.\r
4987         String javaCName = CharsetProviderICU.getJavaCanonicalName("windows-1250");\r
4988         String icuCName = CharsetProviderICU.getICUCanonicalName("Windows-1250");\r
4989         if (javaCName == null || icuCName == null) {\r
4990             errln("Unable to get Java or ICU canonical name from ambiguous alias");\r
4991         }\r
4992         \r
4993     }\r
4994     \r
4995     // Port over from ICU4C for test conversion tables (mbcs version 5.x)\r
4996     // Provide better code coverage in CharsetMBCS, CharsetDecoderICU, and CharsetEncoderICU.\r
4997     public void TestCharsetTestData() {\r
4998         CoderResult result = CoderResult.UNDERFLOW;\r
4999         String charsetName = "test4";\r
5000         CharsetProvider provider = new CharsetProviderICU();\r
5001         Charset charset = ((CharsetProviderICU)provider).charsetForName(charsetName, "com/ibm/icu/dev/data/testdata",\r
5002                             this.getClass().getClassLoader());\r
5003         CharsetEncoder encoder = charset.newEncoder();\r
5004         CharsetDecoder decoder = charset.newDecoder();\r
5005         \r
5006         byte bytearray[] = {\r
5007                 0x01, 0x02, 0x03, 0x0a,\r
5008                 0x01, 0x02, 0x03, 0x0b,\r
5009                 0x01, 0x02, 0x03, 0x0d,\r
5010         };\r
5011         \r
5012         // set the callback for overflow errors\r
5013         ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.OVERFLOW, CharsetCallback.TO_U_CALLBACK_STOP, null);\r
5014         \r
5015         ByteBuffer bb = ByteBuffer.wrap(bytearray);\r
5016         CharBuffer cb = CharBuffer.allocate(10);\r
5017         \r
5018         bb.limit(4);\r
5019         cb.limit(1); // Overflow should occur and is expected\r
5020         result = decoder.decode(bb, cb, false);\r
5021         if (result.isError()) {\r
5022             errln("Error occurred while decoding: " + charsetName + " with error: " + result);\r
5023         }\r
5024         \r
5025         bb.limit(8);\r
5026         result = decoder.decode(bb, cb, false);\r
5027         if (result.isError()) {\r
5028             errln("Error occurred while decoding: " + charsetName + " with error: " + result);\r
5029         }\r
5030         \r
5031         bb.limit(12);\r
5032         result = decoder.decode(bb, cb, true);\r
5033         if (result.isError()) {\r
5034             errln("Error occurred while decoding: " + charsetName + " with error: " + result);\r
5035         }\r
5036         \r
5037         char chararray[] = {\r
5038                 0xDBC4,0xDE34,0xD900,0xDC05,/* \U00101234\U00050005 */\r
5039                 0xD940,     /* first half of \U00060006 or \U00060007 */\r
5040                 0xDC07/* second half of \U00060007 */\r
5041         };\r
5042         \r
5043         cb = CharBuffer.wrap(chararray);\r
5044         bb = ByteBuffer.allocate(10);\r
5045         \r
5046         bb.limit(2);\r
5047         cb.limit(4);\r
5048         result = encoder.encode(cb, bb, false);\r
5049         if (result.isError()) {\r
5050             errln("Error occurred while encoding: " + charsetName + " with error: " + result);\r
5051         }\r
5052         cb.limit(5);\r
5053         result = encoder.encode(cb, bb, false);\r
5054         if (result.isError()) {\r
5055             errln("Error occurred while encoding: " + charsetName + " with error: " + result);\r
5056         }\r
5057         cb.limit(6);\r
5058         result = encoder.encode(cb, bb, true);\r
5059         if (!result.isError()) {\r
5060             errln("Error should have occurred while encoding: " + charsetName);\r
5061         }\r
5062     }\r
5063     \r
5064     /* Round trip test of SCSU converter*/\r
5065     public void TestSCSUConverter(){\r
5066         byte allFeaturesSCSU[]={\r
5067             0x41,(byte) 0xdf, 0x12,(byte) 0x81, 0x03, 0x5f, 0x10, (byte)0xdf, 0x1b, 0x03,\r
5068             (byte)0xdf, 0x1c,(byte) 0x88,(byte) 0x80, 0x0b, (byte)0xbf,(byte) 0xff,(byte) 0xff, 0x0d, 0x0a,\r
5069             0x41, 0x10, (byte)0xdf, 0x12, (byte)0x81, 0x03, 0x5f, 0x10, (byte)0xdf, 0x13,\r
5070             (byte)0xdf, 0x14,(byte) 0x80, 0x15, (byte)0xff \r
5071         }; \r
5072 \r
5073         char allFeaturesUTF16[]={\r
5074             0x0041, 0x00df, 0x0401, 0x015f, 0x00df, 0x01df, 0xf000, 0xdbff,\r
5075             0xdfff, 0x000d, 0x000a, 0x0041, 0x00df, 0x0401, 0x015f, 0x00df,\r
5076             0x01df, 0xf000, 0xdbff, 0xdfff\r
5077         };\r
5078 \r
5079         \r
5080         char germanUTF16[]={\r
5081             0x00d6, 0x006c, 0x0020, 0x0066, 0x006c, 0x0069, 0x0065, 0x00df, 0x0074\r
5082         };\r
5083 \r
5084         byte germanSCSU[]={\r
5085             (byte)0xd6, 0x6c, 0x20, 0x66, 0x6c, 0x69, 0x65,(byte) 0xdf, 0x74\r
5086         };\r
5087 \r
5088        char russianUTF16[]={\r
5089             0x041c, 0x043e, 0x0441, 0x043a, 0x0432, 0x0430\r
5090         };\r
5091 \r
5092        byte russianSCSU[]={\r
5093             0x12, (byte)0x9c,(byte)0xbe,(byte) 0xc1, (byte)0xba, (byte)0xb2, (byte)0xb0\r
5094         };\r
5095 \r
5096        char japaneseUTF16[]={\r
5097             0x3000, 0x266a, 0x30ea, 0x30f3, 0x30b4, 0x53ef, 0x611b,\r
5098             0x3044, 0x3084, 0x53ef, 0x611b, 0x3044, 0x3084, 0x30ea, 0x30f3,\r
5099             0x30b4, 0x3002, 0x534a, 0x4e16, 0x7d00, 0x3082, 0x524d, 0x306b,\r
5100             0x6d41, 0x884c, 0x3057, 0x305f, 0x300c, 0x30ea, 0x30f3, 0x30b4,\r
5101             0x306e, 0x6b4c, 0x300d, 0x304c, 0x3074, 0x3063, 0x305f, 0x308a,\r
5102             0x3059, 0x308b, 0x304b, 0x3082, 0x3057, 0x308c, 0x306a, 0x3044,\r
5103             0x3002, 0x7c73, 0x30a2, 0x30c3, 0x30d7, 0x30eb, 0x30b3, 0x30f3,\r
5104             0x30d4, 0x30e5, 0x30fc, 0x30bf, 0x793e, 0x306e, 0x30d1, 0x30bd,\r
5105             0x30b3, 0x30f3, 0x300c, 0x30de, 0x30c3, 0x30af, 0xff08, 0x30de,\r
5106             0x30c3, 0x30ad, 0x30f3, 0x30c8, 0x30c3, 0x30b7, 0x30e5, 0xff09,\r
5107             0x300d, 0x3092, 0x3001, 0x3053, 0x3088, 0x306a, 0x304f, 0x611b,\r
5108             0x3059, 0x308b, 0x4eba, 0x305f, 0x3061, 0x306e, 0x3053, 0x3068,\r
5109             0x3060, 0x3002, 0x300c, 0x30a2, 0x30c3, 0x30d7, 0x30eb, 0x4fe1,\r
5110             0x8005, 0x300d, 0x306a, 0x3093, 0x3066, 0x8a00, 0x3044, 0x65b9,\r
5111             0x307e, 0x3067, 0x3042, 0x308b, 0x3002\r
5112         };\r
5113 \r
5114         // SCSUEncoder produces a slightly longer result (179B vs. 178B) because of one different choice:\r
5115          //it uses an SQn once where a longer look-ahead could have shown that SCn is more efficient \r
5116         byte japaneseSCSU[]={\r
5117             0x08, 0x00, 0x1b, 0x4c,(byte) 0xea, 0x16, (byte)0xca, (byte)0xd3,(byte) 0x94, 0x0f, 0x53, (byte)0xef, 0x61, 0x1b, (byte)0xe5,(byte) 0x84,\r
5118             (byte)0xc4, 0x0f, (byte)0x53,(byte) 0xef, 0x61, 0x1b, (byte)0xe5, (byte)0x84, (byte)0xc4, 0x16, (byte)0xca, (byte)0xd3, (byte)0x94, 0x08, 0x02, 0x0f,\r
5119             0x53, 0x4a, 0x4e, 0x16, 0x7d, 0x00, 0x30, (byte)0x82, 0x52, 0x4d, 0x30, 0x6b, 0x6d, 0x41,(byte) 0x88, 0x4c,\r
5120             (byte) 0xe5,(byte) 0x97, (byte)0x9f, 0x08, 0x0c, 0x16,(byte) 0xca,(byte) 0xd3, (byte)0x94, 0x15, (byte)0xae, 0x0e, 0x6b, 0x4c, 0x08, 0x0d,\r
5121             (byte) 0x8c, (byte)0xb4, (byte)0xa3,(byte) 0x9f,(byte) 0xca, (byte)0x99, (byte)0xcb,(byte) 0x8b, (byte)0xc2,(byte) 0x97,(byte) 0xcc,(byte) 0xaa,(byte) 0x84, 0x08, 0x02, 0x0e,\r
5122             0x7c, 0x73, (byte)0xe2, 0x16, (byte)0xa3,(byte) 0xb7, (byte)0xcb, (byte)0x93, (byte)0xd3,(byte) 0xb4,(byte) 0xc5, (byte)0xdc, (byte)0x9f, 0x0e, 0x79, 0x3e,\r
5123             0x06, (byte)0xae, (byte)0xb1, (byte)0x9d,(byte) 0x93, (byte)0xd3, 0x08, 0x0c, (byte)0xbe,(byte) 0xa3, (byte)0x8f, 0x08,(byte) 0x88,(byte) 0xbe,(byte) 0xa3,(byte) 0x8d,\r
5124             (byte)0xd3,(byte) 0xa8, (byte)0xa3, (byte)0x97,(byte) 0xc5, 0x17,(byte) 0x89, 0x08, 0x0d, 0x15,(byte) 0xd2, 0x08, 0x01, (byte)0x93, (byte)0xc8,(byte) 0xaa,\r
5125             (byte)0x8f, 0x0e, 0x61, 0x1b, (byte)0x99,(byte) 0xcb, 0x0e, 0x4e, (byte)0xba, (byte)0x9f, (byte)0xa1,(byte) 0xae,(byte) 0x93, (byte)0xa8,(byte) 0xa0, 0x08,\r
5126             0x02, 0x08, 0x0c, (byte)0xe2, 0x16, (byte)0xa3, (byte)0xb7, (byte)0xcb, 0x0f, 0x4f,(byte) 0xe1,(byte) 0x80, 0x05,(byte) 0xec, 0x60, (byte)0x8d,\r
5127             (byte)0xea, 0x06,(byte) 0xd3,(byte) 0xe6, 0x0f,(byte) 0x8a, 0x00, 0x30, 0x44, 0x65,(byte) 0xb9, (byte)0xe4, (byte)0xfe,(byte) 0xe7,(byte) 0xc2, 0x06,\r
5128             (byte)0xcb, (byte)0x82\r
5129         };\r
5130         \r
5131         CharsetProviderICU cs = new CharsetProviderICU();\r
5132         CharsetICU charset = (CharsetICU)cs.charsetForName("scsu");\r
5133         CharsetDecoder decode = charset.newDecoder();\r
5134         CharsetEncoder encode = charset.newEncoder();\r
5135         \r
5136         //String[] codePoints = {"allFeatures", "german","russian","japanese"};\r
5137         byte[][] fromUnicode={allFeaturesSCSU,germanSCSU,russianSCSU,japaneseSCSU};\r
5138         char[][] toUnicode = {allFeaturesUTF16, germanUTF16,russianUTF16,japaneseUTF16};\r
5139         \r
5140         for(int i=0;i<4;i++){\r
5141             ByteBuffer decoderBuffer = ByteBuffer.wrap(fromUnicode[i]);\r
5142             CharBuffer encoderBuffer = CharBuffer.wrap(toUnicode[i]);\r
5143                            \r
5144             try{\r
5145                 // Decoding \r
5146                 CharBuffer decoderResult = decode.decode(decoderBuffer);\r
5147                 encoderBuffer.position(0);\r
5148                 if(!decoderResult.equals(encoderBuffer)){\r
5149                     errln("Error occured while decoding "+ charset.name());\r
5150                 }\r
5151                 // Encoding \r
5152                 ByteBuffer encoderResult = encode.encode(encoderBuffer);\r
5153                 // RoundTrip Test\r
5154                 ByteBuffer roundTrip = encoderResult;\r
5155                 CharBuffer roundTripResult = decode.decode(roundTrip);\r
5156                 encoderBuffer.position(0);\r
5157                 if(!roundTripResult.equals(encoderBuffer)){\r
5158                     errln("Error occured while encoding "+ charset.name());\r
5159                 }\r
5160                 // Test overflow for code coverage reasons\r
5161                 if (i == 0) {\r
5162                     ByteBuffer test = encoderResult;\r
5163                     test.position(0);\r
5164                     CharBuffer smallBuffer = CharBuffer.allocate(11);\r
5165                     decode.reset();\r
5166                     CoderResult status = decode.decode(test, smallBuffer, true);\r
5167                     if (status != CoderResult.OVERFLOW) {\r
5168                         errln("Overflow buffer error should have been thrown.");\r
5169                     }\r
5170                 }\r
5171             }catch(Exception e){\r
5172                 errln("Exception while converting SCSU thrown: " + e);\r
5173             }\r
5174         }\r
5175         \r
5176         /* Provide better code coverage */\r
5177         /* testing illegal codepoints */\r
5178         CoderResult illegalResult = CoderResult.UNDERFLOW;\r
5179         CharBuffer illegalDecoderTrgt = CharBuffer.allocate(10);\r
5180         \r
5181         byte[] illegalDecoderSrc1 = { (byte)0x41, (byte)0xdf, (byte)0x0c };\r
5182         decode.reset();\r
5183         illegalResult = decode.decode(ByteBuffer.wrap(illegalDecoderSrc1), illegalDecoderTrgt, true);\r
5184         if (illegalResult == CoderResult.OVERFLOW || illegalResult == CoderResult.UNDERFLOW) {\r
5185             errln("Malformed error should have been returned for decoder " + charset.name());\r
5186         }\r
5187         /* code coverage test from nucnvtst.c in ICU4C */\r
5188         CoderResult ccResult = CoderResult.UNDERFLOW;\r
5189         int CCBufSize = 120 * 10;\r
5190         ByteBuffer trgt = ByteBuffer.allocate(CCBufSize);\r
5191         CharBuffer test = CharBuffer.allocate(CCBufSize);\r
5192         String [] ccSrc = {\r
5193             "\ud800\udc00", /* smallest surrogate*/\r
5194             "\ud8ff\udcff",\r
5195             "\udBff\udFff", /* largest surrogate pair*/\r
5196             "\ud834\udc00",\r
5197             //"\U0010FFFF",\r
5198             "Hello \u9292 \u9192 World!",\r
5199             "Hell\u0429o \u9292 \u9192 W\u00e4rld!",\r
5200             "Hell\u0429o \u9292 \u9292W\u00e4rld!",\r
5201 \r
5202             "\u0648\u06c8", /* catch missing reset*/\r
5203             "\u0648\u06c8",\r
5204 \r
5205             "\u4444\uE001", /* lowest quotable*/\r
5206             "\u4444\uf2FF", /* highest quotable*/\r
5207             "\u4444\uf188\u4444",\r
5208             "\u4444\uf188\uf288",\r
5209             "\u4444\uf188abc\u0429\uf288",\r
5210             "\u9292\u2222",\r
5211             "Hell\u0429\u04230o \u9292 \u9292W\u00e4\u0192rld!",\r
5212             "Hell\u0429o \u9292 \u9292W\u00e4rld!",\r
5213             "Hello World!123456",\r
5214             "Hello W\u0081\u011f\u0082!", /* Latin 1 run*/\r
5215 \r
5216             "abc\u0301\u0302",  /* uses SQn for u301 u302*/\r
5217             "abc\u4411d",      /* uses SQU*/\r
5218             "abc\u4411\u4412d",/* uses SCU*/\r
5219             "abc\u0401\u0402\u047f\u00a5\u0405", /* uses SQn for ua5*/\r
5220             "\u9191\u9191\u3041\u9191\u3041\u3041\u3000", /* SJIS like data*/\r
5221             "\u9292\u2222",\r
5222             "\u9191\u9191\u3041\u9191\u3041\u3041\u3000",\r
5223             "\u9999\u3051\u300c\u9999\u9999\u3060\u9999\u3065\u3065\u3065\u300c",\r
5224             "\u3000\u266a\u30ea\u30f3\u30b4\u53ef\u611b\u3044\u3084\u53ef\u611b\u3044\u3084\u30ea\u30f3\u30b4\u3002",\r
5225 \r
5226             "", /* empty input*/\r
5227             "\u0000", /* smallest BMP character*/\r
5228             "\uFFFF", /* largest BMP character*/\r
5229 \r
5230             /* regression tests*/\r
5231             "\u6441\ub413\ua733\uf8fe\ueedb\u587f\u195f\u4899\uf23d\u49fd\u0aac\u5792\ufc22\ufc3c\ufc46\u00aa",\r
5232             /*"\u00df\u01df\uf000\udbff\udfff\u000d\n\u0041\u00df\u0401\u015f\u00df\u01df\uf000\udbff\udfff",*/\r
5233             "\u30f9\u8321\u05e5\u181c\ud72b\u2019\u99c9\u2f2f\uc10c\u82e1\u2c4d\u1ebc\u6013\u66dc\ubbde\u94a5\u4726\u74af\u3083\u55b9\u000c",\r
5234             "\u0041\u00df\u0401\u015f",\r
5235             "\u9066\u2123abc",\r
5236             //"\ud266\u43d7\ue386\uc9c0\u4a6b\u9222\u901f\u7410\ua63f\u539b\u9596\u482e\u9d47\ucfe4\u7b71\uc280\uf26a\u982f\u862a\u4edd\uf513\ufda6\u869d\u2ee0\ua216\u3ff6\u3c70\u89c0\u9576\ud5ec\ubfda\u6cca\u5bb3\ubcea\u554c\u914e\ufa4a\uede3\u2990\ud2f5\u2729\u5141\u0f26\uccd8\u5413\ud196\ubbe2\u51b9\u9b48\u0dc8\u2195\u21a2\u21e9\u00e4\u9d92\u0bc0\u06c5",\r
5237             "\uf95b\u2458\u2468\u0e20\uf51b\ue36e\ubfc1\u0080\u02dd\uf1b5\u0cf3\u6059\u7489",\r
5238         };\r
5239         for (int i = 0; i < ccSrc.length; i++) {\r
5240             CharBuffer ubuf = CharBuffer.wrap(ccSrc[i]);\r
5241             encode.reset();\r
5242             decode.reset();\r
5243             trgt.clear();\r
5244             test.clear();\r
5245             ccResult = encode.encode(ubuf, trgt, true);\r
5246             if (ccResult.isError()) {\r
5247                 errln("Error while encoding " + charset.name() + " in test for code coverage[" + i + "].");\r
5248             } else {\r
5249                 trgt.limit(trgt.position());\r
5250                 trgt.position(0);\r
5251                 ccResult = decode.decode(trgt, test, true);\r
5252                 if (ccResult.isError()) {\r
5253                     errln("Error while decoding " + charset.name() + " in test for code coverage[" + i + "].");\r
5254                 } else {\r
5255                     ubuf.position(0);\r
5256                     test.limit(test.position());\r
5257                     test.position(0);\r
5258                     if (!equals(test, ubuf)) {\r
5259                         errln("Roundtrip failed for " + charset.name() + " in test for code coverage[" + i + "].");\r
5260                     }\r
5261                 }\r
5262             }\r
5263         }\r
5264         \r
5265         /* Monkey test */\r
5266         {\r
5267             char[] monkeyIn = {\r
5268                     0x00A8, 0x3003, 0x3005, 0x2015, 0xFF5E, 0x2016, 0x2026, 0x2018, 0x000D, 0x000A,\r
5269                     0x2019, 0x201C, 0x201D, 0x3014, 0x3015, 0x3008, 0x3009, 0x300A, 0x000D, 0x000A,\r
5270                     0x300B, 0x300C, 0x300D, 0x300E, 0x300F, 0x3016, 0x3017, 0x3010, 0x000D, 0x000A,\r
5271                     0x3011, 0x00B1, 0x00D7, 0x00F7, 0x2236, 0x2227, 0x7FC1, 0x8956, 0x000D, 0x000A,\r
5272                     0x9D2C, 0x9D0E, 0x9EC4, 0x5CA1, 0x6C96, 0x837B, 0x5104, 0x5C4B, 0x000D, 0x000A,\r
5273                     0x61B6, 0x81C6, 0x6876, 0x7261, 0x4E59, 0x4FFA, 0x5378, 0x57F7, 0x000D, 0x000A,\r
5274                     0x57F4, 0x57F9, 0x57FA, 0x57FC, 0x5800, 0x5802, 0x5805, 0x5806, 0x000D, 0x000A,\r
5275                     0x580A, 0x581E, 0x6BB5, 0x6BB7, 0x6BBA, 0x6BBC, 0x9CE2, 0x977C, 0x000D, 0x000A,\r
5276                     0x6BBF, 0x6BC1, 0x6BC5, 0x6BC6, 0x6BCB, 0x6BCD, 0x6BCF, 0x6BD2, 0x000D, 0x000A,\r
5277                     0x6BD3, 0x6BD4, 0x6BD6, 0x6BD7, 0x6BD8, 0x6BDB, 0x6BEB, 0x6BEC, 0x000D, 0x000A,\r
5278                     0x6C05, 0x6C08, 0x6C0F, 0x6C11, 0x6C13, 0x6C23, 0x6C34, 0x0041, 0x000D, 0x000A,\r
5279                     0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x000D, 0x000A,\r
5280                     0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x000D, 0x000A,\r
5281                     0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x000D, 0x000A,\r
5282                     0x005B, 0x9792, 0x9CCC, 0x9CCD, 0x9CCE, 0x9CCF, 0x9CD0, 0x9CD3, 0x000D, 0x000A,\r
5283                     0x9CD4, 0x9CD5, 0x9CD7, 0x9CD8, 0x9CD9, 0x9CDC, 0x9CDD, 0x9CDF, 0x000D, 0x000A,\r
5284                     0x9785, 0x9791, 0x00BD, 0x0390, 0x0385, 0x0386, 0x0388, 0x0389, 0x000D, 0x000A,\r
5285                     0x038E, 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x000D, 0x000A,\r
5286                     0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x038A, 0x038C, 0x039C, 0x000D, 0x000A,\r
5287                     /* test non-BMP code points */\r
5288                     0xD869, 0xDE99, 0xD869, 0xDE9C, 0xD869, 0xDE9D, 0xD869, 0xDE9E, 0xD869, 0xDE9F,\r
5289                     0xD869, 0xDEA0, 0xD869, 0xDEA5, 0xD869, 0xDEA6, 0xD869, 0xDEA7, 0xD869, 0xDEA8,\r
5290                     0xD869, 0xDEAB, 0xD869, 0xDEAC, 0xD869, 0xDEAD, 0xD869, 0xDEAE, 0xD869, 0xDEAF,\r
5291                     0xD869, 0xDEB0, 0xD869, 0xDEB1, 0xD869, 0xDEB3, 0xD869, 0xDEB5, 0xD869, 0xDEB6,\r
5292                     0xD869, 0xDEB7, 0xD869, 0xDEB8, 0xD869, 0xDEB9, 0xD869, 0xDEBA, 0xD869, 0xDEBB,\r
5293                     0xD869, 0xDEBC, 0xD869, 0xDEBD, 0xD869, 0xDEBE, 0xD869, 0xDEBF, 0xD869, 0xDEC0,\r
5294                     0xD869, 0xDEC1, 0xD869, 0xDEC2, 0xD869, 0xDEC3, 0xD869, 0xDEC4, 0xD869, 0xDEC8,\r
5295                     0xD869, 0xDECA, 0xD869, 0xDECB, 0xD869, 0xDECD, 0xD869, 0xDECE, 0xD869, 0xDECF,\r
5296                     0xD869, 0xDED0, 0xD869, 0xDED1, 0xD869, 0xDED2, 0xD869, 0xDED3, 0xD869, 0xDED4,\r
5297                     0xD869, 0xDED5, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xDBFF, 0xDFFF,\r
5298                     0xDBFF, 0xDFFF, 0xDBFF, 0xDFFF,\r
5299 \r
5300 \r
5301                     0x4DB3, 0x4DB4, 0x4DB5, 0x4E00, 0x4E00, 0x4E01, 0x4E02, 0x4E03, 0x000D, 0x000A,\r
5302                     0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x33E0, 0x33E6, 0x000D, 0x000A,\r
5303                     0x4E05, 0x4E07, 0x4E04, 0x4E08, 0x4E08, 0x4E09, 0x4E0A, 0x4E0B, 0x000D, 0x000A,\r
5304                     0x4E0C, 0x0021, 0x0022, 0x0023, 0x0024, 0xFF40, 0xFF41, 0xFF42, 0x000D, 0x000A,\r
5305                     0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47, 0xFF48, 0xFF49, 0xFF4A, 0x000D, 0x000A,\r
5306             };\r
5307             encode.reset();\r
5308             decode.reset();\r
5309             CharBuffer monkeyCB = CharBuffer.wrap(monkeyIn);\r
5310             try {\r
5311                 ByteBuffer monkeyBB = encode.encode(monkeyCB); \r
5312                 /* CharBuffer monkeyEndResult =*/ decode.decode(monkeyBB);\r
5313                 \r
5314             } catch (Exception ex) {\r
5315                 errln("Exception thrown while encoding/decoding monkey test in SCSU: " + ex);\r
5316             }\r
5317         }\r
5318         // Test malformed\r
5319         {\r
5320             char[] malformedSequence = {\r
5321                     0xD899, 0xDC7F, 0xDC88, 0xDC88, 0xD888, 0xDDF9\r
5322             };\r
5323             encode.reset();\r
5324             CharBuffer malformedSrc = CharBuffer.wrap(malformedSequence);\r
5325             \r
5326             try {\r
5327                 encode.encode(malformedSrc);\r
5328                 errln("Malformed error should have thrown an exception.");\r
5329             } catch (Exception ex) {\r
5330             }\r
5331         }\r
5332         // Test overflow buffer\r
5333         {\r
5334             ByteBuffer overflowTest = ByteBuffer.wrap(allFeaturesSCSU);\r
5335             int sizes[] = { 8, 2, 11 };\r
5336             for (int i = 0; i < sizes.length; i++) {\r
5337                 try {\r
5338                     decode.reset();\r
5339                     overflowTest.position(0);\r
5340                     smBufDecode(decode, "SCSU overflow test", overflowTest, CharBuffer.allocate(sizes[i]), true, false);\r
5341                     errln("Buffer overflow exception should have been thrown.");\r
5342                 } catch (BufferOverflowException ex) {\r
5343                 } catch (Exception ex) {\r
5344                     errln("Buffer overflow exception should have been thrown.");\r
5345                 }\r
5346             }\r
5347             \r
5348         }\r
5349     } \r
5350     \r
5351     /* Test for BOCU1 converter*/\r
5352     public void TestBOCU1Converter(){\r
5353         char expected[]={\r
5354                   0xFEFF, 0x0061, 0x0062, 0x0020, // 0 \r
5355                   0x0063, 0x0061, 0x000D, 0x000A,\r
5356 \r
5357                   0x0020, 0x0000, 0x00DF, 0x00E6, // 8 \r
5358                   0x0930, 0x0020, 0x0918, 0x0909,\r
5359 \r
5360                   0x3086, 0x304D, 0x0020, 0x3053, // 16 \r
5361                   0x4000, 0x4E00, 0x7777, 0x0020, \r
5362 \r
5363                   0x9FA5, 0x4E00, 0xAC00, 0xBCDE, // 24 \r
5364                   0x0020, 0xD7A3, 0xDC00, 0xD800,\r
5365 \r
5366                   0xD800, 0xDC00, 0xD845, 0xDDDD, // 32 \r
5367                   0xDBBB, 0xDDEE, 0x0020, 0xDBFF,\r
5368 \r
5369                   0xDFFF, 0x0001, 0x0E40, 0x0020, // 40 \r
5370                   0x0009  \r
5371         };\r
5372         \r
5373         byte sampleText[]={ // from cintltst/bocu1tst.c/TestBOCU1 text 1 \r
5374             (byte) 0xFB,\r
5375             (byte) 0xEE,\r
5376             0x28, // from source offset 0\r
5377             0x24, 0x1E, 0x52, (byte) 0xB2, 0x20,\r
5378             (byte) 0xB3,\r
5379             (byte) 0xB1,\r
5380             0x0D,\r
5381             0x0A,\r
5382 \r
5383             0x20, // from 8\r
5384             0x00, (byte) 0xD0, 0x6C, (byte) 0xB6, (byte) 0xD8, (byte) 0xA5,\r
5385             0x20, 0x68,\r
5386             0x59,\r
5387 \r
5388             (byte) 0xF9,\r
5389             0x28, // from 16\r
5390             0x6D, 0x20, 0x73, (byte) 0xE0, 0x2D, (byte) 0xDE, 0x43,\r
5391             (byte) 0xD0, 0x33, 0x20,\r
5392 \r
5393             (byte) 0xFA,\r
5394             (byte) 0x83, // from 24\r
5395             0x25, 0x01, (byte) 0xFB, 0x16, (byte) 0x87, 0x4B, 0x16, 0x20,\r
5396             (byte) 0xE6, (byte) 0xBD, (byte) 0xEB, 0x5B, 0x4B, (byte) 0xCC,\r
5397 \r
5398             (byte) 0xF9,\r
5399             (byte) 0xA2, // from 32\r
5400             (byte) 0xFC, 0x10, 0x3E, (byte) 0xFE, 0x16, 0x3A, (byte) 0x8C,\r
5401             0x20, (byte) 0xFC, 0x03, (byte) 0xAC,\r
5402 \r
5403             0x01, /// from 41 \r
5404             (byte) 0xDE, (byte) 0x83, 0x20, 0x09 \r
5405         };\r
5406         \r
5407         CharsetProviderICU cs = new CharsetProviderICU();\r
5408         CharsetICU charset = (CharsetICU)cs.charsetForName("BOCU-1");\r
5409         CharsetDecoder decode = charset.newDecoder();\r
5410         CharsetEncoder encode = charset.newEncoder();\r
5411        \r
5412         ByteBuffer decoderBuffer = ByteBuffer.wrap(sampleText);\r
5413         CharBuffer encoderBuffer = CharBuffer.wrap(expected);\r
5414         try{\r
5415             // Decoding \r
5416             CharBuffer decoderResult = decode.decode(decoderBuffer);\r
5417             \r
5418             encoderBuffer.position(0);\r
5419             if(!decoderResult.equals(encoderBuffer)){\r
5420                 errln("Error occured while decoding "+ charset.name());\r
5421             }\r
5422             // Encoding \r
5423             ByteBuffer encoderResult = encode.encode(encoderBuffer);\r
5424             // RoundTrip Test\r
5425             ByteBuffer roundTrip = encoderResult;\r
5426             CharBuffer roundTripResult = decode.decode(roundTrip);\r
5427             \r
5428             encoderBuffer.position(0);\r
5429             if(!roundTripResult.equals(encoderBuffer)){\r
5430                 errln("Error occured while encoding "+ charset.name());\r
5431             }\r
5432         }catch(Exception e){\r
5433             errln("Exception while converting BOCU-1 thrown: " + e);\r
5434         }\r
5435     }\r
5436     \r
5437     /* Test that ICU4C and ICU4J get the same ICU canonical name when given the same alias. */\r
5438     public void TestICUCanonicalNameConsistency() {\r
5439         String[] alias = {\r
5440                 "KSC_5601"\r
5441         };\r
5442         String[] expected = {\r
5443                 "windows-949-2000"\r
5444         };\r
5445 \r
5446         for (int i = 0; i < alias.length; i++) {\r
5447             String name = CharsetProviderICU.getICUCanonicalName(alias[i]);\r
5448             if (!name.equals(expected[i])) {\r
5449                 errln("The ICU canonical name in ICU4J does not match that in ICU4C. Result: " + name + "Expected: " + expected[i]);\r
5450             }\r
5451         }\r
5452     }\r
5453     \r
5454     /* Increase code coverage for CharsetICU and CharsetProviderICU*/\r
5455     public void TestCharsetICUCodeCoverage() {\r
5456         CharsetProviderICU provider = new CharsetProviderICU();\r
5457 \r
5458         if (provider.charsetForName("UTF16", null) != null) {\r
5459             errln("charsetForName should have returned a null");\r
5460         }\r
5461 \r
5462         if (CharsetProviderICU.getJavaCanonicalName(null) != null) {\r
5463             errln("getJavaCanonicalName should have returned a null when null is given to it.");\r
5464         }\r
5465 \r
5466         try {\r
5467             Charset testCharset = CharsetICU.forNameICU("bogus");\r
5468             errln("UnsupportedCharsetException should be thrown for charset \"bogus\" - but got charset " + testCharset.name());\r
5469         } catch (UnsupportedCharsetException ex) {\r
5470             logln("UnsupportedCharsetException was thrown for CharsetICU.forNameICU(\"bogus\")");\r
5471         }\r
5472 \r
5473         Charset charset = provider.charsetForName("UTF16");\r
5474 \r
5475         try {\r
5476             ((CharsetICU)charset).getUnicodeSet(null, 0);\r
5477         } catch (IllegalArgumentException ex) {\r
5478             return;\r
5479         }\r
5480         errln("IllegalArgumentException should have been thrown.");\r
5481     }\r
5482     \r
5483     public void TestCharsetLMBCS() {\r
5484         String []lmbcsNames = {\r
5485                 "LMBCS-1",\r
5486                 "LMBCS-2",\r
5487                 "LMBCS-3",\r
5488                 "LMBCS-4",\r
5489                 "LMBCS-5",\r
5490                 "LMBCS-6",\r
5491                 "LMBCS-8",\r
5492                 "LMBCS-11",\r
5493                 "LMBCS-16",\r
5494                 "LMBCS-17",\r
5495                 "LMBCS-18",\r
5496                 "LMBCS-19"\r
5497         };\r
5498         \r
5499         char[] src = {\r
5500                 0x0192, 0x0041, 0x0061, 0x00D0, 0x00F6, 0x0100, 0x0174, 0x02E4, 0x03F5, 0x03FB,\r
5501                 0x05D3, 0x05D4, 0x05EA, 0x0684, 0x0685, 0x1801, 0x11B3, 0x11E8, 0x1F9A, 0x2EB4,\r
5502                 0x3157, 0x3336, 0x3304, 0xD881, 0xDC88\r
5503         };\r
5504         CharBuffer cbInput = CharBuffer.wrap(src);\r
5505         \r
5506         CharsetProviderICU provider = new CharsetProviderICU();\r
5507         \r
5508         for (int i = 0; i < lmbcsNames.length; i++) {\r
5509             Charset charset = provider.charsetForName(lmbcsNames[i]);\r
5510             if (charset == null) {\r
5511                 errln("Unable to create LMBCS charset: " + lmbcsNames[i]);\r
5512                 return;\r
5513             }\r
5514             CharsetEncoder encoder = charset.newEncoder();\r
5515             CharsetDecoder decoder = charset.newDecoder();\r
5516             \r
5517             try {\r
5518                 cbInput.position(0);\r
5519                 ByteBuffer bbTmp = encoder.encode(cbInput);\r
5520                 CharBuffer cbOutput = decoder.decode(bbTmp);\r
5521                 \r
5522                 if (!equals(cbInput, cbOutput)) {\r
5523                     errln("Roundtrip test failed for charset: " + lmbcsNames[i]);\r
5524                 }\r
5525             } catch (Exception ex) {\r
5526                 if (i >= 8) {\r
5527                     /* Expected exceptions */\r
5528                     continue;\r
5529                 }\r
5530                 errln("Exception thrown: " + ex + " while using charset: " + lmbcsNames[i]);\r
5531             }\r
5532             \r
5533         }\r
5534         \r
5535         // Test malformed\r
5536         CoderResult malformedResult = CoderResult.UNDERFLOW;\r
5537         byte[] malformedBytes = {\r
5538                 (byte)0x61, (byte)0x01, (byte)0x29, (byte)0x81, (byte)0xa0, (byte)0x0f\r
5539         };\r
5540         ByteBuffer malformedSrc = ByteBuffer.wrap(malformedBytes);\r
5541         CharBuffer malformedTrgt = CharBuffer.allocate(10);\r
5542         int[] malformedLimits = {\r
5543                 2, 6\r
5544         };\r
5545         CharsetDecoder malformedDecoderTest = provider.charsetForName("LMBCS-1").newDecoder();\r
5546         for (int n = 0; n < malformedLimits.length; n++) {\r
5547             malformedDecoderTest.reset();\r
5548             \r
5549             malformedSrc.position(0);\r
5550             malformedSrc.limit(malformedLimits[n]);\r
5551             \r
5552             malformedTrgt.clear();\r
5553             \r
5554             malformedResult = malformedDecoderTest.decode(malformedSrc,malformedTrgt, true);\r
5555             if (!malformedResult.isMalformed()) {\r
5556                 errln("Malformed error should have resulted.");\r
5557             }\r
5558         }\r
5559     }\r
5560     \r
5561     /*\r
5562      * This is a port of ICU4C TestAmbiguousConverter in cintltst.\r
5563      * Since there is no concept of ambiguous converters in ICU4J\r
5564      * this test is merely for code coverage reasons.\r
5565      */\r
5566     public void TestAmbiguousConverter() {\r
5567         byte [] inBytes = {\r
5568                 0x61, 0x5b, 0x5c\r
5569         };\r
5570         ByteBuffer src = ByteBuffer.wrap(inBytes);\r
5571         CharBuffer trgt = CharBuffer.allocate(20);\r
5572         \r
5573         CoderResult result = CoderResult.UNDERFLOW;\r
5574         CharsetProviderICU provider = new CharsetProviderICU();\r
5575         String[] names = CharsetProviderICU.getAllNames();\r
5576         \r
5577         for (int i = 0; i < names.length; i++) {\r
5578             Charset charset = provider.charsetForName(names[i]);\r
5579             if (charset == null) {\r
5580                 /* We don't care about any failures because not all converters are available. */\r
5581                 continue;\r
5582             }\r
5583             CharsetDecoder decoder = charset.newDecoder();\r
5584             \r
5585             src.position(0);\r
5586             trgt.clear();\r
5587             \r
5588             result = decoder.decode(src, trgt, true);\r
5589             if (result.isError()) {\r
5590                 /* We don't care about any failures. */\r
5591                 continue;\r
5592             }\r
5593         }\r
5594     }\r
5595 }\r