Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.mapping_tests.TestHashMappingProtocol Class Reference
Inheritance diagram for test.mapping_tests.TestHashMappingProtocol:
Inheritance graph
[legend]
Collaboration diagram for test.mapping_tests.TestHashMappingProtocol:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_getitem
def test_fromkeys
def test_pop
def test_mutatingiteration
def test_repr
def test_eq
def test_setdefault
def test_constructor
def test_bool
def test_keys
def test_values
def test_items
def test_contains
def test_len
def test_clear
def test_update
def test_copy
def test_get
def test_popitem
def test_read
def test_write

Public Attributes

 d
 reference
 other
 inmapping
 i

Static Public Attributes

 type2test = None

Detailed Description

Definition at line 535 of file mapping_tests.py.


Member Function Documentation

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 310 of file mapping_tests.py.

00310 
00311     def test_bool(self):
00312         BasicTestMappingProtocol.test_bool(self)
00313         self.assertTrue(not self._empty_mapping())
00314         self.assertTrue(self._full_mapping({"x": "y"}))
00315         self.assertTrue(bool(self._empty_mapping()) is False)
00316         self.assertTrue(bool(self._full_mapping({"x": "y"})) is True)

Here is the call graph for this function:

Definition at line 369 of file mapping_tests.py.

00369 
00370     def test_clear(self):
00371         d = self._full_mapping({1:1, 2:2, 3:3})
00372         d.clear()
00373         self.assertEqual(d, {})
00374 
00375         self.assertRaises(TypeError, d.clear, None)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 305 of file mapping_tests.py.

00305 
00306     def test_constructor(self):
00307         BasicTestMappingProtocol.test_constructor(self)
00308         self.assertTrue(self._empty_mapping() is not self._empty_mapping())
00309         self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2})

Here is the call graph for this function:

Definition at line 338 of file mapping_tests.py.

00338 
00339     def test_contains(self):
00340         d = self._empty_mapping()
00341         self.assertNotIn('a', d)
00342         self.assertTrue(not ('a' in d))
00343         self.assertTrue('a' not in d)
00344         d = self._full_mapping({'a': 1, 'b': 2})
00345         self.assertIn('a', d)
00346         self.assertIn('b', d)
00347         self.assertNotIn('c', d)
00348 
00349         self.assertRaises(TypeError, d.__contains__)

Here is the call graph for this function:

Definition at line 469 of file mapping_tests.py.

00469 
00470     def test_copy(self):
00471         d = self._full_mapping({1:1, 2:2, 3:3})
00472         self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
00473         d = self._empty_mapping()
00474         self.assertEqual(d.copy(), d)
00475         self.assertIsInstance(d.copy(), d.__class__)
00476         self.assertRaises(TypeError, d.copy, None)

Here is the call graph for this function:

Definition at line 622 of file mapping_tests.py.

00622 
00623     def test_eq(self):
00624         self.assertEqual(self._empty_mapping(), self._empty_mapping())
00625         self.assertEqual(self._full_mapping({1: 2}),
00626                          self._full_mapping({1: 2}))
00627 
00628         class Exc(Exception): pass
00629 
00630         class BadCmp(object):
00631             def __eq__(self, other):
00632                 raise Exc()
00633             def __hash__(self):
00634                 return 1
00635 
00636         d1 = self._full_mapping({BadCmp(): 1})
00637         d2 = self._full_mapping({1: 1})
00638         self.assertRaises(Exc, lambda: BadCmp()==1)
00639         self.assertRaises(Exc, lambda: d1==d2)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 565 of file mapping_tests.py.

00565 
00566     def test_fromkeys(self):
00567         TestMappingProtocol.test_fromkeys(self)
00568         class mydict(self.type2test):
00569             def __new__(cls):
00570                 return collections.UserDict()
00571         ud = mydict.fromkeys('ab')
00572         self.assertEqual(ud, {'a':None, 'b':None})
00573         self.assertIsInstance(ud, collections.UserDict)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 477 of file mapping_tests.py.

