diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-09-08 04:00:12 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-09-08 04:00:12 (GMT) |
commit | 16a77adfbd745c202878fabb0b921514fec7ca16 (patch) | |
tree | 988fb5ffb34db1c7a12aa89c100efe1af17e80c6 /Lib | |
parent | 2d84f2c95a59b815ef6864805bb6b04b79d0e106 (diff) | |
download | cpython-16a77adfbd745c202878fabb0b921514fec7ca16.zip cpython-16a77adfbd745c202878fabb0b921514fec7ca16.tar.gz cpython-16a77adfbd745c202878fabb0b921514fec7ca16.tar.bz2 |
Generalize operator.indexOf (PySequence_Index) to work with any
iterable object. I'm not sure how that got overlooked before!
Got rid of the internal _PySequence_IterContains, introduced a new
internal _PySequence_IterSearch, and rewrote all the iteration-based
"count of", "index of", and "is the object in it or not?" routines to
just call the new function. I suppose it's slower this way, but the
code duplication was getting depressing.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_iter.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 8b6891b..37fab7c 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -600,6 +600,47 @@ class TestCase(unittest.TestCase): except OSError: pass + # Test iterators with operator.indexOf (PySequence_Index). + def test_indexOf(self): + from operator import indexOf + self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0) + self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1) + self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3) + self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5) + self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0) + self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6) + + self.assertEqual(indexOf("122325", "2"), 1) + self.assertEqual(indexOf("122325", "5"), 5) + self.assertRaises(ValueError, indexOf, "122325", "6") + + self.assertRaises(TypeError, indexOf, 42, 1) + self.assertRaises(TypeError, indexOf, indexOf, indexOf) + + f = open(TESTFN, "w") + try: + f.write("a\n" "b\n" "c\n" "d\n" "e\n") + finally: + f.close() + f = open(TESTFN, "r") + try: + fiter = iter(f) + self.assertEqual(indexOf(fiter, "b\n"), 1) + self.assertEqual(indexOf(fiter, "d\n"), 1) + self.assertEqual(indexOf(fiter, "e\n"), 0) + self.assertRaises(ValueError, indexOf, fiter, "a\n") + finally: + f.close() + try: + unlink(TESTFN) + except OSError: + pass + + iclass = IteratingSequenceClass(3) + for i in range(3): + self.assertEqual(indexOf(iclass, i), i) + self.assertRaises(ValueError, indexOf, iclass, -1) + # Test iterators on RHS of unpacking assignments. def test_unpack_iter(self): a, b = 1, 2 |