Initial commit: Client Doc docs Server Tools
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
using XWorld.Framework;
|
||||
|
||||
namespace XWorld.Framework.Tests
|
||||
{
|
||||
public class DeterministicRandomTests
|
||||
{
|
||||
[Fact]
|
||||
public void SameSeed_ProducesIdenticalSequence()
|
||||
{
|
||||
var a = new DeterministicRandom(12345UL);
|
||||
var b = new DeterministicRandom(12345UL);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
Assert.Equal(a.Next(100), b.Next(100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DifferentSeed_DivergesQuickly()
|
||||
{
|
||||
var a = new DeterministicRandom(1UL);
|
||||
var b = new DeterministicRandom(2UL);
|
||||
bool anyDiff = false;
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (a.Next(1_000_000) != b.Next(1_000_000)) { anyDiff = true; break; }
|
||||
Assert.True(anyDiff);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Next_StaysInRange()
|
||||
{
|
||||
var rng = new DeterministicRandom(999UL);
|
||||
for (int i = 0; i < 100_000; i++)
|
||||
{
|
||||
int v = rng.Next(7);
|
||||
Assert.InRange(v, 0, 6);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Next_One_AlwaysZero()
|
||||
{
|
||||
var rng = new DeterministicRandom(42UL);
|
||||
for (int i = 0; i < 100; i++)
|
||||
Assert.Equal(0, rng.Next(1));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-3)]
|
||||
public void Next_NonPositiveMax_Throws(int max)
|
||||
{
|
||||
var rng = new DeterministicRandom(42UL);
|
||||
Assert.Throws<ArgumentOutOfRangeException>((Action)(() => rng.Next(max)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void State_SnapshotAndResume_ContinuesIdentically()
|
||||
{
|
||||
var rng = new DeterministicRandom(7UL);
|
||||
for (int i = 0; i < 10; i++) rng.Next(1000);
|
||||
ulong snapshot = rng.State;
|
||||
|
||||
var resumed = new DeterministicRandom(snapshot);
|
||||
// 注意:State 是当前内部状态,用它重建后序列从"下一步"继续
|
||||
int[] fromOriginal = new int[5];
|
||||
int[] fromResumed = new int[5];
|
||||
for (int i = 0; i < 5; i++) fromOriginal[i] = rng.Next(1000);
|
||||
for (int i = 0; i < 5; i++) fromResumed[i] = resumed.Next(1000);
|
||||
Assert.Equal(fromOriginal, fromResumed);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user