add test for sun.misc.Unsafe functionality

This commit is contained in:
Joel Dice 2012-03-13 08:28:33 -06:00
parent 4aefa211a3
commit 756f58210a
3 changed files with 122 additions and 0 deletions

View File

@ -10,8 +10,16 @@
package avian;
import sun.misc.Unsafe;
public abstract class Machine {
private static final Unsafe unsafe = Unsafe.getUnsafe();
public static native void dumpHeap(String outputFile);
public static Unsafe getUnsafe() {
return unsafe;
}
}

View File

@ -0,0 +1,49 @@
package sun.misc;
public final class Unsafe {
private void Unsafe() { }
private static final Unsafe Instance = new Unsafe();
public static Unsafe getUnsafe() {
return Instance;
}
public native long allocateMemory(long bytes);
public native void setMemory(long address, long count, byte value);
public native void freeMemory(long address);
public native byte getByte(long address);
public native void putByte(long address, byte x);
public native short getShort(long address);
public native void putShort(long address, short x);
public native char getChar(long address);
public native void putChar(long address, char x);
public native int getInt(long address);
public native void putInt(long address, int x);
public native long getLong(long address);
public native void putLong(long address, long x);
public native float getFloat(long address);
public native void putFloat(long address, float x);
public native double getDouble(long address);
public native void putDouble(long address, double x);
public native long getAddress(long address);
public native void putAddress(long address, long x);
}

65
test/UnsafeTest.java Normal file
View File

@ -0,0 +1,65 @@
import sun.misc.Unsafe;
public class UnsafeTest {
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) {
Unsafe u = avian.Machine.getUnsafe();
final long size = 64;
long memory = u.allocateMemory(size);
try {
for (int i = 0; i < size; ++i)
u.putByte(memory + i, (byte) 42);
for (int i = 0; i < size; ++i)
expect(u.getByte(memory + i) == 42);
for (int i = 0; i < size / 2; ++i)
u.putShort(memory + (i * 2), (short) -12345);
for (int i = 0; i < size / 2; ++i)
expect(u.getShort(memory + (i * 2)) == -12345);
for (int i = 0; i < size / 2; ++i)
u.putChar(memory + (i * 2), (char) 23456);
for (int i = 0; i < size / 2; ++i)
expect(u.getChar(memory + (i * 2)) == 23456);
for (int i = 0; i < size / 4; ++i)
u.putInt(memory + (i * 4), 0x12345678);
for (int i = 0; i < size / 4; ++i)
expect(u.getInt(memory + (i * 4)) == 0x12345678);
for (int i = 0; i < size / 4; ++i)
u.putFloat(memory + (i * 4), 1.2345678F);
for (int i = 0; i < size / 4; ++i)
expect(u.getFloat(memory + (i * 4)) == 1.2345678F);
for (int i = 0; i < size / 8; ++i)
u.putLong(memory + (i * 8), 0x1234567890ABCDEFL);
for (int i = 0; i < size / 8; ++i)
expect(u.getLong(memory + (i * 8)) == 0x1234567890ABCDEFL);
for (int i = 0; i < size / 8; ++i)
u.putDouble(memory + (i * 8), 1.23456789012345D);
for (int i = 0; i < size / 8; ++i)
expect(u.getDouble(memory + (i * 8)) == 1.23456789012345D);
for (int i = 0; i < size / 8; ++i)
u.putAddress(memory + (i * 8), 0x12345678);
for (int i = 0; i < size / 8; ++i)
expect(u.getAddress(memory + (i * 8)) == 0x12345678);
} finally {
u.freeMemory(memory);
}
}
}