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 (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
  27  * @library ../../../../../
  28  * @modules jdk.vm.ci/jdk.vm.ci.meta
  29  *          jdk.vm.ci/jdk.vm.ci.runtime
  30  *          java.base/jdk.internal.misc
  31  * @build jdk.vm.ci.runtime.test.TestConstantReflectionProvider
  32  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestConstantReflectionProvider
  33  */
  34 
  35 package jdk.vm.ci.runtime.test;
  36 
  37 import static org.junit.Assert.assertEquals;
  38 import static org.junit.Assert.assertFalse;
  39 import static org.junit.Assert.assertNotNull;
  40 import static org.junit.Assert.assertNull;
  41 import static org.junit.Assert.assertTrue;
  42 
  43 import java.lang.reflect.Array;
  44 import java.util.List;
  45 
  46 import jdk.vm.ci.meta.ConstantReflectionProvider;
  47 import jdk.vm.ci.meta.JavaConstant;
  48 import jdk.vm.ci.meta.JavaKind;
  49 
  50 import org.junit.Test;
  51 
  52 /**
  53  * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that
  54  * actually returns non-null results for access operations that are possible, i.e., the tests will
  55  * fail for an implementation that spuriously returns null (which is allowed by the specification).
  56  */
  57 public class TestConstantReflectionProvider extends TypeUniverse {
  58 
  59     @Test
  60     public void constantEqualsTest() {
  61         for (ConstantValue c1 : constants()) {
  62             for (ConstantValue c2 : constants()) {
  63                 // test symmetry
  64                 assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value));
  65                 if (c1.value.getJavaKind() != JavaKind.Object && c2.value.getJavaKind() != JavaKind.Object) {
  66                     assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value));
  67                 }
  68             }
  69         }
  70     }
  71 
  72     @Test
  73     public void readArrayLengthTest() {
  74         for (ConstantValue cv : constants()) {
  75             JavaConstant c = cv.value;
  76             Integer actual = constantReflection.readArrayLength(c);
  77             if (c.getJavaKind() != JavaKind.Object || c.isNull() || !cv.boxed.getClass().isArray()) {
  78                 assertNull(actual);
  79             } else {
  80                 assertNotNull(actual);
  81                 int actualInt = actual;
  82                 assertEquals(Array.getLength(cv.boxed), actualInt);
  83             }
  84         }
  85     }
  86 
  87     static class PrimitiveConstants {
  88         static final long LONG_CONST = 42;
  89         static final int INT_CONST = 66;
  90         static final byte BYTE_CONST = 123;
  91         static final boolean BOOL_CONST = true;
  92     }
  93 
  94     static class BoxedConstants {
  95         static final Long LONG_CONST = 42L;
  96         static final Integer INT_CONST = 66;
  97         static final Byte BYTE_CONST = 123;
  98         static final Boolean BOOL_CONST = true;
  99     }
 100 
 101     @Test
 102     public void boxTest() {
 103         for (ConstantValue cv : constants()) {
 104             JavaConstant c = cv.value;
 105             JavaConstant boxed = constantReflection.boxPrimitive(c);
 106             if (boxed != null && c.getJavaKind().isPrimitive()) {
 107                 assertTrue(boxed.getJavaKind().isObject());
 108                 assertFalse(boxed.isNull());
 109             }
 110         }
 111 
 112         List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
 113         List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
 114         for (int i = 0; i < primitiveConstants.size(); i++) {
 115             ConstantValue prim = primitiveConstants.get(i);
 116             ConstantValue box = boxedConstants.get(i);
 117             assertEquals(box.value, constantReflection.boxPrimitive(prim.value));
 118         }
 119 
 120         assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER));
 121     }
 122 
 123     @Test
 124     public void unboxTest() {
 125         for (ConstantValue cv : constants()) {
 126             JavaConstant c = cv.value;
 127             JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c);
 128             if (unboxed != null) {
 129                 assertFalse(unboxed.getJavaKind().isObject());
 130             }
 131         }
 132         List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
 133         List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
 134         for (int i = 0; i < primitiveConstants.size(); i++) {
 135             ConstantValue prim = primitiveConstants.get(i);
 136             ConstantValue box = boxedConstants.get(i);
 137             assert prim.getSimpleName().equals(box.getSimpleName());
 138             assertEquals(prim.value, constantReflection.unboxPrimitive(box.value));
 139         }
 140 
 141         assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER));
 142     }
 143 }