56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using System.IO;
|
|
using Backend;
|
|
using Xunit;
|
|
|
|
namespace Backend.Tests;
|
|
|
|
public class PolicyRunnerTests
|
|
{
|
|
private static string LocateOnnxModel()
|
|
{
|
|
var dir = AppContext.BaseDirectory;
|
|
while (dir is not null)
|
|
{
|
|
var candidate = Path.Combine(dir, "models", "ppo_lander.onnx");
|
|
if (File.Exists(candidate)) return candidate;
|
|
var parent = Directory.GetParent(dir);
|
|
if (parent is null) break;
|
|
dir = parent.FullName;
|
|
}
|
|
throw new FileNotFoundException(
|
|
"models/ppo_lander.onnx not found — run `python Training/export_onnx.py ...`");
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadsWithoutError()
|
|
{
|
|
using var runner = new PolicyRunner(LocateOnnxModel());
|
|
}
|
|
|
|
[Fact]
|
|
public void SelectAction_ReturnsValidActionForZeroObs()
|
|
{
|
|
using var runner = new PolicyRunner(LocateOnnxModel());
|
|
var obs = new float[7]; // all zeros
|
|
int action = runner.SelectAction(obs);
|
|
Assert.InRange(action, 0, 3);
|
|
}
|
|
|
|
[Fact]
|
|
public void SelectAction_IsDeterministicForSameInput()
|
|
{
|
|
using var runner = new PolicyRunner(LocateOnnxModel());
|
|
var obs = new float[] { 0.1f, -0.2f, 0.05f, 0.0f, 0.0f, 1.0f, 0.0f };
|
|
int a1 = runner.SelectAction(obs);
|
|
int a2 = runner.SelectAction(obs);
|
|
Assert.Equal(a1, a2);
|
|
}
|
|
|
|
[Fact]
|
|
public void SelectAction_ThrowsIfObsLengthWrong()
|
|
{
|
|
using var runner = new PolicyRunner(LocateOnnxModel());
|
|
Assert.Throws<ArgumentException>(() => runner.SelectAction(new float[3]));
|
|
}
|
|
}
|