Skip to content

Commit

Permalink
Several refactorings and improved performance. Also added Array, a …
Browse files Browse the repository at this point in the history
…wrapper around generic arrays for improved performance.
  • Loading branch information
genaray committed Aug 8, 2024
1 parent 1fa6450 commit 98961a7
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 194 deletions.
122 changes: 122 additions & 0 deletions Arch.LowLevel.Tests/ArrayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.ComponentModel.DataAnnotations;

namespace Arch.LowLevel.Tests;
using static Assert;

/// <summary>
/// Checks <see cref="Array{T}"/> related methods.
/// </summary>
[TestFixture]
public class ArrayTest
{
/// <summary>
/// Checks if <see cref="Array{T}"/> is capable of allocating space and adding items.
/// </summary>
[Test]
public void ArrayCreate()
{
var array = new Array<int>(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;

That(array.Count, Is.EqualTo(3));
}

[Test]
public void ArrayEnumerator()
{
var array = new Array<int>(3);
array[0] = 1;
array[1] = 2;
array[2] = 3;

var count = 1;
foreach (var item in array)
That(item, Is.EqualTo(count++));
}

[Test]
public void ArrayEmptyIsEmpty()
{
var empty = Array.Empty<long>();
That(empty, Is.Empty);
}

[Test]
public void ArrayFill()
{
var array = new Array<int>(35);
Array.Fill(ref array, 8);

for (var i = 0; i < array.Length; i++)
That(array[i], Is.EqualTo(8));
}

[Test]
public void ArrayCopy()
{
var src = new Array<int>(15);
var dst = new Array<int>(6);

for (var i = 0; i < src.Length; i++)
src[i] = i;

Array.Fill(ref dst);
Array.Copy(ref src, 4, ref dst, 1, 4);

Multiple(() =>
{
That(dst[0], Is.EqualTo(0));
That(dst[1], Is.EqualTo(4));
That(dst[2], Is.EqualTo(5));
That(dst[3], Is.EqualTo(6));
That(dst[4], Is.EqualTo(7));
That(dst[5], Is.EqualTo(0));
});
}

[Test]
public void ArrayResizeShrink()
{
var array = new Array<int>(19);
for (var i = 0; i < array.Length; i++)
array[i] = i;

var resized = Array.Resize(ref array, 8);
for (var i = 0; i < resized.Length; i++)
That(resized[i], Is.EqualTo(i));
}

[Test]
public void ArrayResizeGrow()
{
var array = new Array<int>(8);
for (var i = 0; i < array.Length; i++)
array[i] = i;

var resized = Array.Resize(ref array, 19);
for (var i = 0; i < array.Length; i++)
That(resized[i], Is.EqualTo(i));
}

[Test]
public void ArrayEquals()
{
var a = new Array<int>(8);
var b = a;

That(a, Is.EqualTo(b));
That(a == b, Is.True);
}

[Test]
public void ArrayNotEquals()
{
var a = new Array<int>(8);
var b = new Array<int>(8);

That(a, Is.Not.EqualTo(b));
That(a != b, Is.True);
}
}
8 changes: 4 additions & 4 deletions Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class JaggedArrayTest
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
[Test]
public void Add([Values(256,512,1024)] int capacity)
public void Add([Values(256,512,1024,2048,4096)] int capacity)
{
// Check add
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);
Expand All @@ -37,7 +37,7 @@ public void Add([Values(256,512,1024)] int capacity)
}

[Test]
public void TryGetValue([Values(256,512,1024)] int capacity)
public void TryGetValue([Values(256,512,1024,2048,4096)] int capacity)
{
// Initialize the JaggedArray
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);
Expand All @@ -62,7 +62,7 @@ public void TryGetValue([Values(256,512,1024)] int capacity)
}

[Test]
public void TryGetValueRef([Values(256,512,1024)] int capacity)
public void TryGetValueRef([Values(256,512,1024,2048,4096)] int capacity)
{
// Initialize the JaggedArray
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);
Expand Down Expand Up @@ -92,7 +92,7 @@ public void TryGetValueRef([Values(256,512,1024)] int capacity)
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
[Test]
public void Remove([Values(256,512,1024)] int capacity)
public void Remove([Values(256,512,1024,2048,4096)] int capacity)
{
// Check add
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);
Expand Down
8 changes: 6 additions & 2 deletions Arch.LowLevel/Arch.LowLevel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

<PackageId>Arch.LowLevel</PackageId>
<Title>Arch.LowLevel</Title>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<Authors>genaray</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Description>LowLevel tools for arch.</Description>
<PackageReleaseNotes>Fixed several JaggedArray related bugs. </PackageReleaseNotes>
<PackageReleaseNotes>Refactored JaggedArrays.
Increased performance of JaggedArrays.
Added Array, a new class that acts as a Wrapper around normal generic Arrays for unsafe operations.
Added tests. </PackageReleaseNotes>
<PackageTags>c#;.net;.net6;.net7;ecs;game;entity;gamedev; game-development; game-engine; entity-component-system; arch;</PackageTags>

<PackageProjectUrl>https://github.com/genaray/Arch.Extended</PackageProjectUrl>
Expand All @@ -35,6 +38,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 98961a7

Please sign in to comment.