TestConstantReflectionProvider.java revision 10420:c558850fac57
1/*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
27 * @library ../../../../../
28 * @modules jdk.vm.ci/jdk.vm.ci.meta
29 *          jdk.vm.ci/jdk.vm.ci.runtime
30 * @build jdk.vm.ci.runtime.test.TestConstantReflectionProvider
31 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestConstantReflectionProvider
32 */
33
34package jdk.vm.ci.runtime.test;
35
36import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.assertFalse;
38import static org.junit.Assert.assertNotNull;
39import static org.junit.Assert.assertNull;
40import static org.junit.Assert.assertTrue;
41
42import java.lang.reflect.Array;
43import java.util.List;
44
45import jdk.vm.ci.meta.ConstantReflectionProvider;
46import jdk.vm.ci.meta.JavaConstant;
47import jdk.vm.ci.meta.JavaKind;
48
49import org.junit.Test;
50
51/**
52 * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that
53 * actually returns non-null results for access operations that are possible, i.e., the tests will
54 * fail for an implementation that spuriously returns null (which is allowed by the specification).
55 */
56public class TestConstantReflectionProvider extends TypeUniverse {
57
58    @Test
59    public void constantEqualsTest() {
60        for (ConstantValue c1 : constants()) {
61            for (ConstantValue c2 : constants()) {
62                // test symmetry
63                assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value));
64                if (c1.value.getJavaKind() != JavaKind.Object && c2.value.getJavaKind() != JavaKind.Object) {
65                    assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value));
66                }
67            }
68        }
69    }
70
71    @Test
72    public void readArrayLengthTest() {
73        for (ConstantValue cv : constants()) {
74            JavaConstant c = cv.value;
75            Integer actual = constantReflection.readArrayLength(c);
76            if (c.getJavaKind() != JavaKind.Object || c.isNull() || !cv.boxed.getClass().isArray()) {
77                assertNull(actual);
78            } else {
79                assertNotNull(actual);
80                int actualInt = actual;
81                assertEquals(Array.getLength(cv.boxed), actualInt);
82            }
83        }
84    }
85
86    static class PrimitiveConstants {
87        static final long LONG_CONST = 42;
88        static final int INT_CONST = 66;
89        static final byte BYTE_CONST = 123;
90        static final boolean BOOL_CONST = true;
91    }
92
93    static class BoxedConstants {
94        static final Long LONG_CONST = 42L;
95        static final Integer INT_CONST = 66;
96        static final Byte BYTE_CONST = 123;
97        static final Boolean BOOL_CONST = true;
98    }
99
100    @Test
101    public void boxTest() {
102        for (ConstantValue cv : constants()) {
103            JavaConstant c = cv.value;
104            JavaConstant boxed = constantReflection.boxPrimitive(c);
105            if (boxed != null && c.getJavaKind().isPrimitive()) {
106                assertTrue(boxed.getJavaKind().isObject());
107                assertFalse(boxed.isNull());
108            }
109        }
110
111        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
112        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
113        for (int i = 0; i < primitiveConstants.size(); i++) {
114            ConstantValue prim = primitiveConstants.get(i);
115            ConstantValue box = boxedConstants.get(i);
116            assertEquals(box.value, constantReflection.boxPrimitive(prim.value));
117        }
118
119        assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER));
120    }
121
122    @Test
123    public void unboxTest() {
124        for (ConstantValue cv : constants()) {
125            JavaConstant c = cv.value;
126            JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c);
127            if (unboxed != null) {
128                assertFalse(unboxed.getJavaKind().isObject());
129            }
130        }
131        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
132        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
133        for (int i = 0; i < primitiveConstants.size(); i++) {
134            ConstantValue prim = primitiveConstants.get(i);
135            ConstantValue box = boxedConstants.get(i);
136            assert prim.getSimpleName().equals(box.getSimpleName());
137            assertEquals(prim.value, constantReflection.unboxPrimitive(box.value));
138        }
139
140        assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER));
141    }
142}
143