PageRenderTime 537ms CodeModel.GetById 151ms app.highlight 210ms RepoModel.GetById 133ms app.codeStats 0ms

/Lib/test/test_bool.py

http://unladen-swallow.googlecode.com/
Python | 348 lines | 298 code | 45 blank | 5 comment | 25 complexity | f7b90f30bfe10b85a0ad4f20fa99066b MD5 | raw file
  1# Test properties of bool promised by PEP 285
  2
  3import unittest
  4from test import test_support
  5
  6import os
  7
  8class BoolTest(unittest.TestCase):
  9
 10    def assertIs(self, a, b):
 11        self.assert_(a is b)
 12
 13    def assertIsNot(self, a, b):
 14        self.assert_(a is not b)
 15
 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")
 24
 25        self.assertRaises(TypeError, int.__new__, bool, 0)
 26
 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)
 37
 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)
 43
 44    def test_str(self):
 45        self.assertEqual(str(False), 'False')
 46        self.assertEqual(str(True), 'True')
 47
 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)
 53
 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)
 68
 69        self.assertEqual(False+2, 2)
 70        self.assertEqual(True+2, 3)
 71        self.assertEqual(2+False, 2)
 72        self.assertEqual(2+True, 3)
 73
 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)
 81
 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)
 89
 90        self.assertEqual(True*1, 1)
 91        self.assertEqual(False*1, 0)
 92        self.assertIsNot(False*1, False)
 93
 94        self.assertEqual(True/1, 1)
 95        self.assertIsNot(True/1, True)
 96        self.assertEqual(False/1, 0)
 97        self.assertIsNot(False/1, False)
 98
 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))
103
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)))
121
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)
134
135        x = [1]
136        self.assertIs(x is x, True)
137        self.assertIs(x is not x, False)
138
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)
143
144        x = {1: 2}
145        self.assertIs(x is x, True)
146        self.assertIs(x is not x, False)
147
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)
152
153        self.assertIs(not True, False)
154        self.assertIs(not False, True)
155
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)
165
166    def test_hasattr(self):
167        self.assertIs(hasattr([], "append"), True)
168        self.assertIs(hasattr([], "wobble"), False)
169
170    def test_callable(self):
171        self.assertIs(callable(len), True)
172        self.assertIs(callable(1), False)
173
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)
181
182    def test_issubclass(self):
183        self.assertIs(issubclass(bool, int), True)
184        self.assertIs(issubclass(int, bool), False)
185
186    def test_haskey(self):
187        self.assertIs({}.has_key(1), False)
188        self.assertIs({1:1}.has_key(1), True)
189
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)
209
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)
233
234    def test_boolean(self):
235        self.assertEqual(True & 1, 1)
236        self.assert_(not isinstance(True & 1, bool))
237        self.assertIs(True & True, True)
238
239        self.assertEqual(True | 1, 1)
240        self.assert_(not isinstance(True | 1, bool))
241        self.assertIs(True | True, True)
242
243        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:
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)
255
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
281        self.assertIs(marshal.loads(marshal.dumps(True)), True)
282        self.assertIs(marshal.loads(marshal.dumps(False)), False)
283
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
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())
332
333        class Bar(object):
334            def __nonzero__(self):
335                return "Yes"
336        check(Bar())
337
338        class Baz(int):
339            def __nonzero__(self):
340                return self
341        check(Baz())
342
343
344def test_main():
345    test_support.run_unittest(BoolTest)
346
347if __name__ == "__main__":
348    test_main()