< Summary

Information
Class: Chronicler.Timing.ChronicleClock
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Timing/ChronicleClock.cs
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 117
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Advance()100%22100%
SetStepDuration(...)100%11100%
GetDeadlineFrame(...)100%22100%
Reset()100%11100%
RecordData(...)100%88100%
ValidateStepDuration(...)100%22100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler/Timing/ChronicleClock.cs

#LineLine coverage
 1//=======================================================================
 2// ChronicleClock.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9
 10namespace Chronicler.Timing;
 11
 12/// <summary>
 13/// A host-advanced simulation clock with an exact elapsed timeline and a positive step.
 14/// </summary>
 15/// <remarks>
 16/// This clock has no wall-time source or thread synchronization. Its owner controls
 17/// advancement and invalidates retained work when resetting or restoring a timeline.
 18/// </remarks>
 19public sealed class ChronicleClock : IRecordable
 20{
 21    /// <summary>Creates a clock at frame and time zero.</summary>
 22    /// <param name="stepDuration">The positive duration added to elapsed time on each advance.</param>
 23    /// <exception cref="ArgumentOutOfRangeException">The step is zero or negative.</exception>
 6324    public ChronicleClock(ChronicleDuration stepDuration)
 25    {
 6326        SetStepDuration(stepDuration);
 6027    }
 28
 29    /// <summary>Gets the completed advance count.</summary>
 30    /// <value>A nonnegative count since the current timeline's origin.</value>
 31    public long FrameCount { get; private set; }
 32
 33    /// <summary>Gets accumulated simulation time.</summary>
 34    /// <value>The sum of all completed step durations relative to the timeline origin.</value>
 35    public ChronicleTimestamp ElapsedTime { get; private set; }
 36
 37    /// <summary>Gets the next step's duration.</summary>
 38    /// <value>A positive interval. Changing it leaves previously accumulated time intact.</value>
 39    public ChronicleDuration StepDuration { get; private set; }
 40
 41    /// <summary>Advances one frame and one configured step, or leaves all state unchanged.</summary>
 42    /// <exception cref="InvalidOperationException">The frame counter is exhausted.</exception>
 43    /// <exception cref="OverflowException">The next elapsed timestamp cannot be represented.</exception>
 44    public void Advance()
 45    {
 224346        if (FrameCount == long.MaxValue)
 247            throw new InvalidOperationException("The simulation clock's frame counter is exhausted.");
 224148        long nextFrame = FrameCount + 1;
 224149        ChronicleTimestamp nextTime = ElapsedTime + StepDuration;
 223850        FrameCount = nextFrame;
 223851        ElapsedTime = nextTime;
 223852    }
 53
 54    /// <summary>Sets the duration of future advances.</summary>
 55    /// <param name="stepDuration">The positive duration to use starting with the next advance.</param>
 56    /// <exception cref="ArgumentOutOfRangeException">The step is zero or negative. The previous step remains in effect.
 57    public void SetStepDuration(ChronicleDuration stepDuration)
 58    {
 217559        ValidateStepDuration(stepDuration);
 216960        StepDuration = stepDuration;
 216961    }
 62
 63    /// <summary>Calculates a frame deadline without scheduling work.</summary>
 64    /// <param name="framesFromNow">A nonnegative count of advances from the current frame. Zero selects the current fra
 65    /// <returns>The current frame plus the requested offset.</returns>
 66    /// <exception cref="ArgumentOutOfRangeException">The offset is negative.</exception>
 67    /// <exception cref="OverflowException">The deadline exceeds <see cref="long.MaxValue"/>.</exception>
 68    public long GetDeadlineFrame(long framesFromNow)
 69    {
 205970        if (framesFromNow < 0)
 171            throw new ArgumentOutOfRangeException(nameof(framesFromNow));
 205872        return checked(FrameCount + framesFromNow);
 73    }
 74
 75    /// <summary>Starts a new zero-based timeline, preserving the configured step.</summary>
 76    /// <remarks>The owner must invalidate work associated with the previous timeline.</remarks>
 77    public void Reset()
 78    {
 579        FrameCount = 0;
 580        ElapsedTime = ChronicleTimestamp.Zero;
 581    }
 82
 83    /// <summary>Transfers the clock through its version-one schema.</summary>
 84    /// <param name="chronicler">The active pass for reading or writing clock state.</param>
 85    /// <remarks>The load stages all fields and validates them before updating the clock. The host stops advancement and
 86    /// <exception cref="InvalidOperationException">A required schema is absent or unsupported, the frame count is negat
 87    /// <exception cref="ArgumentOutOfRangeException">The elapsed timestamp is negative or the step is not positive.</ex
 88    public void RecordData(IChronicler chronicler)
 89    {
 17990        int schemaVersion = 1;
 17991        long frameCount = FrameCount;
 17992        ChronicleTimestamp elapsedTime = ElapsedTime;
 17993        ChronicleDuration stepDuration = StepDuration;
 17994        RecordValues.Look(chronicler, ref schemaVersion, "SchemaVersion", 0);
 17995        RecordValues.Look(chronicler, ref frameCount, nameof(FrameCount), 0L);
 17996        RecordChronicleTime.Look(chronicler, ref elapsedTime, nameof(ElapsedTime));
 17197        RecordChronicleTime.Look(chronicler, ref stepDuration, nameof(StepDuration));
 98
 16599        if (chronicler.Mode == SerializationMode.Loading)
 100        {
 32101            if (schemaVersion != 1)
 4102                throw new InvalidOperationException("The clock schema is missing or unsupported.");
 28103            if (frameCount < 0 || (frameCount == 0) != (elapsedTime == ChronicleTimestamp.Zero))
 6104                throw new InvalidOperationException("Clock frames and elapsed time must be nonnegative and agree at zero
 22105            ValidateStepDuration(stepDuration);
 18106            FrameCount = frameCount;
 18107            ElapsedTime = elapsedTime;
 18108            StepDuration = stepDuration;
 109        }
 151110    }
 111
 112    private static void ValidateStepDuration(ChronicleDuration stepDuration)
 113    {
 2197114        if (stepDuration <= ChronicleDuration.Zero)
 10115            throw new ArgumentOutOfRangeException(nameof(stepDuration), "The simulation step must be positive.");
 2187116    }
 117}