Files
Project-M/Assets/_Project/Tests/EditMode/StorageMathTests.cs
T
2026-06-02 18:28:23 -07:00

142 lines
4.5 KiB
C#

using NUnit.Framework;
using ProjectM.Simulation;
using Unity.Entities;
namespace ProjectM.Tests
{
/// <summary>
/// Plain-Entities tests for <see cref="StorageMath"/> deposit/withdraw merge logic, using a temp World
/// and a real DynamicBuffer&lt;StorageEntry&gt; (no netcode world). Pins shared-storage semantics:
/// deposits merge by item id, withdraws clamp to available and drop empty rows.
/// </summary>
public class StorageMathTests
{
static (World world, Entity e) MakeWorld()
{
var world = new World("StorageMathTestWorld");
var e = world.EntityManager.CreateEntity(typeof(StorageEntry));
return (world, e);
}
[Test]
public void Deposit_New_Item_Appends_Row()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 5);
Assert.AreEqual(1, buf.Length);
Assert.AreEqual((ushort)1, buf[0].ItemId);
Assert.AreEqual(5, buf[0].Count);
}
finally { world.Dispose(); }
}
[Test]
public void Deposit_Same_Item_Merges()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 5);
StorageMath.Deposit(buf, 1, 3);
Assert.AreEqual(1, buf.Length);
Assert.AreEqual(8, buf[0].Count);
}
finally { world.Dispose(); }
}
[Test]
public void Deposit_Different_Items_Separate_Rows()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 5);
StorageMath.Deposit(buf, 2, 7);
Assert.AreEqual(2, buf.Length);
}
finally { world.Dispose(); }
}
[Test]
public void Withdraw_Partial_Decrements()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 5);
int taken = StorageMath.Withdraw(buf, 1, 2);
Assert.AreEqual(2, taken);
Assert.AreEqual(1, buf.Length);
Assert.AreEqual(3, buf[0].Count);
}
finally { world.Dispose(); }
}
[Test]
public void Withdraw_To_Zero_Drops_Row()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 5);
int taken = StorageMath.Withdraw(buf, 1, 5);
Assert.AreEqual(5, taken);
Assert.AreEqual(0, buf.Length);
}
finally { world.Dispose(); }
}
[Test]
public void Withdraw_More_Than_Available_Clamps()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 3);
int taken = StorageMath.Withdraw(buf, 1, 10);
Assert.AreEqual(3, taken);
Assert.AreEqual(0, buf.Length);
}
finally { world.Dispose(); }
}
[Test]
public void Withdraw_Missing_Item_Is_NoOp()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 3);
int taken = StorageMath.Withdraw(buf, 99, 1);
Assert.AreEqual(0, taken);
Assert.AreEqual(1, buf.Length);
}
finally { world.Dispose(); }
}
[Test]
public void Deposit_Ignores_NonPositive_And_ZeroItem()
{
var (world, e) = MakeWorld();
try
{
var buf = world.EntityManager.GetBuffer<StorageEntry>(e);
StorageMath.Deposit(buf, 1, 0);
StorageMath.Deposit(buf, 1, -5);
StorageMath.Deposit(buf, 0, 5);
Assert.AreEqual(0, buf.Length);
}
finally { world.Dispose(); }
}
}
}