Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10620 -- removed carriage returns in a GEOS tests module; fixe…

…d other errata such as unnecessary `pymutable_geometries` module and not having `GEOSGeometry` call `__init__`.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10174 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0b9a855bed58825624b9a517efd94aa0f5af347d 1 parent 6fa30fa
Justin Bronn authored March 26, 2009
7  django/contrib/gis/geos/collections.py
@@ -13,7 +13,6 @@
13 13
 
14 14
 class GeometryCollection(GEOSGeometry):
15 15
     _typeid = 7
16  
-    _minlength = 0
17 16
 
18 17
     def __init__(self, *args, **kwargs):
19 18
         "Initializes a Geometry Collection from a sequence of Geometry objects."
@@ -77,12 +76,6 @@ def _set_collection(self, length, items):
77 76
         if srid: self.srid = srid
78 77
         capi.destroy_geom(prev_ptr)
79 78
 
80  
-    # Because GeometryCollections need to be rebuilt upon the changing of a
81  
-    # component geometry, these routines are set to their counterparts that
82  
-    # rebuild the entire geometry.
83  
-    _set_single = GEOSGeometry._set_single_rebuild
84  
-    _assign_extended_slice = GEOSGeometry._assign_extended_slice_rebuild
85  
-
86 79
     @property
87 80
     def kml(self):
88 81
         "Returns the KML for this Geometry Collection."
1  django/contrib/gis/geos/geometry.py
@@ -91,6 +91,7 @@ def __init__(self, geo_input, srid=None):
91 91
 
92 92
         # Post-initialization setup.
93 93
         self._post_init(srid)
  94
+        super(GEOSGeometry, self).__init__()
94 95
 
95 96
     def _post_init(self, srid):
96 97
         "Helper routine for performing post-initialization setup."
26  django/contrib/gis/geos/mutable_list.py
@@ -50,7 +50,18 @@ class _IndexError:
50 50
     _maxlength = None
51 51
     _IndexError = IndexError
52 52
 
53  
-    ### Special methods for Python list interface ###
  53
+    ### Python initialization and list interface methods ###
  54
+
  55
+    def __init__(self, *args, **kwargs):
  56
+        if not hasattr(self, '_getitem_internal'):
  57
+            self._getitem_internal = self._getitem_external
  58
+
  59
+        if not hasattr(self, '_set_single'):
  60
+            self._set_single = self._set_single_rebuild
  61
+            self._assign_extended_slice = self._assign_extended_slice_rebuild
  62
+
  63
+        super(ListMixin, self).__init__(*args, **kwargs)
  64
+
54 65
     def __getitem__(self, index):
55 66
         "Gets the coordinates of the point(s) at the specified index/slice."
56 67
         if isinstance(index, slice):
@@ -128,19 +139,6 @@ def count(self, val):
128 139
 
129 140
     ### Private API routines unique to ListMixin ###
130 141
 
131  
-    def __init__(self, *args, **kwargs):
132  
-        if not hasattr(self, '_getitem_internal'):
133  
-            self._getitem_internal = self._getitem_external
134  
-
135  
-        if hasattr(self, '_set_single'):
136  
-            self._canSetSingle = True
137  
-        else:
138  
-            self._canSetSingle = False
139  
-            self._set_single             = self._set_single_rebuild
140  
-            self._assign_extended_slice   = self._assign_extended_slice_rebuild
141  
-
142  
-        super(ListMixin, self).__init__(*args, **kwargs)
143  
-
144 142
     def _rebuild(self, newLen, newItems):
145 143
         if newLen < self._minlength:
146 144
             raise ValueError('Must have at least %d items' % self._minlength)
3  django/contrib/gis/geos/point.py
@@ -52,9 +52,6 @@ def _create_point(self, ndim, coords):
52 52
 
53 53
         return capi.create_point(cs)
54 54
 
55  
-    def _getitem_external(self, index):
56  
-        return self._cs.getOrdinate(index, 0)
57  
-
58 55
     def _set_collection(self, length, items):
59 56
         ptr = self._create_point(length, items)
60 57
         if ptr:
6  django/contrib/gis/geos/polygon.py
@@ -133,12 +133,6 @@ def _getitem_internal(self, index):
133 133
     def _getitem_external(self, index):
134 134
         return GEOSGeometry(capi.geom_clone(self._getitem_internal(index)), srid=self.srid)
135 135
 
136  
-    # Because Polygonss need to be rebuilt upon the changing of a
137  
-    # component geometry, these routines are set to their counterparts that
138  
-    # rebuild the entire geometry.
139  
-    _set_single = GEOSGeometry._set_single_rebuild
140  
-    _assign_extended_slice   = GEOSGeometry._assign_extended_slice_rebuild
141  
-
142 136
     #### Polygon Properties ####
143 137
     @property
144 138
     def num_interior_rings(self):
