Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestConstantReflectionProvider.java @ 21663:381ab4105afe
moved com.oracle.graal.java.test to com.oracle.jvmci.runtime.test
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 02 Jun 2015 15:15:58 +0200 |
parents | graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestConstantReflectionProvider.java@48c1ebd24120 |
children |
comparison
equal
deleted
inserted
replaced
21662:b45e0f791465 | 21663:381ab4105afe |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2014, 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 package com.oracle.jvmci.runtime.test; | |
24 | |
25 import static org.junit.Assert.*; | |
26 | |
27 import java.lang.reflect.*; | |
28 import java.util.*; | |
29 | |
30 import org.junit.*; | |
31 | |
32 import com.oracle.jvmci.meta.*; | |
33 | |
34 /** | |
35 * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that | |
36 * actually returns non-null results for access operations that are possible, i.e., the tests will | |
37 * fail for an implementation that spuriously returns null (which is allowed by the specification). | |
38 */ | |
39 public class TestConstantReflectionProvider extends TypeUniverse { | |
40 | |
41 @Test | |
42 public void constantEqualsTest() { | |
43 for (ConstantValue c1 : constants()) { | |
44 for (ConstantValue c2 : constants()) { | |
45 // test symmetry | |
46 assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value)); | |
47 if (c1.value.getKind() != Kind.Object && c2.value.getKind() != Kind.Object) { | |
48 assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value)); | |
49 } | |
50 } | |
51 } | |
52 } | |
53 | |
54 @Test | |
55 public void readArrayLengthTest() { | |
56 for (ConstantValue cv : constants()) { | |
57 JavaConstant c = cv.value; | |
58 Integer actual = constantReflection.readArrayLength(c); | |
59 if (c.getKind() != Kind.Object || c.isNull() || !cv.boxed.getClass().isArray()) { | |
60 assertNull(actual); | |
61 } else { | |
62 assertNotNull(actual); | |
63 int actualInt = actual; | |
64 assertEquals(Array.getLength(cv.boxed), actualInt); | |
65 } | |
66 } | |
67 } | |
68 | |
69 static class PrimitiveConstants { | |
70 static final long LONG_CONST = 42; | |
71 static final int INT_CONST = 66; | |
72 static final byte BYTE_CONST = 123; | |
73 static final boolean BOOL_CONST = true; | |
74 } | |
75 | |
76 static class BoxedConstants { | |
77 static final Long LONG_CONST = 42L; | |
78 static final Integer INT_CONST = 66; | |
79 static final Byte BYTE_CONST = 123; | |
80 static final Boolean BOOL_CONST = true; | |
81 } | |
82 | |
83 @Test | |
84 public void boxTest() { | |
85 for (ConstantValue cv : constants()) { | |
86 JavaConstant c = cv.value; | |
87 JavaConstant boxed = constantReflection.boxPrimitive(c); | |
88 if (boxed != null && c.getKind().isPrimitive()) { | |
89 assertTrue(boxed.getKind().isObject()); | |
90 assertFalse(boxed.isNull()); | |
91 } | |
92 } | |
93 | |
94 List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class); | |
95 List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class); | |
96 for (int i = 0; i < primitiveConstants.size(); i++) { | |
97 ConstantValue prim = primitiveConstants.get(i); | |
98 ConstantValue box = boxedConstants.get(i); | |
99 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 } |