diff --git a/.gitignore b/.gitignore index 9053e42..96b37db 100644 --- a/.gitignore +++ b/.gitignore @@ -24,11 +24,17 @@ /src/ListPool/bin /tests/ListPool.Netstandard2_0.UnitTests/bin /tests/ListPool.Netstandard2_0.UnitTests/obj -/tests/ListPool.Resolvers.Utf8Json.Tests/obj -/tests/ListPool.Resolvers.Utf8Json.Tests/bin/Debug/netcoreapp3.1 -/src/ListPool.Resolvers.Utf8Json/obj -/src/ListPool.Resolvers.Utf8Json/bin/Debug/netstandard2.0 -/src/ListPool.Resolvers.Utf8Json/bin -/tests/ListPool.Resolvers.Utf8Json.Tests/bin/Release/netcoreapp3.1 +/tests/ListPool.Serializers.Utf8Json.Formatters.UnitTests/obj +/tests/ListPool.Serializers.Utf8Json.Formatters.UnitTests/bin +/tests/ListPool.Serializers.Utf8Json.Resolvers.UnitTests/obj +/tests/ListPool.Serializers.Utf8Json.Resolvers.UnitTests/bin +/tests/ListPool.Serializers.SystemTextJson.Converters.UnitTests/obj +/tests/ListPool.Serializers.SystemTextJson.Converters.UnitTests/bin +src/ListPool.Serializers.Utf8Json.Formatters/obj +src/ListPool.Serializers.Utf8Json.Formatters/bin +src/ListPool.Serializers.Utf8Json.Resolvers/obj +src/ListPool.Serializers.Utf8Json.Resolvers/bin +src/ListPool.Serializers.SystemTextJson.Converters/obj +src/ListPool.Serializers.SystemTextJson.Converters/bin /ListPool.Utf8Json.Tests/obj /ListPool.Utf8Json.Tests/bin diff --git a/src/ListPool/ListPool.cs b/src/ListPool/ListPool.cs index 629645a..d0297b0 100644 --- a/src/ListPool/ListPool.cs +++ b/src/ListPool/ListPool.cs @@ -533,6 +533,49 @@ public void EnsureCapacity(int capacity) /// public void SetOffsetManually(int offset) => Count = offset; + /// + /// Sorts the elements in the entire ListPool using the default comparer. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Sort() => Sort(0, Count, null); + + /// + /// Sorts the elements in the entire ListPool using the specified comparer. + /// + /// The IComparer implementation to use when comparing elements, or null to use the default comparer Comparer.Default. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Sort(IComparer comparer) => Sort(0, Count, comparer); + + /// + /// Sorts the elements in a range of elements in the ListPool using the specified comparer. + /// + /// The zero-based starting index of the range to sort. + /// The length of the range to sort. + /// The IComparer implementation to use when comparing elements, or null to use the default comparer Comparer.Default. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Sort(int index, int count, IComparer comparer) => Array.Sort(_items, index, count, comparer); + + /// + /// Sorts the elements in the entire ListPool using the specified Comparison. + /// + /// The Comparison to use when comparing elements. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Sort(Comparison comparison) => Array.Sort(_items, 0, Count, Comparer.Create(comparison)); + + /// + /// Reverses the order of the elements in the entire ListPool. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Reverse() => Array.Reverse(_items, 0, Count); + + /// + /// Reverses the order of the elements in the specified range. + /// + /// The zero-based starting index of the range to reverse. + /// The length of the range to reverse. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Reverse(int index, int count) => Array.Reverse(_items, index, count); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Enumerator GetEnumerator() => new Enumerator(_items, Count); diff --git a/tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs b/tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs index 8083ad6..9456b99 100644 --- a/tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs +++ b/tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs @@ -738,7 +738,7 @@ public void EnsureCapacity_increase_capacity_of_inner_buffer() Assert.Equal(biggerCapacity, sut.Capacity); } - [Fact] + [Fact] public void GetRawBuffer_returns_underline_buffer() { int[] expectedItems = s_fixture.CreateMany().ToArray(); @@ -799,5 +799,110 @@ public void SetOffsetManually_allows_to_set_decrease_internal_offset() Assert.Equal(expectedItems[i], sut[i]); } } + + // New Sort and Reverse tests + + [Fact] + public void Sort_sorts_items_in_ascending_order() + { + using ListPool sut = new ListPool(20); + List unsortedItems = s_fixture.CreateMany(10).ToList(); + foreach (var item in unsortedItems) + { + Console.WriteLine(item); + sut.Add(item); + } + + var sortedItems = unsortedItems.OrderBy(x => x).ToList(); + + sut.Sort(); + Assert.Equal(sortedItems, sut.ToArray()); + } + + [Fact] + public void Sort_with_comparer_sorts_items_in_custom_order() + { + using ListPool sut = new ListPool(20); + List unsortedItems = s_fixture.CreateMany(10).ToList(); + foreach (var item in unsortedItems) + { + sut.Add(item); + } + + var sortedItems = unsortedItems.OrderByDescending(x => x).ToList(); + + sut.Sort(Comparer.Create((x, y) => y.CompareTo(x))); // Descending order + Assert.Equal(sortedItems, sut.ToArray()); + } + + [Fact] + public void Sort_with_index_and_count_sorts_specified_range() + { + using ListPool sut = new ListPool(20); + List unsortedItems = s_fixture.CreateMany(10).ToList(); + foreach (var item in unsortedItems) + { + sut.Add(item); + } + + var sortedItems = unsortedItems.Skip(2).Take(5).OrderBy(x => x).ToList(); + sortedItems.InsertRange(0, unsortedItems.Take(2)); + sortedItems.AddRange(unsortedItems.Skip(7)); + + sut.Sort(2, 5, null); // Sort a range + Assert.Equal(sortedItems, sut.ToArray()); + } + + [Fact] + public void Sort_with_comparison_sorts_items_in_custom_order() + { + using ListPool sut = new ListPool(20); + List unsortedItems = s_fixture.CreateMany(10).ToList(); + foreach (var item in unsortedItems) + { + sut.Add(item); + } + + var sortedItems = unsortedItems.OrderByDescending(x => x).ToList(); + + sut.Sort((x, y) => y.CompareTo(x)); // Descending order + Assert.Equal(sortedItems, sut.ToArray()); + } + + [Fact] + public void Reverse_reverses_the_entire_list() + { + using ListPool sut = new ListPool(20); + List items = s_fixture.CreateMany(10).ToList(); + foreach (var item in items) + { + sut.Add(item); + } + + sut.Reverse(); + + var reversedItems = items.AsEnumerable().Reverse().ToList(); + Assert.Equal(reversedItems, sut.ToArray()); + } + + [Fact] + public void Reverse_with_index_and_count_reverses_specified_range() + { + using ListPool sut = new ListPool(20); + List items = s_fixture.CreateMany(10).ToList(); + foreach (var item in items) + { + sut.Add(item); + } + + sut.Reverse(2, 5); + + var expectedItems = items.Take(2) + .Concat(items.Skip(2).Take(5).Reverse()) + .Concat(items.Skip(7)) + .ToList(); + + Assert.Equal(expectedItems, sut.ToArray()); + } } }