1 /* |
|
2 * Copyright (c) 2013, 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 import java.util.ArrayList; |
|
25 import java.util.HashSet; |
|
26 import java.util.List; |
|
27 import java.util.Random; |
|
28 |
|
29 /* System properties: |
|
30 * NumberOfTests, TestMode, and TestCaseNum are mutually exclusive |
|
31 * TestSeed will be used only with NumberOfTests or TestMode, otherwise it will be ignored |
|
32 * -DNumberOfTests=[0 to 2^20+2^11+1] |
|
33 * -DTestMode=[FULL|DEFAULT] |
|
34 * -DTestSeed=[seedNumber] |
|
35 * -DTestCaseNum=[0 to 2^20+2^11+1] |
|
36 */ |
|
37 public class TestCaseGenerator { |
|
38 // Total number of tests to be run |
|
39 int numberOfTests = -1; |
|
40 //Single test case |
|
41 int testCaseNum = -1; |
|
42 //Seed used to generate test cases |
|
43 int testSeed; |
|
44 |
|
45 int maxTestNum; |
|
46 Random randNum; |
|
47 |
|
48 // used in getNextTestCase |
|
49 int curTestNum; |
|
50 int testCompletedCount; |
|
51 HashSet<Integer> uniqueTestSet; |
|
52 |
|
53 static final int DEFAULT_TEST_COUNT = 250; |
|
54 |
|
55 /* |
|
56 * Get parameter values from command line to set numberOfTests, testCaseNum, |
|
57 * and testSeed |
|
58 */ |
|
59 public TestCaseGenerator(int maxTestNum) { |
|
60 this.maxTestNum = maxTestNum; |
|
61 |
|
62 // Set values for variables based on input from command line |
|
63 |
|
64 // TestMode system property |
|
65 String testModeVal = System.getProperty("TestMode"); |
|
66 if(testModeVal != null && !testModeVal.isEmpty()) { |
|
67 switch (testModeVal.toUpperCase()) { |
|
68 case "FULL": |
|
69 numberOfTests = maxTestNum; |
|
70 break; |
|
71 case "DEFAULT": |
|
72 numberOfTests = DEFAULT_TEST_COUNT; |
|
73 break; |
|
74 default: |
|
75 System.out.println("Invalid property value " + testModeVal + |
|
76 " for numberOfTests. Possible range: 0 to " + |
|
77 maxTestNum + ". Ignoring property"); |
|
78 numberOfTests = -1; |
|
79 } |
|
80 } |
|
81 |
|
82 // NumberOfTests system property |
|
83 String numTestsStr = System.getProperty("NumberOfTests"); |
|
84 if(numTestsStr != null && !numTestsStr.isEmpty()) { |
|
85 int numTests = -1; |
|
86 try { |
|
87 numTests = Integer.parseInt(numTestsStr); |
|
88 if (numTests < 0 || numTests > maxTestNum) { |
|
89 throw new NumberFormatException(); |
|
90 } |
|
91 } catch(NumberFormatException nfe) { |
|
92 System.out.println("Invalid NumberOfTests property value " + |
|
93 numTestsStr + ". Possible range: 0 to " + maxTestNum + |
|
94 "Reset to default: " + DEFAULT_TEST_COUNT); |
|
95 numTests = DEFAULT_TEST_COUNT; |
|
96 } |
|
97 |
|
98 if (numberOfTests != -1 && numTests != -1) { |
|
99 System.out.println("TestMode and NumberOfTests cannot be set together. Ignoring TestMode."); |
|
100 } |
|
101 numberOfTests = numTests; |
|
102 } |
|
103 |
|
104 // TestSeed system property |
|
105 String seedVal = System.getProperty("TestSeed"); |
|
106 if(seedVal != null && !seedVal.isEmpty()) { |
|
107 try { |
|
108 testSeed = Integer.parseInt(seedVal); |
|
109 } catch(NumberFormatException nfe) { |
|
110 Random srand = new Random(); |
|
111 testSeed = srand.nextInt(); |
|
112 } |
|
113 } else { |
|
114 Random srand = new Random(); |
|
115 testSeed = srand.nextInt(); |
|
116 } |
|
117 |
|
118 // TestCaseNum system property |
|
119 String testNumStr = System.getProperty("TestCaseNum"); |
|
120 if(testNumStr != null && !testNumStr.isEmpty()) { |
|
121 try { |
|
122 testCaseNum = Integer.parseInt(testNumStr); |
|
123 if (testCaseNum < 0 || testCaseNum > maxTestNum) { |
|
124 throw new NumberFormatException(); |
|
125 } |
|
126 } catch(NumberFormatException nfe) { |
|
127 System.out.println("Invalid TestCaseNumber property value " + |
|
128 testNumStr + ". Possible value in range: 0 to " + |
|
129 maxTestNum + ". Defaulting to last test case."); |
|
130 testCaseNum = maxTestNum; |
|
131 } |
|
132 |
|
133 if ( numberOfTests != -1) { |
|
134 System.out.println("TestMode or NumberOfTests cannot be set along with TestCaseNum. Ignoring TestCaseNumber."); |
|
135 testCaseNum = -1; |
|
136 } |
|
137 } |
|
138 |
|
139 if (numberOfTests == -1 && testCaseNum == -1) { |
|
140 numberOfTests = DEFAULT_TEST_COUNT; |
|
141 System.out.println("Setting TestMode to default, will run " + numberOfTests + "tests."); |
|
142 } |
|
143 |
|
144 /* |
|
145 * By this point in code, we will have: |
|
146 * - testSeed: as per TestSeed or a Random one |
|
147 * - numberOfTests to run or -1 to denote not set |
|
148 * - testCaseNum to run or -1 to denote not set |
|
149 */ |
|
150 |
|
151 /* |
|
152 * If numberOfTests = maxTestNum, all tests are to be run, |
|
153 * so no randNum will be required |
|
154 */ |
|
155 if (numberOfTests != -1 && numberOfTests < maxTestNum) { |
|
156 System.out.println("Seed = " + testSeed); |
|
157 randNum = new Random(testSeed); |
|
158 uniqueTestSet = new HashSet<>(); |
|
159 } |
|
160 |
|
161 testCompletedCount = 0; |
|
162 // to be used to keep sequential count when running all tests |
|
163 curTestNum = 0; |
|
164 } |
|
165 |
|
166 /* |
|
167 * returns next test case number to run |
|
168 * returns -1 when there are no more tests to run |
|
169 */ |
|
170 public int getNextTestCase() { |
|
171 if (testCaseNum != -1) { |
|
172 int nextTC = testCaseNum; |
|
173 testCaseNum = -1; |
|
174 return nextTC; |
|
175 } |
|
176 if (++testCompletedCount <= numberOfTests) { |
|
177 if (numberOfTests == maxTestNum) { |
|
178 //all the tests need to be run, so just return |
|
179 //next test case sequentially |
|
180 return curTestNum++; |
|
181 } else { |
|
182 int nextTC = -1; |
|
183 // Ensuring unique test are run |
|
184 while(!uniqueTestSet.add(nextTC = randNum.nextInt(maxTestNum))) { |
|
185 } |
|
186 return nextTC; |
|
187 } |
|
188 } |
|
189 return -1; |
|
190 } |
|
191 } |
|