"""Compile the shipping eligibility/cap arithmetic, including boundary and overflow cases.""" import json from pathlib import Path import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] class PointDrop(unittest.TestCase): def test_approved_rules_and_cap(self): flags = json.loads((ROOT/'tools/ava2-itemshop/points-wide.json').read_text()) entries = ','.join('{"%s",%d}' % entry for entry in flags.items()) source = r''' #include "src/server/common/point_drop.h" #include #include int main() { std::map flags={ENTRIES}; auto lookup=[&](const std::string& key){return flags[key];}; auto mob=PortPointRule(lookup,false),stone=PortPointRule(lookup,true); assert(mob.chance==500 && mob.minimum==1 && mob.maximum==1); assert(stone.chance==10000 && stone.minimum==3 && stone.maximum==8); assert(mob.eligible(105,120));assert(!mob.eligible(104,120)); assert(stone.eligible(120,1));assert(!stone.eligible(0,1)); assert(!stone.eligible(120,0)); assert(PortPointDropAmount(8,498,500)==2); assert(PortPointDropAmount(8,500,500)==0); assert(PortPointDropAmount(8,UINT64_MAX,500)==0); assert(PortPointDropAmount(8,UINT64_MAX,0)==8); flags["points_mob_chance"]=0;assert(!PortPointRule(lookup,false).eligible(105,120)); flags["points_stone_chance"]=10001;assert(!PortPointRule(lookup,true).valid()); flags["points_stone_chance"]=10000;flags["points_stone_min"]=-1; assert(!PortPointRule(lookup,true).valid()); flags["points_stone_min"]=9;flags["points_stone_max"]=3; assert(!PortPointRule(lookup,true).valid()); } '''.replace('ENTRIES', entries) with tempfile.TemporaryDirectory() as tmp: src = Path(tmp)/'test.cpp' exe = Path(tmp)/'test' src.write_text(source) subprocess.run(['c++', '-std=c++17', '-I'+str(ROOT), str(src), '-o', str(exe)], check=True) subprocess.run([str(exe)], check=True) if __name__ == '__main__': unittest.main()