PageRenderTime 131ms CodeModel.GetById 101ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/geos/tests/test_mutable_list.py

https://code.google.com/p/mango-py/
Python | 399 lines | 320 code | 67 blank | 12 comment | 68 complexity | 33656bd6803394c2973ef5b6c89dcacb MD5 | raw file
  1# Copyright (c) 2008-2009 Aryeh Leib Taurog, http://www.aryehleib.com
  2# All rights reserved.
  3#
  4# Modified from original contribution by Aryeh Leib Taurog, which was
  5# released under the New BSD license.
  6from django.contrib.gis.geos.mutable_list import ListMixin
  7from django.utils import unittest
  8
  9
 10class UserListA(ListMixin):
 11    _mytype = tuple
 12    def __init__(self, i_list, *args, **kwargs):
 13        self._list = self._mytype(i_list)
 14        super(UserListA, self).__init__(*args, **kwargs)
 15
 16    def __len__(self):  return len(self._list)
 17
 18    def __str__(self):  return str(self._list)
 19
 20    def __repr__(self): return repr(self._list)
 21
 22    def _set_list(self, length, items):
 23        # this would work:
 24        # self._list = self._mytype(items)
 25        # but then we wouldn't be testing length parameter
 26        itemList = ['x'] * length
 27        for i, v in enumerate(items):
 28            itemList[i] = v
 29
 30        self._list = self._mytype(itemList)
 31
 32    def _get_single_external(self, index):
 33        return self._list[index]
 34
 35class UserListB(UserListA):
 36    _mytype = list
 37
 38    def _set_single(self, index, value):
 39        self._list[index] = value
 40
 41def nextRange(length):
 42    nextRange.start += 100
 43    return range(nextRange.start, nextRange.start + length)
 44
 45nextRange.start = 0
 46
 47class ListMixinTest(unittest.TestCase):
 48    """
 49    Tests base class ListMixin by comparing a list clone which is
 50    a ListMixin subclass with a real Python list.
 51    """
 52    limit = 3
 53    listType = UserListA
 54
 55    def lists_of_len(self, length=None):
 56        if length is None: length = self.limit
 57        pl = range(length)
 58        return pl, self.listType(pl)
 59
 60    def limits_plus(self, b):
 61        return range(-self.limit - b, self.limit + b)
 62
 63    def step_range(self):
 64        return range(-1 - self.limit, 0) + range(1, 1 + self.limit)
 65
 66    def test01_getslice(self):
 67        'Slice retrieval'
 68        pl, ul = self.lists_of_len()
 69        for i in self.limits_plus(1):
 70            self.assertEqual(pl[i:], ul[i:], 'slice [%d:]' % (i))
 71            self.assertEqual(pl[:i], ul[:i], 'slice [:%d]' % (i))
 72
 73            for j in self.limits_plus(1):
 74                self.assertEqual(pl[i:j], ul[i:j], 'slice [%d:%d]' % (i,j))
 75                for k in self.step_range():
 76                    self.assertEqual(pl[i:j:k], ul[i:j:k], 'slice [%d:%d:%d]' % (i,j,k))
 77
 78            for k in self.step_range():
 79                self.assertEqual(pl[i::k], ul[i::k], 'slice [%d::%d]' % (i,k))
 80                self.assertEqual(pl[:i:k], ul[:i:k], 'slice [:%d:%d]' % (i,k))
 81
 82        for k in self.step_range():
 83            self.assertEqual(pl[::k], ul[::k], 'slice [::%d]' % (k))
 84
 85    def test02_setslice(self):
 86        'Slice assignment'
 87        def setfcn(x,i,j,k,L): x[i:j:k] = range(L)
 88        pl, ul = self.lists_of_len()
 89        for slen in range(self.limit + 1):
 90            ssl = nextRange(slen)
 91            ul[:] = ssl
 92            pl[:] = ssl
 93            self.assertEqual(pl, ul[:], 'set slice [:]')
 94
 95            for i in self.limits_plus(1):
 96                ssl = nextRange(slen)
 97                ul[i:] = ssl
 98                pl[i:] = ssl
 99                self.assertEqual(pl, ul[:], 'set slice [%d:]' % (i))
