using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
static uint GalaxyCellsSelfTest()
{
static JsonElement Request(string text){using var d=JsonDocument.Parse(text);return d.RootElement.Clone();}
static bool Rejected(Action action){try{action();return false;}catch{return true;}}
var memory=Platform.Allocate(0x2000);if(memory==0)return 0;
lock(galaxyMutex)
{
var saved=galaxyWork;var savedGeneration=galaxyGeneration;
try
{
uint passed=0;var services=new GalaxyServices{origin=[0,0,0]};
GalaxyWork Parse(string ids,string radii="1,1,1,1,1,1,1,1")
{var work=new GalaxyWork{cellMode=true,region=true,centre=[0,0,0]};ParseGalaxyRequestedCells(work,Request("{\"radii\":["+radii+"],\"cellAddresses\":["+ids+"]}"));return work;}
var work=Parse("\"0\",\"7\"");
if(work.cells.Count==2&&work.minimumMass==0&&ValidateGalaxyRequestedCells(work,services))passed|=1;
if(Rejected(()=>Parse("\"17592186044416\""))&&Rejected(()=>Parse("\"0\",\"0\""))&&Rejected(()=>Parse("0")))passed|=2;
var validMasks=true;
for(var mass=0;mass<8;mass++)
{
var cell=((ulong)3<<(30-2*mass))|((ulong)2<<(17-mass))|(5UL<<3)|(uint)mass;
var system=cell|(123UL<<(44-3*mass));validMasks&=GalaxyCanonicalCellAddress(system)==cell;
}
if(validMasks)passed|=4;
if(Rejected(()=>Parse("\"0\"","1,1,1,1,1,1,1"))&&Rejected(()=>Parse("\"0\"","-1,1,1,1,1,1,1,1"))&&
Rejected(()=>Parse("\"0\"","1e309,1,1,1,1,1,1,1"))&&Rejected(()=>Parse("\"0\"","200001,1,1,1,1,1,1,1")))passed|=8;
if(Rejected(()=>Parse(string.Join(',',Enumerable.Range(0,513).Select(i=>"\""+((ulong)i<<3)+"\"")))))passed|=16;
var tangent=Parse("\"2147483648\"","10,10,10,10,10,10,10,10");tangent.centre=[10,0,0];
if(!ValidateGalaxyRequestedCells(tangent,services))passed|=32;
var cellObject=memory;var cellData=memory+0x100;var rows=memory+0x200;var compact=memory+0x300;var name=memory+0x400;
Write(cellObject+0x30,cellData);Write(cellData+0x30,rows);Write(cellData+0x38,1UL);Write(rows,compact);
Write(compact,1UL<<44);Write(compact+8,name);Write(compact+0x40,(byte)6);
Write(compact+0x42,(ushort)319);Write(compact+0x44,(ushort)319);Write(compact+0x46,(ushort)319);
Platform.WriteBytes(name,Encoding.UTF8.GetBytes("FixtureCellCorner\0"));
work=Parse("\"0\"");work.cellObject=cellObject;var copied=new List<GalaxyItem>();
var before=Platform.ReadBytes(memory,0x1000);
if(CopyGalaxyRegionRows(services,work,copied,out var finished)&&finished&&copied.Count==1&&copied[0].position.All(v=>v>9)&&
Platform.ReadBytes(memory,0x1000).AsSpan().SequenceEqual(before))passed|=64;
var sphere=new GalaxyWork{centre=[0,0,0],radius=1,cellObject=cellObject};copied=[];
if(CopyGalaxyRegionRows(services,sphere,copied,out finished)&&finished&&copied.Count==0)passed|=128;
Write(compact,(1UL<<44)|8);work.entry=0;copied=[];
if(CopyGalaxyRegionRows(services,work,copied,out finished)&&finished&&copied.Count==0&&work.failedRows==1)passed|=256;
galaxyWork=Parse("\"7\"");galaxyWork.generation=91;galaxyWork.done=true;galaxyWork.cellIndex=1;galaxyWork.origin=[1,2,3];
JsonElement Result(string suffix="")=>Request(GalaxyCatalogueRequest("galaxy.result",Request("{\"generation\":91,\"limit\":96"+suffix+"}")).ToJsonString());
var complete=Result();
if(complete.GetProperty("scope").GetString()=="requested-cells"&&complete.GetProperty("requestedCells").GetInt32()==1&&
complete.GetProperty("complete").GetBoolean()&&complete.GetProperty("minimumMassCode").GetInt32()==7&&complete.GetProperty("origin")[1].GetDouble()==2)passed|=512;
galaxyWork.items=Enumerable.Range(0,97).Select(i=>new GalaxyItem{id=(1UL<<44)+(ulong)i*(1UL<<23)+7,name="Fixture "+i,position=[1000,1000,1000]}).ToList();
if(Result().GetProperty("items").GetArrayLength()==96&&Result(",\"offset\":96").GetProperty("items").GetArrayLength()==1)passed|=1024;
galaxyWork.failedCells=1;
var failed=!Result().GetProperty("complete").GetBoolean();galaxyWork.failedCells=0;galaxyWork.cellIndex=0;
if(failed&&Result().GetProperty("truncated").GetBoolean()&&!Result().GetProperty("complete").GetBoolean())passed|=2048;
galaxyWork=new(){done=true};galaxyGeneration=91;
var accepted=Request(GalaxyCatalogueRequest("galaxy.cells",Request("{\"centre\":[0,0,0],\"radii\":[1,1,1,1,1,1,1,1],\"cellAddresses\":[\"7\"]}")).ToJsonString());
if(accepted.GetProperty("generation").GetUInt64()==92&&accepted.GetProperty("scope").GetString()=="requested-cells"&&
accepted.GetProperty("origin").ValueKind==JsonValueKind.Null&&galaxyWork.cellMode&&galaxyWork.cells.SequenceEqual(new[]{7UL}))passed|=4096;
galaxyWork.done=true;
var old=galaxyWork;var generation=galaxyGeneration;
if(Rejected(()=>GalaxyCatalogueRequest("galaxy.cells",Request("{\"centre\":[0,0,0],\"radii\":[1],\"cellAddresses\":[\"7\"]}")))&&
ReferenceEquals(old,galaxyWork)&&generation==galaxyGeneration)passed|=8192;
galaxyWork.done=false;
var cancelled=Request(GalaxyCatalogueRequest("galaxy.cancel",Request("{\"generation\":92}")).ToJsonString());
if(cancelled.GetProperty("cancelled").GetBoolean()&&!cancelled.GetProperty("complete").GetBoolean())passed|=16384;
var zero=Parse("\"7\"","0,0,0,0,0,0,0,0");
if(!ValidateGalaxyRequestedCells(zero,services))passed|=32768;
return passed;
}
finally{galaxyWork=saved;galaxyGeneration=savedGeneration;Platform.Free(memory);}
}
}
#if PLUGIN_NAVIGATION
[UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyCellsSelfTest")]
internal static uint ExportGalaxyCellsSelfTest()=>RunTest(GalaxyCellsSelfTest);
#endif
}
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
static uint GalaxyCellsSelfTest()
{
static JsonElement Request(string text){using var d=JsonDocument.Parse(text);return d.RootElement.Clone();}
static bool Rejected(Action action){try{action();return false;}catch{return true;}}
var memory=Platform.Allocate(0x2000);if(memory==0)return 0;
lock(galaxyMutex)
{
var saved=galaxyWork;var savedGeneration=galaxyGeneration;
try
{
uint passed=0;var services=new GalaxyServices{origin=[0,0,0]};
GalaxyWork Parse(string ids,string radii="1,1,1,1,1,1,1,1")
{var work=new GalaxyWork{cellMode=true,region=true,centre=[0,0,0]};ParseGalaxyRequestedCells(work,Request("{\"radii\":["+radii+"],\"cellAddresses\":["+ids+"]}"));return work;}
var work=Parse("\"0\",\"7\"");
if(work.cells.Count==2&&work.minimumMass==0&&ValidateGalaxyRequestedCells(work,services))passed|=1;
if(Rejected(()=>Parse("\"17592186044416\""))&&Rejected(()=>Parse("\"0\",\"0\""))&&Rejected(()=>Parse("0")))passed|=2;
var validMasks=true;
for(var mass=0;mass<8;mass++)
{
var cell=((ulong)3<<(30-2*mass))|((ulong)2<<(17-mass))|(5UL<<3)|(uint)mass;
var system=cell|(123UL<<(44-3*mass));validMasks&=GalaxyCanonicalCellAddress(system)==cell;
}
if(validMasks)passed|=4;
if(Rejected(()=>Parse("\"0\"","1,1,1,1,1,1,1"))&&Rejected(()=>Parse("\"0\"","-1,1,1,1,1,1,1,1"))&&
Rejected(()=>Parse("\"0\"","1e309,1,1,1,1,1,1,1"))&&Rejected(()=>Parse("\"0\"","200001,1,1,1,1,1,1,1")))passed|=8;
if(Rejected(()=>Parse(string.Join(',',Enumerable.Range(0,513).Select(i=>"\""+((ulong)i<<3)+"\"")))))passed|=16;
var tangent=Parse("\"2147483648\"","10,10,10,10,10,10,10,10");tangent.centre=[10,0,0];
if(!ValidateGalaxyRequestedCells(tangent,services))passed|=32;
var cellObject=memory;var cellData=memory+0x100;var rows=memory+0x200;var compact=memory+0x300;var name=memory+0x400;
Write(cellObject+0x30,cellData);Write(cellData+0x30,rows);Write(cellData+0x38,1UL);Write(rows,compact);
Write(compact,1UL<<44);Write(compact+8,name);Write(compact+0x40,(byte)6);
Write(compact+0x42,(ushort)319);Write(compact+0x44,(ushort)319);Write(compact+0x46,(ushort)319);
Platform.WriteBytes(name,Encoding.UTF8.GetBytes("FixtureCellCorner\0"));
work=Parse("\"0\"");work.cellObject=cellObject;var copied=new List<GalaxyItem>();
var before=Platform.ReadBytes(memory,0x1000);
if(CopyGalaxyRegionRows(services,work,copied,out var finished)&&finished&&copied.Count==1&&copied[0].position.All(v=>v>9)&&
Platform.ReadBytes(memory,0x1000).AsSpan().SequenceEqual(before))passed|=64;
var sphere=new GalaxyWork{centre=[0,0,0],radius=1,cellObject=cellObject};copied=[];
if(CopyGalaxyRegionRows(services,sphere,copied,out finished)&&finished&&copied.Count==0)passed|=128;
Write(compact,(1UL<<44)|8);work.entry=0;copied=[];
if(CopyGalaxyRegionRows(services,work,copied,out finished)&&finished&&copied.Count==0&&work.failedRows==1)passed|=256;
galaxyWork=Parse("\"7\"");galaxyWork.generation=91;galaxyWork.done=true;galaxyWork.cellIndex=1;galaxyWork.origin=[1,2,3];
JsonElement Result(string suffix="")=>Request(GalaxyCatalogueRequest("galaxy.result",Request("{\"generation\":91,\"limit\":96"+suffix+"}")).ToJsonString());
var complete=Result();
if(complete.GetProperty("scope").GetString()=="requested-cells"&&complete.GetProperty("requestedCells").GetInt32()==1&&
complete.GetProperty("complete").GetBoolean()&&complete.GetProperty("minimumMassCode").GetInt32()==7&&complete.GetProperty("origin")[1].GetDouble()==2)passed|=512;
galaxyWork.items=Enumerable.Range(0,97).Select(i=>new GalaxyItem{id=(1UL<<44)+(ulong)i*(1UL<<23)+7,name="Fixture "+i,position=[1000,1000,1000]}).ToList();
if(Result().GetProperty("items").GetArrayLength()==96&&Result(",\"offset\":96").GetProperty("items").GetArrayLength()==1)passed|=1024;
galaxyWork.failedCells=1;
var failed=!Result().GetProperty("complete").GetBoolean();galaxyWork.failedCells=0;galaxyWork.cellIndex=0;
if(failed&&Result().GetProperty("truncated").GetBoolean()&&!Result().GetProperty("complete").GetBoolean())passed|=2048;
galaxyWork=new(){done=true};galaxyGeneration=91;
var accepted=Request(GalaxyCatalogueRequest("galaxy.cells",Request("{\"centre\":[0,0,0],\"radii\":[1,1,1,1,1,1,1,1],\"cellAddresses\":[\"7\"]}")).ToJsonString());
if(accepted.GetProperty("generation").GetUInt64()==92&&accepted.GetProperty("scope").GetString()=="requested-cells"&&
accepted.GetProperty("origin").ValueKind==JsonValueKind.Null&&galaxyWork.cellMode&&galaxyWork.cells.SequenceEqual(new[]{7UL}))passed|=4096;
galaxyWork.done=true;
var old=galaxyWork;var generation=galaxyGeneration;
if(Rejected(()=>GalaxyCatalogueRequest("galaxy.cells",Request("{\"centre\":[0,0,0],\"radii\":[1],\"cellAddresses\":[\"7\"]}")))&&
ReferenceEquals(old,galaxyWork)&&generation==galaxyGeneration)passed|=8192;
galaxyWork.done=false;
var cancelled=Request(GalaxyCatalogueRequest("galaxy.cancel",Request("{\"generation\":92}")).ToJsonString());
if(cancelled.GetProperty("cancelled").GetBoolean()&&!cancelled.GetProperty("complete").GetBoolean())passed|=16384;
var zero=Parse("\"7\"","0,0,0,0,0,0,0,0");
if(!ValidateGalaxyRequestedCells(zero,services))passed|=32768;
return passed;
}
finally{galaxyWork=saved;galaxyGeneration=savedGeneration;Platform.Free(memory);}
}
}
#if PLUGIN_NAVIGATION
[UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyCellsSelfTest")]
internal static uint ExportGalaxyCellsSelfTest()=>RunTest(GalaxyCellsSelfTest);
#endif
}