~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Epsilon/epsilon/test/test_view.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
"""
3
 
Tests for L{epsilon.view}.
4
 
"""
5
 
 
6
 
from operator import getitem
7
 
 
8
 
from twisted.trial.unittest import TestCase
9
 
 
10
 
from epsilon.view import SlicedView
11
 
 
12
 
 
13
 
class SlicedViewTests(TestCase):
14
 
    """
15
 
    Tests for L{SlicedView}
16
 
    """
17
 
    def test_outOfBoundsPositiveStart(self):
18
 
        """
19
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
20
 
        slice with a positive start value greater than the maximum allowed
21
 
        index clips that start value to the end of the underlying sequence.
22
 
        """
23
 
        sequence = ['a', 'b', 'c']
24
 
        view = SlicedView(sequence, slice(3, None))
25
 
        self.assertRaises(IndexError, getitem, view, 0)
26
 
 
27
 
 
28
 
    def test_outOfBoundsNegativeStart(self):
29
 
        """
30
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
31
 
        slice with a negative start value greater than the maximum allowed
32
 
        index clips that start value to the beginning of the underlying
33
 
        sequence.
34
 
        """
35
 
        sequence = ['a', 'b', 'c']
36
 
        view = SlicedView(sequence, slice(-4, None))
37
 
        self.assertEqual(view[0], 'a')
38
 
        self.assertEqual(view[1], 'b')
39
 
        self.assertEqual(view[2], 'c')
40
 
        self.assertRaises(IndexError, getitem, view, 3)
41
 
 
42
 
 
43
 
    def test_outOfBoundsPositiveStop(self):
44
 
        """
45
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
46
 
        slice with a positve stop value greater than the maximum allowed index
47
 
        clips that stop value to the end of the underlying sequence.
48
 
        """
49
 
        sequence = ['a', 'b', 'c']
50
 
        view = SlicedView(sequence, slice(None, 4))
51
 
        self.assertEqual(view[0], 'a')
52
 
        self.assertEqual(view[1], 'b')
53
 
        self.assertEqual(view[2], 'c')
54
 
        self.assertRaises(IndexError, getitem, view, 3)
55
 
 
56
 
 
57
 
    def test_outOfBoundsNegativeStop(self):
58
 
        """
59
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
60
 
        slice with a negative stop value greater than the maximum allowed index
61
 
        clips that stop value to the beginning of the underlying sequence.
62
 
        """
63
 
        sequence = ['a', 'b', 'c']
64
 
        view = SlicedView(sequence, slice(None, -4))
65
 
        self.assertRaises(IndexError, getitem, view, 0)
66
 
 
67
 
 
68
 
    def test_positiveIndices(self):
69
 
        """
70
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
71
 
        slice with no start or stop value behaves in the same way as the
72
 
        underlying sequence with respect to indexing with positive values.
73
 
        """
74
 
        sequence = ['a', 'b', 'c']
75
 
        view = SlicedView(sequence, slice(None))
76
 
        self.assertEqual(view[0], 'a')
77
 
        self.assertEqual(view[1], 'b')
78
 
        self.assertEqual(view[2], 'c')
79
 
        self.assertRaises(IndexError, getitem, view, 3)
80
 
 
81
 
 
82
 
    def test_negativeIndices(self):
83
 
        """
84
 
        Similar to L{test_positiveIndices}, but for negative indices.
85
 
        """
86
 
        sequence = ['a', 'b', 'c']
87
 
        view = SlicedView(sequence, slice(None))
88
 
        self.assertEqual(view[-1], 'c')
89
 
        self.assertEqual(view[-2], 'b')
90
 
        self.assertEqual(view[-3], 'a')
91
 
        self.assertRaises(IndexError, getitem, view, -4)
92
 
 
93
 
 
94
 
    def test_length(self):
95
 
        """
96
 
        Verify that L{SlicedView.__len__} returns the length of the underlying
97
 
        sequence when the SlicedView is constructed with no start or stop
98
 
        values.
99
 
        """
