< Summary

Information
Class: Chronicler.Timing.ChronicleTimestamp
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Timing/ChronicleTimestamp.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 115
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%22100%
get_Zero()100%11100%
get_WholeSeconds()100%11100%
get_FractionalSecond()100%11100%
CompareTo(...)100%11100%
Equals(...)100%11100%
Equals(...)100%22100%
GetHashCode()100%11100%
op_Addition(...)100%22100%
op_Subtraction(...)100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
op_LessThan(...)100%11100%
op_LessThanOrEqual(...)100%11100%
op_GreaterThan(...)100%11100%
op_GreaterThanOrEqual(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// ChronicleTimestamp.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/// An exact nonnegative instant relative to a simulation origin, with 2^-32-second precision.
 14/// </summary>
 15/// <remarks>
 16/// The range is <c>(0, 0)</c> through <c>(long.MaxValue, uint.MaxValue)</c>.
 17/// The owner matches origins and lifetimes before comparing or subtracting timestamps.
 18/// </remarks>
 19public readonly struct ChronicleTimestamp : IEquatable<ChronicleTimestamp>, IComparable<ChronicleTimestamp>
 20{
 21    private readonly ChronicleDuration _value;
 22
 23    /// <summary>Creates an instant from nonnegative whole seconds and an unsigned fraction.</summary>
 24    /// <param name="wholeSeconds">The mathematical floor of elapsed seconds since the origin.</param>
 25    /// <param name="fractionalSecond">The nonnegative remainder in units of 2^-32 seconds.</param>
 26    /// <exception cref="ArgumentOutOfRangeException">Whole seconds are negative.</exception>
 27    public ChronicleTimestamp(long wholeSeconds, uint fractionalSecond)
 28    {
 529929        if (wholeSeconds < 0)
 630            throw new ArgumentOutOfRangeException(nameof(wholeSeconds), "A timestamp cannot precede its origin.");
 529331        _value = new ChronicleDuration(wholeSeconds, fractionalSecond);
 529332    }
 33
 34    /// <summary>Gets the simulation origin.</summary>
 35    /// <value>Both components are zero, which is also the default value.</value>
 5436    public static ChronicleTimestamp Zero => default;
 37
 38    /// <summary>Gets floor seconds for this instant.</summary>
 39    /// <value>Nonnegative whole seconds since the simulation origin.</value>
 138840    public long WholeSeconds => _value.WholeSeconds;
 41
 42    /// <summary>Gets the remainder after floor seconds.</summary>
 43    /// <value>A nonnegative count of 2^-32-second units, from zero through 2^32 − 1.</value>
 138844    public uint FractionalSecond => _value.FractionalSecond;
 45
 46    /// <summary>Orders this instant relative to another.</summary>
 47    /// <param name="other">An instant from the same origin and lifetime.</param>
 48    /// <returns>A negative value for less, zero for equal, and a positive value for greater.</returns>
 1224149    public int CompareTo(ChronicleTimestamp other) => _value.CompareTo(other._value);
 50
 51    /// <summary>Tests exact instant equality.</summary>
 52    /// <param name="other">An instant from the same origin and lifetime.</param>
 53    /// <returns>True when both components match.</returns>
 587154    public bool Equals(ChronicleTimestamp other) => _value.Equals(other._value);
 55
 56    /// <summary>Tests equality with a boxed instant.</summary>
 57    /// <param name="obj">The object to compare. Null or another type compares unequal.</param>
 58    /// <returns>True when the object has this value type and both components match.</returns>
 459    public override bool Equals(object? obj) => obj is ChronicleTimestamp other && Equals(other);
 60
 61    /// <summary>Hashes the instant for use in collections.</summary>
 62    /// <returns>A deterministic 32-bit mix of both components.</returns>
 205163    public override int GetHashCode() => _value.GetHashCode();
 64
 65    /// <summary>Offsets an instant by a signed interval.</summary>
 66    /// <param name="time">The starting instant in the current timeline.</param>
 67    /// <param name="delta">The signed displacement in simulation seconds.</param>
 68    /// <returns>The resulting instant relative to the same origin.</returns>
 69    /// <exception cref="OverflowException">The result precedes the origin or exceeds the timestamp range.</exception>
 70    public static ChronicleTimestamp operator +(ChronicleTimestamp time, ChronicleDuration delta)
 71    {
 483972        ChronicleDuration result = time._value + delta;
 481173        if (result.WholeSeconds < 0)
 2774            throw new OverflowException("The timestamp would precede its origin.");
 478475        return new ChronicleTimestamp(result.WholeSeconds, result.FractionalSecond);
 76    }
 77
 78    /// <summary>Measures the signed interval between two instants.</summary>
 79    /// <param name="left">The ending instant.</param>
 80    /// <param name="right">The starting instant from the same origin and lifetime.</param>
 81    /// <returns>The exact difference, negative when the ending instant precedes the start.</returns>
 82    public static ChronicleDuration operator -(ChronicleTimestamp left, ChronicleTimestamp right) =>
 246283        left._value - right._value;
 84
 85    /// <summary>Tests whether the left instant exactly equals the right instant.</summary>
 86    /// <param name="left">The first operand.</param>
 87    /// <param name="right">The second operand from the same origin and lifetime.</param>
 88    /// <returns>True when the left operand exactly equals the right operand.</returns>
 42689    public static bool operator ==(ChronicleTimestamp left, ChronicleTimestamp right) => left.Equals(right);
 90    /// <summary>Tests whether the left instant differs from the right instant.</summary>
 91    /// <param name="left">The first operand.</param>
 92    /// <param name="right">The second operand from the same origin and lifetime.</param>
 93    /// <returns>True when the left operand differs from the right operand.</returns>
 244894    public static bool operator !=(ChronicleTimestamp left, ChronicleTimestamp right) => !left.Equals(right);
 95    /// <summary>Tests whether the left instant is less than the right instant.</summary>
 96    /// <param name="left">The first operand.</param>
 97    /// <param name="right">The second operand from the same origin and lifetime.</param>
 98    /// <returns>True when the left operand is less than the right operand.</returns>
 244899    public static bool operator <(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) < 0;
 100    /// <summary>Tests whether the left instant is less than or equal to the right instant.</summary>
 101    /// <param name="left">The first operand.</param>
 102    /// <param name="right">The second operand from the same origin and lifetime.</param>
 103    /// <returns>True when the left operand is less than or equal to the right operand.</returns>
 2448104    public static bool operator <=(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) <= 0;
 105    /// <summary>Tests whether the left instant is greater than the right instant.</summary>
 106    /// <param name="left">The first operand.</param>
 107    /// <param name="right">The second operand from the same origin and lifetime.</param>
 108    /// <returns>True when the left operand is greater than the right operand.</returns>
 4496109    public static bool operator >(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) > 0;
 110    /// <summary>Tests whether the left instant is greater than or equal to the right instant.</summary>
 111    /// <param name="left">The first operand.</param>
 112    /// <param name="right">The second operand from the same origin and lifetime.</param>
 113    /// <returns>True when the left operand is greater than or equal to the right operand.</returns>
 2448114    public static bool operator >=(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) >= 0;
 115}