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.CharBuffer;
25import java.nio.charset.CharacterCodingException;
26import java.nio.charset.Charset;
27
28/**
29 * TODO type def
30 */
31@TestTargetClass(java.nio.charset.CharsetEncoder.class)
32public class UTF16LECharsetEncoderTest extends AbstractCharsetEncoderTestCase {
33
34    // charset for utf-16le
35    private static final Charset CS = Charset.forName("utf-16le");
36
37    /*
38     * @see CharsetEncoderTest#setUp()
39     */
40    protected void setUp() throws Exception {
41        cs = CS;
42        specifiedReplacement = new byte[] { -3, -1 };
43
44        unibytes = new byte[] { 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
45                114, 0 };
46
47        // unibytesWithRep = new byte[] {(byte)0xfd, (byte)0xff, 32, 0, 98, 0,
48        // 117, 0, 102, 0, 102, 0, 101, 0, 114 ,0};
49
50        super.setUp();
51    }
52
53    /*
54     * @see CharsetEncoderTest#tearDown()
55     */
56    protected void tearDown() throws Exception {
57        super.tearDown();
58    }
59
60    @TestTargetNew(
61        level = TestLevel.TODO,
62        notes = "Empty constructor test.",
63        method = "CharsetEncoder",
64        args = {java.nio.charset.Charset.class, float.class, float.class}
65    )
66    public void testCharsetEncoderCharsetfloatfloat() {
67        // this constructor is invalid for UTF16LE CharsetEncoder
68    }
69
70    @TestTargetNew(
71        level = TestLevel.PARTIAL,
72        notes = "IllegalStateException checking missed.",
73        method = "canEncode",
74        args = {char.class}
75    )
76    public void testCanEncodechar() throws CharacterCodingException {
77        // normal case for utfCS
78        assertTrue(encoder.canEncode('\u0077'));
79        assertTrue(encoder.canEncode('\uc2a3'));
80
81        // for non-mapped char
82        assertTrue(encoder.canEncode('\uc2c0'));
83    }
84
85    @TestTargetNew(
86        level = TestLevel.PARTIAL,
87        notes = "IllegalStateException checking missed.",
88        method = "canEncode",
89        args = {java.lang.CharSequence.class}
90    )
91    public void testCanEncodeCharSequence() {
92        // normal case for utfCS
93        assertTrue(encoder.canEncode("\u0077"));
94        assertTrue(encoder.canEncode("\uc2a3"));
95        assertTrue(encoder.canEncode(""));
96
97        // for non-mapped char
98        assertTrue(encoder.canEncode("\uc2c0"));
99
100        // surrogate char for unicode
101        // 1st byte: d800-dbff
102        // 2nd byte: dc00-dfff
103        // valid surrogate pair
104        assertTrue(encoder.canEncode("\ud800\udc00"));
105        // invalid surrogate pair
106        assertFalse(encoder.canEncode("\ud800\udb00"));
107    }
108
109    @TestTargetNew(
110        level = TestLevel.PARTIAL,
111        notes = "Regression test. IllegalStateException checking missed.",
112        method = "canEncode",
113        args = {java.lang.CharSequence.class}
114    )
115    public void testCanEncodeICUBug() {
116        assertFalse(encoder.canEncode("\ud800"));
117    }
118
119    @TestTargets({
120        @TestTargetNew(
121            level = TestLevel.COMPLETE,
122            notes = "",
123            method = "averageBytesPerChar",
124            args = {}
125        ),
126        @TestTargetNew(
127            level = TestLevel.COMPLETE,
128            notes = "",
129            method = "maxBytesPerChar",
130            args = {}
131        )
132    })
133    public void testSpecificDefaultValue() {
134        assertEquals(2, encoder.averageBytesPerChar(), 0.001);
135        assertEquals(2, encoder.maxBytesPerChar(), 0.001);
136    }
137
138    @TestTargetNew(
139        level = TestLevel.COMPLETE,
140        notes = "",
141        method = "isLegalReplacement",
142        args = {byte[].class}
143    )
144    public void testIsLegalReplacementEmptyArray() {
145        assertTrue(encoder.isLegalReplacement(new byte[0]));
146    }
147
148    CharBuffer getMalformedCharBuffer() {
149        return CharBuffer.wrap("\ud800 buffer");
150    }
151
152    CharBuffer getUnmapCharBuffer() {
153        return null;
154    }
155
156    CharBuffer getExceptionCharBuffer() {
157        return null;
158    }
159
160    protected byte[] getIllegalByteArray() {
161        return new byte[] { 0x00 };
162    }
163
164    protected byte[] getLegalByteArray() {
165        return new byte[] { (byte) 0xd8, 0x00 };
166    }
167
168}
169