diff options
author | 2014-02-10 16:19:09 -0800 | |
---|---|---|
committer | 2014-02-11 13:16:20 -0800 | |
commit | 4d2efce8bf1947880b90efc44448b4940c8016fb (patch) | |
tree | 61adad3b981719a12d00aa9be44f76c004dd44c4 /test/UnsafeTest/UnsafeTest.java | |
parent | 6b3697fec487b355d107b693c965919bf5fff906 (diff) |
Don't hardcode object layout in Unsafe and space_test.
Add a test for Unsafe.arrayBaseOffset() and Unsafe.arrayIndexScale().
Change-Id: I9cbdb79a4a7ee055129f41811a117910c8b2febd
Diffstat (limited to 'test/UnsafeTest/UnsafeTest.java')
-rw-r--r-- | test/UnsafeTest/UnsafeTest.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/UnsafeTest/UnsafeTest.java b/test/UnsafeTest/UnsafeTest.java new file mode 100644 index 0000000000..f3d5289627 --- /dev/null +++ b/test/UnsafeTest/UnsafeTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.lang.reflect.Field; +import sun.misc.Unsafe; + +public class UnsafeTest { + static { + System.loadLibrary("arttest"); + } + + private static void check(int actual, int expected, String msg) { + if (actual != expected) { + System.logE(msg + " : " + actual + " != " + expected); + System.exit(-1); + } + } + + private static Unsafe getUnsafe() throws Exception { + Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); + Field f = unsafeClass.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (Unsafe) f.get(null); + } + + public static void main(String[] args) throws Exception { + Unsafe unsafe = getUnsafe(); + check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class), + "Unsafe.arrayBaseOffset(boolean[])"); + check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class), + "Unsafe.arrayBaseOffset(byte[])"); + check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class), + "Unsafe.arrayBaseOffset(char[])"); + check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class), + "Unsafe.arrayBaseOffset(double[])"); + check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class), + "Unsafe.arrayBaseOffset(float[])"); + check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class), + "Unsafe.arrayBaseOffset(int[])"); + check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class), + "Unsafe.arrayBaseOffset(long[])"); + check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class), + "Unsafe.arrayBaseOffset(Object[])"); + + check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class), + "Unsafe.arrayIndexScale(boolean[])"); + check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class), + "Unsafe.arrayIndexScale(byte[])"); + check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class), + "Unsafe.arrayIndexScale(char[])"); + check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class), + "Unsafe.arrayIndexScale(double[])"); + check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class), + "Unsafe.arrayIndexScale(float[])"); + check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class), + "Unsafe.arrayIndexScale(int[])"); + check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class), + "Unsafe.arrayIndexScale(long[])"); + check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class), + "Unsafe.arrayIndexScale(Object[])"); + } + + private static native int vmArrayBaseOffset(Class clazz); + private static native int vmArrayIndexScale(Class clazz); +} |