<s> import unittest <EOL> from test import test_support <EOL> import operator <EOL> class Number : <EOL> def __init__ ( self , x ) : <EOL> self . x = x <EOL> def __lt__ ( self , other ) : <EOL> return self . x < other <EOL> def __le__ ( self , other ) : <EOL> return self . x <= other <EOL> def __eq__ ( self , other ) : <EOL> return self . x == other <EOL> def __ne__ ( self , other ) : <EOL> return self . x != other <EOL> def __gt__ ( self , other ) : <EOL> return self . x > other <EOL> def __ge__ ( self , other ) : <EOL> return self . x >= other <EOL> def __cmp__ ( self , other ) : <EOL> raise test_support . TestFailed , "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . x , ) <EOL> class Vector : <EOL> def __init__ ( self , data ) : <EOL> self . data = data <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> def __getitem__ ( self , i ) : <EOL> return self . data [ i ] <EOL> def __setitem__ ( self , i , v ) : <EOL> self . data [ i ] = v <EOL> def __hash__ ( self ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> def __nonzero__ ( self ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> def __cmp__ ( self , other ) : <EOL> raise test_support . TestFailed , "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . data , ) <EOL> def __lt__ ( self , other ) : <EOL> return Vector ( [ a < b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __le__ ( self , other ) : <EOL> return Vector ( [ a <= b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __eq__ ( self , other ) : <EOL> return Vector ( [ a == b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __ne__ ( self , other ) : <EOL> return Vector ( [ a != b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __gt__ ( self , other ) : <EOL> return Vector ( [ a > b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __ge__ ( self , other ) : <EOL> return Vector ( [ a >= b for a , b in zip ( self . data , self . __cast ( other ) ) ] ) <EOL> def __cast ( self , other ) : <EOL> if isinstance ( other , Vector ) : <EOL> other = other . data <EOL> if len ( self . data ) != len ( other ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> return other <EOL> opmap = { <EOL> "<STR_LIT>" : ( lambda a , b : a < b , operator . lt , operator . __lt__ ) , <EOL> "<STR_LIT>" : ( lambda a , b : a <= b , operator . le , operator . __le__ ) , <EOL> "<STR_LIT>" : ( lambda a , b : a == b , operator . eq , operator . __eq__ ) , <EOL> "<STR_LIT>" : ( lambda a , b : a != b , operator . ne , operator . __ne__ ) , <EOL> "<STR_LIT>" : ( lambda a , b : a > b , operator . gt , operator . __gt__ ) , <EOL> "<STR_LIT>" : ( lambda a , b : a >= b , operator . ge , operator . __ge__ ) <EOL> } <EOL> class VectorTest ( unittest . TestCase ) : <EOL> def checkfail ( self , error , opname , * args ) : <EOL> for op in opmap [ opname ] : <EOL> self . assertRaises ( error , op , * args ) <EOL> def checkequal ( self , opname , a , b , expres ) : <EOL> for op in opmap [ opname ] : <EOL> realres = op ( a , b ) <EOL> self . assertEqual ( len ( realres ) , len ( expres ) ) <EOL> for i in xrange ( len ( realres ) ) : <EOL> self . assert_ ( realres [ i ] is expres [ i ] ) <EOL> def test_mixed ( self ) : <EOL> a = Vector ( range ( <NUM_LIT:2> ) ) <EOL> b = Vector ( range ( <NUM_LIT:3> ) ) <EOL> for opname in opmap : <EOL> self . checkfail ( ValueError , opname , a , b ) <EOL> a = range ( <NUM_LIT:5> ) <EOL> b = <NUM_LIT:5> * [ <NUM_LIT:2> ] <EOL> args = [ ( a , Vector ( b ) ) , ( Vector ( a ) , b ) , ( Vector ( a ) , Vector ( b ) ) ] <EOL> for ( a , b ) in args : <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ True , True , False , False , False ] ) <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ True , True , True , False , False ] ) <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ False , False , True , False , False ] ) <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ True , True , False , True , True ] ) <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ False , False , False , True , True ] ) <EOL> self . checkequal ( "<STR_LIT>" , a , b , [ False , False , True , True , True ] ) <EOL> for ops in opmap . itervalues ( ) : <EOL> for op in ops : <EOL> self . assertRaises ( TypeError , bool , op ( a , b ) ) <EOL> class NumberTest ( unittest . TestCase ) : <EOL> def test_basic ( self ) : <EOL> for a in xrange ( <NUM_LIT:3> ) : <EOL> for b in xrange ( <NUM_LIT:3> ) : <EOL> for typea in ( int , Number ) : <EOL> for typeb in ( int , Number ) : <EOL> if typea == typeb == int : <EOL> continue <EOL> ta = typea ( a ) <EOL> tb = typeb ( b ) <EOL> for ops in opmap . itervalues ( ) : <EOL> for op in ops : <EOL> realoutcome = op ( a , b ) <EOL> testoutcome = op ( ta , tb ) <EOL> self . assertEqual ( realoutcome , testoutcome ) <EOL> def checkvalue ( self , opname , a , b , expres ) : <EOL> for typea in ( int , Number ) : <EOL> for typeb in ( int , Number ) : <EOL> ta = typea ( a ) <EOL> tb = typeb ( b ) <EOL> for op in opmap [ opname ] : <EOL> realres = op ( ta , tb ) <EOL> realres = getattr ( realres , "<STR_LIT:x>" , realres ) <EOL> self . assert_ ( realres is expres ) <EOL> def test_values ( self ) : <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , False ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , True ) <EOL> self . checkvalue ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:0> , True ) <EOL> class MiscTest ( unittest . TestCase ) : <EOL> def test_misbehavin ( self ) : <EOL> class Misb : <EOL> def __lt__ ( self , other ) : return <NUM_LIT:0> <EOL> def __gt__ ( self , other ) : return <NUM_LIT:0> <EOL> def __eq__ ( self , other ) : return <NUM_LIT:0> <EOL> def __le__ ( self , other ) : raise TestFailed , "<STR_LIT>" <EOL> def __ge__ ( self , other ) : raise TestFailed , "<STR_LIT>" <EOL> def __ne__ ( self , other ) : raise TestFailed , "<STR_LIT>" <EOL> def __cmp__ ( self , other ) : raise RuntimeError , "<STR_LIT>" <EOL> a = Misb ( ) <EOL> b = Misb ( ) <EOL> self . assertEqual ( a < b , <NUM_LIT:0> ) <EOL> self . assertEqual ( a == b , <NUM_LIT:0> ) <EOL> self . assertEqual ( a > b , <NUM_LIT:0> ) <EOL> self . assertRaises ( RuntimeError , cmp , a , b ) <EOL> def test_not ( self ) : <EOL> import operator <EOL> class Exc ( Exception ) : <EOL> pass <EOL> class Bad : <EOL> def __nonzero__ ( self ) : <EOL> raise Exc <EOL> def do ( bad ) : <EOL> not bad <EOL> for func in ( do , operator . not_ ) : <EOL> self . assertRaises ( Exc , func , Bad ( ) ) <EOL> def test_recursion ( self ) : <EOL> from UserList import UserList <EOL> a = UserList ( ) <EOL> b = UserList ( ) <EOL> a . append ( b ) <EOL> b . append ( a ) <EOL> self . assertRaises ( RuntimeError , operator . eq , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . ne , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . lt , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . le , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . gt , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . ge , a , b ) <EOL> b . append ( <NUM_LIT> ) <EOL> self . assert_ ( not ( a == b ) ) <EOL> self . assert_ ( a != b ) <EOL> self . assertRaises ( RuntimeError , operator . lt , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . le , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . gt , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . ge , a , b ) <EOL> a . append ( <NUM_LIT> ) <EOL> self . assertRaises ( RuntimeError , operator . eq , a , b ) <EOL> self . assertRaises ( RuntimeError , operator . ne , a , b ) <EOL> a . insert ( <NUM_LIT:0> , <NUM_LIT:11> ) <EOL> b . insert ( <NUM_LIT:0> , <NUM_LIT:12> ) <EOL> self . assert_ ( not ( a == b ) ) <EOL> self . assert_ ( a != b ) <EOL> self . assert_ ( a < b ) <EOL> class DictTest ( unittest . TestCase ) : <EOL> def test_dicts ( self ) : <EOL> import random <EOL> imag1a = { } <EOL> for i in range ( <NUM_LIT:50> ) : <EOL> imag1a [ random . randrange ( <NUM_LIT:100> ) * <NUM_LIT> ] = random . randrange ( <NUM_LIT:100> ) * <NUM_LIT> <EOL> items = imag1a . items ( ) <EOL> random . shuffle ( items ) <EOL> imag1b = { } <EOL> for k , v in items : <EOL> imag1b [ k ] = v <EOL> imag2 = imag1b . copy ( ) <EOL> imag2 [ k ] = v + <NUM_LIT:1.0> <EOL> self . assert_ ( imag1a == imag1a ) <EOL> self . assert_ ( imag1a == imag1b ) <EOL> self . assert_ ( imag2 == imag2 ) <EOL> self . assert_ ( imag1a != imag2 ) <EOL> for opname in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> for op in opmap [ opname ] : <EOL> self . assertRaises ( TypeError , op , imag1a , imag2 ) <EOL> class ListTest ( unittest . TestCase ) : <EOL> def assertIs ( self , a , b ) : <EOL> self . assert_ ( a is b ) <EOL> def test_coverage ( self ) : <EOL> x = [ <NUM_LIT> ] <EOL> self . assertIs ( x < x , False ) <EOL> self . assertIs ( x <= x , True ) <EOL> self . assertIs ( x == x , True ) <EOL> self . assertIs ( x != x , False ) <EOL> self . assertIs ( x > x , False ) <EOL> self . assertIs ( x >= x , True ) <EOL> y = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . assertIs ( x < y , True ) <EOL> self . assertIs ( x <= y , True ) <EOL> self . assertIs ( x == y , False ) <EOL> self . assertIs ( x != y , True ) <EOL> self . assertIs ( x > y , False ) <EOL> self . assertIs ( x >= y , False ) <EOL> def test_badentry ( self ) : <EOL> class Exc ( Exception ) : <EOL> pass <EOL> class Bad : <EOL> def __eq__ ( self , other ) : <EOL> raise Exc <EOL> x = [ Bad ( ) ] <EOL> y = [ Bad ( ) ] <EOL> for op in opmap [ "<STR_LIT>" ] : <EOL> self . assertRaises ( Exc , op , x , y ) <EOL> def test_goodentry ( self ) : <EOL> class Good : <EOL> def __lt__ ( self , other ) : <EOL> return True <EOL> x = [ Good ( ) ] <EOL> y = [ Good ( ) ] <EOL> for op in opmap [ "<STR_LIT>" ] : <EOL> self . assertIs ( op ( x , y ) , True ) <EOL> def test_main ( ) : <EOL> if test_support . is_jython : <EOL> del MiscTest . test_recursion <EOL> test_support . run_unittest ( VectorTest , NumberTest , MiscTest , DictTest , ListTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
