]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-52_1/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / charset / src / com / ibm / icu / dev / test / charset / TestCharset.java
similarity index 98%
rename from jars/icu4j-4_8_1_1/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java
rename to jars/icu4j-52_1/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java
index 1e48e584b42cf65f242fd404360187cf9ba8c160..995dc32e517925aed424e8123fce5a6a25a82c2d 100644 (file)
@@ -1,6 +1,6 @@
 /**
 *******************************************************************************
-* Copyright (C) 2006-2011, International Business Machines Corporation and    *
+* Copyright (C) 2006-2012, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 *
@@ -5609,6 +5609,7 @@ public class TestCharset extends TestFmwk {
             }
         }
     }
+    
     public void TestIsFixedWidth(){
         String[] fixedWidth = {
                 "US-ASCII",
@@ -5641,4 +5642,97 @@ public class TestCharset extends TestFmwk {
             }
         }
     }
+    
+    public void TestBytesLengthForString() {
+        CharsetProviderICU provider = new CharsetProviderICU();
+        String[] charsets = {
+                "windows-949-2000",
+                "ibm-1047_P100-1995,swaplfnl",
+                "ibm-930_P120-1999",
+                "ISCII,version=0",
+                "ISO_2022,locale=ko,version=0"
+        };
+        
+        int[] expected = {
+                40,
+                20,
+                80, /* changed from 60 to 80 to reflect the updates by #9205 */
+                80,
+                60
+        };
+        
+        int stringLength = 10;
+        int length;
+        int maxCharSize;
+        
+        for (int i = 0; i < charsets.length; i++) {
+            maxCharSize = (int)provider.charsetForName(charsets[i]).newEncoder().maxBytesPerChar();
+            length = CharsetEncoderICU.getMaxBytesForString(stringLength, maxCharSize);
+            
+            if (length != expected[i]) {
+                errln("For charset " + charsets[i] + " with string length " + stringLength + ", expected max byte length is " + expected[i] + " but got " + length);
+            }
+        }
+    }
+    
+    /*
+     * When converting slices of a larger CharBuffer, Charset88591 and CharsetASCII does not handle the buffer correctly when
+     * an unmappable character occurs.
+     * Ticket #8729
+     */
+    public void TestCharsetASCII8859BufferHandling() {
+        String firstLine = "C077693790=|MEMO=|00=|022=|Blanche st and the driveway grate was fault and rotated under my car=|\r\n";
+        String secondLine = "C077693790=|MEMO=|00=|023=|puncturing the fuel tank. I spoke to the store operator (Ram Reddi –=|\r\n";
+        
+        String charsetNames[] = {
+                "ASCII",
+                "ISO-8859-1"
+        };
+        
+        CoderResult result = CoderResult.UNDERFLOW;
+        
+        CharsetEncoder encoder;
+        
+        ByteBuffer outBuffer = ByteBuffer.allocate(500);
+        CharBuffer charBuffer = CharBuffer.allocate(firstLine.length() + secondLine.length());
+        charBuffer.put(firstLine);
+        charBuffer.put(secondLine);
+        charBuffer.flip();
+        
+        for (int i = 0; i < charsetNames.length; i++) {
+            encoder =  CharsetICU.forNameICU(charsetNames[i]).newEncoder();
+            
+            charBuffer.position(firstLine.length());
+            CharBuffer charBufferSlice = charBuffer.slice();
+            charBufferSlice.limit(secondLine.length() - 2);
+    
+    
+            try {
+                result = encoder.encode(charBufferSlice, outBuffer, false);
+                if (!result.isUnmappable()) {
+                    errln("Result of encoding " + charsetNames[i] + " should be: \"Unmappable\". Instead got: " + result);
+                }
+            } catch (IllegalArgumentException ex) {
+                errln("IllegalArgumentException should not have been thrown when encoding: " + charsetNames[i]);
+            }
+        }
+    }
+    
+    /*
+     * When converting with the String method getBytes(), buffer overflow exception is thrown because
+     * of the way ICU4J is calculating the max bytes per char. This should be changed only on the ICU4J
+     * side to match what the Java method is expecting. The ICU4C size will be left unchanged.
+     * Ticket #9205
+     */
+    public void TestBufferOverflowErrorUsingJavagetBytes() {
+        String charsetName = "ibm-5035";
+        String testCase = "\u7d42";
+        
+        try {
+            testCase.getBytes(charsetName);
+        } catch (Exception ex) {
+            errln("Error calling getBytes(): " + ex);
+        }
+        
+    }
 }