64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
"""Exercise actual OX status/quiz functions with timer/notice boundaries."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def function(text, signature):
|
|
start = text.index(signature)
|
|
brace = text.index('{', start)
|
|
depth = 1
|
|
end = brace + 1
|
|
while depth:
|
|
depth += (text[end] == '{') - (text[end] == '}')
|
|
end += 1
|
|
return text[start:end]
|
|
|
|
|
|
class OXBounds(unittest.TestCase):
|
|
def test_equal_and_excess_level_and_untruncated_status(self):
|
|
source = (ROOT/'src/server/game/src/OXEvent.cpp').read_bytes().decode('latin1')
|
|
actual = '\n'.join(function(source, s) for s in
|
|
('OXEventStatus COXEventManager::GetStatus()',
|
|
'bool COXEventManager::Quiz('))
|
|
fixture = r'''
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <vector>
|
|
enum OXEventStatus {OXEVENT_FINISH,OXEVENT_OPEN,OXEVENT_CLOSE,OXEVENT_QUIZ,OXEVENT_ERR};
|
|
int flag=0;
|
|
namespace quest {struct CQuestManager {static CQuestManager& instance(){static CQuestManager m;return m;}int GetEventFlag(const char*){return flag;}};}
|
|
struct OXEventInfoData {bool answer=false;};
|
|
struct Question {const char* Quiz="test";bool answer=true;};
|
|
struct COXEventManager {std::vector<std::vector<Question>> m_vec_quiz;void* m_timedEvent=nullptr;OXEventStatus status=OXEVENT_FINISH;
|
|
OXEventStatus GetStatus();bool Quiz(unsigned char,int);void SetStatus(OXEventStatus s){status=s;}};
|
|
int number(int lo,int hi){assert(lo==0 && hi>=0);return hi;}
|
|
void SendNoticeMap(const char*,int,bool){}
|
|
constexpr int OXEVENT_MAP_INDEX=113;
|
|
void event_cancel(void** p){*p=nullptr;}
|
|
template<class T>T* AllocEventInfo(){static T data;return &data;}
|
|
void* event_create(int,OXEventInfoData*,int){static int event;return &event;}
|
|
constexpr int oxevent_timer=1;
|
|
#define LC_TEXT(s) "notice"
|
|
#define PASSES_PER_SEC(t) (t)
|
|
'''
|
|
main = r'''
|
|
int main(){COXEventManager m;assert(!m.Quiz(0,30));
|
|
for(int value:{-1,4,256,257}){flag=value;assert(m.GetStatus()==OXEVENT_ERR);}
|
|
for(int value=0;value<4;++value){flag=value;assert(m.GetStatus()==static_cast<OXEventStatus>(value));}
|
|
m.m_vec_quiz={{{"first",true}}};assert(m.Quiz(1,30));assert(m.m_vec_quiz[0].empty());
|
|
m.m_vec_quiz={{{"first",true}},{{"last",false}}};assert(m.Quiz(255,-1));
|
|
assert(m.m_vec_quiz[0].size()==1 && m.m_vec_quiz[1].empty());assert(!m.Quiz(2,30));
|
|
assert(m.Quiz(0,30));assert(m.status==OXEVENT_QUIZ);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path=Path(tmp);(path/'probe.cpp').write_text(fixture+actual+main)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined',
|
|
'-fno-sanitize-recover=all',str(path/'probe.cpp'),'-o',str(path/'probe')],check=True)
|
|
subprocess.run([str(path/'probe')],check=True)
|