184  django/contrib/gis/geos/tests/pymutable_geometries.py
... ...
@@ -1,184 +0,0 @@
1  
-from django.contrib.gis.geos import *
2  
-from random import random
3  
-
4  
-SEQ_LENGTH = 10
5  
-SEQ_RANGE = (-1 * SEQ_LENGTH, SEQ_LENGTH)
6  
-SEQ_BOUNDS = (-1 * SEQ_LENGTH, -1, 0, SEQ_LENGTH - 1)
7  
-SEQ_OUT_OF_BOUNDS = (-1 * SEQ_LENGTH -1 , SEQ_LENGTH)
8  
-
9  
-def seqrange(): return xrange(*SEQ_RANGE)
10  
-
11  
-def random_coord(dim    = 2,           # coordinate dimensions
12  
-                 rng    = (-50,50),    # coordinate range
13  
-                 num_type     = float,
14  
-                 round_coords = True):
15  
-
16  
-    if round_coords:
17  
-        num = lambda: num_type(round(random() * (rng[1]-rng[0]) + rng[0]))
18  
-    else:
19  
-        num = lambda: num_type(random() * (rng[1]-rng[0]) + rng[0])
20  
-
21  
-    return tuple( num() for axis in xrange(dim) )
22  
-
23  
-def random_list(length = SEQ_LENGTH, ring = False, **kwargs):
24  
-    result  = [ random_coord(**kwargs) for index in xrange(length) ]
25  
-    if ring:
26  
-        result[-1] = result[0]
27  
-
28  
-    return result
29  
-
30  
-random_list.single = random_coord
31  
-
32  
-def random_coll(count = SEQ_LENGTH, **kwargs):
33  
-    return [ tuple(random_list(**kwargs)) for i in xrange(count) ]
34  
-
35  
-random_coll.single = random_list
36  
-
37  
-class PyMutTestGeom:
38  
-    "The Test Geometry class container."
39  
-    def __init__(self, geom_type, coords_fcn=random_list, subtype=tuple, **kwargs):
40  
-        self.geom_type  = geom_type
41  
-        self.subtype    = subtype
42  
-        self.coords_fcn = coords_fcn
43  
-        self.fcn_args   = kwargs
44  
-        self.coords = self.coords_fcn(**kwargs)
45  
-        self.geom   = self.make_geom()
46  
-
47  
-    def newitem(self, **kwargs):
48  
-        a = self.coords_fcn.single(**kwargs)
49  
-        return self.subtype(a), tuple(a)
50  
-
51  
-    @property
52  
-    def tuple_coords(self):
53  
-        return tuple(self.coords)
54  
-
55  
-    def make_geom(self):
56  
-        return self.geom_type(map(self.subtype,self.coords))
57  
-
58  
-
59  
-def slice_geometries(ring=True):
60  
-    testgeoms = [
61  
-        PyMutTestGeom(LineString),
62  
-        PyMutTestGeom(MultiPoint, subtype=Point),
63  
-        PyMutTestGeom(MultiLineString, coords_fcn=random_coll, subtype=LineString),
64  
-        ]
65  
-    if ring:
66  
-        testgeoms.append(PyMutTestGeom(LinearRing, ring=True))
67  
-
68  
-    return testgeoms
69  
-
70  
-def getslice_functions():
71  
-    def gs_01(x): x[0:4],
72  
-    def gs_02(x): x[5:-1],
73  
-    def gs_03(x): x[6:2:-1],
74  
-    def gs_04(x): x[:],
75  
-    def gs_05(x): x[:3],
76  
-    def gs_06(x): x[::2],
77  
-    def gs_07(x): x[::-4],
78  
-    def gs_08(x): x[7:7],
79  
-    def gs_09(x): x[20:],
80  
-
81  
-    # don't really care about ringy-ness here
82  
-    return mark_ring(vars(), 'gs_')
83  
-
84  
-def delslice_functions():
85  
-    def ds_01(x): del x[0:4]
86  
-    def ds_02(x): del x[5:-1]
87  
-    def ds_03(x): del x[6:2:-1]
88  
-    def ds_04(x): del x[:]     # should this be allowed?
89  
-    def ds_05(x): del x[:3]
90  
-    def ds_06(x): del x[1:9:2]
91  
-    def ds_07(x): del x[::-4]
92  
-    def ds_08(x): del x[7:7]
93  
-    def ds_09(x): del x[-7:-2]
94  
-
95  
-    return mark_ring(vars(), 'ds_')
96  
-
97  
-def setslice_extended_functions(g):
98  
-    a = g.coords_fcn(3, rng=(100,150))
99  
-    def maptype(x,a):
100  
-        if isinstance(x, list): return a
101  
-        else: return map(g.subtype, a)
102  
-
103  
-    def sse_00(x): x[:3:1]      = maptype(x, a)
104  
-    def sse_01(x): x[0:3:1]     = maptype(x, a)
105  
-    def sse_02(x): x[2:5:1]     = maptype(x, a)
106  
-    def sse_03(x): x[-3::1]     = maptype(x, a)
107  
-    def sse_04(x): x[-4:-1:1]   = maptype(x, a)
108  
-    def sse_05(x): x[8:5:-1]    = maptype(x, a)
109  
-    def sse_06(x): x[-6:-9:-1]  = maptype(x, a)
110  
-    def sse_07(x): x[:8:3]      = maptype(x, a)
111  
-    def sse_08(x): x[1::3]      = maptype(x, a)
112  
-    def sse_09(x): x[-2::-3]    = maptype(x, a)
113  
-    def sse_10(x): x[7:1:-2]    = maptype(x, a)
114  
-    def sse_11(x): x[2:8:2]     = maptype(x, a)
115  
-
116  
-    return mark_ring(vars(), 'sse_')
117  
-
118  
-def setslice_simple_functions(g):
119  
-    a = g.coords_fcn(3, rng=(100,150))
120  
-    def maptype(x,a):
121  
-        if isinstance(x, list): return a
122  
-        else: return map(g.subtype, a)
123  
-
124  
-    def ss_00(x): x[:0]  = maptype(x, a)
125  
-    def ss_01(x): x[:1]  = maptype(x, a)
126  
-    def ss_02(x): x[:2]  = maptype(x, a)
127  
-    def ss_03(x): x[:3]  = maptype(x, a)
128  
-    def ss_04(x): x[-4:] = maptype(x, a)
129  
-    def ss_05(x): x[-3:] = maptype(x, a)
130  
-    def ss_06(x): x[-2:] = maptype(x, a)
131  
-    def ss_07(x): x[-1:] = maptype(x, a)
132  
-    def ss_08(x): x[5:]  = maptype(x, a)
133  
-    def ss_09(x): x[:]   = maptype(x, a)
134  
-    def ss_10(x): x[4:4] = maptype(x, a)
135  
-    def ss_11(x): x[4:5] = maptype(x, a)
136  
-    def ss_12(x): x[4:7] = maptype(x, a)
137  
-    def ss_13(x): x[4:8] = maptype(x, a)
138  
-    def ss_14(x): x[10:] = maptype(x, a)
139  
-    def ss_15(x): x[20:30]  = maptype(x, a)
140  
-    def ss_16(x): x[-13:-8] = maptype(x, a)
141  
-    def ss_17(x): x[-13:-9] = maptype(x, a)
142  
-    def ss_18(x): x[-13:-10] = maptype(x, a)
143  
-    def ss_19(x): x[-13:-11] = maptype(x, a)
144  
-
145  
-    return mark_ring(vars(), 'ss_')
146  
-
147  
-def test_geos_functions():
148  
-
149  
-    return (
150  
-        lambda x: x.num_coords,
151  
-        lambda x: x.empty,
152  
-        lambda x: x.valid,
153  
-        lambda x: x.simple,
154  
-        lambda x: x.ring,
155  
-        lambda x: x.boundary,
156  
-        lambda x: x.convex_hull,
157  
-        lambda x: x.extend,
158  
-        lambda x: x.area,
159  
-        lambda x: x.length,
160  
-            )
161  
-
162  
-def mark_ring(locals, name_pat, length=SEQ_LENGTH):
163  
-    '''
164  
-    Accepts an array of functions which perform slice modifications
165  
-    and labels each function as to whether or not it preserves ring-ness
166  
-    '''
167  
-    func_array = [ val for name, val in locals.items()
168  
-                    if hasattr(val, '__call__')
169  
-                    and name.startswith(name_pat) ]
170  
-
171  
-    for i in xrange(len(func_array)):
172  
-        a = range(length)
173  
-        a[-1] = a[0]
174  
-        func_array[i](a)
175  
-        ring = len(a) == 0 or (len(a) > 3 and a[-1] == a[0])
176  
-        func_array[i].ring = ring
177  
-
178  
-    return func_array
179  
-
180  
-def getcoords(o):
181  
-    if hasattr(o, 'coords'):
182  
-        return o.coords
183  
-    else:
184  
-        return o
658  django/contrib/gis/geos/tests/test_mutable_list.py
... ...
@@ -1,329 +1,329 @@
1  
-# Copyright (c) 2008-2009 Aryeh Leib Taurog, all rights reserved.
2  
-# Modified from original contribution by Aryeh Leib Taurog, which was
3  
-# released under the New BSD license.
4  
-import unittest
5  
-from django.contrib.gis.geos.mutable_list import ListMixin
6  
-
7  
-class UserListA(ListMixin):
8  
-    _mytype = tuple
9  
-    def __init__(self, i_list, *args, **kwargs):
10  
-        self._list = self._mytype(i_list)
11  
-        super(UserListA, self).__init__(*args, **kwargs)
12  
-
13  
-    def __len__(self):  return len(self._list)
14  
-
15  
-    def __iter__(self): return iter(self._list)
16  
-
17  
-    def __str__(self):  return str(self._list)
18  
-
19  
-    def __repr__(self): return repr(self._list)
20  
-
21  
-    def _set_collection(self, length, items):
22  
-        # this would work:
23  
-        # self._list = self._mytype(items)
24  
-        # but then we wouldn't be testing length parameter
25  
-        itemList = ['x'] * length
26  
-        for i, v in enumerate(items):
27  
-            itemList[i] = v
28  
-
29  
-        self._list = self._mytype(itemList)
30  
-
31  
-    def _getitem_external(self, index):
32  
-        return self._list[index]
33  
-
34  
-    _getitem_internal = _getitem_external
35  
-    _set_single = ListMixin._set_single_rebuild
36  
-    _assign_extended_slice = ListMixin._assign_extended_slice_rebuild
37  
-
38  
-class UserListB(UserListA):
39  
-    _mytype = list
40  
-
41  
-    def _set_single(self, index, value):
42  
-        self._list[index] = value
43  
-
44  
-def nextRange(length):
45  
-    nextRange.start += 100
46  
-    return range(nextRange.start, nextRange.start + length)
47  
-
48  
-nextRange.start = 0
49  
-
50  
-class ListMixinTest(unittest.TestCase):
51  
-    """
52  
-    Tests base class ListMixin by comparing a list clone which is
53  
-    a ListMixin subclass with a real Python list.
54  
-    """
55  
-    limit = 3
56  
-    listType = UserListA
57  
-
58  
-    @classmethod
59  
-    def lists_of_len(cls, length=None):
60  
-        if length is None: length = cls.limit
61  
-        pl = range(length)
62  
-        return pl, cls.listType(pl)
63  
-
64  
-    @classmethod
65  
-    def limits_plus(cls, b):
66  
-        return range(-cls.limit - b, cls.limit + b)
67  
-
68  
-    @classmethod
69  
-    def step_range(cls):
70  
-        return range(-1 - cls.limit, 0) + range(1, 1 + cls.limit)
71  
-
72  
-    def test01_getslice(self):
73  
-        'Testing slice retrieval'
74  
-        pl, ul = self.lists_of_len()
75  
-        for i in self.limits_plus(1):
76  
-            self.assertEqual(pl[i:], ul[i:], 'slice [%d:]' % (i))
77  
-            self.assertEqual(pl[:i], ul[:i], 'slice [:%d]' % (i))
78  
-
79  
-            for j in self.limits_plus(1):
80  
-                self.assertEqual(pl[i:j], ul[i:j], 'slice [%d:%d]' % (i,j))
81  
-                for k in self.step_range():
82  
-                    self.assertEqual(pl[i:j:k], ul[i:j:k], 'slice [%d:%d:%d]' % (i,j,k))
83  
-
84  
-            for k in self.step_range():
85  
-                self.assertEqual(pl[i::k], ul[i::k], 'slice [%d::%d]' % (i,k))
86  
-                self.assertEqual(pl[:i:k], ul[:i:k], 'slice [:%d:%d]' % (i,k))
87  
-
88  
-        for k in self.step_range():
89  
-            self.assertEqual(pl[::k], ul[::k], 'slice [::%d]' % (k))
90  
-
91  
-    def test02_setslice(self):
92  
-        'Testing slice assignment'
93  
-        def setfcn(x,i,j,k,L): x[i:j:k] = range(L)
94  
-        pl, ul = self.lists_of_len()
95  
-        for slen in range(self.limit + 1):
96  
-            ssl = nextRange(slen)
97  
-            ul[:] = ssl
98  
-            pl[:] = ssl
99  
-            self.assertEqual(pl, ul[:], 'set slice [:]')
100  
-
101  
-            for i in self.limits_plus(1):
102  
-                ssl = nextRange(slen)
103  
-                ul[i:] = ssl
104  
-                pl[i:] = ssl
105  
-                self.assertEqual(pl, ul[:], 'set slice [%d:]' % (i))
106  
-
107  
-                ssl = nextRange(slen)
108  
-                ul[:i] = ssl
109  
-                pl[:i] = ssl
110  
-                self.assertEqual(pl, ul[:], 'set slice [:%d]' % (i))
111  
-
112  
-                for j in self.limits_plus(1):
113  
-                    ssl = nextRange(slen)
114  
-                    ul[i:j] = ssl
115  
-                    pl[i:j] = ssl
116  
-                    self.assertEqual(pl, ul[:], 'set slice [%d:%d]' % (i, j))
117  
-
118  
-                    for k in self.step_range():
119  
-                        ssl = nextRange( len(ul[i:j:k]) )
120  
-                        ul[i:j:k] = ssl
121  
-                        pl[i:j:k] = ssl
122  
-                        self.assertEqual(pl, ul[:], 'set slice [%d:%d:%d]' % (i, j, k))
123  
-
124  
-                        sliceLen = len(ul[i:j:k])
125  
-                        self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen + 1)
126  
-                        if sliceLen > 2:
127  
-                            self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen - 1)
128  
-
129  
-                for k in self.step_range():
130  
-                    ssl = nextRange( len(ul[i::k]) )
131  
-                    ul[i::k] = ssl
132  
-                    pl[i::k] = ssl
133  
-                    self.assertEqual(pl, ul[:], 'set slice [%d::%d]' % (i, k))
134  
-
135  
-                    ssl = nextRange( len(ul[:i:k]) )
136  
-                    ul[:i:k] = ssl
137  
-                    pl[:i:k] = ssl
138  
-                    self.assertEqual(pl, ul[:], 'set slice [:%d:%d]' % (i, k))
139  
-
140  
-            for k in self.step_range():
141  
-                ssl = nextRange(len(ul[::k]))
142  
-                ul[::k] = ssl
143  
-                pl[::k] = ssl
144  
-                self.assertEqual(pl, ul[:], 'set slice [::%d]' % (k))
145  
-
146  
-
147  
-    def test03_delslice(self):
148  
-        'Testing delete slice'
149  
-        for Len in range(self.limit):
150  
-            pl, ul = self.lists_of_len(Len)
151  
-            del pl[:]
152  
-            del ul[:]
153  
-            self.assertEqual(pl[:], ul[:], 'del slice [:]')
154  
-            for i in range(-Len - 1, Len + 1):
155  
-                pl, ul = self.lists_of_len(Len)
156  
-                del pl[i:]
157  
-                del ul[i:]
158  
-                self.assertEqual(pl[:], ul[:], 'del slice [%d:]' % (i))
159  
-                pl, ul = self.lists_of_len(Len)
160  
-                del pl[:i]
161  
-                del ul[:i]
162  
-                self.assertEqual(pl[:], ul[:], 'del slice [:%d]' % (i))
163  
-                for j in range(-Len - 1, Len + 1):
164  
-                    pl, ul = self.lists_of_len(Len)
165  
-                    del pl[i:j]
166  
-                    del ul[i:j]
167  
-                    self.assertEqual(pl[:], ul[:], 'del slice [%d:%d]' % (i,j))
168  
-                    for k in range(-Len - 1,0) + range(1,Len):
169  
-                        pl, ul = self.lists_of_len(Len)
170  
-                        del pl[i:j:k]
171  
-                        del ul[i:j:k]
172  
-                        self.assertEqual(pl[:], ul[:], 'del slice [%d:%d:%d]' % (i,j,k))
173  
-
174  
-                for k in range(-Len - 1,0) + range(1,Len):
175  
-                    pl, ul = self.lists_of_len(Len)
176  
-                    del pl[:i:k]
177  
-                    del ul[:i:k]
178  
-                    self.assertEqual(pl[:], ul[:], 'del slice [:%d:%d]' % (i,k))
179  
-
180  
-                    pl, ul = self.lists_of_len(Len)
181  
-                    del pl[i::k]
182  
-                    del ul[i::k]
183  
-                    self.assertEqual(pl[:], ul[:], 'del slice [%d::%d]' % (i,k))
184  
-
185  
-            for k in range(-Len - 1,0) + range(1,Len):
186  
-                pl, ul = self.lists_of_len(Len)
187  
-                del pl[::k]
188  
-                del ul[::k]
189  
-                self.assertEqual(pl[:], ul[:], 'del slice [::%d]' % (k))
190  
-
191  
-    def test04_get_set_del_single(self):
192  
-        'Testing get/set/delete single item'
193  
-        pl, ul = self.lists_of_len()
194  
-        for i in self.limits_plus(0):
195  
-            self.assertEqual(pl[i], ul[i], 'get single item [%d]' % i)
196  
-
197  
-        for i in self.limits_plus(0):
198  
-            pl, ul = self.lists_of_len()
199  
-            pl[i] = 100
200  
-            ul[i] = 100
201  
-            self.assertEqual(pl[:], ul[:], 'set single item [%d]' % i)
202  
-
203  
-        for i in self.limits_plus(0):
204  
-            pl, ul = self.lists_of_len()
205  
-            del pl[i]
206  
-            del ul[i]
207  
-            self.assertEqual(pl[:], ul[:], 'del single item [%d]' % i)
208  
-
209  
-    def test05_out_of_range_exceptions(self):
210  
-        'Testing out of range exceptions'
211  
-        def setfcn(x, i): x[i] = 20
212  
-        def getfcn(x, i): return x[i]
213  
-        def delfcn(x, i): del x[i]
214  
-        pl, ul = self.lists_of_len()
215  
-        for i in (-1 - self.limit, self.limit):
216  
-            self.assertRaises(IndexError, setfcn, ul, i) # 'set index %d' % i)
217  
-            self.assertRaises(IndexError, getfcn, ul, i) # 'get index %d' % i)
218  
-            self.assertRaises(IndexError, delfcn, ul, i) # 'del index %d' % i)
219  
-
220  
-    def test06_list_methods(self):
221  
-        'Testing list methods'
222  
-        pl, ul = self.lists_of_len()
223  
-        pl.append(40)
224  
-        ul.append(40)
225  
-        self.assertEqual(pl[:], ul[:], 'append')
226  
-
227  
-        pl.extend(range(50,55))
228  
-        ul.extend(range(50,55))
229  
-        self.assertEqual(pl[:], ul[:], 'extend')
230  
-
231  
-        for i in self.limits_plus(1):
232  
-            pl, ul = self.lists_of_len()
233  
-            pl.insert(i,50)
234  
-            ul.insert(i,50)
235  
-            self.assertEqual(pl[:], ul[:], 'insert at %d' % i)
236  
-
237  
-        for i in self.limits_plus(0):
238  
-            pl, ul = self.lists_of_len()
239  
-            self.assertEqual(pl.pop(i), ul.pop(i), 'popped value at %d' % i)
240  
-            self.assertEqual(pl[:], ul[:], 'after pop at %d' % i)
241  
-
242  
-        pl, ul = self.lists_of_len()
243  
-        self.assertEqual(pl.pop(), ul.pop(i), 'popped value')
244  
-        self.assertEqual(pl[:], ul[:], 'after pop')
245  
-
246  
-        pl, ul = self.lists_of_len()
247  
-        def popfcn(x, i): x.pop(i)
248  
-        self.assertRaises(IndexError, popfcn, ul, self.limit)
249  
-        self.assertRaises(IndexError, popfcn, ul, -1 - self.limit)
250  
-
251  
-        pl, ul = self.lists_of_len()
252  
-        for val in range(self.limit):
253  
-            self.assertEqual(pl.index(val), ul.index(val), 'index of %d' % val)
254  
-
255  
-        for val in self.limits_plus(2):
256  
-            self.assertEqual(pl.count(val), ul.count(val), 'count %d' % val)
257  
-
258  
-        for val in range(self.limit):
259  
-            pl, ul = self.lists_of_len()
260  
-            pl.remove(val)
261  
-            ul.remove(val)
262  
-            self.assertEqual(pl[:], ul[:], 'after remove val %d' % val)
263  
-
264  
-        def indexfcn(x, v): return x.index(v)
265  
-        def removefcn(x, v): return x.remove(v)
266  
-        self.assertRaises(ValueError, indexfcn, ul, 40)
267  
-        self.assertRaises(ValueError, removefcn, ul, 40)
268  
-
269  
-    def test07_allowed_types(self):
270  
-        'Testing type-restricted list'
271  
-        pl, ul = self.lists_of_len()
272  
-        ul._allowed = (int, long)
273  
-        ul[1] = 50
274  
-        ul[:2] = [60, 70, 80]
275  
-        def setfcn(x, i, v): x[i] = v
276  
-        self.assertRaises(TypeError, setfcn, ul, 2, 'hello')
277  
-        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), ('hello','goodbye'))
278  
-
279  
-    def test08_min_length(self):
280  
-        'Testing length limits'
281  
-        pl, ul = self.lists_of_len()
282  
-        ul._minlength = 1
283  
-        def delfcn(x,i): del x[:i]
284  
-        def setfcn(x,i): x[:i] = []
285  
-        for i in range(self.limit - ul._minlength + 1, self.limit + 1):
286  
-            self.assertRaises(ValueError, delfcn, ul, i)
287  
-            self.assertRaises(ValueError, setfcn, ul, i)
288  
-        del ul[:ul._minlength]
289  
-
290  
-        ul._maxlength = 4
291  
-        for i in range(0, ul._maxlength - len(ul)):
292  
-            ul.append(i)
293  
-        self.assertRaises(ValueError, ul.append, 10)
294  
-
295  
-    def test09_iterable_check(self):
296  
-        'Testing error on assigning non-iterable to slice'
297  
-        pl, ul = self.lists_of_len(self.limit + 1)
298  
-        def setfcn(x, i, v): x[i] = v
299  
-        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), 2)
300  
-
301  
-    def test10_checkindex(self):
302  
-        'Testing index check'
303  
-        pl, ul = self.lists_of_len()
304  
-        for i in self.limits_plus(0):
305  
-            if i < 0:
306  
-                self.assertEqual(ul._checkindex(i), i + self.limit, '_checkindex(neg index)')
307  
-            else:
308  
-                self.assertEqual(ul._checkindex(i), i, '_checkindex(pos index)')
309  
-
310  
-        for i in (-self.limit - 1, self.limit):
311  
-            self.assertRaises(IndexError, ul._checkindex, i)
312  
-
313  
-        ul._IndexError = TypeError
314  
-        self.assertRaises(TypeError, ul._checkindex, -self.limit - 1)
315  
-
316  
-class ListMixinTestSingle(ListMixinTest):
317  
-    listType = UserListB
318  
-
319  
-def suite():
320  
-    s = unittest.TestSuite()
321  
-    s.addTest(unittest.makeSuite(ListMixinTest))
322  
-    s.addTest(unittest.makeSuite(ListMixinTestSingle))
323  
-    return s
324  
-
325  
-def run(verbosity=2):
326  
-    unittest.TextTestRunner(verbosity=verbosity).run(suite())
327  
-
328  
-if __name__ == '__main__':
329  
-    run()
  1
