# Compares the tool's figure with four counts of each string. usage: analyze.py inputs.json tool.json counts.jsonl
import json,sys
inp={x["id"]:x for x in json.load(open(sys.argv[1]))}
tool={x["id"]:x for x in json.load(open(sys.argv[2]))}
cnt={}
for l in open(sys.argv[3]):
    d=json.loads(l); cnt[d["id"]]=d
ok=True
print("id | tool | utf16 | code points | graphemes | utf8 bytes | tool==utf16 | utf16>=cp>=graphemes")
for i,x in inp.items():
    t=tool[i]["tool_chars"]; c=cnt[i]
    if t is None and x["text"]=="" and tool[i]["raw"]=="": t=0   # empty box: the tool shows no figures, read as 0
    py=len(x["text"].encode("utf-16-le"))//2          # independent UTF-16 count in Python
    assert py==c["utf16_units"], (i,py,c)
    eq=(t==c["utf16_units"]); ordr=c["utf16_units"]>=c["code_points"]>=c["graphemes"]
    ok&=eq and ordr
    print(f'{i} | {t} | {c["utf16_units"]} | {c["code_points"]} | {c["graphemes"]} | {c["utf8_bytes"]} | {eq} | {ordr}')
# controls with a known answer
kc={"control-150-a":150,"control-151-a":151,"control-empty":0}
def tc(k):
    t=tool[k]["tool_chars"]
    return 0 if (t is None and tool[k]["raw"]=="" and inp[k]["text"]=="") else t
for k,v in kc.items(): print("CONTROL",k,"expected",v,"tool",("no figure shown (empty box)" if tool[k]["tool_chars"] is None else tool[k]["tool_chars"]),"PASS" if tc(k)==v else "FAIL")
seg_known={"emoji-zwj":1,"emoji-flag":1,"accent-decomp":1,"control-150-a":150}
for k,v in seg_known.items(): print("CONTROL grapheme",k,"expected",v,"got",cnt[k]["graphemes"],"PASS" if cnt[k]["graphemes"]==v else "FAIL")
print("ALL-CHECKS-PASS" if ok and all(tc(k)==v for k,v in kc.items()) and all(cnt[k]["graphemes"]==v for k,v in seg_known.items()) else "SOME-CHECK-FAILED")
