|
| 1 | +using Celerity.Hashing; |
| 2 | + |
| 3 | +namespace Celerity.Tests.Hashing; |
| 4 | + |
| 5 | +public class UInt64HasherTests |
| 6 | +{ |
| 7 | + private readonly UInt64Hasher _hasher = new UInt64Hasher(); |
| 8 | + |
| 9 | + [Fact] |
| 10 | + public void Hash_Zero_ReturnsZero() |
| 11 | + { |
| 12 | + // Murmur3 fmix64 maps 0 -> 0 (each stage is a no-op on the zero state). |
| 13 | + Assert.Equal(0, _hasher.Hash(0UL)); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Hash_IsDeterministic() |
| 18 | + { |
| 19 | + ulong value = 0xDEADBEEFCAFEBABEUL; |
| 20 | + int result1 = _hasher.Hash(value); |
| 21 | + int result2 = _hasher.Hash(value); |
| 22 | + Assert.Equal(result1, result2); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void Hash_DistinctInputs_ProduceDistinctResultsForSmallRange() |
| 27 | + { |
| 28 | + // Murmur3 fmix64 is a bijection on 64 bits; truncating to 32 bits on a |
| 29 | + // small sequential range should still produce distinct hashes with |
| 30 | + // overwhelming probability. A collision here would indicate a broken |
| 31 | + // mixer rather than an expected birthday-paradox event. |
| 32 | + var seen = new HashSet<int>(); |
| 33 | + for (ulong i = 0; i < 1000; i++) |
| 34 | + { |
| 35 | + Assert.True(seen.Add(_hasher.Hash(i)), |
| 36 | + $"Unexpected collision at input {i}."); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Hash_HighBits_InfluenceResult() |
| 42 | + { |
| 43 | + // Avalanche check: two inputs that differ only in their top bit |
| 44 | + // should produce different 32-bit hashes. |
| 45 | + int low = _hasher.Hash(1UL); |
| 46 | + int high = _hasher.Hash(1UL | (1UL << 63)); |
| 47 | + Assert.NotEqual(low, high); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Hash_DoesNotThrow() |
| 52 | + { |
| 53 | + ulong[] testValues = |
| 54 | + { |
| 55 | + 0UL, |
| 56 | + 1UL, |
| 57 | + ulong.MaxValue, |
| 58 | + 0x7FFFFFFFFFFFFFFFUL, |
| 59 | + 0x8000000000000000UL, |
| 60 | + 0xDEADBEEFCAFEBABEUL, |
| 61 | + }; |
| 62 | + |
| 63 | + foreach (ulong val in testValues) |
| 64 | + { |
| 65 | + var exception = Record.Exception(() => _hasher.Hash(val)); |
| 66 | + Assert.Null(exception); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments