diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-05-03 23:54:49 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-05-03 23:54:49 (GMT) |
commit | 4e9afdca392668bc7e07c79d8af0f95ef497fba4 (patch) | |
tree | 1d5fe4e4a32f838e7ae7189c49a7ae42fc139f51 /Lib/test/test_iter.py | |
parent | 6aebded915ac3ca6dcd99ea4bf3dfc47a31bab1e (diff) | |
download | cpython-4e9afdca392668bc7e07c79d8af0f95ef497fba4.zip cpython-4e9afdca392668bc7e07c79d8af0f95ef497fba4.tar.gz cpython-4e9afdca392668bc7e07c79d8af0f95ef497fba4.tar.bz2 |
Generalize map() to work with iterators.
NEEDS DOC CHANGES.
Possibly contentious: The first time s.next() yields StopIteration (for
a given map argument s) is the last time map() *tries* s.next(). That
is, if other sequence args are longer, s will never again contribute
anything but None values to the result, even if trying s.next() again
could yield another result. This is the same behavior map() used to have
wrt IndexError, so it's the only way to be wholly backward-compatible.
I'm not a fan of letting StopIteration mean "try again later" anyway.
Diffstat (limited to 'Lib/test/test_iter.py')
-rw-r--r-- | Lib/test/test_iter.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 3563661..c87f5ec 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -351,4 +351,39 @@ class TestCase(unittest.TestCase): except OSError: pass + # Test map()'s use of iterators. + def test_builtin_map(self): + self.assertEqual(map(None, SequenceClass(5)), range(5)) + self.assertEqual(map(lambda x: x+1, SequenceClass(5)), range(1, 6)) + + d = {"one": 1, "two": 2, "three": 3} + self.assertEqual(map(None, d), d.keys()) + self.assertEqual(map(lambda k, d=d: (k, d[k]), d), d.items()) + dkeys = d.keys() + expected = [(i < len(d) and dkeys[i] or None, + i, + i < len(d) and dkeys[i] or None) + for i in range(5)] + self.assertEqual(map(None, d, + SequenceClass(5), + iter(d.iterkeys())), + expected) + + f = open(TESTFN, "w") + try: + for i in range(10): + f.write("xy" * i + "\n") # line i has len 2*i+1 + finally: + f.close() + f = open(TESTFN, "r") + try: + self.assertEqual(map(len, f), range(1, 21, 2)) + f.seek(0, 0) + finally: + f.close() + try: + unlink(TESTFN) + except OSError: + pass + run_unittest(TestCase) |