Migrated to git.privacyguard.company
This commit is contained in:
1 parent
4b17a4c827
commit
b5a574355e
151 files changed
+38551
-38550
No files matched your search
@@ -1,33 +1,33 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class CustomIList<T> : IList<T>
|
||||
{
|
||||
protected List<T> _list;
|
||||
|
||||
public CustomIList()
|
||||
{
|
||||
_list = new List<T>();
|
||||
}
|
||||
|
||||
public CustomIList(IEnumerable<T> items)
|
||||
{
|
||||
_list = new List<T>(items);
|
||||
}
|
||||
|
||||
public T this[int index] { get => _list[index]; set => _list[index] = value; }
|
||||
public int Count => _list.Count;
|
||||
public bool IsReadOnly => false;
|
||||
public void Add(T item) => _list.Add(item);
|
||||
public void Clear() => _list.Clear();
|
||||
public bool Contains(T item) => _list.Contains(item);
|
||||
public void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(0, array, arrayIndex, _list.Count);
|
||||
public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
|
||||
public int IndexOf(T item) => _list.IndexOf(item);
|
||||
public void Insert(int index, T item) => _list.Insert(index, item);
|
||||
public bool Remove(T item) => _list.Remove(item);
|
||||
public void RemoveAt(int index) => _list.RemoveAt(index);
|
||||
IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
|
||||
}
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class CustomIList<T> : IList<T>
|
||||
{
|
||||
protected List<T> _list;
|
||||
|
||||
public CustomIList()
|
||||
{
|
||||
_list = new List<T>();
|
||||
}
|
||||
|
||||
public CustomIList(IEnumerable<T> items)
|
||||
{
|
||||
_list = new List<T>(items);
|
||||
}
|
||||
|
||||
public T this[int index] { get => _list[index]; set => _list[index] = value; }
|
||||
public int Count => _list.Count;
|
||||
public bool IsReadOnly => false;
|
||||
public void Add(T item) => _list.Add(item);
|
||||
public void Clear() => _list.Clear();
|
||||
public bool Contains(T item) => _list.Contains(item);
|
||||
public void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(0, array, arrayIndex, _list.Count);
|
||||
public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
|
||||
public int IndexOf(T item) => _list.IndexOf(item);
|
||||
public void Insert(int index, T item) => _list.Insert(index, item);
|
||||
public bool Remove(T item) => _list.Remove(item);
|
||||
public void RemoveAt(int index) => _list.RemoveAt(index);
|
||||
IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class EnumerableWithoutCount<T> : IEnumerable<T>
|
||||
{
|
||||
private readonly IEnumerable<T> _testData;
|
||||
|
||||
public EnumerableWithoutCount(IEnumerable<T> testData)
|
||||
{
|
||||
_testData = testData;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => _testData.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => _testData.GetEnumerator();
|
||||
}
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class EnumerableWithoutCount<T> : IEnumerable<T>
|
||||
{
|
||||
private readonly IEnumerable<T> _testData;
|
||||
|
||||
public EnumerableWithoutCount(IEnumerable<T> testData)
|
||||
{
|
||||
_testData = testData;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => _testData.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => _testData.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -1,85 +1,85 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class MathUtilsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(int.MaxValue / 1024, int.MaxValue / 10 / 1024)]
|
||||
public void DivMod(long testNumber, int blockSize)
|
||||
{
|
||||
for (var testCase = testNumber; testCase >= 0; testCase--)
|
||||
{
|
||||
// Prepare
|
||||
var expectedResult = testCase / blockSize;
|
||||
var expectedRemainder = testCase % blockSize;
|
||||
|
||||
// Act
|
||||
var actualResult = MathUtils.DivMod(testCase, blockSize, out long actualRemainder);
|
||||
|
||||
// Validate
|
||||
_ = actualResult.Should().Be(expectedResult);
|
||||
_ = actualRemainder.Should().Be(expectedRemainder);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, false)]
|
||||
[InlineData(1, true)]
|
||||
[InlineData(2, true)]
|
||||
[InlineData(3, false)]
|
||||
[InlineData(4, true)]
|
||||
[InlineData(5, false)]
|
||||
[InlineData(8, true)]
|
||||
[InlineData(16, true)]
|
||||
[InlineData(20, false)]
|
||||
[InlineData(32, true)]
|
||||
[InlineData(64, true)]
|
||||
[InlineData(128, true)]
|
||||
[InlineData(256, true)]
|
||||
[InlineData(512, true)]
|
||||
[InlineData(1024, true)]
|
||||
[InlineData(2048, true)]
|
||||
[InlineData(4096, true)]
|
||||
[InlineData(8192, true)]
|
||||
[InlineData(16384, true)]
|
||||
[InlineData(32768, true)]
|
||||
[InlineData(65536, true)]
|
||||
[InlineData(131072, true)]
|
||||
[InlineData(262144, true)]
|
||||
[InlineData(524288, true)]
|
||||
[InlineData(1048576, true)]
|
||||
[InlineData(2097152, true)]
|
||||
[InlineData(4194304, true)]
|
||||
[InlineData(8388608, true)]
|
||||
[InlineData(16777216, true)]
|
||||
[InlineData(33554432, true)]
|
||||
[InlineData(67108864, true)]
|
||||
[InlineData(134217728, true)]
|
||||
[InlineData(268435456, true)]
|
||||
[InlineData(536870912, true)]
|
||||
[InlineData(1073741824, true)]
|
||||
[InlineData(2147483648, true)]
|
||||
[InlineData(2147483649, false)]
|
||||
[InlineData(4294967296, true)]
|
||||
public void IsPow2(long testNumber, bool expected)
|
||||
{
|
||||
_ = MathUtils.IsPow2(testNumber).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2, -1)]
|
||||
[InlineData(-1, -1)]
|
||||
[InlineData(0, -1)]
|
||||
[InlineData(1, 0)]
|
||||
[InlineData(2, 1)]
|
||||
[InlineData(3, -1)]
|
||||
[InlineData(4, 2)]
|
||||
[InlineData(8, 3)]
|
||||
public void GetPow2Shift(long testNumber, int expected)
|
||||
{
|
||||
_ = MathUtils.GetPow2Shift(testNumber).Should().Be(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class MathUtilsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(int.MaxValue / 1024, int.MaxValue / 10 / 1024)]
|
||||
public void DivMod(long testNumber, int blockSize)
|
||||
{
|
||||
for (var testCase = testNumber; testCase >= 0; testCase--)
|
||||
{
|
||||
// Prepare
|
||||
var expectedResult = testCase / blockSize;
|
||||
var expectedRemainder = testCase % blockSize;
|
||||
|
||||
// Act
|
||||
var actualResult = MathUtils.DivMod(testCase, blockSize, out long actualRemainder);
|
||||
|
||||
// Validate
|
||||
_ = actualResult.Should().Be(expectedResult);
|
||||
_ = actualRemainder.Should().Be(expectedRemainder);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, false)]
|
||||
[InlineData(1, true)]
|
||||
[InlineData(2, true)]
|
||||
[InlineData(3, false)]
|
||||
[InlineData(4, true)]
|
||||
[InlineData(5, false)]
|
||||
[InlineData(8, true)]
|
||||
[InlineData(16, true)]
|
||||
[InlineData(20, false)]
|
||||
[InlineData(32, true)]
|
||||
[InlineData(64, true)]
|
||||
[InlineData(128, true)]
|
||||
[InlineData(256, true)]
|
||||
[InlineData(512, true)]
|
||||
[InlineData(1024, true)]
|
||||
[InlineData(2048, true)]
|
||||
[InlineData(4096, true)]
|
||||
[InlineData(8192, true)]
|
||||
[InlineData(16384, true)]
|
||||
[InlineData(32768, true)]
|
||||
[InlineData(65536, true)]
|
||||
[InlineData(131072, true)]
|
||||
[InlineData(262144, true)]
|
||||
[InlineData(524288, true)]
|
||||
[InlineData(1048576, true)]
|
||||
[InlineData(2097152, true)]
|
||||
[InlineData(4194304, true)]
|
||||
[InlineData(8388608, true)]
|
||||
[InlineData(16777216, true)]
|
||||
[InlineData(33554432, true)]
|
||||
[InlineData(67108864, true)]
|
||||
[InlineData(134217728, true)]
|
||||
[InlineData(268435456, true)]
|
||||
[InlineData(536870912, true)]
|
||||
[InlineData(1073741824, true)]
|
||||
[InlineData(2147483648, true)]
|
||||
[InlineData(2147483649, false)]
|
||||
[InlineData(4294967296, true)]
|
||||
public void IsPow2(long testNumber, bool expected)
|
||||
{
|
||||
_ = MathUtils.IsPow2(testNumber).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2, -1)]
|
||||
[InlineData(-1, -1)]
|
||||
[InlineData(0, -1)]
|
||||
[InlineData(1, 0)]
|
||||
[InlineData(2, 1)]
|
||||
[InlineData(3, -1)]
|
||||
[InlineData(4, 2)]
|
||||
[InlineData(8, 3)]
|
||||
public void GetPow2Shift(long testNumber, int expected)
|
||||
{
|
||||
_ = MathUtils.GetPow2Shift(testNumber).Should().Be(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,41 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
|
||||
<PackageReference Include="morelinq" Version="3.4.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Open.Numeric.Primes" Version="3.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Recyclable.Collections\Recyclable.Collections.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
|
||||
<PackageReference Include="morelinq" Version="3.4.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Open.Numeric.Primes" Version="3.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Recyclable.Collections\Recyclable.Collections.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,323 +1,323 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableDictionaryTests
|
||||
{
|
||||
private static readonly (int, string)[] _testData = new[] { (1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e") };
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreItems(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, -key);
|
||||
}
|
||||
|
||||
_ = dict.Should().HaveCount(itemsCount);
|
||||
for (var i = 0; i < itemsCount; i++)
|
||||
{
|
||||
long expectedKey = i + 1;
|
||||
dict.GetKey(i).Should().Be(expectedKey);
|
||||
dict.GetValue(i).Should().Be(-expectedKey);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var dict = new RecyclableDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
dict[1] = "b";
|
||||
|
||||
_ = dict[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldSwapWithLast(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, key);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long keyToRemove = (itemsCount + 1) / 2;
|
||||
dict.Remove(keyToRemove).Should().BeTrue();
|
||||
_ = dict.ContainsKey(keyToRemove).Should().BeFalse();
|
||||
_ = dict.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.Remove(0).Should().BeFalse();
|
||||
_ = dict.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var dict = new RecyclableDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
var found = dict.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItems(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, key);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = dict.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current.Key);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableDictionaryTests
|
||||
{
|
||||
private static readonly (int, string)[] _testData = new[] { (1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e") };
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreItems(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, -key);
|
||||
}
|
||||
|
||||
_ = dict.Should().HaveCount(itemsCount);
|
||||
for (var i = 0; i < itemsCount; i++)
|
||||
{
|
||||
long expectedKey = i + 1;
|
||||
dict.GetKey(i).Should().Be(expectedKey);
|
||||
dict.GetValue(i).Should().Be(-expectedKey);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var dict = new RecyclableDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
dict[1] = "b";
|
||||
|
||||
_ = dict[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldSwapWithLast(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, key);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long keyToRemove = (itemsCount + 1) / 2;
|
||||
dict.Remove(keyToRemove).Should().BeTrue();
|
||||
_ = dict.ContainsKey(keyToRemove).Should().BeFalse();
|
||||
_ = dict.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.Remove(0).Should().BeFalse();
|
||||
_ = dict.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var dict = new RecyclableDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
var found = dict.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItems(int itemsCount)
|
||||
{
|
||||
using var dict = new RecyclableDictionary<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
dict.Add(key, key);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = dict.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current.Key);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,293 +1,293 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableHashSetTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreUniqueItems(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item).Should().BeTrue();
|
||||
}
|
||||
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item).Should().BeFalse();
|
||||
}
|
||||
|
||||
_ = set.Should().HaveCount(itemsCount);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldDeleteItem(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long value = (itemsCount + 1) / 2;
|
||||
set.Remove(value).Should().BeTrue();
|
||||
_ = set.Contains(value).Should().BeFalse();
|
||||
_ = set.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
set.Remove(0).Should().BeFalse();
|
||||
_ = set.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItems(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = set.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current);
|
||||
}
|
||||
|
||||
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
|
||||
if (itemsCount == 0)
|
||||
{
|
||||
_ = actual.Should().BeEmpty();
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = 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") });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableHashSetTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreUniqueItems(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item).Should().BeTrue();
|
||||
}
|
||||
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item).Should().BeFalse();
|
||||
}
|
||||
|
||||
_ = set.Should().HaveCount(itemsCount);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldDeleteItem(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long value = (itemsCount + 1) / 2;
|
||||
set.Remove(value).Should().BeTrue();
|
||||
_ = set.Contains(value).Should().BeFalse();
|
||||
_ = set.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
set.Remove(0).Should().BeFalse();
|
||||
_ = set.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItems(int itemsCount)
|
||||
{
|
||||
using var set = new RecyclableHashSet<long>();
|
||||
foreach (var item in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
set.Add(item);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = set.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current);
|
||||
}
|
||||
|
||||
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
|
||||
if (itemsCount == 0)
|
||||
{
|
||||
_ = actual.Should().BeEmpty();
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = 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") });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,282 +1,282 @@
|
||||
using MoreLinq;
|
||||
using Open.Numeric.Primes;
|
||||
using Recyclable.Collections;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public static class RecyclableLongListTestData
|
||||
{
|
||||
|
||||
private static readonly object[] _sourceRefData = Enumerable.Range(1, 100).Cast<object>().ToArray();
|
||||
public static object[] SourceRefData => _sourceRefData;
|
||||
|
||||
public static IEnumerable<long> CreateTestData(long itemsCount)
|
||||
{
|
||||
for (long index = 1; index <= itemsCount; index++)
|
||||
{
|
||||
yield return index;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object> CreateRefTestData(long itemsCount)
|
||||
{
|
||||
for (long index = 1; index <= itemsCount; index++)
|
||||
{
|
||||
yield return index;
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly IEnumerable<long> ItemsCountVariants = new long[]
|
||||
{
|
||||
//RecyclableDefaults.MinPooledArrayLength - 5, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 5, 127, 128, 129, RecyclableDefaults.MinItemsCountForParallelization
|
||||
0, 1, 2, 3, 7, 10, 16, RecyclableDefaults.MinPooledArrayLength - 5, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 5, RecyclableDefaults.MinPooledArrayLength * 100
|
||||
};
|
||||
|
||||
public static readonly IEnumerable<object[]> ItemsCountTestCases = ItemsCountVariants.Select(x => new object[] { (int)x });
|
||||
|
||||
public static readonly IEnumerable<int> BlockSizeVariants = new int[]
|
||||
{
|
||||
1, 2, 4, 16, RecyclableDefaults.MinPooledArrayLength - 5, (int)BitOperations.RoundUpToPowerOf2(RecyclableDefaults.MinPooledArrayLength)
|
||||
};
|
||||
|
||||
private static bool IsValidBlockSize(long itemsCount, long blockSize) => itemsCount is < 1024 || blockSize is > 8;
|
||||
|
||||
private static IEnumerable<object[]> GetTargetDataVariants()
|
||||
{
|
||||
foreach (var itemsCount in ItemsCountVariants)
|
||||
{
|
||||
foreach (var targetBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, targetBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[] { itemsCount, targetBlockSize };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> TargetDataVariants => GetTargetDataVariants();
|
||||
|
||||
private static IEnumerable<object[]> CombineSourceDataWithBlockSize(IEnumerable<object[]> sourceData)
|
||||
{
|
||||
foreach (var testData in sourceData)
|
||||
{
|
||||
foreach (var targetBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize((long)testData[2], targetBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[] { testData[0], testData[1], testData[2], targetBlockSize };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<object[]> CreateSourceDataVariants(long itemsCount, bool refType)
|
||||
{
|
||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
|
||||
|
||||
switch (refType)
|
||||
{
|
||||
case false:
|
||||
long[] testData = CreateTestData(itemsCount).ToArray();
|
||||
|
||||
yield return new object[] { $"Array[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"ICollection[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"ICollection<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable<T>[{itemsCount}] without non-enumerated count", new EnumerableWithoutCount<long>(testData), itemsCount };
|
||||
yield return new object[] { $"IList<T>[{itemsCount}]", new CustomIList<long>(testData), itemsCount };
|
||||
yield return new object[] { $"IReadOnlyList<T>[{itemsCount}])", testData, itemsCount };
|
||||
yield return new object[] { $"List<T>[{itemsCount}]", testData.ToList(), itemsCount };
|
||||
yield return new object[] { $"ReadOnlySpan<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"RecyclableList<T>[{itemsCount}]", testData.ToRecyclableList(), itemsCount };
|
||||
|
||||
foreach (var sourceBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, sourceBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
$"RecyclableLongList<T>[itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize}]",
|
||||
testData.ToRecyclableLongList(sourceBlockSize),
|
||||
itemsCount
|
||||
};
|
||||
}
|
||||
|
||||
yield return new object[] { $"Span<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"T[{itemsCount}]", testData, itemsCount };
|
||||
break;
|
||||
|
||||
case true:
|
||||
IEnumerable<object> testRefData = CreateRefTestData(itemsCount).ToArray();
|
||||
|
||||
yield return new object[] { $"Array[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"ICollection(itemsCount: {itemsCount}", testRefData, itemsCount };
|
||||
yield return new object[] { $"ICollection<T>(itemsCount: {itemsCount}", testRefData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable(itemsCount: {itemsCount})", testRefData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable<T>(itemsCount: {itemsCount}) without non-enumerated count", new EnumerableWithoutCount<object>(testRefData), itemsCount };
|
||||
yield return new object[] { $"IList<T>(itemsCount: {itemsCount})", new CustomIList<object>(testRefData), itemsCount };
|
||||
yield return new object[] { $"IReadOnlyList<T>(itemsCount: {itemsCount})", testRefData, itemsCount };
|
||||
yield return new object[] { $"List<T>(itemsCount: {itemsCount}", testRefData.ToList(), itemsCount };
|
||||
yield return new object[] { $"ReadOnlySpan<T>[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"RecyclableList<T>(itemsCount: {itemsCount})", testRefData.ToRecyclableList(), itemsCount };
|
||||
|
||||
foreach (var sourceBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, sourceBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
$"RecyclableLongList<T>(itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize})",
|
||||
testRefData.ToRecyclableLongList(sourceBlockSize),
|
||||
itemsCount
|
||||
};
|
||||
}
|
||||
|
||||
yield return new object[] { $"Span<T>[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"T[{itemsCount}]", testRefData, itemsCount };
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<object[]> CreateSourceDataVariants(bool refType)
|
||||
{
|
||||
foreach (var itemsCount in ItemsCountVariants)
|
||||
{
|
||||
foreach (var variant in CreateSourceDataVariants(itemsCount, refType))
|
||||
{
|
||||
yield return variant;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> EmptySourceDataVariants => CreateSourceDataVariants(0, false);
|
||||
public static IEnumerable<object[]> SourceDataVariants => CreateSourceDataVariants(false);
|
||||
public static IEnumerable<object[]> SourceDataWithItemIndexVariants
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (var testCase in CreateSourceDataVariants(false).Where(testCase => (long)testCase[2] > 0))
|
||||
{
|
||||
var itemIndexes = CreateItemIndexVariants((long)testCase[2], RecyclableDefaults.BlockSize).ToArray();
|
||||
yield return new object[] { testCase[0], testCase[1], testCase[2], itemIndexes };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceRefDataWithItemIndexVariants
|
||||
{
|
||||
get
|
||||
{
|
||||
IEnumerable<object[]> testCases = CreateSourceDataVariants(true).Where(testCase => (long)testCase[2] > 0);
|
||||
foreach (var testCase in testCases)
|
||||
{
|
||||
var itemIndexes = CreateItemIndexVariants((long)testCase[2], RecyclableDefaults.BlockSize).ToArray();
|
||||
|
||||
yield return new object[] { testCase[0], testCase[1], testCase[2], itemIndexes };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceDataWithBlockSizeVariants => CombineSourceDataWithBlockSize(SourceDataVariants);
|
||||
public static IEnumerable<object[]> SourceRefDataVariants => CreateSourceDataVariants(true);
|
||||
public static IEnumerable<object[]> SourceRefDataWithBlockSizeVariants { get; } = CombineSourceDataWithBlockSize(SourceRefDataVariants);
|
||||
|
||||
private static IEnumerable<long> _initialItemIndexVariants => new long[] { 0, 1, 2, 3, 4, 5 };
|
||||
private static IEnumerable<long> CreateItemIndexVariants(long itemsCount, int blockSize)
|
||||
{
|
||||
IEnumerable<long> primes = _initialItemIndexVariants.Concat(
|
||||
_initialItemIndexVariants.SelectMany(index => new long[] { blockSize - index, blockSize + index })
|
||||
//new long[] { blockSize - 3, blockSize - 2, blockSize - 1, blockSize, blockSize + 1, blockSize + 2, blockSize + 3 }
|
||||
).ToArray();
|
||||
|
||||
IEnumerable<long> second = Prime.Numbers.StartingAt(7)
|
||||
.TakeUntil(prime =>
|
||||
prime > ((blockSize * 2) - 1) ||
|
||||
prime >= itemsCount
|
||||
);
|
||||
|
||||
primes = primes.Concat(second).ToArray();
|
||||
|
||||
primes = primes.Concat(
|
||||
primes.Select(index => itemsCount - index - 1)
|
||||
)
|
||||
.Where(index => itemsCount > 0 && index >= 0 && index < itemsCount).ToArray()
|
||||
.Distinct().ToArray();
|
||||
|
||||
return primes;
|
||||
|
||||
//IEnumerable<long> CreateItemIndexVariants()
|
||||
//{
|
||||
//if (itemsCount > 0)
|
||||
//{
|
||||
// yield return 0;
|
||||
//}
|
||||
|
||||
//foreach (long prime in primes)
|
||||
//{
|
||||
// yield return prime;
|
||||
|
||||
// long index = itemsCount - prime;
|
||||
// if (index >= 0)
|
||||
// {
|
||||
// yield return index;
|
||||
// }
|
||||
//}
|
||||
|
||||
//for (long index = itemsCount - (blockSize * 2) - 1; index < itemsCount; index++)
|
||||
//{
|
||||
// if (index >= 0)
|
||||
// {
|
||||
// yield return index;
|
||||
// }
|
||||
//}
|
||||
|
||||
//if ((blockSize / 16 * 16) + 1 < itemsCount)
|
||||
//{
|
||||
// yield return (blockSize / 16 * 16) + 1;
|
||||
//}
|
||||
//}
|
||||
|
||||
//return CreateItemIndexVariants();
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> CombineSourceRefDataWithBlockSizeWithItemIndex(bool refType)
|
||||
{
|
||||
IEnumerable<object[]> sourceData = refType switch
|
||||
{
|
||||
false => SourceDataWithBlockSizeVariants,
|
||||
true => SourceRefDataWithBlockSizeVariants
|
||||
};
|
||||
|
||||
foreach (var testCase in sourceData)
|
||||
{
|
||||
long itemsCount = (long)testCase[2];
|
||||
if (itemsCount == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
long[] itemIndexes = CreateItemIndexVariants(itemsCount, (int)testCase[3])
|
||||
.Where(_ => IsValidBlockSize((long)testCase[2], (int)testCase[3]))
|
||||
.ToArray();
|
||||
|
||||
yield return new object[] { testCase[0], testCase[1], (long)testCase[2], (int)testCase[3], itemIndexes };
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceDataWithBlockSizeWithItemIndexVariants { get; } = CombineSourceRefDataWithBlockSizeWithItemIndex(false);
|
||||
public static IEnumerable<object[]> SourceRefDataWithBlockSizeWithItemIndexVariants { get; } = CombineSourceRefDataWithBlockSizeWithItemIndex(true);
|
||||
}
|
||||
using MoreLinq;
|
||||
using Open.Numeric.Primes;
|
||||
using Recyclable.Collections;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public static class RecyclableLongListTestData
|
||||
{
|
||||
|
||||
private static readonly object[] _sourceRefData = Enumerable.Range(1, 100).Cast<object>().ToArray();
|
||||
public static object[] SourceRefData => _sourceRefData;
|
||||
|
||||
public static IEnumerable<long> CreateTestData(long itemsCount)
|
||||
{
|
||||
for (long index = 1; index <= itemsCount; index++)
|
||||
{
|
||||
yield return index;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object> CreateRefTestData(long itemsCount)
|
||||
{
|
||||
for (long index = 1; index <= itemsCount; index++)
|
||||
{
|
||||
yield return index;
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly IEnumerable<long> ItemsCountVariants = new long[]
|
||||
{
|
||||
//RecyclableDefaults.MinPooledArrayLength - 5, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 5, 127, 128, 129, RecyclableDefaults.MinItemsCountForParallelization
|
||||
0, 1, 2, 3, 7, 10, 16, RecyclableDefaults.MinPooledArrayLength - 5, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 5, RecyclableDefaults.MinPooledArrayLength * 100
|
||||
};
|
||||
|
||||
public static readonly IEnumerable<object[]> ItemsCountTestCases = ItemsCountVariants.Select(x => new object[] { (int)x });
|
||||
|
||||
public static readonly IEnumerable<int> BlockSizeVariants = new int[]
|
||||
{
|
||||
1, 2, 4, 16, RecyclableDefaults.MinPooledArrayLength - 5, (int)BitOperations.RoundUpToPowerOf2(RecyclableDefaults.MinPooledArrayLength)
|
||||
};
|
||||
|
||||
private static bool IsValidBlockSize(long itemsCount, long blockSize) => itemsCount is < 1024 || blockSize is > 8;
|
||||
|
||||
private static IEnumerable<object[]> GetTargetDataVariants()
|
||||
{
|
||||
foreach (var itemsCount in ItemsCountVariants)
|
||||
{
|
||||
foreach (var targetBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, targetBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[] { itemsCount, targetBlockSize };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> TargetDataVariants => GetTargetDataVariants();
|
||||
|
||||
private static IEnumerable<object[]> CombineSourceDataWithBlockSize(IEnumerable<object[]> sourceData)
|
||||
{
|
||||
foreach (var testData in sourceData)
|
||||
{
|
||||
foreach (var targetBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize((long)testData[2], targetBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[] { testData[0], testData[1], testData[2], targetBlockSize };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<object[]> CreateSourceDataVariants(long itemsCount, bool refType)
|
||||
{
|
||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
|
||||
|
||||
switch (refType)
|
||||
{
|
||||
case false:
|
||||
long[] testData = CreateTestData(itemsCount).ToArray();
|
||||
|
||||
yield return new object[] { $"Array[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"ICollection[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"ICollection<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable<T>[{itemsCount}] without non-enumerated count", new EnumerableWithoutCount<long>(testData), itemsCount };
|
||||
yield return new object[] { $"IList<T>[{itemsCount}]", new CustomIList<long>(testData), itemsCount };
|
||||
yield return new object[] { $"IReadOnlyList<T>[{itemsCount}])", testData, itemsCount };
|
||||
yield return new object[] { $"List<T>[{itemsCount}]", testData.ToList(), itemsCount };
|
||||
yield return new object[] { $"ReadOnlySpan<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"RecyclableList<T>[{itemsCount}]", testData.ToRecyclableList(), itemsCount };
|
||||
|
||||
foreach (var sourceBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, sourceBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
$"RecyclableLongList<T>[itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize}]",
|
||||
testData.ToRecyclableLongList(sourceBlockSize),
|
||||
itemsCount
|
||||
};
|
||||
}
|
||||
|
||||
yield return new object[] { $"Span<T>[{itemsCount}]", testData, itemsCount };
|
||||
yield return new object[] { $"T[{itemsCount}]", testData, itemsCount };
|
||||
break;
|
||||
|
||||
case true:
|
||||
IEnumerable<object> testRefData = CreateRefTestData(itemsCount).ToArray();
|
||||
|
||||
yield return new object[] { $"Array[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"ICollection(itemsCount: {itemsCount}", testRefData, itemsCount };
|
||||
yield return new object[] { $"ICollection<T>(itemsCount: {itemsCount}", testRefData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable(itemsCount: {itemsCount})", testRefData, itemsCount };
|
||||
yield return new object[] { $"IEnumerable<T>(itemsCount: {itemsCount}) without non-enumerated count", new EnumerableWithoutCount<object>(testRefData), itemsCount };
|
||||
yield return new object[] { $"IList<T>(itemsCount: {itemsCount})", new CustomIList<object>(testRefData), itemsCount };
|
||||
yield return new object[] { $"IReadOnlyList<T>(itemsCount: {itemsCount})", testRefData, itemsCount };
|
||||
yield return new object[] { $"List<T>(itemsCount: {itemsCount}", testRefData.ToList(), itemsCount };
|
||||
yield return new object[] { $"ReadOnlySpan<T>[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"RecyclableList<T>(itemsCount: {itemsCount})", testRefData.ToRecyclableList(), itemsCount };
|
||||
|
||||
foreach (var sourceBlockSize in BlockSizeVariants)
|
||||
{
|
||||
if (!IsValidBlockSize(itemsCount, sourceBlockSize))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
$"RecyclableLongList<T>(itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize})",
|
||||
testRefData.ToRecyclableLongList(sourceBlockSize),
|
||||
itemsCount
|
||||
};
|
||||
}
|
||||
|
||||
yield return new object[] { $"Span<T>[{itemsCount}]", testRefData, itemsCount };
|
||||
yield return new object[] { $"T[{itemsCount}]", testRefData, itemsCount };
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<object[]> CreateSourceDataVariants(bool refType)
|
||||
{
|
||||
foreach (var itemsCount in ItemsCountVariants)
|
||||
{
|
||||
foreach (var variant in CreateSourceDataVariants(itemsCount, refType))
|
||||
{
|
||||
yield return variant;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> EmptySourceDataVariants => CreateSourceDataVariants(0, false);
|
||||
public static IEnumerable<object[]> SourceDataVariants => CreateSourceDataVariants(false);
|
||||
public static IEnumerable<object[]> SourceDataWithItemIndexVariants
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (var testCase in CreateSourceDataVariants(false).Where(testCase => (long)testCase[2] > 0))
|
||||
{
|
||||
var itemIndexes = CreateItemIndexVariants((long)testCase[2], RecyclableDefaults.BlockSize).ToArray();
|
||||
yield return new object[] { testCase[0], testCase[1], testCase[2], itemIndexes };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceRefDataWithItemIndexVariants
|
||||
{
|
||||
get
|
||||
{
|
||||
IEnumerable<object[]> testCases = CreateSourceDataVariants(true).Where(testCase => (long)testCase[2] > 0);
|
||||
foreach (var testCase in testCases)
|
||||
{
|
||||
var itemIndexes = CreateItemIndexVariants((long)testCase[2], RecyclableDefaults.BlockSize).ToArray();
|
||||
|
||||
yield return new object[] { testCase[0], testCase[1], testCase[2], itemIndexes };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceDataWithBlockSizeVariants => CombineSourceDataWithBlockSize(SourceDataVariants);
|
||||
public static IEnumerable<object[]> SourceRefDataVariants => CreateSourceDataVariants(true);
|
||||
public static IEnumerable<object[]> SourceRefDataWithBlockSizeVariants { get; } = CombineSourceDataWithBlockSize(SourceRefDataVariants);
|
||||
|
||||
private static IEnumerable<long> _initialItemIndexVariants => new long[] { 0, 1, 2, 3, 4, 5 };
|
||||
private static IEnumerable<long> CreateItemIndexVariants(long itemsCount, int blockSize)
|
||||
{
|
||||
IEnumerable<long> primes = _initialItemIndexVariants.Concat(
|
||||
_initialItemIndexVariants.SelectMany(index => new long[] { blockSize - index, blockSize + index })
|
||||
//new long[] { blockSize - 3, blockSize - 2, blockSize - 1, blockSize, blockSize + 1, blockSize + 2, blockSize + 3 }
|
||||
).ToArray();
|
||||
|
||||
IEnumerable<long> second = Prime.Numbers.StartingAt(7)
|
||||
.TakeUntil(prime =>
|
||||
prime > ((blockSize * 2) - 1) ||
|
||||
prime >= itemsCount
|
||||
);
|
||||
|
||||
primes = primes.Concat(second).ToArray();
|
||||
|
||||
primes = primes.Concat(
|
||||
primes.Select(index => itemsCount - index - 1)
|
||||
)
|
||||
.Where(index => itemsCount > 0 && index >= 0 && index < itemsCount).ToArray()
|
||||
.Distinct().ToArray();
|
||||
|
||||
return primes;
|
||||
|
||||
//IEnumerable<long> CreateItemIndexVariants()
|
||||
//{
|
||||
//if (itemsCount > 0)
|
||||
//{
|
||||
// yield return 0;
|
||||
//}
|
||||
|
||||
//foreach (long prime in primes)
|
||||
//{
|
||||
// yield return prime;
|
||||
|
||||
// long index = itemsCount - prime;
|
||||
// if (index >= 0)
|
||||
// {
|
||||
// yield return index;
|
||||
// }
|
||||
//}
|
||||
|
||||
//for (long index = itemsCount - (blockSize * 2) - 1; index < itemsCount; index++)
|
||||
//{
|
||||
// if (index >= 0)
|
||||
// {
|
||||
// yield return index;
|
||||
// }
|
||||
//}
|
||||
|
||||
//if ((blockSize / 16 * 16) + 1 < itemsCount)
|
||||
//{
|
||||
// yield return (blockSize / 16 * 16) + 1;
|
||||
//}
|
||||
//}
|
||||
|
||||
//return CreateItemIndexVariants();
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> CombineSourceRefDataWithBlockSizeWithItemIndex(bool refType)
|
||||
{
|
||||
IEnumerable<object[]> sourceData = refType switch
|
||||
{
|
||||
false => SourceDataWithBlockSizeVariants,
|
||||
true => SourceRefDataWithBlockSizeVariants
|
||||
};
|
||||
|
||||
foreach (var testCase in sourceData)
|
||||
{
|
||||
long itemsCount = (long)testCase[2];
|
||||
if (itemsCount == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
long[] itemIndexes = CreateItemIndexVariants(itemsCount, (int)testCase[3])
|
||||
.Where(_ => IsValidBlockSize((long)testCase[2], (int)testCase[3]))
|
||||
.ToArray();
|
||||
|
||||
yield return new object[] { testCase[0], testCase[1], (long)testCase[2], (int)testCase[3], itemIndexes };
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> SourceDataWithBlockSizeWithItemIndexVariants { get; } = CombineSourceRefDataWithBlockSizeWithItemIndex(false);
|
||||
public static IEnumerable<object[]> SourceRefDataWithBlockSizeWithItemIndexVariants { get; } = CombineSourceRefDataWithBlockSizeWithItemIndex(true);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,495 +1,495 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclablePriorityQueueTests
|
||||
{
|
||||
private static readonly int[] _testData = new[] { 4, 1, 3, 2, 5 };
|
||||
|
||||
[Fact]
|
||||
public void DequeueShouldReturnItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
foreach (var item in _testData)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().BeInAscendingOrder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyWhenNotInitialized()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
_ = queue.Should().BeEmpty();
|
||||
_ = queue.LongCount.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
foreach (var item in _testData)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
|
||||
queue.Clear();
|
||||
_ = queue.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DequeueShouldThrowWhenNoElements()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
_ = Assert.Throws<ArgumentOutOfRangeException>(() => queue.Dequeue());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldDoNothingWhenEmpty()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(ReadOnlySpan<int>.Empty);
|
||||
_ = queue.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeArrayShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeListShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(new List<int>(_testData));
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyMemoryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlyMemory<int>)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeMemoryShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData.AsMemory());
|
||||
queue.AddRange(_testData.AsMemory());
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeArrayShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData);
|
||||
queue.AddRange(_testData);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeShouldAcceptNulls()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<long?>();
|
||||
queue.AddRange(new long?[] { null, 1 });
|
||||
_ = queue.Should().HaveCount(2).And.Contain((long?)null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeQueueShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableQueue<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeQueueShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableQueue<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeStackShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableStack<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedSetShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedSet<int>();
|
||||
foreach (int item in _testData)
|
||||
{
|
||||
source.Add(item);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeDictionaryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeDictionaryShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v))
|
||||
.Concat(_testData.Select((v, i) => new KeyValuePair<int, int>(i, v)))
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedListShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedList<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<(int Key, int Value)>(comparer: Comparer<(int Key, int Value)>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<(int Key, int Value)>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => (i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedListShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableSortedList<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<(int Key, int Value)>(comparer: Comparer<(int Key, int Value)>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<(int Key, int Value)>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => (i, v))
|
||||
.Concat(_testData.Select((v, i) => (i, v)))
|
||||
.OrderBy(p => p.Item1)
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedDictionaryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedDictionaryShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableSortedDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v))
|
||||
.Concat(_testData.Select((v, i) => new KeyValuePair<int, int>(i, v)))
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeEnumerableShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.OrderBy(v => v).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeEnumerableShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(v => v).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyCollectionShouldAddItemsInSortedOrder()
|
||||
{
|
||||
var source = new TestReadOnlyCollection<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyCollectionShouldNotOverrideItems()
|
||||
{
|
||||
var source = new TestReadOnlyCollection<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
private sealed class TestReadOnlyCollection<T> : IReadOnlyCollection<T>
|
||||
{
|
||||
private readonly T[] _items;
|
||||
|
||||
public TestReadOnlyCollection(T[] items)
|
||||
{
|
||||
_items = items;
|
||||
}
|
||||
|
||||
public int Count => _items.Length;
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)_items).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclablePriorityQueueTests
|
||||
{
|
||||
private static readonly int[] _testData = new[] { 4, 1, 3, 2, 5 };
|
||||
|
||||
[Fact]
|
||||
public void DequeueShouldReturnItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
foreach (var item in _testData)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().BeInAscendingOrder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyWhenNotInitialized()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
_ = queue.Should().BeEmpty();
|
||||
_ = queue.LongCount.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
foreach (var item in _testData)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
|
||||
queue.Clear();
|
||||
_ = queue.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DequeueShouldThrowWhenNoElements()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
_ = Assert.Throws<ArgumentOutOfRangeException>(() => queue.Dequeue());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldDoNothingWhenEmpty()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(ReadOnlySpan<int>.Empty);
|
||||
_ = queue.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSpanShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
queue.AddRange((ReadOnlySpan<int>)_testData);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeArrayShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeListShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(new List<int>(_testData));
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyMemoryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange((ReadOnlyMemory<int>)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeMemoryShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData.AsMemory());
|
||||
queue.AddRange(_testData.AsMemory());
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeArrayShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
queue.AddRange(_testData);
|
||||
queue.AddRange(_testData);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeShouldAcceptNulls()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<long?>();
|
||||
queue.AddRange(new long?[] { null, 1 });
|
||||
_ = queue.Should().HaveCount(2).And.Contain((long?)null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeQueueShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableQueue<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeQueueShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableQueue<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeStackShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableStack<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedSetShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedSet<int>();
|
||||
foreach (int item in _testData)
|
||||
{
|
||||
source.Add(item);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeDictionaryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeDictionaryShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v))
|
||||
.Concat(_testData.Select((v, i) => new KeyValuePair<int, int>(i, v)))
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedListShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedList<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<(int Key, int Value)>(comparer: Comparer<(int Key, int Value)>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<(int Key, int Value)>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => (i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedListShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableSortedList<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<(int Key, int Value)>(comparer: Comparer<(int Key, int Value)>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<(int Key, int Value)>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => (i, v))
|
||||
.Concat(_testData.Select((v, i) => (i, v)))
|
||||
.OrderBy(p => p.Item1)
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedDictionaryShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var source = new RecyclableSortedDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v)).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeSortedDictionaryShouldNotOverrideItems()
|
||||
{
|
||||
using var source = new RecyclableSortedDictionary<int, int>();
|
||||
for (int i = 0; i < _testData.Length; i++)
|
||||
{
|
||||
source.Add(i, _testData[i]);
|
||||
}
|
||||
|
||||
using var queue = new RecyclablePriorityQueue<KeyValuePair<int, int>>(comparer: Comparer<KeyValuePair<int, int>>.Create((a, b) => a.Key.CompareTo(b.Key)));
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<KeyValuePair<int, int>>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Select((v, i) => new KeyValuePair<int, int>(i, v))
|
||||
.Concat(_testData.Select((v, i) => new KeyValuePair<int, int>(i, v)))
|
||||
.ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeEnumerableShouldAddItemsInSortedOrder()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.OrderBy(v => v).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeEnumerableShouldNotOverrideItems()
|
||||
{
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
_ = queue.AddRange((System.Collections.IEnumerable)_testData);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(v => v).ToList();
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyCollectionShouldAddItemsInSortedOrder()
|
||||
{
|
||||
var source = new TestReadOnlyCollection<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(_testData.OrderBy(static value => value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRangeReadOnlyCollectionShouldNotOverrideItems()
|
||||
{
|
||||
var source = new TestReadOnlyCollection<int>(_testData);
|
||||
using var queue = new RecyclablePriorityQueue<int>();
|
||||
|
||||
queue.AddRange(source);
|
||||
queue.AddRange(source);
|
||||
|
||||
var expected = _testData.Concat(_testData).OrderBy(static value => value).ToArray();
|
||||
var result = new List<int>();
|
||||
while (queue.LongCount > 0)
|
||||
{
|
||||
result.Add(queue.Dequeue());
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(expected);
|
||||
}
|
||||
|
||||
private sealed class TestReadOnlyCollection<T> : IReadOnlyCollection<T>
|
||||
{
|
||||
private readonly T[] _items;
|
||||
|
||||
public TestReadOnlyCollection(T[] items)
|
||||
{
|
||||
_items = items;
|
||||
}
|
||||
|
||||
public int Count => _items.Length;
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)_items).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,96 +1,96 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedDictionaryTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddShouldStoreUniqueItemsInOrder()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
dict.Add(v, v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var kv in dict)
|
||||
{
|
||||
result.Add(kv.Key);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(values.OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
dict[1] = "b";
|
||||
|
||||
_ = dict[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveShouldDeleteItem()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
dict.Add(v, v.ToString());
|
||||
}
|
||||
|
||||
dict.Remove(3).Should().BeTrue();
|
||||
_ = dict.ContainsKey(3).Should().BeFalse();
|
||||
_ = dict.Count.Should().Be(values.Length - 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumeratorShouldYieldItemsInOrder()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
foreach (var v in new[] { 3, 1, 4, 2 })
|
||||
{
|
||||
dict.Add(v, v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var item in dict)
|
||||
{
|
||||
result.Add(item.Key);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(new[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
var found = dict.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
dict.Add(1, 1);
|
||||
dict.Add(2, 2);
|
||||
|
||||
dict.Clear();
|
||||
_ = dict.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedDictionaryTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddShouldStoreUniqueItemsInOrder()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
dict.Add(v, v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var kv in dict)
|
||||
{
|
||||
result.Add(kv.Key);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(values.OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
dict[1] = "b";
|
||||
|
||||
_ = dict[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveShouldDeleteItem()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
dict.Add(v, v.ToString());
|
||||
}
|
||||
|
||||
dict.Remove(3).Should().BeTrue();
|
||||
_ = dict.ContainsKey(3).Should().BeFalse();
|
||||
_ = dict.Count.Should().Be(values.Length - 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumeratorShouldYieldItemsInOrder()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
foreach (var v in new[] { 3, 1, 4, 2 })
|
||||
{
|
||||
dict.Add(v, v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var item in dict)
|
||||
{
|
||||
result.Add(item.Key);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(new[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, string>();
|
||||
dict.Add(1, "a");
|
||||
|
||||
var found = dict.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var dict = new RecyclableSortedDictionary<int, int>();
|
||||
dict.Add(1, 1);
|
||||
dict.Add(2, 2);
|
||||
|
||||
dict.Clear();
|
||||
_ = dict.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +1,97 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedListTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, -key);
|
||||
}
|
||||
|
||||
_ = list.Should().HaveCount(itemsCount)
|
||||
.And.BeInAscendingOrder(x => x.Key);
|
||||
for (int i = 0; i < itemsCount; i++)
|
||||
{
|
||||
long expectedKey = i + 1;
|
||||
list.GetKey(i).Should().Be(expectedKey);
|
||||
list.GetValue(i).Should().Be(-expectedKey);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
list[1] = "b";
|
||||
|
||||
_ = list[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldDeleteItem(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long keyToRemove = (itemsCount + 1) / 2;
|
||||
list.Remove(keyToRemove).Should().BeTrue();
|
||||
_ = list.ContainsKey(keyToRemove).Should().BeFalse();
|
||||
_ = list.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Remove(0).Should().BeFalse();
|
||||
_ = list.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
var found = list.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = list.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current.Key);
|
||||
}
|
||||
|
||||
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
|
||||
_ = actual.Should().ContainInConsecutiveOrder(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedListTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, -key);
|
||||
}
|
||||
|
||||
_ = list.Should().HaveCount(itemsCount)
|
||||
.And.BeInAscendingOrder(x => x.Key);
|
||||
for (int i = 0; i < itemsCount; i++)
|
||||
{
|
||||
long expectedKey = i + 1;
|
||||
list.GetKey(i).Should().Be(expectedKey);
|
||||
list.GetValue(i).Should().Be(-expectedKey);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
list[1] = "b";
|
||||
|
||||
_ = list[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldDeleteItem(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long keyToRemove = (itemsCount + 1) / 2;
|
||||
list.Remove(keyToRemove).Should().BeTrue();
|
||||
_ = list.ContainsKey(keyToRemove).Should().BeFalse();
|
||||
_ = list.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Remove(0).Should().BeFalse();
|
||||
_ = list.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
var found = list.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = list.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current.Key);
|
||||
}
|
||||
|
||||
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
|
||||
_ = actual.Should().ContainInConsecutiveOrder(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +1,68 @@
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedSetTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddShouldStoreUniqueItemsInOrder()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
set.Add(v).Should().BeTrue();
|
||||
}
|
||||
|
||||
set.Add(3).Should().BeFalse();
|
||||
_ = set.Should().Equal(values.OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveShouldDeleteItem()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
set.Add(v);
|
||||
}
|
||||
|
||||
set.Remove(3).Should().BeTrue();
|
||||
_ = set.Contains(3).Should().BeFalse();
|
||||
_ = set.Should().Equal(values.Where(x => x != 3).OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumeratorShouldYieldItemsInOrder()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
foreach (var v in new[] { 3, 1, 4, 2 })
|
||||
{
|
||||
set.Add(v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var item in set)
|
||||
{
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(new[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
set.Add(1);
|
||||
set.Add(2);
|
||||
|
||||
set.Clear();
|
||||
_ = set.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedSetTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddShouldStoreUniqueItemsInOrder()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
set.Add(v).Should().BeTrue();
|
||||
}
|
||||
|
||||
set.Add(3).Should().BeFalse();
|
||||
_ = set.Should().Equal(values.OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveShouldDeleteItem()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
var values = new[] { 4, 1, 3, 2, 5 };
|
||||
foreach (var v in values)
|
||||
{
|
||||
set.Add(v);
|
||||
}
|
||||
|
||||
set.Remove(3).Should().BeTrue();
|
||||
_ = set.Contains(3).Should().BeFalse();
|
||||
_ = set.Should().Equal(values.Where(x => x != 3).OrderBy(x => x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumeratorShouldYieldItemsInOrder()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
foreach (var v in new[] { 3, 1, 4, 2 })
|
||||
{
|
||||
set.Add(v);
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var item in set)
|
||||
{
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
_ = result.Should().Equal(new[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyAfterClear()
|
||||
{
|
||||
using var set = new RecyclableSortedSet<int>();
|
||||
set.Add(1);
|
||||
set.Add(2);
|
||||
|
||||
set.Clear();
|
||||
_ = set.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"methodDisplay": "method",
|
||||
"parallelizeTestCollections": false,
|
||||
"preEnumerateTheories": false
|
||||
}
|
||||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"methodDisplay": "method",
|
||||
"parallelizeTestCollections": false,
|
||||
"preEnumerateTheories": false
|
||||
}
|
||||
Reference in new issue
Block a user