<s> from guppy . heapy . test import support <EOL> class TestCase ( support . TestCase ) : <EOL> def setUp ( self ) : <EOL> support . TestCase . setUp ( self ) <EOL> self . sets = self . guppy . sets <EOL> heapdefs = getattr ( self . sets . setsc , '<STR_LIT>' ) , <EOL> self . root = [ ] <EOL> self . heapyc = self . guppy . heapy . heapyc <EOL> self . hv = self . heapyc . HeapView ( self . root , heapdefs ) <EOL> self . nodeset = self . sets . immnodeset <EOL> self . mutnodeset = self . sets . mutnodeset <EOL> self . nodegraph = self . heapyc . NodeGraph <EOL> class TestHeapView ( TestCase ) : <EOL> def test_hiding_tag ( self ) : <EOL> hiding_tag = self . hv . _hiding_tag_ <EOL> a = [ ] <EOL> ns = self . mutnodeset ( [ a ] ) <EOL> ng = self . nodegraph ( [ ( a , a ) ] ) <EOL> self . aseq ( self . hv . relimg ( [ ns ] ) , self . nodeset ( [ a ] ) ) <EOL> self . aseq ( self . hv . relimg ( [ ng ] ) , self . nodeset ( [ a ] ) ) <EOL> ns . _hiding_tag_ = hiding_tag <EOL> self . aseq ( self . hv . relimg ( [ ns ] ) , self . nodeset ( [ ] ) ) <EOL> ng . _hiding_tag_ = hiding_tag <EOL> self . aseq ( self . hv . relimg ( [ ng ] ) , self . nodeset ( [ ] ) ) <EOL> self . hv . _hiding_tag_ = [ ] <EOL> self . aseq ( self . hv . relimg ( [ ns ] ) , self . nodeset ( [ a , None ] ) ) <EOL> self . aseq ( self . hv . relimg ( [ ng ] ) , self . nodeset ( [ a , None ] ) ) <EOL> def test_inheritance_from_heapview ( self ) : <EOL> HeapView = self . guppy . heapy . heapyc . HeapView <EOL> x = '<STR_LIT:x>' <EOL> newroot = [ x ] <EOL> class HV ( HeapView ) : <EOL> def __new__ ( self ) : <EOL> return HeapView . __new__ ( HV , newroot , ( ) ) <EOL> hv = HV ( ) <EOL> assert hv . heap ( ) == self . nodeset ( [ x , newroot ] ) <EOL> def test_inheritance_from_special_types ( self ) : <EOL> hv = self . hv <EOL> hv . _hiding_tag_ = [ ] <EOL> immnodeset = self . sets . immnodeset <EOL> mutnodeset = self . sets . mutnodeset <EOL> for base in ( list , mutnodeset ) : <EOL> class T ( base ) : <EOL> __slots__ = '<STR_LIT:t>' , <EOL> class U ( T ) : <EOL> __slots__ = '<STR_LIT:u>' , <EOL> a = base ( ) <EOL> t = T ( ) <EOL> t . t = [ ] <EOL> u = U ( ) <EOL> u . t = [ ] <EOL> u . u = [ ] <EOL> data = range ( <NUM_LIT:16> ) <EOL> for x in data : <EOL> a . append ( x ) <EOL> t . append ( x ) <EOL> u . append ( x ) <EOL> za = hv . indisize_sum ( [ a ] ) <EOL> zt = hv . indisize_sum ( [ t ] ) <EOL> zu = hv . indisize_sum ( [ u ] ) <EOL> self . assert_ ( za < zt < zu ) <EOL> self . aseq ( hv . relimg ( [ a ] ) , immnodeset ( data ) ) <EOL> self . aseq ( hv . relimg ( [ t ] ) , immnodeset ( data + [ T , t . t ] ) ) <EOL> self . aseq ( hv . relimg ( [ u ] ) , immnodeset ( data + [ U , u . t , u . u ] ) ) <EOL> def rel ( src , tgt ) : <EOL> r = hv . relate ( src , tgt ) <EOL> self . assert_ ( r != ( ( ) , ) * len ( r ) ) <EOL> return r <EOL> self . aseq ( rel ( t , data [ <NUM_LIT:1> ] ) , rel ( a , data [ <NUM_LIT:1> ] ) ) <EOL> self . aseq ( rel ( u , data [ <NUM_LIT:1> ] ) , rel ( a , data [ <NUM_LIT:1> ] ) ) <EOL> self . aseq ( rel ( u , u . t ) , rel ( t , t . t ) ) <EOL> rel ( u , u . u ) <EOL> def test_nodeset_circularity ( self ) : <EOL> pass <EOL> def test_registered_hiding ( self ) : <EOL> hv = self . hv <EOL> class Set ( object ) : <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> class Der ( Set ) : <EOL> pass <EOL> hv . register__hiding_tag__type ( Set ) <EOL> s = Set ( ) <EOL> d = Der ( ) <EOL> t = Set ( ) <EOL> some = [ ] <EOL> other = [ ] <EOL> dother = [ ] <EOL> s . some = some <EOL> s . other = other <EOL> d . some = some <EOL> d . other = dother <EOL> self . root . append ( [ s , t , d ] ) <EOL> self . root . append ( s ) <EOL> self . root . append ( d ) <EOL> self . root . append ( t ) <EOL> x = hv . heap ( ) <EOL> assert dother in x <EOL> assert some in x <EOL> assert other in x <EOL> assert s in x <EOL> assert d in x <EOL> assert t in x <EOL> s . _hiding_tag_ = hv . _hiding_tag_ <EOL> d . _hiding_tag_ = hv . _hiding_tag_ <EOL> x = hv . heap ( ) <EOL> assert some not in x <EOL> assert other not in x <EOL> assert s not in x <EOL> assert d not in x <EOL> assert t in x <EOL> he = [ ] <EOL> hv . _hiding_tag_ = he <EOL> x = hv . heap ( ) <EOL> assert dother in x <EOL> assert some in x <EOL> assert other in x <EOL> assert s in x <EOL> assert d in x <EOL> assert t in x <EOL> def test_timing ( self ) : <EOL> from time import clock <EOL> hv = self . hv <EOL> d = [ ] <EOL> h = [ d ] <EOL> self . root . extend ( <NUM_LIT> * [ h ] ) <EOL> self . root . extend ( range ( <NUM_LIT> ) ) <EOL> start = clock ( ) <EOL> x = hv . heap ( ) <EOL> elapsed0 = clock ( ) - start <EOL> print '<STR_LIT>' , elapsed0 , '<STR_LIT>' , len ( x ) <EOL> class Set ( object ) : <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> class Der ( Set ) : <EOL> pass <EOL> hv . register__hiding_tag__type ( Set ) <EOL> s = Set ( ) <EOL> s . _hiding_tag_ = hv . _hiding_tag_ <EOL> d = Der ( ) <EOL> d . _hiding_tag_ = hv . _hiding_tag_ <EOL> self . root [ <NUM_LIT:0> : <NUM_LIT> ] = <NUM_LIT> * [ s , d ] <EOL> start = clock ( ) <EOL> x = hv . heap ( ) <EOL> elapsed1 = clock ( ) - start <EOL> print '<STR_LIT>' , elapsed1 , '<STR_LIT>' , len ( x ) <EOL> def test_unregistered_hiding ( self ) : <EOL> hv = self . hv <EOL> class Set : <EOL> pass <EOL> s = Set ( ) <EOL> s . _hiding_tag_ = hv . _hiding_tag_ <EOL> t = Set ( ) <EOL> some = [ ] <EOL> other = [ ] <EOL> s . some = some <EOL> s . other = other <EOL> self . root . append ( [ s , t ] ) <EOL> self . root . append ( s ) <EOL> self . root . append ( t ) <EOL> x = hv . heap ( ) <EOL> assert some not in x <EOL> assert other not in x <EOL> assert s not in x <EOL> assert t in x <EOL> class TestLeak ( support . TestCase ) : <EOL> def test_1 ( self ) : <EOL> import gc <EOL> from sys import getrefcount as grc <EOL> support . TestCase . setUp ( self ) <EOL> sets = self . guppy . sets <EOL> heapdefs = getattr ( sets . setsc , '<STR_LIT>' ) , <EOL> root = [ ] <EOL> heapyc = self . guppy . heapy . heapyc <EOL> nodeset = sets . mutnodeset <EOL> nodegraph = heapyc . NodeGraph <EOL> class T ( object ) : <EOL> __slots__ = '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' <EOL> pass <EOL> class U ( T ) : <EOL> __slots__ = '<STR_LIT:b>' , <EOL> pass <EOL> class V ( object ) : <EOL> __slots__ = '<STR_LIT:c>' , <EOL> gc . collect ( ) <EOL> ns = nodeset ( ) <EOL> a = [ ns ] <EOL> a . append ( a ) <EOL> b = [ ] <EOL> he = [ ] <EOL> c = [ ] <EOL> t = T ( ) <EOL> tonly = [ ] <EOL> t . a = a <EOL> t . _hiding_tag_ = he <EOL> t . tonly = tonly <EOL> u = U ( ) <EOL> u . a = a <EOL> u . _hiding_tag_ = he <EOL> u . b = b <EOL> v = V ( ) <EOL> v . c = c <EOL> a = [ x for x in [ list ] ] <EOL> del x <EOL> li = [ he , a , b , c , t , u , v , T , U , V , ns , nodeset , list ] <EOL> rcli0 = [ grc ( x ) for x in li ] <EOL> del x <EOL> ns |= li + range ( <NUM_LIT> , <NUM_LIT> ) <EOL> root . extend ( li ) <EOL> rcli = [ grc ( x ) for x in li ] <EOL> del x <EOL> rec = nodeset ( [ x for x in li ] ) <EOL> x = None <EOL> rec . append ( rec ) <EOL> ns . add ( rec ) <EOL> rec . _hiding_tag_ = rec <EOL> if <NUM_LIT:1> : <EOL> hv = heapyc . HeapView ( root , heapdefs ) <EOL> hv . register__hiding_tag__type ( T ) <EOL> h = hv . heap ( ) <EOL> assert a in h <EOL> assert c in h <EOL> assert tonly in h <EOL> hv . _hiding_tag_ = he <EOL> h = hv . heap ( ) <EOL> del x <EOL> del h <EOL> del hv <EOL> ns . discard ( rec ) <EOL> rec = None <EOL> gc . collect ( ) <EOL> nrcli = [ grc ( x ) for x in li ] <EOL> del x <EOL> self . aseq ( rcli , nrcli ) <EOL> root [ : ] = [ ] <EOL> ns . clear ( ) <EOL> nrcli0 = [ grc ( x ) for x in li ] <EOL> del x <EOL> self . aseq ( rcli0 , nrcli0 ) <EOL> def test_weaky ( self ) : <EOL> import gc <EOL> from sys import getrefcount as grc <EOL> support . TestCase . setUp ( self ) <EOL> sets = self . guppy . sets <EOL> heapdefs = getattr ( sets . setsc , '<STR_LIT>' ) , <EOL> root = [ ] <EOL> heapyc = self . guppy . heapy . heapyc <EOL> nodeset = sets . NodeSet <EOL> nodegraph = heapyc . NodeGraph <EOL> gc . collect ( ) <EOL> probe = [ ] <EOL> rcprobe = grc ( probe ) <EOL> class T ( object ) : <EOL> x = probe <EOL> class U ( T ) : <EOL> pass <EOL> T . U = U <EOL> t = T ( ) <EOL> u = U ( ) <EOL> root . append ( t ) <EOL> root . append ( u ) <EOL> if <NUM_LIT:1> : <EOL> hv = heapyc . HeapView ( root , heapdefs ) <EOL> x = hv . heap ( ) <EOL> assert t in x <EOL> x = None <EOL> T = t = U = u = None <EOL> root [ : ] = [ ] <EOL> gc . collect ( ) <EOL> nrcprobe = grc ( probe ) <EOL> self . aseq ( nrcprobe , rcprobe ) <EOL> class TestNodeGraph ( TestCase ) : <EOL> def test_constructor_and_methods ( self ) : <EOL> ng = self . nodegraph ( ) <EOL> ng . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> ng . add_edges_n1 ( [ <NUM_LIT:3> , <NUM_LIT:4> ] , <NUM_LIT:5> ) <EOL> lng = list ( ng ) <EOL> lng . sort ( ) <EOL> assert lng == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:5> ) , ( <NUM_LIT:4> , <NUM_LIT:5> ) ] <EOL> fl = ng . as_flat_list ( ) <EOL> fl . sort ( ) <EOL> assert fl == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> ] <EOL> cp = ng . copy ( ) <EOL> cp . add_edge ( <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> lng1 = list ( ng . __iter__ ( ) ) <EOL> lng1 . sort ( ) <EOL> assert lng1 == lng <EOL> lcp = list ( cp ) <EOL> lcp . sort ( ) <EOL> assert lcp == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:5> ) , ( <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:5> , <NUM_LIT:6> ) ] <EOL> assert ng . domain_covers ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert not ng . domain_covers ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> rng = ng . domain_restricted ( [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> assert rng . get_domain ( ) == self . nodeset ( [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> lrng = list ( rng ) <EOL> lrng . sort ( ) <EOL> assert lrng == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:5> ) ] <EOL> assert rng . get_range ( ) == self . nodeset ( [ <NUM_LIT:2> , <NUM_LIT:5> ] ) <EOL> rng . invert ( ) <EOL> lrng = list ( rng ) <EOL> lrng . sort ( ) <EOL> assert lrng == [ ( <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , <NUM_LIT:3> ) ] <EOL> ing = ng . inverted ( ) <EOL> ling = list ( ing ) <EOL> ling . sort ( ) <EOL> assert ling == [ ( <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , <NUM_LIT:3> ) , ( <NUM_LIT:5> , <NUM_LIT:4> ) ] <EOL> assert ing . relimg ( [ <NUM_LIT:2> ] ) == self . nodeset ( [ <NUM_LIT:1> ] ) <EOL> assert ing . relimg ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> ] ) == self . nodeset ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> ing . update ( [ ( <NUM_LIT:3> , <NUM_LIT:7> ) , ( <NUM_LIT:4> , <NUM_LIT:8> ) ] ) <EOL> assert ing . relimg ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> ] ) == self . nodeset ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:7> ] ) <EOL> uing = ing . updated ( [ ( <NUM_LIT:2> , <NUM_LIT:9> ) ] ) <EOL> assert ing . relimg ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> ] ) == self . nodeset ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:7> ] ) <EOL> assert uing . relimg ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> ] ) == self . nodeset ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:9> ] ) <EOL> tgts = list ( uing [ <NUM_LIT:2> ] ) <EOL> tgts . sort ( ) <EOL> assert tgts == [ <NUM_LIT:1> , <NUM_LIT:9> ] <EOL> assert len ( uing ) == <NUM_LIT:6> <EOL> uing [ <NUM_LIT:2> ] = ( <NUM_LIT:2> , <NUM_LIT:8> ) <EOL> tgts = list ( uing [ <NUM_LIT:2> ] ) <EOL> tgts . sort ( ) <EOL> assert tgts == [ <NUM_LIT:2> , <NUM_LIT:8> ] <EOL> ng . clear ( ) <EOL> assert list ( ng ) == [ ] <EOL> ng = self . nodegraph ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> assert list ( ng ) == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> assert not ng . is_mapping <EOL> ng = self . nodegraph ( is_mapping = True ) <EOL> assert ng . is_mapping <EOL> assert list ( ng ) == [ ] <EOL> ng . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert list ( ng ) == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> assert ng [ <NUM_LIT:1> ] == <NUM_LIT:2> <EOL> ng = self . nodegraph ( is_mapping = False ) <EOL> assert not ng . is_mapping <EOL> for ng in ( self . nodegraph ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] , True ) , <EOL> self . nodegraph ( iterable = [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] , is_mapping = True ) , <EOL> self . nodegraph ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] , is_mapping = True ) , <EOL> self . nodegraph ( is_mapping = True , iterable = [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> ) : <EOL> assert ng . is_mapping <EOL> assert list ( ng ) == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> assert ng [ <NUM_LIT:1> ] == <NUM_LIT:2> <EOL> ng [ <NUM_LIT:1> ] = <NUM_LIT:3> <EOL> assert ng [ <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> ng = self . nodegraph ( ) <EOL> ng . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> ng . add_edge ( <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> assert not ng . is_sorted <EOL> ng [ <NUM_LIT:1> ] <EOL> assert ng . is_sorted <EOL> def test_inheritance ( self ) : <EOL> class T ( self . heapyc . NodeGraph ) : <EOL> __slots__ = '<STR_LIT:x>' <EOL> def as_sorted_list ( self ) : <EOL> a = list ( self ) <EOL> a . sort ( ) <EOL> return a <EOL> t = T ( ) <EOL> t . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> t . add_edge ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert t . as_sorted_list ( ) == [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] <EOL> t = T ( [ ( <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> ) ] ) <EOL> assert t . as_sorted_list ( ) == [ ( <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> ) ] <EOL> self . nodegraph = T <EOL> self . test_constructor_and_methods ( ) <EOL> class R ( T ) : <EOL> __slots__ = '<STR_LIT>' , <EOL> def __new__ ( self , stop ) : <EOL> r = T . __new__ ( R , is_mapping = <NUM_LIT:1> ) <EOL> r . add_edges_n1 ( range ( stop ) , <NUM_LIT:0> ) <EOL> r . stop = stop <EOL> return r <EOL> def keys ( self ) : <EOL> return list ( self . get_domain ( ) ) <EOL> def values ( self ) : <EOL> return [ self [ k ] for k in self . keys ( ) ] <EOL> r = R ( <NUM_LIT:10> ) <EOL> assert r . stop == <NUM_LIT:10> <EOL> assert r . is_mapping <EOL> lr = list ( r ) <EOL> lr . sort ( ) <EOL> assert lr [ - <NUM_LIT:2> : ] == [ ( <NUM_LIT:8> , <NUM_LIT:0> ) , ( <NUM_LIT:9> , <NUM_LIT:0> ) ] <EOL> keys = r . keys ( ) <EOL> keys . sort ( ) <EOL> assert keys == range ( <NUM_LIT:10> ) <EOL> values = r . values ( ) <EOL> assert values == [ <NUM_LIT:0> ] * <NUM_LIT:10> <EOL> class TestClassifiers ( TestCase ) : <EOL> def test_inrel ( self ) : <EOL> def str_inrel ( c ) : <EOL> c = list ( c ) <EOL> c . sort ( ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( x . kind , x . relator ) for x in c ] ) <EOL> hv = self . hv <EOL> rg = self . nodegraph ( ) <EOL> x = [ ] <EOL> y = [ x ] <EOL> rg . add_edge ( x , y ) <EOL> cli = hv . cli_inrel ( rg , { } , { } ) <EOL> c = cli . classify ( x ) <EOL> self . aseq ( str_inrel ( c ) , '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> y . append ( x ) <EOL> c = cli . classify ( x ) <EOL> self . aseq ( str_inrel ( c ) , '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> r = { str ( i ) : x } <EOL> rg . add_edge ( x , r ) <EOL> c = cli . classify ( x ) <EOL> def test_main ( debug = False ) : <EOL> support . run_unittest ( TestClassifiers , debug ) <EOL> support . run_unittest ( TestNodeGraph , debug ) <EOL> support . run_unittest ( TestLeak , debug ) <EOL> support . run_unittest ( TestHeapView , debug ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
