Removed unused import
[Mograsim.git] / era.mi / src / era / mi / logic / tests / ComponentTest.java
index c49e004..4f6826b 100644 (file)
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
 \r
 import era.mi.logic.Bit;\r
 import era.mi.logic.Simulation;\r
+import era.mi.logic.components.Demux;\r
 import era.mi.logic.components.Merger;\r
 import era.mi.logic.components.Mux;\r
 import era.mi.logic.components.Splitter;\r
@@ -16,6 +17,7 @@ import era.mi.logic.components.TriStateBuffer;
 import era.mi.logic.components.gates.AndGate;\r
 import era.mi.logic.components.gates.NotGate;\r
 import era.mi.logic.components.gates.OrGate;\r
+import era.mi.logic.components.gates.XorGate;\r
 import era.mi.logic.wires.WireArray;\r
 import era.mi.logic.wires.WireArray.WireArrayInput;\r
 \r
@@ -28,7 +30,7 @@ class ComponentTest
                Simulation.TIMELINE.reset();\r
                WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),\r
                                f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);\r
-               new AndGate(1, a, b, f);\r
+               new AndGate(1, f, a, b);\r
                new NotGate(1, f, g);\r
                new Merger(h, c, g);\r
                new Mux(1, i, e, h, d);\r
@@ -139,29 +141,80 @@ class ComponentTest
 \r
     }\r
 \r
+    @Test\r
+    void demuxTest()\r
+    {\r
+       Simulation.TIMELINE.reset();\r
+       WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),\r
+               select = new WireArray(2, 5), in = new WireArray(4, 1);\r
+       WireArrayInput selectIn = select.createInput();\r
+\r
+       selectIn.feedSignals(Bit.ZERO, Bit.ZERO);\r
+       in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+\r
+       new Demux(1, in, select, a, b, c);\r
+       Simulation.TIMELINE.executeAll();\r
+\r
+       assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+       assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       selectIn.feedSignals(Bit.ZERO, Bit.ONE);\r
+       Simulation.TIMELINE.executeAll();\r
+\r
+       assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+\r
+       selectIn.feedSignals(Bit.ONE, Bit.ONE);\r
+       Simulation.TIMELINE.executeAll();\r
+\r
+       assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+       assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
+\r
+    }\r
+    \r
     @Test\r
     void andTest()\r
     {\r
        Simulation.TIMELINE.reset();\r
-       AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));\r
-       gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
-       gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+       WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
+       new AndGate(1, c, a, b);\r
+       a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+       b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
 \r
        Simulation.TIMELINE.executeAll();\r
-       assertBitArrayEquals(gate.getOut().getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+       \r
+       assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
     }\r
 \r
     @Test\r
     void orTest()\r
     {\r
        Simulation.TIMELINE.reset();\r
-       OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));\r
-       gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
-       gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+       WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
+       new OrGate(1, c, a, b);\r
+       a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
+       b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+\r
+       Simulation.TIMELINE.executeAll();\r
+\r
+       assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+    }\r
+    \r
+    @Test\r
+    void xorTest()\r
+    {\r
+       Simulation.TIMELINE.reset();\r
+       WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);\r
+       new XorGate(1, d, a, b, c);\r
+       a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);\r
+       b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
+       c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
 \r
        Simulation.TIMELINE.executeAll();\r
 \r
-       assertBitArrayEquals(gate.getOut().getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+       assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);\r
     }\r
 \r
     @Test\r
@@ -171,8 +224,8 @@ class ComponentTest
        WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),\r
                q = new WireArray(1, 1), nq = new WireArray(1, 1);\r
 \r
-       new OrGate(1, r, nq, t2);\r
-       new OrGate(1, s, q, t1);\r
+       new OrGate(1, t2, r, nq);\r
+       new OrGate(1, t1, s, q);\r
        new NotGate(1, t2, q);\r
        new NotGate(1, t1, nq);\r
 \r
@@ -234,7 +287,7 @@ class ComponentTest
        assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);\r
 \r
        wI2.feedSignals(Bit.ONE, Bit.Z);\r
-       w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));\r
+       w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));\r
        Simulation.TIMELINE.executeAll();\r
        assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);\r
     }\r
@@ -267,7 +320,7 @@ class ComponentTest
                cI.feedSignals(Bit.Z);\r
                test.assertAfterSimulationIs(print, Bit.Z);\r
 \r
-               Connector c1 = new Connector(b, c);\r
+               new Connector(b, c);\r
                test.assertAfterSimulationIs(print, Bit.Z);\r
                System.err.println("ONE");\r
                bI.feedSignals(Bit.ONE);\r
@@ -279,7 +332,7 @@ class ComponentTest
                bI.feedSignals(Bit.Z);\r
                test.assertAfterSimulationIs(print, Bit.Z);\r
                \r
-               Connector c2 = new Connector(a, b);\r
+               new Connector(a, b);\r
                System.err.println("Z 2");\r
                aI.feedSignals(Bit.Z);\r
                test.assertAfterSimulationIs(print, Bit.Z);\r