49 lines
2.7 KiB
Python
49 lines
2.7 KiB
Python
"""Execute actual Cube_make with wide balances and unsigned proto fees."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class CubeGold(unittest.TestCase):
|
|
def test_unsigned_fee_debits_exactly_once_and_insufficient_balance_is_unchanged(self):
|
|
text=(ROOT/'src/server/game/src/cube.cpp').read_text(encoding='latin1')
|
|
actual='bool Cube_make'+text.split('bool Cube_make',1)[1].split('void Cube_show_list',1)[0]
|
|
harness=r'''
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstddef>
|
|
struct Item{unsigned GetID(){return 1;}};using LPITEM=Item*;
|
|
struct Character;using LPCHARACTER=Character*;
|
|
struct CUBE_VALUE{unsigned vnum=19,count=1;};
|
|
struct CUBE_DATA{uint32_t gold=50;int percent=100;unsigned removed=0;CUBE_VALUE value;
|
|
CUBE_VALUE* reward_value(){return &value;}void remove_material(LPCHARACTER){++removed;}} proto;
|
|
struct Character {int64_t balance=1000,delta=0;unsigned charges=0,rewards=0;Item item;
|
|
bool IsCubeOpen(){return true;}Character* GetQuestNPC(){return this;}LPITEM* GetCubeItem(){return nullptr;}
|
|
unsigned GetRaceNum(){return 1;}int64_t GetGold(){return balance;}
|
|
void ChatPacket(int,const char*,...){}void PointChange(int,int64_t n,bool){delta=n;balance+=n;++charges;}
|
|
Item* AutoGiveItem(unsigned,unsigned){++rewards;return &item;}
|
|
unsigned GetPlayerID(){return 1;}int GetX(){return 0;}int GetY(){return 0;}};
|
|
CUBE_DATA* FN_find_cube(LPITEM*,unsigned){return &proto;}
|
|
constexpr int POINT_GOLD=11,CHAT_TYPE_INFO=1,CHAT_TYPE_COMMAND=2;
|
|
#define LC_TEXT(s) "notice"
|
|
int roll=1;int number(int,int){return roll;}
|
|
struct LogManager{static LogManager& instance(){static LogManager m;return m;}void CubeLog(unsigned,int,int,unsigned,unsigned,unsigned,int){}};
|
|
'''
|
|
main=r'''
|
|
int main(){
|
|
for(uint32_t fee:{uint32_t(50),uint32_t(INT32_MAX)+1u,UINT32_MAX}) {
|
|
proto.gold=fee;proto.removed=0;Character ch;ch.balance=int64_t(fee)+123;
|
|
assert(Cube_make(&ch));assert(ch.balance==123 && ch.delta==-int64_t(fee) && ch.charges==1 && proto.removed==1 && ch.rewards==1);
|
|
ch={};ch.balance=int64_t(fee)-1;proto.removed=0;assert(!Cube_make(&ch));assert(ch.balance==int64_t(fee)-1 && ch.charges==0 && proto.removed==0 && ch.rewards==0);
|
|
}
|
|
proto.gold=50;proto.removed=0;roll=100;proto.percent=0;Character ch;
|
|
assert(!Cube_make(&ch));assert(ch.balance==950 && ch.charges==1 && proto.removed==1 && ch.rewards==0);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
p=Path(tmp);(p/'probe.cpp').write_text('#include <initializer_list>\n'+harness+actual+main)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all',str(p/'probe.cpp'),'-o',str(p/'probe')],check=True)
|
|
subprocess.run([str(p/'probe')],check=True)
|