main
John Lauer Review off-board sensor and mechanical libraries with truthful conversion and supplier limits 4c1d6f7
"""Exercise unadopted compiled conversion against source table and faults."""
from pathlib import Path
import ctypes,json,math,hashlib,subprocess,tempfile
R=Path(__file__).resolve().parents[1];O=R/'checkpoints/sensor-curve-review';source=R/'design/sensor-candidate.json';rows=json.loads(source.read_text())['rows']
with tempfile.TemporaryDirectory(prefix='load-ntc-test-') as tmp:
 lib=Path(tmp)/'candidate.so'
 subprocess.run(['gcc','-std=c11','-Wall','-Wextra','-Werror','-shared','-fPIC',str(O/'ntcle428_candidate.c'),'-lm','-o',str(lib)],check=True)
 f=ctypes.CDLL(str(lib)).ntcle428_nominal_c;f.argtypes=[ctypes.c_float,ctypes.POINTER(ctypes.c_float)];f.restype=ctypes.c_bool
 errors=[]
 for t,r,lo,hi in rows:
  out=ctypes.c_float(-999);assert f(r,ctypes.byref(out));errors.append(abs(out.value-t))
 assert max(errors)<0.001
 for resistance in [float('nan'),float('inf'),-float('inf'),0,-1,rows[0][1]*1.001,rows[-1][1]*.999]:
  out=ctypes.c_float(-999);assert not f(resistance,ctypes.byref(out));assert out.value==-999
 assert not f(10000,None)
 last=-math.inf;count=10001
 for i in range(count):
  r=math.exp(math.log(rows[0][1])+(math.log(rows[-1][1])-math.log(rows[0][1]))*i/(count-1));out=ctypes.c_float()
  assert f(r,ctypes.byref(out));assert out.value>=last;assert -40.001<=out.value<=105.001;last=out.value
 report={'status':'candidate-software-checks-pass-not-qualified','sourceSha256':hashlib.sha256(source.read_bytes()).hexdigest(),'candidateSha256':hashlib.sha256((O/'ntcle428_candidate.c').read_bytes()).hexdigest(),'manufacturerTableNodes':len(rows),'maxNodeErrorC':max(errors),'monotonicSamples':count,'invalidCases':8,'productionFirmwareChanged':False,'limitations':['No between-node manufacturer error bound','No physical or sensor calibration','No installed thermal tracking or trip qualification']}
 (O/'checks.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))