+# Copyright (c) 2008-2009 Aryeh Leib Taurog, all rights reserved.
  2
+# Modified from original contribution by Aryeh Leib Taurog, which was
  3
+# released under the New BSD license.
  4
+import unittest
  5
+from django.contrib.gis.geos.mutable_list import ListMixin
  6
+
  7
+class UserListA(ListMixin):
  8
+    _mytype = tuple
  9
+    def __init__(self, i_list, *args, **kwargs):
  10
+        self._list = self._mytype(i_list)
  11
+        super(UserListA, self).__init__(*args, **kwargs)
  12
+
  13
+    def __len__(self):  return len(self._list)
  14
+
  15
+    def __iter__(self): return iter(self._list)
  16
+
  17
+    def __str__(self):  return str(self._list)
  18
+
  19
+    def __repr__(self): return repr(self._list)
  20
+
  21
+    def _set_collection(self, length, items):
  22
+        # this would work:
  23
+        # self._list = self._mytype(items)
  24
+        # but then we wouldn't be testing length parameter
  25
+        itemList = ['x'] * length
  26
+        for i, v in enumerate(items):
  27
+            itemList[i] = v
  28
+
  29
+        self._list = self._mytype(itemList)
  30
+
  31
+    def _getitem_external(self, index):
  32
