From e21eab21073e9285cca2ace7cb7df00301ffb512 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 25 Nov 2023 03:53:06 +0100 Subject: [PATCH] Added tests. --- Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs | 100 ++++++++++++++++++ Arch.LowLevel/Arch.LowLevel.csproj | 4 +- 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs diff --git a/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs b/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs new file mode 100644 index 0000000..06a8348 --- /dev/null +++ b/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs @@ -0,0 +1,100 @@ +using System.Runtime.CompilerServices; +using Arch.LowLevel.Jagged; + +namespace Arch.LowLevel.Tests.Jagged; +using static Assert; + +/// +/// Checks related methods. +/// +[TestFixture] +public class JaggedArrayTest +{ + + /// + /// Checks if is capable of adding items correctly. + /// + [Test] + public void Add([Values(256,512,1024)] int capacity) + { + // Check add + var jaggedArray = new JaggedArray(16000/Unsafe.SizeOf(), -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)); + } + + /// + /// Checks if is capable of adding items correctly. + /// + [Test] + public void Remove([Values(256,512,1024)] int capacity) + { + // Check add + var jaggedArray = new JaggedArray(16000/Unsafe.SizeOf(), -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)); + } + } + + /// + /// Checks if is capable of adding items correctly. + /// + [Test] + public void TrimExcess([Values(2560,5120,10240)] int capacity) + { + // Check add + var jaggedArray = new JaggedArray(16000/Unsafe.SizeOf(), -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)); + } + } +} \ No newline at end of file diff --git a/Arch.LowLevel/Arch.LowLevel.csproj b/Arch.LowLevel/Arch.LowLevel.csproj index d53f06c..2d70ce2 100644 --- a/Arch.LowLevel/Arch.LowLevel.csproj +++ b/Arch.LowLevel/Arch.LowLevel.csproj @@ -13,11 +13,11 @@ Arch.LowLevel Arch.LowLevel - 1.0.9 + 1.1.0 genaray Apache-2.0 LowLevel tools for arch. - Made buckets public acessable. + Additional fixes. c#;.net;.net6;.net7;ecs;game;entity;gamedev; game-development; game-engine; entity-component-system; arch; https://github.com/genaray/Arch.Extended