Skip to content

Update test_ordered_dict.py from Cpython v3.11.2 #4846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 124 additions & 4 deletions Lib/test/test_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def test_copying(self):
# and have a repr/eval round-trip
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
od = OrderedDict(pairs)
od.x = ['x']
od.z = ['z']
def check(dup):
msg = "\ncopy: %s\nod: %s" % (dup, od)
self.assertIsNot(dup, od, msg)
Expand All @@ -295,13 +297,27 @@ def check(dup):
self.assertEqual(len(dup), len(od))
self.assertEqual(type(dup), type(od))
check(od.copy())
check(copy.copy(od))
check(copy.deepcopy(od))
dup = copy.copy(od)
check(dup)
self.assertIs(dup.x, od.x)
self.assertIs(dup.z, od.z)
self.assertFalse(hasattr(dup, 'y'))
dup = copy.deepcopy(od)
check(dup)
self.assertEqual(dup.x, od.x)
self.assertIsNot(dup.x, od.x)
self.assertEqual(dup.z, od.z)
self.assertIsNot(dup.z, od.z)
self.assertFalse(hasattr(dup, 'y'))
# pickle directly pulls the module, so we have to fake it
with replaced_module('collections', self.module):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
check(pickle.loads(pickle.dumps(od, proto)))
dup = pickle.loads(pickle.dumps(od, proto))
check(dup)
self.assertEqual(dup.x, od.x)
self.assertEqual(dup.z, od.z)
self.assertFalse(hasattr(dup, 'y'))
check(eval(repr(od)))
update_test = OrderedDict()
update_test.update(od)
Expand Down Expand Up @@ -756,7 +772,7 @@ def test_sizeof_exact(self):
check = self.check_sizeof

basicsize = size('nQ2P' + '3PnPn2P')
keysize = calcsize('2nP2n')
keysize = calcsize('n2BI2n')

entrysize = calcsize('n2P')
p = calcsize('P')
Expand Down Expand Up @@ -853,6 +869,25 @@ class CPythonOrderedDictSubclassTests(CPythonOrderedDictTests):
class OrderedDict(c_coll.OrderedDict):
pass

# TODO: RUSTPYTHON
@unittest.expectedFailure
class PurePythonOrderedDictWithSlotsCopyingTests(unittest.TestCase):

module = py_coll
class OrderedDict(py_coll.OrderedDict):
__slots__ = ('x', 'y')
test_copying = OrderedDictTests.test_copying

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
class CPythonOrderedDictWithSlotsCopyingTests(unittest.TestCase):

module = c_coll
class OrderedDict(c_coll.OrderedDict):
__slots__ = ('x', 'y')
test_copying = OrderedDictTests.test_copying


class PurePythonGeneralMappingTests(mapping_tests.BasicTestMappingProtocol):

Expand Down Expand Up @@ -904,5 +939,90 @@ def test_popitem(self):
self.assertRaises(KeyError, d.popitem)


class SimpleLRUCache:

def __init__(self, size):
super().__init__()
self.size = size
self.counts = dict.fromkeys(('get', 'set', 'del'), 0)

def __getitem__(self, item):
self.counts['get'] += 1
value = super().__getitem__(item)
self.move_to_end(item)
return value

def __setitem__(self, key, value):
self.counts['set'] += 1
while key not in self and len(self) >= self.size:
self.popitem(last=False)
super().__setitem__(key, value)
self.move_to_end(key)

def __delitem__(self, key):
self.counts['del'] += 1
super().__delitem__(key)


class SimpleLRUCacheTests:

def test_add_after_full(self):
c = self.type2test(2)
c['t1'] = 1
c['t2'] = 2
c['t3'] = 3
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})
self.assertEqual(list(c), ['t2', 't3'])
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})

def test_popitem(self):
c = self.type2test(3)
for i in range(1, 4):
c[i] = i
self.assertEqual(c.popitem(last=False), (1, 1))
self.assertEqual(c.popitem(last=True), (3, 3))
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pop(self):
c = self.type2test(3)
for i in range(1, 4):
c[i] = i
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})
self.assertEqual(c.pop(2), 2)
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})
self.assertEqual(c.pop(4, 0), 0)
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})
self.assertRaises(KeyError, c.pop, 4)
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})

def test_change_order_on_get(self):
c = self.type2test(3)
for i in range(1, 4):
c[i] = i
self.assertEqual(list(c), list(range(1, 4)))
self.assertEqual(c.counts, {'get': 0, 'set': 3, 'del': 0})
self.assertEqual(c[2], 2)
self.assertEqual(c.counts, {'get': 1, 'set': 3, 'del': 0})
self.assertEqual(list(c), [1, 3, 2])


class PySimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):

class type2test(SimpleLRUCache, py_coll.OrderedDict):
pass

@unittest.skip("TODO: RUSTPYTHON")
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
class CSimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):

@classmethod
def setUpClass(cls):
class type2test(SimpleLRUCache, c_coll.OrderedDict):
pass
cls.type2test = type2test


if __name__ == "__main__":
unittest.main()