Files
WoW-Hybrid-gateway/tests/native_weather_route_test.cpp

54 lines
3.0 KiB
C++

// Source Weather::GetWeatherState and native Weather::Write contract coverage.
#include "world_translation.hpp"
#include <bit>
#include <cmath>
#include <iostream>
#include <limits>
using namespace gateway;
namespace {
unsigned checks=0;
void check(bool ok,const char* why){++checks;if(!ok)throw Error(why);}
Bytes source(uint32_t type,float grade,uint32_t sound,uint8_t instant){
Bytes out;le(out,type,4);le(out,std::bit_cast<uint32_t>(grade),4);le(out,sound,4);le(out,instant,1);return out;
}
Bytes native(uint32_t state,float grade,bool instant){
Bytes out;le(out,1,4);le(out,0x470153,4);le(out,9,4);le(out,state,4);
le(out,std::bit_cast<uint32_t>(grade),4);le(out,instant?0x80:0,1);return out;
}
Bytes send(NativeWorldTranslation& world,const Bytes& body){return world.translate({0x2f4,body},0x10046,0,nullptr);}
}
int main(){try{
NativeWorldTranslation world{};world.realm=70;world.selected.guid=42;
// Explicit state expectations from source Weather.h/GetWeatherState,
// including the adjacent representable float on each intensity boundary.
struct Case{float grade;uint32_t rain,snow,storm;};
const Case cases[]={
{0.f,0,0,0},{std::nextafter(0.27f,0.f),0,0,0},{0.27f,3,6,22},
{std::nextafter(0.40f,0.f),3,6,22},{0.40f,4,7,41},
{std::nextafter(0.70f,0.f),4,7,41},{0.70f,5,8,42},{1.f,5,8,42}};
for(const auto& c:cases){
const uint32_t states[]={0,c.rain,c.snow,c.storm};
for(uint32_t type=0;type<4;++type)for(uint8_t instant:{0,1}){
auto wanted=native(states[type],c.grade,instant!=0);
check(send(world,source(type,c.grade,0,instant))==wanted,"weather boundary/bit framing");
check(send(world,source(type,c.grade,0xffffffffu,instant))==wanted,"legacy sound leaked into native weather");
}
}
// Negative zero is a finite source intensity and must retain its wire bits.
check(send(world,source(1,-0.f,0,0))==native(0,-0.f,false),"signed zero intensity changed");
const Bytes empty(4,0);
for(uint32_t type:{4u,0xffffffffu})check(send(world,source(type,.5f,0,0))==empty,"unknown weather type admitted");
for(float bad:{-0.01f,std::nextafter(1.f,2.f),std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity(),std::numeric_limits<float>::quiet_NaN()})
check(send(world,source(1,bad,0,0))==empty,"invalid intensity admitted");
auto full=source(2,.4f,123,1);
for(size_t n=0;n<full.size();++n){
bool rejected=false;try{(void)send(world,Bytes(full.begin(),full.begin()+n));}catch(const Error&){rejected=true;}
check(rejected,"truncated weather accepted");
}
full.push_back(0);bool rejected=false;try{(void)send(world,full);}catch(const Error&){rejected=true;}
check(rejected,"trailing weather data accepted");
check(send(world,source(3,.7f,0,1))==native(42,.7f,true),"malformed weather poisoned next event");
std::cout<<checks<<" weather source/native route checks passed\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}