100
 
        sequence = ['a', 'b', 'c']
101
 
        view = SlicedView(sequence, slice(None))
102
 
        self.assertEqual(len(view), 3)
103
 
 
104
 
 
105
 
    def test_lengthEmptySequence(self):
106
 
        """
107
 
        Verify that L{SlicedView.__len__} works with empty sequences.
108
 
        """
109
 
        sequence = []
110
 
        view = SlicedView([], slice(None))
111
 
        self.assertEqual(len(view), 0)
112
 
 
113
 
 
114
 
    def test_positiveStartLength(self):
115
 
        """
116
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
117
 
        slice with a positive start value.
118
 
        """
119
 
        sequence = ['a', 'b', 'c']
120
 
        view = SlicedView(sequence, slice(1, None))
121
 
        self.assertEqual(len(view), 2)
122
 
 
123
 
 
124
 
    def test_negativeStartLength(self):
125
 
        """
126
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
127
 
        slice with a negative start value.
128
 
        """
129
 
        sequence = ['a', 'b', 'c']
130
 
        view = SlicedView(sequence, slice(-2, None))
131
 
        self.assertEqual(len(view), 2)
132
 
 
133
 
 
134
 
    def test_positiveStopLength(self):
135
 
        """
136
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
137
 
        slice with a positive stop value.
138
 
        """
139
 
        sequence = ['a', 'b', 'c']
140
 
        view = SlicedView(sequence, slice(None, 2))
141
 
        self.assertEqual(len(view), 2)
142
 
 
143
 
 
144
 
    def test_negativeStopLength(self):
145
 
        """
146
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
147
 
        slice with a negative stop value.
148
 
        """
149
 
        sequence = ['a', 'b', 'c']
150
 
        view = SlicedView(sequence, slice(None, -1))
151
 
        self.assertEqual(len(view), 2)
152
 
 
153
 
 
154
 
    def test_positiveStartPositiveStopLength(self):
155
 
        """
156
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
157
 
        slice with positive start and stop values.
158
 
        """
159
 
        sequence = ['a', 'b', 'c']
160
 
        view = SlicedView(sequence, slice(1, 2))
161
 
        self.assertEqual(len(view), 1)
162
 
 
163
 
 
164
 
    def test_positiveStartNegativeStopLength(self):
165
 
        """
166
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
167
 
        slice with a positive start value and a negative stop value.
168
 
        """
169
 
        sequence = ['a', 'b', 'c']
170
 
        view = SlicedView(sequence, slice(1, -1))
171
 
        self.assertEqual(len(view), 1)
172
 
 
173
 
 
174
 
    def test_negativeStartPositiveStopLength(self):
175
 
        """
176
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
177
 
        slice with a negative start value and a positive stop value.
178
 
        """
179
 
        sequence = ['a', 'b', 'c']
180
 
        view = SlicedView(sequence, slice(-2, 2))
181
 
        self.assertEqual(len(view), 1)
182
 
 
183
 
 
184
 
    def test_negativeStartNegativeStopLength(self):
185
 
        """
186
 
        Similar to L{test_length}, but for a L{SlicedView} constructed with a
187
 
        slice with negative start and stop values.
188
 
        """
189
 
        sequence = ['a', 'b', 'c']
190
 
        view = SlicedView(sequence, slice(-2, -1))
191
 
        self.assertEqual(len(view), 1)
192
 
 
193
 
 
194
 
    def test_extendedSliceLength(self):
195
 
        """
196
 
        Verify that L{SlicedView.__len__} reports the correct length when a
197
 
        step is present.
198
 
        """
199
 
        sequence = ['a', 'b', 'c', 'd', 'e']
200
 
        view = SlicedView(sequence, slice(1, -1, 2))
201
 
        self.assertEqual(len(view), 2)
202
 
 
203
 
 
204
 
    def test_positiveStartOnlyPositiveIndices(self):
205
 
        """
206
 
        Verify that the C{__getitem__} of a L{SlicedView} constructed with a
207
 
        slice with only a positive start value returns elements at the
208
 
        requested index plus the slice's start value for positive requested
209
 
        indices.
210
 
        """
