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 17 package libcore.java.nio.charset; 18 19 import java.nio.ByteBuffer; 20 import java.nio.CharBuffer; 21 import java.nio.charset.CharacterCodingException; 22 import java.nio.charset.Charset; 23 import java.nio.charset.CharsetDecoder; 24 import java.nio.charset.CharsetEncoder; 25 import java.nio.charset.CodingErrorAction; 26 import junit.framework.TestCase; 27 28 /** 29 * Super class for concrete charset test suites. 30 */ 31 public abstract class OldCharset_AbstractTest extends TestCase { 32 33 static String charsetName; 34 static private Charset charset; 35 static CharsetDecoder decoder; 36 static CharsetEncoder encoder; 37 38 static final int[] codes = Charset_TestGenerator.codes; 39 40 static final char[] chars = new char[codes.length]; // Is filled with 41 // contents of codes. 42 43 static char[] testChars; 44 static byte[] testBytes; 45 theseChars(int[] codes)46 static char[] theseChars (int[] codes) { 47 char[] chars = new char[codes.length]; 48 for (int i = 0; i < codes.length; i++) chars[i] = (char) codes[i]; 49 return chars; 50 } 51 theseBytes(int[] codes)52 static byte[] theseBytes (int[] codes) { 53 byte[] bytes = new byte[codes.length]; 54 for (int i = 0; i < codes.length; i++) bytes[i] = (byte) codes[i]; 55 return bytes; 56 } 57 58 59 @Override setUp()60 protected void setUp() throws Exception { 61 charset = charset.forName(charsetName); 62 decoder = charset.newDecoder(); 63 encoder = charset.newEncoder(); 64 super.setUp(); 65 } 66 67 @Override tearDown()68 protected void tearDown() throws Exception { 69 super.tearDown(); 70 } 71 test_nameMatch()72 public void test_nameMatch () { 73 assertEquals("Name of charset must match!", charsetName, charset.name()); 74 } 75 test_dumpEncodableChars()76 public void test_dumpEncodableChars () { 77 if (testChars == null) return; 78 if (testChars.length > 0) return; 79 System.out.format("\ntest_dumpEncodableChars() for name %s => %s (class = %s)\n", 80 charsetName, charset.name(), getClass().getName()); 81 Charset_TestGenerator.Dumper out = new Charset_TestGenerator.Dumper1(16); 82 int code = 0; 83 while (code < 256) { 84 while (!encoder.canEncode((char) code)) code ++; 85 if (code < 65536) { 86 out.consume(code); 87 code += 1; 88 } 89 } 90 while (code < 65536) { 91 while (!encoder.canEncode((char) code)) code ++; 92 if (code < 65536) { 93 out.consume(code); 94 code += 20; 95 } 96 } 97 System.out.println(); 98 System.out.println("Encodable Chars dumped for Test Class " + getClass().getName()); 99 fail("Encodable Chars dumped for Test Class " + getClass().getName()); 100 } 101 test_dumpEncoded()102 public void test_dumpEncoded () throws CharacterCodingException { 103 if (testChars == null) return; 104 if (testChars.length == 0) return; 105 if (testBytes.length > 0) return; 106 System.out.format("\ntest_dumpEncoded() for name %s => %s (class = %s)\n", 107 charsetName, charset.name(), getClass().getName()); 108 Charset_TestGenerator.Dumper out = new Charset_TestGenerator.Dumper1(); 109 CharBuffer inputCB = CharBuffer.wrap(testChars); 110 ByteBuffer outputBB; 111 encoder.onUnmappableCharacter(CodingErrorAction.REPORT); 112 outputBB = encoder.encode(inputCB); 113 outputBB.rewind(); 114 while (outputBB.hasRemaining()) { 115 out.consume(outputBB.get() & 0xff); 116 } 117 System.out.println(); 118 System.out.println("Encoded Bytes dumped for Test Class " + getClass().getName()); 119 fail("Encoded Bytes dumped for Test Class " + getClass().getName()); 120 } 121 122 decode(byte[] input, char[] expectedOutput)123 static void decode (byte[] input, char[] expectedOutput) throws CharacterCodingException { 124 ByteBuffer inputBB = ByteBuffer.wrap(input); 125 CharBuffer outputCB; 126 decoder.onMalformedInput(CodingErrorAction.REPORT); 127 outputCB = decoder.decode(inputBB); 128 outputCB.rewind(); 129 130 // assertTrue("Decoded charactes must match!", 131 // Arrays.equals(expectedOutput, outputCB.array())); 132 assertEqualChars("Decoded charactes must match!", 133 expectedOutput, outputCB); 134 135 // assertEqualChars2("Decoded charactes must match!", 136 // expectedOutput, 137 // outputCB.array(), 138 // input); 139 // assertTrue("Decoded charactes (REPLACEed ones INCLUSIVE) must match!", 140 // Arrays.equals(expectedOutput, outputCB.array())); 141 142 // assertEqualChars("Decoded charactes (REPLACEed ones INCLUSIVE) must match!", 143 // expectedOutput, 144 // outputCB.array()); 145 146 // assertEquals("Decoded charactes must match!", 147 // String.valueOf(allChars), 148 // outputCB.toString()); 149 } 150 test_Decode()151 public void test_Decode () throws CharacterCodingException { 152 decode(testBytes, testChars); 153 } 154 test_Encode()155 public void test_Encode () throws CharacterCodingException { 156 CharBuffer inputCB = CharBuffer.wrap(testChars); 157 ByteBuffer outputBB; 158 encoder.onUnmappableCharacter(CodingErrorAction.REPORT); 159 outputBB = encoder.encode(inputCB); 160 outputBB.rewind(); 161 // assertTrue("Encoded bytes must match!", 162 // Arrays.equals(testBytes, outputBB.array())); 163 assertEqualBytes("Encoded bytes must match!", testBytes, outputBB); 164 } 165 166 NNtest_CodecDynamicIndividuals()167 public void NNtest_CodecDynamicIndividuals () throws CharacterCodingException { 168 encoder.onUnmappableCharacter(CodingErrorAction.REPORT); 169 decoder.onMalformedInput(CodingErrorAction.REPORT); 170 171 for (int code = 32; code <= 65533; code ++) { 172 if (encoder.canEncode((char) code)) { 173 // inputCB.rewind(); 174 CharBuffer inputCB = CharBuffer.allocate(1); 175 inputCB.put((char) code); 176 inputCB.rewind(); 177 ByteBuffer intermediateBB = encoder.encode(inputCB); 178 inputCB.rewind(); 179 intermediateBB.rewind(); 180 try { 181 CharBuffer outputCB = decoder.decode(intermediateBB); 182 outputCB.rewind(); 183 assertEqualCBs("decode(encode(A)) must be identical with A = " + code, 184 inputCB, outputCB); 185 if (code == 165) { 186 outputCB.rewind(); 187 System.out.println("WOW:" + outputCB.get()); 188 } 189 } catch (CharacterCodingException e) { 190 fail("failed to decode(encode(" + code + "))"); 191 } 192 } 193 } 194 } 195 test_CodecDynamic()196 public void test_CodecDynamic () throws CharacterCodingException { 197 encoder.onUnmappableCharacter(CodingErrorAction.REPORT); 198 decoder.onMalformedInput(CodingErrorAction.REPORT); 199 CharBuffer inputCB = CharBuffer.allocate(65536); 200 for (int code = 32; code <= 65533; code ++) { 201 if (encoder.canEncode((char) code)) { 202 inputCB.put((char) code); 203 } 204 } 205 inputCB.rewind(); 206 ByteBuffer intermediateBB = encoder.encode(inputCB); 207 inputCB.rewind(); 208 intermediateBB.rewind(); 209 CharBuffer outputCB = decoder.decode(intermediateBB); 210 outputCB.rewind(); 211 assertEqualCBs("decode(encode(A)) must be identical with A!", 212 inputCB, outputCB); 213 } 214 assertEqualCBs(String msg, CharBuffer expectedCB, CharBuffer actualCB)215 static void assertEqualCBs (String msg, CharBuffer expectedCB, CharBuffer actualCB) { 216 boolean match = true; 217 boolean lenMatch = true; 218 char expected, actual; 219 int len = actualCB.length(); 220 if (expectedCB.length() != len) { 221 lenMatch = false; 222 if (expectedCB.length() < len) len = expectedCB.length(); 223 } 224 for (int i = 0; i < len; i++) { 225 expected = expectedCB.get(); 226 actual = actualCB.get(); 227 if (actual != expected) { 228 String detail = String.format( 229 "Mismatch at index %d: %d instead of expected %d.\n", 230 i, (int) actual, (int) expected); 231 match = false; 232 fail(msg + ": " + detail); 233 } 234 // else { 235 // System.out.format("Match index %d: %d = %d\n", 236 // i, (int) actual[i], (int) expected[i]); 237 // } 238 } 239 assertTrue(msg, match); 240 assertTrue(msg + "(IN LENGTH ALSO!)", lenMatch); 241 // assertTrue(msg, Arrays.equals(actual, expected)); 242 } 243 assertEqualChars(String msg, char[] expected, CharBuffer actualCB)244 static void assertEqualChars (String msg, char[] expected, CharBuffer actualCB) { 245 boolean match = true; 246 boolean lenMatch = true; 247 char actual; 248 int len = actualCB.length(); 249 if (expected.length != len) { 250 lenMatch = false; 251 if (expected.length < len) len = expected.length; 252 } 253 for (int i = 0; i < len; i++) { 254 actual = actualCB.get(); 255 if (actual != expected[i]) { 256 String detail = String.format( 257 "Mismatch at index %d: %d instead of expected %d.\n", 258 i, (int) actual, (int) expected[i]); 259 match = false; 260 fail(msg + ": " + detail); 261 } 262 // else { 263 // System.out.format("Match index %d: %d = %d\n", 264 // i, (int) actual[i], (int) expected[i]); 265 // } 266 } 267 assertTrue(msg, match); 268 assertTrue(msg + "(IN LENGTH ALSO!)", lenMatch); 269 // assertTrue(msg, Arrays.equals(actual, expected)); 270 } 271 assertEqualBytes(String msg, byte[] expected, ByteBuffer actualBB)272 static void assertEqualBytes (String msg, byte[] expected, ByteBuffer actualBB) { 273 boolean match = true; 274 boolean lenMatch = true; 275 byte actual; 276 int len = actualBB.remaining(); 277 if (expected.length != len) { 278 lenMatch = false; 279 if (expected.length < len) len = expected.length; 280 } 281 for (int i = 0; i < len; i++) { 282 actual = actualBB.get(); 283 if (actual != expected[i]) { 284 String detail = String.format( 285 "Mismatch at index %d: %d instead of expected %d.\n", 286 i, actual & 0xff, expected[i] & 0xff); 287 match = false; 288 fail(msg + ": " + detail); 289 } 290 } 291 assertTrue(msg, match); 292 assertTrue(msg + "(IN LENGTH ALSO!)", lenMatch); 293 } 294 295 296 static abstract class CodesGenerator { 297 int row = 0, col = 0; 298 consume(int code)299 abstract void consume(int code); 300 isAccepted(int code)301 boolean isAccepted(int code) { 302 return Character.isLetterOrDigit(code); 303 } 304 } 305 306 } 307