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.IRNode;
  27 import jdk.test.lib.jittester.Literal;
  28 import jdk.test.lib.jittester.ProductionFailedException;
  29 import jdk.test.lib.jittester.ProductionParams;
  30 import jdk.test.lib.jittester.Type;
  31 import jdk.test.lib.jittester.TypeList;
  32 import jdk.test.lib.jittester.types.TypeBoolean;
  33 import jdk.test.lib.jittester.types.TypeByte;
  34 import jdk.test.lib.jittester.types.TypeChar;
  35 import jdk.test.lib.jittester.types.TypeDouble;
  36 import jdk.test.lib.jittester.types.TypeFloat;
  37 import jdk.test.lib.jittester.types.TypeInt;
  38 import jdk.test.lib.jittester.types.TypeLong;
  39 import jdk.test.lib.jittester.types.TypeShort;
  40 import jdk.test.lib.jittester.utils.PseudoRandom;
  41 
  42 class LiteralFactory extends Factory {
  43     protected final Type resultType;
  44 
  45     LiteralFactory(Type resultType) {
  46         this.resultType = resultType;
  47     }
  48 
  49     @Override
  50     public IRNode produce() throws ProductionFailedException {
  51         Literal literal;
  52         if (resultType.equals(new TypeBoolean())) {
  53             literal = new Literal(PseudoRandom.randomBoolean(), new TypeBoolean());
  54         } else if (resultType.equals(new TypeChar())) {
  55             literal = new Literal((char) ((char) (PseudoRandom.random() * ('z' - 'A')) + 'A'), new TypeChar());
  56         } else if (resultType.equals(new TypeInt())) {
  57             literal = new Literal((int) (PseudoRandom.random() * Integer.MAX_VALUE), new TypeInt());
  58         } else if (resultType.equals(new TypeLong())) {
  59             literal = new Literal((long) (PseudoRandom.random() * Long.MAX_VALUE), new TypeLong());
  60         } else if (resultType.equals(new TypeFloat())) {
  61             literal = new Literal((float) (PseudoRandom.random() * Float.MAX_VALUE), new TypeFloat());
  62         } else if (resultType.equals(new TypeDouble())) {
  63             literal = new Literal(PseudoRandom.random() * Double.MAX_VALUE, new TypeDouble());
  64         } else if (resultType.equals(new TypeByte())) {
  65             literal = new Literal((byte)(PseudoRandom.random() * Byte.MAX_VALUE),new TypeByte());
  66         } else if (resultType.equals(new TypeShort())) {
  67             literal = new Literal((short)(PseudoRandom.random() * Short.MAX_VALUE), new TypeShort());
  68         } else if (resultType.equals(TypeList.find("java.lang.String"))) {
  69             int size = (int) (PseudoRandom.random() * ProductionParams.stringLiteralSizeLimit.value());
  70             byte[] str = new byte[size];
  71             for (int i = 0; i < size; i++) {
  72                 str[i] = (byte) ((int) (('z' - 'a') * PseudoRandom.random()) + 'a');
  73             }
  74             literal = new Literal("\"" + new String(str) + "\"", TypeList.find("java.lang.String"));
  75         } else {
  76             throw new ProductionFailedException();
  77         }
  78         return literal;
  79     }
  80 }