Skip to content

Commit c6d0a8b

Browse files
committed
Add test for generic IEnumerable.GetEnumerator()
1 parent e131505 commit c6d0a8b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

DataStructures.Tests/BagTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Linq;
23
using DataStructures.Bag;
34
using FluentAssertions;
@@ -122,4 +123,31 @@ public void Count_ShouldReturnCorrectCount()
122123
// Act & Assert
123124
bag.Count.Should().Be(3);
124125
}
126+
127+
[Test]
128+
public void IEnumerableGetEnumerator_YieldsAllItemsWithCorrectMultiplicity()
129+
{
130+
// Arrange
131+
var bag = new Bag<string>
132+
{
133+
"apple",
134+
"banana",
135+
"apple"
136+
};
137+
var genericBag = bag as System.Collections.IEnumerable;
138+
139+
// Act
140+
var enumerator = genericBag.GetEnumerator();
141+
var items = new List<object>();
142+
while (enumerator.MoveNext())
143+
{
144+
items.Add(enumerator.Current!);
145+
}
146+
147+
// Assert
148+
items.Count(i => (string)i == "apple").Should().Be(2);
149+
items.Count(i => (string)i == "banana").Should().Be(1);
150+
items.Count.Should().Be(3);
151+
items.Should().BeEquivalentTo(["apple", "apple", "banana"]);
152+
}
125153
}

DataStructures/Bag/Bag.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,5 @@ public IEnumerator<T> GetEnumerator()
102102
/// <summary>
103103
/// Returns an enumerator that iterates through the bag.
104104
/// </summary>
105-
IEnumerator IEnumerable.GetEnumerator()
106-
{
107-
return GetEnumerator();
108-
}
105+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
109106
}

0 commit comments

Comments
 (0)