1 /*
   2  * Copyright (c) 2000, 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 /* @test
  25  * @bug 4162796 4162796
  26  * @summary Test indexOf and lastIndexOf
  27  * @key randomness
  28  */
  29 
  30 import java.util.Random;
  31 
  32 public class IndexOf {
  33 
  34     static Random generator = new Random();
  35     private static boolean failure = false;
  36 
  37     public static void main(String[] args) throws Exception {
  38         simpleTest();
  39         compareIndexOfLastIndexOf();
  40         compareStringStringBuffer();
  41 
  42         if (failure)
  43            throw new RuntimeException("One or more BitSet failures.");
  44     }
  45 
  46     private static void report(String testName, int failCount) {
  47         System.err.println(testName+": " +
  48                          (failCount==0 ? "Passed":"Failed("+failCount+")"));
  49         if (failCount > 0)
  50             failure = true;
  51     }
  52 
  53     private static String generateTestString(int min, int max) {
  54         StringBuffer aNewString = new StringBuffer(120);
  55         int aNewLength = getRandomIndex(min, max);
  56         for(int y=0; y<aNewLength; y++) {
  57             int achar = generator.nextInt(30)+30;
  58             char test = (char)(achar);
  59             aNewString.append(test);
  60         }
  61         return aNewString.toString();
  62     }
  63 
  64     private static int getRandomIndex(int constraint1, int constraint2) {
  65         int range = constraint2 - constraint1;
  66         int x = generator.nextInt(range);
  67         return constraint1 + x;
  68     }
  69 
  70     private static void simpleTest() {
  71         int failCount = 0;
  72         String sourceString;
  73         StringBuffer sourceBuffer;
  74         String targetString;
  75 
  76         for (int i=0; i<10000; i++) {
  77             do {
  78                 sourceString = generateTestString(99, 100);
  79                 sourceBuffer = new StringBuffer(sourceString);
  80                 targetString = generateTestString(10, 11);
  81             } while (sourceString.indexOf(targetString) != -1);
  82 
  83             int index1 = generator.nextInt(90) + 5;
  84             sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
  85 
  86             if (sourceBuffer.indexOf(targetString) != index1)
  87                 failCount++;
  88             if (sourceBuffer.indexOf(targetString, 5) != index1)
  89                 failCount++;
  90             if (sourceBuffer.indexOf(targetString, 99) == index1)
  91                 failCount++;
  92         }
  93 
  94         report("Basic Test                   ", failCount);
  95     }
  96 
  97     // Note: it is possible although highly improbable that failCount will
  98     // be > 0 even if everthing is working ok
  99     private static void compareIndexOfLastIndexOf() {
 100         int failCount = 0;
 101         String sourceString;
 102         StringBuffer sourceBuffer;
 103         String targetString;
 104 
 105         for (int i=0; i<10000; i++) {
 106             do {
 107                 sourceString = generateTestString(99, 100);
 108                 sourceBuffer = new StringBuffer(sourceString);
 109                 targetString = generateTestString(10, 11);
 110             } while (sourceString.indexOf(targetString) != -1);
 111 
 112             int index1 = generator.nextInt(100);
 113             sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
 114 
 115             // extremely remote possibility of > 1 match
 116             int matches = 0;
 117             int index2 = -1;
 118             while((index2 = sourceBuffer.indexOf(targetString,index2+1)) != -1)
 119                 matches++;
 120             if (matches > 1)
 121                 continue;
 122 
 123             if (sourceBuffer.indexOf(targetString) !=
 124                 sourceBuffer.lastIndexOf(targetString))
 125                 failCount++;
 126             sourceString = sourceBuffer.toString();
 127             if (sourceString.indexOf(targetString) !=
 128                 sourceString.lastIndexOf(targetString))
 129                 failCount++;
 130         }
 131 
 132         report("IndexOf vs LastIndexOf       ", failCount);
 133     }
 134 
 135     private static void compareStringStringBuffer() {
 136         int failCount = 0;
 137 
 138         for (int x=0; x<10000; x++) {
 139             String testString = generateTestString(1, 100);
 140             int len = testString.length();
 141 
 142             StringBuffer testBuffer = new StringBuffer(len);
 143             testBuffer.append(testString);
 144             if (!testString.equals(testBuffer.toString()))
 145                 throw new RuntimeException("Initial equality failure");
 146 
 147             int x1 = 0;
 148             int x2 = 1000;
 149             while(x2 > testString.length()) {
 150                 x1 = generator.nextInt(len);
 151                 x2 = generator.nextInt(100);
 152                 x2 = x1 + x2;
 153             }
 154             String fragment = testString.substring(x1,x2);
 155 
 156             int sAnswer = testString.indexOf(fragment);
 157             int sbAnswer = testBuffer.indexOf(fragment);
 158 
 159             if (sAnswer != sbAnswer)
 160                 failCount++;
 161 
 162             int testIndex = getRandomIndex(-100, 100);
 163 
 164             sAnswer = testString.indexOf(fragment, testIndex);
 165             sbAnswer = testBuffer.indexOf(fragment, testIndex);
 166 
 167             if (sAnswer != sbAnswer)
 168                 failCount++;
 169 
 170             sAnswer = testString.lastIndexOf(fragment);
 171             sbAnswer = testBuffer.lastIndexOf(fragment);
 172 
 173             if (sAnswer != sbAnswer)
 174                 failCount++;
 175 
 176             testIndex = getRandomIndex(-100, 100);
 177 
 178             sAnswer = testString.lastIndexOf(fragment, testIndex);
 179             sbAnswer = testBuffer.lastIndexOf(fragment, testIndex);
 180 
 181             if (sAnswer != sbAnswer)
 182                 failCount++;
 183         }
 184 
 185         report("String vs StringBuffer       ", failCount);
 186     }
 187 
 188 }
--- EOF ---