1 /*
2  * Copyright (c) 2011, 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 package org.graalvm.compiler.replacements.test;
26 
27 import org.junit.Test;
28 
29 import org.graalvm.compiler.core.test.GraalCompilerTest;
30 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode;
31 
32 /**
33  * Tests for {@link InstanceOfDynamicNode}.
34  */
35 public class InstanceOfDynamicTest extends GraalCompilerTest {
36 
id(int value)37     public static int id(int value) {
38         return value;
39     }
40 
41     @Test
test100()42     public void test100() {
43         final Object nul = null;
44         test("isStringDynamic", nul);
45         test("isStringDynamic", "object");
46         test("isStringDynamic", Object.class);
47     }
48 
49     @Test
test101()50     public void test101() {
51         final Object nul = null;
52         test("isStringIntDynamic", nul);
53         test("isStringIntDynamic", "object");
54         test("isStringIntDynamic", Object.class);
55     }
56 
57     @Test
test103()58     public void test103() {
59         test("isInstanceDynamic", String.class, null);
60         test("isInstanceDynamic", String.class, "object");
61         test("isInstanceDynamic", String.class, Object.class);
62         test("isInstanceDynamic", int.class, null);
63         test("isInstanceDynamic", int.class, "Object");
64         test("isInstanceDynamic", int.class, Object.class);
65     }
66 
67     @Test
test104()68     public void test104() {
69         test("isInstanceIntDynamic", String.class, null);
70         test("isInstanceIntDynamic", String.class, "object");
71         test("isInstanceIntDynamic", String.class, Object.class);
72         test("isInstanceIntDynamic", int.class, null);
73         test("isInstanceIntDynamic", int.class, "Object");
74         test("isInstanceIntDynamic", int.class, Object.class);
75     }
76 
isStringDynamic(Object o)77     public static boolean isStringDynamic(Object o) {
78         return String.class.isInstance(o);
79     }
80 
isStringIntDynamic(Object o)81     public static int isStringIntDynamic(Object o) {
82         if (String.class.isInstance(o)) {
83             return o.toString().length();
84         }
85         return o.getClass().getName().length();
86     }
87 
isInstanceDynamic(Class<?> c, Object o)88     public static boolean isInstanceDynamic(Class<?> c, Object o) {
89         return c.isInstance(o);
90     }
91 
isInstanceIntDynamic(Class<?> c, Object o)92     public static int isInstanceIntDynamic(Class<?> c, Object o) {
93         if (c.isInstance(o)) {
94             return o.toString().length();
95         }
96         return o.getClass().getName().length();
97     }
98 }
99