0001 import unittest 0002 from test import test_support 0003 0004 import sys, UserDict, cStringIO 0005 0006 0007 class DictTest(unittest.TestCase): 0008 def test_constructor(self): 0009 # calling built-in types without argument must return empty 0010 self.assertEqual(dict(), {}) 0011 self.assert_(dict() is not {}) 0012 0013 def test_bool(self): 0014 self.assert_(not {}) 0015 self.assert_({1: 2}) 0016 self.assert_(bool({}) is False) 0017 self.assert_(bool({1: 2}) is True) 0018 0019 def test_keys(self): 0020 d = {} 0021 self.assertEqual(d.keys(), []) 0022 d = {'a': 1, 'b': 2} 0023 k = d.keys() 0024 self.assert_(d.has_key('a')) 0025 self.assert_(d.has_key('b')) 0026 0027 self.assertRaises(TypeError, d.keys, None) 0028 0029 def test_values(self): 0030 d = {} 0031 self.assertEqual(d.values(), []) 0032 d = {1:2} 0033 self.assertEqual(d.values(), [2]) 0034 0035 self.assertRaises(TypeError, d.values, None) 0036 0037 def test_items(self): 0038 d = {} 0039 self.assertEqual(d.items(), []) 0040 0041 d = {1:2} 0042 self.assertEqual(d.items(), [(1, 2)]) 0043 0044 self.assertRaises(TypeError, d.items, None) 0045 0046 def test_has_key(self): 0047 d = {} 0048 self.assert_(not d.has_key('a')) 0049 d = {'a': 1, 'b': 2} 0050 k = d.keys() 0051 k.sort() 0052 self.assertEqual(k, ['a', 'b']) 0053 0054 self.assertRaises(TypeError, d.has_key) 0055 0056 def test_contains(self): 0057 d = {} 0058 self.assert_(not ('a' in d)) 0059 self.assert_('a' not in d) 0060 d = {'a': 1, 'b': 2} 0061 self.assert_('a' in d) 0062 self.assert_('b' in d) 0063 self.assert_('c' not in d) 0064 0065 self.assertRaises(TypeError, d.__contains__) 0066 0067 def test_len(self): 0068 d = {} 0069 self.assertEqual(len(d), 0) 0070 d = {'a': 1, 'b': 2} 0071 self.assertEqual(len(d), 2) 0072 0073 def test_getitem(self): 0074 d = {'a': 1, 'b': 2} 0075 self.assertEqual(d['a'], 1) 0076 self.assertEqual(d['b'], 2) 0077 d['c'] = 3 0078 d['a'] = 4 0079 self.assertEqual(d['c'], 3) 0080 self.assertEqual(d['a'], 4) 0081 del d['b'] 0082 self.assertEqual(d, {'a': 4, 'c': 3}) 0083 0084 self.assertRaises(TypeError, d.__getitem__) 0085 0086 class BadEq(object): 0087 def __eq__(self, other): 0088 raise Exc() 0089 0090 d = {} 0091 d[BadEq()] = 42 0092 self.assertRaises(KeyError, d.__getitem__, 23) 0093 0094 class Exc(Exception): pass 0095 0096 class BadHash(object): 0097 fail = False 0098 def __hash__(self): 0099 if self.fail: 0100 raise Exc() 0101 else: 0102 return 42 0103 0104 x = BadHash() 0105 d[x] = 42 0106 x.fail = True 0107 self.assertRaises(Exc, d.__getitem__, x) 0108 0109 def test_clear(self): 0110 d = {1:1, 2:2, 3:3} 0111 d.clear() 0112 self.assertEqual(d, {}) 0113 0114 self.assertRaises(TypeError, d.clear, None) 0115 0116 def test_update(self): 0117 d = {} 0118 d.update({1:100}) 0119 d.update({2:20}) 0120 d.update({1:1, 2:2, 3:3}) 0121 self.assertEqual(d, {1:1, 2:2, 3:3}) 0122 0123 d.update() 0124 self.assertEqual(d, {1:1, 2:2, 3:3}) 0125 0126 self.assertRaises((TypeError, AttributeError), d.update, None) 0127 0128 class SimpleUserDict: 0129 def __init__(self): 0130 self.d = {1:1, 2:2, 3:3} 0131 def keys(self): 0132 return self.d.keys() 0133 def __getitem__(self, i): 0134 return self.d[i] 0135 d.clear() 0136 d.update(SimpleUserDict()) 0137 self.assertEqual(d, {1:1, 2:2, 3:3}) 0138 0139 class Exc(Exception): pass 0140 0141 d.clear() 0142 class FailingUserDict: 0143 def keys(self): 0144 raise Exc 0145 self.assertRaises(Exc, d.update, FailingUserDict()) 0146 0147 class FailingUserDict: 0148 def keys(self): 0149 class BogonIter: 0150 def __init__(self): 0151 self.i = 1 0152 def __iter__(self): 0153 return self 0154 def next(self): 0155 if self.i: 0156 self.i = 0 0157 return 'a' 0158 raise Exc 0159 return BogonIter() 0160 def __getitem__(self, key): 0161 return key 0162 self.assertRaises(Exc, d.update, FailingUserDict()) 0163 0164 class FailingUserDict: 0165 def keys(self): 0166 class BogonIter: 0167 def __init__(self): 0168 self.i = ord('a') 0169 def __iter__(self): 0170 return self 0171 def next(self): 0172 if self.i <= ord('z'): 0173 rtn = chr(self.i) 0174 self.i += 1 0175 return rtn 0176 raise StopIteration 0177 return BogonIter() 0178 def __getitem__(self, key): 0179 raise Exc 0180 self.assertRaises(Exc, d.update, FailingUserDict()) 0181 0182 class badseq(object): 0183 def __iter__(self): 0184 return self 0185 def next(self): 0186 raise Exc() 0187 0188 self.assertRaises(Exc, {}.update, badseq()) 0189 0190 self.assertRaises(ValueError, {}.update, [(1, 2, 3)]) 0191 0192 def test_fromkeys(self): 0193 self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) 0194 d = {} 0195 self.assert_(not(d.fromkeys('abc') is d)) 0196 self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) 0197 self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) 0198 self.assertEqual(d.fromkeys([]), {}) 0199 def g(): 0200 yield 1 0201 self.assertEqual(d.fromkeys(g()), {1:None}) 0202 self.assertRaises(TypeError, {}.fromkeys, 3) 0203 class dictlike(dict): pass 0204 self.assertEqual(dictlike.fromkeys('a'), {'a':None}) 0205 self.assertEqual(dictlike().fromkeys('a'), {'a':None}) 0206 self.assert_(type(dictlike.fromkeys('a')) is dictlike) 0207 self.assert_(type(dictlike().fromkeys('a')) is dictlike) 0208 class mydict(dict): 0209 def __new__(cls): 0210 return UserDict.UserDict() 0211 ud = mydict.fromkeys('ab') 0212 self.assertEqual(ud, {'a':None, 'b':None}) 0213 self.assert_(isinstance(ud, UserDict.UserDict)) 0214 self.assertRaises(TypeError, dict.fromkeys) 0215 0216 class Exc(Exception): pass 0217 0218 class baddict1(dict): 0219 def __init__(self): 0220 raise Exc() 0221 0222 self.assertRaises(Exc, baddict1.fromkeys, [1]) 0223 0224 class BadSeq(object): 0225 def __iter__(self): 0226 return self 0227 def next(self): 0228 raise Exc() 0229 0230 self.assertRaises(Exc, dict.fromkeys, BadSeq()) 0231 0232 class baddict2(dict): 0233 def __setitem__(self, key, value): 0234 raise Exc() 0235 0236 self.assertRaises(Exc, baddict2.fromkeys, [1]) 0237 0238 def test_copy(self): 0239 d = {1:1, 2:2, 3:3} 0240 self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) 0241 self.assertEqual({}.copy(), {}) 0242 self.assertRaises(TypeError, d.copy, None) 0243 0244 def test_get(self): 0245 d = {} 0246 self.assert_(d.get('c') is None) 0247 self.assertEqual(d.get('c', 3), 3) 0248 d = {'a' : 1, 'b' : 2} 0249 self.assert_(d.get('c') is None) 0250 self.assertEqual(d.get('c', 3), 3) 0251 self.assertEqual(d.get('a'), 1) 0252 self.assertEqual(d.get('a', 3), 1) 0253 self.assertRaises(TypeError, d.get) 0254 self.assertRaises(TypeError, d.get, None, None, None) 0255 0256 def test_setdefault(self): 0257 # dict.setdefault() 0258 d = {} 0259 self.assert_(d.setdefault('key0') is None) 0260 d.setdefault('key0', []) 0261 self.assert_(d.setdefault('key0') is None) 0262 d.setdefault('key', []).append(3) 0263 self.assertEqual(d['key'][0], 3) 0264 d.setdefault('key', []).append(4) 0265 self.assertEqual(len(d['key']), 2) 0266 self.assertRaises(TypeError, d.setdefault) 0267 0268 class Exc(Exception): pass 0269 0270 class BadHash(object): 0271 fail = False 0272 def __hash__(self): 0273 if self.fail: 0274 raise Exc() 0275 else: 0276 return 42 0277 0278 x = BadHash() 0279 d[x] = 42 0280 x.fail = True 0281 self.assertRaises(Exc, d.setdefault, x, []) 0282 0283 def test_popitem(self): 0284 # dict.popitem() 0285 for copymode in -1, +1: 0286 # -1: b has same structure as a 0287 # +1: b is a.copy() 0288 for log2size in range(12): 0289 size = 2**log2size 0290 a = {} 0291 b = {} 0292 for i in range(size): 0293 a[repr(i)] = i 0294 if copymode < 0: 0295 b[repr(i)] = i 0296 if copymode > 0: 0297 b = a.copy() 0298 for i in range(size): 0299 ka, va = ta = a.popitem() 0300 self.assertEqual(va, int(ka)) 0301 kb, vb = tb = b.popitem() 0302 self.assertEqual(vb, int(kb)) 0303 self.assert_(not(copymode < 0 and ta != tb)) 0304 self.assert_(not a) 0305 self.assert_(not b) 0306 0307 d = {} 0308 self.assertRaises(KeyError, d.popitem) 0309 0310 def test_pop(self): 0311 # Tests for pop with specified key 0312 d = {} 0313 k, v = 'abc', 'def' 0314 d[k] = v 0315 self.assertRaises(KeyError, d.pop, 'ghi') 0316 0317 self.assertEqual(d.pop(k), v) 0318 self.assertEqual(len(d), 0) 0319 0320 self.assertRaises(KeyError, d.pop, k) 0321 0322 # verify longs/ints get same value when key > 32 bits (for 64-bit archs) 0323 # see SF bug #689659 0324 x = 4503599627370496L 0325 y = 4503599627370496 0326 h = {x: 'anything', y: 'something else'} 0327 self.assertEqual(h[x], h[y]) 0328 0329 self.assertEqual(d.pop(k, v), v) 0330 d[k] = v 0331 self.assertEqual(d.pop(k, 1), v) 0332 0333 self.assertRaises(TypeError, d.pop) 0334 0335 class Exc(Exception): pass 0336 0337 class BadHash(object): 0338 fail = False 0339 def __hash__(self): 0340 if self.fail: 0341 raise Exc() 0342 else: 0343 return 42 0344 0345 x = BadHash() 0346 d[x] = 42 0347 x.fail = True 0348 self.assertRaises(Exc, d.pop, x) 0349 0350 def test_mutatingiteration(self): 0351 d = {} 0352 d[1] = 1 0353 try: 0354 for i in d: 0355 d[i+1] = 1 0356 except RuntimeError: 0357 pass 0358 else: 0359 self.fail("changing dict size during iteration doesn't raise Error") 0360 0361 def test_repr(self): 0362 d = {} 0363 self.assertEqual(repr(d), '{}') 0364 d[1] = 2 0365 self.assertEqual(repr(d), '{1: 2}') 0366 d = {} 0367 d[1] = d 0368 self.assertEqual(repr(d), '{1: {...}}') 0369 0370 class Exc(Exception): pass 0371 0372 class BadRepr(object): 0373 def __repr__(self): 0374 raise Exc() 0375 0376 d = {1: BadRepr()} 0377 self.assertRaises(Exc, repr, d) 0378 0379 def test_le(self): 0380 self.assert_(not ({} < {})) 0381 self.assert_(not ({1: 2} < {1L: 2L})) 0382 0383 class Exc(Exception): pass 0384 0385 class BadCmp(object): 0386 def __eq__(self, other): 0387 raise Exc() 0388 0389 d1 = {BadCmp(): 1} 0390 d2 = {1: 1} 0391 try: 0392 d1 < d2 0393 except Exc: 0394 pass 0395 else: 0396 self.fail("< didn't raise Exc") 0397 0398 import mapping_tests 0399 0400 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): 0401 type2test = dict 0402 0403 class Dict(dict): 0404 pass 0405 0406 class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): 0407 type2test = Dict 0408 0409 def test_main(): 0410 test_support.run_unittest( 0411 DictTest, 0412 GeneralMappingTests, 0413 SubclassMappingTests, 0414 ) 0415 0416 if __name__ == "__main__": 0417 test_main() 0418
Generated by PyXR 0.9.4