Issue #7051: Clarify behaviour of 'g' and 'G'-style formatting.
[python.git] / Lib / test / test_index.py
blob2e997c7326a86ad951cef2b28417e7c1b75108f7
1 import unittest
2 from test import test_support
3 import operator
4 from sys import maxint
5 maxsize = test_support.MAX_Py_ssize_t
6 minsize = -maxsize-1
8 class oldstyle:
9 def __index__(self):
10 return self.ind
12 class newstyle(object):
13 def __index__(self):
14 return self.ind
16 class TrapInt(int):
17 def __index__(self):
18 return self
20 class TrapLong(long):
21 def __index__(self):
22 return self
24 class BaseTestCase(unittest.TestCase):
25 def setUp(self):
26 self.o = oldstyle()
27 self.n = newstyle()
29 def test_basic(self):
30 self.o.ind = -2
31 self.n.ind = 2
32 self.assertEqual(operator.index(self.o), -2)
33 self.assertEqual(operator.index(self.n), 2)
35 def test_slice(self):
36 self.o.ind = 1
37 self.n.ind = 2
38 slc = slice(self.o, self.o, self.o)
39 check_slc = slice(1, 1, 1)
40 self.assertEqual(slc.indices(self.o), check_slc.indices(1))
41 slc = slice(self.n, self.n, self.n)
42 check_slc = slice(2, 2, 2)
43 self.assertEqual(slc.indices(self.n), check_slc.indices(2))
45 def test_wrappers(self):
46 self.o.ind = 4
47 self.n.ind = 5
48 self.assertEqual(6 .__index__(), 6)
49 self.assertEqual(-7L.__index__(), -7)
50 self.assertEqual(self.o.__index__(), 4)
51 self.assertEqual(self.n.__index__(), 5)
53 def test_subclasses(self):
54 r = range(10)
55 self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
56 self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
57 self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
58 self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
60 def test_error(self):
61 self.o.ind = 'dumb'
62 self.n.ind = 'bad'
63 self.assertRaises(TypeError, operator.index, self.o)
64 self.assertRaises(TypeError, operator.index, self.n)
65 self.assertRaises(TypeError, slice(self.o).indices, 0)
66 self.assertRaises(TypeError, slice(self.n).indices, 0)
69 class SeqTestCase(unittest.TestCase):
70 # This test case isn't run directly. It just defines common tests
71 # to the different sequence types below
72 def setUp(self):
73 self.o = oldstyle()
74 self.n = newstyle()
75 self.o2 = oldstyle()
76 self.n2 = newstyle()
78 def test_index(self):
79 self.o.ind = -2
80 self.n.ind = 2
81 self.assertEqual(self.seq[self.n], self.seq[2])
82 self.assertEqual(self.seq[self.o], self.seq[-2])
84 def test_slice(self):
85 self.o.ind = 1
86 self.o2.ind = 3
87 self.n.ind = 2
88 self.n2.ind = 4
89 self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
90 self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
92 def test_repeat(self):
93 self.o.ind = 3
94 self.n.ind = 2
95 self.assertEqual(self.seq * self.o, self.seq * 3)
96 self.assertEqual(self.seq * self.n, self.seq * 2)
97 self.assertEqual(self.o * self.seq, self.seq * 3)
98 self.assertEqual(self.n * self.seq, self.seq * 2)
100 def test_wrappers(self):
101 self.o.ind = 4
102 self.n.ind = 5
103 self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
104 self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
105 self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
106 self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
107 self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
108 self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
110 def test_subclasses(self):
111 self.assertEqual(self.seq[TrapInt()], self.seq[0])
112 self.assertEqual(self.seq[TrapLong()], self.seq[0])
114 def test_error(self):
115 self.o.ind = 'dumb'
116 self.n.ind = 'bad'
117 indexobj = lambda x, obj: obj.seq[x]
118 self.assertRaises(TypeError, indexobj, self.o, self)
119 self.assertRaises(TypeError, indexobj, self.n, self)
120 sliceobj = lambda x, obj: obj.seq[x:]
121 self.assertRaises(TypeError, sliceobj, self.o, self)
122 self.assertRaises(TypeError, sliceobj, self.n, self)
125 class ListTestCase(SeqTestCase):
126 seq = [0,10,20,30,40,50]
128 def test_setdelitem(self):
129 self.o.ind = -2
130 self.n.ind = 2
131 lst = list('ab!cdefghi!j')
132 del lst[self.o]
133 del lst[self.n]
134 lst[self.o] = 'X'
135 lst[self.n] = 'Y'
136 self.assertEqual(lst, list('abYdefghXj'))
138 lst = [5, 6, 7, 8, 9, 10, 11]
139 lst.__setitem__(self.n, "here")
140 self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
141 lst.__delitem__(self.n)
142 self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
144 def test_inplace_repeat(self):
145 self.o.ind = 2
146 self.n.ind = 3
147 lst = [6, 4]
148 lst *= self.o
149 self.assertEqual(lst, [6, 4, 6, 4])
150 lst *= self.n
151 self.assertEqual(lst, [6, 4, 6, 4] * 3)
153 lst = [5, 6, 7, 8, 9, 11]
154 l2 = lst.__imul__(self.n)
155 self.assertTrue(l2 is lst)
156 self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
159 class TupleTestCase(SeqTestCase):
160 seq = (0,10,20,30,40,50)
162 class StringTestCase(SeqTestCase):
163 seq = "this is a test"
165 class UnicodeTestCase(SeqTestCase):
166 seq = u"this is a test"
169 class XRangeTestCase(unittest.TestCase):
171 def test_xrange(self):
172 n = newstyle()
173 n.ind = 5
174 self.assertEqual(xrange(1, 20)[n], 6)
175 self.assertEqual(xrange(1, 20).__getitem__(n), 6)
177 class OverflowTestCase(unittest.TestCase):
179 def setUp(self):
180 self.pos = 2**100
181 self.neg = -self.pos
183 def test_large_longs(self):
184 self.assertEqual(self.pos.__index__(), self.pos)
185 self.assertEqual(self.neg.__index__(), self.neg)
187 def _getitem_helper(self, base):
188 class GetItem(base):
189 def __len__(self):
190 return maxint #cannot return long here
191 def __getitem__(self, key):
192 return key
193 def __getslice__(self, i, j):
194 return i, j
195 x = GetItem()
196 self.assertEqual(x[self.pos], self.pos)
197 self.assertEqual(x[self.neg], self.neg)
198 self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
199 self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1))
201 def test_getitem(self):
202 self._getitem_helper(object)
204 def test_getitem_classic(self):
205 class Empty: pass
206 self._getitem_helper(Empty)
208 def test_sequence_repeat(self):
209 self.assertRaises(OverflowError, lambda: "a" * self.pos)
210 self.assertRaises(OverflowError, lambda: "a" * self.neg)
213 def test_main():
214 test_support.run_unittest(
215 BaseTestCase,
216 ListTestCase,
217 TupleTestCase,
218 StringTestCase,
219 UnicodeTestCase,
220 XRangeTestCase,
221 OverflowTestCase,
224 if __name__ == "__main__":
225 test_main()