< Summary

Information
Class: Chronicler.Timing.ChronicleDuration
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Timing/ChronicleDuration.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 128
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%
get_Zero()100%11100%
CompareTo(...)100%22100%
Equals(...)100%22100%
Equals(...)100%22100%
GetHashCode()100%11100%
op_Addition(...)100%44100%
op_Subtraction(...)100%44100%
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/ChronicleDuration.cs

#LineLine coverage
 1//=======================================================================
 2// ChronicleDuration.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 signed interval with 64-bit whole seconds and 32 binary fractional bits.
 14/// </summary>
 15/// <remarks>
 16/// Whole seconds use mathematical floor. Negative one fractional unit is <c>(-1, uint.MaxValue)</c>.
 17/// The range is <c>(long.MinValue, 0)</c> through <c>(long.MaxValue, uint.MaxValue)</c>.
 18/// Arithmetic throws when the final result exceeds this range.
 19/// </remarks>
 20public readonly struct ChronicleDuration : IEquatable<ChronicleDuration>, IComparable<ChronicleDuration>
 21{
 22    /// <summary>Creates an interval from floor seconds and an unsigned fraction.</summary>
 23    /// <param name="wholeSeconds">The mathematical floor of the interval in seconds.</param>
 24    /// <param name="fractionalSecond">The nonnegative remainder in units of 2^-32 seconds.</param>
 25    public ChronicleDuration(long wholeSeconds, uint fractionalSecond)
 26    {
 2756727        WholeSeconds = wholeSeconds;
 2756728        FractionalSecond = fractionalSecond;
 2756729    }
 30
 31    /// <summary>Gets the zero interval.</summary>
 32    /// <value>Both components are zero, which is also the default value.</value>
 220933    public static ChronicleDuration Zero => default;
 34
 35    /// <summary>Gets floor seconds for this interval.</summary>
 36    /// <value>The signed mathematical floor, including for negative fractions.</value>
 37    public long WholeSeconds { get; }
 38
 39    /// <summary>Gets the remainder after floor seconds.</summary>
 40    /// <value>A nonnegative count of 2^-32-second units, from zero through 2^32 − 1.</value>
 41    public uint FractionalSecond { get; }
 42
 43    /// <summary>Orders this interval relative to another.</summary>
 44    /// <param name="other">The interval to order against.</param>
 45    /// <returns>A negative value for less, zero for equal, and a positive value for greater.</returns>
 46    public int CompareTo(ChronicleDuration other)
 47    {
 4299648        int seconds = WholeSeconds.CompareTo(other.WholeSeconds);
 4299649        return seconds != 0 ? seconds : FractionalSecond.CompareTo(other.FractionalSecond);
 50    }
 51
 52    /// <summary>Tests exact interval equality.</summary>
 53    /// <param name="other">The interval to compare by component.</param>
 54    /// <returns>True when both components match.</returns>
 55    public bool Equals(ChronicleDuration other) =>
 2637756        WholeSeconds == other.WholeSeconds && FractionalSecond == other.FractionalSecond;
 57
 58    /// <summary>Tests equality with a boxed interval.</summary>
 59    /// <param name="obj">The object to compare. Null or another type compares unequal.</param>
 60    /// <returns>True when the object has this value type and both components match.</returns>
 561    public override bool Equals(object? obj) => obj is ChronicleDuration other && Equals(other);
 62
 63    /// <summary>Hashes the interval for use in collections.</summary>
 64    /// <returns>A deterministic 32-bit mix of both components.</returns>
 419565    public override int GetHashCode() => unchecked(
 419566        ((int)WholeSeconds ^ (int)(WholeSeconds >> 32)) * 397 ^ (int)FractionalSecond);
 67
 68    /// <summary>Adds two exact intervals.</summary>
 69    /// <param name="left">The initial interval.</param>
 70    /// <param name="right">The interval to add, which may be negative.</param>
 71    /// <returns>The exact accumulated duration.</returns>
 72    /// <exception cref="OverflowException">The final interval is outside the signed whole-second range.</exception>
 73    public static ChronicleDuration operator +(ChronicleDuration left, ChronicleDuration right)
 74    {
 1096475        ulong fraction = (ulong)left.FractionalSecond + right.FractionalSecond;
 1096476        long seconds = unchecked(left.WholeSeconds + right.WholeSeconds + (long)(fraction >> 32));
 77        // Test the final sign after carrying; the intermediate sum may overflow and recover.
 1096478        if ((left.WholeSeconds ^ right.WholeSeconds) >= 0 && (left.WholeSeconds ^ seconds) < 0)
 102879            throw new OverflowException("The duration sum is outside the supported range.");
 993680        return new ChronicleDuration(seconds, unchecked((uint)fraction));
 81    }
 82
 83    /// <summary>Subtracts one interval from another.</summary>
 84    /// <param name="left">The initial interval.</param>
 85    /// <param name="right">The interval to subtract, which may be negative.</param>
 86    /// <returns>The exact remaining duration.</returns>
 87    /// <exception cref="OverflowException">The final interval is outside the signed whole-second range.</exception>
 88    public static ChronicleDuration operator -(ChronicleDuration left, ChronicleDuration right)
 89    {
 858790        ulong fraction = unchecked((ulong)left.FractionalSecond - right.FractionalSecond);
 91        // Fractional underflow sets bit 63; subtract that borrow before testing the final sign.
 858792        long seconds = unchecked(left.WholeSeconds - right.WholeSeconds - (long)(fraction >> 63));
 858793        if ((left.WholeSeconds ^ right.WholeSeconds) < 0 && (left.WholeSeconds ^ seconds) < 0)
 99994            throw new OverflowException("The duration difference is outside the supported range.");
 758895        return new ChronicleDuration(seconds, unchecked((uint)fraction));
 96    }
 97
 98    /// <summary>Tests whether the left interval exactly equals the right interval.</summary>
 99    /// <param name="left">The first operand.</param>
 100    /// <param name="right">The second operand of the comparison.</param>
 101    /// <returns>True when the left operand exactly equals the right operand.</returns>
 8169102    public static bool operator ==(ChronicleDuration left, ChronicleDuration right) => left.Equals(right);
 103    /// <summary>Tests whether the left interval differs from the right interval.</summary>
 104    /// <param name="left">The first operand.</param>
 105    /// <param name="right">The second operand of the comparison.</param>
 106    /// <returns>True when the left operand differs from the right operand.</returns>
 6121107    public static bool operator !=(ChronicleDuration left, ChronicleDuration right) => !left.Equals(right);
 108    /// <summary>Tests whether the left interval is less than the right interval.</summary>
 109    /// <param name="left">The first operand.</param>
 110    /// <param name="right">The second operand of the comparison.</param>
 111    /// <returns>True when the left operand is less than the right operand.</returns>
 6121112    public static bool operator <(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) < 0;
 113    /// <summary>Tests whether the left interval is less than or equal to the right interval.</summary>
 114    /// <param name="left">The first operand.</param>
 115    /// <param name="right">The second operand of the comparison.</param>
 116    /// <returns>True when the left operand is less than or equal to the right operand.</returns>
 8318117    public static bool operator <=(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) <= 0;
 118    /// <summary>Tests whether the left interval is greater than the right interval.</summary>
 119    /// <param name="left">The first operand.</param>
 120    /// <param name="right">The second operand of the comparison.</param>
 121    /// <returns>True when the left operand is greater than the right operand.</returns>
 6121122    public static bool operator >(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) > 0;
 123    /// <summary>Tests whether the left interval is greater than or equal to the right interval.</summary>
 124    /// <param name="left">The first operand.</param>
 125    /// <param name="right">The second operand of the comparison.</param>
 126    /// <returns>True when the left operand is greater than or equal to the right operand.</returns>
 6121127    public static bool operator >=(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) >= 0;
 128}