+        return self._list[index]
  33
+
  34
+    _getitem_internal = _getitem_external
  35
+    _set_single = ListMixin._set_single_rebuild
  36
+    _assign_extended_slice = ListMixin._assign_extended_slice_rebuild
  37
+
  38
+class UserListB(UserListA):
  39
+    _mytype = list
  40
+
  41
+    def _set_single(self, index, value):
  42
+        self._list[index] = value
  43
+
  44
+def nextRange(length):
  45
+    nextRange.start += 100
  46
+    return range(nextRange.start, nextRange.start + length)
  47
+
  48
+nextRange.start = 0
  49
+
  50
+class ListMixinTest(unittest.TestCase):
  51
+    """
  52
+    Tests base class ListMixin by comparing a list clone which is
  53
+    a ListMixin subclass with a real Python list.
  54
+    """
  55
+    limit = 3
  56
+    listType = UserListA
  57
+
  58
+    @classmethod
  59
+    def lists_of_len(cls, length=None):
  60
+        if length is None: length = cls.limit
  61
+        pl = range(length)
  62
+        return pl, cls.listType(pl)
  63
+
  64
+    @classmethod
  65
+    def limits_plus(cls, b):
  66
+        return range(-cls.limit - b, cls.limit + b)
  67
+
  68
+    @classmethod
  69
