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 */ 16package libcore.java.nio.charset; 17 18import java.nio.ByteBuffer; 19import java.nio.CharBuffer; 20import java.nio.charset.CharacterCodingException; 21import java.nio.charset.CodingErrorAction; 22 23/** 24 * Super class for concrete charset test suites. 25 */ 26public abstract class OldCharset_SingleByteAbstractTest extends OldCharset_AbstractTest { 27 28 static byte[] allBytes; 29 static char[] allChars; 30 31 @Override 32 protected void setUp() throws Exception { 33 allBytes = new byte[256]; 34 for (int i = 0; i < 256; i++) { 35 allBytes[i] = (byte) i; 36 } 37 super.setUp(); 38 } 39 40 @Override 41 protected void tearDown() throws Exception { 42 super.tearDown(); 43 } 44 45 46 47 public static void dumpDecoded () { 48 Charset_TestGenerator.Dumper out = new Charset_TestGenerator.Dumper1(); 49 ByteBuffer inputBB = ByteBuffer.wrap(allBytes); 50 CharBuffer outputCB; 51 decoder.onMalformedInput(CodingErrorAction.REPLACE); 52 try { 53 outputCB = decoder.decode(inputBB); 54 outputCB.rewind(); 55 while (outputCB.hasRemaining()) { 56 out.consume(outputCB.get()); 57 } 58 } catch (CharacterCodingException e) { 59 System.out.println(e); 60// e.printStackTrace(); 61 } 62 } 63 64 public static void decodeReplace (byte[] input, char[] expectedOutput) throws CharacterCodingException { 65 ByteBuffer inputBB = ByteBuffer.wrap(input); 66 CharBuffer outputCB; 67 decoder.onMalformedInput(CodingErrorAction.REPLACE); 68 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 69 outputCB = decoder.decode(inputBB); 70 outputCB.rewind(); 71 assertEqualChars2("Decoded charactes must match!", 72 expectedOutput, 73 outputCB.array(), 74 input); 75// assertTrue("Decoded charactes (REPLACEed ones INCLUSIVE) must match!", 76// Arrays.equals(expectedOutput, outputCB.array())); 77 78// assertEqualChars("Decoded charactes (REPLACEed ones INCLUSIVE) must match!", 79// expectedOutput, 80// outputCB.array()); 81 82// assertEquals("Decoded charactes must match!", 83// String.valueOf(allChars), 84// outputCB.toString()); 85 } 86 87 @Override 88 public void test_Decode () throws CharacterCodingException { 89 decodeReplace(allBytes, allChars); 90// ByteBuffer inputBB = ByteBuffer.wrap(allBytes); 91// CharBuffer outputCB; 92// decoder.onMalformedInput(CodingErrorAction.REPLACE); 93// outputCB = decoder.decode(inputBB); 94// outputCB.rewind(); 95// assertEqualChars("Decoded charactes must match!", 96// allChars, 97// outputCB.array()); 98//// assertEquals("Decoded charactes must match!", 99//// String.valueOf(allChars), 100//// outputCB.toString()); 101 } 102 103 @Override 104 public void test_Encode () throws CharacterCodingException { 105 CharBuffer inputCB = CharBuffer.wrap(allChars); 106 ByteBuffer outputBB; 107 encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 108 outputBB = encoder.encode(inputCB); 109 outputBB.rewind(); 110 assertEqualBytes2("Encoded bytes must match!", allBytes, outputBB.array(), allChars); 111 } 112 113// static void assertEqualChars (String msg, char[] expected, char[] actual) { 114// int len = expected.length; 115// if (actual.length < len) len = actual.length; 116// for (int i = 0; i < len; i++) { 117// if (actual[i] != expected[i]) { 118// System.out.format("Mismatch at index %d: %d instead of expected %d.\n", 119// i, (int) actual[i], (int) expected[i]); 120// } 121//// else { 122//// System.out.format("Match index %d: %d = %d\n", 123//// i, (int) actual[i], (int) expected[i]); 124//// } 125// } 126// assertTrue(msg, Arrays.equals(actual, expected)); 127// } 128 129 static void assertEqualChars2 (String msg, char[] expected, char[] actual, byte[] bytes) { 130 boolean match = true; 131 boolean replaceMatch = true; 132 int len = expected.length; 133 if (actual.length < len) len = actual.length; 134 for (int i = 0; i < len; i++) { 135 if (actual[i] == expected[i]) { 136 // Fine! 137 } 138 else { 139 if (expected[i] == 65533) { 140 if (actual[i] == (bytes[i] & 0xff)) { 141// System.out.format("REPLACE mismatch at index %d (byte %d): %d instead of expected %d.\n", 142// i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]); 143 } else { 144// System.out.format("REPLACE mismatch at index %d (byte %d): %d instead of expected %d.\n", 145// i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]); 146 } 147 replaceMatch = false; 148 } else { 149// System.out.format("MISMATCH at index %d (byte %d): %d instead of expected %d.\n", 150// i, bytes[i] & 0xff, (int) actual[i], (int) expected[i]); 151 match = false; 152 } 153 } 154// if ((actual[i] != expected[i]) && 155// !((actual[i] == bytes[i]) && (expected[i] == 65533))) { 156// 157// match = false; 158// } 159 } 160 assertTrue(msg, match); 161 if (!replaceMatch) { 162// System.out.println("for charset " + charsetName); 163 } 164 } 165 166// static void assertEqualBytes (String msg, byte[] expected, byte[] actual) { 167// int len = expected.length; 168// if (actual.length < len) len = actual.length; 169// for (int i = 0; i < len; i++) { 170// if (actual[i] != expected[i]) { 171// System.out.format("MISMATCH at index %d: %d instead of expected %d.\n", 172// i, actual[i], expected[i]); 173// } 174// } 175// assertTrue(msg, Arrays.equals(actual, expected)); 176// } 177 178 static void assertEqualBytes2 (String msg, byte[] expected, byte[] actual, char[] chars) { 179 boolean match = true; 180 int len = expected.length; 181 if (actual.length < len) len = actual.length; 182 for (int i = 0; i < len; i++) { 183 if ((actual[i] != expected[i]) && 184 !((chars[i] == 65533)) && (actual[i] == 63)) { 185// System.out.format("MISMATCH at index %d: %d instead of expected %d.\n", 186// i, actual[i], expected[i]); 187 match = false; 188 } 189 } 190 assertTrue(msg, match); 191 } 192 193} 194