< Summary

Information
Class: Chronicler.Hashing.ChronicleHash
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Hashing/ChronicleHash.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 90
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
Equals(...)100%22100%
Equals(...)100%22100%
GetHashCode()100%11100%
ToString()100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler/Hashing/ChronicleHash.cs

#LineLine coverage
 1//=======================================================================
 2// ChronicleHash.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;
 9using System.Globalization;
 10
 11namespace Chronicler.Hashing;
 12
 13/// <summary>
 14/// Fixed-width deterministic hash value produced by a Chronicler record-hash pass.
 15/// </summary>
 16public readonly struct ChronicleHash : IEquatable<ChronicleHash>
 17{
 18    /// <summary>Creates a hash from two 64-bit components.</summary>
 19    /// <param name="low">The component emitted last in the hexadecimal representation.</param>
 20    /// <param name="high">The component emitted first in the hexadecimal representation.</param>
 21    public ChronicleHash(ulong low, ulong high)
 22    {
 11088023        Low = low;
 11088024        High = high;
 11088025    }
 26
 27    /// <summary>Gets the low component.</summary>
 28    /// <value>The final 16 hexadecimal digits in <see cref="ToString"/>.</value>
 29    public ulong Low { get; }
 30
 31    /// <summary>Gets the high component.</summary>
 32    /// <value>The first 16 hexadecimal digits in <see cref="ToString"/>.</value>
 33    public ulong High { get; }
 34
 35    /// <summary>Tests exact hash equality.</summary>
 36    /// <param name="other">The hash to compare by component.</param>
 37    /// <returns>True when both components match.</returns>
 38    public bool Equals(ChronicleHash other)
 39    {
 16540        return Low == other.Low && High == other.High;
 41    }
 42
 43    /// <summary>Tests equality with a boxed hash.</summary>
 44    /// <param name="obj">The object to compare. Null or another type compares unequal.</param>
 45    /// <returns>True when the object has this value type and both components match.</returns>
 46    public override bool Equals(object? obj)
 47    {
 2948        return obj is ChronicleHash other && Equals(other);
 49    }
 50
 51    /// <summary>Hashes the hash for use in collections.</summary>
 52    /// <returns>A deterministic 32-bit mix of both components.</returns>
 53    public override int GetHashCode()
 54    {
 55        unchecked
 56        {
 257            ulong mixed = Low ^ (High * 0x9e3779b97f4a7c15UL);
 258            mixed ^= mixed >> 33;
 259            mixed *= 0xff51afd7ed558ccdUL;
 260            mixed ^= mixed >> 33;
 261            return (int)(mixed ^ (mixed >> 32));
 62        }
 63    }
 64
 65    /// <summary>Formats the hash for display or comparison.</summary>
 66    /// <returns>Exactly 32 lowercase hexadecimal digits, high component first, with leading zeroes and no separators.</
 67    public override string ToString()
 68    {
 769        return High.ToString("x16", CultureInfo.InvariantCulture)
 770            + Low.ToString("x16", CultureInfo.InvariantCulture);
 71    }
 72
 73    /// <summary>Tests whether the left hash exactly equals the right hash.</summary>
 74    /// <param name="left">The first operand.</param>
 75    /// <param name="right">The second operand of the comparison.</param>
 76    /// <returns>True when the left operand exactly equals the right operand.</returns>
 77    public static bool operator ==(ChronicleHash left, ChronicleHash right)
 78    {
 179        return left.Equals(right);
 80    }
 81
 82    /// <summary>Tests whether the left hash differs from the right hash.</summary>
 83    /// <param name="left">The first operand.</param>
 84    /// <param name="right">The second operand of the comparison.</param>
 85    /// <returns>True when the left operand differs from the right operand.</returns>
 86    public static bool operator !=(ChronicleHash left, ChronicleHash right)
 87    {
 2088        return !left.Equals(right);
 89    }
 90}