Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions csharp/Platform.Collections.Tests/ArrayPoolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Xunit;
using Platform.Collections.Arrays;

namespace Platform.Collections.Tests
{
public class ArrayPoolTests
{
[Fact]
public void Resize_WhenSizesAreSame_ShouldReturnSameObject()
{
var pool = new ArrayPool<int>();
var originalArray = pool.AllocateDisposable(5);
int[] arr = originalArray;

// Fill with test data
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i + 1;
}

// Resize to the same size
var resizedArray = pool.Resize(originalArray, 5);
int[] newArr = resizedArray;

// Should be the same object reference
Assert.True(object.ReferenceEquals(arr, newArr));

// Data should remain unchanged
Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, newArr);

resizedArray.Dispose();
}

[Fact]
public void Resize_WhenSizesDiffer_ShouldCreateNewObject()
{
var pool = new ArrayPool<int>();
var originalArray = pool.AllocateDisposable(5);
int[] arr = originalArray;

// Fill with test data
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i + 1;
}

// Resize to different size (smaller)
var resizedArray = pool.Resize(originalArray, 3);
int[] newArr = resizedArray;

// Should be different object reference
Assert.False(object.ReferenceEquals(arr, newArr));

// Data should be copied (first 3 elements)
Assert.Equal(new int[] { 1, 2, 3 }, newArr);

resizedArray.Dispose();
}

[Fact]
public void Resize_WhenResizingToLarger_ShouldCopyAllOriginalData()
{
var pool = new ArrayPool<int>();
var originalArray = pool.AllocateDisposable(3);
int[] arr = originalArray;

// Fill with test data
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i + 1;
}

// Resize to larger size
var resizedArray = pool.Resize(originalArray, 5);
int[] newArr = resizedArray;

// Should be different object reference
Assert.False(object.ReferenceEquals(arr, newArr));

// Original data should be copied, new elements should be default (0)
Assert.Equal(new int[] { 1, 2, 3, 0, 0 }, newArr);

resizedArray.Dispose();
}

[Fact]
public void Resize_WithEmptyArray_ShouldAllocateNewArray()
{
var pool = new ArrayPool<int>();
var emptyArray = pool.AllocateDisposable(0);

// Resize empty array to size 3
var resizedArray = pool.Resize(emptyArray, 3);
int[] newArr = resizedArray;

Assert.Equal(3, newArr.Length);
Assert.All(newArr, x => Assert.Equal(0, x)); // All elements should be default

resizedArray.Dispose();
}

[Fact]
public void Resize_ToZeroSize_ShouldReturnEmptyArray()
{
var pool = new ArrayPool<int>();
var originalArray = pool.AllocateDisposable(5);

// Resize to zero size
var resizedArray = pool.Resize(originalArray, 0);
int[] newArr = resizedArray;

Assert.Equal(0, newArr.Length);

resizedArray.Dispose();
}
}
}
7 changes: 6 additions & 1 deletion csharp/Platform.Collections/Arrays/ArrayPool[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ public ArrayPool() : this(ArrayPool.DefaultMaxArraysPerSize) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Disposable<T[]> Resize(Disposable<T[]> source, long size)
{
var destination = AllocateDisposable(size);
T[] sourceArray = source;
if (!sourceArray.IsNullOrEmpty() && sourceArray.LongLength == size)
{
// If the size is the same, return the source without any allocation or copying
return source;
}
var destination = AllocateDisposable(size);
if (!sourceArray.IsNullOrEmpty())
{
T[] destinationArray = destination;
Expand Down
14 changes: 14 additions & 0 deletions experiments/Experiments.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../csharp/Platform.Collections/Platform.Collections.csproj" />
</ItemGroup>

</Project>
Loading