2929
3030import java .io .BufferedReader ;
3131import java .io .File ;
32+ import java .io .FileNotFoundException ;
3233import java .io .IOException ;
3334import java .io .PrintStream ;
3435import java .nio .file .Files ;
5152 * Use jcstress test suite to generate jtreg tests in 'test.src' or current
5253 * directory. Used version is defined in JcstressRunner class.
5354 *
54- * Each generated jtreg test file will contain several tests. Subdirectories are
55- * used to allow running all tests from a file using command line. 'copy',
56- * 'acqrel', 'fences', 'atomicity', 'seqcst.sync', 'seqcst.volatiles' and
57- * 'other' tests will be generated.
58- *
5955 * This generator depends on testlibrary, therefore it should be compiled and
6056 * added to classpath. One can replace @notest by @test in jtreg test
6157 * description above to run this class with jtreg.
@@ -97,58 +93,18 @@ public class TestGenerator {
9793 " */\n \n " , years );
9894 }
9995
100- private static enum JcstressGroup {
101- MEMEFFECTS ("memeffects" ),
102- COPY ("copy" ),
103- ACQREL ("acqrel" ),
104- FENCES ("fences" ),
105- ATOMICITY ("atomicity" ),
106- SEQCST_SYNC ("seqcst.sync" ),
107- SEQCST_VOLATILES ("seqcst.volatiles" ),
108- OTHER ("other" , JcstressGroup .otherFilter ());
109-
110- private final String groupName ;
111- private final Predicate <String > filter ;
112-
113- private JcstressGroup (String groupName , Predicate <String > filter ) {
114- this .groupName = groupName ;
115- this .filter = filter ;
116- }
117-
118- private JcstressGroup (String groupName ) {
119- this (groupName , JcstressGroup .nameFilter (groupName ));
120- }
121-
122- private static Predicate <String > nameFilter (String group ) {
123- return s -> s .startsWith ("org.openjdk.jcstress.tests." + group + "." );
124- }
125-
126- private static Predicate <String > otherFilter () {
127- return (s ) -> {
128- for (JcstressGroup g : EnumSet .complementOf (EnumSet .of (OTHER ))) {
129- if (g .filter .test (s )) {
130- return false ;
131- }
132- }
133- return true ;
134- };
135- }
136- }
137-
13896 public static String DESC_FORMAT = "\n "
13997 + "/**\n "
14098 + " * @test %1$s\n "
14199 + " * @library /test/lib /\n "
142- + " * @run driver/timeout=2400 " + JcstressRunner .class .getName ()
100+ + " * @run driver/timeout=21600 " + JcstressRunner .class .getName ()
143101 // verbose output
144102 + " -v"
145- // test mode preset
146- + " -m default"
147103 // test name
148- + " -t %1$s\n "
104+ + " -t org.openjdk.jcstress.tests. %1$s\\ . \n "
149105 + " */\n " ;
150106
151- public static void main (String [] args ) {
107+ public static void main (String [] args ) throws IOException {
152108 Path path = JcstressRunner .pathToArtifact ();
153109 Path output ;
154110 try {
@@ -162,56 +118,32 @@ public static void main(String[] args) {
162118 } catch (Exception e ) {
163119 throw new Error ("Can not get list of tests" , e );
164120 }
165- for (JcstressGroup group : JcstressGroup .values ()) {
166- try {
167- try (BufferedReader reader = Files .newBufferedReader (output )) {
168- // skip first 4 lines: name, -{80}, revision and empty line
169- for (int i = 0 ; i < 4 ; ++i ) {
170- reader .readLine ();
171- }
172- new TestGenerator (group ).generate (reader );
173- }
174- } catch (IOException e ) {
175- throw new Error ("Generating tests for " + group .name ()
176- + " has failed" , e );
177- }
178- }
179- output .toFile ().delete ();
180- }
181121
182- private final JcstressGroup group ;
122+ BufferedReader reader = Files .newBufferedReader (output );
123+
124+ reader .lines ()
125+ .skip (4 ) // skip first 4 lines: name, -{80}, revision and empty line
126+ .map (s -> s .split ("\\ ." )[4 ]) // group by the package name following "org.openjdk.jcstress.tests."
127+ .distinct ()
128+ .filter (s -> !s .startsWith ("sample" )) // skip sample test
129+ .forEach (TestGenerator ::generate );
183130
184- private TestGenerator (JcstressGroup group ) {
185- this .group = group ;
131+ output .toFile ().delete ();
186132 }
187133
188- private void generate (BufferedReader reader ) throws IOException {
189- // array is needed to change value inside a lambda
190- long [] count = {0L };
191- String root = Utils .TEST_SRC ;
192- Path testFile = Paths .get (root )
193- .resolve (group .groupName )
194- .resolve ("Test.java" );
195- File testDir = testFile .getParent ().toFile ();
196- if (!testDir .mkdirs () && !testDir .exists ()) {
197- throw new Error ("Can not create directories for "
198- + testFile .toString ());
199- }
134+ private static void generate (String group ) {
135+ Path testFile = Paths .get (Utils .TEST_SRC ).resolve (group + ".java" );
200136
137+ System .out .println ("Generating " + testFile );
201138 try (PrintStream ps = new PrintStream (testFile .toFile ())) {
202139 ps .print (COPYRIGHT );
203140 ps .printf ("/* DO NOT MODIFY THIS FILE. GENERATED BY %s */\n " ,
204- getClass () .getName ());
141+ TestGenerator . class .getName ());
205142
206- reader .lines ()
207- .filter (group .filter )
208- .forEach (s -> {
209- count [0 ]++;
210- ps .printf (DESC_FORMAT , s );
211- });
143+ ps .printf (DESC_FORMAT , group );
212144 ps .print ('\n' );
145+ } catch (FileNotFoundException e ) {
146+ System .out .println ("Failed to generate tests for " + group );
213147 }
214- System .out .printf ("%d tests generated in %s%n" ,
215- count [0 ], group .groupName );
216148 }
217149}
0 commit comments