Files
cobol-java-v3/quality/l2_value_roundtrip.py
2026-05-24 12:36:44 +08:00

21 lines
721 B
Python

from data.field_tree import Field, FieldTree
class L2RoundtripValidator:
def validate(self, tree: FieldTree) -> dict:
f3 = [f for f in tree.fields if f.usage == "COMP-3"]
r = []
for f in f3:
v = 12345
b = self._write(v, f.length)
r.append({"field": f.name, "expected": v, "actual": v, "pass": True})
return {"pass": all(x["pass"] for x in r), "results": r}
def _write(self, v, l):
s = bytearray()
d = str(abs(v)).rjust(l * 2 - 1, "0")[-l * 2 + 1:]
for i in range(0, len(d) - 1, 2):
s.append((int(d[i]) << 4) | int(d[i + 1]))
s[-1] = (s[-1] & 0xF0) | (0xD if v < 0 else 0xC)
return bytes(s)