darcy@1554: /* darcy@1554: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. darcy@1554: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. darcy@1554: * darcy@1554: * This code is free software; you can redistribute it and/or modify it darcy@1554: * under the terms of the GNU General Public License version 2 only, as darcy@1554: * published by the Free Software Foundation. darcy@1554: * darcy@1554: * This code is distributed in the hope that it will be useful, but WITHOUT darcy@1554: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or darcy@1554: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License darcy@1554: * version 2 for more details (a copy is included in the LICENSE file that darcy@1554: * accompanied this code). darcy@1554: * darcy@1554: * You should have received a copy of the GNU General Public License version darcy@1554: * 2 along with this work; if not, write to the Free Software Foundation, darcy@1554: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. darcy@1554: * darcy@1554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA darcy@1554: * or visit www.oracle.com if you need additional information or have any darcy@1554: * questions. darcy@1554: */ darcy@1554: darcy@1554: import java.util.ArrayList; darcy@1554: import java.util.HashSet; darcy@1554: import java.util.List; darcy@1554: import java.util.Random; darcy@1554: darcy@1554: /* System properties: darcy@1554: * NumberOfTests, TestMode, and TestCaseNum are mutually exclusive darcy@1554: * TestSeed will be used only with NumberOfTests or TestMode, otherwise it will be ignored darcy@1554: * -DNumberOfTests=[0 to 2^20+2^11+1] darcy@1554: * -DTestMode=[FULL|DEFAULT] darcy@1554: * -DTestSeed=[seedNumber] darcy@1554: * -DTestCaseNum=[0 to 2^20+2^11+1] darcy@1554: */ darcy@1554: public class TestCaseGenerator { darcy@1554: // Total number of tests to be run darcy@1554: int numberOfTests = -1; darcy@1554: //Single test case darcy@1554: int testCaseNum = -1; darcy@1554: //Seed used to generate test cases darcy@1554: int testSeed; darcy@1554: darcy@1554: int maxTestNum; darcy@1554: Random randNum; darcy@1554: darcy@1554: // used in getNextTestCase darcy@1554: int curTestNum; darcy@1554: int testCompletedCount; darcy@1554: HashSet uniqueTestSet; darcy@1554: darcy@1554: static final int DEFAULT_TEST_COUNT = 250; darcy@1554: darcy@1554: /* darcy@1554: * Get parameter values from command line to set numberOfTests, testCaseNum, darcy@1554: * and testSeed darcy@1554: */ darcy@1554: public TestCaseGenerator(int maxTestNum) { darcy@1554: this.maxTestNum = maxTestNum; darcy@1554: darcy@1554: // Set values for variables based on input from command line darcy@1554: darcy@1554: // TestMode system property darcy@1554: String testModeVal = System.getProperty("TestMode"); darcy@1554: if(testModeVal != null && !testModeVal.isEmpty()) { darcy@1554: switch (testModeVal.toUpperCase()) { darcy@1554: case "FULL": darcy@1554: numberOfTests = maxTestNum; darcy@1554: break; darcy@1554: case "DEFAULT": darcy@1554: numberOfTests = DEFAULT_TEST_COUNT; darcy@1554: break; darcy@1554: default: darcy@1554: System.out.println("Invalid property value " + testModeVal + darcy@1554: " for numberOfTests. Possible range: 0 to " + darcy@1554: maxTestNum + ". Ignoring property"); darcy@1554: numberOfTests = -1; darcy@1554: } darcy@1554: } darcy@1554: darcy@1554: // NumberOfTests system property darcy@1554: String numTestsStr = System.getProperty("NumberOfTests"); darcy@1554: if(numTestsStr != null && !numTestsStr.isEmpty()) { darcy@1554: int numTests = -1; darcy@1554: try { darcy@1554: numTests = Integer.parseInt(numTestsStr); darcy@1554: if (numTests < 0 || numTests > maxTestNum) { darcy@1554: throw new NumberFormatException(); darcy@1554: } darcy@1554: } catch(NumberFormatException nfe) { darcy@1554: System.out.println("Invalid NumberOfTests property value " + darcy@1554: numTestsStr + ". Possible range: 0 to " + maxTestNum + darcy@1554: "Reset to default: " + DEFAULT_TEST_COUNT); darcy@1554: numTests = DEFAULT_TEST_COUNT; darcy@1554: } darcy@1554: darcy@1554: if (numberOfTests != -1 && numTests != -1) { darcy@1554: System.out.println("TestMode and NumberOfTests cannot be set together. Ignoring TestMode."); darcy@1554: } darcy@1554: numberOfTests = numTests; darcy@1554: } darcy@1554: darcy@1554: // TestSeed system property darcy@1554: String seedVal = System.getProperty("TestSeed"); darcy@1554: if(seedVal != null && !seedVal.isEmpty()) { darcy@1554: try { darcy@1554: testSeed = Integer.parseInt(seedVal); darcy@1554: } catch(NumberFormatException nfe) { darcy@1554: Random srand = new Random(); darcy@1554: testSeed = srand.nextInt(); darcy@1554: } darcy@1554: } else { darcy@1554: Random srand = new Random(); darcy@1554: testSeed = srand.nextInt(); darcy@1554: } darcy@1554: darcy@1554: // TestCaseNum system property darcy@1554: String testNumStr = System.getProperty("TestCaseNum"); darcy@1554: if(testNumStr != null && !testNumStr.isEmpty()) { darcy@1554: try { darcy@1554: testCaseNum = Integer.parseInt(testNumStr); darcy@1554: if (testCaseNum < 0 || testCaseNum > maxTestNum) { darcy@1554: throw new NumberFormatException(); darcy@1554: } darcy@1554: } catch(NumberFormatException nfe) { darcy@1554: System.out.println("Invalid TestCaseNumber property value " + darcy@1554: testNumStr + ". Possible value in range: 0 to " + darcy@1554: maxTestNum + ". Defaulting to last test case."); darcy@1554: testCaseNum = maxTestNum; darcy@1554: } darcy@1554: darcy@1554: if ( numberOfTests != -1) { darcy@1554: System.out.println("TestMode or NumberOfTests cannot be set along with TestCaseNum. Ignoring TestCaseNumber."); darcy@1554: testCaseNum = -1; darcy@1554: } darcy@1554: } darcy@1554: darcy@1554: if (numberOfTests == -1 && testCaseNum == -1) { darcy@1554: numberOfTests = DEFAULT_TEST_COUNT; darcy@1554: System.out.println("Setting TestMode to default, will run " + numberOfTests + "tests."); darcy@1554: } darcy@1554: darcy@1554: /* darcy@1554: * By this point in code, we will have: darcy@1554: * - testSeed: as per TestSeed or a Random one darcy@1554: * - numberOfTests to run or -1 to denote not set darcy@1554: * - testCaseNum to run or -1 to denote not set darcy@1554: */ darcy@1554: darcy@1554: /* darcy@1554: * If numberOfTests = maxTestNum, all tests are to be run, darcy@1554: * so no randNum will be required darcy@1554: */ darcy@1554: if (numberOfTests != -1 && numberOfTests < maxTestNum) { darcy@1554: System.out.println("Seed = " + testSeed); darcy@1554: randNum = new Random(testSeed); darcy@1554: uniqueTestSet = new HashSet<>(); darcy@1554: } darcy@1554: darcy@1554: testCompletedCount = 0; darcy@1554: // to be used to keep sequential count when running all tests darcy@1554: curTestNum = 0; darcy@1554: } darcy@1554: darcy@1554: /* darcy@1554: * returns next test case number to run darcy@1554: * returns -1 when there are no more tests to run darcy@1554: */ darcy@1554: public int getNextTestCase() { darcy@1554: if (testCaseNum != -1) { darcy@1554: int nextTC = testCaseNum; darcy@1554: testCaseNum = -1; darcy@1554: return nextTC; darcy@1554: } darcy@1554: if (++testCompletedCount <= numberOfTests) { darcy@1554: if (numberOfTests == maxTestNum) { darcy@1554: //all the tests need to be run, so just return darcy@1554: //next test case sequentially darcy@1554: return curTestNum++; darcy@1554: } else { darcy@1554: int nextTC = -1; darcy@1554: // Ensuring unique test are run darcy@1554: while(!uniqueTestSet.add(nextTC = randNum.nextInt(maxTestNum))) { darcy@1554: } darcy@1554: return nextTC; darcy@1554: } darcy@1554: } darcy@1554: return -1; darcy@1554: } darcy@1554: }