|
| 1 | +using Celerity.Collections; |
| 2 | +using Celerity.Hashing; |
| 3 | + |
| 4 | +namespace Celerity.Tests.Collections; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Regression tests for issue #23. <see cref="IntDictionary{TValue, THasher}.TryAdd"/>, |
| 8 | +/// <see cref="CelerityDictionary{TKey, TValue, THasher}.TryAdd"/>, |
| 9 | +/// <see cref="IntSet{THasher}.TryAdd"/>, and |
| 10 | +/// <see cref="CeleritySet{T, THasher}.TryAdd"/> historically walked the probe chain |
| 11 | +/// twice on the new-key path: once via <c>ContainsKey</c>/<c>Contains</c>, then again |
| 12 | +/// via the indexer setter / <c>InsertNon*</c> helper. The fix collapses both walks |
| 13 | +/// into a single <c>ProbeForInsert</c>. These tests pin that contract by counting how |
| 14 | +/// many times the underlying <see cref="IHashProvider{T}"/>'s <c>Hash</c> method is |
| 15 | +/// called: each probe-chain walk starts with exactly one <c>Hash</c> call (the |
| 16 | +/// subsequent linear-probe steps don't re-hash), so hash-call count is a faithful |
| 17 | +/// proxy for probe-chain walk count. |
| 18 | +/// |
| 19 | +/// All tests pre-size the collection so no <c>Resize</c> happens during the asserted |
| 20 | +/// region — <c>Resize</c> would otherwise add hash calls of its own as it re-inserts |
| 21 | +/// the existing entries into the new array. |
| 22 | +/// </summary> |
| 23 | +public class TryAddProbeCountTests |
| 24 | +{ |
| 25 | + private static int _hashCallCount; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// A counting hasher for <see cref="int"/> keys. The implementation is a |
| 29 | + /// minimal Wang-style mix so distinct keys distribute across the table; the |
| 30 | + /// only test-relevant aspect is that each call increments a static counter. |
| 31 | + /// </summary> |
| 32 | + private struct CountingIntHasher : IHashProvider<int> |
| 33 | + { |
| 34 | + public int Hash(int key) |
| 35 | + { |
| 36 | + _hashCallCount++; |
| 37 | + unchecked |
| 38 | + { |
| 39 | + uint x = (uint)key; |
| 40 | + x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 41 | + x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 42 | + x = (x >> 16) ^ x; |
| 43 | + return (int)x; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// A counting hasher for <see cref="string"/> keys. |
| 50 | + /// </summary> |
| 51 | + private struct CountingStringHasher : IHashProvider<string> |
| 52 | + { |
| 53 | + public int Hash(string key) |
| 54 | + { |
| 55 | + _hashCallCount++; |
| 56 | + // Deliberately use a stable, deterministic mix; we don't care about |
| 57 | + // distribution for these tests, only that the call is counted. |
| 58 | + return key.GetHashCode(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void IntDictionary_TryAdd_NewKey_DoesExactlyOneProbeWalk() |
| 64 | + { |
| 65 | + // Pre-size so the asserted inserts never resize. |
| 66 | + var map = new IntDictionary<int, CountingIntHasher>(capacity: 64); |
| 67 | + _hashCallCount = 0; |
| 68 | + |
| 69 | + // 10 brand-new (non-zero) keys. Pre-fix this allocated 20 hash calls. |
| 70 | + for (int i = 1; i <= 10; i++) |
| 71 | + Assert.True(map.TryAdd(i, i * 10)); |
| 72 | + |
| 73 | + Assert.Equal(10, _hashCallCount); |
| 74 | + Assert.Equal(10, map.Count); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void IntDictionary_TryAdd_DuplicateKey_DoesExactlyOneProbeWalk() |
| 79 | + { |
| 80 | + var map = new IntDictionary<int, CountingIntHasher>(capacity: 64); |
| 81 | + for (int i = 1; i <= 5; i++) |
| 82 | + map.TryAdd(i, i); |
| 83 | + |
| 84 | + _hashCallCount = 0; |
| 85 | + for (int i = 1; i <= 5; i++) |
| 86 | + Assert.False(map.TryAdd(i, -1)); |
| 87 | + |
| 88 | + Assert.Equal(5, _hashCallCount); |
| 89 | + // Original values must remain untouched on the duplicate path. |
| 90 | + for (int i = 1; i <= 5; i++) |
| 91 | + Assert.Equal(i, map[i]); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void IntDictionary_Add_NewKey_DoesExactlyOneProbeWalk() |
| 96 | + { |
| 97 | + var map = new IntDictionary<int, CountingIntHasher>(capacity: 64); |
| 98 | + _hashCallCount = 0; |
| 99 | + |
| 100 | + for (int i = 1; i <= 10; i++) |
| 101 | + map.Add(i, i); |
| 102 | + |
| 103 | + Assert.Equal(10, _hashCallCount); |
| 104 | + Assert.Equal(10, map.Count); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void CelerityDictionary_TryAdd_NewKey_DoesExactlyOneProbeWalk() |
| 109 | + { |
| 110 | + var map = new CelerityDictionary<string, int, CountingStringHasher>(capacity: 64); |
| 111 | + _hashCallCount = 0; |
| 112 | + |
| 113 | + for (int i = 1; i <= 10; i++) |
| 114 | + Assert.True(map.TryAdd($"k{i}", i)); |
| 115 | + |
| 116 | + Assert.Equal(10, _hashCallCount); |
| 117 | + Assert.Equal(10, map.Count); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void CelerityDictionary_TryAdd_DuplicateKey_DoesExactlyOneProbeWalk() |
| 122 | + { |
| 123 | + var map = new CelerityDictionary<string, int, CountingStringHasher>(capacity: 64); |
| 124 | + for (int i = 1; i <= 5; i++) |
| 125 | + map.TryAdd($"k{i}", i); |
| 126 | + |
| 127 | + _hashCallCount = 0; |
| 128 | + for (int i = 1; i <= 5; i++) |
| 129 | + Assert.False(map.TryAdd($"k{i}", -1)); |
| 130 | + |
| 131 | + Assert.Equal(5, _hashCallCount); |
| 132 | + for (int i = 1; i <= 5; i++) |
| 133 | + Assert.Equal(i, map[$"k{i}"]); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void IntSet_TryAdd_NewItem_DoesExactlyOneProbeWalk() |
| 138 | + { |
| 139 | + var set = new IntSet<CountingIntHasher>(capacity: 64); |
| 140 | + _hashCallCount = 0; |
| 141 | + |
| 142 | + for (int i = 1; i <= 10; i++) |
| 143 | + Assert.True(set.TryAdd(i)); |
| 144 | + |
| 145 | + Assert.Equal(10, _hashCallCount); |
| 146 | + Assert.Equal(10, set.Count); |
| 147 | + } |
| 148 | + |
| 149 | + [Fact] |
| 150 | + public void IntSet_TryAdd_DuplicateItem_DoesExactlyOneProbeWalk() |
| 151 | + { |
| 152 | + var set = new IntSet<CountingIntHasher>(capacity: 64); |
| 153 | + for (int i = 1; i <= 5; i++) |
| 154 | + set.TryAdd(i); |
| 155 | + |
| 156 | + _hashCallCount = 0; |
| 157 | + for (int i = 1; i <= 5; i++) |
| 158 | + Assert.False(set.TryAdd(i)); |
| 159 | + |
| 160 | + Assert.Equal(5, _hashCallCount); |
| 161 | + Assert.Equal(5, set.Count); |
| 162 | + } |
| 163 | + |
| 164 | + [Fact] |
| 165 | + public void CeleritySet_TryAdd_NewItem_DoesExactlyOneProbeWalk() |
| 166 | + { |
| 167 | + var set = new CeleritySet<string, CountingStringHasher>(capacity: 64); |
| 168 | + _hashCallCount = 0; |
| 169 | + |
| 170 | + for (int i = 1; i <= 10; i++) |
| 171 | + Assert.True(set.TryAdd($"v{i}")); |
| 172 | + |
| 173 | + Assert.Equal(10, _hashCallCount); |
| 174 | + Assert.Equal(10, set.Count); |
| 175 | + } |
| 176 | + |
| 177 | + [Fact] |
| 178 | + public void CeleritySet_TryAdd_DuplicateItem_DoesExactlyOneProbeWalk() |
| 179 | + { |
| 180 | + var set = new CeleritySet<string, CountingStringHasher>(capacity: 64); |
| 181 | + for (int i = 1; i <= 5; i++) |
| 182 | + set.TryAdd($"v{i}"); |
| 183 | + |
| 184 | + _hashCallCount = 0; |
| 185 | + for (int i = 1; i <= 5; i++) |
| 186 | + Assert.False(set.TryAdd($"v{i}")); |
| 187 | + |
| 188 | + Assert.Equal(5, _hashCallCount); |
| 189 | + Assert.Equal(5, set.Count); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public void IntDictionary_TryAdd_PreservesExistingValueOnDuplicate() |
| 194 | + { |
| 195 | + // Behavioural guard: TryAdd must not overwrite the existing value when |
| 196 | + // it returns false. The single-probe rewrite has to be careful here |
| 197 | + // because ProbeForInsert returns the existing slot — we must read, |
| 198 | + // detect, and bail out before writing. |
| 199 | + var map = new IntDictionary<int, CountingIntHasher>(capacity: 64); |
| 200 | + map.TryAdd(7, 700); |
| 201 | + |
| 202 | + Assert.False(map.TryAdd(7, -1)); |
| 203 | + Assert.Equal(700, map[7]); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public void CelerityDictionary_TryAdd_PreservesExistingValueOnDuplicate() |
| 208 | + { |
| 209 | + var map = new CelerityDictionary<string, int, CountingStringHasher>(capacity: 64); |
| 210 | + map.TryAdd("k7", 700); |
| 211 | + |
| 212 | + Assert.False(map.TryAdd("k7", -1)); |
| 213 | + Assert.Equal(700, map["k7"]); |
| 214 | + } |
| 215 | +} |
0 commit comments