00477 
00478     def test_get(self):
00479         BasicTestMappingProtocol.test_get(self)
00480         d = self._empty_mapping()
00481         self.assertTrue(d.get('c') is None)
00482         self.assertEqual(d.get('c', 3), 3)
00483         d = self._full_mapping({'a' : 1, 'b' : 2})
00484         self.assertTrue(d.get('c') is None)
00485         self.assertEqual(d.get('c', 3), 3)
00486         self.assertEqual(d.get('a'), 1)
00487         self.assertEqual(d.get('a', 3), 1)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 537 of file mapping_tests.py.

00537 
00538     def test_getitem(self):
00539         TestMappingProtocol.test_getitem(self)
00540         class Exc(Exception): pass
00541 
00542         class BadEq(object):
00543             def __eq__(self, other):
00544                 raise Exc()
00545             def __hash__(self):
00546                 return 24
00547 
00548         d = self._empty_mapping()
00549         d[BadEq()] = 42
00550         self.assertRaises(KeyError, d.__getitem__, 23)
00551 
00552         class BadHash(object):
00553             fail = False
00554             def __hash__(self):
00555                 if self.fail:
00556                     raise Exc()
00557                 else:
00558                     return 42
00559 
00560         d = self._empty_mapping()
00561         x = BadHash()
00562         d[x] = 42
00563         x.fail = True
00564         self.assertRaises(Exc, d.__getitem__, x)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 332 of file mapping_tests.py.

00332 
00333     def test_items(self):
00334         BasicTestMappingProtocol.test_items(self)
00335 
00336         d = self._full_mapping({1:2})
00337         self.assertEqual(list(d.items()), [(1, 2)])

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 317 of file mapping_tests.py.

00317 
00318     def test_keys(self):
00319         BasicTestMappingProtocol.test_keys(self)
00320         d = self._empty_mapping()
00321         self.assertEqual(list(d.keys()), [])
00322         d = self._full_mapping({'a': 1, 'b': 2})
00323         k = d.keys()
00324         self.assertIn('a', k)
00325         self.assertIn('b', k)
00326         self.assertNotIn('c', k)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 350 of file mapping_tests.py.

00350 
00351     def test_len(self):
00352         BasicTestMappingProtocol.test_len(self)
00353         d = self._full_mapping({'a': 1, 'b': 2})
00354         self.assertEqual(len(d), 2)

Here is the call graph for this function:

Definition at line 593 of file mapping_tests.py.

00593 
00594     def test_mutatingiteration(self):
00595         d = self._empty_mapping()
00596         d[1] = 1
00597         try:
00598             for i in d:
00599                 d[i+1] = 1
00600         except RuntimeError:
00601             pass
00602         else:
00603             self.fail("changing dict size during iteration doesn't raise Error")

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 574 of file mapping_tests.py.

00574 
00575     def test_pop(self):
00576         TestMappingProtocol.test_pop(self)
00577 
00578         class Exc(Exception): pass
00579 
00580         class BadHash(object):
00581             fail = False
00582             def __hash__(self):
00583                 if self.fail:
00584                     raise Exc()
00585                 else:
00586                     return 42
00587 
00588         d = self._empty_mapping()
00589         x = BadHash()
00590         d[x] = 42
00591         x.fail = True
00592         self.assertRaises(Exc, d.pop, x)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 499 of file mapping_tests.py.

00499 
00500     def test_popitem(self):
00501         BasicTestMappingProtocol.test_popitem(self)
00502         for copymode in -1, +1:
00503             # -1: b has same structure as a
00504             # +1: b is a.copy()
00505             for log2size in range(12):
00506                 size = 2**log2size
00507                 a = self._empty_mapping()
00508                 b = self._empty_mapping()
00509                 for i in range(size):
00510                     a[repr(i)] = i
00511                     if copymode < 0:
00512                         b[repr(i)] = i
00513                 if copymode > 0:
00514                     b = a.copy()
00515                 for i in range(size):
00516                     ka, va = ta = a.popitem()
00517                     self.assertEqual(va, int(ka))
00518                     kb, vb = tb = b.popitem()
00519                     self.assertEqual(vb, int(kb))
00520                     self.assertTrue(not(copymode < 0 and ta != tb))
00521                 self.assertTrue(not a)
00522                 self.assertTrue(not b)

