from pathlib import Path import io import shutil import subprocess import sys import tarfile import tempfile ROOT = Path(__file__).resolve().parent CANDIDATE = Path(sys.argv[1]) def expected(commands): timers = {} now = 0 serial = 0 output = [] injection = None interleavings = 0 for line in commands.splitlines(): parts = line.split() op = parts[0] args = [int(x) for x in parts[1:]] if op == 'B': now = args[0] elif op == 'N': i, duration, periodic, stop_after = args timers[i] = dict(duration=duration, periodic=periodic, stop_after=stop_after, count=0, active=False, deadline=0, serial=0) elif op in ('S', 'T', 'U', 'V'): i = args[0] timer = timers[i] if op == 'S': serial += 1 timer.update(active=True, deadline=now + timer['duration'], serial=serial) output.append('S %d 0' % i) elif op == 'T': result = 0 if timer['active'] else -1 timer['active'] = False output.append('T %d %d' % (i, result)) elif op == 'U': timer['duration'] = args[1] output.append('U %d 0' % i) else: output.append('V %d %d %d' % (i, timer['active'], timer['duration'])) elif op == 'I': injection = args elif op in ('P', 'A'): if op == 'A': now = args[0] if injection is not None: i, duration = injection timer = timers[i] if duration >= 0: timer['duration'] = duration serial += 1 timer.update(active=True, deadline=now + timer['duration'], serial=serial) output.append('J %d 0' % i) injection = None interleavings += 1 while True: due = [(t['deadline'], t['serial'], i) for i, t in timers.items() if t['active'] and t['deadline'] <= now] if not due: break _, _, i = min(due) timer = timers[i] timer['count'] += 1 if not timer['periodic']: timer['active'] = False output.append('F %d %d 1' % (i, now)) if timer['stop_after'] and timer['count'] == timer['stop_after']: result = 0 if timer['active'] else -1 timer['active'] = False output.append('C %d %d' % (i, result)) if timer['active']: serial += 1 timer.update(deadline=now + timer['duration'], serial=serial) elif op == 'R': if args[0] == 0: output.append('D -1 -1 1 0') elif args[0] == 1: output.append('D 0 -1 1 0') else: output.append('D -1 0 1 0') interleavings += 1 elif op == 'Q': output.append('Q 0 %d' % interleavings) return output def cases(): result = [] for base, near, far in [(10, 1, 23), (100, 4, 71), (501, 11, 103), (2000, 17, 211)]: # Empty-list polling, insertion in the application context. result.append(f'B {base}\nN 0 {near} 0 0\nI 0 -1\nP\nA {base+near-1}\nV 0\nA {base+near}\nV 0\nA {base+far}\nQ\n') # A nearer client appears while a later deadline has been observed. result.append(f'B {base}\nN 0 {far} 0 0\nN 1 {near} 0 0\nS 0\nI 1 -1\nP\nA {base+near-1}\nA {base+near}\nV 0\nV 1\nA {base+far}\nQ\n') # Restart the observed timer with a different duration. result.append(f'B {base}\nN 0 {far} 0 0\nS 0\nI 0 {near}\nP\nA {base+near}\nV 0\nA {base+far}\nQ\n') # Same-deadline insertion ordering and repeated start before dispatch. result.append(f'B {base}\nN 0 {near} 0 0\nN 1 {near} 0 0\nN 2 {near} 0 0\nS 0\nS 1\nS 2\nS 1\nP\nA {base+near}\nA {base+far}\nQ\n') # Existing suspended wait, restart, removal and inactive return paths. result.append(f'B {base}\nN 0 {far} 0 0\nN 1 {near} 0 0\nS 0\nP\nS 1\nP\nT 1\nT 1\nA {base+near}\nU 0 {near}\nS 0\nP\nA {base+2*near}\nV 0\nA {base+far}\nQ\n') # Periodic work and one-shot work share the timer thread. result.append(f'B {base}\nN 0 {near} 1 3\nN 1 {near*2} 0 0\nS 0\nI 1 -1\nP\nA {base+near}\nA {base+2*near}\nA {base+3*near}\nA {base+far}\nV 0\nV 1\nQ\n') # A later insertion leaves the earlier client deadline intact. result.append(f'B {base}\nN 0 {near} 0 0\nN 1 {far} 0 0\nS 0\nI 1 -1\nP\nA {base+near}\nA {base+far}\nQ\n') # Several notices may coalesce, but all active clients are delivered. lines = [f'B {base}'] for i in range(8): lines += [f'N {i} {near+i} 0 0', f'S {i}', f'S {i}'] lines += ['P'] + [f'A {base+near+i}' for i in range(8)] + ['Q'] result.append('\n'.join(lines)+'\n') # Zero tick one-shot fires on the next dispatch without duplicate delivery. result.append(f'B {base}\nN 0 0 0 0\nS 0\nP\nV 0\nA {base+1}\nQ\n') for mode in range(3): result.append(f'B {base}\nR {mode}\nQ\n') return result def main(): support = (ROOT / 'support.tar').read_bytes() harness = (ROOT / 'observations.c').read_bytes() trials = [(text, expected(text)) for text in cases()] with tempfile.TemporaryDirectory(prefix='softtimer-check-') as temporary: work = Path(temporary) with tarfile.open(fileobj=io.BytesIO(support)) as archive: archive.extractall(work) (work / 'observations.c').write_bytes(harness) for name in ('src/timer.c', 'src/thread.c'): target = work / name target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes((CANDIDATE / name).read_bytes()) sources = ['src/timer.c', 'src/thread.c', 'src/semaphore.c', 'port/controlled.c', 'observations.c'] command = ['gcc', '-std=c11', '-O0', '-D_XOPEN_SOURCE=700', '-Iinclude', '-Iport', '-ffunction-sections', '-fdata-sections', *sources, '-Wl,--gc-sections', '-o', 'observe'] build = subprocess.run(command, cwd=work, capture_output=True, text=True, timeout=25) if build.returncode: print('FAIL build') return 1 checks = 0 for index, (commands, want) in enumerate(trials): run = subprocess.run([str(work / 'observe')], input=commands, capture_output=True, text=True, cwd=work, timeout=4) if run.returncode or run.stdout.splitlines() != want: print('case', index, 'return', run.returncode, file=sys.stderr) print('observed', run.stdout[:3000], file=sys.stderr) print('expected', '\n'.join(want)[:3000], file=sys.stderr) print('FAIL behavior') return 1 checks += len(want) print('checks', checks, 'scenarios', len(trials)) print('PASS') return 0 if __name__ == '__main__': raise SystemExit(main())