summaryrefslogtreecommitdiffstats
path: root/Lib/test/seq_tests.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-07 00:41:02 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-07 00:41:02 (GMT)
commit65baa34115476cd42d41af5ca01c58226caab255 (patch)
treeffb9e77bab2a4b4995cbebc922c489881032e1ab /Lib/test/seq_tests.py
parent2f653c19f4ef01f326692a2e2a8111eeaa4286ac (diff)
downloadcpython-65baa34115476cd42d41af5ca01c58226caab255.zip
cpython-65baa34115476cd42d41af5ca01c58226caab255.tar.gz
cpython-65baa34115476cd42d41af5ca01c58226caab255.tar.bz2
Issue 2025: Add index() and count() methods to tuple so that it will follow
the ABC for collections.Sequence.
Diffstat (limited to 'Lib/test/seq_tests.py')
-rw-r--r--Lib/test/seq_tests.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py
index ebd6157..81fe627 100644
--- a/Lib/test/seq_tests.py
+++ b/Lib/test/seq_tests.py
@@ -4,6 +4,7 @@ Tests common to tuple, list and UserList.UserList
import unittest
from test import test_support
+import sys
# Various iterables
# This is used for checking the constructor (here and in test_deque.py)
@@ -326,3 +327,64 @@ class CommonTest(unittest.TestCase):
self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
self.assertRaises(TypeError, a.__getitem__, 'x')
+
+ def test_count(self):
+ a = self.type2test([0, 1, 2])*3
+ self.assertEqual(a.count(0), 3)
+ self.assertEqual(a.count(1), 3)
+ self.assertEqual(a.count(3), 0)
+
+ self.assertRaises(TypeError, a.count)
+
+ class BadExc(Exception):
+ pass
+
+ class BadCmp:
+ def __eq__(self, other):
+ if other == 2:
+ raise BadExc()
+ return False
+
+ self.assertRaises(BadExc, a.count, BadCmp())
+
+ def test_index(self):
+ u = self.type2test([0, 1])
+ self.assertEqual(u.index(0), 0)
+ self.assertEqual(u.index(1), 1)
+ self.assertRaises(ValueError, u.index, 2)
+
+ u = self.type2test([-2, -1, 0, 0, 1, 2])
+ self.assertEqual(u.count(0), 2)
+ self.assertEqual(u.index(0), 2)
+ self.assertEqual(u.index(0, 2), 2)
+ self.assertEqual(u.index(-2, -10), 0)
+ self.assertEqual(u.index(0, 3), 3)
+ self.assertEqual(u.index(0, 3, 4), 3)
+ self.assertRaises(ValueError, u.index, 2, 0, -10)
+
+ self.assertRaises(TypeError, u.index)
+
+ class BadExc(Exception):
+ pass
+
+ class BadCmp:
+ def __eq__(self, other):
+ if other == 2:
+ raise BadExc()
+ return False
+
+ a = self.type2test([0, 1, 2, 3])
+ self.assertRaises(BadExc, a.index, BadCmp())
+
+ a = self.type2test([-2, -1, 0, 0, 1, 2])
+ self.assertEqual(a.index(0), 2)
+ self.assertEqual(a.index(0, 2), 2)
+ self.assertEqual(a.index(0, -4), 2)
+ self.assertEqual(a.index(-2, -10), 0)
+ self.assertEqual(a.index(0, 3), 3)
+ self.assertEqual(a.index(0, -3), 3)
+ self.assertEqual(a.index(0, 3, 4), 3)
+ self.assertEqual(a.index(0, -3, -2), 3)
+ self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2)
+ self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize)
+ self.assertRaises(ValueError, a.index, 2, 0, -10)