|
| 1 | +using Celerity.Collections; |
| 2 | +using Celerity.Hashing; |
| 3 | + |
| 4 | +namespace Celerity.Tests.Collections; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Regression tests for the bulk-constructor sizing fix (issue #27). The |
| 8 | +/// <c>IEnumerable<…></c> source constructors of every hash-table collection |
| 9 | +/// document that the source's <c>Count</c> is used to size the backing storage |
| 10 | +/// "so inserts do not resize" (or "so the initial fill avoids resize work"). |
| 11 | +/// Before the fix the table was sized to the raw count, but the resize threshold |
| 12 | +/// is <c>size × loadFactor</c>, so the last few inserts of the bulk fill still |
| 13 | +/// tripped a full rehash. The fix scales the requested capacity up by |
| 14 | +/// <c>1 / loadFactor</c> so the whole source fits below the threshold. |
| 15 | +/// |
| 16 | +/// These tests pin the contract the same way <see cref="TryAddProbeCountTests"/> |
| 17 | +/// does: by counting <see cref="IHashProvider{T}.Hash"/> calls. Building a table |
| 18 | +/// of <c>N</c> distinct keys with no resize costs exactly <c>N</c> hash calls |
| 19 | +/// (one probe-chain walk per insert). A resize re-hashes every entry already in |
| 20 | +/// the table, so a resize during construction would push the count above <c>N</c>. |
| 21 | +/// Asserting <c>== N</c> therefore fails on the pre-fix sizing and passes after it. |
| 22 | +/// |
| 23 | +/// <c>SmallDictionary</c> is intentionally excluded: it has no load factor and no |
| 24 | +/// hasher (it linear-scans), so its capacity-verbatim sizing already fills without |
| 25 | +/// resizing and there is nothing to count. |
| 26 | +/// </summary> |
| 27 | +public class BulkConstructorNoResizeTests |
| 28 | +{ |
| 29 | + private const int N = 100; |
| 30 | + |
| 31 | + private static int _hashCallCount; |
| 32 | + |
| 33 | + private struct CountingIntHasher : IHashProvider<int> |
| 34 | + { |
| 35 | + public int Hash(int key) |
| 36 | + { |
| 37 | + _hashCallCount++; |
| 38 | + unchecked |
| 39 | + { |
| 40 | + uint x = (uint)key; |
| 41 | + x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 42 | + x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 43 | + x = (x >> 16) ^ x; |
| 44 | + return (int)x; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private struct CountingLongHasher : IHashProvider<long> |
| 50 | + { |
| 51 | + public int Hash(long key) |
| 52 | + { |
| 53 | + _hashCallCount++; |
| 54 | + unchecked |
| 55 | + { |
| 56 | + ulong x = (ulong)key; |
| 57 | + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9UL; |
| 58 | + x = (x ^ (x >> 27)) * 0x94d049bb133111ebUL; |
| 59 | + x = x ^ (x >> 31); |
| 60 | + return (int)x; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private struct CountingStringHasher : IHashProvider<string> |
| 66 | + { |
| 67 | + public int Hash(string key) |
| 68 | + { |
| 69 | + _hashCallCount++; |
| 70 | + return key.GetHashCode(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static KeyValuePair<int, int>[] IntPairs() => |
| 75 | + Enumerable.Range(1, N).Select(i => new KeyValuePair<int, int>(i, i * 10)).ToArray(); |
| 76 | + |
| 77 | + private static KeyValuePair<long, int>[] LongPairs() => |
| 78 | + Enumerable.Range(1, N).Select(i => new KeyValuePair<long, int>(i, i * 10)).ToArray(); |
| 79 | + |
| 80 | + private static KeyValuePair<string, int>[] StringPairs() => |
| 81 | + Enumerable.Range(1, N).Select(i => new KeyValuePair<string, int>($"k{i}", i)).ToArray(); |
| 82 | + |
| 83 | + // ── Dictionaries ──────────────────────────────────────────────────────────── |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void IntDictionary_BulkConstruct_FromKnownCount_DoesNotResize() |
| 87 | + { |
| 88 | + KeyValuePair<int, int>[] src = IntPairs(); |
| 89 | + _hashCallCount = 0; |
| 90 | + |
| 91 | + var map = new IntDictionary<int, CountingIntHasher>(src); |
| 92 | + |
| 93 | + Assert.Equal(N, _hashCallCount); |
| 94 | + Assert.Equal(N, map.Count); |
| 95 | + for (int i = 1; i <= N; i++) |
| 96 | + Assert.Equal(i * 10, map[i]); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void LongDictionary_BulkConstruct_FromKnownCount_DoesNotResize() |
| 101 | + { |
| 102 | + KeyValuePair<long, int>[] src = LongPairs(); |
| 103 | + _hashCallCount = 0; |
| 104 | + |
| 105 | + var map = new LongDictionary<int, CountingLongHasher>(src); |
| 106 | + |
| 107 | + Assert.Equal(N, _hashCallCount); |
| 108 | + Assert.Equal(N, map.Count); |
| 109 | + for (int i = 1; i <= N; i++) |
| 110 | + Assert.Equal(i * 10, map[i]); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void CelerityDictionary_BulkConstruct_FromKnownCount_DoesNotResize() |
| 115 | + { |
| 116 | + KeyValuePair<string, int>[] src = StringPairs(); |
| 117 | + _hashCallCount = 0; |
| 118 | + |
| 119 | + var map = new CelerityDictionary<string, int, CountingStringHasher>(src); |
| 120 | + |
| 121 | + Assert.Equal(N, _hashCallCount); |
| 122 | + Assert.Equal(N, map.Count); |
| 123 | + for (int i = 1; i <= N; i++) |
| 124 | + Assert.Equal(i, map[$"k{i}"]); |
| 125 | + } |
| 126 | + |
| 127 | + // ── Sets ────────────────────────────────────────────────────────────────────── |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void IntSet_BulkConstruct_FromKnownCount_DoesNotResize() |
| 131 | + { |
| 132 | + int[] src = Enumerable.Range(1, N).ToArray(); |
| 133 | + _hashCallCount = 0; |
| 134 | + |
| 135 | + var set = new IntSet<CountingIntHasher>(src); |
| 136 | + |
| 137 | + Assert.Equal(N, _hashCallCount); |
| 138 | + Assert.Equal(N, set.Count); |
| 139 | + for (int i = 1; i <= N; i++) |
| 140 | + Assert.True(set.Contains(i)); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public void LongSet_BulkConstruct_FromKnownCount_DoesNotResize() |
| 145 | + { |
| 146 | + long[] src = Enumerable.Range(1, N).Select(i => (long)i).ToArray(); |
| 147 | + _hashCallCount = 0; |
| 148 | + |
| 149 | + var set = new LongSet<CountingLongHasher>(src); |
| 150 | + |
| 151 | + Assert.Equal(N, _hashCallCount); |
| 152 | + Assert.Equal(N, set.Count); |
| 153 | + for (int i = 1; i <= N; i++) |
| 154 | + Assert.True(set.Contains(i)); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void CeleritySet_BulkConstruct_FromKnownCount_DoesNotResize() |
| 159 | + { |
| 160 | + string[] src = Enumerable.Range(1, N).Select(i => $"v{i}").ToArray(); |
| 161 | + _hashCallCount = 0; |
| 162 | + |
| 163 | + var set = new CeleritySet<string, CountingStringHasher>(src); |
| 164 | + |
| 165 | + Assert.Equal(N, _hashCallCount); |
| 166 | + Assert.Equal(N, set.Count); |
| 167 | + for (int i = 1; i <= N; i++) |
| 168 | + Assert.True(set.Contains($"v{i}")); |
| 169 | + } |
| 170 | + |
| 171 | + // ── MultiMap ──────────────────────────────────────────────────────────────── |
| 172 | + |
| 173 | + [Fact] |
| 174 | + public void CelerityMultiMap_BulkConstruct_FromKnownDistinctCount_DoesNotResize() |
| 175 | + { |
| 176 | + KeyValuePair<int, int>[] src = IntPairs(); |
| 177 | + _hashCallCount = 0; |
| 178 | + |
| 179 | + var map = new CelerityMultiMap<int, int, CountingIntHasher>(src); |
| 180 | + |
| 181 | + Assert.Equal(N, _hashCallCount); |
| 182 | + Assert.Equal(N, map.Count); |
| 183 | + for (int i = 1; i <= N; i++) |
| 184 | + Assert.Equal(new[] { i * 10 }, map[i].ToArray()); |
| 185 | + } |
| 186 | + |
| 187 | + // ── Load-factor scaling: the headroom must track a non-default load factor ──── |
| 188 | + |
| 189 | + [Theory] |
| 190 | + [InlineData(0.25f)] |
| 191 | + [InlineData(0.5f)] |
| 192 | + [InlineData(0.75f)] |
| 193 | + [InlineData(0.95f)] |
| 194 | + public void IntDictionary_BulkConstruct_HoldsSourceWithoutResize_AcrossLoadFactors(float loadFactor) |
| 195 | + { |
| 196 | + KeyValuePair<int, int>[] src = IntPairs(); |
| 197 | + _hashCallCount = 0; |
| 198 | + |
| 199 | + var map = new IntDictionary<int, CountingIntHasher>(src, loadFactor: loadFactor); |
| 200 | + |
| 201 | + Assert.Equal(N, _hashCallCount); |
| 202 | + Assert.Equal(N, map.Count); |
| 203 | + for (int i = 1; i <= N; i++) |
| 204 | + Assert.Equal(i * 10, map[i]); |
| 205 | + } |
| 206 | + |
| 207 | + // ── Fallback: a non-collection source (unknown count) still builds correctly ── |
| 208 | + // (No resize guarantee is possible without a count; only correctness is pinned.) |
| 209 | + |
| 210 | + [Fact] |
| 211 | + public void IntDictionary_BulkConstruct_FromNonCollectionEnumerable_IsCorrect() |
| 212 | + { |
| 213 | + IEnumerable<KeyValuePair<int, int>> src = |
| 214 | + Enumerable.Range(1, N).Select(i => new KeyValuePair<int, int>(i, i * 10)); |
| 215 | + |
| 216 | + var map = new IntDictionary<int, CountingIntHasher>(src); |
| 217 | + |
| 218 | + Assert.Equal(N, map.Count); |
| 219 | + for (int i = 1; i <= N; i++) |
| 220 | + Assert.Equal(i * 10, map[i]); |
| 221 | + } |
| 222 | +} |
0 commit comments