Here is the call graph for this function:

Definition at line 42 of file mapping_tests.py.

00042 
00043     def test_read(self):
00044         # Test for read only operations on mapping
00045         p = self._empty_mapping()
00046         p1 = dict(p) #workaround for singleton objects
00047         d = self._full_mapping(self.reference)
00048         if d is p:
00049             p = p1
00050         #Indexing
00051         for key, value in self.reference.items():
00052             self.assertEqual(d[key], value)
00053         knownkey = list(self.other.keys())[0]
00054         self.assertRaises(KeyError, lambda:d[knownkey])
00055         #len
00056         self.assertEqual(len(p), 0)
00057         self.assertEqual(len(d), len(self.reference))
00058         #__contains__
00059         for k in self.reference:
00060             self.assertIn(k, d)
00061         for k in self.other:
00062             self.assertNotIn(k, d)
00063         #cmp
00064         self.assertEqual(p, p)
00065         self.assertEqual(d, d)
00066         self.assertNotEqual(p, d)
00067         self.assertNotEqual(d, p)
00068         #__non__zero__
00069         if p: self.fail("Empty mapping must compare to False")
00070         if not d: self.fail("Full mapping must compare to True")
00071         # keys(), items(), iterkeys() ...
00072         def check_iterandlist(iter, lst, ref):
00073             self.assertTrue(hasattr(iter, '__next__'))
00074             self.assertTrue(hasattr(iter, '__iter__'))
00075             x = list(iter)
00076             self.assertTrue(set(x)==set(lst)==set(ref))
00077         check_iterandlist(iter(d.keys()), list(d.keys()),
00078                           self.reference.keys())
00079         check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
00080         check_iterandlist(iter(d.values()), list(d.values()),
00081                           self.reference.values())
00082         check_iterandlist(iter(d.items()), list(d.items()),
00083                           self.reference.items())
00084         #get
00085         key, value = next(iter(d.items()))
00086         knownkey, knownvalue = next(iter(self.other.items()))
00087         self.assertEqual(d.get(key, knownvalue), value)
00088         self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
00089         self.assertNotIn(knownkey, d)

Here is the call graph for this function:

Definition at line 604 of file mapping_tests.py.

00604 
00605     def test_repr(self):
00606         d = self._empty_mapping()
00607         self.assertEqual(repr(d), '{}')
00608         d[1] = 2
00609         self.assertEqual(repr(d), '{1: 2}')
00610         d = self._empty_mapping()
00611         d[1] = d
00612         self.assertEqual(repr(d), '{1: {...}}')
00613 
00614         class Exc(Exception): pass
00615 
00616         class BadRepr(object):
00617             def __repr__(self):
00618                 raise Exc()
00619 
00620         d = self._full_mapping({1: BadRepr()})
00621         self.assertRaises(Exc, repr, d)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 640 of file mapping_tests.py.

00640 
00641     def test_setdefault(self):
00642         TestMappingProtocol.test_setdefault(self)
00643 
00644         class Exc(Exception): pass
00645 
00646         class BadHash(object):
00647             fail = False
00648             def __hash__(self):
00649                 if self.fail:
00650                     raise Exc()
00651                 else:
00652                     return 42
00653 
00654         d = self._empty_mapping()
00655         x = BadHash()
00656         d[x] = 42
00657         x.fail = True
00658         self.assertRaises(Exc, d.setdefault, x, [])

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 376 of file mapping_tests.py.