+    def step_range(cls):
  70
+        return range(-1 - cls.limit, 0) + range(1, 1 + cls.limit)
  71
+
  72
+    def test01_getslice(self):
  73
+        'Testing slice retrieval'
  74
+        pl, ul = self.lists_of_len()
  75
+        for i in self.limits_plus(1):
  76
+            self.assertEqual(pl[i:], ul[i:], 'slice [%d:]' % (i))
  77
+            self.assertEqual(pl[:i], ul[:i], 'slice [:%d]' % (i))
  78
+
  79
+            for j in self.limits_plus(1):
  80
+                self.assertEqual(pl[i:j], ul[i:j], 'slice [%d:%d]' % (i,j))
  81
+                for k in self.step_range():
  82
+                    self.assertEqual(pl[i:j:k], ul[i:j:k], 'slice [%d:%d:%d]' % (i,j,k))
  83
+
  84
+            for k in self.step_range():
  85
+                self.assertEqual(pl[i::k], ul[i::k], 'slice [%d::%d]' % (i,k))
  86
+                self.assertEqual(pl[:i:k], ul[:i:k], 'slice [:%d:%d]' % (i,k))
  87
+
  88
+        for k in self.step_range():
  89
+            self.assertEqual(pl[::k], ul[::k], 'slice [::%d]' % (k))
  90
