Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Containers.Test/ContiguousMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int> 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<string, int>("Apple", 2), map.Single());
Assert.AreEqual(2, map["apple"]);
}

[TestMethod]
public void Indexer_SetNewKey_AddsKeyValuePair()
{
Expand Down
4 changes: 2 additions & 2 deletions Containers/ContiguousMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading