added some classes that improve testing experience
authorChristian Femers <femers@in.tum.de>
Fri, 10 May 2019 17:22:46 +0000 (19:22 +0200)
committerChristian Femers <femers@in.tum.de>
Fri, 10 May 2019 17:22:46 +0000 (19:22 +0200)
era.mi/src/era/mi/logic/components/BitDisplay.java [new file with mode: 0644]
era.mi/src/era/mi/logic/tests/ComponentTest.java
era.mi/src/era/mi/logic/tests/Connector.java [new file with mode: 0644]
era.mi/src/era/mi/logic/tests/TestBitDisplay.java [new file with mode: 0644]

diff --git a/era.mi/src/era/mi/logic/components/BitDisplay.java b/era.mi/src/era/mi/logic/components/BitDisplay.java
new file mode 100644 (file)
index 0000000..940f69c
--- /dev/null
@@ -0,0 +1,35 @@
+package era.mi.logic.components;
+
+import java.util.Arrays;
+
+import era.mi.logic.Bit;
+import era.mi.logic.wires.WireArray;
+
+public class BitDisplay extends BasicComponent
+{
+       private final WireArray in;
+       private Bit[] displayedValue;
+
+       public BitDisplay(WireArray in)
+       {
+               super(1);
+               this.in = in;
+               in.addObserver(this);
+       }
+
+       @Override
+       protected void compute()
+       {
+               displayedValue = in.getValues();
+       }
+
+       public Bit[] getDisplayedValue()
+       {
+               return displayedValue;
+       }
+
+       public boolean isDisplaying(Bit... values)
+       {
+               return Arrays.equals(displayedValue, values);
+       }
+}
index 0638cf8..722c52a 100644 (file)
@@ -3,15 +3,13 @@ package era.mi.logic.tests;
 import static org.junit.jupiter.api.Assertions.*;
 
 import java.util.Arrays;
+import java.util.function.LongConsumer;
 
 import org.junit.jupiter.api.Test;
 
 import era.mi.logic.Bit;
 import era.mi.logic.Simulation;
-import era.mi.logic.components.Merger2;
-import era.mi.logic.components.Mux;
 import era.mi.logic.components.Mux2;
-import era.mi.logic.components.Splitter;
 import era.mi.logic.components.gates.AndGate;
 import era.mi.logic.components.gates.NotGate;
 import era.mi.logic.components.gates.OrGate;
@@ -245,4 +243,49 @@ class ComponentTest
                }
                assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
        }
+       
+       @Test
+       void wireConnections()
+       {
+               // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
+               
+               Simulation.TIMELINE.reset();
+
+               WireArray a = new WireArray(1, 2);
+               WireArray b = new WireArray(1, 2);
+               WireArray c = new WireArray(1, 2);
+               WireArrayInput aI = a.createInput();
+               WireArrayInput bI = b.createInput();
+               WireArrayInput cI = c.createInput();
+
+               TestBitDisplay test = new TestBitDisplay(c);
+               LongConsumer print = time -> System.out.format("Time %2d\n   %s\n   %s\n   %s\n", time, a, b, c);
+
+               cI.feedSignals(Bit.ONE);
+               test.assertAfterSimulationIs(print, Bit.ONE);
+
+               cI.feedSignals(Bit.X);
+               test.assertAfterSimulationIs(print, Bit.X);
+
+               cI.feedSignals(Bit.X);
+               cI.feedSignals(Bit.Z);
+               test.assertAfterSimulationIs(print, Bit.Z);
+
+               Connector c1 = new Connector(b, c);
+               test.assertAfterSimulationIs(print, Bit.Z);
+               System.out.println("ONE");
+               bI.feedSignals(Bit.ONE);
+               test.assertAfterSimulationIs(print, Bit.ONE);
+               System.out.println("ZERO");
+               bI.feedSignals(Bit.ZERO);
+               test.assertAfterSimulationIs(print, Bit.ZERO);
+               System.out.println("Z");
+               bI.feedSignals(Bit.Z);
+               test.assertAfterSimulationIs(Bit.Z);
+       }
+
+       private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
+       {
+               assertArrayEquals(expected, actual);
+       }
 }
diff --git a/era.mi/src/era/mi/logic/tests/Connector.java b/era.mi/src/era/mi/logic/tests/Connector.java
new file mode 100644 (file)
index 0000000..d59ee8c
--- /dev/null
@@ -0,0 +1,38 @@
+package era.mi.logic.tests;
+
+import era.mi.logic.Simulation;
+import era.mi.logic.wires.WireArray;
+import era.mi.logic.wires.WireArray.WireArrayInput;
+import era.mi.logic.wires.WireArrayObserver;
+
+public class Connector implements WireArrayObserver
+{
+       private final WireArray a;
+       private final WireArray b;
+       private final WireArrayInput aI;
+       private final WireArrayInput bI;
+
+       public Connector(WireArray a, WireArray b)
+       {
+               if (a.length != b.length)
+                       throw new IllegalArgumentException("WireArray width does not match: " + a.length + ", " + b.length);
+               this.a = a;
+               this.b = b;
+               a.addObserver(this);
+               b.addObserver(this);
+               aI = a.createInput();
+               bI = b.createInput();
+       }
+
+       @Override
+       public void update(WireArray initiator)
+       {
+               Simulation.TIMELINE.addEvent((e) ->
+               {
+                       if (initiator == a)
+                               bI.feedSignals(a.getValues());
+                       else
+                               aI.feedSignals(b.getValues());
+               }, 1);
+       }
+}
diff --git a/era.mi/src/era/mi/logic/tests/TestBitDisplay.java b/era.mi/src/era/mi/logic/tests/TestBitDisplay.java
new file mode 100644 (file)
index 0000000..9913116
--- /dev/null
@@ -0,0 +1,48 @@
+package era.mi.logic.tests;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import java.util.Arrays;
+import java.util.function.LongConsumer;
+
+import era.mi.logic.Bit;
+import era.mi.logic.Simulation;
+import era.mi.logic.components.BitDisplay;
+import era.mi.logic.wires.WireArray;
+
+public final class TestBitDisplay extends BitDisplay
+{
+
+       public TestBitDisplay(WireArray in)
+       {
+               super(in);
+       }
+
+       public void assertDisplays(Bit... expected)
+       {
+               assertArrayEquals(expected, getDisplayedValue());
+       }
+
+       public void assertAfterSimulationIs(Bit... expected)
+       {
+               Simulation.TIMELINE.executeAll();
+               assertDisplays(expected);
+       }
+
+       public void assertAfterSimulationIs(LongConsumer r, Bit... expected)
+       {
+               while (Simulation.TIMELINE.hasNext())
+                       {
+                               Simulation.TIMELINE.executeNext();
+                               r.accept(Simulation.TIMELINE.getSimulationTime());
+                       }
+               assertDisplays(expected);
+       }
+
+       @Override
+       protected void compute()
+       {
+               super.compute();
+               System.out.println("update: value is " + Arrays.toString(getDisplayedValue()));
+       }
+}