summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_iter.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-05-05 11:33:43 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-05-05 11:33:43 (GMT)
commit75f8e35ef41aa6e7c915d99de8bd40be2745955c (patch)
treecbc72fb0979618f4b31f151d73d7b3bf775fd263 /Lib/test/test_iter.py
parent1434299a991edcb3a2bf604dc139719b2cca5490 (diff)
downloadcpython-75f8e35ef41aa6e7c915d99de8bd40be2745955c.zip
cpython-75f8e35ef41aa6e7c915d99de8bd40be2745955c.tar.gz
cpython-75f8e35ef41aa6e7c915d99de8bd40be2745955c.tar.bz2
Generalize PySequence_Count() (operator.countOf) to work with iterators.
Diffstat (limited to 'Lib/test/test_iter.py')
-rw-r--r--Lib/test/test_iter.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index bb9b102..7d15e1c 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -527,4 +527,39 @@ class TestCase(unittest.TestCase):
except OSError:
pass
+ # Test iterators with operator.countOf (PySequence_Count).
+ def test_countOf(self):
+ from operator import countOf
+ self.assertEqual(countOf([1,2,2,3,2,5], 2), 3)
+ self.assertEqual(countOf((1,2,2,3,2,5), 2), 3)
+ self.assertEqual(countOf("122325", "2"), 3)
+ self.assertEqual(countOf("122325", "6"), 0)
+
+ self.assertRaises(TypeError, countOf, 42, 1)
+ self.assertRaises(TypeError, countOf, countOf, countOf)
+
+ d = {"one": 3, "two": 3, "three": 3, 1j: 2j}
+ for k in d:
+ self.assertEqual(countOf(d, k), 1)
+ self.assertEqual(countOf(d.itervalues(), 3), 3)
+ self.assertEqual(countOf(d.itervalues(), 2j), 1)
+ self.assertEqual(countOf(d.itervalues(), 1j), 0)
+
+ f = open(TESTFN, "w")
+ try:
+ f.write("a\n" "b\n" "c\n" "b\n")
+ finally:
+ f.close()
+ f = open(TESTFN, "r")
+ try:
+ for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0):
+ f.seek(0, 0)
+ self.assertEqual(countOf(f, letter + "\n"), count)
+ finally:
+ f.close()
+ try:
+ unlink(TESTFN)
+ except OSError:
+ pass
+
run_unittest(TestCase)