00376 
00377     def test_update(self):
00378         BasicTestMappingProtocol.test_update(self)
00379         # mapping argument
00380         d = self._empty_mapping()
00381         d.update({1:100})
00382         d.update({2:20})
00383         d.update({1:1, 2:2, 3:3})
00384         self.assertEqual(d, {1:1, 2:2, 3:3})
00385 
00386         # no argument
00387         d.update()
00388         self.assertEqual(d, {1:1, 2:2, 3:3})
00389 
00390         # keyword arguments
00391         d = self._empty_mapping()
00392         d.update(x=100)
00393         d.update(y=20)
00394         d.update(x=1, y=2, z=3)
00395         self.assertEqual(d, {"x":1, "y":2, "z":3})
00396 
00397         # item sequence
00398         d = self._empty_mapping()
00399         d.update([("x", 100), ("y", 20)])
00400         self.assertEqual(d, {"x":100, "y":20})
00401 
00402         # Both item sequence and keyword arguments
00403         d = self._empty_mapping()
00404         d.update([("x", 100), ("y", 20)], x=1, y=2)
00405         self.assertEqual(d, {"x":1, "y":2})
00406 
00407         # iterator
00408         d = self._full_mapping({1:3, 2:4})
00409         d.update(self._full_mapping({1:2, 3:4, 5:6}).items())
00410         self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
00411 
00412         class SimpleUserDict:
00413             def __init__(self):
00414                 self.d = {1:1, 2:2, 3:3}
00415             def keys(self):
00416                 return self.d.keys()
00417             def __getitem__(self, i):
00418                 return self.d[i]
00419         d.clear()
00420         d.update(SimpleUserDict())
00421         self.assertEqual(d, {1:1, 2:2, 3:3})

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 327 of file mapping_tests.py.

00327 
00328     def test_values(self):
00329         BasicTestMappingProtocol.test_values(self)
00330         d = self._full_mapping({1:2})
00331         self.assertEqual(list(d.values()), [2])

Here is the call graph for this function:

Definition at line 90 of file mapping_tests.py.

00090 
00091     def test_write(self):
00092         # Test for write operations on mapping
00093         p = self._empty_mapping()
00094         #Indexing
00095         for key, value in self.reference.items():
00096             p[key] = value
00097             self.assertEqual(p[key], value)
00098         for key in self.reference.keys():
00099             del p[key]
00100             self.assertRaises(KeyError, lambda:p[key])
00101         p = self._empty_mapping()
00102         #update
00103         p.update(self.reference)
00104         self.assertEqual(dict(p), self.reference)
00105         items = list(p.items())
00106         p = self._empty_mapping()
00107         p.update(items)
00108         self.assertEqual(dict(p), self.reference)
00109         d = self._full_mapping(self.reference)
00110         #setdefault
00111         key, value = next(iter(d.items()))
00112         knownkey, knownvalue = next(iter(self.other.items()))
00113         self.assertEqual(d.setdefault(key, knownvalue), value)
00114         self.assertEqual(d[key], value)
00115         self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
00116         self.assertEqual(d[knownkey], knownvalue)
00117         #pop
00118         self.assertEqual(d.pop(knownkey), knownvalue)
00119         self.assertNotIn(knownkey, d)
00120         self.assertRaises(KeyError, d.pop, knownkey)
00121         default = 909
00122         d[knownkey] = knownvalue
00123         self.assertEqual(d.pop(knownkey, default), knownvalue)
00124         self.assertNotIn(knownkey, d)
00125         self.assertEqual(d.pop(knownkey, default), default)
00126         #popitem
00127         key, value = d.popitem()
00128         self.assertNotIn(key, d)
00129         self.assertEqual(value, self.reference[key])
00130         p=self._empty_mapping()
00131         self.assertRaises(KeyError, p.popitem)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 413 of file mapping_tests.py.

Definition at line 225 of file mapping_tests.py.

Definition at line 39 of file mapping_tests.py.

Definition at line 35 of file mapping_tests.py.

Definition at line 31 of file mapping_tests.py.


The documentation for this class was generated from the following file: