107 lines
5.5 KiB
Python
107 lines
5.5 KiB
Python
import json,sys,tempfile,subprocess,random
|
|
from pathlib import Path
|
|
TASK_ID='build-plugin-route'
|
|
def mix(h, v):
|
|
v &= (1 << 64)-1
|
|
for i in range(8):
|
|
h = ((h ^ ((v >> (8*i)) & 255))*16777619) & 0xffffffff
|
|
return h
|
|
|
|
def observations(events, model):
|
|
calls = [0]*26
|
|
fails = [0]*26
|
|
history = []
|
|
accepted = rejected = total = 0
|
|
h = 2166136261
|
|
lo = hi = None
|
|
lines = []
|
|
for seq, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc, value, detail = -2, 0, 0
|
|
if kind == 'Y':
|
|
if len(args)==1 and 0 <= args[0] < len(history):
|
|
previous = history[-1-args[0]]
|
|
rc,value,detail=previous[2],previous[3],previous[0]
|
|
elif kind == 'Z':
|
|
if len(args)==1 and 0 <= args[0] < 26:
|
|
rc,value,detail=0,calls[args[0]],fails[args[0]]
|
|
else:
|
|
rc,value,detail=model.execute(kind,args)
|
|
history.append([seq,ord(kind),rc,value]); history=history[-8:]
|
|
if 'A'<=kind<='Z':
|
|
channel=ord(kind)-65;calls[channel]+=1;fails[channel]+=int(rc<0)
|
|
if rc>=0:
|
|
accepted+=1;total+=value
|
|
lo=value if lo is None else min(lo,value)
|
|
hi=value if hi is None else max(hi,value)
|
|
else:rejected+=1
|
|
for value_to_mix in (seq,ord(kind),rc,value):h=mix(h,value_to_mix)
|
|
state=model.state()+[seq,accepted,rejected,total,h,lo or 0,hi or 0,len(history)]
|
|
for record in reversed(history):state+=record
|
|
state += [0]*(4*(8-len(history)))
|
|
for a,b in zip(calls,fails):state += [a,b]
|
|
lines.append('E %d RESULT %d %d %d'%(seq,rc,value,detail))
|
|
lines.append('S %d '%seq+' '.join(map(str,state)))
|
|
return lines
|
|
class Model:
|
|
def __init__(self,s):self.active=self.generation=self.samples=self.total=self.last=0
|
|
def state(self):return [self.active,self.generation,self.samples,self.total,self.last]
|
|
def execute(self,k,a):
|
|
if k=='R' and not a:
|
|
self.active=self.generation=self.samples=self.total=self.last=0;return 0,0,0
|
|
if k=='C' and not a:self.active=0;return 0,0,0
|
|
if k=='L' and len(a)==1 and 1<=a[0]<=4:
|
|
rc=-1
|
|
if a[0] in (1,2):self.active=a[0];self.generation+=1;rc=0
|
|
return rc,self.active,self.generation
|
|
if k=='V' and len(a)==1 and -1000000<=a[0]<=1000000:
|
|
if not self.active:return -1,0,0
|
|
gain,offset=((2,7) if self.active==1 else (5,-3))
|
|
self.last=a[0]*gain+offset;self.total+=self.last;self.samples+=1
|
|
return 0,self.last,self.samples
|
|
return -2,0,0
|
|
|
|
def scenarios(data):
|
|
rng=random.Random(data['seed'])
|
|
directed=[['R'],['L',1],['V',12],['L',2],['V',-11],['L',4],['V',31],['L',3],['V',4],['C'],['V',1],['Y',0],['Z',11]]
|
|
for launch in ('.','launch'):
|
|
events=directed[:]
|
|
for _ in range(180):
|
|
k=rng.choice('LLVVVVCRYZ')
|
|
events.append([k,rng.randrange(0,6)] if k=='L' else [k,rng.randrange(-1000000,1000001)] if k=='V' else [k,rng.randrange(12)] if k=='Y' else [k,rng.randrange(29)-1] if k=='Z' else [k])
|
|
events += [['L',0],['L',5],['V',1000001],['V',-1000001],['V'],['Q'],['R',1]]
|
|
yield dict(name='launch-'+launch,events=events,directories=['launch'],cwd=launch)
|
|
def main():
|
|
data=json.loads(Path(__file__).with_name('cases.json').read_text())
|
|
candidate=Path(sys.argv[1])
|
|
prepared=[]
|
|
# Prepare every file before any compiler or candidate subprocess starts.
|
|
for scenario in scenarios(data):
|
|
scratch=Path(tempfile.mkdtemp(prefix=TASK_ID+'-'))
|
|
for name,content in data['context'].items():
|
|
target=scratch/name;target.parent.mkdir(parents=True,exist_ok=True);target.write_text(content)
|
|
for name,content in scenario.get('files',{}).items():
|
|
target=scratch/name;target.parent.mkdir(parents=True,exist_ok=True);target.write_text(content)
|
|
for name in data['candidate_files']:
|
|
target=scratch/name;target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes((candidate/name).read_bytes())
|
|
for directory in scenario.get('directories',[]):(scratch/directory).mkdir(parents=True,exist_ok=True)
|
|
events=scenario['events']
|
|
prepared.append((scratch,scenario,observations(events,Model(scenario)),''.join(' '.join(map(str,e))+'\n' for e in events)))
|
|
for scratch,scenario,answers,stdin in prepared:
|
|
built=subprocess.run(['make','-s']+scenario.get('make',[]),cwd=scratch,capture_output=True,text=True,timeout=35)
|
|
if built.returncode:
|
|
print('build failed: '+built.stderr[-1800:]);return 1
|
|
result=subprocess.run([str(scratch/'app')],cwd=scratch/scenario.get('cwd','.'),input=stdin,capture_output=True,text=True,timeout=20)
|
|
# Candidate-controlled paths are never read or written by this root process after execution.
|
|
if result.stderr:print(result.stderr[-1800:],file=sys.stderr,end='')
|
|
if result.returncode:
|
|
print('candidate process failed: '+str(result.returncode));return 1
|
|
observed=result.stdout.splitlines()
|
|
for index in range(max(len(observed),len(answers))):
|
|
got=observed[index] if index<len(observed) else '<missing>'
|
|
want=answers[index] if index<len(answers) else '<extra>'
|
|
if got!=want:
|
|
print('scenario %s observation %d mismatch: got %s; expected %s'%(scenario['name'],index+1,got[:1600],want[:1600]));return 1
|
|
print('PASS');return 0
|
|
if __name__=='__main__':raise SystemExit(main())
|