82 lines
4.1 KiB
C++
82 lines
4.1 KiB
C++
#include "gateway/spell_modifier_group_values.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <limits>
|
|
using gateway::spell_modifier::SourceWeight;
|
|
using gateway::spell_modifier::reconstruct;
|
|
static unsigned checks;
|
|
static void require(bool good, const char* what) {
|
|
++checks;
|
|
if (!good) { std::cerr << "FAIL: " << what << '\n'; std::exit(1); }
|
|
}
|
|
int main() {
|
|
using W = SourceWeight;
|
|
std::array<int32_t,64> v{};
|
|
constexpr uint64_t all = ~uint64_t(0);
|
|
constexpr auto lo = std::numeric_limits<int32_t>::min();
|
|
constexpr auto hi = std::numeric_limits<int32_t>::max();
|
|
require(reconstruct({},v,0)==0,"empty qualified expression is zero");
|
|
// Nature's Grace / Rebirth: one modifier intersects bits24 and28. The
|
|
// OFFLINE-qualified expression chooses one observation, not equal-value
|
|
// deduplication. The helper has no authority to choose this expression.
|
|
v[24]=v[28]=-500;
|
|
const std::array<W,1> once{{{24,1}}};
|
|
require(reconstruct(once,v,(uint64_t(1)<<24)|(uint64_t(1)<<28))==-500,
|
|
"qualified overlapping modifier counted once");
|
|
const std::array<W,2> independent{{{24,1},{28,1}}};
|
|
require(reconstruct(independent,v,all)==-1000,"independent equal modifiers both count");
|
|
v[28]=-700;
|
|
require(reconstruct(once,v,all)==-500,"unused bit value cannot alter expression");
|
|
require(reconstruct(independent,v,all)==-1200,"independent distinct modifiers add");
|
|
// Observations A+C=40, B+C=50, C=20 reconstruct A+B+C=70.
|
|
v[1]=40;v[2]=50;v[3]=20;
|
|
std::array<W,3> unionWeights{{{1,1},{2,1},{3,-1}}};
|
|
for (int i=0;i<6;++i) {
|
|
require(reconstruct(unionWeights,v,14)==70,"inclusion/exclusion permutation");
|
|
std::next_permutation(unionWeights.begin(),unionWeights.end(),
|
|
[](W a,W b){return a.bit<b.bit;});
|
|
}
|
|
require(!reconstruct(unionWeights,v,6),"missing subtractand observation holds");
|
|
require(!reconstruct(unionWeights,v,12),"missing addend observation holds");
|
|
v[1]=v[2]=v[3]=0;
|
|
require(reconstruct(unionWeights,v,14)==0,"observed zero reset is a value");
|
|
require(!reconstruct(unionWeights,v,0),"unobserved zero is not reset evidence");
|
|
v[1]=hi;v[2]=hi;v[3]=hi;
|
|
require(reconstruct(unionWeights,v,14)==hi,"int64 intermediate may exceed int32");
|
|
const std::array<W,2> sum{{{1,1},{2,1}}};
|
|
require(!reconstruct(sum,v,6),"positive int32 overflow holds");
|
|
v[1]=lo;v[2]=-1;
|
|
require(!reconstruct(sum,v,6),"negative int32 overflow holds");
|
|
v[2]=0;
|
|
require(reconstruct(sum,v,6)==lo,"INT32_MIN allowed exactly");
|
|
const std::array<W,1> negative{{{1,-1}}};
|
|
require(!reconstruct(negative,v,2),"negation widens before INT32_MIN overflow check");
|
|
const std::array<W,2> bothNegative{{{1,-1},{2,-1}}};
|
|
v[2]=hi;
|
|
require(reconstruct(bothNegative,v,6)==1,"widened negation cancels to valid total");
|
|
for (int weight : {0,2,-2,-128,127}) {
|
|
const std::array<W,1> bad{{{1,static_cast<int8_t>(weight)}}};
|
|
require(!reconstruct(bad,v,all),"only signed unit weights accepted");
|
|
}
|
|
for (int bit : {64,255}) {
|
|
const std::array<W,1> bad{{{static_cast<uint8_t>(bit),1}}};
|
|
require(!reconstruct(bad,v,all),"invalid bit before shifting/indexing");
|
|
}
|
|
for (int second : {1,-1}) {
|
|
const std::array<W,2> duplicate{{{1,1},{1,static_cast<int8_t>(second)}}};
|
|
require(!reconstruct(duplicate,v,all),"duplicate bit rejected even if cancelling");
|
|
}
|
|
const std::array<W,1> top{{{63,1}}};v[63]=hi;
|
|
require(reconstruct(top,v,uint64_t(1)<<63)==hi,"bit63 observed correctly");
|
|
require(!reconstruct(top,v,(uint64_t(1)<<63)-1),"lower observations do not cover bit63");
|
|
std::array<W,64> dense{};
|
|
for(unsigned i=0;i<64;++i) {dense[i]={static_cast<uint8_t>(i),1};v[i]=hi;}
|
|
require(!reconstruct(dense,v,all),"all64 maximum aggregates hold without overflow");
|
|
v.fill(0);
|
|
require(reconstruct(dense,v,all)==0,"all64 observed resets reconstruct zero");
|
|
require(!reconstruct(dense,v,all-1),"dense expression requires each observation");
|
|
std::cout << checks << " checks PASS\n";
|
|
}
|