PageRenderTime 29ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Lib/test/test_index.py

http://unladen-swallow.googlecode.com/
Python | 225 lines | 182 code | 41 blank | 2 comment | 1 complexity | 8390393cbc05d3151f388c82f541f535 MD5 | raw file
  1import unittest
  2from test import test_support
  3import operator
  4from sys import maxint
  5maxsize = test_support.MAX_Py_ssize_t
  6minsize = -maxsize-1
  7
  8class oldstyle:
  9    def __index__(self):
 10        return self.ind
 11
 12class newstyle(object):
 13    def __index__(self):
 14        return self.ind
 15
 16class TrapInt(int):
 17    def __index__(self):
 18        return self
 19
 20class TrapLong(long):
 21    def __index__(self):
 22        return self
 23
 24class BaseTestCase(unittest.TestCase):
 25    def setUp(self):
 26        self.o = oldstyle()
 27        self.n = newstyle()
 28
 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)
 34
 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))
 44
 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)
 52
 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))
 59
 60    def test_error(self):
 61        self.o.ind = 'dumb'
 62        self.n.ind = 'bad'
 63        self.failUnlessRaises(TypeError, operator.index, self.o)
 64        self.failUnlessRaises(TypeError, operator.index, self.n)
 65        self.failUnlessRaises(TypeError, slice(self.o).indices, 0)
 66        self.failUnlessRaises(TypeError, slice(self.n).indices, 0)
 67
 68
 69class 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()
 77
 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])
 83
 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])
 91
 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)
 99
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)
109
110    def test_subclasses(self):
111        self.assertEqual(self.seq[TrapInt()], self.seq[0])
112        self.assertEqual(self.seq[TrapLong()], self.seq[0])
113
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.failUnlessRaises(TypeError, indexobj, self.o, self)
119        self.failUnlessRaises(TypeError, indexobj, self.n, self)
120        sliceobj = lambda x, obj: obj.seq[x:]
121        self.failUnlessRaises(TypeError, sliceobj, self.o, self)
122        self.failUnlessRaises(TypeError, sliceobj, self.n, self)
123
124
125class ListTestCase(SeqTestCase):
126    seq = [0,10,20,30,40,50]
127
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'))
137
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])
143
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)
152
153        lst = [5, 6, 7, 8, 9, 11]
154        l2 = lst.__imul__(self.n)
155        self.assert_(l2 is lst)
156        self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
157
158
159class TupleTestCase(SeqTestCase):
160    seq = (0,10,20,30,40,50)
161
162class StringTestCase(SeqTestCase):
163    seq = "this is a test"
164
165class UnicodeTestCase(SeqTestCase):
166    seq = u"this is a test"
167
168
169class XRangeTestCase(unittest.TestCase):
170
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)
176
177class OverflowTestCase(unittest.TestCase):
178
179    def setUp(self):
180        self.pos = 2**100
181        self.neg = -self.pos
182
183    def test_large_longs(self):
184        self.assertEqual(self.pos.__index__(), self.pos)
185        self.assertEqual(self.neg.__index__(), self.neg)
186
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))
200
201    def test_getitem(self):
202        self._getitem_helper(object)
203
204    def test_getitem_classic(self):
205        class Empty: pass
206        self._getitem_helper(Empty)
207
208    def test_sequence_repeat(self):
209        self.failUnlessRaises(OverflowError, lambda: "a" * self.pos)
210        self.failUnlessRaises(OverflowError, lambda: "a" * self.neg)
211
212
213def test_main():
214    test_support.run_unittest(
215        BaseTestCase,
216        ListTestCase,
217        TupleTestCase,
218        StringTestCase,
219        UnicodeTestCase,
220        XRangeTestCase,
221        OverflowTestCase,
222    )
223
224if __name__ == "__main__":
225    test_main()