@@ -24,6 +24,8 @@ def valid_ranges(*types):
2424unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong ]
2525signed_types = [c_byte , c_short , c_int , c_long , c_longlong ]
2626
27+ bool_types = []
28+
2729float_types = [c_double , c_float ]
2830
2931try :
@@ -35,8 +37,16 @@ def valid_ranges(*types):
3537 unsigned_types .append (c_ulonglong )
3638 signed_types .append (c_longlong )
3739
40+ try :
41+ c_bool
42+ except NameError :
43+ pass
44+ else :
45+ bool_types .append (c_bool )
46+
3847unsigned_ranges = valid_ranges (* unsigned_types )
3948signed_ranges = valid_ranges (* signed_types )
49+ bool_values = [True , False , 0 , 1 , - 1 , 5000 , 'test' , [], [1 ]]
4050
4151################################################################
4252
@@ -59,6 +69,11 @@ def test_signed_values(self):
5969 for t , (l , h ) in zip (signed_types , signed_ranges ):
6070 self .failUnlessEqual (t (l ).value , l )
6171 self .failUnlessEqual (t (h ).value , h )
72+
73+ def test_bool_values (self ):
74+ from operator import truth
75+ for t , v in zip (bool_types , bool_values ):
76+ self .failUnlessEqual (t (v ).value , truth (v ))
6277
6378 def test_typeerror (self ):
6479 # Only numbers are allowed in the contructor,
@@ -82,7 +97,7 @@ def test_from_param(self):
8297
8398 def test_byref (self ):
8499 # calling byref returns also a PyCArgObject instance
85- for t in signed_types + unsigned_types + float_types :
100+ for t in signed_types + unsigned_types + float_types + bool_types :
86101 parm = byref (t ())
87102 self .failUnlessEqual (ArgType , type (parm ))
88103
@@ -101,7 +116,7 @@ def test_integers(self):
101116 self .assertRaises (TypeError , t , 3.14 )
102117
103118 def test_sizes (self ):
104- for t in signed_types + unsigned_types + float_types :
119+ for t in signed_types + unsigned_types + float_types + bool_types :
105120 size = struct .calcsize (t ._type_ )
106121 # sizeof of the type...
107122 self .failUnlessEqual (sizeof (t ), size )
@@ -163,6 +178,18 @@ def test_char_from_address(self):
163178
164179 a [0 ] = '?'
165180 self .failUnlessEqual (v .value , a [0 ])
181+
182+ # array does not support c_bool / 't'
183+ # def test_bool_from_address(self):
184+ # from ctypes import c_bool
185+ # from array import array
186+ # a = array(c_bool._type_, [True])
187+ # v = t.from_address(a.buffer_info()[0])
188+ # self.failUnlessEqual(v.value, a[0])
189+ # self.failUnlessEqual(type(v) is t)
190+ # a[0] = False
191+ # self.failUnlessEqual(v.value, a[0])
192+ # self.failUnlessEqual(type(v) is t)
166193
167194 def test_init (self ):
168195 # c_int() can be initialized from Python's int, and c_int.
0 commit comments