Files
SVSimServer/SVSim.BattleEngine.Tests/RngSeamTests.cs

56 lines
2.7 KiB
C#

using System;
using NUnit.Framework;
using SVSim.BattleEngine.Rng;
namespace SVSim.BattleEngine.Tests
{
[TestFixture]
public class RngSeamTests
{
// RandomSourceBridge.Range must mirror the engine's exact roll arithmetic:
// BattleManagerBase.StableRandom does `(int)Math.Floor((double)val * unit)`.
[Test]
public void Bridge_Range_mirrors_engine_floor_arithmetic()
{
Assert.That(RandomSourceBridge.Range(7, 0.0), Is.EqualTo(0)); // floor(7*0) = 0
Assert.That(RandomSourceBridge.Range(7, 0.999), Is.EqualTo(6)); // floor(6.993) = 6 (never == val)
Assert.That(RandomSourceBridge.Range(3, 0.5), Is.EqualTo(1)); // floor(1.5) = 1 (middle of 3)
Assert.That(RandomSourceBridge.Range(1, 0.5), Is.EqualTo(0)); // floor(0.5) = 0
}
// SeededRandomSource(seed) must reproduce the engine's own generators EXACTLY: BattleManagerBase
// seeds both _stableRandom and _stableRandomOnlySelf as `new System.Random(RandomSeed)`
// (BattleManagerBase.cs:721-722). NextUnit() == synced.NextDouble(); NextSelf(max) == self.Next(max).
[Test]
public void SeededSource_reproduces_two_System_Random_streams()
{
const int seed = 12345;
var src = new SeededRandomSource(seed);
var refSynced = new System.Random(seed); // mirrors _stableRandom
var refSelf = new System.Random(seed); // mirrors _stableRandomOnlySelf (separate stream)
for (int i = 0; i < 8; i++)
Assert.That(src.NextUnit(), Is.EqualTo(refSynced.NextDouble()), $"NextUnit drift at {i}");
for (int i = 0; i < 8; i++)
Assert.That(src.NextSelf(100), Is.EqualTo(refSelf.Next(100)), $"NextSelf drift at {i}");
}
// ScriptedRandomSource feeds a known sequence (the oracle's control + the Phase-3 replay seam).
// It MUST throw on overrun, not wrap: an unexpected extra roll should fail loudly so a test
// surfaces a miscount of engine RNG calls rather than silently reusing a value.
[Test]
public void ScriptedSource_returns_sequence_then_throws_on_overrun()
{
var src = new ScriptedRandomSource(new[] { 0.1, 0.5 }, new[] { 3 });
Assert.That(src.NextUnit(), Is.EqualTo(0.1));
Assert.That(src.NextUnit(), Is.EqualTo(0.5));
Assert.That(() => src.NextUnit(), Throws.InvalidOperationException, "should throw on unit overrun");
Assert.That(src.NextSelf(99), Is.EqualTo(3));
Assert.That(() => src.NextSelf(99), Throws.InvalidOperationException, "should throw on self overrun");
}
}
}