|
| 1 | +using Celerity.Collections; |
| 2 | +using Celerity.Hashing; |
| 3 | + |
| 4 | +namespace Celerity.Tests.Collections; |
| 5 | + |
| 6 | +public class ContainsValueTests |
| 7 | +{ |
| 8 | + // ---------------- IntDictionary ---------------- |
| 9 | + |
| 10 | + [Fact] |
| 11 | + public void IntDictionary_EmptyMap_ReturnsFalse() |
| 12 | + { |
| 13 | + var map = new IntDictionary<int>(); |
| 14 | + Assert.False(map.ContainsValue(0)); |
| 15 | + Assert.False(map.ContainsValue(42)); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void IntDictionary_FindsValueInRegularSlot() |
| 20 | + { |
| 21 | + var map = new IntDictionary<int> { [1] = 100, [2] = 200, [3] = 300 }; |
| 22 | + Assert.True(map.ContainsValue(200)); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void IntDictionary_ReturnsFalseForMissingValue() |
| 27 | + { |
| 28 | + var map = new IntDictionary<int> { [1] = 100, [2] = 200 }; |
| 29 | + Assert.False(map.ContainsValue(999)); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void IntDictionary_FindsValueOnlyInZeroKeySlot() |
| 34 | + { |
| 35 | + var map = new IntDictionary<int>(); |
| 36 | + map[0] = 777; |
| 37 | + Assert.True(map.ContainsValue(777)); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void IntDictionary_DefaultValueLookup_ZeroValue() |
| 42 | + { |
| 43 | + // After insert, value 0 must be reachable via ContainsValue. |
| 44 | + var map = new IntDictionary<int> { [5] = 0, [6] = 1 }; |
| 45 | + Assert.True(map.ContainsValue(0)); |
| 46 | + |
| 47 | + // But an empty dictionary must NOT report 0 — the EMPTY_KEY slots |
| 48 | + // are filled with default(TValue) and must be skipped by the scan. |
| 49 | + var empty = new IntDictionary<int>(); |
| 50 | + Assert.False(empty.ContainsValue(0)); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void IntDictionary_DefaultValueLookup_OnlyZeroKeyHasDefaultValue() |
| 55 | + { |
| 56 | + // Same trap as above but with the value sitting only in the |
| 57 | + // out-of-band zero-key slot. |
| 58 | + var map = new IntDictionary<int>(); |
| 59 | + map[0] = 0; |
| 60 | + Assert.True(map.ContainsValue(0)); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void IntDictionary_NullValueLookup_ReferenceType() |
| 65 | + { |
| 66 | + var map = new IntDictionary<string>(); |
| 67 | + map[1] = "one"; |
| 68 | + map[2] = null; |
| 69 | + Assert.True(map.ContainsValue(null)); |
| 70 | + |
| 71 | + var noNulls = new IntDictionary<string> { [1] = "one", [2] = "two" }; |
| 72 | + Assert.False(noNulls.ContainsValue(null)); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void IntDictionary_DuplicateValues_ReturnsTrue() |
| 77 | + { |
| 78 | + var map = new IntDictionary<int> |
| 79 | + { |
| 80 | + [1] = 42, |
| 81 | + [2] = 42, |
| 82 | + [3] = 42, |
| 83 | + }; |
| 84 | + Assert.True(map.ContainsValue(42)); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void IntDictionary_SurvivesResize() |
| 89 | + { |
| 90 | + var map = new IntDictionary<int>(capacity: 4); |
| 91 | + for (int i = 1; i <= 100; i++) |
| 92 | + map[i] = i * 10; |
| 93 | + |
| 94 | + Assert.True(map.ContainsValue(770)); |
| 95 | + Assert.False(map.ContainsValue(-1)); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void IntDictionary_AfterRemove_ReturnsFalse() |
| 100 | + { |
| 101 | + var map = new IntDictionary<int> { [1] = 100, [2] = 200 }; |
| 102 | + map.Remove(1); |
| 103 | + Assert.False(map.ContainsValue(100)); |
| 104 | + Assert.True(map.ContainsValue(200)); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void IntDictionary_AfterZeroKeyRemove_ReturnsFalse() |
| 109 | + { |
| 110 | + var map = new IntDictionary<int>(); |
| 111 | + map[0] = 555; |
| 112 | + map[1] = 100; |
| 113 | + map.Remove(0); |
| 114 | + Assert.False(map.ContainsValue(555)); |
| 115 | + Assert.True(map.ContainsValue(100)); |
| 116 | + } |
| 117 | + |
| 118 | + // ---------------- LongDictionary ---------------- |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void LongDictionary_EmptyMap_ReturnsFalse() |
| 122 | + { |
| 123 | + var map = new LongDictionary<int>(); |
| 124 | + Assert.False(map.ContainsValue(0)); |
| 125 | + Assert.False(map.ContainsValue(42)); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void LongDictionary_FindsValueInRegularSlot() |
| 130 | + { |
| 131 | + var map = new LongDictionary<int> { [1L] = 100, [2L] = 200, [3L] = 300 }; |
| 132 | + Assert.True(map.ContainsValue(200)); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void LongDictionary_ReturnsFalseForMissingValue() |
| 137 | + { |
| 138 | + var map = new LongDictionary<int> { [1L] = 100, [2L] = 200 }; |
| 139 | + Assert.False(map.ContainsValue(999)); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void LongDictionary_FindsValueOnlyInZeroKeySlot() |
| 144 | + { |
| 145 | + var map = new LongDictionary<int>(); |
| 146 | + map[0L] = 777; |
| 147 | + Assert.True(map.ContainsValue(777)); |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public void LongDictionary_DefaultValueLookup_ZeroValue() |
| 152 | + { |
| 153 | + var empty = new LongDictionary<int>(); |
| 154 | + Assert.False(empty.ContainsValue(0)); |
| 155 | + |
| 156 | + var map = new LongDictionary<int> { [5L] = 0, [6L] = 1 }; |
| 157 | + Assert.True(map.ContainsValue(0)); |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public void LongDictionary_NullValueLookup_ReferenceType() |
| 162 | + { |
| 163 | + var map = new LongDictionary<string>(); |
| 164 | + map[1L] = "one"; |
| 165 | + map[2L] = null; |
| 166 | + Assert.True(map.ContainsValue(null)); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void LongDictionary_SurvivesResize() |
| 171 | + { |
| 172 | + var map = new LongDictionary<int>(capacity: 4); |
| 173 | + for (long i = 1; i <= 100; i++) |
| 174 | + map[i] = (int)(i * 10); |
| 175 | + |
| 176 | + Assert.True(map.ContainsValue(770)); |
| 177 | + Assert.False(map.ContainsValue(-1)); |
| 178 | + } |
| 179 | + |
| 180 | + // ---------------- CelerityDictionary ---------------- |
| 181 | + |
| 182 | + [Fact] |
| 183 | + public void CelerityDictionary_EmptyMap_ReturnsFalse() |
| 184 | + { |
| 185 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher>(); |
| 186 | + Assert.False(map.ContainsValue(0)); |
| 187 | + Assert.False(map.ContainsValue(42)); |
| 188 | + } |
| 189 | + |
| 190 | + [Fact] |
| 191 | + public void CelerityDictionary_FindsValueInRegularSlot() |
| 192 | + { |
| 193 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher> { [1] = 100, [2] = 200, [3] = 300 }; |
| 194 | + Assert.True(map.ContainsValue(200)); |
| 195 | + } |
| 196 | + |
| 197 | + [Fact] |
| 198 | + public void CelerityDictionary_ReturnsFalseForMissingValue() |
| 199 | + { |
| 200 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher> { [1] = 100, [2] = 200 }; |
| 201 | + Assert.False(map.ContainsValue(999)); |
| 202 | + } |
| 203 | + |
| 204 | + [Fact] |
| 205 | + public void CelerityDictionary_FindsValueOnlyInDefaultKeySlot_IntKey() |
| 206 | + { |
| 207 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher>(); |
| 208 | + map[0] = 777; |
| 209 | + Assert.True(map.ContainsValue(777)); |
| 210 | + } |
| 211 | + |
| 212 | + [Fact] |
| 213 | + public void CelerityDictionary_FindsValueOnlyInDefaultKeySlot_NullStringKey() |
| 214 | + { |
| 215 | + var map = new CelerityDictionary<string, int, StringFnV1AHasher>(); |
| 216 | + map[null!] = 777; |
| 217 | + Assert.True(map.ContainsValue(777)); |
| 218 | + } |
| 219 | + |
| 220 | + [Fact] |
| 221 | + public void CelerityDictionary_DefaultValueLookup_ZeroValue() |
| 222 | + { |
| 223 | + // EMPTY_KEY slots in the probe array are populated with default(TKey) |
| 224 | + // and default(TValue). ContainsValue must skip those. |
| 225 | + var empty = new CelerityDictionary<int, int, Int32WangNaiveHasher>(); |
| 226 | + Assert.False(empty.ContainsValue(0)); |
| 227 | + |
| 228 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher> { [5] = 0, [6] = 1 }; |
| 229 | + Assert.True(map.ContainsValue(0)); |
| 230 | + } |
| 231 | + |
| 232 | + [Fact] |
| 233 | + public void CelerityDictionary_NullValueLookup_ReferenceType() |
| 234 | + { |
| 235 | + var map = new CelerityDictionary<int, string, Int32WangNaiveHasher>(); |
| 236 | + map[1] = "one"; |
| 237 | + map[2] = null; |
| 238 | + Assert.True(map.ContainsValue(null)); |
| 239 | + |
| 240 | + var noNulls = new CelerityDictionary<int, string, Int32WangNaiveHasher> { [1] = "one", [2] = "two" }; |
| 241 | + Assert.False(noNulls.ContainsValue(null)); |
| 242 | + } |
| 243 | + |
| 244 | + [Fact] |
| 245 | + public void CelerityDictionary_DuplicateValues_ReturnsTrue() |
| 246 | + { |
| 247 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher> |
| 248 | + { |
| 249 | + [1] = 42, |
| 250 | + [2] = 42, |
| 251 | + [3] = 42, |
| 252 | + }; |
| 253 | + Assert.True(map.ContainsValue(42)); |
| 254 | + } |
| 255 | + |
| 256 | + [Fact] |
| 257 | + public void CelerityDictionary_SurvivesResize() |
| 258 | + { |
| 259 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher>(capacity: 4); |
| 260 | + for (int i = 1; i <= 100; i++) |
| 261 | + map[i] = i * 10; |
| 262 | + |
| 263 | + Assert.True(map.ContainsValue(770)); |
| 264 | + Assert.False(map.ContainsValue(-1)); |
| 265 | + } |
| 266 | + |
| 267 | + [Fact] |
| 268 | + public void CelerityDictionary_AfterDefaultKeyRemove_ReturnsFalse() |
| 269 | + { |
| 270 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher>(); |
| 271 | + map[0] = 555; |
| 272 | + map[1] = 100; |
| 273 | + map.Remove(0); |
| 274 | + Assert.False(map.ContainsValue(555)); |
| 275 | + Assert.True(map.ContainsValue(100)); |
| 276 | + } |
| 277 | + |
| 278 | + [Fact] |
| 279 | + public void CelerityDictionary_AfterClear_ReturnsFalse() |
| 280 | + { |
| 281 | + var map = new CelerityDictionary<int, int, Int32WangNaiveHasher> { [1] = 100, [2] = 200 }; |
| 282 | + map[0] = 300; |
| 283 | + map.Clear(); |
| 284 | + Assert.False(map.ContainsValue(100)); |
| 285 | + Assert.False(map.ContainsValue(200)); |
| 286 | + Assert.False(map.ContainsValue(300)); |
| 287 | + } |
| 288 | +} |
0 commit comments