CsCheck/Check.cs
/// <summary>Check a hash of a series of values. Cache values on a correct run and fail with stack trace at first difference.</summary>
/// <param name="action">The code called to add values into the hash.</param>
/// <param name="expected">The expected hash value set after an initial run to find it.</param>
/// <param name="decimalPlaces">The number of decimal places to round for double, float and decimal.</param>
/// <param name="significantFigures">The number of significant figures to round for double, float and decimal.</param>
/// <param name="memberName">Automatically set to the method name.</param>
/// <param name="filePath">Automatically set to the file path.</param>
Check a hash of a series of values. Cache values on a correct run and fail with stack trace at first difference.
Check a hash of a series of values . Cache values on a correct run and fail with stack trace at first difference .
public static void Hash(Action<Hash> action, long expected = 0, int? decimalPlaces = null, int? significantFigures = null,
            [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "")
        {
            if (expected == 0)
            {
                var hash = new Hash(null, -1, decimalPlaces, significantFigures);
                action(hash);
                var offset = hash.BestOffset();
                hash = new Hash(null, offset, decimalPlaces, significantFigures);
                action(hash);
                var fullHashCode = CsCheck.Hash.FullHash(offset, hash.GetHashCode());
                throw new CsCheckException("Hash is " + fullHashCode);
            }
            else
            {
                var (offset, expectedHashCode) = CsCheck.Hash.OffsetHash(expected);
                if (File.Exists(CsCheck.Hash.Filename(expected, memberName, filePath)))
                {
                    var hasher = new Hash(null, offset, decimalPlaces, significantFigures);
                    action(hasher);
                    if (hasher.GetHashCode() == expectedHashCode) return;
                }
                var hash = new Hash(expectedHashCode, offset, decimalPlaces, significantFigures, memberName, filePath);
                action(hash);
                int actualHashCode = hash.GetHashCode();
                hash.Close();
                if (actualHashCode != expectedHashCode)
                {
                    hash = new Hash(null, -1, decimalPlaces, significantFigures);
                    action(hash);
                    var offsetCheck = hash.BestOffset();
                    if (offsetCheck != offset)
                    {
                        offset = offsetCheck;
                        hash = new Hash(null, offset, decimalPlaces, significantFigures);
                        action(hash);
                        actualHashCode = hash.GetHashCode();
                    }
                    var actualFullHash = CsCheck.Hash.FullHash(offset, actualHashCode);
                    throw new CsCheckException("Actual " + actualFullHash + " but expected " + expected);
                }
            }
        }
public static void Hash ( Action < Hash > action , long expected = 0 , int ? decimalPlaces = null , int ? significantFigures = null , [ CallerMemberName ] string memberName = " " , [ CallerFilePath ] string filePath = " " ) { if ( expected == 0 ) { var hash = new Hash ( null , - 1 , decimalPlaces , significantFigures ) ; action ( hash ) ; var offset = hash . BestOffset ( ) ; hash = new Hash ( null , offset , decimalPlaces , significantFigures ) ; action ( hash ) ; var fullHashCode = CsCheck . Hash . FullHash ( offset , hash . GetHashCode ( ) ) ; throw new CsCheckException ( " " + fullHashCode ) ; } else { var ( offset , expectedHashCode ) = CsCheck . Hash . OffsetHash ( expected ) ; if ( File . Exists ( CsCheck . Hash . Filename ( expected , memberName , filePath ) ) ) { var hasher = new Hash ( null , offset , decimalPlaces , significantFigures ) ; action ( hasher ) ; if ( hasher . GetHashCode ( ) == expectedHashCode ) return ; } var hash = new Hash ( expectedHashCode , offset , decimalPlaces , significantFigures , memberName , filePath ) ; action ( hash ) ; int actualHashCode = hash . GetHashCode ( ) ; hash . Close ( ) ; if ( actualHashCode != expectedHashCode ) { hash = new Hash ( null , - 1 , decimalPlaces , significantFigures ) ; action ( hash ) ; var offsetCheck = hash . BestOffset ( ) ; if ( offsetCheck != offset ) { offset = offsetCheck ; hash = new Hash ( null , offset , decimalPlaces , significantFigures ) ; action ( hash ) ; actualHashCode = hash . GetHashCode ( ) ; } var actualFullHash = CsCheck . Hash . FullHash ( offset , actualHashCode ) ; throw new CsCheckException ( " " + actualFullHash + " " + expected ) ; } } }
Check a hash of a series of values.
Check a hash of a series of values .
// Check hash without opening the file if it already exists for better IO performance for most common code path.
action
Action<Hash>
The code called to add values into the hash.
The code called to add values into the hash .
expected
long
The expected hash value set after an initial run to find it.
The expected hash value set after an initial run to find it .
decimalPlaces
int?
The number of decimal places to round for double, float and decimal.
The number of decimal places to round for double float and decimal .
significantFigures
int?
The number of significant figures to round for double, float and decimal.
The number of significant figures to round for double float and decimal .
string
[CallerFilePath]
memberName
Automatically set to the method name.
Automatically set to the method name .
filePath
Automatically set to the file path.
Automatically set to the file path .