+
  91
+    def test02_setslice(self):
  92
+        'Testing slice assignment'
  93
+        def setfcn(x,i,j,k,L): x[i:j:k] = range(L)
  94
+        pl, ul = self.lists_of_len()
  95
+        for slen in range(self.limit + 1):
  96
+            ssl = nextRange(slen)
  97
+            ul[:] = ssl
  98
+            pl[:] = ssl
  99
+            self.assertEqual(pl, ul[:], 'set slice [:]')
  100
+
  101
+            for i in self.limits_plus(1):
  102
+                ssl = nextRange(slen)
  103
+                ul[i:] = ssl
  104
+                pl[i:] = ssl
  105
+                self.assertEqual(pl, ul[:], 'set slice [%d:]' % (i))
  106
+
  107
+                ssl = nextRange(slen)
  108
+                ul[:i] = ssl
  109
+                pl[:i] = ssl
  110
+                self.assertEqual(pl, ul[:], 'set slice [:%d]' % (i))
  111
+
  112
+                for j in self.limits_plus(1):
  113
+                    ssl = nextRange(slen)
  114
+                    ul[i:j] = ssl
  115
+                    pl[i:j] = ssl
  116
+                    self.assertEqual(pl, ul[:], 'set slice [%d:%d]' % (i, j))
  117
