Skip to content

Commit a04430a

Browse files
Copilotstephentoub
andcommitted
Consolidate UnionWith tests into single parameterized test
Co-authored-by: stephentoub <[email protected]>
1 parent c892504 commit a04430a

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -911,63 +911,40 @@ static void TestComparerSerialization<TCompared>(IEqualityComparer<TCompared> eq
911911
#region UnionWith
912912

913913
[Theory]
914-
[MemberData(nameof(ValidCollectionSizes))]
915-
public void HashSet_Generic_UnionWith_EmptySetWithHashSetSameComparer_CopiesData(int count)
916-
{
917-
HashSet<T> source = (HashSet<T>)GenericISetFactory(count);
918-
HashSet<T> destination = new HashSet<T>(source.Comparer);
919-
920-
destination.UnionWith(source);
921-
922-
Assert.Equal(source.Count, destination.Count);
923-
Assert.True(source.SetEquals(destination));
924-
}
925-
926-
[Theory]
927-
[MemberData(nameof(ValidCollectionSizes))]
928-
public void HashSet_Generic_UnionWith_EmptySetWithSparselyFilledHashSet_CopiesData(int count)
914+
[InlineData(0, true, true)]
915+
[InlineData(0, true, false)]
916+
[InlineData(0, false, true)]
917+
[InlineData(0, false, false)]
918+
[InlineData(1, true, true)]
919+
[InlineData(1, true, false)]
920+
[InlineData(1, false, true)]
921+
[InlineData(1, false, false)]
922+
[InlineData(75, true, true)]
923+
[InlineData(75, true, false)]
924+
[InlineData(75, false, true)]
925+
[InlineData(75, false, false)]
926+
public void HashSet_Generic_UnionWith_HashSet(int count, bool destinationEmpty, bool sourceSparseFilled)
929927
{
930928
HashSet<T> source = (HashSet<T>)CreateEnumerable(EnumerableType.HashSet, null, count, 0, 0);
931-
List<T> sourceElements = source.ToList();
932929

933-
foreach (int i in NonSquares(count))
934-
source.Remove(sourceElements[i]);
935-
936-
HashSet<T> destination = new HashSet<T>(source.Comparer);
937-
938-
destination.UnionWith(source);
930+
if (sourceSparseFilled)
931+
{
932+
List<T> sourceElements = source.ToList();
933+
foreach (int i in NonSquares(count))
934+
source.Remove(sourceElements[i]);
935+
}
939936

940-
Assert.Equal(source.Count, destination.Count);
941-
Assert.True(source.SetEquals(destination));
942-
}
937+
HashSet<T> destination = destinationEmpty
938+
? new HashSet<T>(source.Comparer)
939+
: (HashSet<T>)GenericISetFactory(1);
943940

944-
[Fact]
945-
public void HashSet_Generic_UnionWith_EmptySetWithEmptyHashSet()
946-
{
947-
HashSet<T> source = new HashSet<T>();
948-
HashSet<T> destination = new HashSet<T>();
941+
HashSet<T> expected = new HashSet<T>(destination, source.Comparer);
942+
foreach (T item in source)
943+
expected.Add(item);
949944

950945
destination.UnionWith(source);
951946

952-
Assert.Equal(0, destination.Count);
953-
}
954-
955-
[Theory]
956-
[MemberData(nameof(ValidCollectionSizes))]
957-
public void HashSet_Generic_UnionWith_NonEmptySet_DoesNotCopyButAddsElements(int count)
958-
{
959-
if (count > 0)
960-
{
961-
HashSet<T> source = (HashSet<T>)GenericISetFactory(count);
962-
HashSet<T> destination = (HashSet<T>)GenericISetFactory(1);
963-
T existingItem = destination.First();
964-
965-
destination.UnionWith(source);
966-
967-
Assert.True(destination.Count >= source.Count);
968-
Assert.True(source.IsSubsetOf(destination));
969-
Assert.True(destination.Contains(existingItem));
970-
}
947+
Assert.True(expected.SetEquals(destination));
971948
}
972949

973950
#endregion

0 commit comments

Comments
 (0)