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