1 /*
   2  * Copyright (c) 2016, 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 package compiler.jvmci.constantReflectionProviderTest;
  25 
  26 import static compiler.jvmci.constantReflectionProviderTest.TestHelper.CONSTANT_REFLECTION_PROVIDER;
  27 import static compiler.jvmci.constantReflectionProviderTest.TestHelper.DUMMY_CLASS_INSTANCE;
  28 import java.util.LinkedList;
  29 import jdk.vm.ci.meta.JavaConstant;
  30 import org.testng.annotations.DataProvider;
  31 
  32 public class BoxPrimitiveDataProvider {
  33 
  34     @DataProvider(name = "boxPrimitiveDataProvider")
  35     public static Object[][] boxPrimitiveDataProvider() {
  36         LinkedList<Object[]> cfgSet = new LinkedList<>();
  37         // Boolean testing
  38         cfgSet.add(new Object[]{JavaConstant.forBoolean(true),
  39                 CONSTANT_REFLECTION_PROVIDER.forObject((Boolean) true)});
  40         cfgSet.add(new Object[]{JavaConstant.forBoolean(false),
  41                 CONSTANT_REFLECTION_PROVIDER.forObject((Boolean) false)});
  42         // Boxed boolean testing (returns null)
  43         cfgSet.add(new Object[]{
  44                 CONSTANT_REFLECTION_PROVIDER.forObject((Boolean) true), null});
  45         cfgSet.add(new Object[]{
  46                 CONSTANT_REFLECTION_PROVIDER.forObject((Boolean) false), null});
  47         for (byte number : new byte[]{-128, 0, 1, 127}) {
  48             // Integer primitives testing
  49             cfgSet.add(new Object[]{JavaConstant.forByte((byte) number),
  50                     CONSTANT_REFLECTION_PROVIDER.forObject(Byte.valueOf(number))});
  51             cfgSet.add(new Object[]{JavaConstant.forShort(number),
  52                     CONSTANT_REFLECTION_PROVIDER.forObject(Short.valueOf(number))});
  53             cfgSet.add(new Object[]{JavaConstant.forInt(number),
  54                     CONSTANT_REFLECTION_PROVIDER.forObject(Integer.valueOf(number))});
  55             cfgSet.add(new Object[]{JavaConstant.forLong(number),
  56                     CONSTANT_REFLECTION_PROVIDER.forObject(Long.valueOf(number))});
  57             if (number >= 0) {
  58                 cfgSet.add(new Object[]{JavaConstant.forChar((char) number),
  59                         CONSTANT_REFLECTION_PROVIDER.forObject(Character.valueOf((char) number))});
  60             }
  61             // Float and Double variables are not cached,
  62             // so the tested method returns "null" on them
  63             cfgSet.add(new Object[]{JavaConstant.forFloat((float) number), null});
  64             cfgSet.add(new Object[]{JavaConstant.forDouble((double) number), null});
  65             // Boxed primitives testing (return null)
  66             cfgSet.add(new Object[]{
  67                 CONSTANT_REFLECTION_PROVIDER.forObject(Byte.valueOf(number)), null});
  68             cfgSet.add(new Object[]{
  69                 CONSTANT_REFLECTION_PROVIDER.forObject(Short.valueOf(number) ), null});
  70             cfgSet.add(new Object[]{
  71                 CONSTANT_REFLECTION_PROVIDER.forObject(Integer.valueOf(number)), null});
  72             cfgSet.add(new Object[]{
  73                 CONSTANT_REFLECTION_PROVIDER.forObject(Long.valueOf(number)), null});
  74             cfgSet.add(new Object[]{
  75                 CONSTANT_REFLECTION_PROVIDER.forObject(Character.valueOf((char) number)), null});
  76             cfgSet.add(new Object[]{
  77                 CONSTANT_REFLECTION_PROVIDER.forObject(Float.valueOf(number)), null});
  78             cfgSet.add(new Object[]{
  79                 CONSTANT_REFLECTION_PROVIDER.forObject(Double.valueOf(number)), null});
  80         }
  81         // Integer primitives testing with big non-cached values (returns null)
  82         cfgSet.add(new Object[]{JavaConstant.forShort(Short.MAX_VALUE), null});
  83         cfgSet.add(new Object[]{JavaConstant.forInt(Integer.MAX_VALUE), null});
  84         cfgSet.add(new Object[]{JavaConstant.forLong(Long.MAX_VALUE), null});
  85         cfgSet.add(new Object[]{JavaConstant.forChar(Character.MAX_VALUE), null});
  86         // Non-primitives testing
  87         cfgSet.add(new Object[]{
  88             CONSTANT_REFLECTION_PROVIDER.forObject(DUMMY_CLASS_INSTANCE.objectField), null});
  89         cfgSet.add(new Object[]{
  90             CONSTANT_REFLECTION_PROVIDER.forObject(DUMMY_CLASS_INSTANCE.booleanArrayWithValues), null});
  91         // Null testing
  92         cfgSet.add(new Object[]{JavaConstant.NULL_POINTER, null});
  93         cfgSet.add(new Object[]{null, null});
  94         return cfgSet.toArray(new Object[0][0]);
  95     }
  96 }