85 lines
3.9 KiB
Python
85 lines
3.9 KiB
Python
"""Reader reachability, using retained signed facts; no model exports or assets."""
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
|
|
import m2_sequence_refs as refs
|
|
from m2_sequence_hash import native_sequence_hash
|
|
|
|
|
|
def model(ids, slots=()):
|
|
body = bytearray(304)
|
|
body[:4] = b'MD20'
|
|
struct.pack_into('<I', body, 4, 272)
|
|
struct.pack_into('<II', body, 28, len(ids), 304)
|
|
for i, ident in enumerate(ids):
|
|
record = bytearray(64)
|
|
struct.pack_into('<H', record, 0, ident)
|
|
struct.pack_into('<hH', record, 60, -1, i)
|
|
body.extend(record)
|
|
struct.pack_into('<II', body, 36, len(slots), len(body))
|
|
body.extend(struct.pack('<%dH' % len(slots), *slots))
|
|
return body
|
|
|
|
|
|
class SequenceReachability(unittest.TestCase):
|
|
def test_six_signed_stand_misses_are_issues(self):
|
|
facts = json.loads((Path(__file__).parent / 'data/m2_sequence_hash_signed_stand76.json').read_text())
|
|
for case in facts['cases']:
|
|
with self.subTest(fdid=case['fdid']):
|
|
ids, slots = case['sequence_ids'], [v & 0xffff for v in case['table']]
|
|
self.assertTrue(all(v == 0xffff or v < len(ids) for v in slots))
|
|
raw = model(ids, slots)
|
|
issues = refs.inspect_model(raw)['issues']
|
|
self.assertTrue(any(x.get('animation') == 0 and x.get('status') == 'unreachable' for x in issues), issues)
|
|
repaired, _ = native_sequence_hash(raw)
|
|
self.assertEqual(refs.inspect_model(repaired)['issues'], [])
|
|
|
|
def test_absent_stand_and_empty_hash_are_valid(self):
|
|
for raw in (model([147]), model([]), model([147], [0xffff, 0])):
|
|
self.assertEqual(refs.inspect_model(raw)['issues'], [])
|
|
|
|
def test_out_of_range_remains_issue(self):
|
|
self.assertTrue(any(x['field'] == 'sequence_lookup' for x in refs.inspect_model(model([0], [1]))['issues']))
|
|
|
|
def test_repeated_slot_is_not_a_proven_loop(self):
|
|
reader = refs.NativeSequenceLookup([7, 14, 21, 0], [0, 1, 3, 0xffff, 0xffff, 2, 0xffff])
|
|
# Query0 visits 0,1,5,0,2: the second visit to slot0 still progresses.
|
|
result = reader.query(0, probe_budget=4)
|
|
self.assertEqual((result['status'], result['reason']), ('hold', 'probe_budget_exhausted'))
|
|
self.assertEqual(reader.query(0, probe_budget=5)['index'], 3)
|
|
|
|
def test_proven_nontermination_has_no_terminal_slot(self):
|
|
raw = model([0, 1], [0])
|
|
issues = refs.inspect_model(raw)['issues']
|
|
self.assertTrue(any(x.get('animation') == 1 and x.get('status') == 'nonterminating'
|
|
and x['reason'] == 'no_terminal_slot_for_query' for x in issues))
|
|
self.assertEqual(refs.NativeSequenceLookup([0], [1]).query(0)['status'], 'out_of_range')
|
|
|
|
def test_shared_budget_is_an_explicit_hold_not_success_or_loop(self):
|
|
table = [{'animation': x} for x in (7, 14, 21, 0)]
|
|
issues, observations = [], []
|
|
refs.check_reachability(table, [0, 1, 3, 0xffff, 0xffff, 2, 0xffff],
|
|
issues, observations, total_budget=1)
|
|
self.assertEqual(issues, [])
|
|
self.assertEqual(len(observations), 3)
|
|
self.assertTrue(all(x['status'] == 'hold' for x in observations))
|
|
|
|
def test_skeleton_uses_same_reachability_condition(self):
|
|
body = bytearray(24)
|
|
struct.pack_into('<II', body, 8, 1, 24)
|
|
record = bytearray(64)
|
|
struct.pack_into('<hH', record, 60, -1, 0)
|
|
body.extend(record)
|
|
struct.pack_into('<II', body, 16, 1, len(body))
|
|
body.extend(struct.pack('<H', 0xffff))
|
|
result = refs.inspect_skeleton(b'SKS1' + struct.pack('<I', len(body)) + body)
|
|
self.assertTrue(any(x.get('status') == 'unreachable' for x in result['issues']))
|
|
self.assertTrue(result['lookup_reachability_complete'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|