preserves the test case seed value during a debug session, closes #675

This commit is contained in:
hneemann 2021-03-07 12:12:23 +01:00
parent 4b6ac347bd
commit 08f091d232
4 changed files with 41 additions and 2 deletions

View File

@ -1194,6 +1194,9 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
if (tsl.isEmpty())
throw new TestingDataException(Lang.get("err_noTestData"));
for (Circuit.TestCase tc : tsl)
tc.getTestCaseDescription().setNewSeed();
windowPosManager.register("testResult", new ValueTableDialog(Main.this, Lang.get("msg_testResult"))
.addTestResult(tsl, circuitComponent.getCircuit(), library))
.setVisible(true);

View File

@ -7,6 +7,7 @@ package de.neemann.digital.testing;
import de.neemann.digital.lang.Lang;
import de.neemann.digital.testing.parser.*;
import de.neemann.digital.testing.parser.functions.Random;
import java.io.IOException;
import java.util.ArrayList;
@ -20,6 +21,8 @@ public class TestCaseDescription {
private transient ArrayList<String> names;
private transient ArrayList<VirtualSignal> virtualSignals;
private transient ModelInitializer modelInitializer;
private transient Random random;
private transient long seed;
/**
@ -48,6 +51,14 @@ public class TestCaseDescription {
*/
public TestCaseDescription(TestCaseDescription valueToCopy) {
this.dataString = valueToCopy.dataString;
this.seed = valueToCopy.seed;
}
/**
* Creates a new seed value
*/
public void setNewSeed() {
seed = 0;
}
/**
@ -65,6 +76,9 @@ public class TestCaseDescription {
throw new TestingDataException(Lang.get("err_errorParsingTestdata"), e);
}
}
if (seed == 0)
seed = System.currentTimeMillis();
random.setSeed(seed);
}
private void parseDataString() throws IOException, ParserException {
@ -73,6 +87,7 @@ public class TestCaseDescription {
names = tdp.getNames();
virtualSignals = tdp.getVirtualSignals();
modelInitializer = tdp.getModelInitializer();
random = tdp.getRandom();
}
/**

View File

@ -37,6 +37,7 @@ public class Parser {
private final ArrayList<VirtualSignal> virtualSignals;
private final Tokenizer tok;
private final HashMap<String, Function> functions = new HashMap<>();
private final Random random;
private LineEmitter emitter;
/**
@ -46,7 +47,8 @@ public class Parser {
*/
public Parser(String data) {
functions.put("signExt", new SignExtend());
functions.put("random", new Random());
random = new Random();
functions.put("random", random);
functions.put("ite", new IfThenElse());
names = new ArrayList<>();
virtualSignals = new ArrayList<>();
@ -302,6 +304,13 @@ public class Parser {
return modelInit;
}
/**
* @return the random function
*/
public Random getRandom() {
return random;
}
/**
* @return the line emitter
*/

View File

@ -17,15 +17,27 @@ import java.util.ArrayList;
*/
public class Random extends Function {
private final java.util.Random rnd;
/**
* Creates a new function
*/
public Random() {
super(1);
this.rnd = new java.util.Random();
}
/**
* Sets the random seed
*
* @param seed the seed
*/
public void setSeed(long seed) {
rnd.setSeed(seed);
}
@Override
public long calcValue(Context c, ArrayList<Expression> args) throws ParserException {
return (long) (Math.random() * args.get(0).value(c));
return (long) (rnd.nextDouble() * args.get(0).value(c));
}
}