mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-19 09:54:49 -04:00
added load/store test case
This commit is contained in:
parent
10bde858e5
commit
46e643b36c
@ -102,8 +102,22 @@ public class Circuit {
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static Circuit loadCircuit(File filename, ShapeFactory shapeFactory) throws IOException {
|
||||
XStream xStream = getxStream();
|
||||
try (InputStream in = new FileInputStream(filename)) {
|
||||
final Circuit circuit = loadCircuit(new FileInputStream(filename), shapeFactory);
|
||||
circuit.origin = filename;
|
||||
return circuit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new circuit instance from a stored file
|
||||
*
|
||||
* @param in the input stream
|
||||
* @param shapeFactory shapeFactory used to create the shapes
|
||||
* @return the circuit
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static Circuit loadCircuit(InputStream in, ShapeFactory shapeFactory) throws IOException {
|
||||
try {
|
||||
XStream xStream = getxStream();
|
||||
Circuit circuit = (Circuit) xStream.fromXML(in);
|
||||
for (VisualElement ve : circuit.getElements())
|
||||
ve.setShapeFactory(shapeFactory);
|
||||
@ -118,8 +132,11 @@ public class Circuit {
|
||||
e.setPos(e.getPos().mul(2));
|
||||
circuit.version = 1;
|
||||
}
|
||||
circuit.origin = filename;
|
||||
return circuit;
|
||||
} catch (RuntimeException e) {
|
||||
throw new IOException(Lang.get("err_invalidFileFormat"), e);
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,12 +147,22 @@ public class Circuit {
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public void save(File filename) throws IOException {
|
||||
XStream xStream = Circuit.getxStream();
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) {
|
||||
out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
||||
xStream.marshal(this, new PrettyPrintWriter(out));
|
||||
save(new FileOutputStream(filename));
|
||||
origin = filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the circuit in the given file
|
||||
*
|
||||
* @param out the writer
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public void save(OutputStream out) throws IOException {
|
||||
try (Writer w = new OutputStreamWriter(out, "utf-8")) {
|
||||
XStream xStream = Circuit.getxStream();
|
||||
w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
||||
xStream.marshal(this, new PrettyPrintWriter(w));
|
||||
modified = false;
|
||||
origin = filename;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,53 @@
|
||||
package de.neemann.digital.draw.elements;
|
||||
|
||||
|
||||
import de.neemann.digital.core.element.Keys;
|
||||
import de.neemann.digital.core.io.In;
|
||||
import de.neemann.digital.core.io.InValue;
|
||||
import de.neemann.digital.core.memory.DataField;
|
||||
import de.neemann.digital.core.memory.ROM;
|
||||
import de.neemann.digital.draw.library.ElementLibrary;
|
||||
import de.neemann.digital.draw.shapes.ShapeFactory;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class CircuitTest extends TestCase {
|
||||
|
||||
private static Circuit createCircuit() {
|
||||
Circuit c = new Circuit();
|
||||
|
||||
// add a ROM
|
||||
final DataField data = new DataField(4);
|
||||
data.setData(0, 0xffff0000ffff0000L);
|
||||
data.setData(1, 0x8fff0000ffff0000L);
|
||||
final VisualElement rom = new VisualElement(ROM.DESCRIPTION.getName())
|
||||
.setAttribute(Keys.DATA, data);
|
||||
c.add(rom);
|
||||
|
||||
// add an input
|
||||
final VisualElement in = new VisualElement(In.DESCRIPTION.getName())
|
||||
.setAttribute(Keys.INPUT_DEFAULT, new InValue(0x8fff0000ffff0000L));
|
||||
c.add(in);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
public void test64Bit() throws IOException {
|
||||
Circuit c = createCircuit();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
c.save(baos);
|
||||
|
||||
// check ROM
|
||||
c = Circuit.loadCircuit(new ByteArrayInputStream(baos.toByteArray()), new ShapeFactory(new ElementLibrary()));
|
||||
VisualElement rom = c.getElements().get(0);
|
||||
DataField d = rom.getElementAttributes().get(Keys.DATA);
|
||||
assertEquals(0xffff0000ffff0000L, d.getDataWord(0));
|
||||
assertEquals(0x8fff0000ffff0000L, d.getDataWord(1));
|
||||
|
||||
// check input
|
||||
VisualElement in = c.getElements().get(1);
|
||||
assertEquals(0x8fff0000ffff0000L, in.getElementAttributes().get(Keys.INPUT_DEFAULT).getValue());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user