Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdline_options_tester/src/TestIterator.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
import java.util.*;
24
25
public class TestIterator {
26
private TestSuite _suite;
27
28
private String _loopIndex;
29
private String _loopFrom;
30
private String _loopUntil;
31
private String _loopInc;
32
33
private boolean _singleIterationOnly;
34
private boolean _loopIndexIncremented;
35
36
private boolean _isInnerLoopOpen;
37
private ArrayList _subItems;
38
39
/**
40
* This constructor creates a standard TestIterator object, which runs a given set of tests/variable assignments/etc.
41
* a number of times. The number of times that the stuff is run depends on the <code>from</code>, <code>until</code>,
42
* and <code>inc</code> parameters.
43
* The <code>index</code> variable is started at <code>from</code>, and incremented by <code>inc</code> after each
44
* iteration. Before each iteration is started, if <code>index</code> is equal to <code>until</code>, the run is
45
* terminated.
46
* The effect is the equivalent of <code>for (index = from; index != until; index += inc) { dostuff }</code>, where
47
* <code>dostuff</code> is determined by what is added to this TestIterator using the addXXX methods.
48
* The <code>from</code>, <code>until</code>, and <code>inc</code> strings may have variables in them, but after
49
* variable substitution, must evaluate to integers.
50
*/
51
TestIterator( TestSuite suite, String index, String from, String until, String inc ) {
52
_suite = suite;
53
54
_loopIndex = index;
55
_loopFrom = from;
56
_loopUntil = until;
57
_loopInc = inc;
58
59
_isInnerLoopOpen = false;
60
_subItems = new ArrayList();
61
62
TestSuite.putVariable( _loopIndex, TestSuite.evaluateVariables( _loopFrom ) );
63
}
64
65
/**
66
* This constructor creates a single-iteration version of this iterator. It will run everything passed
67
* to it (by the addXXX methods) exactly once.
68
*/
69
TestIterator( TestSuite suite ) {
70
_suite = suite;
71
72
_singleIterationOnly = true;
73
74
_isInnerLoopOpen = false;
75
_subItems = new ArrayList();
76
}
77
78
void addTest( Test t ) {
79
if (_isInnerLoopOpen) {
80
getLastSubIterator().addTest( t );
81
} else {
82
_subItems.add( t );
83
if (canRunTest()) {
84
_suite.runTest( t );
85
}
86
}
87
}
88
89
void addSubIterator( TestIterator it ) {
90
if (_isInnerLoopOpen) {
91
getLastSubIterator().addSubIterator( it );
92
} else {
93
_subItems.add( it );
94
_isInnerLoopOpen = true;
95
}
96
}
97
98
void addCommand( Command c ) {
99
if (_isInnerLoopOpen) {
100
getLastSubIterator().addCommand( c );
101
} else {
102
_subItems.add( c );
103
c.executeSelf();
104
}
105
}
106
107
boolean closeInnerLoop() {
108
if (_isInnerLoopOpen) {
109
if (getLastSubIterator().closeInnerLoop()) {
110
return true;
111
}
112
_isInnerLoopOpen = false;
113
return true;
114
}
115
// end of this TestIterator's corresponding <loop> element, so now we actually run the remaining
116
// iterations of the loop
117
incrementLoopIndex();
118
while (canRunTest()) {
119
runIteration();
120
}
121
return false;
122
}
123
124
private void incrementLoopIndex() {
125
if (_singleIterationOnly) {
126
_loopIndexIncremented = true;
127
return;
128
}
129
130
try {
131
int increment = (_loopInc == null ? 1 : Integer.parseInt( TestSuite.evaluateVariables( _loopInc ).trim() ));
132
String value = TestSuite.getVariable( _loopIndex );
133
value = Integer.toString( Integer.parseInt( value.trim() ) + increment );
134
TestSuite.putVariable( _loopIndex, value );
135
} catch (Exception e) {
136
notifyInfiniteLoop(e);
137
System.exit(1);
138
}
139
}
140
141
private void runEntireLoop() {
142
TestSuite.putVariable( _loopIndex, TestSuite.evaluateVariables( _loopFrom ) );
143
while (canRunTest()) {
144
runIteration();
145
}
146
}
147
148
private void runIteration() {
149
for (int i = 0; i < _subItems.size(); i++) {
150
Object o = _subItems.get(i);
151
if (o instanceof Test && canRunTest()) {
152
_suite.runTest( (Test)o );
153
} else if (o instanceof TestIterator) {
154
( (TestIterator)o ).runEntireLoop();
155
} else if (o instanceof Command) {
156
( (Command)o ).executeSelf();
157
}
158
}
159
incrementLoopIndex();
160
}
161
162
private boolean canRunTest() {
163
if (_singleIterationOnly) {
164
return !_loopIndexIncremented;
165
}
166
try {
167
return ! (Integer.parseInt(TestSuite.getVariable(_loopIndex)) == Integer.parseInt(TestSuite.evaluateVariables(_loopUntil)));
168
} catch (Exception e) {
169
notifyInfiniteLoop(e);
170
System.exit(1);
171
}
172
return false; // dead code, to satisfy compiler only
173
}
174
175
private TestIterator getLastSubIterator() {
176
return (TestIterator)_subItems.get( _subItems.size() - 1 );
177
}
178
179
private void notifyInfiniteLoop( Exception e ) {
180
// if there is an exception here, it's an accidental infinite loop. tell user to fix!
181
System.err.println( "The test file passed to the cmdLineTester application contained an invalid loop" );
182
System.err.println( "of " + _loopIndex + " from " + _loopFrom + " until " + _loopUntil + ", with increment " + _loopInc + ". This loop" );
183
System.err.println( "contains an error that will get it stuck in an infinite cycle. Please fix it.. or" );
184
System.err.println( "modify the cmdLineTester application to deal with it, if it really is what you" );
185
System.err.println( "want to do. The cmdLineTester application will now terminate." );
186
e.printStackTrace();
187
}
188
}
189
190