Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d8a0778

Browse files
authored
Add TryAdd and Clear regression tests (#32407)
* Add TryAdd and Clear regression tests * Add Run Condition on Clear() * Address PR Feedback * Address PR Feedback #2 * Address PR Feedback #3 * Remove Extra Line * Add MoveNext Result Asserts
1 parent 61c90ce commit d8a0778

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/System.Collections/tests/Generic/Dictionary/Dictionary.Tests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ public void IDictionary_NonGeneric_Contains_KeyOfWrongType(int count)
126126
}
127127
}
128128

129+
[Fact]
130+
public void Clear_OnEmptyCollection_DoesNotInvalidateEnumerator()
131+
{
132+
if (ModifyEnumeratorAllowed.HasFlag(ModifyOperation.Clear))
133+
{
134+
IDictionary dictionary = new Dictionary<string, string>();
135+
IEnumerator valuesEnum = dictionary.GetEnumerator();
136+
137+
dictionary.Clear();
138+
Assert.Empty(dictionary);
139+
Assert.False(valuesEnum.MoveNext());
140+
}
141+
}
142+
129143
#endregion
130144

131145
#region ICollection tests
@@ -250,6 +264,18 @@ public void Remove_NonExistentEntries_DoesNotPreventEnumeration()
250264
}
251265
}
252266

267+
[Fact]
268+
public void TryAdd_ItemAlreadyExists_DoesNotInvalidateEnumerator()
269+
{
270+
var dictionary = new Dictionary<string, string>();
271+
dictionary.Add("a", "b");
272+
273+
IEnumerator valuesEnum = dictionary.GetEnumerator();
274+
Assert.False(dictionary.TryAdd("a", "c"));
275+
276+
Assert.True(valuesEnum.MoveNext());
277+
}
278+
253279
[Theory]
254280
[MemberData(nameof(CopyConstructorInt32Data))]
255281
public void CopyConstructorInt32(int size, Func<int, int> keyValueSelector, Func<IDictionary<int, int>, IDictionary<int, int>> dictionarySelector)

0 commit comments

Comments
 (0)