Cleaned up ComponentTest.
[Mograsim.git] / era.mi / src / era / mi / logic / tests / ComponentTest.java
1 package era.mi.logic.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6 import static org.junit.jupiter.api.Assertions.fail;
7
8 import java.util.Arrays;
9
10 import org.junit.jupiter.api.Test;
11
12 import era.mi.logic.Bit;
13 import era.mi.logic.Simulation;
14 import era.mi.logic.components.Merger;
15 import era.mi.logic.components.Mux;
16 import era.mi.logic.components.Splitter;
17 import era.mi.logic.components.TriStateBuffer;
18 import era.mi.logic.components.gates.AndGate;
19 import era.mi.logic.components.gates.NotGate;
20 import era.mi.logic.components.gates.OrGate;
21 import era.mi.logic.wires.WireArray;
22 import era.mi.logic.wires.WireArray.WireArrayInput;
23
24 class ComponentTest
25 {
26     
27         @Test
28         void circuitExampleTest()
29         {
30                 Simulation.TIMELINE.reset();
31                 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),
32                                 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);
33                 new AndGate(1, a, b, f);
34                 new NotGate(1, f, g);
35                 new Merger(h, c, g);
36                 new Mux(1, i, e, h, d);
37                 new Splitter(i, k, j);
38                 
39                 a.createInput().feedSignals(Bit.ZERO);
40                 b.createInput().feedSignals(Bit.ONE);
41                 c.createInput().feedSignals(Bit.ZERO);
42                 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
43                 e.createInput().feedSignals(Bit.ZERO);
44                 
45                 Simulation.TIMELINE.executeAll();
46                 
47                 assertEquals(Bit.ONE, j.getValue());
48                 assertEquals(Bit.ZERO, k.getValue());
49         }
50
51     @Test
52     void splitterTest()
53     {
54         Simulation.TIMELINE.reset();
55         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
56         in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
57         new Splitter(in, a, b, c);
58
59         Simulation.TIMELINE.executeAll();
60
61         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues());
62         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues());
63         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues());
64     }
65
66     @Test
67     void mergerTest()
68     {
69         Simulation.TIMELINE.reset();
70         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
71         a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
72         b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
73         c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
74
75         new Merger(out, a, b, c);
76
77         Simulation.TIMELINE.executeAll();
78
79         assertTrue(Arrays.equals(out.getValues(),
80                 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
81     }
82
83     @Test
84     void triStateBufferTest()
85     {
86         WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
87                 notEn = new WireArray(1, 1);
88         new NotGate(1, en, notEn);
89         new TriStateBuffer(1, a, b, en);
90         new TriStateBuffer(1, b, a, notEn);
91
92         WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
93         enI.feedSignals(Bit.ONE);
94         aI.feedSignals(Bit.ONE);
95
96         Simulation.TIMELINE.executeAll();
97
98         assertEquals(Bit.ONE, b.getValue());
99
100         bI.feedSignals(Bit.ZERO);
101
102         Simulation.TIMELINE.executeAll();
103
104         assertEquals(Bit.X, b.getValue());
105         assertEquals(Bit.ONE, a.getValue());
106
107         aI.clearSignals();
108         enI.feedSignals(Bit.ZERO);
109
110         Simulation.TIMELINE.executeAll();
111
112         assertEquals(Bit.ZERO, a.getValue());
113
114     }
115
116     @Test
117     void muxTest()
118     {
119         Simulation.TIMELINE.reset();
120         WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
121                 select = new WireArray(2, 5), out = new WireArray(4, 1);
122         WireArrayInput selectIn = select.createInput();
123
124         selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125         a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126         c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
127
128         new Mux(1, out, select, a, b, c);
129         Simulation.TIMELINE.executeAll();
130
131         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues());
132         selectIn.feedSignals(Bit.ZERO, Bit.ONE);
133         Simulation.TIMELINE.executeAll();
134
135         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues());
136
137         selectIn.feedSignals(Bit.ONE, Bit.ONE);
138         Simulation.TIMELINE.executeAll();
139
140         assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues());
141
142     }
143
144     @Test
145     void andTest()
146     {
147         Simulation.TIMELINE.reset();
148         AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
149         gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
150         gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
151
152         Simulation.TIMELINE.executeAll();
153         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues());
154     }
155
156     @Test
157     void orTest()
158     {
159         Simulation.TIMELINE.reset();
160         OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
161         gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
162         gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
163
164         Simulation.TIMELINE.executeAll();
165
166         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues());
167     }
168
169     @Test
170     void rsLatchCircuitTest()
171     {
172         Simulation.TIMELINE.reset();
173         WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
174                 q = new WireArray(1, 1), nq = new WireArray(1, 1);
175
176         new OrGate(1, r, nq, t2);
177         new OrGate(1, s, q, t1);
178         new NotGate(1, t2, q);
179         new NotGate(1, t1, nq);
180
181         WireArrayInput sIn = s.createInput(), rIn = r.createInput();
182
183         sIn.feedSignals(Bit.ONE);
184         rIn.feedSignals(Bit.ZERO);
185
186         Simulation.TIMELINE.executeAll();
187
188         assertEquals(Bit.ONE, q.getValue());
189         assertEquals(Bit.ZERO, nq.getValue());
190
191         sIn.feedSignals(Bit.ZERO);
192
193         Simulation.TIMELINE.executeAll();
194         assertEquals(Bit.ONE, q.getValue());
195         assertEquals(Bit.ZERO, nq.getValue());
196
197         rIn.feedSignals(Bit.ONE);
198
199         Simulation.TIMELINE.executeAll();
200
201         assertEquals(Bit.ZERO, q.getValue());
202         assertEquals(Bit.ONE, nq.getValue());
203     }
204
205     @Test
206     void numericValueTest()
207     {
208         Simulation.TIMELINE.reset();
209
210         WireArray a = new WireArray(4, 1);
211         a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
212
213         Simulation.TIMELINE.executeAll();
214
215         assertEquals(15, a.getUnsignedValue());
216         assertEquals(-1, a.getSignedValue());
217     }
218
219     @Test
220     void multipleInputs()
221     {
222         Simulation.TIMELINE.reset();
223         WireArray w = new WireArray(2, 1);
224         WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
225         wI1.feedSignals(Bit.ONE, Bit.Z);
226         wI2.feedSignals(Bit.Z, Bit.X);
227         Simulation.TIMELINE.executeAll();
228         assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues());
229
230         wI2.feedSignals(Bit.ZERO, Bit.Z);
231         Simulation.TIMELINE.executeAll();
232         assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues());
233
234         wI2.feedSignals(Bit.Z, Bit.Z);
235         Simulation.TIMELINE.executeAll();
236         assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
237
238         wI2.feedSignals(Bit.ONE, Bit.Z);
239         w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
240         Simulation.TIMELINE.executeAll();
241         assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
242     }
243
244 //      @Test
245 //      void wireConnections()
246 //      {
247 //              // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
248 //              
249 //              Simulation.TIMELINE.reset();
250 //
251 //              WireArray a = new WireArray(1, 2);
252 //              WireArray b = new WireArray(1, 2);
253 //              WireArray c = new WireArray(1, 2);
254 //              WireArrayInput aI = a.createInput();
255 //              WireArrayInput bI = b.createInput();
256 //              WireArrayInput cI = c.createInput();
257 //
258 //              TestBitDisplay test = new TestBitDisplay(c);
259 //              LongConsumer print = time -> System.out.format("Time %2d\n   %s\n   %s\n   %s\n", time, a, b, c);
260 //
261 //              cI.feedSignals(Bit.ONE);
262 //              test.assertAfterSimulationIs(print, Bit.ONE);
263 //
264 //              cI.feedSignals(Bit.X);
265 //              test.assertAfterSimulationIs(print, Bit.X);
266 //
267 //              cI.feedSignals(Bit.X);
268 //              cI.feedSignals(Bit.Z);
269 //              test.assertAfterSimulationIs(print, Bit.Z);
270 //
271 //              Connector c1 = new Connector(b, c);
272 //              test.assertAfterSimulationIs(print, Bit.Z);
273 //              System.out.println("ONE");
274 //              bI.feedSignals(Bit.ONE);
275 //              test.assertAfterSimulationIs(print, Bit.ONE);
276 //              System.out.println("ZERO");
277 //              bI.feedSignals(Bit.ZERO);
278 //              test.assertAfterSimulationIs(print, Bit.ZERO);
279 //              System.out.println("Z");
280 //              bI.feedSignals(Bit.Z);
281 //              test.assertAfterSimulationIs(Bit.Z);
282 //      }
283
284     private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
285     {
286         assertArrayEquals(expected, actual);
287     }
288 }