0001 # Python test set -- part 6, built-in types 0002 0003 from test.test_support import * 0004 0005 print '6. Built-in types' 0006 0007 print '6.1 Truth value testing' 0008 if None: raise TestFailed, 'None is true instead of false' 0009 if 0: raise TestFailed, '0 is true instead of false' 0010 if 0L: raise TestFailed, '0L is true instead of false' 0011 if 0.0: raise TestFailed, '0.0 is true instead of false' 0012 if '': raise TestFailed, '\'\' is true instead of false' 0013 if not 1: raise TestFailed, '1 is false instead of true' 0014 if not 1L: raise TestFailed, '1L is false instead of true' 0015 if not 1.0: raise TestFailed, '1.0 is false instead of true' 0016 if not 'x': raise TestFailed, '\'x\' is false instead of true' 0017 if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' 0018 def f(): pass 0019 class C: pass 0020 import sys 0021 x = C() 0022 if not f: raise TestFailed, 'f is false instead of true' 0023 if not C: raise TestFailed, 'C is false instead of true' 0024 if not sys: raise TestFailed, 'sys is false instead of true' 0025 if not x: raise TestFailed, 'x is false instead of true' 0026 0027 print '6.2 Boolean operations' 0028 if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' 0029 if 1 and 1: pass 0030 else: raise TestFailed, '1 and 1 is false instead of true' 0031 if not 1: raise TestFailed, 'not 1 is true instead of false' 0032 0033 print '6.3 Comparisons' 0034 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass 0035 else: raise TestFailed, 'int comparisons failed' 0036 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass 0037 else: raise TestFailed, 'long int comparisons failed' 0038 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass 0039 else: raise TestFailed, 'float comparisons failed' 0040 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass 0041 else: raise TestFailed, 'string comparisons failed' 0042 if None is None: pass 0043 else: raise TestFailed, 'identity test failed' 0044 0045 try: float('') 0046 except ValueError: pass 0047 else: raise TestFailed, "float('') didn't raise ValueError" 0048 0049 try: float('5\0') 0050 except ValueError: pass 0051 else: raise TestFailed, "float('5\0') didn't raise ValueError" 0052 0053 try: 5.0 / 0.0 0054 except ZeroDivisionError: pass 0055 else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" 0056 0057 try: 5.0 // 0.0 0058 except ZeroDivisionError: pass 0059 else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" 0060 0061 try: 5.0 % 0.0 0062 except ZeroDivisionError: pass 0063 else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" 0064 0065 try: 5 / 0L 0066 except ZeroDivisionError: pass 0067 else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" 0068 0069 try: 5 // 0L 0070 except ZeroDivisionError: pass 0071 else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" 0072 0073 try: 5 % 0L 0074 except ZeroDivisionError: pass 0075 else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" 0076 0077 print '6.4 Numeric types (mostly conversions)' 0078 if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons' 0079 if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' 0080 if -1 != -1L or -1 != -1.0 or -1L != -1.0: 0081 raise TestFailed, 'int/long/float value not equal' 0082 # calling built-in types without argument must return 0 0083 if int() != 0: raise TestFailed, 'int() does not return 0' 0084 if long() != 0L: raise TestFailed, 'long() does not return 0L' 0085 if float() != 0.0: raise TestFailed, 'float() does not return 0.0' 0086 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass 0087 else: raise TestFailed, 'int() does not round properly' 0088 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass 0089 else: raise TestFailed, 'long() does not round properly' 0090 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass 0091 else: raise TestFailed, 'float() does not work properly' 0092 print '6.4.1 32-bit integers' 0093 if 12 + 24 != 36: raise TestFailed, 'int op' 0094 if 12 + (-24) != -12: raise TestFailed, 'int op' 0095 if (-12) + 24 != 12: raise TestFailed, 'int op' 0096 if (-12) + (-24) != -36: raise TestFailed, 'int op' 0097 if not 12 < 24: raise TestFailed, 'int op' 0098 if not -24 < -12: raise TestFailed, 'int op' 0099 # Test for a particular bug in integer multiply 0100 xsize, ysize, zsize = 238, 356, 4 0101 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): 0102 raise TestFailed, 'int mul commutativity' 0103 # And another. 0104 m = -sys.maxint - 1 0105 for divisor in 1, 2, 4, 8, 16, 32: 0106 j = m // divisor 0107 prod = divisor * j 0108 if prod != m: 0109 raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m) 0110 if type(prod) is not int: 0111 raise TestFailed, ("expected type(prod) to be int, not %r" % 0112 type(prod)) 0113 # Check for expected * overflow to long. 0114 for divisor in 1, 2, 4, 8, 16, 32: 0115 j = m // divisor - 1 0116 prod = divisor * j 0117 if type(prod) is not long: 0118 raise TestFailed, ("expected type(%r) to be long, not %r" % 0119 (prod, type(prod))) 0120 # Check for expected * overflow to long. 0121 m = sys.maxint 0122 for divisor in 1, 2, 4, 8, 16, 32: 0123 j = m // divisor + 1 0124 prod = divisor * j 0125 if type(prod) is not long: 0126 raise TestFailed, ("expected type(%r) to be long, not %r" % 0127 (prod, type(prod))) 0128 0129 print '6.4.2 Long integers' 0130 if 12L + 24L != 36L: raise TestFailed, 'long op' 0131 if 12L + (-24L) != -12L: raise TestFailed, 'long op' 0132 if (-12L) + 24L != 12L: raise TestFailed, 'long op' 0133 if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' 0134 if not 12L < 24L: raise TestFailed, 'long op' 0135 if not -24L < -12L: raise TestFailed, 'long op' 0136 x = sys.maxint 0137 if int(long(x)) != x: raise TestFailed, 'long op' 0138 try: y = int(long(x)+1L) 0139 except OverflowError: raise TestFailed, 'long op' 0140 if not isinstance(y, long): raise TestFailed, 'long op' 0141 x = -x 0142 if int(long(x)) != x: raise TestFailed, 'long op' 0143 x = x-1 0144 if int(long(x)) != x: raise TestFailed, 'long op' 0145 try: y = int(long(x)-1L) 0146 except OverflowError: raise TestFailed, 'long op' 0147 if not isinstance(y, long): raise TestFailed, 'long op' 0148 0149 try: 5 << -5 0150 except ValueError: pass 0151 else: raise TestFailed, 'int negative shift <<' 0152 0153 try: 5L << -5L 0154 except ValueError: pass 0155 else: raise TestFailed, 'long negative shift <<' 0156 0157 try: 5 >> -5 0158 except ValueError: pass 0159 else: raise TestFailed, 'int negative shift >>' 0160 0161 try: 5L >> -5L 0162 except ValueError: pass 0163 else: raise TestFailed, 'long negative shift >>' 0164 0165 print '6.4.3 Floating point numbers' 0166 if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' 0167 if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op' 0168 if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' 0169 if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' 0170 if not 12.0 < 24.0: raise TestFailed, 'float op' 0171 if not -24.0 < -12.0: raise TestFailed, 'float op' 0172 0173 print '6.5 Sequence types' 0174 0175 print '6.5.1 Strings' 0176 if len('') != 0: raise TestFailed, 'len(\'\')' 0177 if len('a') != 1: raise TestFailed, 'len(\'a\')' 0178 if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' 0179 if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation' 0180 if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' 0181 if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' 0182 if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string' 0183 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass 0184 else: raise TestFailed, 'in/not in string' 0185 x = 'x'*103 0186 if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' 0187 0188 #extended slices for strings 0189 a = '0123456789' 0190 vereq(a[::], a) 0191 vereq(a[::2], '02468') 0192 vereq(a[1::2], '13579') 0193 vereq(a[::-1],'9876543210') 0194 vereq(a[::-2], '97531') 0195 vereq(a[3::-2], '31') 0196 vereq(a[-100:100:], a) 0197 vereq(a[100:-100:-1], a[::-1]) 0198 vereq(a[-100L:100L:2L], '02468') 0199 0200 if have_unicode: 0201 a = unicode('0123456789', 'ascii') 0202 vereq(a[::], a) 0203 vereq(a[::2], unicode('02468', 'ascii')) 0204 vereq(a[1::2], unicode('13579', 'ascii')) 0205 vereq(a[::-1], unicode('9876543210', 'ascii')) 0206 vereq(a[::-2], unicode('97531', 'ascii')) 0207 vereq(a[3::-2], unicode('31', 'ascii')) 0208 vereq(a[-100:100:], a) 0209 vereq(a[100:-100:-1], a[::-1]) 0210 vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) 0211 0212 0213 print '6.5.2 Tuples [see test_tuple.py]' 0214 0215 print '6.5.3 Lists [see test_list.py]' 0216 0217 print '6.6 Mappings == Dictionaries [see test_dict.py]' 0218 0219 0220 try: type(1, 2) 0221 except TypeError: pass 0222 else: raise TestFailed, 'type(), w/2 args expected TypeError' 0223 0224 try: type(1, 2, 3, 4) 0225 except TypeError: pass 0226 else: raise TestFailed, 'type(), w/4 args expected TypeError' 0227 0228 print 'Buffers' 0229 try: buffer('asdf', -1) 0230 except ValueError: pass 0231 else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" 0232 0233 try: buffer(None) 0234 except TypeError: pass 0235 else: raise TestFailed, "buffer(None) should raise TypeError" 0236 0237 a = buffer('asdf') 0238 hash(a) 0239 b = a * 5 0240 if a == b: 0241 raise TestFailed, 'buffers should not be equal' 0242 if str(b) != ('asdf' * 5): 0243 raise TestFailed, 'repeated buffer has wrong content' 0244 if str(a * 0) != '': 0245 raise TestFailed, 'repeated buffer zero times has wrong content' 0246 if str(a + buffer('def')) != 'asdfdef': 0247 raise TestFailed, 'concatenation of buffers yields wrong content' 0248 if str(buffer(a)) != 'asdf': 0249 raise TestFailed, 'composing buffers failed' 0250 if str(buffer(a, 2)) != 'df': 0251 raise TestFailed, 'specifying buffer offset failed' 0252 if str(buffer(a, 0, 2)) != 'as': 0253 raise TestFailed, 'specifying buffer size failed' 0254 if str(buffer(a, 1, 2)) != 'sd': 0255 raise TestFailed, 'specifying buffer offset and size failed' 0256 try: buffer(buffer('asdf', 1), -1) 0257 except ValueError: pass 0258 else: raise TestFailed, "buffer(buffer('asdf', 1), -1) should raise ValueError" 0259 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as': 0260 raise TestFailed, 'composing length-specified buffer failed' 0261 if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as': 0262 raise TestFailed, 'composing length-specified buffer failed' 0263 if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as': 0264 raise TestFailed, 'composing length-specified buffer failed' 0265 if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's': 0266 raise TestFailed, 'composing length-specified buffer failed' 0267 0268 try: a[1] = 'g' 0269 except TypeError: pass 0270 else: raise TestFailed, "buffer assignment should raise TypeError" 0271 0272 try: a[0:1] = 'g' 0273 except TypeError: pass 0274 else: raise TestFailed, "buffer slice assignment should raise TypeError" 0275
Generated by PyXR 0.9.4