211
 
        sequence = ['a', 'b', 'c']
212
 
        view = SlicedView(sequence, slice(1, None))
213
 
        self.assertEqual(view[0], 'b')
214
 
        self.assertEqual(view[1], 'c')
215
 
        self.assertRaises(IndexError, getitem, view, 2)
216
 
 
217
 
 
218
 
    def test_positiveStartOnlyNegativeIndices(self):
219
 
        """
220
 
        Similar to L{test_positiveStartOnlyPositiveIndices}, but cover
221
 
        negative requested indices instead of positive ones.
222
 
        """
223
 
        sequence = ['a', 'b', 'c']
224
 
        view = SlicedView(sequence, slice(1, None))
225
 
        self.assertEqual(view[-1], 'c')
226
 
        self.assertEqual(view[-2], 'b')
227
 
        self.assertRaises(IndexError, getitem, view, -3)
228
 
 
229
 
 
230
 
    def test_negativeStartOnlyPositiveIndices(self):
231
 
        """
232
 
        Similar to L{test_positiveStartOnlyPositiveIndices}, but for the case
233
 
        of a negative slice start value.
234
 
        """
235
 
        sequence = ['a', 'b', 'c']
236
 
        view = SlicedView(sequence, slice(-2, None))
237
 
        self.assertEqual(view[0], 'b')
238
 
        self.assertEqual(view[1], 'c')
239
 
        self.assertRaises(IndexError, getitem, view, 2)
240
 
 
241
 
 
242
 
    def test_negativeStartOnlyNegativeIndices(self):
243
 
        """
244
 
        Similar to L{test_negativeStartOnlyPositiveIndices}, but cover negative
245
 
        requested indices instead of positive ones.
246
 
        """
247
 
        sequence = ['a', 'b', 'c']
248
 
        view = SlicedView(sequence, slice(-2, None))
249
 
        self.assertEqual(view[-1], 'c')
250
 
        self.assertEqual(view[-2], 'b')
251
 
        self.assertRaises(IndexError, getitem, view, -3)
252
 
 
253
 
 
254
 
    def test_positiveStopOnlyPositiveIndices(self):
255
 
        """
256
 
        Verify that L{__getitem__} of L{SlicedView} constructed with a slice
257
 
        with a positive stop value returns elements at the requested index for
258
 
        indices less than the stop value and raises IndexError for positive
259
 
        requested indices greater than or equal to the stop value.
260
 
        """
261
 
        sequence = ['a', 'b', 'c']
262
 
        view = SlicedView(sequence, slice(None, 2))
263
 
        self.assertEqual(view[0], 'a')
264
 
        self.assertEqual(view[1], 'b')
265
 
        self.assertRaises(IndexError, getitem, view, 2)
266
 
 
267
 
 
268
 
    def test_positveStopOnlyNegativeIndices(self):
269
 
        """
270
 
        Similar to L{test_positiveStopOnlyPositiveIndices}, but cover negative
271
 
        requested indices instead of positive ones.
272
 
        """
273
 
        sequence = ['a', 'b', 'c']
274
 
        view = SlicedView(sequence, slice(None, 2))
275
 
        self.assertEqual(view[-1], 'b')
276
 
        self.assertEqual(view[-2], 'a')
277
 
        self.assertRaises(IndexError, getitem, view, -3)
278
 
 
279
 
 
280
 
    def test_negativeStopOnlyPositiveIndices(self):
281
 
        """
282
 
        Similar to L{test_positiveStopOnlyPositiveIndices}, but test a
283
 
        L{SlicedView} created with a slice with a negative stop value.
284
 
        """
285
 
        sequence = ['a', 'b', 'c']
286
 
        view = SlicedView(sequence, slice(None, -1))
287
 
        self.assertEqual(view[0], 'a')
288
 
        self.assertEqual(view[1], 'b')
289
 
        self.assertRaises(IndexError, getitem, view, 2)
