001/*
002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.runtime.test;
024
025import static org.junit.Assert.*;
026
027import java.lang.reflect.*;
028import java.util.*;
029
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034/**
035 * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that
036 * actually returns non-null results for access operations that are possible, i.e., the tests will
037 * fail for an implementation that spuriously returns null (which is allowed by the specification).
038 */
039public class TestConstantReflectionProvider extends TypeUniverse {
040
041    @Test
042    public void constantEqualsTest() {
043        for (ConstantValue c1 : constants()) {
044            for (ConstantValue c2 : constants()) {
045                // test symmetry
046                assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value));
047                if (c1.value.getKind() != Kind.Object && c2.value.getKind() != Kind.Object) {
048                    assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value));
049                }
050            }
051        }
052    }
053
054    @Test
055    public void readArrayLengthTest() {
056        for (ConstantValue cv : constants()) {
057            JavaConstant c = cv.value;
058            Integer actual = constantReflection.readArrayLength(c);
059            if (c.getKind() != Kind.Object || c.isNull() || !cv.boxed.getClass().isArray()) {
060                assertNull(actual);
061            } else {
062                assertNotNull(actual);
063                int actualInt = actual;
064                assertEquals(Array.getLength(cv.boxed), actualInt);
065            }
066        }
067    }
068
069    static class PrimitiveConstants {
070        static final long LONG_CONST = 42;
071        static final int INT_CONST = 66;
072        static final byte BYTE_CONST = 123;
073        static final boolean BOOL_CONST = true;
074    }
075
076    static class BoxedConstants {
077        static final Long LONG_CONST = 42L;
078        static final Integer INT_CONST = 66;
079        static final Byte BYTE_CONST = 123;
080        static final Boolean BOOL_CONST = true;
081    }
082
083    @Test
084    public void boxTest() {
085        for (ConstantValue cv : constants()) {
086            JavaConstant c = cv.value;
087            JavaConstant boxed = constantReflection.boxPrimitive(c);
088            if (boxed != null && c.getKind().isPrimitive()) {
089                assertTrue(boxed.getKind().isObject());
090                assertFalse(boxed.isNull());
091            }
092        }
093
094        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
095        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
096        for (int i = 0; i < primitiveConstants.size(); i++) {
097            ConstantValue prim = primitiveConstants.get(i);
098            ConstantValue box = boxedConstants.get(i);
099            assertEquals(box.value, constantReflection.boxPrimitive(prim.value));
100        }
101
102        assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER));
103    }
104
105    @Test
106    public void unboxTest() {
107        for (ConstantValue cv : constants()) {
108            JavaConstant c = cv.value;
109            JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c);
110            if (unboxed != null) {
111                assertFalse(unboxed.getKind().isObject());
112            }
113        }
114        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
115        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
116        for (int i = 0; i < primitiveConstants.size(); i++) {
117            ConstantValue prim = primitiveConstants.get(i);
118            ConstantValue box = boxedConstants.get(i);
119            assert prim.getSimpleName().equals(box.getSimpleName());
120            assertEquals(prim.value, constantReflection.unboxPrimitive(box.value));
121        }
122
123        assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER));
124    }
125}