annotate Lib/test/seq_tests.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 19ffa12ffdd4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
1 """
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
2 Tests common to tuple, list and UserList.UserList
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
3 """
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
4
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
5 import unittest
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
6 import sys
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
7
34802
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
8 # Various iterables
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
9 # This is used for checking the constructor (here and in test_deque.py)
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
10 def iterfunc(seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
11 'Regular generator'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
12 for i in seqn:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
13 yield i
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
14
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
15 class Sequence:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
16 'Sequence using __getitem__'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
17 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
18 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
19 def __getitem__(self, i):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
20 return self.seqn[i]
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
21
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
22 class IterFunc:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
23 'Sequence using iterator protocol'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
24 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
25 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
26 self.i = 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
27 def __iter__(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
28 return self
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
29 def next(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
30 if self.i >= len(self.seqn): raise StopIteration
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
31 v = self.seqn[self.i]
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
32 self.i += 1
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
33 return v
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
34
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
35 class IterGen:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
36 'Sequence using iterator protocol defined with a generator'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
37 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
38 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
39 self.i = 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
40 def __iter__(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
41 for val in self.seqn:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
42 yield val
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
43
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
44 class IterNextOnly:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
45 'Missing __getitem__ and __iter__'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
46 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
47 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
48 self.i = 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
49 def next(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
50 if self.i >= len(self.seqn): raise StopIteration
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
51 v = self.seqn[self.i]
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
52 self.i += 1
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
53 return v
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
54
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
55 class IterNoNext:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
56 'Iterator missing next()'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
57 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
58 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
59 self.i = 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
60 def __iter__(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
61 return self
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
62
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
63 class IterGenExc:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
64 'Test propagation of exceptions'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
65 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
66 self.seqn = seqn
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
67 self.i = 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
68 def __iter__(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
69 return self
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
70 def next(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
71 3 // 0
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
72
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
73 class IterFuncStop:
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
74 'Test immediate stop'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
75 def __init__(self, seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
76 pass
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
77 def __iter__(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
78 return self
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
79 def next(self):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
80 raise StopIteration
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
81
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
82 from itertools import chain, imap
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
83 def itermulti(seqn):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
84 'Test multiple tiers of iterators'
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
85 return chain(imap(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
86
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
87 class CommonTest(unittest.TestCase):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
88 # The type to be tested
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
89 type2test = None
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
90
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
91 def test_constructors(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
92 l0 = []
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
93 l1 = [0]
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
94 l2 = [0, 1]
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
95
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
96 u = self.type2test()
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
97 u0 = self.type2test(l0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
98 u1 = self.type2test(l1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
99 u2 = self.type2test(l2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
100
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
101 uu = self.type2test(u)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
102 uu0 = self.type2test(u0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
103 uu1 = self.type2test(u1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
104 uu2 = self.type2test(u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
105
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
106 v = self.type2test(tuple(u))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
107 class OtherSeq:
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
108 def __init__(self, initseq):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
109 self.__data = initseq
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
110 def __len__(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
111 return len(self.__data)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
112 def __getitem__(self, i):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
113 return self.__data[i]
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 s = OtherSeq(u0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
115 v0 = self.type2test(s)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116 self.assertEqual(len(v0), len(s))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
117
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
118 s = "this is also a sequence"
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
119 vv = self.type2test(s)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
120 self.assertEqual(len(vv), len(s))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
121
34802
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
122 # Create from various iteratables
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
123 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
124 for g in (Sequence, IterFunc, IterGen,
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
125 itermulti, iterfunc):
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
126 self.assertEqual(self.type2test(g(s)), self.type2test(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
127 self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
128 self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
129 self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
130 self.assertRaises(TypeError, self.type2test, IterNoNext(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
131 self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 33629
diff changeset
132
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
133 def test_truth(self):
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
134 self.assertFalse(self.type2test())
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
135 self.assertTrue(self.type2test([42]))
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
136
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
137 def test_getitem(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
138 u = self.type2test([0, 1, 2, 3, 4])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
139 for i in xrange(len(u)):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
140 self.assertEqual(u[i], i)
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
141 self.assertEqual(u[long(i)], i)
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
142 for i in xrange(-len(u), -1):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
143 self.assertEqual(u[i], len(u)+i)
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
144 self.assertEqual(u[long(i)], len(u)+i)
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145 self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
146 self.assertRaises(IndexError, u.__getitem__, len(u))
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
147 self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
148
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
149 u = self.type2test()
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
150 self.assertRaises(IndexError, u.__getitem__, 0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
151 self.assertRaises(IndexError, u.__getitem__, -1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
152
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
153 self.assertRaises(TypeError, u.__getitem__)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
154
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
155 a = self.type2test([10, 11])
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
156 self.assertEqual(a[0], 10)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
157 self.assertEqual(a[1], 11)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
158 self.assertEqual(a[-2], 10)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
159 self.assertEqual(a[-1], 11)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
160 self.assertRaises(IndexError, a.__getitem__, -3)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
161 self.assertRaises(IndexError, a.__getitem__, 3)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
162
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
163 def test_getslice(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
164 l = [0, 1, 2, 3, 4]
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
165 u = self.type2test(l)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
166
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
167 self.assertEqual(u[0:0], self.type2test())
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
168 self.assertEqual(u[1:2], self.type2test([1]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
169 self.assertEqual(u[-2:-1], self.type2test([3]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
170 self.assertEqual(u[-1000:1000], u)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
171 self.assertEqual(u[1000:-1000], self.type2test([]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
172 self.assertEqual(u[:], u)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
173 self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
174 self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
175
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
176 # Extended slices
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
177 self.assertEqual(u[::], u)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
178 self.assertEqual(u[::2], self.type2test([0, 2, 4]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
179 self.assertEqual(u[1::2], self.type2test([1, 3]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180 self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
182 self.assertEqual(u[3::-2], self.type2test([3, 1]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
183 self.assertEqual(u[3:3:-2], self.type2test([]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
184 self.assertEqual(u[3:2:-2], self.type2test([3]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
185 self.assertEqual(u[3:1:-2], self.type2test([3]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
186 self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
187 self.assertEqual(u[::-100], self.type2test([4]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
188 self.assertEqual(u[100:-100:], self.type2test([]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
189 self.assertEqual(u[-100:100:], u)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
190 self.assertEqual(u[100:-100:-1], u[::-1])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
191 self.assertEqual(u[-100:100:-1], self.type2test([]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
192 self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
193
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
194 # Test extreme cases with long ints
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
195 a = self.type2test([0,1,2,3,4])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
196 self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
197 self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
198
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
199 self.assertRaises(TypeError, u.__getslice__)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
200
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
201 def test_contains(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
202 u = self.type2test([0, 1, 2])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
203 for i in u:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
204 self.assertIn(i, u)
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
205 for i in min(u)-1, max(u)+1:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
206 self.assertNotIn(i, u)
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
207
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
208 self.assertRaises(TypeError, u.__contains__)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
209
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
210 def test_contains_fake(self):
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
211 class AllEq:
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
212 # Sequences must use rich comparison against each item
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
213 # (unless "is" is true, or an earlier item answered)
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
214 # So instances of AllEq must be found in all non-empty sequences.
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
215 def __eq__(self, other):
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
216 return True
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 45468
diff changeset
217 __hash__ = None # Can't meet hash invariant requirements
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
218 self.assertNotIn(AllEq(), self.type2test([]))
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
219 self.assertIn(AllEq(), self.type2test([1]))
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
220
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
221 def test_contains_order(self):
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
222 # Sequences must test in-order. If a rich comparison has side
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
223 # effects, these will be visible to tests against later members.
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
224 # In this test, the "side effect" is a short-circuiting raise.
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
225 class DoNotTestEq(Exception):
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
226 pass
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
227 class StopCompares:
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
228 def __eq__(self, other):
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
229 raise DoNotTestEq
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35420
diff changeset
230
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
231 checkfirst = self.type2test([1, StopCompares()])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
232 self.assertIn(1, checkfirst)
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
233 checklast = self.type2test([StopCompares(), 1])
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
234 self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 34802
diff changeset
235
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
236 def test_len(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
237 self.assertEqual(len(self.type2test()), 0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
238 self.assertEqual(len(self.type2test([])), 0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
239 self.assertEqual(len(self.type2test([0])), 1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
240 self.assertEqual(len(self.type2test([0, 1, 2])), 3)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
241
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 def test_minmax(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
243 u = self.type2test([0, 1, 2])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
244 self.assertEqual(min(u), 0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
245 self.assertEqual(max(u), 2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
246
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
247 def test_addmul(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
248 u1 = self.type2test([0])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
249 u2 = self.type2test([0, 1])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
250 self.assertEqual(u1, u1 + self.type2test())
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
251 self.assertEqual(u1, self.type2test() + u1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
252 self.assertEqual(u1 + self.type2test([1]), u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
253 self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
254 self.assertEqual(self.type2test(), u2*0)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
255 self.assertEqual(self.type2test(), 0*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
256 self.assertEqual(self.type2test(), u2*0L)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
257 self.assertEqual(self.type2test(), 0L*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
258 self.assertEqual(u2, u2*1)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
259 self.assertEqual(u2, 1*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
260 self.assertEqual(u2, u2*1L)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
261 self.assertEqual(u2, 1L*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
262 self.assertEqual(u2+u2, u2*2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
263 self.assertEqual(u2+u2, 2*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
264 self.assertEqual(u2+u2, u2*2L)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
265 self.assertEqual(u2+u2, 2L*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
266 self.assertEqual(u2+u2+u2, u2*3)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
267 self.assertEqual(u2+u2+u2, 3*u2)
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
268
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
269 class subclass(self.type2test):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
270 pass
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
271 u3 = subclass([0, 1])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
272 self.assertEqual(u3, u3*1)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
273 self.assertIsNot(u3, u3*1)
31159
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
274
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
275 def test_iadd(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
276 u = self.type2test([0, 1])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
277 u += self.type2test()
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
278 self.assertEqual(u, self.type2test([0, 1]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
279 u += self.type2test([2, 3])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
280 self.assertEqual(u, self.type2test([0, 1, 2, 3]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
281 u += self.type2test([4, 5])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
282 self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
283
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
284 u = self.type2test("spam")
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
285 u += self.type2test("eggs")
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
286 self.assertEqual(u, self.type2test("spameggs"))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
287
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
288 def test_imul(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
289 u = self.type2test([0, 1])
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
290 u *= 3
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
291 self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
292
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
293 def test_getitemoverwriteiter(self):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
294 # Verify that __getitem__ overrides are not recognized by __iter__
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
295 class T(self.type2test):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
296 def __getitem__(self, key):
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
297 return str(key) + '!!!'
e1f1fb74451b Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
298 self.assertEqual(iter(T((1,2))).next(), 1)
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
299
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
300 def test_repeat(self):
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
301 for m in xrange(4):
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
302 s = tuple(range(m))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
303 for n in xrange(-3, 5):
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
304 self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
305 self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
306 self.assertEqual(id(s), id(s*1))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
307
43837
b31f61d7f8b0 Issue 1704621. Fix segfaults in list_repeat() and list_inplace_repeat().
Guido van Rossum <guido@python.org>
parents: 35465
diff changeset
308 def test_bigrepeat(self):
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
309 import sys
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
310 if sys.maxint <= 2147483647:
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
311 x = self.type2test([0])
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
312 x *= 2**16
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
313 self.assertRaises(MemoryError, x.__mul__, 2**16)
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
314 if hasattr(x, '__imul__'):
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 43837
diff changeset
315 self.assertRaises(MemoryError, x.__imul__, 2**16)
43837
b31f61d7f8b0 Issue 1704621. Fix segfaults in list_repeat() and list_inplace_repeat().
Guido van Rossum <guido@python.org>
parents: 35465
diff changeset
316
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
317 def test_subscript(self):
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
318 a = self.type2test([10, 11])
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
319 self.assertEqual(a.__getitem__(0L), 10)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
320 self.assertEqual(a.__getitem__(1L), 11)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
321 self.assertEqual(a.__getitem__(-2L), 10)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
322 self.assertEqual(a.__getitem__(-1L), 11)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
323 self.assertRaises(IndexError, a.__getitem__, -3)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
324 self.assertRaises(IndexError, a.__getitem__, 3)
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
325 self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
326 self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
327 self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
328 self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
329 self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
330 self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 31159
diff changeset
331 self.assertRaises(TypeError, a.__getitem__, 'x')
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
332
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
333 def test_count(self):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
334 a = self.type2test([0, 1, 2])*3
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
335 self.assertEqual(a.count(0), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
336 self.assertEqual(a.count(1), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
337 self.assertEqual(a.count(3), 0)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
338
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
339 self.assertRaises(TypeError, a.count)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
340
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
341 class BadExc(Exception):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
342 pass
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
343
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
344 class BadCmp:
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
345 def __eq__(self, other):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
346 if other == 2:
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
347 raise BadExc()
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
348 return False
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
349
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
350 self.assertRaises(BadExc, a.count, BadCmp())
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
351
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
352 def test_index(self):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
353 u = self.type2test([0, 1])
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
354 self.assertEqual(u.index(0), 0)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
355 self.assertEqual(u.index(1), 1)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
356 self.assertRaises(ValueError, u.index, 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
357
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
358 u = self.type2test([-2, -1, 0, 0, 1, 2])
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
359 self.assertEqual(u.count(0), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
360 self.assertEqual(u.index(0), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
361 self.assertEqual(u.index(0, 2), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
362 self.assertEqual(u.index(-2, -10), 0)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
363 self.assertEqual(u.index(0, 3), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
364 self.assertEqual(u.index(0, 3, 4), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
365 self.assertRaises(ValueError, u.index, 2, 0, -10)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
366
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
367 self.assertRaises(TypeError, u.index)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
368
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
369 class BadExc(Exception):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
370 pass
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
371
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
372 class BadCmp:
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
373 def __eq__(self, other):
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
374 if other == 2:
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
375 raise BadExc()
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
376 return False
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
377
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
378 a = self.type2test([0, 1, 2, 3])
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
379 self.assertRaises(BadExc, a.index, BadCmp())
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
380
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
381 a = self.type2test([-2, -1, 0, 0, 1, 2])
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
382 self.assertEqual(a.index(0), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
383 self.assertEqual(a.index(0, 2), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
384 self.assertEqual(a.index(0, -4), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
385 self.assertEqual(a.index(-2, -10), 0)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
386 self.assertEqual(a.index(0, 3), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
387 self.assertEqual(a.index(0, -3), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
388 self.assertEqual(a.index(0, 3, 4), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
389 self.assertEqual(a.index(0, -3, -2), 3)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
390 self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
391 self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 43837
diff changeset
392 self.assertRaises(ValueError, a.index, 2, 0, -10)