1 /*
   2  * Copyright (c) 2015, 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.test.lib.jittester.factories;
  25 
  26 import jdk.test.lib.jittester.Literal;
  27 import jdk.test.lib.jittester.ProductionFailedException;
  28 import jdk.test.lib.jittester.ProductionParams;
  29 import jdk.test.lib.jittester.Type;
  30 import jdk.test.lib.jittester.TypeList;
  31 import jdk.test.lib.jittester.utils.PseudoRandom;
  32 
  33 import java.util.Locale;
  34 
  35 class LiteralFactory extends Factory<Literal> {
  36     protected final Type resultType;
  37 
  38     LiteralFactory(Type resultType) {
  39         this.resultType = resultType;
  40     }
  41 
  42     @Override
  43     public Literal produce() throws ProductionFailedException {
  44         Literal literal;
  45         if (resultType.equals(TypeList.BOOLEAN)) {
  46             literal = new Literal(PseudoRandom.randomBoolean(), TypeList.BOOLEAN);
  47         } else if (resultType.equals(TypeList.CHAR)) {
  48             literal = new Literal((char) ((char) (PseudoRandom.random() * ('z' - 'A')) + 'A'), TypeList.CHAR);
  49         } else if (resultType.equals(TypeList.INT)) {
  50             literal = new Literal((int) (PseudoRandom.random() * Integer.MAX_VALUE), TypeList.INT);
  51         } else if (resultType.equals(TypeList.LONG)) {
  52             literal = new Literal((long) (PseudoRandom.random() * Long.MAX_VALUE), TypeList.LONG);
  53         } else if (resultType.equals(TypeList.FLOAT)) {
  54             literal = new Literal(new Float(String.format(
  55                     (Locale) null,
  56                     "%." + ProductionParams.floatingPointPrecision.value() + "EF",
  57                     (float) PseudoRandom.random() * Float.MAX_VALUE)),
  58                     TypeList.FLOAT);
  59         } else if (resultType.equals(TypeList.DOUBLE)) {
  60             literal = new Literal(new Double(String.format(
  61                     (Locale) null,
  62                     "%." + 2 * ProductionParams.floatingPointPrecision.value() + "E",
  63                     PseudoRandom.random() * Double.MAX_VALUE)),
  64                     TypeList.DOUBLE);
  65         } else if (resultType.equals(TypeList.BYTE)) {
  66             literal = new Literal((byte)(PseudoRandom.random() * Byte.MAX_VALUE), TypeList.BYTE);
  67         } else if (resultType.equals(TypeList.SHORT)) {
  68             literal = new Literal((short)(PseudoRandom.random() * Short.MAX_VALUE), TypeList.SHORT);
  69         } else if (resultType.equals(TypeList.STRING)) {
  70             int size = (int) (PseudoRandom.random() * ProductionParams.stringLiteralSizeLimit.value());
  71             byte[] str = new byte[size];
  72             for (int i = 0; i < size; i++) {
  73                 str[i] = (byte) ((int) (('z' - 'a') * PseudoRandom.random()) + 'a');
  74             }
  75             literal = new Literal(new String(str), TypeList.STRING);
  76         } else {
  77             throw new ProductionFailedException();
  78         }
  79         return literal;
  80     }
  81 }