<s> import unittest <EOL> import os <EOL> import re <EOL> import time <EOL> import ufora . FORA . python . FORA as FORA <EOL> import ufora . FORA . python . Evaluator . Evaluator as Evaluator <EOL> import ufora . FORA . python . Evaluator . LocalEvaluator as LocalEvaluator <EOL> import ufora . FORA . python . Runtime as Runtime <EOL> import ufora . native . FORA as FORANative <EOL> import ufora . native . Cumulus as CumulusNative <EOL> import ufora . native . CallbackScheduler as CallbackScheduler <EOL> import logging <EOL> import ufora . native . TCMalloc as TCMallocNative <EOL> import pyfora <EOL> def makeJovt ( * args ) : <EOL> return FORANative . JOVListToJOVT ( list ( args ) ) <EOL> def symbolJov ( sym ) : <EOL> return FORANative . parseStringToJOV ( "<STR_LIT>" + sym ) <EOL> class TestSimpleForwardReasoner ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . callbackScheduler = CallbackScheduler . singletonForTesting ( ) <EOL> self . runtime = Runtime . getMainRuntime ( ) <EOL> self . axioms = self . runtime . getAxioms ( ) <EOL> self . compiler = self . runtime . getTypedForaCompiler ( ) <EOL> self . builtinsAsJOV = FORANative . JudgmentOnValue . Constant ( FORA . builtin ( ) . implVal_ ) <EOL> pyforaPath = os . path . join ( os . path . split ( pyfora . __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . purePythonAsJOV = FORANative . JudgmentOnValue . Constant ( FORA . importModule ( pyforaPath ) . implVal_ ) <EOL> self . instructionGraph = self . runtime . getInstructionGraph ( ) <EOL> self . reasoner = FORANative . SimpleForwardReasoner ( self . compiler , self . instructionGraph , self . axioms ) <EOL> def nodeAndFrameCounts ( self , reasoner , frame ) : <EOL> allFrames = set ( ) <EOL> toCheck = [ frame ] <EOL> while toCheck : <EOL> frameToCheck = toCheck . pop ( ) <EOL> if frameToCheck not in allFrames : <EOL> allFrames . add ( frameToCheck ) <EOL> for subframe in reasoner . subframesFor ( frameToCheck ) . values ( ) : <EOL> toCheck . append ( subframe ) <EOL> reachableFrames = len ( allFrames ) <EOL> allFrameCount = reasoner . totalFrameCount ( ) <EOL> badApplyNodes = <NUM_LIT:0> <EOL> for f in allFrames : <EOL> for n in f . unknownApplyNodes ( ) : <EOL> badApplyNodes += <NUM_LIT:1> <EOL> return badApplyNodes , reachableFrames , reasoner . totalFrameCount ( ) <EOL> def dumpReasonerSummary ( self , reasoner , frame ) : <EOL> badApplyNodes , reachableFrames , allFrameCount = self . nodeAndFrameCounts ( reasoner , frame ) <EOL> logging . info ( "<STR_LIT>" , reachableFrames , allFrameCount , badApplyNodes ) <EOL> def test_builtin_math_isSimple ( self ) : <EOL> frame = self . reasoner . reasonAboutApply ( makeJovt ( self . builtinsAsJOV , symbolJov ( "<STR_LIT>" ) , symbolJov ( "<STR_LIT>" ) ) ) <EOL> self . assertFrameHasConstantResult ( frame ) <EOL> def reasonAboutExpression ( self , expression , ** variableJudgments ) : <EOL> keys = sorted ( list ( variableJudgments . keys ( ) ) ) <EOL> functionText = "<STR_LIT>" + "<STR_LIT:U+002C>" . join ( [ '<STR_LIT:_>' ] + keys ) + "<STR_LIT>" + expression + "<STR_LIT>" <EOL> frame = self . reasoner . reasonAboutApply ( <EOL> makeJovt ( <EOL> FORANative . parseStringToJOV ( functionText ) , <EOL> * [ FORANative . parseStringToJOV ( variableJudgments [ k ] ) <EOL> if isinstance ( variableJudgments [ k ] , str ) else variableJudgments [ k ] for k in keys ] <EOL> ) <EOL> ) <EOL> self . dumpReasonerSummary ( self . reasoner , frame ) <EOL> return frame <EOL> def test_nested_iterators ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_pure_python_lt ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> "<STR_LIT>" , <EOL> purePython = self . purePythonAsJOV , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_pure_python_isinstance ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> "<STR_LIT>" , <EOL> purePython = self . purePythonAsJOV <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT:true>" ) <EOL> self . assertFrameHasConstantResult ( frame ) <EOL> def test_loop_resolves ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_loop_with_iterator ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_tuple_matching ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT:1>" ) <EOL> def test_nested_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_tuple_iterator ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_tuple_loop_resolves ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_function_call_works ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_function_call_with_branching_works ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> self . assertTrue ( len ( frame . unknownApplyNodes ( ) ) == <NUM_LIT:0> ) <EOL> def test_function_call_and_branching ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_tuple_tracing_works ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT:2>" ) <EOL> def test_simple_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_simple_dual_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_simple_mutual_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_call_site_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_recursion_on_tuples ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def DISABLEDtest_memory_load ( self ) : <EOL> b0 = TCMallocNative . getBytesUsed ( ) <EOL> b1 = b0 <EOL> for ix in range ( <NUM_LIT> ) : <EOL> self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , x = str ( ix ) <EOL> ) <EOL> print ( b1 - b0 ) / ( ix + <NUM_LIT:1> ) / <NUM_LIT> / <NUM_LIT> , "<STR_LIT>" , ( TCMallocNative . getBytesUsed ( ) - b0 ) / <NUM_LIT> / <NUM_LIT> , "<STR_LIT>" <EOL> b1 = TCMallocNative . getBytesUsed ( ) <EOL> def test_recursion_on_tuples_2 ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_two_layer_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_same_function_called_with_different_arguments ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOV ( frame , "<STR_LIT>" ) <EOL> def test_heterogeneous_vec_of_vec ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_vector_recursion ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_list_comprehension ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_switch_is_exclusive_and_disjoint ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT:1>" ) <EOL> def test_alternativesWork ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" , <EOL> x = "<STR_LIT>" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_heterogeneous_vec_of_vec_function_apply ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_tuple_function_iteration ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertFrameHasResultJOVs ( frame , "<STR_LIT>" ) <EOL> def test_tuple_function_iteration2 ( self ) : <EOL> frame = self . reasonAboutExpression ( <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertTrue ( len ( frame . unknownApplyNodes ( ) ) > <NUM_LIT:0> ) <EOL> def assertFrameHasConstantResult ( self , frame ) : <EOL> self . assertTrue ( len ( frame . exits ( ) . resultPart ( ) . vals ) == <NUM_LIT:1> , frame . exits ( ) ) <EOL> self . assertTrue ( len ( frame . exits ( ) . throwPart ( ) . vals ) == <NUM_LIT:0> , frame . exits ( ) ) <EOL> self . assertTrue ( frame . exits ( ) . resultPart ( ) [ <NUM_LIT:0> ] . constant ( ) ) <EOL> def assertFrameHasResultJOV ( self , frame , resultJOV ) : <EOL> if isinstance ( resultJOV , str ) : <EOL> resultJOV = FORANative . parseStringToJOV ( resultJOV ) <EOL> self . assertTrue ( len ( frame . exits ( ) . resultPart ( ) . vals ) == <NUM_LIT:1> , frame . exits ( ) ) <EOL> self . assertEqual ( frame . exits ( ) . resultPart ( ) [ <NUM_LIT:0> ] , resultJOV ) <EOL> def assertFrameHasResultJOVs ( self , frame , * resultJOVs ) : <EOL> self . assertTrue ( len ( frame . unknownApplyNodes ( ) ) == <NUM_LIT:0> ) <EOL> jovsToFind = [ ] <EOL> for resultJOV in resultJOVs : <EOL> if isinstance ( resultJOV , str ) : <EOL> resultJOV = FORANative . parseStringToJOV ( resultJOV ) <EOL> jovsToFind . append ( resultJOV ) <EOL> resultJOVs = list ( jovsToFind ) <EOL> for res in frame . exits ( ) . resultPart ( ) : <EOL> self . assertTrue ( res in jovsToFind , <EOL> "<STR_LIT>" % <EOL> ( res , frame . exits ( ) . resultPart ( ) , resultJOVs ) <EOL> ) <EOL> jovsToFind . remove ( res ) <EOL> self . assertTrue ( <EOL> len ( jovsToFind ) == <NUM_LIT:0> , <EOL> "<STR_LIT>" % <EOL> ( jovsToFind , frame . exits ( ) . resultPart ( ) ) <EOL> ) </s>
