From: Tim Peters Date: Tue, 11 Feb 2003 16:40:16 +0000 (+0000) Subject: Added tests to ensure that list and dict "chunking" are actually X-Git-Tag: v2.3c1~1916 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8d2613adbeba5b133fd1ca0f2fadf1f5e37342f1;p=python Added tests to ensure that list and dict "chunking" are actually getting done. Since this isn't yet implemented in cPickle, the new tests are in TempAbstractPickleTests (which cPickle doesn't run). --- diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 4c039bc6eb..b161dd5773 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -18,6 +18,14 @@ def opcode_in_pickle(code, pickle): return True return False +# Return the number of times opcode code appears in pickle. +def count_opcode(code, pickle): + n = 0 + for op, dummy, dummy in pickletools.genops(pickle): + if op.code == code: + n += 1 + return n + # We can't very well test the extension registry without putting known stuff # in it, but we have to be careful to restore its original state. Code # should do this: @@ -664,7 +672,6 @@ class AbstractPickleTests(unittest.TestCase): self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness - # XXX Temporary hack, so long as the C implementation of pickle protocol # XXX 2 isn't ready. When it is, move the methods in TempAbstractPickleTests # XXX into AbstractPickleTests above, and get rid of TempAbstractPickleTests @@ -682,6 +689,49 @@ class TempAbstractPickleTests(unittest.TestCase): self.assertEqual(x.foo, y.foo) self.assertEqual(x.bar, y.bar) + def test_list_chunking(self): + n = 10 # too small to chunk + x = range(n) + for proto in protocols: + s = self.dumps(x, proto) + y = self.loads(s) + self.assertEqual(x, y) + num_appends = count_opcode(pickle.APPENDS, s) + self.assertEqual(num_appends, proto > 0) + + n = 2500 # expect at least two chunks when proto > 0 + x = range(n) + for proto in protocols: + s = self.dumps(x, proto) + y = self.loads(s) + self.assertEqual(x, y) + num_appends = count_opcode(pickle.APPENDS, s) + if proto == 0: + self.assertEqual(num_appends, 0) + else: + self.failUnless(num_appends >= 2) + + def test_dict_chunking(self): + n = 10 # too small to chunk + x = dict.fromkeys(range(n)) + for proto in protocols: + s = self.dumps(x, proto) + y = self.loads(s) + self.assertEqual(x, y) + num_setitems = count_opcode(pickle.SETITEMS, s) + self.assertEqual(num_setitems, proto > 0) + + n = 2500 # expect at least two chunks when proto > 0 + x = dict.fromkeys(range(n)) + for proto in protocols: + s = self.dumps(x, proto) + y = self.loads(s) + self.assertEqual(x, y) + num_setitems = count_opcode(pickle.SETITEMS, s) + if proto == 0: + self.assertEqual(num_setitems, 0) + else: + self.failUnless(num_setitems >= 2) class MyInt(int): sample = 1