Notify watchers only on output changes and pass previous values to callbacks

This commit is contained in:
Stone_Red
2026-03-26 01:19:25 +01:00
parent aa6d13a46c
commit c40195d0a3
3 changed files with 68 additions and 15 deletions
@@ -46,7 +46,6 @@ public abstract class SimulatorTestsBase
Assert.AreEqual(expected, sim.GetOutput(sink), $"AND2 failed for {i1} & {i2}");
}
sim.Reset();
Check(false, false, false);
Check(false, true, false);
Check(true, false, false);
@@ -73,7 +72,6 @@ public abstract class SimulatorTestsBase
Assert.AreEqual(expected, sim.GetOutput(sink), $"OR2 failed for {i1} | {i2}");
}
sim.Reset();
Check(false, false, false);
Check(false, true, true);
Check(true, false, true);
@@ -90,7 +88,6 @@ public abstract class SimulatorTestsBase
sim.ConnectGates(s1, not, 0);
sim.ConnectGates(not, sink, 0);
sim.Reset();
sim.SetSource(s1, false);
_ = sim.RunUntilStable();
Assert.IsTrue(sim.GetOutput(sink), "NOT(0) should be 1");
@@ -118,7 +115,6 @@ public abstract class SimulatorTestsBase
sim.ConnectGates(s3, lut, 2);
sim.ConnectGates(lut, sink, 0);
sim.Reset();
_ = sim.RunUntilStable();
sim.SetSource(s1, true); sim.SetSource(s2, true); sim.SetSource(s3, false);
@@ -149,9 +145,50 @@ public abstract class SimulatorTestsBase
sim.ConnectGates(src, inst.Inputs[0], 0);
sim.ConnectGates(inst.Outputs[0], sink, 0);
sim.Reset();
sim.SetSource(src, true);
_ = sim.RunUntilStable();
Assert.IsFalse(sim.GetOutput(sink), "Macro NOT(1) should be 0");
}
[TestMethod]
public void TestWatcherNotification()
{
ICircuitSimulator sim = CreateSimulator();
int source = sim.AddGate(GateKind.Source);
int not = sim.AddGate(GateKind.Not);
sim.ConnectGates(source, not, 0);
int watcher1Count = 0;
int watcher2Count = 0;
IDisposable sub1 = sim.WatchGate(not, (id, val) => watcher1Count++);
IDisposable sub2 = sim.WatchGate(not, (id, val) => watcher2Count++);
// Initial state
sim.SetSource(source, false);
_ = sim.RunUntilStable();
Assert.AreEqual(1, watcher1Count, "Watcher 1 should fire on initial change");
Assert.AreEqual(1, watcher2Count, "Watcher 2 should fire on initial change");
// No change
sim.SetSource(source, false);
_ = sim.RunUntilStable();
Assert.AreEqual(1, watcher1Count, "Watcher 1 should not fire if no change");
Assert.AreEqual(1, watcher2Count, "Watcher 2 should not fire if no change");
// Change
sim.SetSource(source, true);
_ = sim.RunUntilStable();
Assert.AreEqual(2, watcher1Count, "Watcher 1 should fire on second change");
Assert.AreEqual(2, watcher2Count, "Watcher 2 should fire on second change");
// Dispose one watcher
sub2.Dispose();
// Change again
sim.SetSource(source, false);
_ = sim.RunUntilStable();
Assert.AreEqual(3, watcher1Count, "Watcher 1 should fire after sub2 is disposed");
Assert.AreEqual(2, watcher2Count, "Watcher 2 should NOT fire after being disposed");
}
}