100
101                ssl = nextRange(slen)
102                ul[:i] = ssl
103                pl[:i] = ssl
104                self.assertEqual(pl, ul[:], 'set slice [:%d]' % (i))
105
106                for j in self.limits_plus(1):
107                    ssl = nextRange(slen)
108                    ul[i:j] = ssl
109                    pl[i:j] = ssl
110                    self.assertEqual(pl, ul[:], 'set slice [%d:%d]' % (i, j))
111
112                    for k in self.step_range():
113                        ssl = nextRange( len(ul[i:j:k]) )
114                        ul[i:j:k] = ssl
115                        pl[i:j:k] = ssl
116                        self.assertEqual(pl, ul[:], 'set slice [%d:%d:%d]' % (i, j, k))
117
118                        sliceLen = len(ul[i:j:k])
119                        self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen + 1)
120                        if sliceLen > 2:
121                            self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen - 1)
122
123                for k in self.step_range():
124                    ssl = nextRange( len(ul[i::k]) )
125                    ul[i::k] = ssl
126                    pl[i::k] = ssl
127                    self.assertEqual(pl, ul[:], 'set slice [%d::%d]' % (i, k))
128
129                    ssl = nextRange( len(ul[:i:k]) )
130                    ul[:i:k] = ssl
131                    pl[:i:k] = ssl
132                    self.assertEqual(pl, ul[:], 'set slice [:%d:%d]' % (i, k))
133
134            for k in self.step_range():
135                ssl = nextRange(len(ul[::k]))
136                ul[::k] = ssl
137                pl[::k] = ssl
138                self.assertEqual(pl, ul[:], 'set slice [::%d]' % (k))
139
140
141    def test03_delslice(self):
142        'Delete slice'
143        for Len in range(self.limit):
144            pl, ul = self.lists_of_len(Len)
145            del pl[:]
146            del ul[:]
147            self.assertEqual(pl[:], ul[:], 'del slice [:]')
148            for i in range(-Len - 1, Len + 1):
149                pl, ul = self.lists_of_len(Len)
150                del pl[i:]
151                del ul[i:]
152                self.assertEqual(pl[:], ul[:], 'del slice [%d:]' % (i))
153                pl, ul = self.lists_of_len(Len)
154                del pl[:i]
155                del ul[:i]
156                self.assertEqual(pl[:], ul[:], 'del slice [:%d]' % (i))
157                for j in range(-Len - 1, Len + 1):
158                    pl, ul = self.lists_of_len(Len)
159                    del pl[i:j]
160                    del ul[i:j]
161                    self.assertEqual(pl[:], ul[:], 'del slice [%d:%d]' % (i,j))
162                    for k in range(-Len - 1,0) + range(1,Len):
163                        pl, ul = self.lists_of_len(Len)
164                        del pl[i:j:k]
165                        del ul[i:j:k]
166                        self.assertEqual(pl[:], ul[:], 'del slice [%d:%d:%d]' % (i,j,k))
167
168                for k in range(-Len - 1,0) + range(1,Len):
169                    pl, ul = self.lists_of_len(Len)
170                    del pl[:i:k]
171                    del ul[:i:k]
172                    self.assertEqual(pl[:], ul[:], 'del slice [:%d:%d]' % (i,k))
173
174                    pl, ul = self.lists_of_len(Len)
175                    del pl[i::k]
176                    del ul[i::k]
177                    self.assertEqual(pl[:], ul[:], 'del slice [%d::%d]' % (i,k))
178
179            for k in range(-Len - 1,0) + range(1,Len):
180                pl, ul = self.lists_of_len(Len)
181                del pl[::k]
182                del ul[::k]
183                self.assertEqual(pl[:], ul[:], 'del slice [::%d]' % (k))
184
185    def test04_get_set_del_single(self):
186        'Get/set/delete single item'
187        pl, ul = self.lists_of_len()
188        for i in self.limits_plus(0):
189            self.assertEqual(pl[i], ul[i], 'get single item [%d]' % i)
190
191        for i in self.limits_plus(0):
192            pl, ul = self.lists_of_len()
193            pl[i] = 100
194            ul[i] = 100
195            self.assertEqual(pl[:], ul[:], 'set single item [%d]' % i)
196
197        for i in self.limits_plus(0):
198            pl, ul = self.lists_of_len()
199            del pl[i]
200            del ul[i]
201            self.assertEqual(pl[:], ul[:], 'del single item [%d]' % i)
202
203    def test05_out_of_range_exceptions(self):
204        'Out of range exceptions'
205        def setfcn(x, i): x[i] = 20
206        def getfcn(x, i): return x[i]
207        def delfcn(x, i): del x[i]
208        pl, ul = self.lists_of_len()
209        for i in (-1 - self.limit, self.limit):
210            self.assertRaises(IndexError, setfcn, ul, i) # 'set index %d' % i)
211            self.assertRaises(IndexError, getfcn, ul, i) # 'get index %d' % i)
212            self.assertRaises(IndexError, delfcn, ul, i) # 'del index %d' % i)
213
214    def test06_list_methods(self):
215        'List methods'
216        pl, ul = self.lists_of_len()
217        pl.append(40)
218        ul.append(40)
219        self.assertEqual(pl[:], ul[:], 'append')
220
221        pl.extend(range(50,55))
222        ul.extend(range(50,55))
223        self.assertEqual(pl[:], ul[:], 'extend')
224
225        pl.reverse()
226        ul.reverse()
227        self.assertEqual(pl[:], ul[:], 'reverse')
228
229        for i in self.limits_plus(1):
230            pl, ul = self.lists_of_len()
231            pl.insert(i,50)
232            ul.insert(i,50)
233            self.assertEqual(pl[:], ul[:], 'insert at %d' % i)
234
235        for i in self.limits_plus(0):
236            pl, ul = self.lists_of_len()
237            self.assertEqual(pl.pop(i), ul.pop(i), 'popped value at %d' % i)
238            self.assertEqual(pl[:], ul[:], 'after pop at %d' % i)
239
240        pl, ul = self.lists_of_len()
241        self.assertEqual(pl.pop(), ul.pop(i), 'popped value')
242        self.assertEqual(pl[:], ul[:], 'after pop')
243
244        pl, ul = self.lists_of_len()
245        def popfcn(x, i): x.pop(i)
246        self.assertRaises(IndexError, popfcn, ul, self.limit)
247        self.assertRaises(IndexError, popfcn, ul, -1 - self.limit)
248
249        pl, ul = self.lists_of_len()
250        for val in range(self.limit):
251            self.assertEqual(pl.index(val), ul.index(val), 'index of %d' % val)
252
253        for val in self.limits_plus(2):
254            self.assertEqual(pl.count(val), ul.count(val), 'count %d' % val)
255
256        for val in range(self.limit):
257            pl, ul = self.lists_of_len()
258            pl.remove(val)
259            ul.remove(val)
260            self.assertEqual(pl[:], ul[:], 'after remove val %d' % val)
261
262        def indexfcn(x, v): return x.index(v)
263        def removefcn(x, v): return x.remove(v)
264        self.assertRaises(ValueError, indexfcn, ul, 40)
265        self.assertRaises(ValueError, removefcn, ul, 40)
266
267    def test07_allowed_types(self):
268        'Type-restricted list'
269        pl, ul = self.lists_of_len()
270        ul._allowed = (int, long)
271        ul[1] = 50
272        ul[:2] = [60, 70, 80]
273        def setfcn(x, i, v): x[i] = v
274        self.assertRaises(TypeError, setfcn, ul, 2, 'hello')
275        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), ('hello','goodbye'))
276
277    def test08_min_length(self):
278        'Length limits'
279        pl, ul = self.lists_of_len()
280        ul._minlength = 1
281        def delfcn(x,i): del x[:i]
282        def setfcn(x,i): x[:i] = []
283        for i in range(self.limit - ul._minlength + 1, self.limit + 1):
284            self.assertRaises(ValueError, delfcn, ul, i)
285            self.assertRaises(ValueError, setfcn, ul, i)
286        del ul[:ul._minlength]
287
288        ul._maxlength = 4
289        for i in range(0, ul._maxlength - len(ul)):
290            ul.append(i)
291        self.assertRaises(ValueError, ul.append, 10)
292
293    def test09_iterable_check(self):
294        'Error on assigning non-iterable to slice'
295        pl, ul = self.lists_of_len(self.limit + 1)
296        def setfcn(x, i, v): x[i] = v
297        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), 2)
298
299    def test10_checkindex(self):
300        'Index check'
301        pl, ul = self.lists_of_len()
302        for i in self.limits_plus(0):
303            if i < 0:
304                self.assertEqual(ul._checkindex(i), i + self.limit, '_checkindex(neg index)')
305            else:
306                self.assertEqual(ul._checkindex(i), i, '_checkindex(pos index)')
307
308        for i in (-self.limit - 1, self.limit):
309            self.assertRaises(IndexError, ul._checkindex, i)
310
311        ul._IndexError = TypeError
312        self.assertRaises(TypeError, ul._checkindex, -self.limit - 1)
313
314    def test_11_sorting(self):
315        'Sorting'
316        pl, ul = self.lists_of_len()
317        pl.insert(0, pl.pop())
318        ul.insert(0, ul.pop())
319        pl.sort()
320        ul.sort()
321        self.assertEqual(pl[:], ul[:], 'sort')
322        mid = pl[len(pl) / 2]
323        pl.sort(key=lambda x: (mid-x)**2)
324        ul.sort(key=lambda x: (mid-x)**2)
325        self.assertEqual(pl[:], ul[:], 'sort w/ key')
326
327        pl.insert(0, pl.pop())
328        ul.insert(0, ul.pop())
329        pl.sort(reverse=True)
330        ul.sort(reverse=True)
331        self.assertEqual(pl[:], ul[:], 'sort w/ reverse')
332        mid = pl[len(pl) / 2]
333        pl.sort(key=lambda x: (mid-x)**2)
334        ul.sort(key=lambda x: (mid-x)**2)
335        self.assertEqual(pl[:], ul[:], 'sort w/ key')
336
337    def test_12_arithmetic(self):
338        'Arithmetic'
339        pl, ul = self.lists_of_len()
340        al = range(10,14)
341        self.assertEqual(list(pl + al), list(ul + al), 'add')
342        self.assertEqual(type(ul), type(ul + al), 'type of add result')
343        self.assertEqual(list(al + pl), list(al + ul), 'radd')
344        self.assertEqual(type(al), type(al + ul), 'type of radd result')
345        objid = id(ul)
346        pl += al
347        ul += al
348        self.assertEqual(pl[:], ul[:], 'in-place add')
349        self.assertEqual(objid, id(ul), 'in-place add id')
350
351        for n in (-1,0,1,3):
352            pl, ul = self.lists_of_len()
353            self.assertEqual(list(pl * n), list(ul * n), 'mul by %d' % n)
354            self.assertEqual(type(ul), type(ul * n), 'type of mul by %d result' % n)
355            self.assertEqual(list(n * pl), list(n * ul), 'rmul by %d' % n)
356            self.assertEqual(type(ul), type(n * ul), 'type of rmul by %d result' % n)
357            objid = id(ul)
358            pl *= n
359            ul *= n
360            self.assertEqual(pl[:], ul[:], 'in-place mul by %d' % n)
361            self.assertEqual(objid, id(ul), 'in-place mul by %d id' % n)
362
363        pl, ul = self.lists_of_len()
364        self.assertEqual(pl, ul, 'cmp for equal')
365        self.assertTrue(pl >= ul, 'cmp for gte self')
366        self.assertTrue(pl <= ul, 'cmp for lte self')
367        self.assertTrue(ul >= pl, 'cmp for self gte')
368        self.assertTrue(ul <= pl, 'cmp for self lte')
369
370        self.assertTrue(pl + [5] > ul, 'cmp')
371        self.assertTrue(pl + [5] >= ul, 'cmp')
372        self.assertTrue(pl < ul + [2], 'cmp')
373        self.assertTrue(pl <= ul + [2], 'cmp')
374        self.assertTrue(ul + [5] > pl, 'cmp')
375        self.assertTrue(ul + [5] >= pl, 'cmp')
376        self.assertTrue(ul < pl + [2], 'cmp')
377        self.assertTrue(ul <= pl + [2], 'cmp')
378
379        pl[1] = 20
380        self.assertTrue(pl > ul, 'cmp for gt self')
381        self.assertTrue(ul < pl, 'cmp for self lt')
382        pl[1] = -20
383        self.assertTrue(pl < ul, 'cmp for lt self')
384        self.assertTrue(pl < ul, 'cmp for lt self')
385
386class ListMixinTestSingle(ListMixinTest):
387    listType = UserListB
388
389def suite():
390    s = unittest.TestSuite()
391    s.addTest(unittest.makeSuite(ListMixinTest))
392    s.addTest(unittest.makeSuite(ListMixinTestSingle))
393    return s
394
395def run(verbosity=2):
396    unittest.TextTestRunner(verbosity=verbosity).run(suite())
397
398if __name__ == '__main__':
399    run()