Merge pull request #23 from mlemanczyk/codex/add-addrange-method-to-recyclabledictionary

AddRange span overload for HashSet
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-07-17 15:51:31 +02:00
commit 1ee464d6b8
4 files changed
+3211

No files matched your search

File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -95,5 +95,229 @@ namespace Recyclable.CollectionsTests
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
_ = actual.Should().ContainInConsecutiveOrder(expected);
}
[Fact]
public void AddRangeShouldAddItemsFromSpan()
{
using var dict = new RecyclableDictionary<int, string>();
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
dict.AddRange(data);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(i + 1);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Fact]
public void AddRangeShouldAddItemsFromDictionary()
{
using var source = new RecyclableDictionary<int, string>();
source.Add(1, "a");
source.Add(2, "b");
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(2);
dict.GetKey(0).Should().Be(1);
dict.GetValue(0).Should().Be("a");
dict.GetKey(1).Should().Be(2);
dict.GetValue(1).Should().Be("b");
}
[Fact]
public void AddRangeShouldAddListItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclableList<KeyValuePair<int, string>>(data);
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Fact]
public void AddRangeShouldAddLongListItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclableLongList<KeyValuePair<int, string>>(data);
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Theory]
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
public void AddRangeShouldAddSortedDictionaryItemsInCorrectOrder(int itemsCount)
{
long[] data = RecyclableLongListTestData.CreateTestData(itemsCount).ToArray();
using var source = new RecyclableSortedDictionary<int, long>();
for (int i = 0; i < data.Length; i++)
{
source.Add(i, data[i]);
}
using var dict = new RecyclableDictionary<int, long>();
dict.AddRange(source);
_ = dict.Count.Should().Be(itemsCount);
for (int i = 0; i < data.Length; i++)
{
dict.GetKey(i).Should().Be(i);
dict.GetValue(i).Should().Be(data[i]);
}
}
[Theory]
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
public void AddRangeShouldAddSortedListItemsInCorrectOrder(int itemsCount)
{
long[] data = RecyclableLongListTestData.CreateTestData(itemsCount).ToArray();
using var source = new RecyclableSortedList<int, long>();
for (int i = 0; i < data.Length; i++)
{
source.Add(i, data[i]);
}
using var dict = new RecyclableDictionary<int, long>();
dict.AddRange(source);
_ = dict.Count.Should().Be(itemsCount);
for (int i = 0; i < data.Length; i++)
{
dict.GetKey(i).Should().Be(i);
dict.GetValue(i).Should().Be(data[i]);
}
}
[Fact]
public void AddRangeShouldAddQueueItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclableQueue<KeyValuePair<int, string>>(data);
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Fact]
public void AddRangeShouldAddStackItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclableStack<KeyValuePair<int, string>>(data);
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Fact]
public void AddRangeShouldAddLinkedListItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclableLinkedList<KeyValuePair<int, string>>(data);
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
[Fact]
public void AddRangeShouldAddPriorityQueueItemsInCorrectOrder()
{
var data = new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b"),
new KeyValuePair<int, string>(3, "c"),
};
using var source = new RecyclablePriorityQueue<KeyValuePair<int, string>>(comparer: Comparer<KeyValuePair<int, string>>.Create((x, y) => x.Key.CompareTo(y.Key)));
foreach (var kvp in data)
{
source.Enqueue(kvp);
}
using var dict = new RecyclableDictionary<int, string>();
dict.AddRange(source);
_ = dict.Count.Should().Be(3);
for (int i = 0; i < 3; i++)
{
dict.GetKey(i).Should().Be(data[i].Key);
dict.GetValue(i).Should().Be(data[i].Value);
}
}
}
}
@@ -76,5 +76,218 @@ namespace Recyclable.CollectionsTests
_ = actual.Should().Contain(expected).And.HaveCount(itemsCount);
}
}
[Fact]
public void AddRangeShouldAddItemsFromSpan()
{
using var set = new RecyclableHashSet<int>();
var data = new[] { 1, 2, 3 };
set.AddRange(data);
_ = set.Count.Should().Be(3);
_ = set.Should().ContainInOrder(data);
}
[Fact]
public void AddRangeShouldIgnoreDuplicates()
{
using var set = new RecyclableHashSet<int>();
var data = new[] { 1, 2, 3 };
set.AddRange(data);
set.AddRange(data);
_ = set.Count.Should().Be(3);
_ = set.Should().ContainInOrder(data);
}
[Fact]
public void AddRangeShouldAddItemsFromList()
{
using var set = new RecyclableHashSet<int>();
var list = new List<int> { 1, 2, 3 };
set.AddRange(list);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromRecyclableList()
{
using var set = new RecyclableHashSet<int>();
using var list = new RecyclableList<int>(new[] { 1, 2, 3 });
set.AddRange(list);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromLongList()
{
using var set = new RecyclableHashSet<int>();
using var list = new RecyclableLongList<int>(new[] { 1, 2, 3 });
set.AddRange(list);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromStack()
{
using var set = new RecyclableHashSet<int>();
using var stack = new RecyclableStack<int>(new[] { 1, 2, 3 });
set.AddRange(stack);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromQueue()
{
using var set = new RecyclableHashSet<int>();
using var queue = new RecyclableQueue<int>(new[] { 1, 2, 3 });
set.AddRange(queue);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromArray()
{
using var set = new RecyclableHashSet<int>();
Array array = new int[] { 1, 2, 3 };
set.AddRange(array);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromICollection()
{
using var set = new RecyclableHashSet<int>();
System.Collections.ArrayList list = new System.Collections.ArrayList { 1, 2, 3 };
set.AddRange(list);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromGenericICollection()
{
using var set = new RecyclableHashSet<int>();
ICollection<int> list = new List<int> { 1, 2, 3 };
set.AddRange(list);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromSortedSet()
{
using var source = new RecyclableSortedSet<int>();
source.Add(1);
source.Add(2);
source.Add(3);
using var set = new RecyclableHashSet<int>();
set.AddRange(source);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromLinkedList()
{
using var source = new RecyclableLinkedList<int>(new[] { 1, 2, 3 });
using var set = new RecyclableHashSet<int>();
set.AddRange(source);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromPriorityQueue()
{
using var source = new RecyclablePriorityQueue<int>();
source.Enqueue(1);
source.Enqueue(2);
source.Enqueue(3);
using var set = new RecyclableHashSet<int>();
set.AddRange(source);
_ = set.Count.Should().Be(3);
_ = set.Should().Contain(new[] { 1, 2, 3 });
}
[Fact]
public void AddRangeShouldAddItemsFromDictionary()
{
using var source = new RecyclableDictionary<int, string>();
source.Add(1, "a");
source.Add(2, "b");
using var set = new RecyclableHashSet<KeyValuePair<int, string>>();
set.AddRange(source);
_ = set.Count.Should().Be(2);
set.Should().Contain(new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b")
});
}
[Fact]
public void AddRangeShouldAddItemsFromSortedDictionary()
{
using var source = new RecyclableSortedDictionary<int, string>();
source.Add(1, "a");
source.Add(2, "b");
using var set = new RecyclableHashSet<KeyValuePair<int, string>>();
set.AddRange(source);
_ = set.Count.Should().Be(2);
set.Should().Contain(new[]
{
new KeyValuePair<int, string>(1, "a"),
new KeyValuePair<int, string>(2, "b")
});
}
[Fact]
public void AddRangeShouldAddItemsFromSortedList()
{
using var source = new RecyclableSortedList<int, string>();
source.Add(1, "a");
source.Add(2, "b");
using var set = new RecyclableHashSet<(int Key, string Value)>();
set.AddRange(source);
_ = set.Count.Should().Be(2);
set.Should().Contain(new[] { (1, "a"), (2, "b") });
}
}
}