Files
Neutron/tests/GamePort.PairLifecycleTests/Program.cs
T

191 lines
9.8 KiB
C#

using System.Numerics;
using System.Text.Json;
using GamePort.Game;
var fixture = new Fixture();
var rows = new List<Observation>();
void Tick(string name) => rows.Add(fixture.Tick(name));
fixture.Reset(0,1);Tick("enter");Tick("stay");fixture.Actors[1].Position.X=20;Tick("tangent_leave");
fixture.Actors[0].Integration=false;fixture.Actors[0].Authored=true;fixture.Actors[1].Position.X=19;Tick("reenter_sleeping");
fixture.Actors[1].Position.X=21;Tick("leave_wakes_authored");
fixture.Reset(0,1);fixture.Actors[0].Exclusions.Add(fixture.Actors[1]);Tick("earlier_excludes");
fixture.Reset(0,1);fixture.Actors[1].Exclusions.Add(fixture.Actors[0]);Tick("later_excludes");
fixture.Order(1,0);Tick("reordered_exclusion_leaves");
fixture.Reset(0,1);Tick("before_disable");fixture.Actors[1].Enabled=false;Tick("disabled_retains_contact");
fixture.Actors[1].Enabled=true;fixture.Actors[0].Allowed=false;Tick("permission_retains_contact");
fixture.Actors[0].Allowed=true;fixture.Enabled=false;Tick("global_off_retains_contact");
fixture.Reset(0,1);fixture.Actors[1].Allowed=false;Tick("later_permission_rejects");
fixture.Reset(0,1);fixture.Actors[1].Position.X=19.999f;Tick("inside");
fixture.Reset(0,1);fixture.Actors[1].Position.X=20.001f;Tick("outside");
fixture.Reset(0,1,2);Tick("three_actor_order");
fixture.Reset(0,1,2);fixture.Mutation=1;Tick("response_moves_root");
fixture.Reset(0,1,2);fixture.Mutation=2;Tick("enter_disables_outer");
fixture.Reset(0,1,2);fixture.Mutation=3;Tick("enter_disables_later");
fixture.Reset(0,1);fixture.Mutation=4;Tick("enter_changes_counterpart_membership");
fixture.Reset(0,0,1);Tick("duplicate_body_identity");
fixture.Reset(0,1);fixture.Actors[1].Inhibit=true;Tick("other_inhibits_response");
fixture.Reset(0,1);fixture.Actors[0].Integration=false;Tick("sleeping_still_contacts");
fixture.Reset(0,1);fixture.Actors[0].Mode=2;Tick("box_first");
fixture.Reset(0,1);fixture.Actors[1].Mode=2;Tick("box_second");
fixture.Reset(0,1);fixture.Actors[0].Mode=fixture.Actors[1].Mode=2;Tick("both_box_keep_order");
fixture.BoxHit=false;Tick("box_separation");
fixture.Reset(0);Tick("single_actor");
if (args.Length == 2 && args[0] == "--native")
{
using var original=JsonDocument.Parse(File.ReadAllText(args[1]));
var references=original.RootElement.GetProperty("cases").EnumerateArray().ToArray();
if(references.Length!=rows.Count)throw new Exception("Original case count");
for(var i=0;i<rows.Count;i++)
{
var row=rows[i];var expected=references[i];
Check(row.Name==expected.GetProperty("name").GetString(),row.Name+" identity");
Check(row.Events==expected.GetProperty("events").GetString(),row.Name+" callback order");
foreach(var (field,actual) in new[]{("contacts",row.Contacts),("integration",row.Integration),("enabled",row.Enabled)})
Check(actual.SequenceEqual(expected.GetProperty(field).EnumerateArray().Select(v=>v.GetInt32())),row.Name+" "+field);
Check(row.RootX.SequenceEqual(expected.GetProperty("rootX").EnumerateArray().Select(v=>v.GetSingle())),row.Name+" roots");
}
Console.WriteLine($"Matched {rows.Count} complete original pair observations.");
}
else if(args.Length!=0)throw new ArgumentException("Use --native <observations.json>");
//Fixed reference expectations are checked even when generated traces are absent.
var events = new Dictionary<string,string>
{
["enter"]="R01;P01;R10;P10;E01;E10;",
["stay"]="R01;P01;R10;P10;S01;S10;",
["tangent_leave"]="L01;L10;",
["reenter_sleeping"]="R01;R10;P10;E01;E10;",
["leave_wakes_authored"]="L01;L10;",
["earlier_excludes"]="",
["later_excludes"]="R01;P01;R10;P10;E01;E10;",
["reordered_exclusion_leaves"]="L10;L01;",
["before_disable"]="R01;P01;R10;P10;E01;E10;",
["disabled_retains_contact"]="",
["permission_retains_contact"]="",
["global_off_retains_contact"]="",
["later_permission_rejects"]="",
["inside"]="R01;P01;R10;P10;E01;E10;",
["outside"]="",
["three_actor_order"]="R01;P01;R10;P10;E01;E10;R02;P02;R20;P20;E02;E20;R12;P12;R21;P21;E12;E21;",
["response_moves_root"]="R01;P01;R10;P10;E01;E10;R12;P12;R21;P21;E12;E21;",
["enter_disables_outer"]="R01;P01;R10;P10;E01;E10;R02;P02;R20;P20;E02;E20;R12;P12;R21;P21;E12;E21;",
["enter_disables_later"]="R01;P01;R10;P10;E01;E10;",
["enter_changes_counterpart_membership"]="R01;P01;R10;P10;E01;S10;",
["duplicate_body_identity"]="R01;P01;R10;P10;E01;E10;R01;P01;R10;P10;S01;S10;",
["other_inhibits_response"]="R01;R10;P10;E01;E10;",
["sleeping_still_contacts"]="R01;R10;P10;E01;E10;",
["box_first"]="B01;E01;E10;",
["box_second"]="B10;E01;E10;",
["both_box_keep_order"]="B01;E01;E10;",
["box_separation"]="B01;L01;L10;",
["single_actor"]="",
};
foreach(var row in rows)
{
if(events.TryGetValue(row.Name,out var expected))Check(row.Events==expected,row.Name+" reference order");
else throw new Exception("Missing saved reference: "+row.Name);
//Complete finite state observations, retained independently of the local capture.
int[] contacts=row.Name switch
{
"three_actor_order" or "enter_disables_outer"=>[6,5,3],
"response_moves_root"=>[2,5,2],
"tangent_leave" or "leave_wakes_authored" or "earlier_excludes" or
"reordered_exclusion_leaves" or "later_permission_rejects" or "outside" or
"box_separation" or "single_actor"=>[0,0,0],
_=>[2,1,0]
};
int[] integration=row.Name is "reenter_sleeping" or "sleeping_still_contacts" ? [0,1,1] : [1,1,1];
int[] enabled=row.Name switch
{
"disabled_retains_contact"=>[1,0,1],"enter_disables_outer"=>[0,1,1],
"enter_disables_later"=>[1,1,0],_=>[1,1,1]
};
float[] roots=row.Name switch
{
"tangent_leave"=>[0,20,0],"reenter_sleeping"=>[0,19,0],"leave_wakes_authored"=>[0,21,0],
"inside"=>[0,19.999f,0],"outside"=>[0,20.001f,0],"response_moves_root"=>[1000,0,0],_=>[0,0,0]
};
Check(row.Contacts.SequenceEqual(contacts)&&row.Integration.SequenceEqual(integration)
&&row.Enabled.SequenceEqual(enabled)&&row.RootX.SequenceEqual(roots),row.Name+" complete saved state");
}
Check(rows.Single(r=>r.Name=="disabled_retains_contact").Contacts.SequenceEqual(new[]{2,1,0}),"disabled contact retention");
Check(rows.Single(r=>r.Name=="global_off_retains_contact").Contacts.SequenceEqual(new[]{2,1,0}),"global contact retention");
Check(rows.Single(r=>r.Name=="leave_wakes_authored").Integration[0]==1,"leave wakes authored integration");
Check(rows.Single(r=>r.Name=="response_moves_root").RootX[0]==1000,"response publishes live root before next pair");
fixture.Reset(0,1);fixture.Actors[0].Mode=2;
try {OriginalPairCollision.Step(fixture.Bodies);throw new Exception("Missing box solver accepted");}
catch(InvalidOperationException) { }
Console.WriteLine("PASS:28 pair lifecycle cases, retained contacts, callback mutations and explicit box admission.");
static void Check(bool condition,string message){if(!condition)throw new Exception(message);}
sealed record Observation(string Name,string Events,int[] Contacts,int[] Integration,int[] Enabled,float[] RootX);
sealed class Fixture
{
public Actor[] Actors {get;}
public IOriginalPairBody[] Bodies {get;private set;}=[];
public string Events="";
public int Mutation;
public bool Enabled=true,BoxHit=true;
public Fixture()=>Actors=Enumerable.Range(0,3).Select(i=>new Actor(this,i)).ToArray();
public void Reset(params int[] order)
{
foreach(var actor in Actors)actor.Reset();
Mutation=0;Enabled=BoxHit=true;Order(order);
}
public void Order(params int[] order)=>Bodies=order.Select(i=>(IOriginalPairBody)Actors[i]).ToArray();
public Observation Tick(string name)
{
Events="";
OriginalPairCollision.Step(Bodies,(a,b)=>{Log('B',(Actor)a,(Actor)b);return BoxHit;},Enabled);
return new(name,Events,Actors.Select(a=>a.Contacts.Aggregate(0,(mask,b)=>mask|1<<b.Id)).ToArray(),
Actors.Select(a=>a.Integration?1:0).ToArray(),Actors.Select(a=>a.Enabled?1:0).ToArray(),Actors.Select(a=>a.Position.X).ToArray());
}
public void Log(char kind,Actor a,Actor b)=>Events+=$"{kind}{a.Id}{b.Id};";
}
sealed class Actor(Fixture fixture,int id):IOriginalPairBody
{
public int Id=>id;
public bool Enabled,Allowed,Integration,Authored,Inhibit;
public int Mode;
public Vector3 Position;
public HashSet<Actor> Contacts=[];
public HashSet<Actor> Exclusions=[];
public bool PairCollisionEnabled=>Enabled;
public int CollisionMode=>Mode;
public Vector3 Root=>Position;
public float Radius=>10;
public bool AllowsPair(IOriginalPairBody other)=>Allowed;
public bool Excludes(IOriginalPairBody other)=>Exclusions.Contains((Actor)other);
public bool HasContact(IOriginalPairBody other)=>Contacts.Contains((Actor)other);
public void Reset(){Enabled=Allowed=Integration=true;Authored=Inhibit=false;Mode=0;Position=default;Contacts.Clear();Exclusions.Clear();}
public void SphereOverlap(IOriginalPairBody other,float distance)
{
var b=(Actor)other;fixture.Log('R',this,b);
//Actual470BF0 invokes414 only if the other body's1E0 is false and own210 is true.
if(!b.Inhibit&&Integration)
{
fixture.Log('P',this,b);
if(fixture.Mutation==1&&id==0)Position.X=1000;
}
}
public void Enter(IOriginalPairBody other)
{
var b=(Actor)other;fixture.Log('E',this,b);Contacts.Add(b);
if(id==0&&b.Id==1)
switch(fixture.Mutation)
{
case 2: Enabled=false;break;
case 3: fixture.Actors[2].Enabled=false;break;
case 4: b.Contacts.Add(this);break;
}
}
public void Stay(IOriginalPairBody other)=>fixture.Log('S',this,(Actor)other);
public void Leave(IOriginalPairBody other)
{
var b=(Actor)other;fixture.Log('L',this,b);
if(!Integration&&Authored)Integration=true;
Contacts.Remove(b);
}
}