Home | History | Annotate | Download | only in charset
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package tests.api.java.nio.charset;
     17 
     18 import dalvik.annotation.TestLevel;
     19 import dalvik.annotation.TestTargetClass;
     20 import dalvik.annotation.TestTargetNew;
     21 
     22 import java.nio.ByteBuffer;
     23 import java.nio.CharBuffer;
     24 import java.nio.charset.CharacterCodingException;
     25 import java.nio.charset.Charset;
     26 import java.nio.charset.CodingErrorAction;
     27 import java.util.Arrays;
     28 
     29 /**
     30  * Super class for concrete charset test suites.
     31  */
     32 public class Charset_SingleByteAbstractTest extends Charset_AbstractTest {
     33 
     34     static byte[] allBytes;
     35     static char[] allChars;
     36 
     37     @Override
     38     protected void setUp() throws Exception {
     39         allBytes = new byte[256];
     40         for (int i = 0; i < 256; i++) {
     41             allBytes[i] = (byte) i;
     42         }
     43         super.setUp();
     44     }
     45 
     46     @Override
     47     protected void tearDown() throws Exception {
     48         super.tearDown();
     49     }
     50 
     51 
     52 
     53     public static void dumpDecoded () {
     54         Charset_TestGenerator.Dumper out = new Charset_TestGenerator.Dumper1();
     55         ByteBuffer inputBB = ByteBuffer.wrap(allBytes);
     56         CharBuffer outputCB;
     57         decoder.onMalformedInput(CodingErrorAction.REPLACE);
     58         try {
     59             outputCB = decoder.decode(inputBB);
     60             outputCB.rewind();
     61             while (outputCB.hasRemaining()) {
     62                 out.consume(outputCB.get());
     63             }
     64         } catch (CharacterCodingException e) {
     65             System.out.println(e);
     66 //                e.printStackTrace();
     67         }
     68     }
     69 
     70     public static void decodeReplace (byte[] input, char[] expectedOutput) throws CharacterCodingException {
     71         ByteBuffer inputBB = ByteBuffer.wrap(input);
     72         CharBuffer outputCB;
     73         decoder.onMalformedInput(CodingErrorAction.REPLACE);
     74         outputCB = decoder.decode(inputBB);
     75         outputCB.rewind();
     76         assertEqualChars2("Decoded charactes must match!",
     77                 expectedOutput,
     78                 outputCB.array(),
     79                 input);
     80 //        assertTrue("Decoded charactes (REPLACEed ones INCLUSIVE) must match!",
     81 //                Arrays.equals(expectedOutput, outputCB.array()));
     82 
     83 //        assertEqualChars("Decoded charactes (REPLACEed ones INCLUSIVE) must match!",
     84 //                expectedOutput,
     85 //                outputCB.array());
     86 
     87 //        assertEquals("Decoded charactes must match!",
     88 //                String.valueOf(allChars),
     89 //                outputCB.toString());
     90     }
     91 
     92     @TestTargetNew(
     93         level = TestLevel.PARTIAL_COMPLETE,
     94         method = "functionalCoDec_REPR",
     95         args = {}
     96     )
     97     @Override
     98     public void test_Decode () throws CharacterCodingException {
     99         decodeReplace(allBytes, allChars);
    100 //        ByteBuffer inputBB = ByteBuffer.wrap(allBytes);
    101 //        CharBuffer outputCB;
    102 //        decoder.onMalformedInput(CodingErrorAction.REPLACE);
    103 //        outputCB = decoder.decode(inputBB);
    104 //        outputCB.rewind();
    105 //        assertEqualChars("Decoded charactes must match!",
    106 //                allChars,
    107 //                outputCB.array());
    108 ////        assertEquals("Decoded charactes must match!",
    109 ////                String.valueOf(allChars),
    110 ////                outputCB.toString());
    111     }
    112 
    113     @TestTargetNew(
    114         level = TestLevel.PARTIAL_COMPLETE,
    115         method = "functionalCoDec_REPR",
    116         args = {}
    117     )
    118     @Override
    119     public void test_Encode () throws CharacterCodingException {
    120         CharBuffer inputCB = CharBuffer.wrap(allChars);
    121         ByteBuffer outputBB;
    122         encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    123         outputBB = encoder.encode(inputCB);
    124         outputBB.rewind();
    125         assertEqualBytes2("Encoded bytes must match!", allBytes, outputBB.array(), allChars);
    126     }
    127 
    128 //    static void assertEqualChars (String msg, char[] expected, char[] actual) {
    129 //        int len = expected.length;
    130 //        if (actual.length < len) len = actual.length;
    131 //        for (int i = 0; i < len; i++) {
    132 //            if (actual[i] != expected[i]) {
    133 //                System.out.format("Mismatch at index %d: %d instead of expected %d.\n",
    134 //                        i, (int) actual[i], (int) expected[i]);
    135 //            }
    136 ////            else {
    137 ////                System.out.format("Match index %d: %d = %d\n",
    138 ////                        i, (int) actual[i], (int) expected[i]);
    139 ////            }
    140 //        }
    141 //        assertTrue(msg, Arrays.equals(actual, expected));
    142 //    }
    143 
    144     static void assertEqualChars2 (String msg, char[] expected, char[] actual, byte[] bytes) {
    145         boolean match = true;
    146         boolean replaceMatch = true;
    147         int len = expected.length;
    148         if (actual.length < len) len = actual.length;
    149         for (int i = 0; i < len; i++) {
    150             if (actual[i] == expected[i]) {
    151                 // Fine!
    152             }
    153             else {
    154                 if (expected[i] == 65533) {
    155                     if (actual[i] == (bytes[i] & 0xff)) {
    156 //                        System.out.format("REPLACE mismatch at index %d (byte %d): %d instead of expected %d.\n",
    157 //                                i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]);
    158                     } else {
    159 //                        System.out.format("REPLACE mismatch at index %d (byte %d): %d instead of expected %d.\n",
    160 //                                i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]);
    161                     }
    162                     replaceMatch = false;
    163                 } else {
    164 //                    System.out.format("MISMATCH at index %d (byte %d): %d instead of expected %d.\n",
    165 //                            i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]);
    166                     match = false;
    167                 }
    168             }
    169 //            if ((actual[i] != expected[i]) &&
    170 //                    !((actual[i] == bytes[i]) && (expected[i] == 65533))) {
    171 //
    172 //                match = false;
    173 //            }
    174         }
    175         assertTrue(msg, match);
    176         if (!replaceMatch) {
    177 //            System.out.println("for charset " + charsetName);
    178         }
    179     }
    180 
    181 //    static void assertEqualBytes (String msg, byte[] expected, byte[] actual) {
    182 //        int len = expected.length;
    183 //        if (actual.length < len) len = actual.length;
    184 //        for (int i = 0; i < len; i++) {
    185 //            if (actual[i] != expected[i]) {
    186 //                System.out.format("MISMATCH at index %d: %d instead of expected %d.\n",
    187 //                        i, actual[i], expected[i]);
    188 //            }
    189 //        }
    190 //        assertTrue(msg, Arrays.equals(actual, expected));
    191 //    }
    192 
    193     static void assertEqualBytes2 (String msg, byte[] expected, byte[] actual, char[] chars) {
    194         boolean match = true;
    195         int len = expected.length;
    196         if (actual.length < len) len = actual.length;
    197         for (int i = 0; i < len; i++) {
    198             if ((actual[i] != expected[i]) &&
    199                     !((chars[i] == 65533)) && (actual[i] == 63)) {
    200 //              System.out.format("MISMATCH at index %d: %d instead of expected %d.\n",
    201 //                      i, actual[i], expected[i]);
    202                 match = false;
    203             }
    204         }
    205         assertTrue(msg, match);
    206     }
    207 
    208     public static void main(String[] args) {
    209 //        charset = Charset.defaultCharset();
    210 //        decoder = charset.newDecoder();
    211 //        System.out.println(charset.name());
    212         dumpDecoded();
    213     }
    214 
    215 }
    216