+
  118
+                    for k in self.step_range():
  119
+                        ssl = nextRange( len(ul[i:j:k]) )
  120
+                        ul[i:j:k] = ssl
  121
+                        pl[i:j:k] = ssl
  122
+                        self.assertEqual(pl, ul[:], 'set slice [%d:%d:%d]' % (i, j, k))
  123
+
  124
+                        sliceLen = len(ul[i:j:k])
  125
+                        self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen + 1)
  126
+                        if sliceLen > 2:
  127
+                            self.assertRaises(ValueError, setfcn, ul, i, j, k, sliceLen - 1)
  128
+
  129
+                for k in self.step_range():
  130
+                    ssl = nextRange( len(ul[i::k]) )
  131
+                    ul[i::k] = ssl
  132
+                    pl[i::k] = ssl
  133
+                    self.assertEqual(pl, ul[:], 'set slice [%d::%d]' % (i, k))
  134
+
  135
+                    ssl = nextRange( len(ul[:i:k]) )
  136
+                    ul[:i:k] = ssl
  137
+                    pl[:i:k] = ssl
  138
+                    self.assertEqual(pl, ul[:], 'set slice [:%d:%d]' % (i, k))
  139
+
  140
+            for k in self.step_range():
  141
+                ssl = nextRange(len(ul[::k]))
  142
+                ul[::k] = ssl
  143
+                pl[::k] = ssl
  144
+                self.assertEqual(pl, ul[:], 'set slice [::%d]' % (k))
  145
+
  146
+
  147
+    def test03_delslice(self):
  148
+        'Testing delete slice'
  149
+        for Len in range(self.limit):
  150
+            pl, ul = self.lists_of_len(Len)
  151
+            del pl[:]
  152
+            del ul[:]
  153
+            self.assertEqual(pl[:], ul[:], 'del slice [:]')
  154
+            for i in range(-Len - 1, Len + 1):
  155
+                pl, ul = self.lists_of_len(Len)
  156
+                del pl[i:]
  157
+                del ul[i:]
  158
+                self.assertEqual(pl[:], ul[:], 'del slice [%d:]' % (i))
  159
+                pl, ul = self.lists_of_len(Len)
  160
+                del pl[:i]
  161
+                del ul[:i]
  162
+                self.assertEqual(pl[:], ul[:], 'del slice [:%d]' % (i))
  163
+                for j in range(-Len - 1, Len + 1):
  164
+                    pl, ul = self.lists_of_len(Len)
  165
+                    del pl[i:j]
  166
+                    del ul[i:j]
  167
+                    self.assertEqual(pl[:], ul[:], 'del slice [%d:%d]' % (i,j))
  168
+                    for k in range(-Len - 1,0) + range(1,Len):
  169
+                        pl, ul = self.lists_of_len(Len)
  170
+                        del pl[i:j:k]
  171
+                        del ul[i:j:k]
  172
+                        self.assertEqual(pl[:], ul[:], 'del slice [%d:%d:%d]' % (i,j,k))
  173
+
  174
+                for k in range(-Len - 1,0) + range(1,Len):
  175
+                    pl, ul = self.lists_of_len(Len)
  176
+                    del pl[:i:k]
  177
+                    del ul[:i:k]
  178
+                    self.assertEqual(pl[:], ul[:], 'del slice [:%d:%d]' % (i,k))
  179
+
  180
+                    pl, ul = self.lists_of_len(Len)
  181
+                    del pl[i::k]
  182
+                    del ul[i::k]
  183
+                    self.assertEqual(pl[:], ul[:], 'del slice [%d::%d]' % (i,k))
  184
+
  185
+            for k in range(-Len - 1,0) + range(1,Len):
  186
+                pl, ul = self.lists_of_len(Len)
  187
+                del pl[::k]
  188
+                del ul[::k]
  189
+                self.assertEqual(pl[:], ul[:], 'del slice [::%d]' % (k))
  190
+
  191
+    def test04_get_set_del_single(self):
  192
+        'Testing get/set/delete single item'
  193
+        pl, ul = self.lists_of_len()
  194
+        for i in self.limits_plus(0):
  195
+            self.assertEqual(pl[i], ul[i], 'get single item [%d]' % i)
  196
+
  197
+        for i in self.limits_plus(0):
  198
+            pl, ul = self.lists_of_len()
  199
+            pl[i] = 100
  200
+            ul[i] = 100
  201
+            self.assertEqual(pl[:], ul[:], 'set single item [%d]' % i)
  202
+
  203
+        for i in self.limits_plus(0):
  204
+            pl, ul = self.lists_of_len()
  205
+            del pl[i]
  206
+            del ul[i]
  207
+            self.assertEqual(pl[:], ul[:], 'del single item [%d]' % i)
  208
+
  209
+    def test05_out_of_range_exceptions(self):
  210
+        'Testing out of range exceptions'
  211
+        def setfcn(x, i): x[i] = 20
  212
+        def getfcn(x, i): return x[i]
  213
+        def delfcn(x, i): del x[i]
  214
+        pl, ul = self.lists_of_len()
  215
+        for i in (-1 - self.limit, self.limit):
  216
