view src/test/java/org/icedrobot/daneel/dex/EncodedValueTest.java @ 118:56844aab0e5c

Fixed parsing of encoded float and double values. * dex/EncodedValue.java (parse): Implemented for float and double values. * (readS64, readU64): Fixed casting bug. * (readL64): Added new helper method. * dex/EncodedValueTest.java: Fixed broken test, never trust the implementation. * rewriter/FieldConstantTest.java: New regression test for above changes.
author Michael Starzinger <michi@complang.tuwien.ac.at>
date Mon, 04 Apr 2011 23:11:15 +0200
parents 71b4ce25fa98
children
line wrap: on
line source

/*
 * Daneel - Dalvik to Java bytecode compiler
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.daneel.dex;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.nio.ByteBuffer;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.mockito.Mockito.*;

public class EncodedValueTest {

    private static final byte[] SEQ_BYTE = new byte[] { 0, -1 };
    private static final byte[] SEQ_SHORT1 = new byte[] { 2, -1 };
    private static final byte[] SEQ_SHORT2 = new byte[] { 34, -1, 1 };
    private static final byte[] SEQ_SHORT3 = new byte[] { 34, 0, 1 };
    private static final byte[] SEQ_SHORT4 = new byte[] { 34, -1, 127 };
    private static final byte[] SEQ_INT = new byte[] { 4, -5 };
    private static final byte[] SEQ_LONG = new byte[] { -26, 127, -1, -1, -1,
            -1, -1, -1, -1 };
    private static final byte[] SEQ_CHAR1 = new byte[] { 3, -1 };
    private static final byte[] SEQ_CHAR2 = new byte[] { 35, -1, 1 };
    private static final byte[] SEQ_CHAR3 = new byte[] { 35, 0, 1 };
    private static final byte[] SEQ_CHAR4 = new byte[] { 35, -1, 127 };
    private static final byte[] SEQ_CHAR5 = new byte[] { 35, -1, -1 };
    private static final byte[] SEQ_BOOLEAN1 = new byte[] { 31 };
    private static final byte[] SEQ_BOOLEAN2 = new byte[] { 63 };
    private static final byte[] SEQ_NULL = new byte[] { 30 };
    private static final byte[] SEQ_STRING = new byte[] { 23, 5 };
    private static final byte[] SEQ_UNKNOWN = new byte[] { 1 };

    private static final String SOME_STRING = "some string";

    private static DexFile dexFile;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        dexFile = mock(DexFile.class);
        when(dexFile.getString(5)).thenReturn(SOME_STRING);
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        dexFile = null;
    }

    @Test
    public void testParse() {
        assertEquals(Byte.valueOf((byte) -1), parse(SEQ_BYTE));
        assertEquals(Short.valueOf((short) -1), parse(SEQ_SHORT1));
        assertEquals(Short.valueOf((short) 511), parse(SEQ_SHORT2));
        assertEquals(Short.valueOf((short) 256), parse(SEQ_SHORT3));
        assertEquals(Short.valueOf(Short.MAX_VALUE), parse(SEQ_SHORT4));
        assertEquals(Integer.valueOf(-5), parse(SEQ_INT));
        assertEquals(Long.valueOf(-129l), parse(SEQ_LONG));
        assertEquals(Character.valueOf((char) 255), parse(SEQ_CHAR1));
        assertEquals(Character.valueOf((char) 511), parse(SEQ_CHAR2));
        assertEquals(Character.valueOf((char) 256), parse(SEQ_CHAR3));
        assertEquals(Character.valueOf((char) 32767), parse(SEQ_CHAR4));
        assertEquals(Character.valueOf(Character.MAX_VALUE), parse(SEQ_CHAR5));
        assertEquals(Boolean.FALSE, parse(SEQ_BOOLEAN1));
        assertEquals(Boolean.TRUE, parse(SEQ_BOOLEAN2));
        assertNull(parse(SEQ_NULL));
        assertEquals(SOME_STRING, parse(SEQ_STRING));
    }
    
    @Test(expected = DexParseException.class)
    public void testParseUnknown() {
        parse(SEQ_UNKNOWN);
    }

    private static Object parse(byte[] seq) {
        return EncodedValue.parse(ByteBuffer.wrap(seq), dexFile);
    }
}