adds a test case

This commit is contained in:
hneemann 2021-05-01 09:02:31 +02:00
parent e94def3b84
commit a346437071
6 changed files with 293 additions and 4 deletions

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2021 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital;
/**
* Helper to create a list of strings.
* Takes care of adding a separator at the right places in between the items.
*/
public class StringList {
private final StringBuilder builder;
private boolean first;
private String sep;
/**
* Create a new instance
*/
public StringList() {
this(new StringBuilder());
}
/**
* Creates a new instance
*
* @param builder the StringBuilder to use
*/
public StringList(StringBuilder builder) {
this.builder = builder;
this.first = true;
this.sep = " ";
}
/**
* Sets the separator.
* The default value is a blank.
*
* @param sep the separator
* @return this for chained calls
*/
public StringList separator(String sep) {
this.sep = sep;
return this;
}
/**
* Adds a item to the list.
* Adds a separator if needed.
*
* @param item the item to add.
* @return this for chained calls
*/
public StringList add(String item) {
if (first)
first = false;
else
builder.append(sep);
builder.append(item);
return this;
}
@Override
public String toString() {
return builder.toString();
}
}

View File

@ -5,6 +5,7 @@
*/
package de.neemann.digital.data;
import de.neemann.digital.StringList;
import de.neemann.digital.core.IntFormat;
import de.neemann.digital.core.Observable;
import de.neemann.digital.core.ValueFormatter;
@ -282,20 +283,23 @@ public class ValueTable extends Observable implements Iterable<TestRow> {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
StringList sl = new StringList(sb);
for (String n : names)
sb.append(n).append(" ");
sl.add(n);
sb.append("\n");
if (tableRowIndex == null)
for (TestRow row : values) {
sl = new StringList(sb);
for (Value v : row.getValues())
sb.append(v.toString()).append(" ");
sl.add(v.toString());
sb.append("\n");
}
else
for (int i : tableRowIndex) {
sl = new StringList(sb);
for (Value v : values.get(i).getValues())
sb.append(v.toString()).append(" ");
sl.add(v.toString());
sb.append("\n");
}

View File

@ -49,7 +49,7 @@ public class ValueTableObserver implements ModelStateObserverTyped {
if (event == ModelEvent.STARTED)
logData.clear();
if (event.getType() == type) {
if (event.getType() == type || event.getType() == ModelEventType.CHECKBURN) {
Value[] row = new Value[logData.getColumns()];
for (int i = 0; i < logData.getColumns(); i++)
row[i] = new Value(signals.get(i).getValue());

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2021 Helmut Neemann
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.gui.components.data;
import de.neemann.digital.core.Model;
import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.Signal;
import de.neemann.digital.data.ValueTable;
import de.neemann.digital.integration.ToBreakRunner;
import junit.framework.TestCase;
import java.util.ArrayList;
public class ValueTableObserverTest extends TestCase {
private static final class Case {
private final String name;
private final boolean microStep;
private final int clocks;
private final int expectedSize;
private final String expectedData;
public Case(String name, boolean microStep, int clocks, int expectedSize, String expectedData) {
this.name = name;
this.microStep = microStep;
this.clocks = clocks;
this.expectedSize = expectedSize;
this.expectedData = expectedData;
}
}
private final Case[] cases = new Case[]{
new Case("dig/graph/simple.dig", false, 4, 4,
"C Y X Z\n" +
"1 1 0 1\n" +
"0 0 1 0\n" +
"1 1 0 1\n" +
"0 0 1 0\n"),
new Case("dig/graph/simple.dig", true, 4, 12,
"C Y X Z\n" +
"1 1 1 0\n" +
"1 1 0 0\n" +
"1 1 0 1\n" +
"0 0 0 1\n" +
"0 0 1 1\n" +
"0 0 1 0\n" +
"1 1 1 0\n" +
"1 1 0 0\n" +
"1 1 0 1\n" +
"0 0 0 1\n" +
"0 0 1 1\n" +
"0 0 1 0\n"),
new Case("dig/graph/simple2.dig", false, 4, 4,
"C Y\n" +
"1 1\n" +
"0 0\n" +
"1 1\n" +
"0 0\n"),
new Case("dig/graph/simple2.dig", true, 4, 4,
"C Y\n" +
"1 1\n" +
"0 0\n" +
"1 1\n" +
"0 0\n"),
};
public void testTable() throws Exception {
for (Case c : cases) {
Model m = new ToBreakRunner(c.name).getModel();
ArrayList<Signal> list = m.getSignals();
ValueTableObserver o = new ValueTableObserver(c.microStep, list, 100);
m.addObserver(o);
ObservableValue clock = m.getClocks().get(0).getClockOutput();
for (int i = 0; i < c.clocks; i++)
m.modify(() -> clock.setBool(!clock.getBool()));
ValueTable logData = o.getLogData();
assertEquals(c.expectedSize, logData.getRows());
assertEquals(c.expectedData, logData.toString());
}
}
}

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<circuit>
<version>1</version>
<attributes/>
<visualElements>
<visualElement>
<elementName>Clock</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>C</string>
</entry>
</elementAttributes>
<pos x="220" y="320"/>
</visualElement>
<visualElement>
<elementName>Out</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>Y</string>
</entry>
</elementAttributes>
<pos x="420" y="320"/>
</visualElement>
<visualElement>
<elementName>Not</elementName>
<elementAttributes/>
<pos x="280" y="360"/>
</visualElement>
<visualElement>
<elementName>Not</elementName>
<elementAttributes/>
<pos x="360" y="400"/>
</visualElement>
<visualElement>
<elementName>Out</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>X</string>
</entry>
</elementAttributes>
<pos x="420" y="360"/>
</visualElement>
<visualElement>
<elementName>Out</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>Z</string>
</entry>
</elementAttributes>
<pos x="420" y="400"/>
</visualElement>
</visualElements>
<wires>
<wire>
<p1 x="220" y="320"/>
<p2 x="260" y="320"/>
</wire>
<wire>
<p1 x="260" y="320"/>
<p2 x="420" y="320"/>
</wire>
<wire>
<p1 x="340" y="400"/>
<p2 x="360" y="400"/>
</wire>
<wire>
<p1 x="400" y="400"/>
<p2 x="420" y="400"/>
</wire>
<wire>
<p1 x="260" y="360"/>
<p2 x="280" y="360"/>
</wire>
<wire>
<p1 x="320" y="360"/>
<p2 x="340" y="360"/>
</wire>
<wire>
<p1 x="340" y="360"/>
<p2 x="420" y="360"/>
</wire>
<wire>
<p1 x="260" y="320"/>
<p2 x="260" y="360"/>
</wire>
<wire>
<p1 x="340" y="360"/>
<p2 x="340" y="400"/>
</wire>
</wires>
<measurementOrdering/>
</circuit>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<circuit>
<version>1</version>
<attributes/>
<visualElements>
<visualElement>
<elementName>Clock</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>C</string>
</entry>
</elementAttributes>
<pos x="220" y="320"/>
</visualElement>
<visualElement>
<elementName>Out</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>Y</string>
</entry>
</elementAttributes>
<pos x="260" y="320"/>
</visualElement>
</visualElements>
<wires>
<wire>
<p1 x="220" y="320"/>
<p2 x="260" y="320"/>
</wire>
</wires>
<measurementOrdering/>
</circuit>