test/tools/javac/annotations/repeatingAnnotations/combo/TestCaseGenerator.java

Thu, 07 Feb 2013 20:47:06 -0800

author
darcy
date
Thu, 07 Feb 2013 20:47:06 -0800
changeset 1554
5125b9854d07
permissions
-rw-r--r--

7195131: Update 2 compiler combo tests for repeating annotations to include package and default use cases
Reviewed-by: darcy
Contributed-by: sonali.goel@oracle.com

darcy@1554 1 /*
darcy@1554 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
darcy@1554 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@1554 4 *
darcy@1554 5 * This code is free software; you can redistribute it and/or modify it
darcy@1554 6 * under the terms of the GNU General Public License version 2 only, as
darcy@1554 7 * published by the Free Software Foundation.
darcy@1554 8 *
darcy@1554 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@1554 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@1554 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@1554 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@1554 13 * accompanied this code).
darcy@1554 14 *
darcy@1554 15 * You should have received a copy of the GNU General Public License version
darcy@1554 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@1554 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@1554 18 *
darcy@1554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@1554 20 * or visit www.oracle.com if you need additional information or have any
darcy@1554 21 * questions.
darcy@1554 22 */
darcy@1554 23
darcy@1554 24 import java.util.ArrayList;
darcy@1554 25 import java.util.HashSet;
darcy@1554 26 import java.util.List;
darcy@1554 27 import java.util.Random;
darcy@1554 28
darcy@1554 29 /* System properties:
darcy@1554 30 * NumberOfTests, TestMode, and TestCaseNum are mutually exclusive
darcy@1554 31 * TestSeed will be used only with NumberOfTests or TestMode, otherwise it will be ignored
darcy@1554 32 * -DNumberOfTests=[0 to 2^20+2^11+1]
darcy@1554 33 * -DTestMode=[FULL|DEFAULT]
darcy@1554 34 * -DTestSeed=[seedNumber]
darcy@1554 35 * -DTestCaseNum=[0 to 2^20+2^11+1]
darcy@1554 36 */
darcy@1554 37 public class TestCaseGenerator {
darcy@1554 38 // Total number of tests to be run
darcy@1554 39 int numberOfTests = -1;
darcy@1554 40 //Single test case
darcy@1554 41 int testCaseNum = -1;
darcy@1554 42 //Seed used to generate test cases
darcy@1554 43 int testSeed;
darcy@1554 44
darcy@1554 45 int maxTestNum;
darcy@1554 46 Random randNum;
darcy@1554 47
darcy@1554 48 // used in getNextTestCase
darcy@1554 49 int curTestNum;
darcy@1554 50 int testCompletedCount;
darcy@1554 51 HashSet<Integer> uniqueTestSet;
darcy@1554 52
darcy@1554 53 static final int DEFAULT_TEST_COUNT = 250;
darcy@1554 54
darcy@1554 55 /*
darcy@1554 56 * Get parameter values from command line to set numberOfTests, testCaseNum,
darcy@1554 57 * and testSeed
darcy@1554 58 */
darcy@1554 59 public TestCaseGenerator(int maxTestNum) {
darcy@1554 60 this.maxTestNum = maxTestNum;
darcy@1554 61
darcy@1554 62 // Set values for variables based on input from command line
darcy@1554 63
darcy@1554 64 // TestMode system property
darcy@1554 65 String testModeVal = System.getProperty("TestMode");
darcy@1554 66 if(testModeVal != null && !testModeVal.isEmpty()) {
darcy@1554 67 switch (testModeVal.toUpperCase()) {
darcy@1554 68 case "FULL":
darcy@1554 69 numberOfTests = maxTestNum;
darcy@1554 70 break;
darcy@1554 71 case "DEFAULT":
darcy@1554 72 numberOfTests = DEFAULT_TEST_COUNT;
darcy@1554 73 break;
darcy@1554 74 default:
darcy@1554 75 System.out.println("Invalid property value " + testModeVal +
darcy@1554 76 " for numberOfTests. Possible range: 0 to " +
darcy@1554 77 maxTestNum + ". Ignoring property");
darcy@1554 78 numberOfTests = -1;
darcy@1554 79 }
darcy@1554 80 }
darcy@1554 81
darcy@1554 82 // NumberOfTests system property
darcy@1554 83 String numTestsStr = System.getProperty("NumberOfTests");
darcy@1554 84 if(numTestsStr != null && !numTestsStr.isEmpty()) {
darcy@1554 85 int numTests = -1;
darcy@1554 86 try {
darcy@1554 87 numTests = Integer.parseInt(numTestsStr);
darcy@1554 88 if (numTests < 0 || numTests > maxTestNum) {
darcy@1554 89 throw new NumberFormatException();
darcy@1554 90 }
darcy@1554 91 } catch(NumberFormatException nfe) {
darcy@1554 92 System.out.println("Invalid NumberOfTests property value " +
darcy@1554 93 numTestsStr + ". Possible range: 0 to " + maxTestNum +
darcy@1554 94 "Reset to default: " + DEFAULT_TEST_COUNT);
darcy@1554 95 numTests = DEFAULT_TEST_COUNT;
darcy@1554 96 }
darcy@1554 97
darcy@1554 98 if (numberOfTests != -1 && numTests != -1) {
darcy@1554 99 System.out.println("TestMode and NumberOfTests cannot be set together. Ignoring TestMode.");
darcy@1554 100 }
darcy@1554 101 numberOfTests = numTests;
darcy@1554 102 }
darcy@1554 103
darcy@1554 104 // TestSeed system property
darcy@1554 105 String seedVal = System.getProperty("TestSeed");
darcy@1554 106 if(seedVal != null && !seedVal.isEmpty()) {
darcy@1554 107 try {
darcy@1554 108 testSeed = Integer.parseInt(seedVal);
darcy@1554 109 } catch(NumberFormatException nfe) {
darcy@1554 110 Random srand = new Random();
darcy@1554 111 testSeed = srand.nextInt();
darcy@1554 112 }
darcy@1554 113 } else {
darcy@1554 114 Random srand = new Random();
darcy@1554 115 testSeed = srand.nextInt();
darcy@1554 116 }
darcy@1554 117
darcy@1554 118 // TestCaseNum system property
darcy@1554 119 String testNumStr = System.getProperty("TestCaseNum");
darcy@1554 120 if(testNumStr != null && !testNumStr.isEmpty()) {
darcy@1554 121 try {
darcy@1554 122 testCaseNum = Integer.parseInt(testNumStr);
darcy@1554 123 if (testCaseNum < 0 || testCaseNum > maxTestNum) {
darcy@1554 124 throw new NumberFormatException();
darcy@1554 125 }
darcy@1554 126 } catch(NumberFormatException nfe) {
darcy@1554 127 System.out.println("Invalid TestCaseNumber property value " +
darcy@1554 128 testNumStr + ". Possible value in range: 0 to " +
darcy@1554 129 maxTestNum + ". Defaulting to last test case.");
darcy@1554 130 testCaseNum = maxTestNum;
darcy@1554 131 }
darcy@1554 132
darcy@1554 133 if ( numberOfTests != -1) {
darcy@1554 134 System.out.println("TestMode or NumberOfTests cannot be set along with TestCaseNum. Ignoring TestCaseNumber.");
darcy@1554 135 testCaseNum = -1;
darcy@1554 136 }
darcy@1554 137 }
darcy@1554 138
darcy@1554 139 if (numberOfTests == -1 && testCaseNum == -1) {
darcy@1554 140 numberOfTests = DEFAULT_TEST_COUNT;
darcy@1554 141 System.out.println("Setting TestMode to default, will run " + numberOfTests + "tests.");
darcy@1554 142 }
darcy@1554 143
darcy@1554 144 /*
darcy@1554 145 * By this point in code, we will have:
darcy@1554 146 * - testSeed: as per TestSeed or a Random one
darcy@1554 147 * - numberOfTests to run or -1 to denote not set
darcy@1554 148 * - testCaseNum to run or -1 to denote not set
darcy@1554 149 */
darcy@1554 150
darcy@1554 151 /*
darcy@1554 152 * If numberOfTests = maxTestNum, all tests are to be run,
darcy@1554 153 * so no randNum will be required
darcy@1554 154 */
darcy@1554 155 if (numberOfTests != -1 && numberOfTests < maxTestNum) {
darcy@1554 156 System.out.println("Seed = " + testSeed);
darcy@1554 157 randNum = new Random(testSeed);
darcy@1554 158 uniqueTestSet = new HashSet<>();
darcy@1554 159 }
darcy@1554 160
darcy@1554 161 testCompletedCount = 0;
darcy@1554 162 // to be used to keep sequential count when running all tests
darcy@1554 163 curTestNum = 0;
darcy@1554 164 }
darcy@1554 165
darcy@1554 166 /*
darcy@1554 167 * returns next test case number to run
darcy@1554 168 * returns -1 when there are no more tests to run
darcy@1554 169 */
darcy@1554 170 public int getNextTestCase() {
darcy@1554 171 if (testCaseNum != -1) {
darcy@1554 172 int nextTC = testCaseNum;
darcy@1554 173 testCaseNum = -1;
darcy@1554 174 return nextTC;
darcy@1554 175 }
darcy@1554 176 if (++testCompletedCount <= numberOfTests) {
darcy@1554 177 if (numberOfTests == maxTestNum) {
darcy@1554 178 //all the tests need to be run, so just return
darcy@1554 179 //next test case sequentially
darcy@1554 180 return curTestNum++;
darcy@1554 181 } else {
darcy@1554 182 int nextTC = -1;
darcy@1554 183 // Ensuring unique test are run
darcy@1554 184 while(!uniqueTestSet.add(nextTC = randNum.nextInt(maxTestNum))) {
darcy@1554 185 }
darcy@1554 186 return nextTC;
darcy@1554 187 }
darcy@1554 188 }
darcy@1554 189 return -1;
darcy@1554 190 }
darcy@1554 191 }

mercurial