diff --git a/Containers.Test/ContiguousMapTests.cs b/Containers.Test/ContiguousMapTests.cs index 5a0659b..42f20f2 100644 --- a/Containers.Test/ContiguousMapTests.cs +++ b/Containers.Test/ContiguousMapTests.cs @@ -157,6 +157,22 @@ public void Indexer_Set_UpdatesValue() Assert.AreEqual(1, map.Count); // Count should not change } + [TestMethod] + public void Indexer_SetExistingKeyWithDifferentSpelling_KeepsOriginalKey() + { + // Arrange + ContiguousMap map = new(StringComparer.OrdinalIgnoreCase) { ["Apple"] = 1 }; + + // Act + map["APPLE"] = 2; + + // Assert + Assert.AreSequenceEqual(["Apple"], map.Keys); + Assert.AreEqual("Apple", map.AsSpan()[0].Key); + Assert.AreEqual(new KeyValuePair("Apple", 2), map.Single()); + Assert.AreEqual(2, map["apple"]); + } + [TestMethod] public void Indexer_SetNewKey_AddsKeyValuePair() { diff --git a/Containers/ContiguousMap.cs b/Containers/ContiguousMap.cs index b72142a..299d367 100644 --- a/Containers/ContiguousMap.cs +++ b/Containers/ContiguousMap.cs @@ -174,8 +174,8 @@ public TValue this[TKey key] if (keyToIndex.TryGetValue(key, out int index)) { - // Key exists, update the value - items[index] = new Entry(key, value); + // Key exists, update the value and keep the key that was stored first + items[index] = new Entry(items[index].Key, value); } else {