summaryrefslogtreecommitdiff
path: root/Lib/test/test_mmap.py
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2007-08-28 15:28:19 +0000
committerThomas Wouters <thomas@python.org>2007-08-28 15:28:19 +0000
commit3ccec68a05abae43cf74dc7821c61ba88ab6cb46 (patch)
tree07e97200d168eec13110e16a11b974310b8b550a /Lib/test/test_mmap.py
parent0f4a14b56fcbd939e60f424517db61ca6f2f3885 (diff)
downloadcpython-git-3ccec68a05abae43cf74dc7821c61ba88ab6cb46.tar.gz
Improve extended slicing support in builtin types and classes. Specifically:
- Specialcase extended slices that amount to a shallow copy the same way as is done for simple slices, in the tuple, string and unicode case. - Specialcase step-1 extended slices to optimize the common case for all involved types. - For lists, allow extended slice assignment of differing lengths as long as the step is 1. (Previously, 'l[:2:1] = []' failed even though 'l[:2] = []' and 'l[:2:None] = []' do not.) - Implement extended slicing for buffer, array, structseq, mmap and UserString.UserString. - Implement slice-object support (but not non-step-1 slice assignment) for UserString.MutableString. - Add tests for all new functionality.
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r--Lib/test/test_mmap.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index 0b53823076..769eaed0b1 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -306,6 +306,40 @@ class MmapTests(unittest.TestCase):
m[x] = ch = chr(x & 255)
self.assertEqual(m[x], ch)
+ def test_extended_getslice(self):
+ # Test extended slicing by comparing with list slicing.
+ s = "".join(chr(c) for c in reversed(range(256)))
+ m = mmap.mmap(-1, len(s))
+ m[:] = s
+ self.assertEqual(m[:], s)
+ indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
+ for start in indices:
+ for stop in indices:
+ # Skip step 0 (invalid)
+ for step in indices[1:]:
+ self.assertEqual(m[start:stop:step],
+ s[start:stop:step])
+
+ def test_extended_set_del_slice(self):
+ # Test extended slicing by comparing with list slicing.
+ s = "".join(chr(c) for c in reversed(range(256)))
+ m = mmap.mmap(-1, len(s))
+ indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
+ for start in indices:
+ for stop in indices:
+ # Skip invalid step 0
+ for step in indices[1:]:
+ m[:] = s
+ self.assertEqual(m[:], s)
+ L = list(s)
+ # Make sure we have a slice of exactly the right length,
+ # but with different data.
+ data = L[start:stop:step]
+ data = "".join(reversed(data))
+ L[start:stop:step] = data
+ m[start:stop:step] = data
+ self.assertEquals(m[:], "".join(L))
+
def test_main():
run_unittest(MmapTests)