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 Merge01 {
  31 
  32     private static final String GOLDEN_HASH = "1618288402";
  33 
  34     private static Random random = new Random(11);
  35     private static final String PRE  = "pre\u307E\u3048\u306B";
  36     private static final String POST = "post\u3042\u3068\u3067";
  37     private static final String TMP = "tmp\u305D\u3046\u304B";
  38     private static final String SUF = "suf\u3051\u3069\u3082";
  39 
  40     private static void crop(StringBuilder sb) {
  41         int max = 100;
  42         if (sb.length() > max) {
  43             sb.delete(0, 20);
  44             sb.delete(60, sb.length());
  45         }
  46     }
  47 
  48     private static String randomCut(String s, int size) {
  49         StringBuilder res = new StringBuilder();
  50         for(int i=0; i<size; i++) {
  51             int r = random.nextInt(s.length());
  52             res.append(s.charAt(r));
  53         }
  54         return res.toString();
  55     }
  56 
  57     public static void main(String[] args) {
  58         StringGoldChecker goldChecker = new StringGoldChecker(GOLDEN_HASH);
  59         goldChecker.print(CompilerTestLauncher.launch(test));
  60         goldChecker.check();
  61     }
  62 
  63     public static String randomString() {
  64         int p = random.nextInt(100);
  65         StringBuilder prefix = new StringBuilder();
  66         prefix.append(p);
  67         prefix.append(PRE);
  68         prefix.append(random.nextInt(1000));
  69 
  70         StringBuilder suffix = new StringBuilder();
  71         StringBuilder t = new StringBuilder();
  72         t.append(p);
  73         t.append(TMP);
  74         suffix.append(p + SUF);
  75         //merge:
  76         suffix.append(t.toString());
  77         suffix.append(t);
  78 
  79         StringBuilder postfix = new StringBuilder();
  80         postfix.append(POST);
  81         postfix.append(p);
  82         if(p>50) {
  83             postfix.append(p);
  84         } else {
  85             postfix.append(p+1);
  86         }
  87         postfix.append(random.nextInt(42));
  88 
  89         //merge:
  90         StringBuilder res = new StringBuilder();
  91         res.append(prefix.toString());
  92         res.append(suffix.toString());
  93         res.append(postfix.toString());
  94 
  95 
  96         return randomCut(res.toString(), 10);
  97     }
  98 
  99 
 100     private static final CompilerTest<Integer> test = new CompilerTest<Integer>("Merge01") {
 101         @Override
 102         public Integer execute(Random random) {
 103             StringBuilder res = new StringBuilder();
 104             int s = 200;
 105             while (s>0) {
 106                 s -= random.nextInt(50);
 107                 int p = random.nextInt(1000);
 108                 if(p>s) {
 109                     StringBuilder t = new StringBuilder();
 110                     t.append("");
 111                     //merge:
 112                     res.append(t);
 113                 } else {
 114                     res.append(p);
 115                 }
 116                 crop(res);
 117                 StringBuilder t = new StringBuilder();
 118                 t.append(s);
 119                 t.append(t);
 120                 res.append(randomString());
 121                 //merge:
 122                 res.append(t.toString());
 123             }
 124 
 125             return res.toString().hashCode();
 126         }
 127     };
 128 
 129 }