Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/CloudWeatherTests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1#pragma once
2#include "../CloudWeatherReceiver.h"
3#include <fstream>
4
5static CloudWeather::MapPtr ConstantWeather(uint8_t value,unsigned width=2048,uint64_t revision=1)
6{
7 auto map=std::make_shared<CloudWeather::Map>(); map->revision=revision;
8 map->mips.push_back({width,width/2,std::vector<uint8_t>(size_t(width)*width/2,value)});
9 CloudWeather::BuildMips(*map); return map;
10}
11static CloudWeather::MapPtr RegionalWeatherFixture()
12{
13 auto map=std::make_shared<CloudWeather::Map>(); map->revision=10;
14 CloudWeather::Mip m; m.width=2048; m.height=1024; m.pixels.resize(2048*1024);
15 for(unsigned y=0;y<m.height;++y) for(unsigned x=0;x<m.width;++x)
16 m.pixels[y*m.width+x]=uint8_t(127.5+70*std::sin(x*6.28318530718/128)
17 +40*std::sin(x*6.28318530718/32+y*.06));
18 map->mips.push_back(std::move(m)); CloudWeather::BuildMips(*map); return map;
19}
20static std::vector<std::vector<uint8_t>> WeatherPackets(const CloudWeather::MapPtr& map,bool rle)
21{
22 using namespace CloudWeather;
23 const auto& base=map->mips.front(); std::vector<uint8_t> encoded;
24 const auto put=[](std::vector<uint8_t>& p,size_t offset,uint64_t n,unsigned bytes) {
25 for(unsigned i=0;i<bytes;++i) p[offset+i]=uint8_t(n>>(8*i));
26 };
27 if (rle)
28 {
29 for(size_t i=0;i<base.pixels.size();)
30 {
31 size_t end=i+1;
32 while(end<base.pixels.size() && end-i<65535 && base.pixels[end]==base.pixels[i]) ++end;
33 encoded.push_back(uint8_t(end-i)); encoded.push_back(uint8_t((end-i)>>8)); encoded.push_back(base.pixels[i]); i=end;
34 }
35 }
36 else encoded=base.pixels;
37 std::vector<std::vector<uint8_t>> packets;
38 const auto crc=Crc32(base.pixels);
39 for(unsigned offset=0;offset<encoded.size();offset+=ChunkBytes)
40 {
41 const auto count=(std::min)(ChunkBytes,unsigned(encoded.size()-offset));
42 std::vector<uint8_t> p(HeaderBytes+count);
43 p[0]=145; p[1]=1; p[2]=rle?1:0; p[3]=1; p[38]=p[39]=1;
44 put(p,4,base.width,2); put(p,6,base.height,2); put(p,8,map->revision,8); put(p,16,map->simulationTimeMs,8);
45 put(p,24,base.pixels.size(),4); put(p,28,encoded.size(),4); put(p,32,offset,4); put(p,36,count,2); put(p,40,crc,4);
46 std::copy(encoded.begin()+offset,encoded.begin()+offset+count,p.begin()+HeaderBytes); packets.push_back(std::move(p));
47 }
48 return packets;
49}
50static void CloudWeatherTests()
51{
52 using namespace CloudWeather;
53 Check(Crc32(std::vector<uint8_t>{'1','2','3','4','5','6','7','8','9'})==0xcbf43926u,"IEEE CRC32 known vector");
54 Frame empty;
55 Check(Sample(empty,1,0,0)==0 && Sample(empty,0,0,1)==0,"startup convection is zero worldwide");
56 float u,v;
57 UV(0,1,0,u,v); Check(std::abs(u-.5f)<1e-6f && std::abs(v-.5f)<1e-6f,"prime meridian geography");
58 UV(-1,0,0,u,v); Check(std::abs(u-.75f)<1e-6f,"east longitude orientation");
59 UV(0,0,1,u,v); Check(v==0,"north pole orientation");
60 UV(0,0,-1,u,v); Check(v==1,"south pole orientation");
61 const auto map=RegionalWeatherFixture();
62 Check(std::abs(Sample(map,-.0001f,.5f)-Sample(map,.9999f,.5f))<.0001f,"longitude wraps across dateline");
63 Check(Sample(map,.5f,-1)==Sample(map,.5f,0),"latitude clamps at polar row");
64 auto packets=WeatherPackets(map,false);
65 Assembler assembler; MapPtr completed;
66 // Reordered and duplicate chunks, with no partial publication.
67 for(size_t i=packets.size();i-->0;)
68 {
69 auto result=assembler.Accept(packets[i].data(),packets[i].size());
70 if(i) Check(!result,"incomplete global image never publishes"); else completed=result;
71 Check(!assembler.Accept(packets[i].data(),packets[i].size()),"duplicate chunks do not republish");
72 }
73 Check(completed && completed->mips.front().pixels==map->mips.front().pixels,"raw snapshot exact round trip");
74 Check(completed->mips.back().pixels.size()==1,"complete mip chain generated off thread");
75 Header h;
76 for(unsigned size=0;size<HeaderBytes;++size) Check(!Parse(packets[0].data(),size,h),"truncated header rejected");
77 for(unsigned offset : {1u,2u,3u,4u,6u,24u,28u,32u,36u,38u,39u,44u})
78 {
79 auto bad=packets[0]; bad[offset]=255;
80 Check(!Parse(bad.data(),bad.size(),h),"unsupported or malformed header rejected");
81 }
82 auto full=ConstantWeather(255,4096,11);
83 const auto compressed=WeatherPackets(full,true);
84 Check(compressed.size()==1,"RLE efficiently represents full-resolution uniform map");
85 completed=assembler.Accept(compressed[0].data(),compressed[0].size());
86 Check(completed && completed->mips.front().pixels==full->mips.front().pixels,"4096x2048 RLE snapshot round trip");
87 Check(!assembler.Accept(packets[0].data(),packets[0].size()),"stale revision cannot replace latest");
88 for(int fault=0;fault<4;++fault)
89 {
90 Assembler invalid; auto bad=compressed[0];
91 if(fault==0) bad[40]^=1; // wrong CRC
92 if(fault==1) bad[48]=bad[49]=0; // zero run
93 if(fault==2) bad.back()^=1; // corrupt data
94 if(fault==3) { bad.push_back(1); bad[36]++; } // trailing RLE byte
95 Check(!invalid.Accept(bad.data(),bad.size()),"corrupt compressed snapshot cannot publish");
96 }
97 Frame halfway{{},full,.5f}; Check(std::abs(Sample(halfway,0,1,0)-.5f)<1e-6f,"initial map blends from black");
98 Receiver receiver;
99 Check(!receiver.Latest(),"receiver starts empty");
100 receiver.Submit(compressed[0].data(),compressed[0].size());
101 const auto deadline=std::chrono::steady_clock::now()+std::chrono::seconds(5);
102 while(!receiver.Latest() && std::chrono::steady_clock::now()<deadline) std::this_thread::sleep_for(std::chrono::milliseconds(1));
103 Check(receiver.Latest() && receiver.Latest()->revision==11,"worker publishes complete decoded image");
104 receiver.Reset(); Check(!receiver.Latest(),"reconnect discards previous server data");
105 receiver.Submit(compressed[0].data(),compressed[0].size()); receiver.Reset();
106 std::this_thread::sleep_for(std::chrono::milliseconds(100));
107 Check(!receiver.Latest(),"old worker job cannot publish after reset");
108 // Independent C# producer fixture, including a split RLE triple.
109 Assembler crossLanguage;
110 for(int chunk=0;chunk<2;++chunk)
111 {
112 const auto path=std::string("Tests/Fixtures/CloudWeatherV1/chunk-")+char('0'+chunk)+".bin";
113 std::ifstream fixture(path,std::ios::binary);
114 Check(bool(fixture),"C# interoperability fixture is present");
115 std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(fixture)),{});
116 const auto result=crossLanguage.Accept(bytes.data(),bytes.size());
117 if(!chunk) Check(!result,"split server snapshot stays unpublished");
118 else
119 {
120 Check(result && result->revision==1790000000000ull && result->simulationTimeMs==123456789,"C# metadata decodes in C++");
121 const auto& p=result->mips.front().pixels;
122 Check(p[0]==0 && p[2047]==28 && p[512*2048+1024]==144 && p[1023*2048]==224 && p.back()==252,
123 "C# RLE cross-chunk fixture has exact expected convection pixels");
124 }
125 }
126}
MapPtr Accept(const uint8_t *packet, size_t size)
bool Submit(const uint8_t *packet, size_t size)
constexpr unsigned ChunkBytes
constexpr unsigned HeaderBytes
uint32_t Crc32(const std::vector< uint8_t > &bytes)
float Sample(const MapPtr &map, float u, float v)
bool Parse(const uint8_t *p, size_t length, Header &h)
std::shared_ptr< const Map > MapPtr
void UV(float x, float y, float z, float &u, float &v)
void BuildMips(Map &map)
path
Definition erin.py:153
std::vector< uint8_t > pixels