using Recyclable.Collections; namespace Recyclable.CollectionsTests { public class RecyclableListTestData : IDisposable { private static void DisposeTestData(IEnumerable? testCases) { if (testCases != null) { foreach (var testData in testCases) { foreach (var item in testData) { if (item is IDisposable disposable) { disposable.Dispose(); } } } } } private static void DisposeTestData(IEnumerable>? testCases) { if (testCases != null) { foreach (var testData in testCases) { if (testData is IDisposable disposable) { disposable.Dispose(); } } } } private static Dictionary>? _testCases = new(); public RecyclableListTestData() { _testCases ??= new(); } private static IEnumerable DoCreateTestData(long itemsCount) { for (long index = 1; index <= itemsCount; index++) { yield return index; } } public static IEnumerable CreateTestData(long itemsCount) { if (_testCases!.TryGetValue(itemsCount, out var testCases)) { return testCases; } testCases = DoCreateTestData(itemsCount).ToArray(); _testCases.Add(itemsCount, testCases); return testCases; } public void Dispose() { DisposeTestData(_testCases?.Values); DisposeTestData(_sourceDataVariants); DisposeTestData(_emptySourceDataVariants); _testCases = null; _sourceDataVariants = null; _targetDataVariants = null; _sourceTargetDataVariants = null; _emptySourceDataVariants = null; GC.SuppressFinalize(this); } public static readonly IEnumerable ItemsCountVariants = new long[] { 0, 1, 2, 3, 10, 16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096, 8_192, 16_384, 32_768, 65_536, 131_072, 262_144, 524_288, 1_048_576, 10_000_000 }; public static readonly IEnumerable BlockSizeVariants = new int[] { 1, 2, 3, 10, 16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096, 8_192, 16_384, 32_768, 65_536, 131_072, 262_144, 524_288, 1_048_576 }; private static IEnumerable GetTargetDataVariants() { foreach (var itemsCount in ItemsCountVariants) { foreach (var targetBlockSize in BlockSizeVariants) { yield return new object[] { itemsCount, targetBlockSize }; } } } private static IEnumerable? _targetDataVariants; public static IEnumerable TargetDataVariants => _targetDataVariants ??= GetTargetDataVariants().ToArray(); private static IEnumerable CreateSourceDataVariants() { foreach (var itemsCount in ItemsCountVariants) { yield return new object[] { $"int[{itemsCount}]", CreateTestData(itemsCount).ToArray(), itemsCount }; yield return new object[] { $"List(itemsCount: {itemsCount}", CreateTestData(itemsCount).ToList(), itemsCount }; yield return new object[] { $"RecyclableArrayList(itemsCount: {itemsCount})", CreateTestData(itemsCount).ToRecyclableArrayList(), itemsCount }; foreach (var sourceBlockSize in BlockSizeVariants) { yield return new object[] { $"RecyclableList(itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize})", CreateTestData(itemsCount).ToRecyclableList(sourceBlockSize), itemsCount }; } yield return new object[] { $"IList(itemsCount: {itemsCount})", new CustomIList(CreateTestData(itemsCount)), itemsCount }; yield return new object[] { $"IEnumerable(itemsCount: {itemsCount}) with non-enumerated count", CreateTestData(itemsCount), itemsCount }; yield return new object[] { $"IEnumerable(itemsCount: {itemsCount}) without non-enumerated count", new EnumerableWithoutCount(CreateTestData(itemsCount)), itemsCount }; } } private static IEnumerable? _sourceDataVariants; public static IEnumerable SourceDataVariants => _sourceDataVariants ??= CreateSourceDataVariants().ToArray(); private static IEnumerable CreateSourceEmptyDataVariants() => new[] { new object[] { "int[]", Array.Empty() }, new object[] { "List", Array.Empty().ToList() }, new object[] { "RecyclableArrayList", Array.Empty().ToRecyclableArrayList() }, new object[] { "RecyclableList", Array.Empty().ToRecyclableList() }, new object[] { "IList", new CustomIList(Array.Empty()) }, new object[] { "IEnumerable with non-enumerated count", Array.Empty().AsEnumerable() }, new object[] { "IEnumerable without non-enumerated count", new EnumerableWithoutCount(Array.Empty()) } }; private static IEnumerable? _emptySourceDataVariants; public static IEnumerable EmptySourceDataVariants => _emptySourceDataVariants ??= CreateSourceEmptyDataVariants().ToArray(); private static IEnumerable CreateSourceTargetDataVariants() { foreach (var testData in SourceDataVariants) { foreach (var targetBlockSize in BlockSizeVariants) { yield return new object[] { testData[0], testData[1], testData[2], targetBlockSize }; } } } private static IEnumerable? _sourceTargetDataVariants; public static IEnumerable SourceTargetDataVariants => _sourceTargetDataVariants ??= CreateSourceTargetDataVariants().ToArray(); } }