1 /*
   2  * Copyright (c) 2012, 2018, 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 package vm.compiler.optimizations.stringconcat.explicit;
  24 
  25 import nsk.share.StringGoldChecker;
  26 import vm.compiler.share.CompilerTest;
  27 import vm.compiler.share.CompilerTestLauncher;
  28 import vm.compiler.share.Random;
  29 
  30 public class Explicit01 {
  31 
  32     private static final String GOLDEN_HASH = "-914118083";
  33 
  34     private static StringBuilder staticSB = new StringBuilder();
  35     private static Random random = new Random(11);
  36     private static final String PRE  = "pre\u307E\u3048\u306B";
  37     private static final String POST = "post\u3042\u3068\u3067";
  38 
  39     private static void crop(StringBuilder sb) {
  40         int max = 100;
  41         if (sb.length() > max) {
  42             sb.delete(0, 20);
  43             sb.delete(60, sb.length());
  44         }
  45     }
  46 
  47     private static String randomCut(String s, int size) {
  48         StringBuilder res = new StringBuilder();
  49         for (int i = 0; i < size; i++) {
  50             int r = random.nextInt(s.length());
  51             res.append(s.charAt(r));
  52         }
  53         return res.toString();
  54     }
  55 
  56     public static void main(String[] args) {
  57         StringGoldChecker goldChecker = new StringGoldChecker(GOLDEN_HASH);
  58         goldChecker.print(CompilerTestLauncher.launch(test));
  59         goldChecker.check();
  60     }
  61 
  62     public static String randomString() {
  63         int p = random.nextInt(100);
  64         StringBuilder prefix = new StringBuilder();
  65         prefix.append(p);
  66         prefix.append(PRE);
  67         prefix.append(random.nextInt(1000));
  68         StringBuilder postfix = new StringBuilder();
  69         postfix.append(POST);
  70         postfix.append(p);
  71         if (p > 50) {
  72             postfix.append(p);
  73         } else {
  74             postfix.append(p + 1);
  75         }
  76         postfix.append(random.nextInt(42));
  77         staticSB.append(prefix.toString());
  78         crop(staticSB);
  79         postfix.append(staticSB.toString());
  80         String s = prefix.toString() + postfix.toString();
  81 
  82         return randomCut(s, 10);
  83     }
  84 
  85     private static final CompilerTest<Integer> test = new CompilerTest<Integer>("Explicit01") {
  86         @Override
  87         public Integer execute(Random random) {
  88             StringBuilder res = new StringBuilder();
  89             int s = 200;
  90             while (s > 0) {
  91                 s -= random.nextInt(50);
  92                 int p = random.nextInt(1000);
  93                 if (p > s) {
  94                     res.append(s);
  95                 } else {
  96                     res.append(p);
  97                 }
  98                 crop(res);
  99                 res.append(s + "x\u306B");
 100                 res.append(randomString());
 101             }
 102 
 103             return res.toString().hashCode();
 104         }
 105     };
 106 
 107 
 108 }