blob: 9441181cfcf1cf259718a010614e7e68f6ae7e0b [file] [log] [blame]
Guido van Rossum85f18201992-11-27 22:53:50 +00001# Python test set -- part 6, built-in types
2
Thomas Wouters89f507f2006-12-13 04:49:30 +00003from test.test_support import run_unittest, have_unicode
4import unittest
Guido van Rossum85f18201992-11-27 22:53:50 +00005import sys
Guido van Rossum85f18201992-11-27 22:53:50 +00006
Thomas Wouters89f507f2006-12-13 04:49:30 +00007class TypesTests(unittest.TestCase):
Guido van Rossum85f18201992-11-27 22:53:50 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 def test_truth_values(self):
10 if None: self.fail('None is true instead of false')
11 if 0: self.fail('0 is true instead of false')
Guido van Rossume2a383d2007-01-15 16:59:06 +000012 if 0: self.fail('0L is true instead of false')
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 if 0.0: self.fail('0.0 is true instead of false')
14 if '': self.fail('\'\' is true instead of false')
15 if not 1: self.fail('1 is false instead of true')
Guido van Rossume2a383d2007-01-15 16:59:06 +000016 if not 1: self.fail('1L is false instead of true')
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 if not 1.0: self.fail('1.0 is false instead of true')
18 if not 'x': self.fail('\'x\' is false instead of true')
19 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
20 def f(): pass
21 class C: pass
22 import sys
23 x = C()
24 if not f: self.fail('f is false instead of true')
25 if not C: self.fail('C is false instead of true')
26 if not sys: self.fail('sys is false instead of true')
27 if not x: self.fail('x is false instead of true')
Guido van Rossum85f18201992-11-27 22:53:50 +000028
Thomas Wouters89f507f2006-12-13 04:49:30 +000029 def test_boolean_ops(self):
30 if 0 or 0: self.fail('0 or 0 is true instead of false')
31 if 1 and 1: pass
32 else: self.fail('1 and 1 is false instead of true')
33 if not 1: self.fail('not 1 is true instead of false')
Neil Schemenauereff72442002-03-24 01:24:54 +000034
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 def test_comparisons(self):
36 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
37 else: self.fail('int comparisons failed')
Guido van Rossume2a383d2007-01-15 16:59:06 +000038 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 else: self.fail('long int comparisons failed')
40 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
41 else: self.fail('float comparisons failed')
42 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
43 else: self.fail('string comparisons failed')
44 if None is None: pass
45 else: self.fail('identity test failed')
Neil Schemenauereff72442002-03-24 01:24:54 +000046
Thomas Wouters89f507f2006-12-13 04:49:30 +000047 def test_float_constructor(self):
48 self.assertRaises(ValueError, float, '')
49 self.assertRaises(ValueError, float, '5\0')
Neil Schemenauereff72442002-03-24 01:24:54 +000050
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 def test_zero_division(self):
52 try: 5.0 / 0.0
53 except ZeroDivisionError: pass
54 else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000055
Thomas Wouters89f507f2006-12-13 04:49:30 +000056 try: 5.0 // 0.0
57 except ZeroDivisionError: pass
58 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000059
Thomas Wouters89f507f2006-12-13 04:49:30 +000060 try: 5.0 % 0.0
61 except ZeroDivisionError: pass
62 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000063
Guido van Rossume2a383d2007-01-15 16:59:06 +000064 try: 5 / 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000065 except ZeroDivisionError: pass
66 else: self.fail("5 / 0L didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000067
Guido van Rossume2a383d2007-01-15 16:59:06 +000068 try: 5 // 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 except ZeroDivisionError: pass
70 else: self.fail("5 // 0L didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000071
Guido van Rossume2a383d2007-01-15 16:59:06 +000072 try: 5 % 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 except ZeroDivisionError: pass
74 else: self.fail("5 % 0L didn't raise ZeroDivisionError")
Tim Petersa3c01ce2001-12-04 23:05:10 +000075
Thomas Wouters89f507f2006-12-13 04:49:30 +000076 def test_numeric_types(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000077 if 0 != 0 or 0 != 0.0 or 0 != 0.0: self.fail('mixed comparisons')
78 if 1 != 1 or 1 != 1.0 or 1 != 1.0: self.fail('mixed comparisons')
79 if -1 != -1 or -1 != -1.0 or -1 != -1.0:
Thomas Wouters89f507f2006-12-13 04:49:30 +000080 self.fail('int/long/float value not equal')
81 # calling built-in types without argument must return 0
82 if int() != 0: self.fail('int() does not return 0')
Guido van Rossume2a383d2007-01-15 16:59:06 +000083 if int() != 0: self.fail('long() does not return 0L')
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 if float() != 0.0: self.fail('float() does not return 0.0')
85 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
86 else: self.fail('int() does not round properly')
Guido van Rossume2a383d2007-01-15 16:59:06 +000087 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 else: self.fail('long() does not round properly')
89 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
90 else: self.fail('float() does not work properly')
Neil Schemenauereff72442002-03-24 01:24:54 +000091
Thomas Wouters89f507f2006-12-13 04:49:30 +000092 def test_normal_integers(self):
93 # Ensure the first 256 integers are shared
94 a = 256
95 b = 128*2
96 if a is not b: self.fail('256 is not shared')
97 if 12 + 24 != 36: self.fail('int op')
98 if 12 + (-24) != -12: self.fail('int op')
99 if (-12) + 24 != 12: self.fail('int op')
100 if (-12) + (-24) != -36: self.fail('int op')
101 if not 12 < 24: self.fail('int op')
102 if not -24 < -12: self.fail('int op')
103 # Test for a particular bug in integer multiply
104 xsize, ysize, zsize = 238, 356, 4
105 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
106 self.fail('int mul commutativity')
107 # And another.
108 m = -sys.maxint - 1
109 for divisor in 1, 2, 4, 8, 16, 32:
110 j = m // divisor
111 prod = divisor * j
112 if prod != m:
113 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
114 if type(prod) is not int:
115 self.fail("expected type(prod) to be int, not %r" %
116 type(prod))
117 # Check for expected * overflow to long.
118 for divisor in 1, 2, 4, 8, 16, 32:
119 j = m // divisor - 1
120 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000121 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122 self.fail("expected type(%r) to be long, not %r" %
123 (prod, type(prod)))
124 # Check for expected * overflow to long.
125 m = sys.maxint
126 for divisor in 1, 2, 4, 8, 16, 32:
127 j = m // divisor + 1
128 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000129 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 self.fail("expected type(%r) to be long, not %r" %
131 (prod, type(prod)))
Neil Schemenauereff72442002-03-24 01:24:54 +0000132
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133 def test_long_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000134 if 12 + 24 != 36: self.fail('long op')
135 if 12 + (-24) != -12: self.fail('long op')
136 if (-12) + 24 != 12: self.fail('long op')
137 if (-12) + (-24) != -36: self.fail('long op')
138 if not 12 < 24: self.fail('long op')
139 if not -24 < -12: self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000140 x = sys.maxint
Guido van Rossume2a383d2007-01-15 16:59:06 +0000141 if int(int(x)) != x: self.fail('long op')
142 try: y = int(int(x)+1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000144 if not isinstance(y, int): self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000145 x = -x
Guido van Rossume2a383d2007-01-15 16:59:06 +0000146 if int(int(x)) != x: self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147 x = x-1
Guido van Rossume2a383d2007-01-15 16:59:06 +0000148 if int(int(x)) != x: self.fail('long op')
149 try: y = int(int(x)-1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000151 if not isinstance(y, int): self.fail('long op')
Neil Schemenauereff72442002-03-24 01:24:54 +0000152
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 try: 5 << -5
154 except ValueError: pass
155 else: self.fail('int negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000156
Guido van Rossume2a383d2007-01-15 16:59:06 +0000157 try: 5 << -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000158 except ValueError: pass
159 else: self.fail('long negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000160
Thomas Wouters89f507f2006-12-13 04:49:30 +0000161 try: 5 >> -5
162 except ValueError: pass
163 else: self.fail('int negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000164
Guido van Rossume2a383d2007-01-15 16:59:06 +0000165 try: 5 >> -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000166 except ValueError: pass
167 else: self.fail('long negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000168
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169 def test_floats(self):
170 if 12.0 + 24.0 != 36.0: self.fail('float op')
171 if 12.0 + (-24.0) != -12.0: self.fail('float op')
172 if (-12.0) + 24.0 != 12.0: self.fail('float op')
173 if (-12.0) + (-24.0) != -36.0: self.fail('float op')
174 if not 12.0 < 24.0: self.fail('float op')
175 if not -24.0 < -12.0: self.fail('float op')
Guido van Rossum85f18201992-11-27 22:53:50 +0000176
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177 def test_strings(self):
178 if len('') != 0: self.fail('len(\'\')')
179 if len('a') != 1: self.fail('len(\'a\')')
180 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
181 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
182 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
183 if 0*'abcde' != '': self.fail('string repetition 0*')
184 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
185 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
186 else: self.fail('in/not in string')
187 x = 'x'*103
188 if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000189
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 #extended slices for strings
191 a = '0123456789'
192 self.assertEqual(a[::], a)
193 self.assertEqual(a[::2], '02468')
194 self.assertEqual(a[1::2], '13579')
195 self.assertEqual(a[::-1],'9876543210')
196 self.assertEqual(a[::-2], '97531')
197 self.assertEqual(a[3::-2], '31')
198 self.assertEqual(a[-100:100:], a)
199 self.assertEqual(a[100:-100:-1], a[::-1])
Guido van Rossume2a383d2007-01-15 16:59:06 +0000200 self.assertEqual(a[-100:100:2], '02468')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000201
202 if have_unicode:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000203 a = str('0123456789', 'ascii')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 self.assertEqual(a[::], a)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000205 self.assertEqual(a[::2], str('02468', 'ascii'))
206 self.assertEqual(a[1::2], str('13579', 'ascii'))
207 self.assertEqual(a[::-1], str('9876543210', 'ascii'))
208 self.assertEqual(a[::-2], str('97531', 'ascii'))
209 self.assertEqual(a[3::-2], str('31', 'ascii'))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210 self.assertEqual(a[-100:100:], a)
211 self.assertEqual(a[100:-100:-1], a[::-1])
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000212 self.assertEqual(a[-100:100:2], str('02468', 'ascii'))
Tim Petersc411dba2002-07-16 21:35:23 +0000213
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000214
Thomas Wouters89f507f2006-12-13 04:49:30 +0000215 def test_type_function(self):
216 self.assertRaises(TypeError, type, 1, 2)
217 self.assertRaises(TypeError, type, 1, 2, 3, 4)
Guido van Rossum85f18201992-11-27 22:53:50 +0000218
Thomas Wouters89f507f2006-12-13 04:49:30 +0000219 def test_buffers(self):
220 self.assertRaises(ValueError, buffer, 'asdf', -1)
221 self.assertRaises(TypeError, buffer, None)
Raymond Hettinger99285712003-04-24 16:52:47 +0000222
Thomas Wouters89f507f2006-12-13 04:49:30 +0000223 a = buffer('asdf')
224 hash(a)
225 b = a * 5
226 if a == b:
227 self.fail('buffers should not be equal')
228 if str(b) != ('asdf' * 5):
229 self.fail('repeated buffer has wrong content')
230 if str(a * 0) != '':
231 self.fail('repeated buffer zero times has wrong content')
232 if str(a + buffer('def')) != 'asdfdef':
233 self.fail('concatenation of buffers yields wrong content')
234 if str(buffer(a)) != 'asdf':
235 self.fail('composing buffers failed')
236 if str(buffer(a, 2)) != 'df':
237 self.fail('specifying buffer offset failed')
238 if str(buffer(a, 0, 2)) != 'as':
239 self.fail('specifying buffer size failed')
240 if str(buffer(a, 1, 2)) != 'sd':
241 self.fail('specifying buffer offset and size failed')
242 self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
243 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
244 self.fail('composing length-specified buffer failed')
245 if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as':
246 self.fail('composing length-specified buffer failed')
247 if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
248 self.fail('composing length-specified buffer failed')
249 if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's':
250 self.fail('composing length-specified buffer failed')
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000251
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252 try: a[1] = 'g'
253 except TypeError: pass
254 else: self.fail("buffer assignment should raise TypeError")
Neil Schemenauereff72442002-03-24 01:24:54 +0000255
Thomas Wouters89f507f2006-12-13 04:49:30 +0000256 try: a[0:1] = 'g'
257 except TypeError: pass
258 else: self.fail("buffer slice assignment should raise TypeError")
Guido van Rossum29d26062001-12-11 04:37:34 +0000259
Thomas Wouters89f507f2006-12-13 04:49:30 +0000260 # array.array() returns an object that does not implement a char buffer,
261 # something which int() uses for conversion.
262 import array
263 try: int(buffer(array.array('c')))
264 except TypeError: pass
265 else: self.fail("char buffer (at C level) not working")
Neil Schemenauereff72442002-03-24 01:24:54 +0000266
Thomas Wouters89f507f2006-12-13 04:49:30 +0000267def test_main():
268 run_unittest(TypesTests)
Neil Schemenauereff72442002-03-24 01:24:54 +0000269
Thomas Wouters89f507f2006-12-13 04:49:30 +0000270if __name__ == '__main__':
271 test_main()