[Python-checkins] CVS: python/dist/src/Lib/test test_iter.py,1.6,1.7

Tim Peters tim_one@users.sourceforge.net
Thu, 03 May 2001 16:54:51 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv29503/python/dist/src/Lib/test

Modified Files:
	test_iter.py 
Log Message:
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.


Index: test_iter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_iter.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** test_iter.py	2001/05/03 07:09:25	1.6
--- test_iter.py	2001/05/03 23:54:49	1.7
***************
*** 352,354 ****
--- 352,389 ----
                  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)