2 *******************************************************************************
\r
3 * Copyright (C) 2006-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 *******************************************************************************
\r
10 package com.ibm.icu.dev.test.charset;
\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
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
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
69 protected void init(){
\r
71 if ("UTF-16".equals(m_encoding)) {
\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
85 public static void main(String[] args) throws Exception {
\r
86 new TestCharset().run(args);
\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
94 Charset cs2 = icu.charsetForName("UTF-16LE");
\r
95 CharsetEncoder e2 = cs2.newEncoder();
\r
96 CharsetDecoder d2 = cs2.newDecoder();
\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
105 if((c>=0xd800&&c<=0xdFFF)||c>0x10FFFF){
\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
116 if(!UTF16.isTrailSurrogate(trail)){
\r
117 errln("trail is not trail!"+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
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
134 bs1.put((byte)0x00);
\r
135 bs1.put((byte)(c));
\r
136 bs2.put((byte)(c));
\r
137 bs2.put((byte)0x00);
\r
139 bs1.put((byte)(c>>8));
\r
140 bs1.put((byte)(c&0xFF));
\r
142 bs2.put((byte)(c&0xFF));
\r
143 bs2.put((byte)(c>>8));
\r
150 us.limit(us.position());
\r
152 if(us.length()==0){
\r
157 bs1.limit(bs1.position());
\r
159 ByteBuffer newBS = ByteBuffer.allocate(bs1.capacity());
\r
160 //newBS.put((byte)0xFE);
\r
161 //newBS.put((byte)0xFF);
\r
164 smBufDecode(d1, "UTF-16", bs1, us);
\r
165 smBufEncode(e1, "UTF-16", us, newBS);
\r
167 bs2.limit(bs2.position());
\r
170 //newBS.put((byte)0xFF);
\r
171 //newBS.put((byte)0xFE);
\r
174 smBufDecode(d2, "UTF16-LE", bs2, us);
\r
175 smBufEncode(e2, "UTF-16LE", us, newBS);
\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
185 Charset cs2 = icu.charsetForName("UTF-32LE");
\r
186 CharsetEncoder e2 = cs2.newEncoder();
\r
187 CharsetDecoder d2 = cs2.newDecoder();
\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
196 if((c>=0xd800&&c<=0xdFFF)||c>0x10FFFF){
\r
201 char lead = UTF16.getLeadSurrogate(c);
\r
202 char trail = UTF16.getTrailSurrogate(c);
\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
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
219 bs1.limit(bs1.position());
\r
221 bs2.limit(bs2.position());
\r
223 us.limit(us.position());
\r
225 if(us.length()==0){
\r
230 ByteBuffer newBS = ByteBuffer.allocate(bs1.capacity());
\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
239 smBufDecode(d1, "UTF-32", bs1, us);
\r
240 smBufEncode(e1, "UTF-32", us, newBS);
\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
252 smBufDecode(d2, "UTF-32LE", bs2, us);
\r
253 smBufEncode(e2, "UTF-32LE", us, newBS);
\r
257 public void TestASCIIConverter() {
\r
258 runTestASCIIBasedConverter("ASCII", 0x80);
\r
260 public void Test88591Converter() {
\r
261 runTestASCIIBasedConverter("iso-8859-1", 0x100);
\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
270 /* test with and without array-backed buffers */
\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
279 boolean fail = false;
\r
280 boolean arrays = false;
\r
281 boolean decoding = false;
\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
297 smBufDecode(decoder, converter, bs, us, true, false, true);
\r
301 smBufDecode(decoder, converter, bs, us, true, false, false);
\r
305 smBufEncode(encoder, converter, us, bs, true, false, true);
\r
309 smBufEncode(encoder, converter, us, bs, true, false, false);
\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
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
323 smBufDecode(decoder, converter, bs, us, true, false, true);
\r
327 } catch (Exception ex) {
\r
330 smBufDecode(decoder, converter, bs, us, true, false, false);
\r
334 } catch (Exception ex) {
\r
338 errln("Failed to fail to decode 0x" + Integer.toHexString(i)
\r
339 + (arrays ? " with arrays" : " without arrays") + " in " + converter);
\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
349 smBufEncode(encoder, converter, us, bs, true, false, true);
\r
353 } catch (Exception ex) {
\r
356 smBufEncode(encoder, converter, us, bs, true, false, false);
\r
360 } catch (Exception ex) {
\r
364 errln("Failed to fail to encode 0x" + Integer.toHexString(i)
\r
365 + (arrays ? " with arrays" : " without arrays") + " in " + converter);
\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
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
399 // expecting overflow
\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
406 } catch (Exception ex) {
\r
407 if (!(ex instanceof BufferOverflowException)) {
\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
417 } catch (Exception ex) {
\r
418 if (!(ex instanceof BufferOverflowException)) {
\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
428 } catch (Exception ex) {
\r
429 if (!(ex instanceof BufferOverflowException)) {
\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
439 } catch (Exception ex) {
\r
440 if (!(ex instanceof BufferOverflowException)) {
\r
448 errln("Incorrect result in " + converter + " for underflow / overflow edge cases");
\r
452 // test surrogate combinations in encoding
\r
453 String lead = "\ud888";
\r
454 String trail = "\udc88";
\r
456 String ext = "\u0275"; // theta
\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
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
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
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
498 source = CharBuffer.wrap(input[index].toCharArray());
\r
499 cr = encoder.encode(source, bs, true);
\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
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
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
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
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
549 // 1 << 7 to 1 << 11 (2 byters)
\r
550 for (; i < size[1]; 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
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
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
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
592 bs = ByteBuffer.wrap(bytes, x, 3).slice();
\r
593 us = CharBuffer.wrap(chars, y, 1).slice();
\r
596 cr = decoder.decode(bs, us, true);
\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
605 cr = encoder.encode(us, bs, true);
\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
614 bs = ByteBuffer.wrap(bytes, x, 3).slice();
\r
615 us = CharBuffer.wrap(new String(chars, y, 1));
\r
618 cr = decoder.decode(bs, us, true);
\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
627 cr = encoder.encode(us, bs, true);
\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
643 public void TestHZ() {
\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
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
666 CharBuffer start = CharBuffer.wrap(in);
\r
667 ByteBuffer bytes = encoder.encode(start);
\r
668 CharBuffer finish = decoder.decode(bytes);
\r
670 if (!equals(start, finish)) {
\r
671 errln(converter + " roundtrip test failed: start does not match finish");
\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
677 logln("start: " + hex(in));
\r
678 logln("finish: " + hex(finishArray));
\r
680 } catch (CharacterCodingException ex) {
\r
681 errln(converter + " roundtrip test failed: " + ex.getMessage());
\r
682 ex.printStackTrace(System.err);
\r
685 /* For better code coverage */
\r
686 CoderResult result = CoderResult.UNDERFLOW;
\r
688 (byte)0x7e, (byte)0x7d, (byte)0x41,
\r
689 (byte)0x7e, (byte)0x7b, (byte)0x21,
\r
692 (char)0x0041, (char)0x0042, (char)0x3000
\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
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
708 if (!result.isOverflow()) {
\r
709 errln("Overflow buffer error should have occurred while encoding HZ (" + i + ")");
\r
712 if (result.isError()) {
\r
713 errln("Error should not have occurred while encoding HZ.(" + i + ")");
\r
719 public void TestUTF8Surrogates() {
\r
720 byte[][] in = new byte[][] {
\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
729 /* expected test results */
\r
730 char[][] results = new char[][] {
\r
731 /* number of bytes read, code point */
\r
735 { '\ud800', '\udc00', }, // 10000
\r
736 { '\udbd0', '\udf21', }, // 104321
\r
737 { '\ud801', '\udc01', }, // 10401
\r
740 /* error test input */
\r
741 byte[][] in2 = new byte[][] {
\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
753 /* expected error test results */
\r
754 char[][] results2 = new char[][] {
\r
755 /* number of bytes read, code point */
\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
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
772 smBufDecode(decoder, converter, source, expected, true, false,
\r
775 } catch (Exception ex) {
\r
776 errln("Incorrect result in " + converter);
\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
785 smBufDecode(decoder, converter, source, expected, true, false,
\r
788 } catch (Exception ex) {
\r
789 errln("Incorrect result in " + converter);
\r
793 public void TestSurrogateBehavior() {
\r
794 CharsetProviderICU icu = new CharsetProviderICU();
\r
796 // get all the converters into an array
\r
797 Object[] converters = CharsetProviderICU.getAvailableNames();
\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
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
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
824 int[] positions = new int[] { 1,1,2,4,5,6,7 };
\r
825 int n = positions.length;
\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
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
844 boolean currentlybad = false;
\r
845 Charset icuChar = icu.charsetForName(converter);
\r
846 CharsetEncoder encoder = icuChar.newEncoder();
\r
849 CharBuffer source = CharBuffer.wrap(input);
\r
850 ByteBuffer target = ByteBuffer.allocate(30);
\r
851 ByteBuffer expected = null;
\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
857 } catch (CharacterCodingException ex) {
\r
858 errln("Unexpected CharacterCodingException: " + ex.getMessage());
\r
860 } catch (RuntimeException ex) {
\r
861 if (!currentlybad) {currentlybad = true; badcount++; logln(""); }
\r
862 errln(converter + " " + ex.getClass().getName() + ": " + ex.getMessage());
\r
866 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
\r
867 encoder.onMalformedInput(CodingErrorAction.REPORT);
\r
868 for (int i=0; i<n; i++) {
\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
874 if (!currentlybad) {currentlybad = true; badcount++; logln(""); }
\r
875 if (results[i].isMalformed() && cr.isMalformed()) {
\r
877 } else if (results[i].isUnmappable() && cr.isUnmappable()) {
\r
882 errln("(index=" + i + ") " + converter + " Received: " + cr + " Expected: " + results[i]);
\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
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
900 if (!currentlybad) {
\r
902 pass.add(converter);
\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
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
917 // public void TestCharsetCallback() {
\r
918 // String currentTest = "initialization";
\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
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
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
939 // // get relevant fields
\r
940 // Object SUB_STOP_ON_ILLEGAL = getFieldValue(CharsetCallback, "SUB_STOP_ON_ILLEGAL", null);
\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
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
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
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
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
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
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
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
994 // // reset certain fields
\r
995 // setFieldValue(CharsetDecoderICU.class, "invalidCharLength", decoder, invalidCharLength);
\r
996 // setFieldValue(CharsetICU.class, "subChar1", ((CharsetICU) decoder.charset()), subChar1_alternate);
\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
1001 // // begin creating the tests array
\r
1002 // ArrayList tests = new ArrayList();
\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
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
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
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
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
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
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
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
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
1063 // // invoke the method
\r
1064 // CoderResult actual = (CoderResult)method.invoke(field, args);
\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
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
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
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
1093 // } catch (Exception ex) {
\r
1094 // errln("TestCharsetCallback skipped due to " + ex.toString());
\r
1095 // ex.printStackTrace();
\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
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
1124 // field.set(instance, value);
\r
1126 // private boolean coderResultsEqual(CoderResult a, CoderResult b) {
\r
1127 // if (a == null && b == null)
\r
1129 // if (a == null || b == null)
\r
1131 // if ((a.isUnderflow() && b.isUnderflow()) || (a.isOverflow() && b.isOverflow()))
\r
1133 // if (a.length() != b.length())
\r
1135 // if ((a.isMalformed() && b.isMalformed()) || (a.isUnmappable() && b.isUnmappable()))
\r
1139 // private boolean buffersEqual(ByteBuffer a, ByteBuffer b) {
\r
1140 // if (a.position() != b.position())
\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
1148 // private boolean buffersEqual(CharBuffer a, CharBuffer b) {
\r
1149 // if (a.position() != b.position())
\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
1157 // private boolean buffersEqual(IntBuffer a, IntBuffer b) {
\r
1158 // if (a.position() != b.position())
\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
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
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
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
1186 public void TestAPISemantics(/*String encoding*/)
\r
1187 throws Exception {
\r
1189 ByteBuffer byes = ByteBuffer.wrap(byteStr);
\r
1190 CharBuffer uniVal = CharBuffer.wrap(unistr);
\r
1191 ByteBuffer expected = ByteBuffer.wrap(expectedByteStr);
\r
1194 if(m_decoder==null){
\r
1195 warnln("Could not load decoder.");
\r
1198 m_decoder.reset();
\r
1199 /* Convert the whole buffer to Unicode */
\r
1201 CharBuffer chars = CharBuffer.allocate(unistr.length());
\r
1202 CoderResult result = m_decoder.decode(byes, chars, false);
\r
1204 if (result.isError()) {
\r
1205 errln("ToChars encountered Error");
\r
1208 if (result.isOverflow()) {
\r
1209 errln("ToChars encountered overflow exception");
\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
1220 } catch (Exception e) {
\r
1221 errln("ToChars - exception in buffer");
\r
1225 /* Convert single bytes to Unicode */
\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
1233 result = m_decoder.decode(b, chars, false);
\r
1234 if(result.isOverflow()){
\r
1235 errln("ToChars single threw an overflow exception");
\r
1237 if (result.isError()) {
\r
1238 errln("ToChars single the result is an error "+result.toString());
\r
1241 if (unistr.length() != (chars.limit())) {
\r
1242 errln("ToChars single len does not match");
\r
1245 if (!equals(chars, unistr)) {
\r
1246 errln("ToChars single does not match");
\r
1247 printchars(chars);
\r
1250 } catch (Exception e) {
\r
1251 errln("ToChars - exception in single");
\r
1252 //e.printStackTrace();
\r
1256 /* Convert the buffer one at a time to Unicode */
\r
1258 CharBuffer chars = CharBuffer.allocate(unistr.length());
\r
1259 m_decoder.reset();
\r
1261 for (int i = 1; i <= byteStr.length; 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
1267 if(result.isOverflow()){
\r
1268 errln("ToChars Simple threw an overflow exception");
\r
1271 if (chars.limit() != unistr.length()) {
\r
1272 errln("ToChars Simple buffer len does not match");
\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
1282 } catch (Exception e) {
\r
1283 errln("ToChars - exception in single buffer");
\r
1284 //e.printStackTrace(System.err);
\r
1288 errln("Test Simple ToChars for encoding : FAILED");
\r
1292 /* Convert the whole buffer from unicode */
\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
1301 if(result.isOverflow()){
\r
1302 errln("FromChars threw an overflow exception");
\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
1311 } catch (Exception e) {
\r
1312 errln("FromChars - exception in buffer");
\r
1313 //e.printStackTrace(System.err);
\r
1317 /* Convert the buffer one char at a time to unicode */
\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
1325 result = m_encoder.encode(c, bytes, false);
\r
1326 if(result.isOverflow()){
\r
1327 errln("FromChars single threw an overflow exception");
\r
1329 if(result.isError()){
\r
1330 errln("FromChars single threw an error: "+ result.toString());
\r
1333 if (expectedByteStr.length != bytes.limit()) {
\r
1334 errln("FromChars single len does not match");
\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
1346 } catch (Exception e) {
\r
1347 errln("FromChars - exception in single");
\r
1348 //e.printStackTrace(System.err);
\r
1352 /* Convert one char at a time to unicode */
\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
1360 result = m_encoder.encode(uniVal, bytes, false);
\r
1361 if(result.isOverflow()){
\r
1362 errln("FromChars simple threw an overflow exception");
\r
1364 if(result.isError()){
\r
1365 errln("FromChars simple threw an error: "+ result.toString());
\r
1368 if (bytes.limit() != expectedByteStr.length) {
\r
1369 errln("FromChars Simple len does not match");
\r
1372 if (!bytes.equals(byes)) {
\r
1373 errln("FromChars Simple does not match");
\r
1374 printbytes(bytes);
\r
1378 } catch (Exception e) {
\r
1379 errln("FromChars - exception in single buffer");
\r
1380 //e.printStackTrace(System.err);
\r
1384 errln("Test Simple FromChars " + m_encoding + " --FAILED");
\r
1388 void printchars(CharBuffer buf) {
\r
1390 char[] chars = new char[buf.limit()];
\r
1391 //save the current position
\r
1392 int pos = buf.position();
\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
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
1409 void printbytes(ByteBuffer buf) {
\r
1411 byte[] bytes = new byte[buf.limit()];
\r
1412 //save the current position
\r
1413 int pos = buf.position();
\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
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
1430 public boolean equals(CharBuffer buf, String str) {
\r
1431 return equals(buf, str.toCharArray());
\r
1433 public boolean equals(CharBuffer buf, CharBuffer str) {
\r
1434 if (buf.limit() != str.limit())
\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
1442 public boolean equals(CharBuffer buf, CharBuffer str, int limit) {
\r
1443 if (limit > buf.limit() || limit > str.limit())
\r
1445 for (int i = 0; i < limit; i++)
\r
1446 if (buf.get(i) != str.get(i))
\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
1456 //reset to old position
\r
1457 buf.position(pos);
\r
1458 return equals(chars, compareTo);
\r
1461 public boolean equals(char[] chars, char[] compareTo) {
\r
1462 if (chars.length != compareTo.length) {
\r
1464 "Length does not match chars: "
\r
1467 + compareTo.length);
\r
1470 boolean result = true;
\r
1471 for (int i = 0; i < chars.length; i++) {
\r
1472 if (chars[i] != compareTo[i]) {
\r
1477 + hex(compareTo[i])
\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
1493 //reset to old position
\r
1494 buf.position(pos);
\r
1495 return equals(chars, compareTo);
\r
1497 public boolean equals(ByteBuffer buf, ByteBuffer compareTo) {
\r
1498 if (buf.limit() != compareTo.limit())
\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
1506 public boolean equals(ByteBuffer buf, ByteBuffer compareTo, int limit) {
\r
1507 if (limit > buf.limit() || limit > compareTo.limit())
\r
1509 for (int i = 0; i < limit; i++)
\r
1510 if (buf.get(i) != compareTo.get(i))
\r
1514 public boolean equals(byte[] chars, byte[] compareTo) {
\r
1515 if (false/*chars.length != compareTo.length*/) {
\r
1517 "Length does not match chars: "
\r
1520 + compareTo.length);
\r
1523 boolean result = true;
\r
1524 for (int i = 0; i < chars.length; i++) {
\r
1525 if (chars[i] != compareTo[i]) {
\r
1530 + hex(compareTo[i])
\r
1542 public void TestCallback(String encoding) throws Exception {
\r
1563 char[] subChars = { 'P', 'I' };
\r
1567 decoder.replaceWith(new String(subChars));
\r
1568 ByteBuffer mySource = ByteBuffer.wrap(gbSource);
\r
1569 CharBuffer myTarget = CharBuffer.allocate(5);
\r
1571 decoder.decode(mySource, myTarget, true);
\r
1572 char[] expectedResult =
\r
1573 { '\u22A6', '\u22A7', '\u22A8', '\u0050', '\u0049', };
\r
1575 if (!equals(myTarget, new String(expectedResult))) {
\r
1576 errln("Test callback GB18030 to Unicode : FAILED");
\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
1591 if(m_encoder==null){
\r
1592 warnln("Could not load encoder.");
\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
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
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
1614 logln("Total Number of chasets = " + map.size());
\r
1617 public void TestJavaCanonicalNameOnAvailableCharsets() {
\r
1618 CharsetProviderICU provider = new CharsetProviderICU();
\r
1619 Iterator allCharsets = provider.charsets();
\r
1620 String errorMessage = null;
\r
1622 while (allCharsets.hasNext()) {
\r
1623 Charset _chset = (Charset)allCharsets.next();
\r
1624 Charset chset = Charset.forName(_chset.name());
\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
1630 errorMessage = errorMessage + "\nError: Charset.forName( " + _chset.name() + " ) returned " + chset + " instead of " + _chset;
\r
1635 if (errorMessage != null) {
\r
1636 errln(errorMessage);
\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
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
1655 CharsetEncoder encoder = cs.newEncoder();
\r
1656 if(encoder!=null){
\r
1657 logln("Creation of encoder succeeded. "+cs.toString());
\r
1659 }catch(Exception ex){
\r
1660 errln("Could not instantiate encoder for "+charsets[i]+". Error: "+ex.toString());
\r
1663 CharsetDecoder decoder = cs.newDecoder();
\r
1664 if(decoder!=null){
\r
1665 logln("Creation of decoder succeeded. "+cs.toString());
\r
1667 }catch(Exception ex){
\r
1668 errln("Could not instantiate decoder for "+charsets[i]+". Error: "+ex.toString());
\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
1682 public void TestEncoderCreation(){
\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
1689 errln("Error creating charset encoder.");
\r
1691 }catch(Exception e){
\r
1692 warnln("Error creating charset encoder."+ e.toString());
\r
1693 // e.printStackTrace();
\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
1701 errln("Error creating charset encoder.");
\r
1703 }catch(Exception e){
\r
1704 warnln("Error creating charset encoder."+ e.toString());
\r
1707 public void TestSubBytes(){
\r
1709 //create utf-8 decoder
\r
1710 CharsetDecoder decoder = new CharsetProviderICU().charsetForName("utf-8").newDecoder();
\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
1715 ByteBuffer buffer = ByteBuffer.allocate(20);
\r
1717 //add a evil byte to make the byte buffer be malformed input
\r
1718 buffer.put((byte)0xd8);
\r
1720 //put the valid byte array
\r
1721 buffer.put(unibytes);
\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
1733 logln("Output: "+ new String(out.array()) + "("+ hex(out.array())+")");
\r
1734 }catch (CharacterCodingException ex){
\r
1735 errln("Unexpected exception: "+ex.toString());
\r
1739 public void TestImplFlushFailure(){
\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
1751 }catch (Exception ex){
\r
1752 errln("Could not create encoder for iso-2022-jp exception: "+ex.toString());
\r
1756 public void TestISO88591() {
\r
1758 Charset cs = new CharsetProviderICU().charsetForName("iso-8859-1");
\r
1760 CharsetEncoder encoder = cs.newEncoder();
\r
1761 if(encoder!=null){
\r
1762 encoder.canEncode("\uc2a3");
\r
1764 errln("Could not create encoder for iso-8859-1");
\r
1767 errln("Could not create Charset for iso-8859-1");
\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
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
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
1792 /* private void printCB(CharBuffer buf){
\r
1794 while(buf.hasRemaining()){
\r
1795 System.out.println(hex(buf.get()));
\r
1800 public void TestUTF8() throws CharacterCodingException{
\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
1810 CharsetEncoder encoderJDK = Charset.forName("utf-8").newEncoder();
\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
1820 public void TestUTF16Bom(){
\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
1827 CharsetEncoder encoder = cs.newEncoder();
\r
1828 ByteBuffer outBuf = ByteBuffer.allocate(in.length*2+2);
\r
1830 encoder.encode(inBuf, outBuf, true);
\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
1835 while(outBuf.hasRemaining()){
\r
1836 logln("0x"+hex(outBuf.get()));
\r
1838 CharsetDecoder decoder = cs.newDecoder();
\r
1840 CharBuffer rt = CharBuffer.allocate(in.length);
\r
1841 CoderResult cr = decoder.decode(outBuf, rt, true);
\r
1843 errln("Decoding with BOM failed. Error: "+ cr.toString());
\r
1849 Charset utf16 = Charset.forName("UTF-16");
\r
1850 CharsetDecoder dc = utf16.newDecoder();
\r
1851 cr = dc.decode(outBuf, rt, true);
\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
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
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
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
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
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
1892 result = decoder.flush(myTarget);
\r
1894 if (result.isError()) {
\r
1895 if (throwException) {
\r
1896 throw new Exception();
\r
1898 errln("Test complete buffers while decoding failed. " + result.toString());
\r
1901 if (result.isOverflow()) {
\r
1902 if (throwException) {
\r
1903 throw new BufferOverflowException();
\r
1905 errln("Test complete buffers while decoding threw overflow exception");
\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
1920 myTarget.limit(target.position());
\r
1921 mySource.limit(source.position());
\r
1922 mySource.position(source.position());
\r
1924 myTarget.position(0);
\r
1926 int inputLen = mySource.remaining();
\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
1934 result = decoder.decode(mySource, myTarget, false);
\r
1936 if (result.isError()) {
\r
1937 errln("Test small input buffers while decoding failed. " + result.toString());
\r
1940 if (result.isOverflow()) {
\r
1941 if (throwException) {
\r
1942 throw new BufferOverflowException();
\r
1944 errln("Test small input buffers while decoding threw overflow exception");
\r
1949 if (result.isUnderflow() && !equals(myTarget, target, targetLimit)) {
\r
1950 errln("Test small input buffers while decoding " + encoding + " TO Unicode--failed");
\r
1955 myTarget.limit(0);
\r
1956 mySource.limit(0);
\r
1957 mySource.position(source.position());
\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
1969 } else /*if (result.isError())*/ {
\r
1970 errln("Test small output buffers while decoding " + result.toString());
\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
1977 result = decoder.flush(myTarget);
\r
1978 if (result.isError()) {
\r
1979 errln("Test small output buffers while decoding " + result.toString());
\r
1985 if (!equals(myTarget, target, targetLimit)) {
\r
1986 errln("Test small output buffers " + encoding + " TO Unicode failed");
\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
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
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
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
2012 mySource = ByteBuffer.allocateDirect(source.capacity() * 2).asCharBuffer();
\r
2013 myTarget = ByteBuffer.allocateDirect(target.capacity());
\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
2021 logln("Running tests on small input buffers for " + encoding);
\r
2023 myTarget.limit(target.limit());
\r
2024 mySource.limit(source.limit());
\r
2025 mySource.position(source.position());
\r
2026 CoderResult result = null;
\r
2028 result = encoder.encode(mySource, myTarget, true);
\r
2030 result = encoder.flush(myTarget);
\r
2033 if (result.isError()) {
\r
2034 if (throwException) {
\r
2035 throw new Exception();
\r
2037 errln("Test complete while encoding failed. " + result.toString());
\r
2039 if (result.isOverflow()) {
\r
2040 if (throwException) {
\r
2041 throw new BufferOverflowException();
\r
2043 errln("Test complete while encoding threw overflow exception");
\r
2045 if (!equals(myTarget, target, targetLimit)) {
\r
2046 errln("Test complete buffers while encoding for " + encoding + " failed");
\r
2049 logln("Tests complete buffers for " + encoding + " passed");
\r
2056 logln("Running tests on small input buffers for " + encoding);
\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
2070 if (result.isOverflow()) {
\r
2071 if (throwException) {
\r
2072 throw new BufferOverflowException();
\r
2074 errln("Test small input buffers while encoding threw overflow exception");
\r
2077 if (!equals(myTarget, target, targetLimit)) {
\r
2078 errln("Test small input buffers " + encoding + " From Unicode failed");
\r
2080 logln("Tests on small input buffers for " + encoding + " passed");
\r
2084 logln("Running tests on small output buffers for " + encoding);
\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
2093 logln("myTarget.limit: " + myTarget.limit() + " myTarget.capcity: " + myTarget.capacity());
\r
2096 int pos = myTarget.position();
\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
2102 if (result.isError()) {
\r
2103 errln("Test small output buffers while encoding " + result.toString());
\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
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
2119 if (!equals(myTarget, target, targetLimit)) {
\r
2120 errln("Test small output buffers " + encoding + " From Unicode failed.");
\r
2122 logln("Tests on small output buffers for " + encoding + " passed");
\r
2126 public void convertAllTest(ByteBuffer bSource, CharBuffer uSource) throws Exception {
\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
2134 "--Test convertAll() "
\r
2136 + " to Unicode --FAILED");
\r
2138 } catch (Exception e) {
\r
2139 //e.printStackTrace();
\r
2140 errln(e.getMessage());
\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
2150 "--Test convertAll() "
\r
2152 + " to Unicode --FAILED");
\r
2154 } catch (Exception e) {
\r
2155 //e.printStackTrace();
\r
2156 errln("encoder.encode() failed "+ e.getMessage()+" "+e.toString());
\r
2163 public void TestString(ByteBuffer bSource, CharBuffer uSource) throws Exception {
\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
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
2179 } catch (Exception e) {
\r
2180 //e.printStackTrace();
\r
2181 errln(e.getMessage());
\r
2185 /*private void fromUnicodeTest() throws Exception {
\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
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
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
2201 logln("Test Unicode to " + encoding +" passed");
\r
2204 public void TestToUnicode( ) throws Exception {
\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
2210 CharBuffer myTarget = CharBuffer.allocate(uSource.length);
\r
2211 ByteBuffer mySource = ByteBuffer.wrap(getByteArray(gbSource));
\r
2213 CoderResult result = decoder.decode(mySource, myTarget, true);
\r
2214 if (result.isError()) {
\r
2215 errln("Test ToUnicode -- FAILED");
\r
2217 if (!equals(myTarget, uSource)) {
\r
2218 errln("--Test " + encoding + " to Unicode :FAILED");
\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
2231 private void smBufCharset(Charset charset) {
\r
2233 ByteBuffer bTarget = charset.encode(CharBuffer.wrap(uSource));
\r
2234 CharBuffer uTarget =
\r
2235 charset.decode(ByteBuffer.wrap(getByteArray(gbSource)));
\r
2237 if (!equals(uTarget, uSource)) {
\r
2238 errln("Test " + charset.toString() + " to Unicode :FAILED");
\r
2240 if (!equals(bTarget, gbSource)) {
\r
2241 errln("Test " + charset.toString() + " from Unicode :FAILED");
\r
2243 } catch (Exception ex) {
\r
2244 errln("Encountered exception in smBufCharset");
\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
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
2261 // synchronized(charset){
\r
2262 while (!interrupted()) {
\r
2265 } catch (UnsupportedCharsetException ueEx) {
\r
2266 errln(ueEx.toString());
\r
2273 final Thread t2 = new Thread() {
\r
2274 public void run() {
\r
2275 // synchronized(charset){
\r
2276 while (!interrupted()) {
\r
2279 } catch (UnsupportedCharsetException ueEx) {
\r
2280 errln(ueEx.toString());
\r
2291 if (i > 1000000000) {
\r
2294 } catch (Exception e) {
\r
2298 } catch (Exception e) {
\r
2304 } catch (Exception e) {
\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
2317 final Thread t1 = new Thread() {
\r
2318 public void run() {
\r
2319 while (!interrupted()) {
\r
2321 synchronized (encoder) {
\r
2322 smBufEncode(encoder, encoding);
\r
2324 synchronized (decoder) {
\r
2325 smBufDecode(decoder, encoding);
\r
2327 } catch (UnsupportedCharsetException ueEx) {
\r
2328 errln(ueEx.toString());
\r
2334 final Thread t2 = new Thread() {
\r
2335 public void run() {
\r
2336 while (!interrupted()) {
\r
2338 synchronized (encoder) {
\r
2339 smBufEncode(encoder, encoding);
\r
2341 synchronized (decoder) {
\r
2342 smBufDecode(decoder, encoding);
\r
2344 } catch (UnsupportedCharsetException ueEx) {
\r
2345 errln(ueEx.toString());
\r
2354 if (i > 1000000000) {
\r
2357 } catch (Exception e) {
\r
2361 } catch (Exception e) {
\r
2367 } catch (Exception e) {
\r
2373 public void TestMBCS(){
\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
2386 logln("Output: "+ hex(out.array()));
\r
2388 errln("Encode operation failed for encoder: "+encoderICU.toString());
\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
2403 logln("Output: "+ hex(out.array()));
\r
2405 errln("Decode operation failed for encoder: "+decoderICU.toString());
\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
2418 public void TestJB5027() {
\r
2419 CharsetProviderICU provider= new CharsetProviderICU();
\r
2421 Charset fake = provider.charsetForName("doesNotExist");
\r
2423 errln("\"doesNotExist\" returned " + fake);
\r
2425 Charset xfake = provider.charsetForName("x-doesNotExist");
\r
2427 errln("\"x-doesNotExist\" returned " + xfake);
\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
2433 CharsetProviderICU provider= new CharsetProviderICU();
\r
2434 Object[] available = CharsetProviderICU.getAvailableNames();
\r
2435 for(int i=0; i<available.length;i++){
\r
2437 String canon = CharsetProviderICU.getICUCanonicalName((String)available[i]);
\r
2439 // ',' is not allowed by Java's charset name checker
\r
2440 if(canon.indexOf(',')>=0){
\r
2443 Charset cs = provider.charsetForName((String)available[i]);
\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
2455 if(inAliasList == false){
\r
2456 errln("Could not find ICU canonical name: "+canon+ " for java canonical name: "+ available[i]+ " "+ i);
\r
2458 }catch(UnsupportedCharsetException ex){
\r
2459 errln("could no load charset "+ available[i]+" "+ex.getMessage());
\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
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
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
2487 logln(cs.getClass().toString()+ ":" +result.toString());
\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
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
2505 logln(cs.getClass().toString()+ ":" +result.toString());
\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
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
2521 errln("ICU's decoder did not return the same result as Sun. ICU: "+ir.toString()+" Sun: "+jr.toString());
\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
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
2541 errln("ICU's decoder did not return the same result as Sun. ICU: "+ie.toString()+" Sun: "+je.toString());
\r
2544 private Exception execConvertAll(Charset cs){
\r
2545 ByteBuffer in = ByteBuffer.allocate(400);
\r
2547 while(in.position()!=in.capacity()){
\r
2548 in.put((byte)0xD8);
\r
2550 in.put((byte)0xDC);
\r
2554 in.limit(in.position());
\r
2556 CharsetDecoder decoder = cs.newDecoder();
\r
2557 decoder.onMalformedInput(CodingErrorAction.IGNORE);
\r
2558 decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
\r
2560 CharBuffer out = decoder.decode(in);
\r
2562 logln(cs.toString()+" encoing succeeded as expected!");
\r
2564 }catch ( Exception ex){
\r
2565 errln("Did not get expected exception for encoding: "+cs.toString());
\r
2570 public void TestUTF32BOM(){
\r
2572 Charset cs = (new CharsetProviderICU()).charsetForName("UTF-32");
\r
2573 char[] in = new char[] { 0xd800, 0xdc00,
\r
2581 CharBuffer inBuf = CharBuffer.allocate(in.length);
\r
2583 CharsetEncoder encoder = cs.newEncoder();
\r
2584 ByteBuffer outBuf = ByteBuffer.allocate(in.length*4+4);
\r
2586 encoder.encode(inBuf, outBuf, true);
\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
2592 while(outBuf.hasRemaining()){
\r
2593 logln("0x"+hex(outBuf.get()));
\r
2595 CharsetDecoder decoder = cs.newDecoder();
\r
2596 outBuf.limit(outBuf.position());
\r
2598 CharBuffer rt = CharBuffer.allocate(in.length);
\r
2599 CoderResult cr = decoder.decode(outBuf, rt, true);
\r
2601 errln("Decoding with BOM failed. Error: "+ cr.toString());
\r
2607 Charset utf16 = Charset.forName("UTF-32");
\r
2608 CharsetDecoder dc = utf16.newDecoder();
\r
2609 cr = dc.decode(outBuf, rt, true);
\r
2611 }catch(UnsupportedCharsetException ex){
\r
2612 // swallow the expection.
\r
2620 /*The following two methods provides the option of exceptions when Decoding
\r
2621 * and Encoding if needed for testing purposes.
\r
2623 private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target) {
\r
2624 smBufDecode(decoder, encoding, source, target, true);
\r
2626 private void smBufDecode(CharsetDecoder decoder, String encoding, ByteBuffer source, CharBuffer target, boolean backedByArray) {
\r
2628 smBufDecode(decoder, encoding, source, target, false, false, backedByArray);
\r
2630 catch (Exception ex) {
\r
2631 System.out.println("!exception!");
\r
2634 private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target) {
\r
2635 smBufEncode(encoder, encoding, source, target, true);
\r
2637 private void smBufEncode(CharsetEncoder encoder, String encoding, CharBuffer source, ByteBuffer target, boolean backedByArray) {
\r
2639 smBufEncode(encoder, encoding, source, target, false, false);
\r
2641 catch (Exception ex) {
\r
2642 System.out.println("!exception!");
\r
2645 //Test CharsetICUProvider
\r
2646 public void TestNullCanonicalName() {
\r
2647 String enc = null;
\r
2648 String canonicalName = CharsetProviderICU.getICUCanonicalName(enc);
\r
2650 if (canonicalName != null) {
\r
2651 errln("getICUCanonicalName return a non-null string for given null string");
\r
2654 public void TestGetAllNames() {
\r
2655 String[] names = null;
\r
2657 names = CharsetProviderICU.getAllNames();
\r
2659 if (names == null) {
\r
2660 errln("getAllNames returned a null string.");
\r
2664 public void TestCharsetContains() {
\r
2667 CharsetProvider provider = new CharsetProviderICU();
\r
2668 Charset cs1 = provider.charsetForName("UTF-32");
\r
2669 Charset cs2 = null;
\r
2671 test = cs1.contains(cs2);
\r
2673 if (test != false) {
\r
2674 errln("Charset.contains returned true for a null charset.");
\r
2677 cs2 = CharsetICU.forNameICU("UTF-32");
\r
2679 test = cs1.contains(cs2);
\r
2681 if (test != true) {
\r
2682 errln("Charset.contains returned false for an identical charset.");
\r
2685 cs2 = provider.charsetForName("UTF-8");
\r
2687 test = cs1.contains(cs2);
\r
2689 if (test != false) {
\r
2690 errln("Charset.contains returned true for a different charset.");
\r
2693 public void TestCharsetICUNullCharsetName() {
\r
2694 String charsetName = null;
\r
2697 CharsetICU.forNameICU(charsetName);
\r
2698 errln("CharsetICU.forName should have thown an exception after getting a null charsetName.");
\r
2700 catch(Exception ex) {
\r
2704 //Test CharsetASCII
\r
2705 public void TestCharsetASCIIOverFlow() {
\r
2706 int byteBufferLimit;
\r
2707 int charBufferLimit;
\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
2714 CharBuffer charBuffer = CharBuffer.allocate(0x90);
\r
2715 ByteBuffer byteBuffer = ByteBuffer.allocate(0x90);
\r
2717 CharBuffer charBufferTest = CharBuffer.allocate(0xb0);
\r
2718 ByteBuffer byteBufferTest = ByteBuffer.allocate(0xb0);
\r
2720 for(int j=0;j<=0x7f; j++){
\r
2721 charBuffer.put((char)j);
\r
2722 byteBuffer.put((byte)j);
\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
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
2736 smBufDecode(decoder, "ASCII", byteBuffer, charBufferTest, true, false);
\r
2737 errln("Overflow exception while decoding ASCII should have been thrown.");
\r
2739 catch(Exception ex) {
\r
2742 smBufEncode(encoder, "ASCII", charBuffer, byteBufferTest, true, false);
\r
2743 errln("Overflow exception while encoding ASCII should have been thrown.");
\r
2745 catch (Exception ex) {
\r
2748 // For better code coverage
\r
2749 /* For better code coverage */
\r
2750 byte byteout[] = {
\r
2754 (char)0x0001, (char)0x0002
\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
2760 if (!(encoder.encode(cb2, bb, true)).isOverflow()) {
\r
2761 errln("Overflow error while encoding ASCII should have occurred.");
\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
2772 CharBuffer us = CharBuffer.allocate(0x100);
\r
2773 ByteBuffer bs = ByteBuffer.allocate(0x100);
\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
2782 bs.limit(bs.position());
\r
2784 us.limit(us.position());
\r
2787 smBufDecode(decoder, "UTF-7", bs, us);
\r
2788 smBufEncode(encoder, "UTF-7", us, bs);
\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
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
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
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
2810 ccbs.limit(ccbs.position());
\r
2812 ccus.limit(ccus.position());
\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
2819 catch (Exception ex) {
\r
2825 //test for illegal base64 character
\r
2826 ccbs.put((byte)0x2b); ccbs.put((byte)0xff);
\r
2827 ccus.put((char)0x0000);
\r
2829 ccbs.limit(ccbs.position());
\r
2831 ccus.limit(ccus.position());
\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
2838 catch (Exception ex) {
\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
2848 ccbs.limit(ccbs.position());
\r
2850 ccus.limit(ccus.position());
\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
2857 catch (Exception ex) {
\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
2867 ccbs.limit(ccbs.position());
\r
2869 ccus.limit(ccus.position());
\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
2876 catch (Exception ex) {
\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
2886 ccbs.limit(ccbs.position());
\r
2888 ccus.limit(ccus.position());
\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
2895 catch (Exception ex) {
\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
2905 ccbs.limit(ccbs.position());
\r
2907 ccus.limit(ccus.position());
\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
2914 catch (Exception ex) {
\r
2920 //test for overflow buffer error
\r
2921 ccbs.put((byte)0x2b); ccbs.put((byte)0x49);
\r
2923 ccbs.limit(ccbs.position());
\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
2932 catch (Exception ex) {
\r
2938 //test for overflow buffer error
\r
2939 ccbs.put((byte)0x0c); ccbs.put((byte)0x0c);
\r
2941 ccbs.limit(ccbs.position());
\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
2950 catch (Exception ex) {
\r
2952 //end of charset decoder code coverage code
\r
2954 //start of charset encoder code coverage code
\r
2957 //test for overflow buffer error
\r
2958 ccus.put((char)0x002b);
\r
2959 ccbs.put((byte)0x2b);
\r
2961 ccbs.limit(ccbs.position());
\r
2963 ccus.limit(ccus.position());
\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
2970 catch (Exception ex) {
\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
2980 ccbs.limit(ccbs.position());
\r
2982 ccus.limit(ccus.position());
\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
2989 catch (Exception ex) {
\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
3000 ccus.limit(ccus.position());
\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
3007 catch (Exception ex) {
\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
3018 ccus.limit(ccus.position());
\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
3025 catch (Exception ex) {
\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
3036 ccus.limit(ccus.position());
\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
3043 catch (Exception ex) {
\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
3054 ccus.limit(ccus.position());
\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
3061 catch (Exception ex) {
\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
3073 ccus.limit(ccus.position());
\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
3080 catch (Exception ex) {
\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
3091 ccus.limit(ccus.position());
\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
3098 catch (Exception ex) {
\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
3109 ccus.limit(ccus.position());
\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
3116 catch (Exception ex) {
\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
3127 ccus.limit(ccus.position());
\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
3134 catch (Exception ex) {
\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
3144 ccbs.limit(ccbs.position());
\r
3146 ccus.limit(ccus.position());
\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
3157 //test for overflow buffer error
\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
3162 ccbs.limit(ccbs.position());
\r
3164 ccus.limit(ccus.position());
\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
3172 //end of charset encoder code coverage code
\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
3181 CharBuffer us = CharBuffer.allocate(0x100);
\r
3182 ByteBuffer bs = ByteBuffer.allocate(0x100);
\r
3183 ByteBuffer bsr = ByteBuffer.allocate(0x100);
\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
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
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
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
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
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
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
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
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
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
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
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
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
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
3325 bs.limit(bs.position());
\r
3327 us.limit(us.position());
\r
3329 bsr.limit(bsr.position());
\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
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
3345 /* DEV */ (char)0x0904,
\r
3346 /* PNJ */ (char)0x0A01, (char)0x0A03, (char)0x0A33, (char)0x0A70
\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
3353 us = CharBuffer.allocate(u_pts.length);
\r
3354 bs = ByteBuffer.allocate(b_pts.length);
\r
3358 bs.limit(bs.position());
\r
3360 us.limit(us.position());
\r
3364 smBufDecode(decoder, "ISCII-update", bs, us, true, true);
\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
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
3376 //start of charset decoder code coverage code
\r
3377 //test overflow buffer
\r
3378 ccbs.put((byte)0x49);
\r
3380 ccbs.limit(ccbs.position());
\r
3386 smBufDecode(decoder, "ISCII-CC-DE-1", ccbs, ccus, true, false);
\r
3387 errln("Exception while decoding ISCII should have been thrown.");
\r
3389 catch (Exception ex) {
\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
3399 ccbs.limit(ccbs.position());
\r
3401 ccus.limit(ccus.position());
\r
3405 smBufDecode(decoder, "ISCII-CC-DE-2", ccbs, ccus, true, false);
\r
3406 errln("Exception while decoding ISCII should have been thrown.");
\r
3408 catch (Exception ex) {
\r
3411 //end of charset decoder code coverage code
\r
3416 //start of charset encoder code coverage code
\r
3417 //test ascii overflow buffer
\r
3418 ccus.put((char)0x41);
\r
3420 ccus.limit(ccus.position());
\r
3426 smBufEncode(encoder, "ISCII-CC-EN-1", ccus, ccbs, true, false);
\r
3427 errln("Exception while encoding ISCII should have been thrown.");
\r
3429 catch (Exception ex) {
\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
3439 ccus.limit(ccus.position());
\r
3441 ccbs.limit(ccbs.position());
\r
3445 smBufEncode(encoder, "ISCII-CC-EN-2", ccus, ccbs, true, false);
\r
3446 errln("Exception while encoding ISCII should have been thrown.");
\r
3448 catch (Exception ex) {
\r
3454 //test surrogate malform
\r
3455 ccus.put((char)0x06E3);
\r
3456 ccbs.put((byte)0x00);
\r
3458 ccus.limit(ccus.position());
\r
3460 ccbs.limit(ccbs.position());
\r
3464 smBufEncode(encoder, "ISCII-CC-EN-3", ccus, ccbs, true, false);
\r
3465 errln("Exception while encoding ISCII should have been thrown.");
\r
3467 catch (Exception ex) {
\r
3473 //test surrogate malform
\r
3474 ccus.put((char)0xD801); ccus.put((char)0xDD01);
\r
3475 ccbs.put((byte)0x00);
\r
3477 ccus.limit(ccus.position());
\r
3479 ccbs.limit(ccbs.position());
\r
3483 smBufEncode(encoder, "ISCII-CC-EN-4", ccus, ccbs, true, false);
\r
3484 errln("Exception while encoding ISCII should have been thrown.");
\r
3486 catch (Exception ex) {
\r
3492 //test trail surrogate malform
\r
3493 ccus.put((char)0xDD01);
\r
3494 ccbs.put((byte)0x00);
\r
3496 ccus.limit(ccus.position());
\r
3498 ccbs.limit(ccbs.position());
\r
3502 smBufEncode(encoder, "ISCII-CC-EN-5", ccus, ccbs, true, false);
\r
3503 errln("Exception while encoding ISCII should have been thrown.");
\r
3505 catch (Exception ex) {
\r
3511 //test lead surrogates malform
\r
3512 ccus.put((char)0xD801); ccus.put((char)0xD802);
\r
3513 ccbs.put((byte)0x00);
\r
3515 ccus.limit(ccus.position());
\r
3517 ccbs.limit(ccbs.position());
\r
3521 smBufEncode(encoder, "ISCII-CC-EN-6", ccus, ccbs, true, false);
\r
3522 errln("Exception while encoding ISCII should have been thrown.");
\r
3524 catch (Exception ex) {
\r
3530 //test overflow buffer
\r
3531 ccus.put((char)0x0901);
\r
3532 ccbs.put((byte)0x00);
\r
3534 ccus.limit(ccus.position());
\r
3536 ccbs.limit(ccbs.position());
\r
3539 cs = provider.charsetForName("ISCII,version=0");
\r
3540 encoder = cs.newEncoder();
\r
3543 smBufEncode(encoder, "ISCII-CC-EN-7", ccus, ccbs, true, false);
\r
3544 errln("Exception while encoding ISCII should have been thrown.");
\r
3546 catch (Exception ex) {
\r
3548 //end of charset encoder code coverage code
\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
3558 CharBuffer us = CharBuffer.allocate(0x20);
\r
3559 ByteBuffer bs = ByteBuffer.allocate(0x20);
\r
3561 us.put((char)0x00A3); us.put((char)0x2020); us.put((char)0x41);
\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
3567 bs.limit(bs.position());
\r
3569 us.limit(us.position());
\r
3572 smBufDecode(decoder, "IMAP", bs, us);
\r
3573 smBufEncode(encoder, "IMAP", us, bs);
\r
3575 //the rest of the code in this method is for better code coverage
\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
3584 bs.limit(bs.position());
\r
3586 us.limit(us.position());
\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
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
3602 bs.limit(bs.position());
\r
3604 us.limit(us.position());
\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
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
3620 bs.limit(bs.position());
\r
3622 us.limit(us.position());
\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
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
3639 bs.limit(bs.position());
\r
3641 us.limit(us.position());
\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
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
3658 bs.limit(bs.position());
\r
3660 us.limit(us.position());
\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
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
3677 bs.limit(bs.position());
\r
3679 us.limit(us.position());
\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
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
3696 bs.limit(bs.position());
\r
3698 us.limit(us.position());
\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
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
3715 bs.limit(bs.position());
\r
3717 us.limit(us.position());
\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
3726 us = CharBuffer.allocate(0x08);
\r
3727 bs = ByteBuffer.allocate(0x08);
\r
3729 //test flushing buffer overflow
\r
3730 us.put((char)0x0061);
\r
3731 bs.put((byte)0x61); bs.put((byte)0x00);
\r
3733 bs.limit(bs.position());
\r
3735 us.limit(us.position());
\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
3743 //end of charset encoder code coverage
\r
3745 us = CharBuffer.allocate(0x10);
\r
3746 bs = ByteBuffer.allocate(0x10);
\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
3753 bs.limit(bs.position());
\r
3755 us.limit(us.position());
\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
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
3772 bs.limit(bs.position());
\r
3774 us.limit(us.position());
\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
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
3792 bs.limit(bs.position());
\r
3794 us.limit(us.position());
\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
3802 //end of charset decoder coder coverage
\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
3813 CharBuffer us = CharBuffer.allocate(0x10);
\r
3814 ByteBuffer bs = ByteBuffer.allocate(0x10);
\r
3817 //test malform surrogate
\r
3818 us.put((char)0xD901);
\r
3819 bs.put((byte)0x00);
\r
3821 bs.limit(bs.position());
\r
3823 us.limit(us.position());
\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
3835 //test malform surrogate
\r
3836 us.put((char)0xD901); us.put((char)0xD902);
\r
3837 bs.put((byte)0x00);
\r
3839 bs.limit(bs.position());
\r
3841 us.limit(us.position());
\r
3844 result = encoder.encode(us, bs, true);
\r
3846 if (!result.isError() && !result.isOverflow()) {
\r
3847 errln("Error while encoding UTF32LE (2) should have occurred.");
\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
3858 bs.limit(bs.position());
\r
3860 us.limit(us.position());
\r
3863 result = encoder.encode(us, bs, true);
\r
3865 if (!result.isError() && !result.isOverflow()) {
\r
3866 errln("Error while encoding UTF32LE (3) should have occurred.");
\r
3872 //test malform lead surrogate
\r
3873 us.put((char)0xD90D); us.put((char)0xD90E);
\r
3874 bs.put((byte)0x00);
\r
3876 bs.limit(bs.position());
\r
3878 us.limit(us.position());
\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
3890 //test overflow buffer
\r
3891 us.put((char)0x0061);
\r
3892 bs.put((byte)0x00);
\r
3894 bs.limit(bs.position());
\r
3896 us.limit(us.position());
\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
3908 //test malform trail surrogate
\r
3909 us.put((char)0xDD01);
\r
3910 bs.put((byte)0x00);
\r
3912 bs.limit(bs.position());
\r
3914 us.limit(us.position());
\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
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
3932 // Test for malform and change fromUChar32 for next call
\r
3935 (char)0xDC01, (char)0xDC02, (char)0xDC03,
\r
3936 (char)0xD901, (char)0xD902
\r
3940 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
\r
3943 CharBuffer us = CharBuffer.allocate(u_pts1.length);
\r
3944 ByteBuffer bs = ByteBuffer.allocate(b_pts1.length);
\r
3954 result = encoder.encode(us, bs, true);
\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
3961 // Test for malform surrogate from previous buffer
\r
3967 result = encoder.encode(us, bs, true);
\r
3969 if (!result.isMalformed()) {
\r
3970 errln("Error while encoding UTF-16LE (2) should have occured.");
\r
3973 // Test for malform trail surrogate
\r
3981 result = encoder.encode(us, bs, true);
\r
3988 result = encoder.encode(us, bs, true);
\r
3990 if (!result.isMalformed()) {
\r
3991 errln("Error while encoding UTF-16LE (3) should have occured.");
\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
4003 //start of decoding code coverage
\r
4004 char us_array[] = {
\r
4005 0x0000, 0x0000, 0x0000, 0x0000,
\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
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
4020 CharBuffer us = CharBuffer.allocate(us_array.length);
\r
4021 ByteBuffer bs = ByteBuffer.allocate(bs_array1.length);
\r
4024 bs.put(bs_array1);
\r
4026 us.limit(us.position());
\r
4028 bs.limit(bs.position());
\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
4037 decoder = cs.newDecoder();
\r
4039 bs = ByteBuffer.allocate(bs_array2.length);
\r
4040 bs.put(bs_array2);
\r
4044 bs.limit(bs.position());
\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
4054 //Test malform exception
\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
4061 us.limit(us.position());
\r
4063 bs.limit(bs.position());
\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
4072 //Test BOM testing
\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
4079 us.limit(us.position());
\r
4081 bs.limit(bs.position());
\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
4090 //end of decoding code coverage
\r
4092 //start of encoding code coverage
\r
4093 us = CharBuffer.allocate(0x10);
\r
4094 bs = ByteBuffer.allocate(0x10);
\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
4100 us.limit(us.position());
\r
4102 bs.limit(bs.position());
\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
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
4118 us.limit(us.position());
\r
4120 bs.limit(bs.position());
\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
4131 //test malform surrogate
\r
4132 us.put((char)0x0000); us.put((char)0xD902);
\r
4134 us.limit(us.position());
\r
4136 bs.limit(bs.position());
\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
4147 //test malform surrogate
\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
4152 us.limit(us.position());
\r
4154 bs.limit(bs.position());
\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
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
4169 us.limit(us.position());
\r
4171 bs.limit(bs.position());
\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
4182 //test malform surrogate
\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
4187 us.limit(us.position());
\r
4189 bs.limit(bs.position());
\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
4196 //end of encoding code coverage
\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
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
4213 us.limit(us.position());
\r
4215 bs.limit(bs.position());
\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
4223 // test overflow buffer handling in CharsetDecoderICU
\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
4234 errln("Overflow buffer error while decoding UTF32LE should have occurred.");
\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
4243 us.limit(us.position());
\r
4245 bs.limit(bs.position());
\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
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
4261 us.limit(us.position());
\r
4263 bs.limit(bs.position());
\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
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
4280 us.limit(us.position());
\r
4282 bs.limit(bs.position());
\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
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
4298 us.limit(us.position());
\r
4300 bs.limit(bs.position());
\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
4308 //end of decode UTF32LE
\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
4319 us.limit(us.position());
\r
4321 bs.limit(bs.position());
\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
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
4336 us.limit(us.position());
\r
4338 bs.limit(bs.position());
\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
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
4354 us.limit(us.position());
\r
4356 bs.limit(bs.position());
\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
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
4373 us.limit(us.position());
\r
4375 bs.limit(bs.position());
\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
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
4390 us.limit(us.position());
\r
4392 bs.limit(bs.position());
\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
4401 //end of decode UTF32BE
\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
4411 CharBuffer us = CharBuffer.allocate(0x10);
\r
4412 ByteBuffer bs = ByteBuffer.allocate(0x10);
\r
4418 //encode and decode using read only buffer
\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
4427 us.limit(limit_us);
\r
4429 bs.limit(limit_bs);
\r
4431 bs2 = bs.asReadOnlyBuffer();
\r
4432 us2 = us.asReadOnlyBuffer();
\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
4439 us2.limit(limit_us);
\r
4441 bs.limit(limit_bs);
\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
4452 //test overflow buffer while encoding
\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
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
4470 us2.limit(limit_us);
\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
4481 us2.limit(limit_us);
\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
4492 us2.limit(limit_us);
\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
4501 //not readonly buffer
\r
4504 us.limit(limit_us);
\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
4515 us.limit(limit_us);
\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
4526 us.limit(limit_us);
\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
4537 us.limit(limit_us + 1);
\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
4549 //test encoding 4 byte characters
\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
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
4566 us2.limit(limit_us);
\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
4577 us2.limit(limit_us);
\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
4588 us2.limit(limit_us);
\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
4600 //decoding code coverage
\r
4601 //test malform error
\r
4603 bs.put((byte)0xC0); bs.put((byte)0xC0);
\r
4604 us.put((char)0x0000);
\r
4605 bs2 = bs.asReadOnlyBuffer();
\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
4625 result = decoder.decode(bs2, us, true);
\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
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
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
4655 //test malform string
\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
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
4674 bs.put((byte)0xF3); bs.put((byte)0xB4); bs.put((byte)0x8A); bs.put((byte)0x8C);
\r
4675 bs2 = bs.asReadOnlyBuffer();
\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
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
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
4714 bs.put((byte)0x41); bs.put((byte)0x42);
\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
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
4734 CharBuffer us = CharBuffer.allocate(0x10);
\r
4735 ByteBuffer bs = ByteBuffer.allocate(0x10);
\r
4737 //test flush buffer and malform string
\r
4738 bs.put((byte)0xFF);
\r
4739 us.put((char)0x0000);
\r
4741 us.limit(us.position());
\r
4743 bs.limit(bs.position());
\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
4755 us.put((char)0xD902); us.put((char)0xDD01); us.put((char)0x0041);
\r
4762 result = encoder.encode(us, bs, true);
\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
4775 //test overflow buffer
\r
4777 decoder = provider.charsetForName("UTF-16BE").newDecoder();
\r
4779 bs.put((byte)0xFF); bs.put((byte)0xFE); bs.put((byte)0x41);
\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
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
4798 byte bytearray[] = {
\r
4799 (byte)0x1b, (byte)0x24, (byte)0x29, (byte)0x43, (byte)0x41, (byte)0x42,
\r
4801 char chararray[] = {
\r
4804 ByteBuffer bb = ByteBuffer.wrap(bytearray);
\r
4805 CharBuffer cb = CharBuffer.wrap(chararray);
\r
4807 result = decoder.decode(bb, cb, true);
\r
4809 if (!result.isOverflow()) {
\r
4810 errln("Overflow buffer while decoding ISO-2022-KR should have occurred.");
\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
4820 byte bytearray[] = {
\r
4821 (byte)0x1b, (byte)0x24, (byte)0x28, (byte)0x44, (byte)0x0A, (byte)0x41,
\r
4823 char chararray[] = {
\r
4826 ByteBuffer bb = ByteBuffer.wrap(bytearray);
\r
4827 CharBuffer cb = CharBuffer.wrap(chararray);
\r
4829 result = decoder.decode(bb, cb, true);
\r
4831 if (!result.isOverflow()) {
\r
4832 errln("Overflow buffer while decoding ISO-2022-KR should have occurred.");
\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
4842 byte bytearray[] = {
\r
4845 char chararray[] = {
\r
4849 ByteBuffer bb = ByteBuffer.wrap(bytearray);
\r
4850 CharBuffer cb = CharBuffer.wrap(chararray);
\r
4852 result = decoder.decode(bb, cb, true);
\r
4853 result = decoder.flush(cb);
\r
4855 if (result.isError()) {
\r
4856 errln("Error occurred while decoding US-ASCII.");
\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
4868 String context3[] = {
\r
4873 // Testing encoder escape callback
\r
4874 String context1[] = {
\r
4880 char chararray[] = {
\r
4883 ByteBuffer bb = ByteBuffer.allocate(20);
\r
4884 CharBuffer cb = CharBuffer.wrap(chararray);
\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
4891 ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_ESCAPE, context1[i]); // This callback is valid.
\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
4899 // Testing encoder skip callback
\r
4900 for (int i = 0; i < context3.length; i++) {
\r
4904 ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_SKIP, context3[i]);
\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
4912 // Testing encoder sub callback
\r
4913 for (int i = 0; i < context3.length; i++) {
\r
4917 ((CharsetEncoderICU)encoder).setFromUCallback(CoderResult.unmappableForLength(1), CharsetCallback.FROM_U_CALLBACK_SUBSTITUTE, context3[i]);
\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
4925 // Testing decoder escape callback
\r
4926 String context2[] = {
\r
4932 byte bytearray[] = {
\r
4933 (byte)0x1b, (byte)0x2e, (byte)0x43
\r
4935 bb = ByteBuffer.wrap(bytearray);
\r
4936 cb = CharBuffer.allocate(20);
\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
4943 ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.malformedForLength(1), CharsetCallback.TO_U_CALLBACK_ESCAPE, context2[i]); // This callback is valid.
\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
4951 // Testing decoder skip callback
\r
4952 for (int i = 0; i < context3.length; i++) {
\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
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
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
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
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
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
5006 byte bytearray[] = {
\r
5007 0x01, 0x02, 0x03, 0x0a,
\r
5008 0x01, 0x02, 0x03, 0x0b,
\r
5009 0x01, 0x02, 0x03, 0x0d,
\r
5012 // set the callback for overflow errors
\r
5013 ((CharsetDecoderICU)decoder).setToUCallback(CoderResult.OVERFLOW, CharsetCallback.TO_U_CALLBACK_STOP, null);
\r
5015 ByteBuffer bb = ByteBuffer.wrap(bytearray);
\r
5016 CharBuffer cb = CharBuffer.allocate(10);
\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
5026 result = decoder.decode(bb, cb, false);
\r
5027 if (result.isError()) {
\r
5028 errln("Error occurred while decoding: " + charsetName + " with error: " + result);
\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
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
5043 cb = CharBuffer.wrap(chararray);
\r
5044 bb = ByteBuffer.allocate(10);
\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
5053 result = encoder.encode(cb, bb, false);
\r
5054 if (result.isError()) {
\r
5055 errln("Error occurred while encoding: " + charsetName + " with error: " + result);
\r
5058 result = encoder.encode(cb, bb, true);
\r
5059 if (!result.isError()) {
\r
5060 errln("Error should have occurred while encoding: " + charsetName);
\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
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
5080 char germanUTF16[]={
\r
5081 0x00d6, 0x006c, 0x0020, 0x0066, 0x006c, 0x0069, 0x0065, 0x00df, 0x0074
\r
5084 byte germanSCSU[]={
\r
5085 (byte)0xd6, 0x6c, 0x20, 0x66, 0x6c, 0x69, 0x65,(byte) 0xdf, 0x74
\r
5088 char russianUTF16[]={
\r
5089 0x041c, 0x043e, 0x0441, 0x043a, 0x0432, 0x0430
\r
5092 byte russianSCSU[]={
\r
5093 0x12, (byte)0x9c,(byte)0xbe,(byte) 0xc1, (byte)0xba, (byte)0xb2, (byte)0xb0
\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
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
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
5136 //String[] codePoints = {"allFeatures", "german","russian","japanese"};
\r
5137 byte[][] fromUnicode={allFeaturesSCSU,germanSCSU,russianSCSU,japaneseSCSU};
\r
5138 char[][] toUnicode = {allFeaturesUTF16, germanUTF16,russianUTF16,japaneseUTF16};
\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
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
5152 ByteBuffer encoderResult = encode.encode(encoderBuffer);
\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
5160 // Test overflow for code coverage reasons
\r
5162 ByteBuffer test = encoderResult;
\r
5164 CharBuffer smallBuffer = CharBuffer.allocate(11);
\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
5171 }catch(Exception e){
\r
5172 errln("Exception while converting SCSU thrown: " + e);
\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
5181 byte[] illegalDecoderSrc1 = { (byte)0x41, (byte)0xdf, (byte)0x0c };
\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
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
5195 "\udBff\udFff", /* largest surrogate pair*/
\r
5198 "Hello \u9292 \u9192 World!",
\r
5199 "Hell\u0429o \u9292 \u9192 W\u00e4rld!",
\r
5200 "Hell\u0429o \u9292 \u9292W\u00e4rld!",
\r
5202 "\u0648\u06c8", /* catch missing reset*/
\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
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
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
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
5226 "", /* empty input*/
\r
5227 "\u0000", /* smallest BMP character*/
\r
5228 "\uFFFF", /* largest BMP character*/
\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
5239 for (int i = 0; i < ccSrc.length; i++) {
\r
5240 CharBuffer ubuf = CharBuffer.wrap(ccSrc[i]);
\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
5249 trgt.limit(trgt.position());
\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
5256 test.limit(test.position());
\r
5258 if (!equals(test, ubuf)) {
\r
5259 errln("Roundtrip failed for " + charset.name() + " in test for code coverage[" + i + "].");
\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
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
5309 CharBuffer monkeyCB = CharBuffer.wrap(monkeyIn);
\r
5311 ByteBuffer monkeyBB = encode.encode(monkeyCB);
\r
5312 /* CharBuffer monkeyEndResult =*/ decode.decode(monkeyBB);
\r
5314 } catch (Exception ex) {
\r
5315 errln("Exception thrown while encoding/decoding monkey test in SCSU: " + ex);
\r
5320 char[] malformedSequence = {
\r
5321 0xD899, 0xDC7F, 0xDC88, 0xDC88, 0xD888, 0xDDF9
\r
5324 CharBuffer malformedSrc = CharBuffer.wrap(malformedSequence);
\r
5327 encode.encode(malformedSrc);
\r
5328 errln("Malformed error should have thrown an exception.");
\r
5329 } catch (Exception ex) {
\r
5332 // Test overflow buffer
\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
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
5351 /* Test for BOCU1 converter*/
\r
5352 public void TestBOCU1Converter(){
\r
5354 0xFEFF, 0x0061, 0x0062, 0x0020, // 0
\r
5355 0x0063, 0x0061, 0x000D, 0x000A,
\r
5357 0x0020, 0x0000, 0x00DF, 0x00E6, // 8
\r
5358 0x0930, 0x0020, 0x0918, 0x0909,
\r
5360 0x3086, 0x304D, 0x0020, 0x3053, // 16
\r
5361 0x4000, 0x4E00, 0x7777, 0x0020,
\r
5363 0x9FA5, 0x4E00, 0xAC00, 0xBCDE, // 24
\r
5364 0x0020, 0xD7A3, 0xDC00, 0xD800,
\r
5366 0xD800, 0xDC00, 0xD845, 0xDDDD, // 32
\r
5367 0xDBBB, 0xDDEE, 0x0020, 0xDBFF,
\r
5369 0xDFFF, 0x0001, 0x0E40, 0x0020, // 40
\r
5373 byte sampleText[]={ // from cintltst/bocu1tst.c/TestBOCU1 text 1
\r
5376 0x28, // from source offset 0
\r
5377 0x24, 0x1E, 0x52, (byte) 0xB2, 0x20,
\r
5384 0x00, (byte) 0xD0, 0x6C, (byte) 0xB6, (byte) 0xD8, (byte) 0xA5,
\r
5390 0x6D, 0x20, 0x73, (byte) 0xE0, 0x2D, (byte) 0xDE, 0x43,
\r
5391 (byte) 0xD0, 0x33, 0x20,
\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
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
5403 0x01, /// from 41
\r
5404 (byte) 0xDE, (byte) 0x83, 0x20, 0x09
\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
5412 ByteBuffer decoderBuffer = ByteBuffer.wrap(sampleText);
\r
5413 CharBuffer encoderBuffer = CharBuffer.wrap(expected);
\r
5416 CharBuffer decoderResult = decode.decode(decoderBuffer);
\r
5418 encoderBuffer.position(0);
\r
5419 if(!decoderResult.equals(encoderBuffer)){
\r
5420 errln("Error occured while decoding "+ charset.name());
\r
5423 ByteBuffer encoderResult = encode.encode(encoderBuffer);
\r
5425 ByteBuffer roundTrip = encoderResult;
\r
5426 CharBuffer roundTripResult = decode.decode(roundTrip);
\r
5428 encoderBuffer.position(0);
\r
5429 if(!roundTripResult.equals(encoderBuffer)){
\r
5430 errln("Error occured while encoding "+ charset.name());
\r
5432 }catch(Exception e){
\r
5433 errln("Exception while converting BOCU-1 thrown: " + e);
\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
5442 String[] expected = {
\r
5443 "windows-949-2000"
\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
5454 /* Increase code coverage for CharsetICU and CharsetProviderICU*/
\r
5455 public void TestCharsetICUCodeCoverage() {
\r
5456 CharsetProviderICU provider = new CharsetProviderICU();
\r
5458 if (provider.charsetForName("UTF16", null) != null) {
\r
5459 errln("charsetForName should have returned a null");
\r
5462 if (CharsetProviderICU.getJavaCanonicalName(null) != null) {
\r
5463 errln("getJavaCanonicalName should have returned a null when null is given to it.");
\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
5473 Charset charset = provider.charsetForName("UTF16");
\r
5476 ((CharsetICU)charset).getUnicodeSet(null, 0);
\r
5477 } catch (IllegalArgumentException ex) {
\r
5480 errln("IllegalArgumentException should have been thrown.");
\r
5483 public void TestCharsetLMBCS() {
\r
5484 String []lmbcsNames = {
\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
5504 CharBuffer cbInput = CharBuffer.wrap(src);
\r
5506 CharsetProviderICU provider = new CharsetProviderICU();
\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
5514 CharsetEncoder encoder = charset.newEncoder();
\r
5515 CharsetDecoder decoder = charset.newDecoder();
\r
5518 cbInput.position(0);
\r
5519 ByteBuffer bbTmp = encoder.encode(cbInput);
\r
5520 CharBuffer cbOutput = decoder.decode(bbTmp);
\r
5522 if (!equals(cbInput, cbOutput)) {
\r
5523 errln("Roundtrip test failed for charset: " + lmbcsNames[i]);
\r
5525 } catch (Exception ex) {
\r
5527 /* Expected exceptions */
\r
5530 errln("Exception thrown: " + ex + " while using charset: " + lmbcsNames[i]);
\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
5540 ByteBuffer malformedSrc = ByteBuffer.wrap(malformedBytes);
\r
5541 CharBuffer malformedTrgt = CharBuffer.allocate(10);
\r
5542 int[] malformedLimits = {
\r
5545 CharsetDecoder malformedDecoderTest = provider.charsetForName("LMBCS-1").newDecoder();
\r
5546 for (int n = 0; n < malformedLimits.length; n++) {
\r
5547 malformedDecoderTest.reset();
\r
5549 malformedSrc.position(0);
\r
5550 malformedSrc.limit(malformedLimits[n]);
\r
5552 malformedTrgt.clear();
\r
5554 malformedResult = malformedDecoderTest.decode(malformedSrc,malformedTrgt, true);
\r
5555 if (!malformedResult.isMalformed()) {
\r
5556 errln("Malformed error should have resulted.");
\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
5566 public void TestAmbiguousConverter() {
\r
5567 byte [] inBytes = {
\r
5570 ByteBuffer src = ByteBuffer.wrap(inBytes);
\r
5571 CharBuffer trgt = CharBuffer.allocate(20);
\r
5573 CoderResult result = CoderResult.UNDERFLOW;
\r
5574 CharsetProviderICU provider = new CharsetProviderICU();
\r
5575 String[] names = CharsetProviderICU.getAllNames();
\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
5583 CharsetDecoder decoder = charset.newDecoder();
\r
5588 result = decoder.decode(src, trgt, true);
\r
5589 if (result.isError()) {
\r
5590 /* We don't care about any failures. */
\r