1 /*
   2  * Copyright (c) 2013, 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 java.lang.reflect.Array;
  26 
  27 import org.junit.Test;
  28 
  29 import org.graalvm.compiler.core.test.GraalCompilerTest;
  30 
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 /**
  34  * Tests the implementation of Array.createInstance.
  35  */
  36 public class DynamicNewArrayTest extends GraalCompilerTest {
  37 
  38     private class Element {
  39     }
  40 
  41     @Test
  42     public void test1() {
  43         test("test1snippet");
  44     }
  45 
  46     @Test
  47     public void test2() {
  48         test("test2snippet");
  49     }
  50 
  51     @Test
  52     public void test3() {
  53         test("dynamic", Long.class, 7);
  54     }
  55 
  56     @Test
  57     public void test4() {
  58         test("dynamic", Boolean.class, -7);
  59         test("dynamicSynchronized", Boolean.class, -7);
  60     }
  61 
  62     @Test
  63     public void test5() {
  64         test("dynamic", byte.class, 7);
  65     }
  66 
  67     @Test
  68     public void test6() {
  69         test("dynamic", null, 5);
  70     }
  71 
  72     @Test
  73     public void test7() {
  74         test("dynamic", void.class, 5);
  75     }
  76 
  77     @Test
  78     public void testStub() {
  79         ResolvedJavaMethod method = getResolvedJavaMethod("dynamic");
  80         // this will use the stub call because Element[] is not loaded
  81         Result actual1 = executeActual(method, null, Element.class, 7);
  82         // this call will use the fast path
  83         Result actual2 = executeActual(method, null, Element.class, 7);
  84         Result expected = executeExpected(method, null, Element.class, 7);
  85         assertEquals(actual1, expected);
  86         assertEquals(actual2, expected);
  87     }
  88 
  89     public static Object test1snippet() {
  90         return Array.newInstance(Integer.class, 7);
  91     }
  92 
  93     public static Object test2snippet() {
  94         return Array.newInstance(char.class, 7);
  95     }
  96 
  97     public static Object dynamic(Class<?> elementType, int length) {
  98         return Array.newInstance(elementType, length);
  99     }
 100 
 101     public static synchronized Object dynamicSynchronized(Class<?> elementType, int length) {
 102         return Array.newInstance(elementType, length);
 103     }
 104 }