<s> """<STR_LIT>""" <EOL> import py . test <EOL> from pypy . objspace . std . setobject import W_SetObject , W_FrozensetObject <EOL> from pypy . objspace . std . setobject import _initialize_set <EOL> from pypy . objspace . std . setobject import set_intersection__Set_Set <EOL> from pypy . objspace . std . setobject import set_intersection__Set_ANY <EOL> from pypy . objspace . std . setobject import eq__Set_Set <EOL> letters = '<STR_LIT>' <EOL> class W_SubSetObject ( W_SetObject ) : pass <EOL> class TestW_SetObject : <EOL> def setup_method ( self , method ) : <EOL> self . word = self . space . wrap ( '<STR_LIT>' ) <EOL> self . otherword = self . space . wrap ( '<STR_LIT>' ) <EOL> self . letters = self . space . wrap ( letters ) <EOL> self . true = self . space . w_True <EOL> self . false = self . space . w_False <EOL> def test_and ( self ) : <EOL> s = W_SetObject ( self . space , None ) <EOL> _initialize_set ( self . space , s , self . word ) <EOL> t0 = W_SetObject ( self . space , None ) <EOL> _initialize_set ( self . space , t0 , self . otherword ) <EOL> t1 = W_FrozensetObject ( self . space , None ) <EOL> _initialize_set ( self . space , t1 , self . otherword ) <EOL> r0 = set_intersection__Set_Set ( self . space , s , t0 ) <EOL> r1 = set_intersection__Set_Set ( self . space , s , t1 ) <EOL> assert eq__Set_Set ( self . space , r0 , r1 ) == self . true <EOL> sr = set_intersection__Set_ANY ( self . space , s , self . otherword ) <EOL> assert eq__Set_Set ( self . space , r0 , sr ) == self . true <EOL> def test_compare ( self ) : <EOL> s = W_SetObject ( self . space , None ) <EOL> _initialize_set ( self . space , s , self . word ) <EOL> t = W_SetObject ( self . space , None ) <EOL> _initialize_set ( self . space , t , self . word ) <EOL> assert self . space . eq_w ( s , t ) <EOL> class AppTestAppSetTest : <EOL> def test_subtype ( self ) : <EOL> class subset ( set ) : pass <EOL> a = subset ( ) <EOL> b = a | set ( '<STR_LIT:abc>' ) <EOL> assert type ( b ) is subset <EOL> def test_compare ( self ) : <EOL> raises ( TypeError , cmp , set ( '<STR_LIT:abc>' ) , set ( '<STR_LIT>' ) ) <EOL> assert set ( '<STR_LIT:abc>' ) != '<STR_LIT:abc>' <EOL> raises ( TypeError , "<STR_LIT>" ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) < set ( '<STR_LIT>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) <= frozenset ( '<STR_LIT>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) < frozenset ( '<STR_LIT>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) >= frozenset ( '<STR_LIT>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) > frozenset ( '<STR_LIT>' ) ) <EOL> assert set ( '<STR_LIT:abc>' ) <= frozenset ( '<STR_LIT:abc>' ) <EOL> assert set ( '<STR_LIT:abc>' ) >= frozenset ( '<STR_LIT:abc>' ) <EOL> assert set ( '<STR_LIT:abc>' ) <= frozenset ( '<STR_LIT>' ) <EOL> assert set ( '<STR_LIT:abc>' ) >= frozenset ( '<STR_LIT>' ) <EOL> assert set ( '<STR_LIT:abc>' ) < frozenset ( '<STR_LIT>' ) <EOL> assert set ( '<STR_LIT:abc>' ) > frozenset ( '<STR_LIT>' ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) < frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) > frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert not set ( ) == <NUM_LIT> <EOL> assert set ( ) != <NUM_LIT> <EOL> assert ( set ( '<STR_LIT:abc>' ) == frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert ( set ( '<STR_LIT:abc>' ) == set ( '<STR_LIT:abc>' ) ) <EOL> assert ( frozenset ( '<STR_LIT:abc>' ) == frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert ( frozenset ( '<STR_LIT:abc>' ) == set ( '<STR_LIT:abc>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) != frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) != set ( '<STR_LIT:abc>' ) ) <EOL> assert not ( frozenset ( '<STR_LIT:abc>' ) != frozenset ( '<STR_LIT:abc>' ) ) <EOL> assert not ( frozenset ( '<STR_LIT:abc>' ) != set ( '<STR_LIT:abc>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) == frozenset ( '<STR_LIT>' ) ) <EOL> assert not ( set ( '<STR_LIT:abc>' ) == set ( '<STR_LIT>' ) ) <EOL> assert not ( frozenset ( '<STR_LIT:abc>' ) == frozenset ( '<STR_LIT>' ) ) <EOL> assert not ( frozenset ( '<STR_LIT:abc>' ) == set ( '<STR_LIT>' ) ) <EOL> assert ( set ( '<STR_LIT:abc>' ) != frozenset ( '<STR_LIT>' ) ) <EOL> assert ( set ( '<STR_LIT:abc>' ) != set ( '<STR_LIT>' ) ) <EOL> assert ( frozenset ( '<STR_LIT:abc>' ) != frozenset ( '<STR_LIT>' ) ) <EOL> assert ( frozenset ( '<STR_LIT:abc>' ) != set ( '<STR_LIT>' ) ) <EOL> def test_libpython_equality ( self ) : <EOL> for thetype in [ frozenset , set ] : <EOL> word = "<STR_LIT>" <EOL> otherword = "<STR_LIT>" <EOL> s = thetype ( word ) <EOL> assert s == set ( word ) <EOL> assert s , frozenset ( word ) <EOL> assert not s == word <EOL> assert s != set ( otherword ) <EOL> assert s != frozenset ( otherword ) <EOL> assert s != word <EOL> def test_copy ( self ) : <EOL> s1 = set ( '<STR_LIT:abc>' ) <EOL> s2 = s1 . copy ( ) <EOL> assert s1 is not s2 <EOL> assert s1 == s2 <EOL> assert type ( s2 ) is set <EOL> s1 = frozenset ( '<STR_LIT:abc>' ) <EOL> s2 = s1 . copy ( ) <EOL> assert s1 is s2 <EOL> assert s1 == s2 <EOL> class myfrozen ( frozenset ) : <EOL> pass <EOL> s1 = myfrozen ( '<STR_LIT:abc>' ) <EOL> s2 = s1 . copy ( ) <EOL> assert s1 is not s2 <EOL> assert s1 == s2 <EOL> assert type ( s2 ) is myfrozen <EOL> class myfrozen ( frozenset ) : <EOL> def __new__ ( cls ) : <EOL> return frozenset . __new__ ( cls , '<STR_LIT:abc>' ) <EOL> s1 = myfrozen ( ) <EOL> raises ( TypeError , s1 . copy ) <EOL> def test_update ( self ) : <EOL> s1 = set ( '<STR_LIT:abc>' ) <EOL> s1 . update ( set ( '<STR_LIT>' ) ) <EOL> assert s1 == set ( '<STR_LIT>' ) <EOL> s1 = set ( '<STR_LIT:abc>' ) <EOL> s1 . update ( frozenset ( '<STR_LIT>' ) ) <EOL> assert s1 == set ( '<STR_LIT>' ) <EOL> def test_recursive_repr ( self ) : <EOL> class A ( object ) : <EOL> def __init__ ( self , s ) : <EOL> self . s = s <EOL> def __repr__ ( self ) : <EOL> return repr ( self . s ) <EOL> s = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> s . add ( A ( s ) ) <EOL> therepr = repr ( s ) <EOL> assert therepr . startswith ( "<STR_LIT>" ) <EOL> assert therepr . endswith ( "<STR_LIT>" ) <EOL> inner = set ( therepr [ <NUM_LIT:5> : - <NUM_LIT:2> ] . split ( "<STR_LIT:U+002CU+0020>" ) ) <EOL> assert inner == set ( [ "<STR_LIT:1>" , "<STR_LIT:2>" , "<STR_LIT:3>" , "<STR_LIT>" ] ) <EOL> def test_recursive_repr_frozenset ( self ) : <EOL> class A ( object ) : <EOL> def __repr__ ( self ) : <EOL> return repr ( self . s ) <EOL> a = A ( ) <EOL> s = frozenset ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , a ] ) <EOL> a . s = s <EOL> therepr = repr ( s ) <EOL> assert therepr . startswith ( "<STR_LIT>" ) <EOL> assert therepr . endswith ( "<STR_LIT>" ) <EOL> inner = set ( therepr [ <NUM_LIT:11> : - <NUM_LIT:2> ] . split ( "<STR_LIT:U+002CU+0020>" ) ) <EOL> assert inner == set ( [ "<STR_LIT:1>" , "<STR_LIT:2>" , "<STR_LIT:3>" , "<STR_LIT>" ] ) <EOL> def test_keyerror_has_key ( self ) : <EOL> s = set ( ) <EOL> try : <EOL> s . remove ( <NUM_LIT:1> ) <EOL> except KeyError , e : <EOL> assert e . args [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> else : <EOL> assert <NUM_LIT:0> , "<STR_LIT>" <EOL> def test_subclass_with_hash ( self ) : <EOL> class H ( set ) : <EOL> def __hash__ ( self ) : <EOL> return int ( id ( self ) & <NUM_LIT> ) <EOL> s = H ( ) <EOL> f = set ( [ s ] ) <EOL> print f <EOL> assert s in f <EOL> f . remove ( s ) <EOL> f . add ( s ) <EOL> f . discard ( s ) <EOL> def test_autoconvert_to_frozen__contains ( self ) : <EOL> s = set ( [ frozenset ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ] ) <EOL> assert set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) in s <EOL> def test_autoconvert_to_frozen_remove ( self ) : <EOL> s = set ( [ frozenset ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ] ) <EOL> s . remove ( set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> assert len ( s ) == <NUM_LIT:0> <EOL> raises ( KeyError , s . remove , set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> def test_autoconvert_to_frozen_discard ( self ) : <EOL> s = set ( [ frozenset ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ] ) <EOL> s . discard ( set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> assert len ( s ) == <NUM_LIT:0> <EOL> s . discard ( set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> def test_autoconvert_to_frozen_onlyon_type_error ( self ) : <EOL> class A ( set ) : <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> s = A ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> s2 = set ( [ <NUM_LIT:2> , <NUM_LIT:3> , s ] ) <EOL> assert A ( ) not in s2 <EOL> s2 . add ( frozenset ( ) ) <EOL> assert A ( ) not in s2 <EOL> raises ( KeyError , s2 . remove , A ( ) ) <EOL> def test_autoconvert_key_error ( self ) : <EOL> s = set ( [ frozenset ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , frozenset ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] ) <EOL> try : <EOL> s . remove ( set ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> except KeyError , e : <EOL> assert isinstance ( e . args [ <NUM_LIT:0> ] , frozenset ) <EOL> assert e . args [ <NUM_LIT:0> ] == frozenset ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_contains ( self ) : <EOL> letters = '<STR_LIT>' <EOL> word = '<STR_LIT>' <EOL> s = set ( word ) <EOL> for c in letters : <EOL> assert ( c in s ) == ( c in word ) <EOL> raises ( TypeError , s . __contains__ , [ ] ) <EOL> def test_remove ( self ) : <EOL> s = set ( '<STR_LIT:abc>' ) <EOL> s . remove ( '<STR_LIT:a>' ) <EOL> assert '<STR_LIT:a>' not in s <EOL> raises ( KeyError , s . remove , '<STR_LIT:a>' ) <EOL> raises ( TypeError , s . remove , [ ] ) <EOL> s . add ( frozenset ( '<STR_LIT>' ) ) <EOL> assert set ( '<STR_LIT>' ) in s <EOL> s . remove ( set ( '<STR_LIT>' ) ) <EOL> assert set ( '<STR_LIT>' ) not in s <EOL> raises ( KeyError , s . remove , set ( '<STR_LIT>' ) ) <EOL> def test_remove_keyerror_unpacking ( self ) : <EOL> s = set ( ) <EOL> for v1 in [ '<STR_LIT>' , ( <NUM_LIT:1> , ) ] : <EOL> try : <EOL> s . remove ( v1 ) <EOL> except KeyError , e : <EOL> v2 = e . args [ <NUM_LIT:0> ] <EOL> assert v1 == v2 <EOL> else : <EOL> assert False , '<STR_LIT>' <EOL> def test_singleton_empty_frozenset ( self ) : <EOL> class Frozenset ( frozenset ) : <EOL> pass <EOL> f = frozenset ( ) <EOL> F = Frozenset ( ) <EOL> efs = [ f , Frozenset ( f ) ] <EOL> assert len ( set ( map ( id , efs ) ) ) == len ( efs ) </s>
