The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_bool.py
blob2a9ef5dd7ccfb8e16cfcb5204ae684d1a8d8d90c
1 # Test properties of bool promised by PEP 285
3 import unittest
4 from test import test_support
6 import os
8 class BoolTest(unittest.TestCase):
10 def assertIs(self, a, b):
11 self.assertTrue(a is b)
13 def assertIsNot(self, a, b):
14 self.assertTrue(a is not b)
16 def test_subclass(self):
17 try:
18 class C(bool):
19 pass
20 except TypeError:
21 pass
22 else:
23 self.fail("bool should not be subclassable")
25 self.assertRaises(TypeError, int.__new__, bool, 0)
27 def test_print(self):
28 try:
29 fo = open(test_support.TESTFN, "wb")
30 print >> fo, False, True
31 fo.close()
32 fo = open(test_support.TESTFN, "rb")
33 self.assertEqual(fo.read(), 'False True\n')
34 finally:
35 fo.close()
36 os.remove(test_support.TESTFN)
38 def test_repr(self):
39 self.assertEqual(repr(False), 'False')
40 self.assertEqual(repr(True), 'True')
41 self.assertEqual(eval(repr(False)), False)
42 self.assertEqual(eval(repr(True)), True)
44 def test_str(self):
45 self.assertEqual(str(False), 'False')
46 self.assertEqual(str(True), 'True')
48 def test_int(self):
49 self.assertEqual(int(False), 0)
50 self.assertIsNot(int(False), False)
51 self.assertEqual(int(True), 1)
52 self.assertIsNot(int(True), True)
54 def test_math(self):
55 self.assertEqual(+False, 0)
56 self.assertIsNot(+False, False)
57 self.assertEqual(-False, 0)
58 self.assertIsNot(-False, False)
59 self.assertEqual(abs(False), 0)
60 self.assertIsNot(abs(False), False)
61 self.assertEqual(+True, 1)
62 self.assertIsNot(+True, True)
63 self.assertEqual(-True, -1)
64 self.assertEqual(abs(True), 1)
65 self.assertIsNot(abs(True), True)
66 self.assertEqual(~False, -1)
67 self.assertEqual(~True, -2)
69 self.assertEqual(False+2, 2)
70 self.assertEqual(True+2, 3)
71 self.assertEqual(2+False, 2)
72 self.assertEqual(2+True, 3)
74 self.assertEqual(False+False, 0)
75 self.assertIsNot(False+False, False)
76 self.assertEqual(False+True, 1)
77 self.assertIsNot(False+True, True)
78 self.assertEqual(True+False, 1)
79 self.assertIsNot(True+False, True)
80 self.assertEqual(True+True, 2)
82 self.assertEqual(True-True, 0)
83 self.assertIsNot(True-True, False)
84 self.assertEqual(False-False, 0)
85 self.assertIsNot(False-False, False)
86 self.assertEqual(True-False, 1)
87 self.assertIsNot(True-False, True)
88 self.assertEqual(False-True, -1)
90 self.assertEqual(True*1, 1)
91 self.assertEqual(False*1, 0)
92 self.assertIsNot(False*1, False)
94 self.assertEqual(True/1, 1)
95 self.assertIsNot(True/1, True)
96 self.assertEqual(False/1, 0)
97 self.assertIsNot(False/1, False)
99 for b in False, True:
100 for i in 0, 1, 2:
101 self.assertEqual(b**i, int(b)**i)
102 self.assertIsNot(b**i, bool(int(b)**i))
104 for a in False, True:
105 for b in False, True:
106 self.assertIs(a&b, bool(int(a)&int(b)))
107 self.assertIs(a|b, bool(int(a)|int(b)))
108 self.assertIs(a^b, bool(int(a)^int(b)))
109 self.assertEqual(a&int(b), int(a)&int(b))
110 self.assertIsNot(a&int(b), bool(int(a)&int(b)))
111 self.assertEqual(a|int(b), int(a)|int(b))
112 self.assertIsNot(a|int(b), bool(int(a)|int(b)))
113 self.assertEqual(a^int(b), int(a)^int(b))
114 self.assertIsNot(a^int(b), bool(int(a)^int(b)))
115 self.assertEqual(int(a)&b, int(a)&int(b))
116 self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
117 self.assertEqual(int(a)|b, int(a)|int(b))
118 self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
119 self.assertEqual(int(a)^b, int(a)^int(b))
120 self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
122 self.assertIs(1==1, True)
123 self.assertIs(1==0, False)
124 self.assertIs(0<1, True)
125 self.assertIs(1<0, False)
126 self.assertIs(0<=0, True)
127 self.assertIs(1<=0, False)
128 self.assertIs(1>0, True)
129 self.assertIs(1>1, False)
130 self.assertIs(1>=1, True)
131 self.assertIs(0>=1, False)
132 self.assertIs(0!=1, True)
133 self.assertIs(0!=0, False)
135 x = [1]
136 self.assertIs(x is x, True)
137 self.assertIs(x is not x, False)
139 self.assertIs(1 in x, True)
140 self.assertIs(0 in x, False)
141 self.assertIs(1 not in x, False)
142 self.assertIs(0 not in x, True)
144 x = {1: 2}
145 self.assertIs(x is x, True)
146 self.assertIs(x is not x, False)
148 self.assertIs(1 in x, True)
149 self.assertIs(0 in x, False)
150 self.assertIs(1 not in x, False)
151 self.assertIs(0 not in x, True)
153 self.assertIs(not True, False)
154 self.assertIs(not False, True)
156 def test_convert(self):
157 self.assertRaises(TypeError, bool, 42, 42)
158 self.assertIs(bool(10), True)
159 self.assertIs(bool(1), True)
160 self.assertIs(bool(-1), True)
161 self.assertIs(bool(0), False)
162 self.assertIs(bool("hello"), True)
163 self.assertIs(bool(""), False)
164 self.assertIs(bool(), False)
166 def test_hasattr(self):
167 self.assertIs(hasattr([], "append"), True)
168 self.assertIs(hasattr([], "wobble"), False)
170 def test_callable(self):
171 self.assertIs(callable(len), True)
172 self.assertIs(callable(1), False)
174 def test_isinstance(self):
175 self.assertIs(isinstance(True, bool), True)
176 self.assertIs(isinstance(False, bool), True)
177 self.assertIs(isinstance(True, int), True)
178 self.assertIs(isinstance(False, int), True)
179 self.assertIs(isinstance(1, bool), False)
180 self.assertIs(isinstance(0, bool), False)
182 def test_issubclass(self):
183 self.assertIs(issubclass(bool, int), True)
184 self.assertIs(issubclass(int, bool), False)
186 def test_haskey(self):
187 self.assertIs({}.has_key(1), False)
188 self.assertIs({1:1}.has_key(1), True)
190 def test_string(self):
191 self.assertIs("xyz".endswith("z"), True)
192 self.assertIs("xyz".endswith("x"), False)
193 self.assertIs("xyz0123".isalnum(), True)
194 self.assertIs("@#$%".isalnum(), False)
195 self.assertIs("xyz".isalpha(), True)
196 self.assertIs("@#$%".isalpha(), False)
197 self.assertIs("0123".isdigit(), True)
198 self.assertIs("xyz".isdigit(), False)
199 self.assertIs("xyz".islower(), True)
200 self.assertIs("XYZ".islower(), False)
201 self.assertIs(" ".isspace(), True)
202 self.assertIs("XYZ".isspace(), False)
203 self.assertIs("X".istitle(), True)
204 self.assertIs("x".istitle(), False)
205 self.assertIs("XYZ".isupper(), True)
206 self.assertIs("xyz".isupper(), False)
207 self.assertIs("xyz".startswith("x"), True)
208 self.assertIs("xyz".startswith("z"), False)
210 if test_support.have_unicode:
211 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True)
212 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False)
213 self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True)
214 self.assertIs(unicode("@#$%", 'ascii').isalnum(), False)
215 self.assertIs(unicode("xyz", 'ascii').isalpha(), True)
216 self.assertIs(unicode("@#$%", 'ascii').isalpha(), False)
217 self.assertIs(unicode("0123", 'ascii').isdecimal(), True)
218 self.assertIs(unicode("xyz", 'ascii').isdecimal(), False)
219 self.assertIs(unicode("0123", 'ascii').isdigit(), True)
220 self.assertIs(unicode("xyz", 'ascii').isdigit(), False)
221 self.assertIs(unicode("xyz", 'ascii').islower(), True)
222 self.assertIs(unicode("XYZ", 'ascii').islower(), False)
223 self.assertIs(unicode("0123", 'ascii').isnumeric(), True)
224 self.assertIs(unicode("xyz", 'ascii').isnumeric(), False)
225 self.assertIs(unicode(" ", 'ascii').isspace(), True)
226 self.assertIs(unicode("XYZ", 'ascii').isspace(), False)
227 self.assertIs(unicode("X", 'ascii').istitle(), True)
228 self.assertIs(unicode("x", 'ascii').istitle(), False)
229 self.assertIs(unicode("XYZ", 'ascii').isupper(), True)
230 self.assertIs(unicode("xyz", 'ascii').isupper(), False)
231 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True)
232 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False)
234 def test_boolean(self):
235 self.assertEqual(True & 1, 1)
236 self.assertTrue(not isinstance(True & 1, bool))
237 self.assertIs(True & True, True)
239 self.assertEqual(True | 1, 1)
240 self.assertTrue(not isinstance(True | 1, bool))
241 self.assertIs(True | True, True)
243 self.assertEqual(True ^ 1, 0)
244 self.assertTrue(not isinstance(True ^ 1, bool))
245 self.assertIs(True ^ True, False)
247 def test_fileclosed(self):
248 try:
249 f = file(test_support.TESTFN, "w")
250 self.assertIs(f.closed, False)
251 f.close()
252 self.assertIs(f.closed, True)
253 finally:
254 os.remove(test_support.TESTFN)
256 def test_operator(self):
257 import operator
258 self.assertIs(operator.truth(0), False)
259 self.assertIs(operator.truth(1), True)
260 self.assertIs(operator.isCallable(0), False)
261 self.assertIs(operator.isCallable(len), True)
262 self.assertIs(operator.isNumberType(None), False)
263 self.assertIs(operator.isNumberType(0), True)
264 self.assertIs(operator.not_(1), False)
265 self.assertIs(operator.not_(0), True)
266 self.assertIs(operator.isSequenceType(0), False)
267 self.assertIs(operator.isSequenceType([]), True)
268 self.assertIs(operator.contains([], 1), False)
269 self.assertIs(operator.contains([1], 1), True)
270 self.assertIs(operator.isMappingType(1), False)
271 self.assertIs(operator.isMappingType({}), True)
272 self.assertIs(operator.lt(0, 0), False)
273 self.assertIs(operator.lt(0, 1), True)
274 self.assertIs(operator.is_(True, True), True)
275 self.assertIs(operator.is_(True, False), False)
276 self.assertIs(operator.is_not(True, True), False)
277 self.assertIs(operator.is_not(True, False), True)
279 def test_marshal(self):
280 import marshal
281 self.assertIs(marshal.loads(marshal.dumps(True)), True)
282 self.assertIs(marshal.loads(marshal.dumps(False)), False)
284 def test_pickle(self):
285 import pickle
286 self.assertIs(pickle.loads(pickle.dumps(True)), True)
287 self.assertIs(pickle.loads(pickle.dumps(False)), False)
288 self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
289 self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
291 def test_cpickle(self):
292 import cPickle
293 self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
294 self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
295 self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
296 self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
298 def test_mixedpickle(self):
299 import pickle, cPickle
300 self.assertIs(pickle.loads(cPickle.dumps(True)), True)
301 self.assertIs(pickle.loads(cPickle.dumps(False)), False)
302 self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
303 self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
305 self.assertIs(cPickle.loads(pickle.dumps(True)), True)
306 self.assertIs(cPickle.loads(pickle.dumps(False)), False)
307 self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
308 self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
310 def test_picklevalues(self):
311 import pickle, cPickle
313 # Test for specific backwards-compatible pickle values
314 self.assertEqual(pickle.dumps(True), "I01\n.")
315 self.assertEqual(pickle.dumps(False), "I00\n.")
316 self.assertEqual(cPickle.dumps(True), "I01\n.")
317 self.assertEqual(cPickle.dumps(False), "I00\n.")
318 self.assertEqual(pickle.dumps(True, True), "I01\n.")
319 self.assertEqual(pickle.dumps(False, True), "I00\n.")
320 self.assertEqual(cPickle.dumps(True, True), "I01\n.")
321 self.assertEqual(cPickle.dumps(False, True), "I00\n.")
323 def test_convert_to_bool(self):
324 # Verify that TypeError occurs when bad things are returned
325 # from __nonzero__(). This isn't really a bool test, but
326 # it's related.
327 check = lambda o: self.assertRaises(TypeError, bool, o)
328 class Foo(object):
329 def __nonzero__(self):
330 return self
331 check(Foo())
333 class Bar(object):
334 def __nonzero__(self):
335 return "Yes"
336 check(Bar())
338 class Baz(int):
339 def __nonzero__(self):
340 return self
341 check(Baz())
344 def test_main():
345 test_support.run_unittest(BoolTest)
347 if __name__ == "__main__":
348 test_main()