"""Checks exact arithmetic and explicit verbal countermodels."""
import json,copy,sys
from pathlib import Path
from fractions import Fraction
from calculate import calculate
def check(c,m):
 if not c:raise AssertionError(m)
def main():
 d=json.loads(Path(__file__).with_name("numbers.json").read_text());r=calculate(d)
 expected={"fulfilled":180,"revenue":558000,"weighted_average":"3100","final_price":"2400","price_change":"-1/25","invoice_total":"90900","gross_before":"160","gross_after":"144","gross_change":"-1/10","break_even_orders":280,"capacity_covers":False}
 check(r==expected,"Fixed numeric example")
 # Independent weighted mean expressed by fulfilled group weights.
 check(Fraction(3,5)*2500+Fraction(2,5)*4000==3100,"Independent weighted mean")
 check(350*280==98000 and 350*250==87500,"Independent order boundary")
 changed=copy.deepcopy(d);changed["capacity"]=280;check(calculate(changed)["capacity_covers"],"Exact capacity")
 changed=copy.deepcopy(d);changed["groups"][1]["received"]=116;check(calculate(changed)["weighted_average"]=="3250","Equal fulfilled groups")
 changed=copy.deepcopy(d)
 for row in changed["groups"]:row["cancelled"]=row["received"]
 check(calculate(changed)["weighted_average"]is None,"No fulfilled orders")
 changed=copy.deepcopy(d);changed["fee_percent"]=0;check(calculate(changed)["invoice_total"]=="90000","No fee")
 new=set(range(8));standard=set(range(8,12));sent=set(range(10));all_ids=new|standard
 check(len(sent)==10 and bool(standard-sent) and bool(sent&standard),"Verbal facts1,2,6")
 # Both urgency models fit the document because urgency was not recorded.
 urgent_all=new;urgent_none=set()
 check(new<=urgent_all and not(new<=urgent_none),"Countermodels for statement3")
 urgent={0};registered_yes={0};registered_no=set()
 check(urgent<=registered_yes and not(urgent<=registered_no),"Countermodels for statement4")
 check(urgent<=all_ids,"Changed fact: all twelve registered")
 bads=[]
 bad=copy.deepcopy(d);bad["groups"][0]["cancelled"]=121;bads.append(bad)
 bad=copy.deepcopy(d);bad["fee_percent"]=-1;bads.append(bad)
 bad=copy.deepcopy(d);bad["order_contribution"]=0;bads.append(bad)
 for bad in bads:
  try:calculate(bad)
  except ValueError:pass
  else:raise AssertionError("Invalid input accepted")
 print(json.dumps({"numeric_questions":6,"independent_arithmetic":True,"verbal_facts_and_countermodels":True,"edges":["exact280","equal groups","no fulfilled orders","no fee"],"invalid_inputs_rejected":len(bads)}))
if __name__=="__main__":
 try:main()
 except Exception as error:print("FAIL: "+str(error),file=sys.stderr);sys.exit(1)
