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