blob: 0045469f012b07296ce96b234a9fdda2a3dcdd03 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001import unittest
2from test import test_support
3import operator
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004from sys import maxint
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005
6class oldstyle:
7 def __index__(self):
8 return self.ind
9
10class newstyle(object):
11 def __index__(self):
12 return self.ind
13
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000014class TrapInt(int):
15 def __index__(self):
16 return self
17
Guido van Rossume2a383d2007-01-15 16:59:06 +000018class TrapLong(int):
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000019 def __index__(self):
20 return self
21
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000022class BaseTestCase(unittest.TestCase):
23 def setUp(self):
24 self.o = oldstyle()
25 self.n = newstyle()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000026
27 def test_basic(self):
28 self.o.ind = -2
29 self.n.ind = 2
30 self.assertEqual(operator.index(self.o), -2)
31 self.assertEqual(operator.index(self.n), 2)
32
33 def test_slice(self):
34 self.o.ind = 1
35 self.n.ind = 2
36 slc = slice(self.o, self.o, self.o)
37 check_slc = slice(1, 1, 1)
38 self.assertEqual(slc.indices(self.o), check_slc.indices(1))
39 slc = slice(self.n, self.n, self.n)
40 check_slc = slice(2, 2, 2)
41 self.assertEqual(slc.indices(self.n), check_slc.indices(2))
42
43 def test_wrappers(self):
44 self.o.ind = 4
45 self.n.ind = 5
46 self.assertEqual(6 .__index__(), 6)
Guido van Rossume2a383d2007-01-15 16:59:06 +000047 self.assertEqual(-7 .__index__(), -7)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000048 self.assertEqual(self.o.__index__(), 4)
49 self.assertEqual(self.n.__index__(), 5)
50
51 def test_subclasses(self):
52 r = range(10)
53 self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
54 self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
55 self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
56 self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
57
58 def test_error(self):
59 self.o.ind = 'dumb'
60 self.n.ind = 'bad'
61 self.failUnlessRaises(TypeError, operator.index, self.o)
62 self.failUnlessRaises(TypeError, operator.index, self.n)
63 self.failUnlessRaises(TypeError, slice(self.o).indices, 0)
64 self.failUnlessRaises(TypeError, slice(self.n).indices, 0)
65
66
67class SeqTestCase(unittest.TestCase):
68 # This test case isn't run directly. It just defines common tests
69 # to the different sequence types below
70 def setUp(self):
71 self.o = oldstyle()
72 self.n = newstyle()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073 self.o2 = oldstyle()
74 self.n2 = newstyle()
75
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000076 def test_index(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 self.o.ind = -2
78 self.n.ind = 2
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000079 self.assertEqual(self.seq[self.n], self.seq[2])
80 self.assertEqual(self.seq[self.o], self.seq[-2])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081
82 def test_slice(self):
83 self.o.ind = 1
84 self.o2.ind = 3
85 self.n.ind = 2
86 self.n2.ind = 4
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000087 self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
88 self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000089
90 def test_repeat(self):
91 self.o.ind = 3
92 self.n.ind = 2
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000093 self.assertEqual(self.seq * self.o, self.seq * 3)
94 self.assertEqual(self.seq * self.n, self.seq * 2)
95 self.assertEqual(self.o * self.seq, self.seq * 3)
96 self.assertEqual(self.n * self.seq, self.seq * 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097
98 def test_wrappers(self):
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000099 self.o.ind = 4
100 self.n.ind = 5
101 self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
102 self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
103 self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
104 self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
105 self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
106 self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000108 def test_subclasses(self):
109 self.assertEqual(self.seq[TrapInt()], self.seq[0])
110 self.assertEqual(self.seq[TrapLong()], self.seq[0])
111
112 def test_error(self):
113 self.o.ind = 'dumb'
114 self.n.ind = 'bad'
115 indexobj = lambda x, obj: obj.seq[x]
116 self.failUnlessRaises(TypeError, indexobj, self.o, self)
117 self.failUnlessRaises(TypeError, indexobj, self.n, self)
118 sliceobj = lambda x, obj: obj.seq[x:]
119 self.failUnlessRaises(TypeError, sliceobj, self.o, self)
120 self.failUnlessRaises(TypeError, sliceobj, self.n, self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121
122
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000123class ListTestCase(SeqTestCase):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 seq = [0,10,20,30,40,50]
125
126 def test_setdelitem(self):
127 self.o.ind = -2
128 self.n.ind = 2
129 lst = list('ab!cdefghi!j')
130 del lst[self.o]
131 del lst[self.n]
132 lst[self.o] = 'X'
133 lst[self.n] = 'Y'
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000134 self.assertEqual(lst, list('abYdefghXj'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135
136 lst = [5, 6, 7, 8, 9, 10, 11]
137 lst.__setitem__(self.n, "here")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000138 self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 lst.__delitem__(self.n)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000140 self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141
142 def test_inplace_repeat(self):
143 self.o.ind = 2
144 self.n.ind = 3
145 lst = [6, 4]
146 lst *= self.o
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000147 self.assertEqual(lst, [6, 4, 6, 4])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 lst *= self.n
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000149 self.assertEqual(lst, [6, 4, 6, 4] * 3)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150
151 lst = [5, 6, 7, 8, 9, 11]
152 l2 = lst.__imul__(self.n)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000153 self.assert_(l2 is lst)
154 self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155
156
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000157class TupleTestCase(SeqTestCase):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 seq = (0,10,20,30,40,50)
159
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000160class StringTestCase(SeqTestCase):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 seq = "this is a test"
162
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000163class UnicodeTestCase(SeqTestCase):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000164 seq = u"this is a test"
165
166
167class XRangeTestCase(unittest.TestCase):
168
169 def test_xrange(self):
170 n = newstyle()
171 n.ind = 5
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000172 self.assertEqual(xrange(1, 20)[n], 6)
173 self.assertEqual(xrange(1, 20).__getitem__(n), 6)
174
175class OverflowTestCase(unittest.TestCase):
176
177 def setUp(self):
178 self.pos = 2**100
179 self.neg = -self.pos
180
181 def test_large_longs(self):
182 self.assertEqual(self.pos.__index__(), self.pos)
183 self.assertEqual(self.neg.__index__(), self.neg)
184
185 def _getitem_helper(self, base):
186 class GetItem(base):
187 def __len__(self):
188 return maxint
189 def __getitem__(self, key):
190 return key
191 def __getslice__(self, i, j):
192 return i, j
193 x = GetItem()
194 self.assertEqual(x[self.pos], self.pos)
195 self.assertEqual(x[self.neg], self.neg)
196 self.assertEqual(x[self.neg:self.pos], (-1, maxint))
197 self.assertEqual(x[self.neg:self.pos:1].indices(maxint), (0, maxint, 1))
198
199 def test_getitem(self):
200 self._getitem_helper(object)
201
202 def test_getitem_classic(self):
203 class Empty: pass
204 self._getitem_helper(Empty)
205
206 def test_sequence_repeat(self):
207 self.failUnlessRaises(OverflowError, lambda: "a" * self.pos)
208 self.failUnlessRaises(OverflowError, lambda: "a" * self.neg)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209
210
211def test_main():
212 test_support.run_unittest(
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000213 BaseTestCase,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214 ListTestCase,
215 TupleTestCase,
216 StringTestCase,
217 UnicodeTestCase,
218 XRangeTestCase,
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000219 OverflowTestCase,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220 )
221
222if __name__ == "__main__":
223 test_main()