Implement ActionPool (backend only)
This commit is contained in:
+56
-42
@@ -1,20 +1,34 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Craftimizer.Test.Solver;
|
||||
|
||||
[TestClass]
|
||||
public class ActionSetTests
|
||||
{
|
||||
private readonly ActionPool pool = ActionPool.Default;
|
||||
|
||||
[TestMethod]
|
||||
public void TestActionPoolSize()
|
||||
{
|
||||
Assert.AreEqual(ActionPool.EnumSize, Enum.GetValues<ActionType>().Length);
|
||||
Assert.AreEqual(ActionPool.MaskSize, Unsafe.SizeOf<ActionSet>() * 8);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestAcceptedActions()
|
||||
{
|
||||
var actions = ActionSet.AcceptedActions;
|
||||
var lut = ActionSet.AcceptedActionsLUT;
|
||||
|
||||
Assert.IsTrue(actions.Length <= 32);
|
||||
foreach (var i in Enum.GetValues<ActionType>())
|
||||
{
|
||||
var idx = lut[(byte)i];
|
||||
if (idx != -1)
|
||||
Assert.AreEqual(i, actions[idx]);
|
||||
byte idx;
|
||||
try
|
||||
{
|
||||
idx = pool.FromAction(i);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Assert.AreEqual(i, pool.ToAction(idx));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,14 +39,14 @@ public class ActionSetTests
|
||||
Assert.IsTrue(set.IsEmpty);
|
||||
Assert.AreEqual(0, set.Count);
|
||||
|
||||
set.AddAction(ActionType.BasicSynthesis);
|
||||
set.AddAction(ActionType.WasteNot2);
|
||||
set.AddAction(in pool, ActionType.BasicSynthesis);
|
||||
set.AddAction(in pool, ActionType.WasteNot2);
|
||||
|
||||
Assert.AreEqual(2, set.Count);
|
||||
Assert.IsFalse(set.IsEmpty);
|
||||
|
||||
set.RemoveAction(ActionType.BasicSynthesis);
|
||||
set.RemoveAction(ActionType.WasteNot2);
|
||||
set.RemoveAction(in pool, ActionType.BasicSynthesis);
|
||||
set.RemoveAction(in pool, ActionType.WasteNot2);
|
||||
|
||||
Assert.IsTrue(set.IsEmpty);
|
||||
Assert.AreEqual(0, set.Count);
|
||||
@@ -43,17 +57,17 @@ public class ActionSetTests
|
||||
{
|
||||
var set = new ActionSet();
|
||||
|
||||
Assert.IsTrue(set.AddAction(ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.AddAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.AddAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.AddAction(in pool, ActionType.BasicSynthesis));
|
||||
|
||||
Assert.IsTrue(set.RemoveAction(ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.RemoveAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.RemoveAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.RemoveAction(in pool, ActionType.BasicSynthesis));
|
||||
|
||||
Assert.IsTrue(set.AddAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.AddAction(ActionType.WasteNot2));
|
||||
Assert.IsTrue(set.AddAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.AddAction(in pool, ActionType.WasteNot2));
|
||||
|
||||
Assert.IsTrue(set.RemoveAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.RemoveAction(ActionType.WasteNot2));
|
||||
Assert.IsTrue(set.RemoveAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.RemoveAction(in pool, ActionType.WasteNot2));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -61,18 +75,18 @@ public class ActionSetTests
|
||||
{
|
||||
var set = new ActionSet();
|
||||
|
||||
set.AddAction(ActionType.BasicSynthesis);
|
||||
set.AddAction(in pool, ActionType.BasicSynthesis);
|
||||
|
||||
Assert.IsTrue(set.HasAction(ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.HasAction(ActionType.WasteNot2));
|
||||
Assert.IsTrue(set.HasAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsFalse(set.HasAction(in pool, ActionType.WasteNot2));
|
||||
|
||||
set.AddAction(ActionType.WasteNot2);
|
||||
Assert.IsTrue(set.HasAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.HasAction(ActionType.WasteNot2));
|
||||
set.AddAction(in pool, ActionType.WasteNot2);
|
||||
Assert.IsTrue(set.HasAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.HasAction(in pool, ActionType.WasteNot2));
|
||||
|
||||
set.RemoveAction(ActionType.BasicSynthesis);
|
||||
Assert.IsFalse(set.HasAction(ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.HasAction(ActionType.WasteNot2));
|
||||
set.RemoveAction(in pool, ActionType.BasicSynthesis);
|
||||
Assert.IsFalse(set.HasAction(in pool, ActionType.BasicSynthesis));
|
||||
Assert.IsTrue(set.HasAction(in pool, ActionType.WasteNot2));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -80,25 +94,25 @@ public class ActionSetTests
|
||||
{
|
||||
var set = new ActionSet();
|
||||
|
||||
set.AddAction(ActionType.BasicSynthesis);
|
||||
set.AddAction(ActionType.ByregotsBlessing);
|
||||
set.AddAction(ActionType.DelicateSynthesis);
|
||||
set.AddAction(ActionType.Reflect);
|
||||
set.AddAction(in pool, ActionType.BasicSynthesis);
|
||||
set.AddAction(in pool, ActionType.ByregotsBlessing);
|
||||
set.AddAction(in pool, ActionType.DelicateSynthesis);
|
||||
set.AddAction(in pool, ActionType.Reflect);
|
||||
|
||||
Assert.AreEqual(4, set.Count);
|
||||
|
||||
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(0));
|
||||
Assert.AreEqual(ActionType.Reflect, set.ElementAt(1));
|
||||
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(2));
|
||||
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(3));
|
||||
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 0));
|
||||
Assert.AreEqual(ActionType.Reflect, set.ElementAt(in pool, 1));
|
||||
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 2));
|
||||
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 3));
|
||||
|
||||
set.RemoveAction(ActionType.Reflect);
|
||||
set.RemoveAction(in pool, ActionType.Reflect);
|
||||
|
||||
Assert.AreEqual(3, set.Count);
|
||||
|
||||
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(0));
|
||||
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(1));
|
||||
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(2));
|
||||
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 0));
|
||||
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 1));
|
||||
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 2));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -118,13 +132,13 @@ public class ActionSetTests
|
||||
|
||||
var set = new ActionSet();
|
||||
foreach(var action in actions)
|
||||
set.AddAction(action);
|
||||
set.AddAction(in pool, action);
|
||||
|
||||
var counts = new Dictionary<ActionType, int>();
|
||||
var rng = new Random(0);
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var action = set.SelectRandom(rng);
|
||||
var action = set.SelectRandom(in pool, rng);
|
||||
|
||||
CollectionAssert.Contains(actions, action);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user