1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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 com.android.dex;
18 
19 import com.android.dex.util.ByteArrayByteInput;
20 import junit.framework.TestCase;
21 
22 public final class EncodedValueReaderTest extends TestCase {
23 
testReadByte()24     public void testReadByte() {
25         assertEquals((byte) 0x80, readerOf(0, 0x80).readByte());
26         assertEquals((byte) 0xff, readerOf(0, 0xff).readByte());
27         assertEquals((byte) 0x00, readerOf(0, 0x00).readByte());
28         assertEquals((byte) 0x01, readerOf(0, 0x01).readByte());
29         assertEquals((byte) 0x7f, readerOf(0, 0x7f).readByte());
30     }
31 
testReadShort()32     public void testReadShort() {
33         assertEquals((short) 0x8000, readerOf(34, 0x00, 0x80).readShort());
34         assertEquals((short)      0, readerOf( 2, 0x00).readShort());
35         assertEquals((short)   0xab, readerOf(34, 0xab, 0x00).readShort());
36         assertEquals((short) 0xabcd, readerOf(34, 0xcd, 0xab).readShort());
37         assertEquals((short) 0x7FFF, readerOf(34, 0xff, 0x7f).readShort());
38     }
39 
testReadInt()40     public void testReadInt() {
41         assertEquals(0x80000000, readerOf(100, 0x00, 0x00, 0x00, 0x80).readInt());
42         assertEquals(      0x00, readerOf(  4, 0x00).readInt());
43         assertEquals(      0xab, readerOf( 36, 0xab, 0x00).readInt());
44         assertEquals(    0xabcd, readerOf( 68, 0xcd, 0xab, 0x00).readInt());
45         assertEquals(  0xabcdef, readerOf(100, 0xef, 0xcd, 0xab, 0x00).readInt());
46         assertEquals(0xabcdef01, readerOf(100, 0x01, 0xef, 0xcd, 0xab).readInt());
47         assertEquals(0x7fffffff, readerOf(100, 0xff, 0xff, 0xff, 127).readInt());
48     }
49 
testReadLong()50     public void testReadLong() {
51         assertEquals(0x8000000000000000L, readerOf( -26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80).readLong());
52         assertEquals(              0x00L, readerOf(   6, 0x00).readLong());
53         assertEquals(              0xabL, readerOf(  38, 0xab, 0x00).readLong());
54         assertEquals(            0xabcdL, readerOf(  70, 0xcd, 0xab, 0x00).readLong());
55         assertEquals(          0xabcdefL, readerOf( 102, 0xef, 0xcd, 0xab, 0x00).readLong());
56         assertEquals(        0xabcdef01L, readerOf(-122, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
57         assertEquals(      0xabcdef0123L, readerOf( -90, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
58         assertEquals(    0xabcdef012345L, readerOf( -58, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
59         assertEquals(  0xabcdef01234567L, readerOf( -26, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
60         assertEquals(0xabcdef0123456789L, readerOf( -26, 0x89, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab).readLong());
61         assertEquals(0x7fffffffffffffffL, readerOf( -26, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f).readLong());
62     }
63 
testReadFloat()64     public void testReadFloat() {
65         assertEquals(Float.NEGATIVE_INFINITY, readerOf(48, -128, -1).readFloat());
66         assertEquals(Float.POSITIVE_INFINITY, readerOf(48, -128, 127).readFloat());
67         assertEquals(Float.NaN, readerOf(48, -64, 127).readFloat());
68         assertEquals(-0.0f, readerOf(16, -128).readFloat());
69         assertEquals(0.0f, readerOf(16, 0).readFloat());
70         assertEquals(0.5f, readerOf(16, 63).readFloat());
71         assertEquals(1f, readerOf(48, -128, 63).readFloat());
72         assertEquals(1.0E06f, readerOf(80, 36, 116, 73).readFloat());
73         assertEquals(1.0E12f, readerOf(112, -91, -44, 104, 83).readFloat());
74     }
75 
testReadDouble()76     public void testReadDouble() {
77         assertEquals(Double.NEGATIVE_INFINITY, readerOf(49, -16, -1).readDouble());
78         assertEquals(Double.POSITIVE_INFINITY, readerOf(49, -16, 127).readDouble());
79         assertEquals(Double.NaN, readerOf(49, -8, 127).readDouble());
80         assertEquals(-0.0, readerOf(17, -128).readDouble());
81         assertEquals(0.0, readerOf(17, 0).readDouble());
82         assertEquals(0.5, readerOf(49, -32, 63).readDouble());
83         assertEquals(1.0, readerOf(49, -16, 63).readDouble());
84         assertEquals(1.0E06, readerOf(113, -128, -124, 46, 65).readDouble());
85         assertEquals(1.0E12, readerOf(-111, -94, -108, 26, 109, 66).readDouble());
86         assertEquals(1.0E24, readerOf(-15, -76, -99, -39, 121, 67, 120, -22, 68).readDouble());
87     }
88 
testReadChar()89     public void testReadChar() {
90         assertEquals('\u0000', readerOf( 3, 0x00).readChar());
91         assertEquals('\u00ab', readerOf( 3, 0xab).readChar());
92         assertEquals('\uabcd', readerOf(35, 0xcd, 0xab).readChar());
93         assertEquals('\uffff', readerOf(35, 0xff, 0xff).readChar());
94     }
95 
testReadBoolean()96     public void testReadBoolean() {
97         assertEquals(true, readerOf(63).readBoolean());
98         assertEquals(false, readerOf(31).readBoolean());
99     }
100 
testReadNull()101     public void testReadNull() {
102         readerOf(30).readNull();
103     }
104 
testReadReference()105     public void testReadReference() {
106         assertEquals(      0xab, readerOf(0x17, 0xab).readString());
107         assertEquals(    0xabcd, readerOf(0x37, 0xcd, 0xab).readString());
108         assertEquals(  0xabcdef, readerOf(0x57, 0xef, 0xcd, 0xab).readString());
109         assertEquals(0xabcdef01, readerOf(0x77, 0x01, 0xef, 0xcd, 0xab).readString());
110     }
111 
testReadWrongType()112     public void testReadWrongType() {
113         try {
114             readerOf(0x17, 0xab).readField();
115             fail();
116         } catch (IllegalStateException expected) {
117         }
118     }
119 
readerOf(int... bytes)120     private EncodedValueReader readerOf(int... bytes) {
121         byte[] data = new byte[bytes.length];
122         for (int i = 0; i < bytes.length; i++) {
123             data[i] = (byte) bytes[i];
124         }
125         return new EncodedValueReader(new ByteArrayByteInput(data));
126     }
127 }
128