Skip to content

Commit 16397a0

Browse files
[release/6.0-rc2] Fix behavior ObjectCollection for single item contains (#59557)
* Fix behavior ObjectCollection for single item contains * Address PR feedback Co-authored-by: Juan Sebastian Hoyos Ayala <[email protected]>
1 parent 9b3cad0 commit 16397a0

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/Headers/ObjectCollection.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ public void Clear()
9494
}
9595

9696
public bool Contains(T item) =>
97-
ReferenceEquals(item, _items) ||
98-
(_size != 0 && _items is T[] items && Array.IndexOf(items, item, 0, _size) != -1);
97+
_size <= 0 ? false :
98+
_items is T o ? o.Equals(item) :
99+
_items is T[] items && Array.IndexOf(items, item, 0, _size) != -1;
99100

100101
public void CopyTo(T[] array, int arrayIndex)
101102
{
@@ -120,14 +121,16 @@ public void CopyTo(T[] array, int arrayIndex)
120121

121122
public bool Remove(T item)
122123
{
123-
if (ReferenceEquals(_items, item))
124+
if (_items is T o)
124125
{
125-
_items = null;
126-
_size = 0;
127-
return true;
126+
if (o.Equals(item))
127+
{
128+
_items = null;
129+
_size = 0;
130+
return true;
131+
}
128132
}
129-
130-
if (_items is T[] items)
133+
else if (_items is T[] items)
131134
{
132135
int index = Array.IndexOf(items, item, 0, _size);
133136
if (index != -1)

src/libraries/System.Net.Http/tests/UnitTests/Headers/ObjectCollectionTest.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,74 @@ public void Ctor_ExecuteBothOverloads_MatchExpectation()
3636
c.Add("value1");
3737

3838
Assert.Throws<InvalidOperationException>(() => { c.Add(null); });
39+
}
40+
41+
[Fact]
42+
public void ContainsAndRemove_UsesEqualitySemantics()
43+
{
44+
ObjectCollection<string> c = new ObjectCollection<string>();
3945

46+
string val1 = "value1";
47+
string val1DifferentReference = "value" + 1;
48+
Assert.NotSame(val1, val1DifferentReference);
49+
Assert.Equal(val1, val1DifferentReference);
50+
51+
string val2 = "value2";
52+
string val2DifferentReference = "value" + 2;
53+
Assert.NotSame(val2, val2DifferentReference);
54+
Assert.Equal(val2, val2DifferentReference);
55+
56+
string val3 = "value3";
57+
58+
// Start empty
59+
Assert.Equal(0, c.Count);
60+
Assert.False(c.Contains(val1));
61+
62+
// Single item
63+
c.Add(val1);
4064
Assert.Equal(1, c.Count);
41-
Assert.True(c.Contains("value1"));
65+
Assert.True(c.Contains(val1));
66+
Assert.True(c.Contains(val1DifferentReference));
67+
Assert.False(c.Contains(val2));
68+
69+
// Single item removal
70+
Assert.True(c.Remove(val1));
71+
Assert.Equal(0, c.Count);
72+
Assert.False(c.Contains(val1));
73+
74+
// Multi-value
75+
c.Add(val1);
76+
c.Add(val2);
77+
Assert.Equal(2, c.Count);
78+
Assert.True(c.Contains(val1));
79+
Assert.True(c.Contains(val1DifferentReference));
80+
Assert.True(c.Contains(val2));
81+
Assert.True(c.Contains(val1DifferentReference));
82+
Assert.False(c.Contains(val3));
83+
84+
// Removal when multiple exist, using different reference.
85+
Assert.True(c.Remove(val1));
86+
Assert.False(c.Contains(val1));
87+
Assert.True(c.Contains(val2));
88+
Assert.Equal(1, c.Count);
89+
90+
// Removal of non-existent
91+
Assert.False(c.Remove(val3));
92+
Assert.False(c.Remove(val1DifferentReference));
93+
Assert.Equal(1, c.Count);
94+
Assert.True(c.Contains(val2DifferentReference));
95+
96+
// Removal last item
97+
Assert.True(c.Remove(val2DifferentReference));
98+
Assert.Equal(0, c.Count);
99+
Assert.False(c.Contains(val2));
100+
Assert.False(c.Contains(val1));
101+
102+
// Remove from empty
103+
Assert.False(c.Remove(val1));
104+
Assert.False(c.Remove(val2));
105+
Assert.False(c.Remove(val3));
106+
Assert.Equal(0, c.Count);
42107
}
43108
}
44109
}

0 commit comments

Comments
 (0)