Integrated new types, tests still work, not used yet
[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.*;
4
5 import java.util.Arrays;
6 import java.util.function.LongConsumer;
7
8 import org.junit.jupiter.api.Test;
9
10 import era.mi.logic.Simulation;
11 import era.mi.logic.components.Connector;
12 import era.mi.logic.components.Demux;
13 import era.mi.logic.components.Merger;
14 import era.mi.logic.components.Mux;
15 import era.mi.logic.components.Splitter;
16 import era.mi.logic.components.TriStateBuffer;
17 import era.mi.logic.components.gates.AndGate;
18 import era.mi.logic.components.gates.NotGate;
19 import era.mi.logic.components.gates.OrGate;
20 import era.mi.logic.components.gates.XorGate;
21 import era.mi.logic.types.Bit;
22 import era.mi.logic.wires.Wire;
23 import era.mi.logic.wires.Wire.WireEnd;
24
25 @SuppressWarnings("unused")
26 class ComponentTest
27 {
28
29         @Test
30         void circuitExampleTest()
31         {
32                 Simulation.TIMELINE.reset();
33                 Wire a = new Wire(1, 1), b = new Wire(1, 1), c = new Wire(1, 10), d = new Wire(2, 1), e = new Wire(1, 1), f = new Wire(1, 1),
34                                 g = new Wire(1, 1), h = new Wire(2, 1), i = new Wire(2, 1), j = new Wire(1, 1), k = new Wire(1, 1);
35                 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
36                 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
37                 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
38                 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
39                 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
40
41                 a.createEnd().feedSignals(Bit.ZERO);
42                 b.createEnd().feedSignals(Bit.ONE);
43                 c.createEnd().feedSignals(Bit.ZERO);
44                 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
45                 e.createEnd().feedSignals(Bit.ZERO);
46
47                 Simulation.TIMELINE.executeAll();
48
49                 assertEquals(Bit.ONE, j.getValue());
50                 assertEquals(Bit.ZERO, k.getValue());
51         }
52
53         @Test
54         void splitterTest()
55         {
56                 Simulation.TIMELINE.reset();
57                 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
58                 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
59                 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
60
61                 Simulation.TIMELINE.executeAll();
62
63                 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
64                 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
65                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
66         }
67
68         @Test
69         void mergerTest()
70         {
71                 Simulation.TIMELINE.reset();
72                 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
73                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
74                 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
75                 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
76
77                 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
78
79                 Simulation.TIMELINE.executeAll();
80
81                 assertTrue(
82                                 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
83         }
84
85         @Test
86         void triStateBufferTest()
87         {
88                 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
89                 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
90                 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
91                 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
92
93                 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
94                 enI.feedSignals(Bit.ONE);
95                 aI.feedSignals(Bit.ONE);
96                 bI.feedSignals(Bit.Z);
97
98                 Simulation.TIMELINE.executeAll();
99
100                 assertEquals(Bit.ONE, b.getValue());
101
102                 bI.feedSignals(Bit.ZERO);
103
104                 Simulation.TIMELINE.executeAll();
105
106                 assertEquals(Bit.X, b.getValue());
107                 assertEquals(Bit.ONE, a.getValue());
108
109                 aI.clearSignals();
110                 enI.feedSignals(Bit.ZERO);
111
112                 Simulation.TIMELINE.executeAll();
113
114                 assertEquals(Bit.ZERO, a.getValue());
115
116         }
117
118         @Test
119         void muxTest()
120         {
121                 Simulation.TIMELINE.reset();
122                 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), out = new Wire(4, 1);
123                 WireEnd selectIn = select.createEnd();
124
125                 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
126                 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
127                 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
128
129                 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
130                 Simulation.TIMELINE.executeAll();
131
132                 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
133                 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
134                 Simulation.TIMELINE.executeAll();
135
136                 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
137
138                 selectIn.feedSignals(Bit.ONE, Bit.ONE);
139                 Simulation.TIMELINE.executeAll();
140
141                 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
142
143         }
144
145         @Test
146         void demuxTest()
147         {
148                 Simulation.TIMELINE.reset();
149                 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), in = new Wire(4, 1);
150                 WireEnd selectIn = select.createEnd();
151
152                 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
153                 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
154
155                 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
156                 Simulation.TIMELINE.executeAll();
157
158                 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
159                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
160                 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
161                 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
162                 Simulation.TIMELINE.executeAll();
163
164                 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
165                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
166                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
167
168                 selectIn.feedSignals(Bit.ONE, Bit.ONE);
169                 Simulation.TIMELINE.executeAll();
170
171                 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
172                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
173                 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
174
175         }
176
177         @Test
178         void andTest()
179         {
180                 Simulation.TIMELINE.reset();
181                 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
182                 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
183                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
184                 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
185
186                 Simulation.TIMELINE.executeAll();
187
188                 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
189         }
190
191         @Test
192         void orTest()
193         {
194                 Simulation.TIMELINE.reset();
195                 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
196                 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
197                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
198                 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
199
200                 Simulation.TIMELINE.executeAll();
201
202                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
203         }
204
205         @Test
206         void xorTest()
207         {
208                 Simulation.TIMELINE.reset();
209                 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
210                 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
211                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
212                 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
213                 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
214
215                 Simulation.TIMELINE.executeAll();
216
217                 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
218         }
219
220         @Test
221         void notTest()
222         {
223                 Simulation.TIMELINE.reset();
224                 Wire a = new Wire(3, 1), b = new Wire(3, 2);
225                 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
226                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
227
228                 Simulation.TIMELINE.executeAll();
229
230                 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
231         }
232
233         @Test
234         void rsLatchCircuitTest()
235         {
236                 Simulation.TIMELINE.reset();
237                 Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1), q = new Wire(1, 1), nq = new Wire(1, 1);
238
239                 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
240                 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
241                 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
242                 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
243
244                 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
245
246                 sIn.feedSignals(Bit.ONE);
247                 rIn.feedSignals(Bit.ZERO);
248
249                 Simulation.TIMELINE.executeAll();
250
251                 assertEquals(Bit.ONE, q.getValue());
252                 assertEquals(Bit.ZERO, nq.getValue());
253
254                 sIn.feedSignals(Bit.ZERO);
255
256                 Simulation.TIMELINE.executeAll();
257                 assertEquals(Bit.ONE, q.getValue());
258                 assertEquals(Bit.ZERO, nq.getValue());
259
260                 rIn.feedSignals(Bit.ONE);
261
262                 Simulation.TIMELINE.executeAll();
263
264                 assertEquals(Bit.ZERO, q.getValue());
265                 assertEquals(Bit.ONE, nq.getValue());
266         }
267
268         @Test
269         void numericValueTest()
270         {
271                 Simulation.TIMELINE.reset();
272
273                 Wire a = new Wire(4, 1);
274                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
275
276                 Simulation.TIMELINE.executeAll();
277
278                 assertEquals(15, a.getUnsignedValue());
279                 assertEquals(-1, a.getSignedValue());
280         }
281
282         @Test
283         void multipleInputs()
284         {
285                 Simulation.TIMELINE.reset();
286                 Wire w = new Wire(2, 1);
287                 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
288                 wI1.feedSignals(Bit.ONE, Bit.Z);
289                 wI2.feedSignals(Bit.Z, Bit.X);
290                 Simulation.TIMELINE.executeAll();
291                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
292
293                 wI2.feedSignals(Bit.ZERO, Bit.Z);
294                 Simulation.TIMELINE.executeAll();
295                 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
296
297                 wI2.feedSignals(Bit.Z, Bit.Z);
298                 Simulation.TIMELINE.executeAll();
299                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
300
301                 wI2.feedSignals(Bit.ONE, Bit.Z);
302                 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
303                 Simulation.TIMELINE.executeAll();
304                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
305         }
306
307         @Test
308         void wireConnections()
309         {
310                 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
311
312                 Simulation.TIMELINE.reset();
313
314                 Wire a = new Wire(1, 2);
315                 Wire b = new Wire(1, 2);
316                 Wire c = new Wire(1, 2);
317                 WireEnd aI = a.createEnd();
318                 WireEnd bI = b.createEnd();
319                 WireEnd cI = c.createEnd();
320
321                 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
322                 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
323                 LongConsumer print = time -> System.out.format("Time %2d\n   a: %s\n   b: %s\n   c: %s\n", time, a, b, c);
324
325                 cI.feedSignals(Bit.ONE);
326                 test.assertAfterSimulationIs(print, Bit.ONE);
327
328                 cI.feedSignals(Bit.X);
329                 test.assertAfterSimulationIs(print, Bit.X);
330
331                 cI.feedSignals(Bit.X);
332                 cI.feedSignals(Bit.Z);
333                 test.assertAfterSimulationIs(print, Bit.Z);
334
335                 new Connector(b.createEnd(), c.createEnd()).connect();
336                 test.assertAfterSimulationIs(print, Bit.Z);
337                 System.err.println("ONE");
338                 bI.feedSignals(Bit.ONE);
339                 test.assertAfterSimulationIs(print, Bit.ONE);
340                 System.err.println("ZERO");
341                 bI.feedSignals(Bit.ZERO);
342                 test.assertAfterSimulationIs(print, Bit.ZERO);
343                 System.err.println("Z");
344                 bI.feedSignals(Bit.Z);
345                 test.assertAfterSimulationIs(print, Bit.Z);
346
347                 new Connector(a.createEnd(), b.createEnd()).connect();
348                 System.err.println("Z 2");
349                 aI.feedSignals(Bit.Z);
350                 test.assertAfterSimulationIs(print, Bit.Z);
351                 test2.assertAfterSimulationIs(Bit.Z);
352                 System.err.println("ONE 2");
353                 aI.feedSignals(Bit.ONE);
354                 test.assertAfterSimulationIs(print, Bit.ONE);
355                 test2.assertAfterSimulationIs(Bit.ONE);
356                 System.err.println("ZERO 2");
357                 aI.feedSignals(Bit.ZERO);
358                 test.assertAfterSimulationIs(print, Bit.ZERO);
359                 test2.assertAfterSimulationIs(Bit.ZERO);
360                 System.err.println("Z 2 II");
361                 aI.feedSignals(Bit.Z);
362                 test.assertAfterSimulationIs(print, Bit.Z);
363                 test2.assertAfterSimulationIs(Bit.Z);
364
365                 System.err.println("No Conflict yet");
366                 bI.feedSignals(Bit.ONE);
367                 test.assertAfterSimulationIs(print, Bit.ONE);
368                 test2.assertAfterSimulationIs(Bit.ONE);
369                 aI.feedSignals(Bit.ONE);
370                 test.assertAfterSimulationIs(print, Bit.ONE);
371                 test2.assertAfterSimulationIs(Bit.ONE);
372                 System.err.println("Conflict");
373                 aI.feedSignals(Bit.ZERO);
374                 test.assertAfterSimulationIs(print, Bit.X);
375                 test2.assertAfterSimulationIs(Bit.X);
376                 aI.feedSignals(Bit.ONE);
377                 test.assertAfterSimulationIs(print, Bit.ONE);
378                 test2.assertAfterSimulationIs(Bit.ONE);
379         }
380
381         private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
382         {
383                 assertArrayEquals(expected, actual);
384         }
385 }