+            self.assertRaises(IndexError, setfcn, ul, i) # 'set index %d' % i)
  217
+            self.assertRaises(IndexError, getfcn, ul, i) # 'get index %d' % i)
  218
+            self.assertRaises(IndexError, delfcn, ul, i) # 'del index %d' % i)
  219
+
  220
+    def test06_list_methods(self):
  221
+        'Testing list methods'
  222
+        pl, ul = self.lists_of_len()
  223
+        pl.append(40)
  224
+        ul.append(40)
  225
+        self.assertEqual(pl[:], ul[:], 'append')
  226
+
  227
+        pl.extend(range(50,55))
  228
+        ul.extend(range(50,55))
  229
+        self.assertEqual(pl[:], ul[:], 'extend')
  230
+
  231
+        for i in self.limits_plus(1):
  232
+            pl, ul = self.lists_of_len()
  233
+            pl.insert(i,50)
  234
+            ul.insert(i,50)
  235
+            self.assertEqual(pl[:], ul[:], 'insert at %d' % i)
  236
+
  237
+        for i in self.limits_plus(0):
  238
+            pl, ul = self.lists_of_len()
  239
+            self.assertEqual(pl.pop(i), ul.pop(i), 'popped value at %d' % i)
  240
+            self.assertEqual(pl[:], ul[:], 'after pop at %d' % i)
  241
+
  242
+        pl, ul = self.lists_of_len()
  243
+        self.assertEqual(pl.pop(), ul.pop(i), 'popped value')
  244
+        self.assertEqual(pl[:], ul[:], 'after pop')
  245
+
  246
+        pl, ul = self.lists_of_len()
  247
+        def popfcn(x, i): x.pop(i)
  248
+        self.assertRaises(IndexError, popfcn, ul, self.limit)
  249
+        self.assertRaises(IndexError, popfcn, ul, -1 - self.limit)
  250
+
  251
+        pl, ul = self.lists_of_len()
  252
+        for val in range(self.limit):
  253
+            self.assertEqual(pl.index(val), ul.index(val), 'index of %d' % val)
  254
+
  255
+        for val in self.limits_plus(2):
  256
+            self.assertEqual(pl.count(val), ul.count(val), 'count %d' % val)
  257
+
  258
+        for val in range(self.limit):
  259
+            pl, ul = self.lists_of_len()
  260
+            pl.remove(val)
  261
+            ul.remove(val)
  262
+            self.assertEqual(pl[:], ul[:], 'after remove val %d' % val)
  263
+
  264
+        def indexfcn(x, v): return x.index(v)
  265
+        def removefcn(x, v): return x.remove(v)
  266
+        self.assertRaises(ValueError, indexfcn, ul, 40)
  267
+        self.assertRaises(ValueError, removefcn, ul, 40)
  268
+
  269
+    def test07_allowed_types(self):
  270
+        'Testing type-restricted list'
  271
+        pl, ul = self.lists_of_len()
  272
+        ul._allowed = (int, long)
  273
+        ul[1] = 50
  274
+        ul[:2] = [60, 70, 80]
  275
+        def setfcn(x, i, v): x[i] = v
  276
+        self.assertRaises(TypeError, setfcn, ul, 2, 'hello')
  277
+        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), ('hello','goodbye'))
  278
+
  279
+    def test08_min_length(self):
  280
+        'Testing length limits'
  281
+        pl, ul = self.lists_of_len()
  282
+        ul._minlength = 1
  283
+        def delfcn(x,i): del x[:i]
  284
+        def setfcn(x,i): x[:i] = []
  285
+        for i in range(self.limit - ul._minlength + 1, self.limit + 1):
  286
+            self.assertRaises(ValueError, delfcn, ul, i)
  287
+            self.assertRaises(ValueError, setfcn, ul, i)
  288
+        del ul[:ul._minlength]
  289
+
  290
+        ul._maxlength = 4
  291
+        for i in range(0, ul._maxlength - len(ul)):
  292
+            ul.append(i)
  293
+        self.assertRaises(ValueError, ul.append, 10)
  294
+
  295
+    def test09_iterable_check(self):
  296
+        'Testing error on assigning non-iterable to slice'
  297
+        pl, ul = self.lists_of_len(self.limit + 1)
  298
+        def setfcn(x, i, v): x[i] = v
  299
+        self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), 2)
  300
+
  301
+    def test10_checkindex(self):
  302
+        'Testing index check'
  303
+        pl, ul = self.lists_of_len()
  304
+        for i in self.limits_plus(0):
  305
+            if i < 0:
  306
+                self.assertEqual(ul._checkindex(i), i + self.limit, '_checkindex(neg index)')
  307
+            else:
  308
+                self.assertEqual(ul._checkindex(i), i, '_checkindex(pos index)')
  309
+
  310
+        for i in (-self.limit - 1, self.limit):
  311
+            self.assertRaises(IndexError, ul._checkindex, i)
  312
+
  313
+        ul._IndexError = TypeError
  314
+        self.assertRaises(TypeError, ul._checkindex, -self.limit - 1)
  315
+
  316
+class ListMixinTestSingle(ListMixinTest):
  317
+    listType = UserListB
  318
+
  319
+def suite():
  320
+    s = unittest.TestSuite()
  321
+    s.addTest(unittest.makeSuite(ListMixinTest))
  322
+    s.addTest(unittest.makeSuite(ListMixinTestSingle))
  323
+    return s
  324
+
  325
+def run(verbosity=2):
  326
+    unittest.TextTestRunner(verbosity=verbosity).run(suite())
  327
+
  328
+if __name__ == '__main__':
  329
+    run()

0 notes on commit 0b9a855

Please sign in to comment.
Something went wrong with that request. Please try again.