Contract for verifying the efficiency of the hash code generation algorithm for a given type.

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class HashCodeAcceptanceContract<T> : AbstractContract
Visual Basic (Declaration)
Public Class HashCodeAcceptanceContract(Of T) _
	Inherits AbstractContract

Type Parameters

T
The type of the object for which the hash code generation must be evaluated.

Remarks

Built-in verifications:

  • CollisionProbabilityTest : The probability of hash code collision for the specified set of instances is less than the specified limit.
  • UniformDistributionTest : The probability of deviation from uniform distribution is less than the specified limit.

Examples

The following examples shows a simple class that overrides the GetHashCode()()() method, and the test which verifies that the algorithm is resistant to hash code collision against the specified set of distinct values.
CopyC#
public class Foo
{
    private readonly int key1;
    private readonly int key2;

    public Foo(int key1, int key2)
    {
        this.name = key1;
        this.key2 = key2;
    }

    public override int GetHashCode()
    {
        int hash = 17;
        hash = hash * 23 + id1.GetHashCode();
        hash = hash * 23 + id2.GetHashCode();
        return hash;
    }
}

[TestFixture]
public class FooTest
{
    [VerifyContract]
    public readonly IContract HashCodeAcceptanceTests = new HashCodeAcceptanceContract<Foo>
    {
        CollisionProbabilityLimit = CollisionProbability.VeryLow,
        UniformDistributionQuality = UniformDistributionQuality.Excellent,
        DistinctInstances = GetDistinctInstances();
    };

    private static IEnumerable<Foo> GetDistinctInstances()
    {
        for(int i=0; i<1000; i++)
            for(int j=0; j<1000; j++)
                yield return new Foo(i, j);
    }
}

Inheritance Hierarchy

System..::.Object
  MbUnit.Framework.ContractVerifiers..::.AbstractContract
    MbUnit.Framework.ContractVerifiers..::.HashCodeAcceptanceContract<(Of <(T>)>)

See Also