290
 
 
291
 
 
292
 
    def test_negativeStopOnlyNegativeIndices(self):
293
 
        """
294
 
        Similar to L{test_negativeStopOnlyPositiveIndices}, but cover negative
295
 
        requested indices instead of positive ones.
296
 
        """
297
 
        sequence = ['a', 'b', 'c']
298
 
        view = SlicedView(sequence, slice(None, -1))
299
 
        self.assertEqual(view[-1], 'b')
300
 
        self.assertEqual(view[-2], 'a')
301
 
        self.assertRaises(IndexError, getitem, view, -3)
302
 
 
303
 
 
304
 
    def test_positiveStartPositiveStopPositiveIndices(self):
305
 
        """
306
 
        Verify that L{__getitem__} of L{SlicedView} constructed with a slice
307
 
        with positive start and stop values returns elements at the requested
308
 
        index plus the slice's start value for positive requested indices less
309
 
        than the difference between the stop and start values and raises
310
 
        IndexError for positive requested indices greater than or equal to that
311
 
        difference.
312
 
        """
313
 
        sequence = ['a', 'b', 'c', 'd']
314
 
        view = SlicedView(sequence, slice(1, 3))
315
 
        self.assertEqual(view[0], 'b')
316
 
        self.assertEqual(view[1], 'c')
317
 
        self.assertRaises(IndexError, getitem, view, 2)
318
 
 
319
 
 
320
 
    def test_positiveStartPositiveStopNegativeIndices(self):
321
 
        """
322
 
        Similar to L{test_positiveStartPositiveStopPositiveIndices}, but cover
323
 
        negative requested indices instead of positive ones.
324
 
        """
325
 
        sequence = ['a', 'b', 'c', 'd']
326
 
        view = SlicedView(sequence, slice(1, 3))
327
 
        self.assertEqual(view[-1], 'c')
328
 
        self.assertEqual(view[-2], 'b')
329
 
        self.assertRaises(IndexError, getitem, view, -3)
330
 
 
331
 
 
332
 
    def test_positiveStartNegativeStopPositiveIndices(self):
333
 
        """
334
 
        Verify that L{__getitem__} of a L{SlicedView} constructed with a slice
335
 
        with a positive start and a negative stop value returns elements at the
336
 
        requested index plus the slice's start value for positive requested
337
 
        indices within the bounds defined by the stop value and raises an
338
 
        IndexError for positive requested indices outside those bounds.
339
 
        """
340
 
        sequence = ['a', 'b', 'c']
341
 
        view = SlicedView(sequence, slice(1, -1))
342
 
        self.assertEqual(view[0], 'b')
343
 
        self.assertRaises(IndexError, getitem, view, 1)
344
 
 
345
 
 
346
 
    def test_positiveStartNegativeStopNegativeIndices(self):
347
 
        """
348
 
        Similar to L{test_positiveStartNegativeStopPositiveIndices}, but cover
349
 
        negative requested indices instead of positive ones.
350
 
        """
351
 
        sequence = ['a', 'b', 'c']
352
 
        view = SlicedView(sequence, slice(1, -1))
353
 
        self.assertEqual(view[-1], 'b')
354
 
        self.assertRaises(IndexError, getitem, view, -2)
355
 
 
356
 
 
357
 
    def test_negativeStartPositiveStopPositiveIndices(self):
358
 
        """
359
 
        Similar to L{test_positiveStartNegativeStopPositiveIndices}, but for a
360
 
        negative slice start and positive slice stop.
361
 
        """
362
 
        sequence = ['a', 'b', 'c']
363
 
        view = SlicedView(sequence, slice(-2, 2))
364
 
        self.assertEqual(view[0], 'b')
365
 
        self.assertRaises(IndexError, getitem, view, 1)
366
 
 
367
 
 
368
 
    def test_negativeStartPositiveStopNegativeIndices(self):
369
 
        """
370
 
        Similar to L{test_negativeStartPositiveStopPositiveIndices}, but cover
371
 
        negative requested indices instead of positive ones.
372
 
        """
