1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.graalvm.compiler.hotspot.test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
  30 import org.junit.Assert;
  31 import org.junit.Assume;
  32 import org.junit.Test;
  33 
  34 public class BoxDeoptimizationTest extends GraalCompilerTest {
  35     private static boolean isJDK13OrLater = JavaVersionUtil.JAVA_SPECIFICATION_VERSION >= 13;
  36 
  37     public static void testInteger() {
  38         Object[] values = {42, new Exception()};
  39         GraalDirectives.deoptimize();
  40         Assert.assertSame(values[0], Integer.valueOf(42));
  41     }
  42 
  43     @Test
  44     public void test1() {
  45         Assume.assumeTrue(isJDK13OrLater);
  46         test("testInteger");
  47     }
  48 
  49     public static void testLong() {
  50         Object[] values = {42L, new Exception()};
  51         GraalDirectives.deoptimize();
  52         Assert.assertSame(values[0], Long.valueOf(42));
  53     }
  54 
  55     @Test
  56     public void test2() {
  57         Assume.assumeTrue(isJDK13OrLater);
  58         test("testLong");
  59     }
  60 
  61     public static void testChar() {
  62         Object[] values = {'a', new Exception()};
  63         GraalDirectives.deoptimize();
  64         Assert.assertSame(values[0], Character.valueOf('a'));
  65     }
  66 
  67     @Test
  68     public void test3() {
  69         Assume.assumeTrue(isJDK13OrLater);
  70         test("testChar");
  71     }
  72 
  73     public static void testShort() {
  74         Object[] values = {(short) 42, new Exception()};
  75         GraalDirectives.deoptimize();
  76         Assert.assertSame(values[0], Short.valueOf((short) 42));
  77     }
  78 
  79     @Test
  80     public void test4() {
  81         Assume.assumeTrue(isJDK13OrLater);
  82         test("testShort");
  83     }
  84 
  85     public static void testByte() {
  86         Object[] values = {(byte) 42, new Exception()};
  87         GraalDirectives.deoptimize();
  88         Assert.assertSame(values[0], Byte.valueOf((byte) 42));
  89     }
  90 
  91     @Test
  92     public void test5() {
  93         Assume.assumeTrue(isJDK13OrLater);
  94         test("testByte");
  95     }
  96 
  97     public static void testBoolean() {
  98         Object[] values = {true, new Exception()};
  99         GraalDirectives.deoptimize();
 100         Assert.assertSame(values[0], Boolean.valueOf(true));
 101     }
 102 
 103     @Test
 104     public void test6() {
 105         Assume.assumeTrue(isJDK13OrLater);
 106         test("testBoolean");
 107     }
 108 }