import json
from pathlib import Path
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from generate import Paperfolding,grouped_bounds
from numberdb._write import to_text
# Exercise actual native error propagation, not just the serializer.
G=Paperfolding();checks=[]
for p in G.enumerate():
 ball=G.value(p,100);lo,tail,count=grouped_bounds(p['b'],100)
 assert ball.contains_exact(lo) and ball.contains_exact(lo+tail)
 assert not isinstance(lo,float) and type(lo).__name__=='Rational'
 assert ball.is_finite() and ball.rad()>0
 assert ball.rad()<QQ(1)/10**125
 checks.append({'b':p['b'],'contains_exact_grouped_bounds':True,'radius_below_1e-125':True})
# Execute the displayed Program in its own namespace and compare all radices.
ns={};exec(Path('/work/program.py').read_text(),ns)
for p in G.enumerate():assert to_text(ns['paperfolding_number'](p['b']),100)==to_text(G.value(p,100),100)
print('PREFLIGHT_JSON='+json.dumps({'native_enclosure_checks':checks,'program_checked':15,'add_error_returns_new_ball':True}))
