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