<s> from ctypes import * <EOL> import unittest <EOL> import os <EOL> import ctypes <EOL> import _ctypes_test <EOL> class BITS ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:A>" , c_int , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:B>" , c_int , <NUM_LIT:2> ) , <EOL> ( "<STR_LIT:C>" , c_int , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:D>" , c_int , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT:E>" , c_int , <NUM_LIT:5> ) , <EOL> ( "<STR_LIT:F>" , c_int , <NUM_LIT:6> ) , <EOL> ( "<STR_LIT>" , c_int , <NUM_LIT:7> ) , <EOL> ( "<STR_LIT:H>" , c_int , <NUM_LIT:8> ) , <EOL> ( "<STR_LIT:I>" , c_int , <NUM_LIT:9> ) , <EOL> ( "<STR_LIT:M>" , c_short , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:N>" , c_short , <NUM_LIT:2> ) , <EOL> ( "<STR_LIT:O>" , c_short , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:P>" , c_short , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT>" , c_short , <NUM_LIT:5> ) , <EOL> ( "<STR_LIT:R>" , c_short , <NUM_LIT:6> ) , <EOL> ( "<STR_LIT:S>" , c_short , <NUM_LIT:7> ) ] <EOL> func = CDLL ( _ctypes_test . __file__ ) . unpack_bitfields <EOL> func . argtypes = POINTER ( BITS ) , c_char <EOL> class C_Test ( unittest . TestCase ) : <EOL> def test_ints ( self ) : <EOL> for i in range ( <NUM_LIT> ) : <EOL> for name in "<STR_LIT>" : <EOL> b = BITS ( ) <EOL> setattr ( b , name , i ) <EOL> self . failUnlessEqual ( ( name , i , getattr ( b , name ) ) , ( name , i , func ( byref ( b ) , name ) ) ) <EOL> def test_shorts ( self ) : <EOL> for i in range ( <NUM_LIT> ) : <EOL> for name in "<STR_LIT>" : <EOL> b = BITS ( ) <EOL> setattr ( b , name , i ) <EOL> self . failUnlessEqual ( ( name , i , getattr ( b , name ) ) , ( name , i , func ( byref ( b ) , name ) ) ) <EOL> signed_int_types = ( c_byte , c_short , c_int , c_long , c_longlong ) <EOL> unsigned_int_types = ( c_ubyte , c_ushort , c_uint , c_ulong , c_ulonglong ) <EOL> int_types = unsigned_int_types + signed_int_types <EOL> class BitFieldTest ( unittest . TestCase ) : <EOL> def test_longlong ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_longlong , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:b>" , c_longlong , <NUM_LIT> ) , <EOL> ( "<STR_LIT:c>" , c_longlong , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_longlong ) ) <EOL> x = X ( ) <EOL> x . a , x . b , x . c = - <NUM_LIT:1> , <NUM_LIT:7> , - <NUM_LIT:1> <EOL> self . failUnlessEqual ( ( x . a , x . b , x . c ) , ( - <NUM_LIT:1> , <NUM_LIT:7> , - <NUM_LIT:1> ) ) <EOL> def test_ulonglong ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_ulonglong , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:b>" , c_ulonglong , <NUM_LIT> ) , <EOL> ( "<STR_LIT:c>" , c_ulonglong , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_longlong ) ) <EOL> x = X ( ) <EOL> self . failUnlessEqual ( ( x . a , x . b , x . c ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> x . a , x . b , x . c = <NUM_LIT:7> , <NUM_LIT:7> , <NUM_LIT:7> <EOL> self . failUnlessEqual ( ( x . a , x . b , x . c ) , ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:1> ) ) <EOL> def test_signed ( self ) : <EOL> for c_typ in signed_int_types : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_typ ) , <EOL> ( "<STR_LIT:a>" , c_typ , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:b>" , c_typ , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:c>" , c_typ , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_typ ) * <NUM_LIT:2> ) <EOL> x = X ( ) <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> x . a = - <NUM_LIT:1> <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> x . a , x . b = <NUM_LIT:0> , - <NUM_LIT:1> <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def test_unsigned ( self ) : <EOL> for c_typ in unsigned_int_types : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_typ , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:b>" , c_typ , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:c>" , c_typ , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_typ ) ) <EOL> x = X ( ) <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> x . a = - <NUM_LIT:1> <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , <NUM_LIT:7> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> x . a , x . b = <NUM_LIT:0> , - <NUM_LIT:1> <EOL> self . failUnlessEqual ( ( c_typ , x . a , x . b , x . c ) , ( c_typ , <NUM_LIT:0> , <NUM_LIT:7> , <NUM_LIT:0> ) ) <EOL> def fail_fields ( self , * fields ) : <EOL> return self . get_except ( type ( Structure ) , "<STR_LIT:X>" , ( ) , <EOL> { "<STR_LIT>" : fields } ) <EOL> def test_nonint_types ( self ) : <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_char_p , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_void_p , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> if c_int != c_long : <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , POINTER ( c_int ) , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_char , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> try : <EOL> c_wchar <EOL> except NameError : <EOL> pass <EOL> else : <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_wchar , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> class Dummy ( Structure ) : <EOL> _fields_ = [ ] <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , Dummy , <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( TypeError , '<STR_LIT>' ) ) <EOL> def test_single_bitfield_size ( self ) : <EOL> for c_typ in int_types : <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_typ , - <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( ValueError , '<STR_LIT>' ) ) <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_typ , <NUM_LIT:0> ) ) <EOL> self . failUnlessEqual ( result , ( ValueError , '<STR_LIT>' ) ) <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_typ , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_typ ) ) <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_typ , sizeof ( c_typ ) * <NUM_LIT:8> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_typ ) ) <EOL> result = self . fail_fields ( ( "<STR_LIT:a>" , c_typ , sizeof ( c_typ ) * <NUM_LIT:8> + <NUM_LIT:1> ) ) <EOL> self . failUnlessEqual ( result , ( ValueError , '<STR_LIT>' ) ) <EOL> def test_multi_bitfields_size ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_short , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:b>" , c_short , <NUM_LIT> ) , <EOL> ( "<STR_LIT:c>" , c_short , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_short ) ) <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_short , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT>" , c_short ) , <EOL> ( "<STR_LIT:b>" , c_short , <NUM_LIT> ) , <EOL> ( "<STR_LIT:c>" , c_short , <NUM_LIT:1> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_short ) * <NUM_LIT:3> ) <EOL> self . failUnlessEqual ( X . a . offset , <NUM_LIT:0> ) <EOL> self . failUnlessEqual ( X . a1 . offset , sizeof ( c_short ) ) <EOL> self . failUnlessEqual ( X . b . offset , sizeof ( c_short ) * <NUM_LIT:2> ) <EOL> self . failUnlessEqual ( X . c . offset , sizeof ( c_short ) * <NUM_LIT:2> ) <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_short , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT:b>" , c_short , <NUM_LIT> ) , <EOL> ( "<STR_LIT:c>" , c_short , <NUM_LIT> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_short ) * <NUM_LIT:3> ) <EOL> self . failUnlessEqual ( X . a . offset , sizeof ( c_short ) * <NUM_LIT:0> ) <EOL> self . failUnlessEqual ( X . b . offset , sizeof ( c_short ) * <NUM_LIT:1> ) <EOL> self . failUnlessEqual ( X . c . offset , sizeof ( c_short ) * <NUM_LIT:2> ) <EOL> def get_except ( self , func , * args , ** kw ) : <EOL> try : <EOL> func ( * args , ** kw ) <EOL> except Exception , detail : <EOL> return detail . __class__ , str ( detail ) <EOL> def test_mixed_1 ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_byte , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT:b>" , c_int , <NUM_LIT:4> ) ] <EOL> if os . name in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_int ) * <NUM_LIT:2> ) <EOL> else : <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_int ) ) <EOL> def test_mixed_2 ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_byte , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT:b>" , c_int , <NUM_LIT:32> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_int ) * <NUM_LIT:2> ) <EOL> def test_mixed_3 ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_byte , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT:b>" , c_ubyte , <NUM_LIT:4> ) ] <EOL> self . failUnlessEqual ( sizeof ( X ) , sizeof ( c_byte ) ) <EOL> def test_anon_bitfields ( self ) : <EOL> class X ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_byte , <NUM_LIT:4> ) , <EOL> ( "<STR_LIT:b>" , c_ubyte , <NUM_LIT:4> ) ] <EOL> class Y ( Structure ) : <EOL> _anonymous_ = [ "<STR_LIT:_>" ] <EOL> _fields_ = [ ( "<STR_LIT:_>" , X ) ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
