Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Nov 25, 2023
1 parent 8210c6f commit e21eab2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
100 changes: 100 additions & 0 deletions Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Runtime.CompilerServices;
using Arch.LowLevel.Jagged;

namespace Arch.LowLevel.Tests.Jagged;
using static Assert;

/// <summary>
/// Checks <see cref="JaggedArray{T}"/> related methods.
/// </summary>
[TestFixture]
public class JaggedArrayTest
{

/// <summary>
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
[Test]
public void Add([Values(256,512,1024)] int capacity)
{
// Check add
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);

// adding
for (var index = 0; index < jaggedArray.Capacity; index++)
{
jaggedArray.Add(index, index);
}

// Checking
for (var index = 0; index < jaggedArray.Capacity; index++)
{
var item = jaggedArray[index];
That(item, Is.EqualTo(index));
}

That(jaggedArray.Capacity, Is.EqualTo(capacity));
}

/// <summary>
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
[Test]
public void Remove([Values(256,512,1024)] int capacity)
{
// Check add
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);

// Adding
for (var index = 0; index < jaggedArray.Capacity; index++)
{
jaggedArray.Add(index, index);
}

// Removing
for (var index = jaggedArray.Capacity-1; index >= 0; index--)
{
jaggedArray.Remove(index);
}

// Checking
for (var index = 0; index < jaggedArray.Capacity; index++)
{
var item = jaggedArray[index];
That(item, Is.EqualTo(-1));
}
}

/// <summary>
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
[Test]
public void TrimExcess([Values(2560,5120,10240)] int capacity)
{
// Check add
var jaggedArray = new JaggedArray<int>(16000/Unsafe.SizeOf<int>(), -1, capacity);

// Adding
for (var index = 0; index < jaggedArray.Capacity; index++)
{
jaggedArray.Add(index, index);
}

// Removing half of items
for (var index = jaggedArray.Capacity-1; index >= jaggedArray.Capacity/2; index--)
{
jaggedArray.Remove(index);
}

var buckets = jaggedArray.Buckets;
jaggedArray.TrimExcess();
That(jaggedArray.Buckets, Is.EqualTo((buckets + 2 - 1)/2));

// Checking first half still having the desired value
for (var index = 0; index < jaggedArray.Capacity/2; index++)
{
var item = jaggedArray[index];
That(item, Is.EqualTo(index));
}
}
}
4 changes: 2 additions & 2 deletions Arch.LowLevel/Arch.LowLevel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

<PackageId>Arch.LowLevel</PackageId>
<Title>Arch.LowLevel</Title>
<Version>1.0.9</Version>
<Version>1.1.0</Version>
<Authors>genaray</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Description>LowLevel tools for arch.</Description>
<PackageReleaseNotes>Made buckets public acessable. </PackageReleaseNotes>
<PackageReleaseNotes>Additional fixes. </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 Down

0 comments on commit e21eab2

Please sign in to comment.