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 17package tests.api.java.nio.charset; 18 19import dalvik.annotation.TestTargetClass; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestLevel; 23 24import java.nio.ByteBuffer; 25import java.nio.CharBuffer; 26import java.nio.charset.CharacterCodingException; 27import java.nio.charset.Charset; 28import java.nio.charset.CharsetDecoder; 29 30/** 31 * TODO type def 32 */ 33@TestTargetClass(java.nio.charset.CharsetEncoder.class) 34public class UTF16CharsetEncoderTest extends AbstractCharsetEncoderTestCase { 35 36 // charset for utf-16 37 // charset for utf-16be 38 private static final Charset CS = Charset.forName("utf-16"); 39 40 private static final CharsetDecoder decoder = CS.newDecoder(); 41 42 /* 43 * @see CharsetEncoderTest#setUp() 44 */ 45 protected void setUp() throws Exception { 46 cs = CS; 47 specifiedReplacement = new byte[] { -3, -1 }; 48 surrogate = new byte[] { -1, -2 }; 49 unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 50 114, 0 }; 51 unibytesWithRep = new byte[] { -3, -1, 32, 0, 98, 0, 117, 0, 102, 0, 52 102, 0, 101, 0, 114, 0 }; 53 super.setUp(); 54 } 55 56 /* 57 * @see CharsetEncoderTest#tearDown() 58 */ 59 protected void tearDown() throws Exception { 60 super.tearDown(); 61 } 62 63 @TestTargetNew( 64 level = TestLevel.TODO, 65 notes = "Test is empty", 66 method = "CharsetEncoder", 67 args = {java.nio.charset.Charset.class, float.class, float.class} 68 ) 69 public void testCharsetEncoderCharsetfloatfloat() { 70 // this constructor is invalid for UTF16LE CharsetEncoder 71 } 72 73 @TestTargetNew( 74 level = TestLevel.PARTIAL, 75 notes = "IllegalStateException checking missed.", 76 method = "canEncode", 77 args = {char.class} 78 ) 79 public void testCanEncodechar() throws CharacterCodingException { 80 // normal case for utfCS 81 assertTrue(encoder.canEncode('\u0077')); 82 assertTrue(encoder.canEncode('\uc2a3')); 83 84 // for non-mapped char 85 assertTrue(encoder.canEncode('\uc2c0')); 86 } 87 88 @TestTargetNew( 89 level = TestLevel.PARTIAL, 90 notes = "IllegalStateException checking missed.", 91 method = "canEncode", 92 args = {java.lang.CharSequence.class} 93 ) 94 public void testCanEncodeCharSequence() { 95 // normal case for utfCS 96 assertTrue(encoder.canEncode("\u0077")); 97 assertTrue(encoder.canEncode("\uc2a3")); 98 assertTrue(encoder.canEncode("")); 99 100 // for non-mapped char 101 assertTrue(encoder.canEncode("\uc2c0")); 102 103 // surrogate char for unicode 104 // 1st byte: d800-dbff 105 // 2nd byte: dc00-dfff 106 // valid surrogate pair 107 assertTrue(encoder.canEncode("\ud800\udc00")); 108 // invalid surrogate pair 109 assertFalse(encoder.canEncode("\ud800\udb00")); 110 } 111 112 @TestTargets({ 113 @TestTargetNew( 114 level = TestLevel.PARTIAL, 115 notes = "IllegalStateException checking missed.", 116 method = "canEncode", 117 args = {char.class} 118 ), 119 @TestTargetNew( 120 level = TestLevel.PARTIAL, 121 notes = "IllegalStateException checking missed.", 122 method = "canEncode", 123 args = {java.lang.CharSequence.class} 124 ) 125 }) 126 public void testCanEncodeICUBug() { 127 assertFalse(encoder.canEncode('\ud800')); 128 assertFalse(encoder.canEncode("\ud800")); 129 } 130 131 @TestTargets({ 132 @TestTargetNew( 133 level = TestLevel.COMPLETE, 134 notes = "", 135 method = "averageBytesPerChar", 136 args = {} 137 ), 138 @TestTargetNew( 139 level = TestLevel.COMPLETE, 140 notes = "", 141 method = "maxBytesPerChar", 142 args = {} 143 ) 144 }) 145 public void testSpecificDefaultValue() { 146 assertEquals(encoder.averageBytesPerChar(), 2, 0.001); 147 // assertEquals(4, encoder.maxBytesPerChar()); 148 // FIXME: different here! 149 assertEquals(encoder.maxBytesPerChar(), 2, 0.001); 150 } 151 152 CharBuffer getMalformedCharBuffer() { 153 return CharBuffer.wrap("\ud800 buffer"); 154 } 155 156 CharBuffer getUnmapCharBuffer() { 157 return null; 158 } 159 160 CharBuffer getExceptionCharBuffer() { 161 return null; 162 } 163 164 @TestTargetNew( 165 level = TestLevel.COMPLETE, 166 notes = "", 167 method = "isLegalReplacement", 168 args = {byte[].class} 169 ) 170 public void testIsLegalReplacementEmptyArray() { 171 assertTrue(encoder.isLegalReplacement(new byte[0])); 172 } 173 174 protected byte[] getIllegalByteArray() { 175 return new byte[] { 0x00 }; 176 } 177 178 protected byte[] getLegalByteArray() { 179 // FIXME: Different Here! 180 // return new byte[]{(byte)0xd8, 0x00}; 181 return new byte[] { (byte) 0x00, (byte) 0xd8 }; 182 } 183 184 void assertByteArray(ByteBuffer out, byte[] expected) { 185 out = out.duplicate(); 186 if (out.position() > 0) { 187 out.flip(); 188 } 189 try { 190 assertEquals(decoder.decode(out), decoder.decode(ByteBuffer 191 .wrap(expected))); 192 } catch (CharacterCodingException e) { 193 fail(e.toString()); 194 } 195 } 196} 197