blob: d9fb8870352f8e3bd6e977d03adf5a7a7b86a587 [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001# Test properties of bool promised by PEP 285
2
Walter Dörwalddbcede52003-04-25 10:22:01 +00003import unittest
4from test import test_support
5
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +00006import os
Guido van Rossum5f820362002-04-03 23:01:45 +00007
Walter Dörwalddbcede52003-04-25 10:22:01 +00008from test.test_support import verbose, TestFailed, TESTFN, vereq, have_unicode
9
Guido van Rossum5f820362002-04-03 23:01:45 +000010def veris(a, b):
11 if a is not b:
12 raise TestFailed, "%r is %r" % (a, b)
13
14def verisnot(a, b):
15 if a is b:
16 raise TestFailed, "%r is %r" % (a, b)
17
Walter Dörwalddbcede52003-04-25 10:22:01 +000018class BoolTest(unittest.TestCase):
Guido van Rossum5f820362002-04-03 23:01:45 +000019
Walter Dörwalddbcede52003-04-25 10:22:01 +000020 def assertIs(self, a, b):
21 self.assert_(a is b)
Guido van Rossum5f820362002-04-03 23:01:45 +000022
Walter Dörwalddbcede52003-04-25 10:22:01 +000023 def assertIsNot(self, a, b):
24 self.assert_(a is not b)
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +000025
Walter Dörwalddbcede52003-04-25 10:22:01 +000026 def test_subclass(self):
27 try:
28 class C(bool):
29 pass
30 except TypeError:
31 pass
32 else:
33 self.fail("bool should not be subclassable")
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +000034
Walter Dörwalddbcede52003-04-25 10:22:01 +000035 self.assertRaises(TypeError, int.__new__, bool, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +000036
Walter Dörwalddbcede52003-04-25 10:22:01 +000037 def test_print(self):
38 try:
39 fo = open(test_support.TESTFN, "wb")
40 print >> fo, False, True
41 fo.close()
42 fo = open(test_support.TESTFN, "rb")
43 self.assertEqual(fo.read(), 'False True\n')
44 finally:
45 fo.close()
46 os.remove(test_support.TESTFN)
Guido van Rossum5f820362002-04-03 23:01:45 +000047
Walter Dörwalddbcede52003-04-25 10:22:01 +000048 def test_repr(self):
49 self.assertEqual(repr(False), 'False')
50 self.assertEqual(repr(True), 'True')
51 self.assertEqual(eval(repr(False)), False)
52 self.assertEqual(eval(repr(True)), True)
Guido van Rossum5f820362002-04-03 23:01:45 +000053
Walter Dörwalddbcede52003-04-25 10:22:01 +000054 def test_str(self):
55 self.assertEqual(str(False), 'False')
56 self.assertEqual(str(True), 'True')
Guido van Rossum5f820362002-04-03 23:01:45 +000057
Walter Dörwalddbcede52003-04-25 10:22:01 +000058 def test_int(self):
59 self.assertEqual(int(False), 0)
60 self.assertIsNot(int(False), False)
61 self.assertEqual(int(True), 1)
62 self.assertIsNot(int(True), True)
Guido van Rossum5f820362002-04-03 23:01:45 +000063
Walter Dörwalddbcede52003-04-25 10:22:01 +000064 def test_math(self):
65 self.assertEqual(+False, 0)
66 self.assertIsNot(+False, False)
67 self.assertEqual(-False, 0)
68 self.assertIsNot(-False, False)
69 self.assertEqual(abs(False), 0)
70 self.assertIsNot(abs(False), False)
71 self.assertEqual(+True, 1)
72 self.assertIsNot(+True, True)
73 self.assertEqual(-True, -1)
74 self.assertEqual(abs(True), 1)
75 self.assertIsNot(abs(True), True)
76 self.assertEqual(~False, -1)
77 self.assertEqual(~True, -2)
Guido van Rossum5f820362002-04-03 23:01:45 +000078
Walter Dörwalddbcede52003-04-25 10:22:01 +000079 self.assertEqual(False+2, 2)
80 self.assertEqual(True+2, 3)
81 self.assertEqual(2+False, 2)
82 self.assertEqual(2+True, 3)
Guido van Rossum5f820362002-04-03 23:01:45 +000083
Walter Dörwalddbcede52003-04-25 10:22:01 +000084 self.assertEqual(False+False, 0)
85 self.assertIsNot(False+False, False)
86 self.assertEqual(False+True, 1)
87 self.assertIsNot(False+True, True)
88 self.assertEqual(True+False, 1)
89 self.assertIsNot(True+False, True)
90 self.assertEqual(True+True, 2)
Guido van Rossum5f820362002-04-03 23:01:45 +000091
Walter Dörwalddbcede52003-04-25 10:22:01 +000092 self.assertEqual(True-True, 0)
93 self.assertIsNot(True-True, False)
94 self.assertEqual(False-False, 0)
95 self.assertIsNot(False-False, False)
96 self.assertEqual(True-False, 1)
97 self.assertIsNot(True-False, True)
98 self.assertEqual(False-True, -1)
Guido van Rossum5f820362002-04-03 23:01:45 +000099
Walter Dörwalddbcede52003-04-25 10:22:01 +0000100 self.assertEqual(True*1, 1)
101 self.assertEqual(False*1, 0)
102 self.assertIsNot(False*1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000103
Walter Dörwalddbcede52003-04-25 10:22:01 +0000104 self.assertEqual(True/1, 1)
105 self.assertIsNot(True/1, True)
106 self.assertEqual(False/1, 0)
107 self.assertIsNot(False/1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000108
Walter Dörwalddbcede52003-04-25 10:22:01 +0000109 for b in False, True:
110 for i in 0, 1, 2:
111 self.assertEqual(b**i, int(b)**i)
112 self.assertIsNot(b**i, bool(int(b)**i))
Guido van Rossum5f820362002-04-03 23:01:45 +0000113
Walter Dörwalddbcede52003-04-25 10:22:01 +0000114 for a in False, True:
115 for b in False, True:
116 self.assertIs(a&b, bool(int(a)&int(b)))
117 self.assertIs(a|b, bool(int(a)|int(b)))
118 self.assertIs(a^b, bool(int(a)^int(b)))
119 self.assertEqual(a&int(b), int(a)&int(b))
120 self.assertIsNot(a&int(b), bool(int(a)&int(b)))
121 self.assertEqual(a|int(b), int(a)|int(b))
122 self.assertIsNot(a|int(b), bool(int(a)|int(b)))
123 self.assertEqual(a^int(b), int(a)^int(b))
124 self.assertIsNot(a^int(b), bool(int(a)^int(b)))
125 self.assertEqual(int(a)&b, int(a)&int(b))
126 self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
127 self.assertEqual(int(a)|b, int(a)|int(b))
128 self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
129 self.assertEqual(int(a)^b, int(a)^int(b))
130 self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
Guido van Rossum5f820362002-04-03 23:01:45 +0000131
Walter Dörwalddbcede52003-04-25 10:22:01 +0000132 self.assertIs(1==1, True)
133 self.assertIs(1==0, False)
134 self.assertIs(0<1, True)
135 self.assertIs(1<0, False)
136 self.assertIs(0<=0, True)
137 self.assertIs(1<=0, False)
138 self.assertIs(1>0, True)
139 self.assertIs(1>1, False)
140 self.assertIs(1>=1, True)
141 self.assertIs(0>=1, False)
142 self.assertIs(0!=1, True)
143 self.assertIs(0!=0, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000144
Walter Dörwalddbcede52003-04-25 10:22:01 +0000145 x = [1]
146 self.assertIs(x is x, True)
147 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000148
Walter Dörwalddbcede52003-04-25 10:22:01 +0000149 self.assertIs(1 in x, True)
150 self.assertIs(0 in x, False)
151 self.assertIs(1 not in x, False)
152 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000153
Walter Dörwalddbcede52003-04-25 10:22:01 +0000154 x = {1: 2}
155 self.assertIs(x is x, True)
156 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000157
Walter Dörwalddbcede52003-04-25 10:22:01 +0000158 self.assertIs(1 in x, True)
159 self.assertIs(0 in x, False)
160 self.assertIs(1 not in x, False)
161 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000162
Walter Dörwalddbcede52003-04-25 10:22:01 +0000163 self.assertIs(not True, False)
164 self.assertIs(not False, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000165
Walter Dörwalddbcede52003-04-25 10:22:01 +0000166 def test_convert(self):
167 self.assertRaises(TypeError, bool, 42, 42)
168 self.assertIs(bool(10), True)
169 self.assertIs(bool(1), True)
170 self.assertIs(bool(-1), True)
171 self.assertIs(bool(0), False)
172 self.assertIs(bool("hello"), True)
173 self.assertIs(bool(""), False)
174 self.assertIs(bool(), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000175
Walter Dörwalddbcede52003-04-25 10:22:01 +0000176 def test_hasattr(self):
177 self.assertIs(hasattr([], "append"), True)
178 self.assertIs(hasattr([], "wobble"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000179
Walter Dörwalddbcede52003-04-25 10:22:01 +0000180 def test_callable(self):
181 self.assertIs(callable(len), True)
182 self.assertIs(callable(1), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000183
Walter Dörwalddbcede52003-04-25 10:22:01 +0000184 def test_isinstance(self):
185 self.assertIs(isinstance(True, bool), True)
186 self.assertIs(isinstance(False, bool), True)
187 self.assertIs(isinstance(True, int), True)
188 self.assertIs(isinstance(False, int), True)
189 self.assertIs(isinstance(1, bool), False)
190 self.assertIs(isinstance(0, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000191
Walter Dörwalddbcede52003-04-25 10:22:01 +0000192 def test_issubclass(self):
193 self.assertIs(issubclass(bool, int), True)
194 self.assertIs(issubclass(int, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000195
Walter Dörwalddbcede52003-04-25 10:22:01 +0000196 def test_haskey(self):
197 self.assertIs({}.has_key(1), False)
198 self.assertIs({1:1}.has_key(1), True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000199
Walter Dörwalddbcede52003-04-25 10:22:01 +0000200 def test_string(self):
201 self.assertIs("xyz".endswith("z"), True)
202 self.assertIs("xyz".endswith("x"), False)
203 self.assertIs("xyz0123".isalnum(), True)
204 self.assertIs("@#$%".isalnum(), False)
205 self.assertIs("xyz".isalpha(), True)
206 self.assertIs("@#$%".isalpha(), False)
207 self.assertIs("0123".isdigit(), True)
208 self.assertIs("xyz".isdigit(), False)
209 self.assertIs("xyz".islower(), True)
210 self.assertIs("XYZ".islower(), False)
211 self.assertIs(" ".isspace(), True)
212 self.assertIs("XYZ".isspace(), False)
213 self.assertIs("X".istitle(), True)
214 self.assertIs("x".istitle(), False)
215 self.assertIs("XYZ".isupper(), True)
216 self.assertIs("xyz".isupper(), False)
217 self.assertIs("xyz".startswith("x"), True)
218 self.assertIs("xyz".startswith("z"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000219
Walter Dörwalddbcede52003-04-25 10:22:01 +0000220 if have_unicode:
221 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True)
222 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False)
223 self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True)
224 self.assertIs(unicode("@#$%", 'ascii').isalnum(), False)
225 self.assertIs(unicode("xyz", 'ascii').isalpha(), True)
226 self.assertIs(unicode("@#$%", 'ascii').isalpha(), False)
227 self.assertIs(unicode("0123", 'ascii').isdecimal(), True)
228 self.assertIs(unicode("xyz", 'ascii').isdecimal(), False)
229 self.assertIs(unicode("0123", 'ascii').isdigit(), True)
230 self.assertIs(unicode("xyz", 'ascii').isdigit(), False)
231 self.assertIs(unicode("xyz", 'ascii').islower(), True)
232 self.assertIs(unicode("XYZ", 'ascii').islower(), False)
233 self.assertIs(unicode("0123", 'ascii').isnumeric(), True)
234 self.assertIs(unicode("xyz", 'ascii').isnumeric(), False)
235 self.assertIs(unicode(" ", 'ascii').isspace(), True)
236 self.assertIs(unicode("XYZ", 'ascii').isspace(), False)
237 self.assertIs(unicode("X", 'ascii').istitle(), True)
238 self.assertIs(unicode("x", 'ascii').istitle(), False)
239 self.assertIs(unicode("XYZ", 'ascii').isupper(), True)
240 self.assertIs(unicode("xyz", 'ascii').isupper(), False)
241 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True)
242 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000243
Walter Dörwalddbcede52003-04-25 10:22:01 +0000244 def test_boolean(self):
245 self.assertEqual(True & 1, 1)
246 self.assert_(not isinstance(True & 1, bool))
247 self.assertIs(True & True, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000248
Walter Dörwalddbcede52003-04-25 10:22:01 +0000249 self.assertEqual(True | 1, 1)
250 self.assert_(not isinstance(True | 1, bool))
251 self.assertIs(True | True, True)
Guido van Rossume2763392002-04-05 19:30:08 +0000252
Walter Dörwalddbcede52003-04-25 10:22:01 +0000253 self.assertEqual(True ^ 1, 0)
254 self.assert_(not isinstance(True ^ 1, bool))
255 self.assertIs(True ^ True, False)
256
257 def test_fileclosed(self):
258 try:
259 f = file(TESTFN, "w")
260 self.assertIs(f.closed, False)
261 f.close()
262 self.assertIs(f.closed, True)
263 finally:
264 os.remove(TESTFN)
265
266 def test_operator(self):
267 import operator
268 self.assertIs(operator.truth(0), False)
269 self.assertIs(operator.truth(1), True)
270 self.assertIs(operator.isCallable(0), False)
271 self.assertIs(operator.isCallable(len), True)
272 self.assertIs(operator.isNumberType(None), False)
273 self.assertIs(operator.isNumberType(0), True)
274 self.assertIs(operator.not_(1), False)
275 self.assertIs(operator.not_(0), True)
276 self.assertIs(operator.isSequenceType(0), False)
277 self.assertIs(operator.isSequenceType([]), True)
278 self.assertIs(operator.contains([], 1), False)
279 self.assertIs(operator.contains([1], 1), True)
280 self.assertIs(operator.isMappingType(1), False)
281 self.assertIs(operator.isMappingType({}), True)
282 self.assertIs(operator.lt(0, 0), False)
283 self.assertIs(operator.lt(0, 1), True)
284 self.assertIs(operator.is_(True, True), True)
285 self.assertIs(operator.is_(True, False), False)
286 self.assertIs(operator.is_not(True, True), False)
287 self.assertIs(operator.is_not(True, False), True)
288
289 def test_marshal(self):
290 import marshal
291 veris(marshal.loads(marshal.dumps(True)), True)
292 veris(marshal.loads(marshal.dumps(False)), False)
293
294 def test_pickle(self):
295 import pickle
296 self.assertIs(pickle.loads(pickle.dumps(True)), True)
297 self.assertIs(pickle.loads(pickle.dumps(False)), False)
298 self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
299 self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
300
301 def test_cpickle(self):
302 import cPickle
303 self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
304 self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
305 self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
306 self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
307
308 def test_mixedpickle(self):
309 import pickle, cPickle
310 self.assertIs(pickle.loads(cPickle.dumps(True)), True)
311 self.assertIs(pickle.loads(cPickle.dumps(False)), False)
312 self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
313 self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
314
315 self.assertIs(cPickle.loads(pickle.dumps(True)), True)
316 self.assertIs(cPickle.loads(pickle.dumps(False)), False)
317 self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
318 self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
319
320 def test_picklevalues(self):
321 import pickle, cPickle
322
323 # Test for specific backwards-compatible pickle values
324 self.assertEqual(pickle.dumps(True), "I01\n.")
325 self.assertEqual(pickle.dumps(False), "I00\n.")
326 self.assertEqual(cPickle.dumps(True), "I01\n.")
327 self.assertEqual(cPickle.dumps(False), "I00\n.")
328 self.assertEqual(pickle.dumps(True, True), "I01\n.")
329 self.assertEqual(pickle.dumps(False, True), "I00\n.")
330 self.assertEqual(cPickle.dumps(True, True), "I01\n.")
331 self.assertEqual(cPickle.dumps(False, True), "I00\n.")
332
333def test_main():
334 suite = unittest.TestSuite()
335 suite.addTest(unittest.makeSuite(BoolTest))
336 test_support.run_suite(suite)
337
338if __name__ == "__main__":
339 test_main()
340