59 lines
2.9 KiB
Python
59 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Compile the production wire and total-price helper, including malformed bounds."""
|
|
import subprocess,tempfile,unittest
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
class ItemShop(unittest.TestCase):
|
|
def test_native_wire_and_integer_quantity_discount(self):
|
|
source=r'''
|
|
#include "src/server/common/ava2_itemshop.h"
|
|
#include <cassert>
|
|
#include <cstring>
|
|
int main(){
|
|
static_assert(sizeof(Ava2ShopRequest)==103,"request");
|
|
static_assert(sizeof(Ava2ShopProduct)==49,"native catalogue");
|
|
static_assert(sizeof(Ava2ShopLog)==90,"native log");
|
|
int64_t price=123;
|
|
assert(Ava2ShopTotal(99,2,25,price) && price==148);
|
|
assert(Ava2ShopTotal(1,1,99,price) && price==0);
|
|
assert(Ava2ShopTotal(1000000000000LL,20,0,price) && price==20000000000000LL);
|
|
assert(!Ava2ShopTotal(-1,1,0,price));assert(!Ava2ShopTotal(1000000000001LL,1,0,price));
|
|
assert(!Ava2ShopTotal(1,0,0,price));assert(!Ava2ShopTotal(1,21,0,price));assert(!Ava2ShopTotal(1,1,101,price));
|
|
Ava2ShopProduct p={};p.id=0x01020304;p.vnum=71084;p.price=99;p.discount=25;p.stock=-1;
|
|
const unsigned char* bytes=reinterpret_cast<const unsigned char*>(&p);
|
|
assert(bytes[0]==4 && bytes[3]==1 && bytes[20]==25 && bytes[45]==255);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=Path(d);(p/'test.cpp').write_text(source)
|
|
subprocess.run(['c++','-std=c++17','-I'+str(ROOT),str(p/'test.cpp'),'-o',str(p/'test')],check=True);subprocess.run([str(p/'test')],check=True)
|
|
def test_sorted_prototype_index_points_to_the_matching_item(self):
|
|
source=(ROOT/'src/server/db/src/ClientManagerBoot.cpp').read_bytes().decode('latin1')
|
|
body=source[source.index('bool CClientManager::InitializeItemTable()'):source.index('bool CClientManager::InitializeSkillTable()')]
|
|
start=body.rindex('// END_OF_QUEST_ITEM_PROTO_DISABLE')
|
|
tail=body[start:body.rfind('return true;')+len('return true;')]
|
|
harness=r"""
|
|
#include <vector>
|
|
#include <map>
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#define sys_log(...) ((void)0)
|
|
#define itertype(c) decltype(c)::iterator
|
|
using namespace std;
|
|
using DWORD=unsigned;
|
|
struct TItemTable { DWORD dwVnum;unsigned bType; };
|
|
struct FCompareVnum { bool operator()(const TItemTable& a,const TItemTable& b)const {return a.dwVnum<b.dwVnum;} };
|
|
struct Database {
|
|
vector<TItemTable> m_vec_itemTable={{71084,3},{54051,3},{1,0},{71502,28}};
|
|
map<DWORD,TItemTable*> m_map_itemTableByVnum;
|
|
bool Build() { BODY }
|
|
};
|
|
int main(){ Database db;assert(db.Build());for(auto& row:db.m_map_itemTableByVnum)assert(row.first==row.second->dwVnum);assert(db.m_map_itemTableByVnum[54051]->bType==3);assert(db.m_map_itemTableByVnum[71502]->bType==28); }
|
|
""".replace('BODY',tail)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=Path(d);(p/'test.cpp').write_text(harness)
|
|
subprocess.run(['c++','-std=c++17',str(p/'test.cpp'),'-o',str(p/'test')],check=True)
|
|
subprocess.run([str(p/'test')],check=True)
|
|
|
|
if __name__=='__main__':unittest.main()
|