[Python-checkins] python/dist/src/Lib/test test_itertools.py,1.14,1.15

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
2003年6月29日 13:36:25 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv26692
Modified Files:
	test_itertools.py 
Log Message:
More tests
* Test with infinite inputs (using take() on the output)
* Test whether GC can find and eliminate cycles.
Index: test_itertools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_itertools.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** test_itertools.py	28 Jun 2003 05:44:36 -0000	1.14
--- test_itertools.py	29 Jun 2003 20:36:23 -0000	1.15
***************
*** 33,36 ****
--- 33,39 ----
 raise StopIteration
 
+ def take(n, seq):
+ 'Convenience function for partially consuming a long of infinite iterable'
+ return list(islice(seq, n))
 
 class TestBasicOps(unittest.TestCase):
***************
*** 39,42 ****
--- 42,46 ----
 self.assertEqual(list(chain('abc')), list('abc'))
 self.assertEqual(list(chain('')), [])
+ self.assertEqual(take(4, chain('abc', 'def')), list('abcd'))
 self.assertRaises(TypeError, chain, 2, 3)
 
***************
*** 44,47 ****
--- 48,52 ----
 self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
 self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
+ self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)])
 self.assertRaises(TypeError, count, 2, 3)
 self.assertRaises(TypeError, count, 'a')
***************
*** 50,54 ****
 
 def test_cycle(self):
! self.assertEqual(list(islice(cycle('abc'),10)), list('abcabcabca'))
 self.assertEqual(list(cycle('')), [])
 self.assertRaises(TypeError, cycle)
--- 55,59 ----
 
 def test_cycle(self):
! self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
 self.assertEqual(list(cycle('')), [])
 self.assertRaises(TypeError, cycle)
***************
*** 59,62 ****
--- 64,68 ----
 self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
 self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
+ self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6])
 self.assertRaises(TypeError, ifilter)
 self.assertRaises(TypeError, ifilter, lambda x:x)
***************
*** 68,71 ****
--- 74,78 ----
 self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
 self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
+ self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
 self.assertRaises(TypeError, ifilterfalse)
 self.assertRaises(TypeError, ifilterfalse, lambda x:x)
***************
*** 79,82 ****
--- 86,90 ----
 self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6)))
 self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3)))
+ self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3)))
 self.assertEqual(list(izip('abcdef')), zip('abcdef'))
 self.assertRaises(TypeError, izip)
***************
*** 97,100 ****
--- 105,109 ----
 [(0, 'a'), (1, 'a'), (2, 'a')])
 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
+ self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
 self.assertEqual(list(repeat('a', 0)), [])
 self.assertEqual(list(repeat('a', -3)), [])
***************
*** 108,111 ****
--- 117,124 ----
 self.assertEqual(list(imap(None, 'abc', range(5))),
 [('a',0),('b',1),('c',2)])
+ self.assertEqual(list(imap(None, 'abc', count())),
+ [('a',0),('b',1),('c',2)])
+ self.assertEqual(take(2,imap(None, 'abc', count())),
+ [('a',0),('b',1)])
 self.assertEqual(list(imap(operator.pow, [])), [])
 self.assertRaises(TypeError, imap)
***************
*** 118,121 ****
--- 131,136 ----
 self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
 [0**1, 1**2, 2**3])
+ self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
+ [0**1, 1**2, 2**3])
 self.assertEqual(list(starmap(operator.pow, [])), [])
 self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
***************
*** 274,277 ****
--- 289,331 ----
 return chain(imap(lambda x:x, R(Ig(G(seqn)))))
 
+ class TestGC(unittest.TestCase):
+ 
+ def makecycle(self, iterator, container):
+ container.append(iterator)
+ iterator.next()
+ del container, iterator
+ 
+ def test_chain(self):
+ a = []
+ self.makecycle(chain(a), a)
+ 
+ def test_cycle(self):
+ a = []
+ self.makecycle(cycle([a]*2), a)
+ 
+ def test_ifilter(self):
+ a = []
+ self.makecycle(ifilter(lambda x:True, [a]*2), a)
+ 
+ def test_ifilterfalse(self):
+ a = []
+ self.makecycle(ifilterfalse(lambda x:False, a), a)
+ 
+ def test_izip(self):
+ a = []
+ self.makecycle(izip([a]*2, [a]*3), a)
+ 
+ def test_imap(self):
+ a = []
+ self.makecycle(imap(lambda x:x, [a]*2), a)
+ 
+ def test_islice(self):
+ a = []
+ self.makecycle(islice([a]*2, None), a)
+ 
+ def test_starmap(self):
+ a = []
+ self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
+ 
 
 class TestVariousIteratorArgs(unittest.TestCase):
***************
*** 421,425 ****
 
 >>> def some(pred, seq):
! ... "Returns True if pred(x) is True at least one element in the iterable"
 ... return bool(nth(ifilter(pred, seq), 0))
 
--- 475,479 ----
 
 >>> def some(pred, seq):
! ... "Returns True if pred(x) is True for at least one element in the iterable"
 ... return bool(nth(ifilter(pred, seq), 0))
 
***************
*** 506,517 ****
 
 def test_main(verbose=None):
! test_classes = (TestBasicOps, TestVariousIteratorArgs)
 test_support.run_unittest(*test_classes)
 
 # verify reference counting
 if verbose and hasattr(sys, "gettotalrefcount"):
 counts = [None] * 5
 for i in xrange(len(counts)):
 test_support.run_unittest(*test_classes)
 counts[i] = sys.gettotalrefcount()
 print counts
--- 560,573 ----
 
 def test_main(verbose=None):
! test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC)
 test_support.run_unittest(*test_classes)
 
 # verify reference counting
 if verbose and hasattr(sys, "gettotalrefcount"):
+ import gc
 counts = [None] * 5
 for i in xrange(len(counts)):
 test_support.run_unittest(*test_classes)
+ gc.collect()
 counts[i] = sys.gettotalrefcount()
 print counts

AltStyle によって変換されたページ (->オリジナル) /