public void ShouldNotGenerateAdditionalCompareEvent() { var limit = 300UL; var compare = 100UL; var compareCounter = 0; var clockSource = new BaseClockSource(); var timer = new ComparingTimer(clockSource, frequency: 1000000, owner: null, localName: String.Empty, limit: limit, enabled: true, eventEnabled: true, compare: compare); timer.CompareReached += delegate { compareCounter++; }; Assert.AreEqual(0, compareCounter); // Run up to the first compare clockSource.Advance(TimeInterval.FromTicks(100), true); Assert.AreEqual(1, compareCounter); // overwrite timer's value with the same value it has; // this should be a nop timer.Value = 100; Assert.AreEqual(1, compareCounter); // Now run to the next compare event clockSource.Advance(TimeInterval.FromTicks(300), true); Assert.AreEqual(2, compareCounter); Assert.AreEqual(timer.Value, compare); Assert.AreEqual(timer.Compare, compare); }
public void ShouldObserveShorterPeriodClockAfterAdd() { var clockSource = new BaseClockSource(); var counterA = 0; var counterB = 0; Action handlerA = () => counterA++; Action handlerB = () => counterB++; ClockEntry entryA = new ClockEntry(1000, 1, handlerA, null, String.Empty) { Value = 0 }; ClockEntry entryB = new ClockEntry(100, 1, handlerB, null, String.Empty) { Value = 0 }; clockSource.AddClockEntry(entryA); clockSource.Advance(TimeInterval.FromSeconds(500)); clockSource.AddClockEntry(entryB); entryA = clockSource.GetClockEntry(handlerA); entryB = clockSource.GetClockEntry(handlerB); Assert.AreEqual(entryA.Value, 500); Assert.AreEqual(entryA.Period, 1000); Assert.AreEqual(entryB.Value, 0); Assert.AreEqual(entryB.Period, 100); clockSource.Advance(TimeInterval.FromSeconds(50)); entryA = clockSource.GetClockEntry(handlerA); entryB = clockSource.GetClockEntry(handlerB); Assert.AreEqual(counterA, 0); Assert.AreEqual(counterB, 0); Assert.AreEqual(entryA.Value, 550); Assert.AreEqual(entryA.Period, 1000); Assert.AreEqual(entryB.Value, 50); Assert.AreEqual(entryB.Period, 100); clockSource.Advance(TimeInterval.FromSeconds(50)); entryA = clockSource.GetClockEntry(handlerA); entryB = clockSource.GetClockEntry(handlerB); Assert.AreEqual(counterA, 0); Assert.AreEqual(counterB, 1); Assert.AreEqual(entryA.Value, 600); Assert.AreEqual(entryA.Period, 1000); Assert.AreEqual(entryB.Value, 0); Assert.AreEqual(entryB.Period, 100); }
public void ShouldTickWithOneHandler() { var clocksource = new BaseClockSource(); var counter = 0; clocksource.AddClockEntry(new ClockEntry(2, 1, () => counter++) { Value = 0 }); clocksource.Advance(TimeInterval.FromTicks(1)); Assert.AreEqual(0, counter); clocksource.Advance(TimeInterval.FromTicks(1)); Assert.AreEqual(1, counter); clocksource.Advance(TimeInterval.FromTicks(1)); Assert.AreEqual(1, counter); clocksource.Advance(TimeInterval.FromTicks(2)); Assert.AreEqual(2, counter); }
public void ShouldHaveHandlersInSync() { // we test here whether handler executed by the slower clock entry // always "sees" value of the faster one as ten times its own value var clockSource = new BaseClockSource(); Action firstHandler = () => { }; var values = new List <ulong>(); clockSource.AddClockEntry(new ClockEntry(10000, 10, firstHandler, null, String.Empty)); clockSource.AddClockEntry(new ClockEntry(10, 1, () => values.Add(clockSource.GetClockEntry(firstHandler).Value), null, String.Empty)); clockSource.Advance(TimeInterval.FromSeconds(9), true); clockSource.Advance(TimeInterval.FromSeconds(8), true); clockSource.Advance(TimeInterval.FromSeconds(20), true); CollectionAssert.AreEqual(new [] { 100, 200, 300 }, values); }
public void ShouldHaveHandlersInSync() { // we test here whether handler executed by the slower clock entry // always "sees" value of the faster one as ten times its own value var clockSource = new BaseClockSource(); Action firstHandler = () => { }; var values = new List <ulong>(); // clock entry with ratio -10 is 10 times slower than the one with 1 clockSource.AddClockEntry(new ClockEntry(10000, 1, firstHandler)); clockSource.AddClockEntry(new ClockEntry(1, -10, () => values.Add(clockSource.GetClockEntry(firstHandler).Value))); clockSource.Advance(TimeInterval.FromTicks(9), true); clockSource.Advance(TimeInterval.FromTicks(8), true); clockSource.Advance(TimeInterval.FromTicks(20), true); CollectionAssert.AreEqual(new [] { 10, 20, 30 }, values); }
public void ShouldGenerateCompareEventAtCompareAfterClear() { var limit = 300UL; var compare = 100UL; var compareCounter = 0; var clockSource = new BaseClockSource(); var timer = new ComparingTimer(clockSource, frequency: 1000000, owner: null, localName: String.Empty, limit: limit, enabled: true, eventEnabled: true, compare: compare); timer.CompareReached += delegate { compareCounter++; }; // Run to 200 ticks, then clear var advance = 200UL; clockSource.Advance(TimeInterval.FromTicks(advance), true); timer.Value = 0; // Now run to compare compareCounter = 0; clockSource.Advance(TimeInterval.FromTicks(compare), true); Assert.AreEqual(compareCounter, 1); Assert.AreEqual(timer.Value, compare); Assert.AreEqual(timer.Compare, compare); }
public void ShouldTickWithTwoHandlers() { var clocksource = new BaseClockSource(); var counterA = 0; var counterB = 0; clocksource.AddClockEntry(new ClockEntry(2, 1, () => counterA++, null, String.Empty) { Value = 0 }); clocksource.AddClockEntry(new ClockEntry(5, 1, () => counterB++, null, String.Empty) { Value = 0 }); clocksource.Advance(TimeInterval.FromTicks(2)); Assert.AreEqual(1, counterA); Assert.AreEqual(0, counterB); clocksource.Advance(TimeInterval.FromTicks(2)); Assert.AreEqual(2, counterA); Assert.AreEqual(0, counterB); clocksource.Advance(TimeInterval.FromTicks(1)); Assert.AreEqual(2, counterA); Assert.AreEqual(1, counterB); }
public void ShouldTickWithTwoHandlers() { var clocksource = new BaseClockSource(); var counterA = 0; var counterB = 0; clocksource.AddClockEntry(new ClockEntry(2, 1, () => counterA++) { Value = 0 }); clocksource.AddClockEntry(new ClockEntry(5, 1, () => counterB++) { Value = 0 }); clocksource.Advance(2); Assert.AreEqual(1, counterA); Assert.AreEqual(0, counterB); clocksource.Advance(2); Assert.AreEqual(2, counterA); Assert.AreEqual(0, counterB); clocksource.Advance(1); Assert.AreEqual(2, counterA); Assert.AreEqual(1, counterB); }
public void ShouldHandleCompareValueChangeWhenEnabled() { var compareCounter = 0; var clockSource = new BaseClockSource(); var timer = new ComparingTimer(clockSource, 1000000, null, String.Empty, 65535 + 1, enabled: true, workMode: WorkMode.Periodic, eventEnabled: true, compare: 65535); timer.CompareReached += delegate { compareCounter++; }; clockSource.Advance(TimeInterval.FromTicks(65535), true); Assert.AreEqual(65535, timer.Value); Assert.AreEqual(65535, timer.Compare); Assert.AreEqual(1, compareCounter); clockSource.Advance(TimeInterval.FromTicks(1), true); Assert.AreEqual(0, timer.Value); Assert.AreEqual(65535, timer.Compare); Assert.AreEqual(1, compareCounter); compareCounter = 0; clockSource.Advance(TimeInterval.FromTicks(16304), true); Assert.AreEqual(16304, timer.Value); Assert.AreEqual(65535, timer.Compare); Assert.AreEqual(0, compareCounter); timer.Compare = 0; Assert.AreEqual(16304, timer.Value); Assert.AreEqual(0, timer.Compare); Assert.AreEqual(0, compareCounter); clockSource.Advance(TimeInterval.FromTicks(65535 - 16304), true); Assert.AreEqual(65535, timer.Value); Assert.AreEqual(0, timer.Compare); Assert.AreEqual(0, compareCounter); clockSource.Advance(TimeInterval.FromTicks(1), true); Assert.AreEqual(0, timer.Value); Assert.AreEqual(0, timer.Compare); Assert.AreEqual(1, compareCounter); clockSource.Advance(TimeInterval.FromTicks(65535), true); Assert.AreEqual(65535, timer.Value); Assert.AreEqual(0, timer.Compare); Assert.AreEqual(1, compareCounter); clockSource.Advance(TimeInterval.FromTicks(1), true); Assert.AreEqual(0, timer.Value); Assert.AreEqual(0, timer.Compare); Assert.AreEqual(2, compareCounter); }
public void ShouldClearWhenValueSetToZero() { var compare = 200UL; var limit = 300UL; var compareCounter = 0; var clockSource = new BaseClockSource(); var timer = new ComparingTimer(clockSource, frequency: 1000000, owner: null, localName: String.Empty, limit: limit, enabled: true, eventEnabled: true, compare: compare); timer.CompareReached += delegate { compareCounter++; }; // Run to 100 ticks var advance = 100UL; clockSource.Advance(TimeInterval.FromTicks(advance), true); Assert.AreEqual(advance, timer.Value); Assert.AreEqual(compare, timer.Compare); Assert.AreEqual(compareCounter, 0); // Clear timer timer.Value = 0; Assert.AreEqual(timer.Value, 0); Assert.AreEqual(timer.Compare, compare); }