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