@@ -24,6 +24,8 @@ def valid_ranges(*types):
24
24
unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong ]
25
25
signed_types = [c_byte , c_short , c_int , c_long , c_longlong ]
26
26
27
+ bool_types = []
28
+
27
29
float_types = [c_double , c_float ]
28
30
29
31
try :
@@ -35,8 +37,16 @@ def valid_ranges(*types):
35
37
unsigned_types .append (c_ulonglong )
36
38
signed_types .append (c_longlong )
37
39
40
+ try :
41
+ c_bool
42
+ except NameError :
43
+ pass
44
+ else :
45
+ bool_types .append (c_bool )
46
+
38
47
unsigned_ranges = valid_ranges (* unsigned_types )
39
48
signed_ranges = valid_ranges (* signed_types )
49
+ bool_values = [True , False , 0 , 1 , - 1 , 5000 , 'test' , [], [1 ]]
40
50
41
51
################################################################
42
52
@@ -59,6 +69,11 @@ def test_signed_values(self):
59
69
for t , (l , h ) in zip (signed_types , signed_ranges ):
60
70
self .failUnlessEqual (t (l ).value , l )
61
71
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 ))
62
77
63
78
def test_typeerror (self ):
64
79
# Only numbers are allowed in the contructor,
@@ -82,7 +97,7 @@ def test_from_param(self):
82
97
83
98
def test_byref (self ):
84
99
# 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 :
86
101
parm = byref (t ())
87
102
self .failUnlessEqual (ArgType , type (parm ))
88
103
@@ -101,7 +116,7 @@ def test_integers(self):
101
116
self .assertRaises (TypeError , t , 3.14 )
102
117
103
118
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 :
105
120
size = struct .calcsize (t ._type_ )
106
121
# sizeof of the type...
107
122
self .failUnlessEqual (sizeof (t ), size )
@@ -163,6 +178,18 @@ def test_char_from_address(self):
163
178
164
179
a [0 ] = '?'
165
180
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)
166
193
167
194
def test_init (self ):
168
195
# c_int() can be initialized from Python's int, and c_int.
0 commit comments