373
 
        sequence = ['a', 'b', 'c']
374
 
        view = SlicedView(sequence, slice(-2, 2))
375
 
        self.assertEqual(view[-1], 'b')
376
 
        self.assertRaises(IndexError, getitem, view, -2)
377
 
 
378
 
 
379
 
    def test_negativeStartNegativeStopPositiveIndices(self):
380
 
        """
381
 
        Similar to L{test_negativeStartPositiveStopPositiveIndices}, but for a
382
 
        negative slice stop.
383
 
        """
384
 
        sequence = ['a', 'b', 'c']
385
 
        view = SlicedView(sequence, slice(-2, -1))
386
 
        self.assertEqual(view[0], 'b')
387
 
        self.assertRaises(IndexError, getitem, view, 1)
388
 
 
389
 
 
390
 
    def test_negativeStartNegativeStopNegativeIndices(self):
391
 
        """
392
 
        Similar to L{test_negativeStartPositiveStopPositiveIndices}, but cover
393
 
        negative requested indices instead of positive ones.
394
 
        """
395
 
        sequence = ['a', 'b', 'c']
396
 
        view = SlicedView(sequence, slice(-2, -1))
397
 
        self.assertEqual(view[-1], 'b')
398
 
        self.assertRaises(IndexError, getitem, view, -2)
399
 
 
400
 
 
401
 
    def test_positiveStepPositiveIndices(self):
402
 
        """
403
 
        Verify that a positive step produces the correct results, skipping over
404
 
        the appropriate elements.
405
 
        """
406
 
        sequence = ['a', 'b', 'c', 'd', 'e']
407
 
        view = SlicedView(sequence, slice(1, -1, 2))
408
 
        self.assertEqual(view[0], 'b')
409
 
        self.assertEqual(view[1], 'd')
410
 
        self.assertRaises(IndexError, getitem, view, 2)
411
 
 
412
 
 
413
 
    def test_positiveStepNegativeIndices(self):
414
 
        """
415
 
        Verify that a negative step produces the correct results, skipping over
416
 
        the appropriate elements.
417
 
        """
418
 
        sequence = ['a', 'b', 'c', 'd', 'e']
419
 
        view = SlicedView(sequence, slice(1, -1, 2))
420
 
        self.assertEqual(view[-1], 'd')
421
 
        self.assertEqual(view[-2], 'b')
422
 
        self.assertRaises(IndexError, getitem, view, -3)
423
 
 
424
 
 
425
 
    def test_negativeStepPositiveIndices(self):
426
 
        """
427
 
        Verify that a negative step produces the correct results, skipping over
428
 
        the appropriate elements.
429
 
        """
430
 
        sequence = ['a', 'b', 'c', 'd', 'e']
431
 
        view = SlicedView(sequence, slice(-1, 1, -2))
432
 
        self.assertEqual(view[0], 'e')
433
 
        self.assertEqual(view[1], 'c')
434
 
        self.assertRaises(IndexError, getitem, view, 2)
435
 
 
436
 
 
437
 
    def test_negativeStepNegativeIndices(self):
438
 
        """
439
 
        Verify that a negative step produces the correct results, skipping over
440
 
        the appropriate elements.
441
 
        """
442
 
        sequence = ['a', 'b', 'c', 'd', 'e']
443
 
        view = SlicedView(sequence, slice(-1, 1, -2))
444
 
        self.assertEqual(view[-1], 'c')
445
 
        self.assertEqual(view[-2], 'e')
446
 
        self.assertRaises(IndexError, getitem, view, -3)
447
 
 
448
 
 
449
 
    def test_slice(self):
450
 
        """
451
 
        Verify a L{SlicedView} itself can be sliced.
452
 
        """
453
 
        sequence = ['a', 'b', 'c']
454
 
        view = SlicedView(sequence, slice(1, None))
455
 
        viewView = view[1:]
456
 
        self.assertIdentical(viewView.sequence, view)
457
 
        self.assertEqual(viewView.bounds, slice(1, None, None))