blob: 33185a05d46cdb70989916e00eca748304db83c3 [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 +00008class BoolTest(unittest.TestCase):
Guido van Rossum5f820362002-04-03 23:01:45 +00009
Walter Dörwalddbcede52003-04-25 10:22:01 +000010 def assertIs(self, a, b):
11 self.assert_(a is b)
Guido van Rossum5f820362002-04-03 23:01:45 +000012
Walter Dörwalddbcede52003-04-25 10:22:01 +000013 def assertIsNot(self, a, b):
14 self.assert_(a is not b)
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +000015
Walter Dörwalddbcede52003-04-25 10:22:01 +000016 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")
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +000024
Walter Dörwalddbcede52003-04-25 10:22:01 +000025 self.assertRaises(TypeError, int.__new__, bool, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +000026
Walter Dörwalddbcede52003-04-25 10:22:01 +000027 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000037
Walter Dörwalddbcede52003-04-25 10:22:01 +000038 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000043
Walter Dörwalddbcede52003-04-25 10:22:01 +000044 def test_str(self):
45 self.assertEqual(str(False), 'False')
46 self.assertEqual(str(True), 'True')
Guido van Rossum5f820362002-04-03 23:01:45 +000047
Walter Dörwalddbcede52003-04-25 10:22:01 +000048 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000053
Walter Dörwalddbcede52003-04-25 10:22:01 +000054 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000068
Walter Dörwalddbcede52003-04-25 10:22:01 +000069 self.assertEqual(False+2, 2)
70 self.assertEqual(True+2, 3)
71 self.assertEqual(2+False, 2)
72 self.assertEqual(2+True, 3)
Guido van Rossum5f820362002-04-03 23:01:45 +000073
Walter Dörwalddbcede52003-04-25 10:22:01 +000074 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000081
Walter Dörwalddbcede52003-04-25 10:22:01 +000082 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)
Guido van Rossum5f820362002-04-03 23:01:45 +000089
Walter Dörwalddbcede52003-04-25 10:22:01 +000090 self.assertEqual(True*1, 1)
91 self.assertEqual(False*1, 0)
92 self.assertIsNot(False*1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +000093
Walter Dörwalddbcede52003-04-25 10:22:01 +000094 self.assertEqual(True/1, 1)
95 self.assertIsNot(True/1, True)
96 self.assertEqual(False/1, 0)
97 self.assertIsNot(False/1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +000098
Walter Dörwalddbcede52003-04-25 10:22:01 +000099 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))
Guido van Rossum5f820362002-04-03 23:01:45 +0000103
Walter Dörwalddbcede52003-04-25 10:22:01 +0000104 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)))
Guido van Rossum5f820362002-04-03 23:01:45 +0000121
Walter Dörwalddbcede52003-04-25 10:22:01 +0000122 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000134
Walter Dörwalddbcede52003-04-25 10:22:01 +0000135 x = [1]
136 self.assertIs(x is x, True)
137 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000138
Walter Dörwalddbcede52003-04-25 10:22:01 +0000139 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000143
Walter Dörwalddbcede52003-04-25 10:22:01 +0000144 x = {1: 2}
145 self.assertIs(x is x, True)
146 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000147
Walter Dörwalddbcede52003-04-25 10:22:01 +0000148 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000152
Walter Dörwalddbcede52003-04-25 10:22:01 +0000153 self.assertIs(not True, False)
154 self.assertIs(not False, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000155
Walter Dörwalddbcede52003-04-25 10:22:01 +0000156 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000165
Walter Dörwalddbcede52003-04-25 10:22:01 +0000166 def test_hasattr(self):
167 self.assertIs(hasattr([], "append"), True)
168 self.assertIs(hasattr([], "wobble"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000169
Walter Dörwalddbcede52003-04-25 10:22:01 +0000170 def test_callable(self):
171 self.assertIs(callable(len), True)
172 self.assertIs(callable(1), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000173
Walter Dörwalddbcede52003-04-25 10:22:01 +0000174 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000181
Walter Dörwalddbcede52003-04-25 10:22:01 +0000182 def test_issubclass(self):
183 self.assertIs(issubclass(bool, int), True)
184 self.assertIs(issubclass(int, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000185
Walter Dörwalddbcede52003-04-25 10:22:01 +0000186 def test_haskey(self):
187 self.assertIs({}.has_key(1), False)
188 self.assertIs({1:1}.has_key(1), True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000189
Walter Dörwalddbcede52003-04-25 10:22:01 +0000190 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000209
Walter Dörwald90437c02003-05-01 13:12:34 +0000210 if test_support.have_unicode:
Walter Dörwalddbcede52003-04-25 10:22:01 +0000211 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)
Guido van Rossum5f820362002-04-03 23:01:45 +0000233
Walter Dörwalddbcede52003-04-25 10:22:01 +0000234 def test_boolean(self):
235 self.assertEqual(True & 1, 1)
236 self.assert_(not isinstance(True & 1, bool))
237 self.assertIs(True & True, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000238
Walter Dörwalddbcede52003-04-25 10:22:01 +0000239 self.assertEqual(True | 1, 1)
240 self.assert_(not isinstance(True | 1, bool))
241 self.assertIs(True | True, True)
Guido van Rossume2763392002-04-05 19:30:08 +0000242
Walter Dörwalddbcede52003-04-25 10:22:01 +0000243 self.assertEqual(True ^ 1, 0)
244 self.assert_(not isinstance(True ^ 1, bool))
245 self.assertIs(True ^ True, False)
246
247 def test_fileclosed(self):
248 try:
Walter Dörwald90437c02003-05-01 13:12:34 +0000249 f = file(test_support.TESTFN, "w")
Walter Dörwalddbcede52003-04-25 10:22:01 +0000250 self.assertIs(f.closed, False)
251 f.close()
252 self.assertIs(f.closed, True)
253 finally:
Walter Dörwald90437c02003-05-01 13:12:34 +0000254 os.remove(test_support.TESTFN)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000255
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)
278
279 def test_marshal(self):
280 import marshal
Walter Dörwald90437c02003-05-01 13:12:34 +0000281 self.assertIs(marshal.loads(marshal.dumps(True)), True)
282 self.assertIs(marshal.loads(marshal.dumps(False)), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000283
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)
290
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)
297
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)
304
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)
309
310 def test_picklevalues(self):
311 import pickle, cPickle
312
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.")
322
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000323 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())
Tim Peters478c1052003-06-29 05:46:54 +0000332
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000333 class Bar(object):
334 def __nonzero__(self):
335 return "Yes"
336 check(Bar())
Tim Peters478c1052003-06-29 05:46:54 +0000337
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000338 class Baz(int):
339 def __nonzero__(self):
340 return self
341 check(Baz())
Tim Peters478c1052003-06-29 05:46:54 +0000342
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000343
Walter Dörwalddbcede52003-04-25 10:22:01 +0000344def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000345 test_support.run_unittest(BoolTest)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000346
347if __name__ == "__main__":
348 test_main()