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
17package com.android.dx.util;
18
19import com.android.dex.util.ByteArrayByteInput;
20import com.android.dex.Leb128;
21import java.io.IOException;
22import java.util.Arrays;
23import junit.framework.TestCase;
24
25public final class Leb128UtilsTest extends TestCase {
26
27    public void testDecodeUnsignedLeb() throws IOException {
28        assertEquals(0, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 0)));
29        assertEquals(1, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 1)));
30        assertEquals(127, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 0x7f)));
31        assertEquals(16256, Leb128.readUnsignedLeb128(
32                new ByteArrayByteInput((byte) 0x80, (byte) 0x7f)));
33    }
34
35    public void testEncodeUnsignedLeb() throws IOException {
36        assertEquals(new byte[] { 0 }, encodeUnsignedLeb(0));
37        assertEquals(new byte[] { 1 }, encodeUnsignedLeb(1));
38        assertEquals(new byte[] { 0x7f }, encodeUnsignedLeb(127));
39        assertEquals(new byte[] { (byte) 0x80, 0x7f }, encodeUnsignedLeb(16256));
40        assertEquals(new byte[] { (byte) 0xb4, 0x07 }, encodeUnsignedLeb(0x3b4));
41        assertEquals(new byte[] { (byte) 0x8c, 0x08 }, encodeUnsignedLeb(0x40c));
42        assertEquals(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0xf },
43                encodeUnsignedLeb(0xffffffff));
44    }
45
46    public void testDecodeSignedLeb() throws IOException {
47        assertEquals(0, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0)));
48        assertEquals(1, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 1)));
49        assertEquals(-1, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0x7f)));
50        assertEquals(0x3c, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0x3c)));
51        assertEquals(-128, Leb128.readSignedLeb128(
52                new ByteArrayByteInput((byte) 0x80, (byte) 0x7f)));
53    }
54
55    public void testEncodeSignedLeb() throws IOException {
56        assertEquals(new byte[] { 0 }, encodeSignedLeb(0));
57        assertEquals(new byte[] { 1 }, encodeSignedLeb(1));
58        assertEquals(new byte[] { 0x7f }, encodeSignedLeb(-1));
59        assertEquals(new byte[] { (byte) 0x80, 0x7f }, encodeSignedLeb(-128));
60    }
61
62    private byte[] encodeSignedLeb(int value) {
63        ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(5);
64        Leb128.writeSignedLeb128(out, value);
65        return out.toByteArray();
66    }
67
68    private byte[] encodeUnsignedLeb(int value) {
69        ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(5);
70        Leb128.writeUnsignedLeb128(out, value);
71        return out.toByteArray();
72    }
73
74    private void assertEquals(byte[] expected, byte[] actual) {
75        assertTrue(Arrays.toString(actual), Arrays.equals(expected, actual));
76    }
77}
78