202 lines
5.4 KiB
C#
202 lines
5.4 KiB
C#
using Xunit;
|
|
using MicroUI;
|
|
using System;
|
|
|
|
public class BasicTests : IDisposable
|
|
{
|
|
public BasicTests()
|
|
{
|
|
MuAssert.TestMode = true;
|
|
}
|
|
public void Dispose()
|
|
{
|
|
MuAssert.TestMode = false;
|
|
}
|
|
|
|
[Fact]
|
|
public void MathUtil_Clamp_Int_WorksLikeC()
|
|
{
|
|
Assert.Equal(5, MathUtil.Clamp(5, 0, 10));
|
|
Assert.Equal(0, MathUtil.Clamp(-5, 0, 10));
|
|
Assert.Equal(10, MathUtil.Clamp(15, 0, 10));
|
|
}
|
|
|
|
[Fact]
|
|
public void MathUtil_Clamp_Float_WorksLikeC()
|
|
{
|
|
Assert.Equal(5.0f, MathUtil.Clamp(5.0f, 0.0f, 10.0f));
|
|
Assert.Equal(0.0f, MathUtil.Clamp(-5.0f, 0.0f, 10.0f));
|
|
Assert.Equal(10.0f, MathUtil.Clamp(15.0f, 0.0f, 10.0f));
|
|
}
|
|
|
|
[Fact]
|
|
public void FixedStack_PushPop_Works()
|
|
{
|
|
var stack = new FixedStack<int>(4);
|
|
stack.Push(1);
|
|
stack.Push(2);
|
|
Assert.Equal(2, stack.Pop());
|
|
Assert.Equal(1, stack.Pop());
|
|
}
|
|
|
|
[Fact]
|
|
public void FixedStack_Peek_Works()
|
|
{
|
|
var stack = new FixedStack<int>(4);
|
|
stack.Push(42);
|
|
Assert.Equal(42, stack.Peek());
|
|
}
|
|
|
|
[Fact]
|
|
public void FixedStack_Clear_ResetsIndex()
|
|
{
|
|
var stack = new FixedStack<int>(4);
|
|
stack.Push(1);
|
|
stack.Push(2);
|
|
stack.Clear();
|
|
Assert.Equal(0, stack.Index);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuVec2_ConstructsCorrectly()
|
|
{
|
|
var v = new MuVec2(3, 7);
|
|
Assert.Equal(3, v.X);
|
|
Assert.Equal(7, v.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuRect_ConstructsCorrectly()
|
|
{
|
|
var r = new MuRect(1, 2, 3, 4);
|
|
Assert.Equal(1, r.X);
|
|
Assert.Equal(2, r.Y);
|
|
Assert.Equal(3, r.W);
|
|
Assert.Equal(4, r.H);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuColor_ConstructsCorrectly()
|
|
{
|
|
var c = new MuColor(10, 20, 30, 40);
|
|
Assert.Equal((byte)10, c.R);
|
|
Assert.Equal((byte)20, c.G);
|
|
Assert.Equal((byte)30, c.B);
|
|
Assert.Equal((byte)40, c.A);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuPool_GetAndInit_Works()
|
|
{
|
|
var ctx = new MuContext();
|
|
ctx.Frame = 1;
|
|
var pool = new MuPoolItem[4];
|
|
uint id1 = 123, id2 = 456;
|
|
// Initially not found
|
|
Assert.Equal(-1, MuPool.Get(ctx, pool, id1));
|
|
// Init first
|
|
int idx1 = MuPool.Init(ctx, pool, id1);
|
|
Assert.Equal(id1, pool[idx1].Id);
|
|
// Should now be found
|
|
Assert.Equal(idx1, MuPool.Get(ctx, pool, id1));
|
|
// Init second
|
|
int idx2 = MuPool.Init(ctx, pool, id2);
|
|
Assert.Equal(id2, pool[idx2].Id);
|
|
Assert.Equal(idx2, MuPool.Get(ctx, pool, id2));
|
|
}
|
|
|
|
[Fact]
|
|
public void MuPool_Update_SetsLastUpdate()
|
|
{
|
|
var ctx = new MuContext();
|
|
ctx.Frame = 42;
|
|
var pool = new MuPoolItem[2];
|
|
int idx = MuPool.Init(ctx, pool, 1);
|
|
MuPool.Update(ctx, pool, idx);
|
|
Assert.Equal(42, pool[idx].LastUpdate);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuAssert_Expect_ThrowsOnFalse()
|
|
{
|
|
var ex = Assert.Throws<Exception>(() => MuAssert.Expect(false));
|
|
Assert.Contains("assertion failed", ex.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuPool_RecyclesLeastRecentlyUsedSlot()
|
|
{
|
|
var ctx = new MuContext();
|
|
ctx.Frame = 1;
|
|
var pool = new MuPoolItem[2];
|
|
int idx1 = MuPool.Init(ctx, pool, 100);
|
|
ctx.Frame++;
|
|
int idx2 = MuPool.Init(ctx, pool, 200);
|
|
Assert.NotEqual(idx1, idx2);
|
|
// Both slots are now used, next init should recycle the LRU (idx1)
|
|
ctx.Frame++;
|
|
int idx3 = MuPool.Init(ctx, pool, 300);
|
|
Assert.Equal(idx1, idx3); // idx1 was least recently used
|
|
Assert.Equal(300u, pool[idx3].Id);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuPool_GetReturnsMinusOneForMissingId()
|
|
{
|
|
var ctx = new MuContext();
|
|
ctx.Frame = 1;
|
|
var pool = new MuPoolItem[2];
|
|
Assert.Equal(-1, MuPool.Get(ctx, pool, 999));
|
|
}
|
|
|
|
[Fact]
|
|
public void MuPool_InitThrowsIfNoSlotAvailable()
|
|
{
|
|
var ctx = new MuContext();
|
|
ctx.Frame = 0;
|
|
var pool = new MuPoolItem[1];
|
|
pool[0].LastUpdate = 0;
|
|
// This will throw because no slot is older than frame 0
|
|
Assert.Throws<Exception>(() => MuPool.Init(ctx, pool, 1));
|
|
}
|
|
|
|
[Fact]
|
|
public void MuRect_Equality_Works()
|
|
{
|
|
var a = new MuRect(1, 2, 3, 4);
|
|
var b = new MuRect(1, 2, 3, 4);
|
|
var c = new MuRect(0, 0, 0, 0);
|
|
Assert.Equal(a.X, b.X);
|
|
Assert.Equal(a.Y, b.Y);
|
|
Assert.Equal(a.W, b.W);
|
|
Assert.Equal(a.H, b.H);
|
|
Assert.NotEqual(a.X, c.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void MuVec2_Arithmetic_Works()
|
|
{
|
|
var a = new MuVec2(2, 3);
|
|
var b = new MuVec2(4, 5);
|
|
var sum = new MuVec2(a.X + b.X, a.Y + b.Y);
|
|
Assert.Equal(6, sum.X);
|
|
Assert.Equal(8, sum.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void FixedStack_Overflow_Throws()
|
|
{
|
|
var stack = new FixedStack<int>(2);
|
|
stack.Push(1);
|
|
stack.Push(2);
|
|
// Next push should trigger assertion (but not throw in release)
|
|
Assert.Throws<Exception>(() => stack.Push(3));
|
|
}
|
|
|
|
[Fact]
|
|
public void FixedStack_Underflow_Throws()
|
|
{
|
|
var stack = new FixedStack<int>(2);
|
|
Assert.Throws<Exception>(() => stack.Pop());
|
|
}
|
|
} |