Skip to content

Commit

Permalink
Update ListPoolTests.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
mastef committed Jun 3, 2024
1 parent c4059c3 commit 9dc6b87
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>().ToArray();
Expand Down Expand Up @@ -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<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(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<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in unsortedItems)
{
sut.Add(item);
}

var sortedItems = unsortedItems.OrderByDescending(x => x).ToList();

sut.Sort(Comparer<int>.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<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(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<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(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<int> sut = new ListPool<int>(20);
List<int> items = s_fixture.CreateMany<int>(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<int> sut = new ListPool<int>(20);
List<int> items = s_fixture.CreateMany<int>(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());
}
}
}

0 comments on commit 9dc6b87

Please sign in to comment.