"""Run the shipped shop decoder against malformed and valid wire frames.""" import os,subprocess,tempfile,unittest from pathlib import Path ROOT=Path(__file__).resolve().parents[2] class WireTests(unittest.TestCase): def test_complete_frame_and_no_partial_mutation(self): code=r''' #include "src/port/UserInterface/Ava2ShopWire.h" #include #include #pragma pack(push,1) struct Item { uint32_t vnum; int64_t price; char body[35]; }; struct Price { uint32_t vnum,count; }; struct Frame { uint32_t owner; Item items[40]; Price prices[40]; }; #pragma pack(pop) static_assert(sizeof(Item)==47,"native baseline wire size"); static_assert(sizeof(Frame)==2204,"shop start payload size"); int main(){ Frame frame={};frame.items[0].vnum=25163;frame.items[0].price=100000000000LL;frame.prices[0]={90011,10}; Item items[40]={};Price prices[40]={};uint32_t owner=99; const char* data=reinterpret_cast(&frame); assert(!DecodeAva2ShopStart(data,sizeof(frame)-1,owner,items,prices));assert(owner==99); assert(!DecodeAva2ShopStart(data,sizeof(frame)+1,owner,items,prices));assert(owner==99); assert(!DecodeAva2ShopStart(nullptr,sizeof(frame),owner,items,prices)); frame.prices[1]={90010,0};assert(!DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));assert(items[0].vnum==0); frame.prices[1]={90010,20};assert(!DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));assert(owner==99); frame.prices[1]={0,0};assert(DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices)); assert(items[0].price==100000000000LL); frame.items[0].price=-1;assert(!DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));assert(items[0].price==100000000000LL); assert(owner==0 && items[0].vnum==25163 && prices[0].vnum==90011 && prices[0].count==10); } ''' with tempfile.TemporaryDirectory() as d: p=Path(d);(p/'test.cpp').write_text(code) subprocess.run([os.environ.get('CXX','c++'),'-std=c++17','-I'+str(ROOT),str(p/'test.cpp'),'-o',str(p/'test')],check=True) subprocess.run([str(p/'test')],check=True) if __name__=='__main__':unittest.main()