Added some tests

This commit is contained in:
Sander Hautvast 2020-11-10 21:57:13 +01:00
parent d926ed2adc
commit 0a16440c2e
9 changed files with 95 additions and 9 deletions

View file

@ -17,10 +17,10 @@ public class TypeMapper {
MAP.put(short.class, "S"); MAP.put(short.class, "S");
MAP.put(boolean.class, "Z"); MAP.put(boolean.class, "Z");
} }
//TODO something with arrays
public static String map(Class<?> type) { public static String map(Class<?> type) {
return Optional.ofNullable(MAP.get(type)) return Optional.ofNullable(MAP.get(type))
.orElseThrow(() -> new RuntimeException("Type " + type.getName() + " not found")); // this MUST not happen -> TODO map all types .orElseThrow(() -> new RuntimeException("Type " + type.getName() + " not found"));
} }

View file

@ -25,7 +25,7 @@ public abstract class ConstantPoolEntry {
* *
* @return a byte indicating the type of the constant * @return a byte indicating the type of the constant
*/ */
byte getTag(){ byte getTag() {
return getBytes()[0]; return getBytes()[0];
} }
@ -58,10 +58,10 @@ public abstract class ConstantPoolEntry {
* @return upper 8 bits as byte * @return upper 8 bits as byte
*/ */
protected byte upperByte(int u16) { protected byte upperByte(int u16) {
return (byte) (u16 << 8); return (byte) (u16 >>> 8);
} }
protected byte getByte(long bits, int positions) { protected byte getByte(long bits, int positions) {
return (byte) ((bits >>> positions) & 0xFF); return (byte) ((bits >>> (positions * 8)) & 0xFF);
} }
} }

View file

@ -13,8 +13,9 @@ public class DoubleEntry extends LeafEntry {
@Override @Override
public byte[] getBytes() { public byte[] getBytes() {
long bits = Double.doubleToRawLongBits(doubleVal); long bits = Double.doubleToRawLongBits(doubleVal);
return new byte[]{TAG, getByte(bits, 56), getByte(bits, 48), getByte(bits, 40), getByte(bits, 32), return new byte[]{TAG, getByte(bits, 7), getByte(bits, 6), getByte(bits, 5),
getByte(bits, 24), getByte(bits, 16), getByte(bits, 8), (byte) (bits & 0xFF)}; getByte(bits, 4),
getByte(bits, 3), getByte(bits, 2), getByte(bits, 1), (byte) (bits & 0xFF)};
} }
@Override @Override

View file

@ -18,8 +18,9 @@ public class LongEntry extends LeafEntry {
@Override @Override
public byte[] getBytes() { public byte[] getBytes() {
return new byte[]{TAG, getByte(longVal, 56), getByte(longVal, 48), getByte(longVal, 40), getByte(longVal, 32), return new byte[]{TAG, getByte(longVal, 7), getByte(longVal, 6), getByte(longVal, 5),
getByte(longVal, 24), getByte(longVal, 16), getByte(longVal, 8), (byte) (longVal & 0xFF)}; getByte(longVal, 4), getByte(longVal, 3), getByte(longVal, 2),
getByte(longVal, 1), (byte) (longVal & 0xFF)};
} }
@Override @Override

View file

@ -0,0 +1,32 @@
package nl.sander.beejava.constantpool.entry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConstantPoolEntryTest {
private final ConstantPoolEntry entry = new ConstantPoolEntry() {
@Override
public byte[] getBytes() {
return new byte[]{};
}
};
@Test
public void getSetIndex() {
int value = 2;
entry.setIndex(value);
assertEquals(value, entry.getIndex());
}
@Test
public void lowerByte() {
assertEquals(1, entry.lowerByte(257));
}
@Test
public void upperByte() {
assertEquals(1, entry.upperByte(257));
}
}

View file

@ -0,0 +1,13 @@
package nl.sander.beejava.constantpool.entry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class DoubleEntryTest {
@Test
void getBytes() {
assertArrayEquals(new byte[]{6, 64, 40, 0, 0, 0, 0, 0, 0}, new DoubleEntry(12D).getBytes());
}
}

View file

@ -0,0 +1,13 @@
package nl.sander.beejava.constantpool.entry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class FloatEntryTest {
@Test
void getBytes() {
assertArrayEquals(new byte[]{4, 0, 0, 0, 0}, new FloatEntry(0F).getBytes());
}
}

View file

@ -0,0 +1,13 @@
package nl.sander.beejava.constantpool.entry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class IntegerEntryTest {
@Test
void getBytes() {
assertArrayEquals(new byte[]{3, 18, 52, 86, 120}, new IntegerEntry(0x12345678).getBytes());
}
}

View file

@ -0,0 +1,13 @@
package nl.sander.beejava.constantpool.entry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class LongEntryTest {
@Test
void getBytes() {
assertArrayEquals(new byte[]{5, 18, 52, 86, 120, -102, -68, -34, -16}, new LongEntry(0x123456789ABCDEF0L).getBytes());
}
}