<s> from pypy . translator . translator import TranslationContext <EOL> from pypy . translator . backendopt . innerloop import find_inner_loops <EOL> from pypy . conftest import option <EOL> def test_simple_loop ( ) : <EOL> def snippet_fn ( x , y ) : <EOL> while y > <NUM_LIT:0> : <EOL> y -= x <EOL> return y <EOL> t = TranslationContext ( ) <EOL> graph = t . buildflowgraph ( snippet_fn ) <EOL> if option . view : <EOL> t . view ( ) <EOL> loops = find_inner_loops ( graph ) <EOL> assert len ( loops ) == <NUM_LIT:1> <EOL> loop = loops [ <NUM_LIT:0> ] <EOL> assert loop . headblock . operations [ <NUM_LIT:0> ] . opname == '<STR_LIT>' <EOL> assert len ( loop . links ) == <NUM_LIT:2> <EOL> assert loop . links [ <NUM_LIT:0> ] in loop . headblock . exits <EOL> assert loop . links [ <NUM_LIT:1> ] in loop . links [ <NUM_LIT:0> ] . target . exits <EOL> assert loop . links [ <NUM_LIT:1> ] . target is loop . headblock <EOL> def test_two_loops ( ) : <EOL> def snippet_fn ( x , y ) : <EOL> while y > <NUM_LIT:0> : <EOL> y -= x <EOL> while y < <NUM_LIT:0> : <EOL> y += x <EOL> return y <EOL> t = TranslationContext ( ) <EOL> graph = t . buildflowgraph ( snippet_fn ) <EOL> if option . view : <EOL> t . view ( ) <EOL> loops = find_inner_loops ( graph ) <EOL> assert len ( loops ) == <NUM_LIT:2> <EOL> assert loops [ <NUM_LIT:0> ] . headblock is not loops [ <NUM_LIT:1> ] . headblock <EOL> for loop in loops : <EOL> assert loop . headblock . operations [ <NUM_LIT:0> ] . opname in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert len ( loop . links ) == <NUM_LIT:2> <EOL> assert loop . links [ <NUM_LIT:0> ] in loop . headblock . exits <EOL> assert loop . links [ <NUM_LIT:1> ] in loop . links [ <NUM_LIT:0> ] . target . exits <EOL> assert loop . links [ <NUM_LIT:1> ] . target is loop . headblock <EOL> def test_nested_loops ( ) : <EOL> def snippet_fn ( x , z ) : <EOL> y = <NUM_LIT:0> <EOL> while y <= <NUM_LIT:10> : <EOL> while z < y : <EOL> z += y <EOL> y += <NUM_LIT:1> <EOL> return z <EOL> t = TranslationContext ( ) <EOL> graph = t . buildflowgraph ( snippet_fn ) <EOL> if option . view : <EOL> t . view ( ) <EOL> loops = find_inner_loops ( graph ) <EOL> assert len ( loops ) == <NUM_LIT:1> <EOL> loop = loops [ <NUM_LIT:0> ] <EOL> assert loop . headblock . operations [ <NUM_LIT:0> ] . opname == '<STR_LIT>' <EOL> assert len ( loop . links ) == <NUM_LIT:2> <EOL> assert loop . links [ <NUM_LIT:0> ] in loop . headblock . exits <EOL> assert loop . links [ <NUM_LIT:1> ] in loop . links [ <NUM_LIT:0> ] . target . exits <EOL> assert loop . links [ <NUM_LIT:1> ] . target is loop . headblock </s>
