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