annotate Lib/test/test_index.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
1 import unittest
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
2 from test import test_support
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
3 import operator
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
4 from sys import maxint
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39507
diff changeset
5 maxsize = test_support.MAX_Py_ssize_t
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39507
diff changeset
6 minsize = -maxsize-1
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
7
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
8 class oldstyle:
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
9 def __index__(self):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
10 return self.ind
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
11
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
12 class newstyle(object):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
13 def __index__(self):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
14 return self.ind
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
15
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
16 class TrapInt(int):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
17 def __index__(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
18 return self
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
19
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
20 class TrapLong(long):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
21 def __index__(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
22 return self
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
23
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
24 class BaseTestCase(unittest.TestCase):
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
25 def setUp(self):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
26 self.o = oldstyle()
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
27 self.n = newstyle()
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
28
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
29 def test_basic(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
30 self.o.ind = -2
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
31 self.n.ind = 2
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
32 self.assertEqual(operator.index(self.o), -2)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
33 self.assertEqual(operator.index(self.n), 2)
39503
149de7256864 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39464
diff changeset
34
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
35 def test_slice(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
36 self.o.ind = 1
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
37 self.n.ind = 2
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
38 slc = slice(self.o, self.o, self.o)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
39 check_slc = slice(1, 1, 1)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
40 self.assertEqual(slc.indices(self.o), check_slc.indices(1))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
41 slc = slice(self.n, self.n, self.n)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
42 check_slc = slice(2, 2, 2)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
43 self.assertEqual(slc.indices(self.n), check_slc.indices(2))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
44
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
45 def test_wrappers(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
46 self.o.ind = 4
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
47 self.n.ind = 5
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
48 self.assertEqual(6 .__index__(), 6)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
49 self.assertEqual(-7L.__index__(), -7)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
50 self.assertEqual(self.o.__index__(), 4)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
51 self.assertEqual(self.n.__index__(), 5)
60082
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59726
diff changeset
52 self.assertEqual(True.__index__(), 1)
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59726
diff changeset
53 self.assertEqual(False.__index__(), 0)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
54
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
55 def test_subclasses(self):
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
56 r = range(10)
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
57 self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
58 self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
59 self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
60 self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
39503
149de7256864 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39464
diff changeset
61
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
62 def test_error(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
63 self.o.ind = 'dumb'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
64 self.n.ind = 'bad'
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
65 self.assertRaises(TypeError, operator.index, self.o)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
66 self.assertRaises(TypeError, operator.index, self.n)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
67 self.assertRaises(TypeError, slice(self.o).indices, 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
68 self.assertRaises(TypeError, slice(self.n).indices, 0)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
69
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
70
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
71 class SeqTestCase(unittest.TestCase):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
72 # This test case isn't run directly. It just defines common tests
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
73 # to the different sequence types below
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
74 def setUp(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
75 self.o = oldstyle()
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
76 self.n = newstyle()
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
77 self.o2 = oldstyle()
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
78 self.n2 = newstyle()
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
79
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
80 def test_index(self):
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
81 self.o.ind = -2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
82 self.n.ind = 2
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
83 self.assertEqual(self.seq[self.n], self.seq[2])
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
84 self.assertEqual(self.seq[self.o], self.seq[-2])
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
85
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
86 def test_slice(self):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
87 self.o.ind = 1
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
88 self.o2.ind = 3
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
89 self.n.ind = 2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
90 self.n2.ind = 4
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
91 self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
92 self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
93
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
94 def test_slice_bug7532(self):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
95 seqlen = len(self.seq)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
96 self.o.ind = int(seqlen * 1.5)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
97 self.n.ind = seqlen + 2
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
98 self.assertEqual(self.seq[self.o:], self.seq[0:0])
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
99 self.assertEqual(self.seq[:self.o], self.seq)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
100 self.assertEqual(self.seq[self.n:], self.seq[0:0])
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
101 self.assertEqual(self.seq[:self.n], self.seq)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
102 if isinstance(self.seq, ClassicSeq):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
103 return
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
104 # These tests fail for ClassicSeq (see bug #7532)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
105 self.o2.ind = -seqlen - 2
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
106 self.n2.ind = -int(seqlen * 1.5)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
107 self.assertEqual(self.seq[self.o2:], self.seq)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
108 self.assertEqual(self.seq[:self.o2], self.seq[0:0])
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
109 self.assertEqual(self.seq[self.n2:], self.seq)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
110 self.assertEqual(self.seq[:self.n2], self.seq[0:0])
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
111
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
112 def test_repeat(self):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
113 self.o.ind = 3
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
114 self.n.ind = 2
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
115 self.assertEqual(self.seq * self.o, self.seq * 3)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
116 self.assertEqual(self.seq * self.n, self.seq * 2)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
117 self.assertEqual(self.o * self.seq, self.seq * 3)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
118 self.assertEqual(self.n * self.seq, self.seq * 2)
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
119
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
120 def test_wrappers(self):
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
121 self.o.ind = 4
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
122 self.n.ind = 5
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
123 self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
124 self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
125 self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
126 self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
127 self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
128 self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
129
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
130 def test_subclasses(self):
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
131 self.assertEqual(self.seq[TrapInt()], self.seq[0])
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
132 self.assertEqual(self.seq[TrapLong()], self.seq[0])
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
133
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
134 def test_error(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
135 self.o.ind = 'dumb'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
136 self.n.ind = 'bad'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
137 indexobj = lambda x, obj: obj.seq[x]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
138 self.assertRaises(TypeError, indexobj, self.o, self)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
139 self.assertRaises(TypeError, indexobj, self.n, self)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
140 sliceobj = lambda x, obj: obj.seq[x:]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
141 self.assertRaises(TypeError, sliceobj, self.o, self)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
142 self.assertRaises(TypeError, sliceobj, self.n, self)
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
143
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
144
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
145 class ListTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
146 seq = [0,10,20,30,40,50]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
147
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
148 def test_setdelitem(self):
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
149 self.o.ind = -2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
150 self.n.ind = 2
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
151 lst = list('ab!cdefghi!j')
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
152 del lst[self.o]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
153 del lst[self.n]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
154 lst[self.o] = 'X'
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
155 lst[self.n] = 'Y'
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
156 self.assertEqual(lst, list('abYdefghXj'))
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
157
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
158 lst = [5, 6, 7, 8, 9, 10, 11]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
159 lst.__setitem__(self.n, "here")
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
160 self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
161 lst.__delitem__(self.n)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
162 self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
163
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
164 def test_inplace_repeat(self):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
165 self.o.ind = 2
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
166 self.n.ind = 3
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
167 lst = [6, 4]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
168 lst *= self.o
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
169 self.assertEqual(lst, [6, 4, 6, 4])
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
170 lst *= self.n
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
171 self.assertEqual(lst, [6, 4, 6, 4] * 3)
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
172
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
173 lst = [5, 6, 7, 8, 9, 11]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
174 l2 = lst.__imul__(self.n)
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
175 self.assertIs(l2, lst)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
176 self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
177
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
178
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
179 class _BaseSeq:
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
180
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
181 def __init__(self, iterable):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
182 self._list = list(iterable)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
183
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
184 def __repr__(self):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
185 return repr(self._list)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
186
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
187 def __eq__(self, other):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
188 return self._list == other
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
189
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
190 def __len__(self):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
191 return len(self._list)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
192
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
193 def __mul__(self, n):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
194 return self.__class__(self._list*n)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
195 __rmul__ = __mul__
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
196
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
197 def __getitem__(self, index):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
198 return self._list[index]
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
199
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
200
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
201 class _GetSliceMixin:
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
202
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
203 def __getslice__(self, i, j):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
204 return self._list.__getslice__(i, j)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
205
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
206
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
207 class ClassicSeq(_BaseSeq): pass
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
208 class NewSeq(_BaseSeq, object): pass
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
209 class ClassicSeqDeprecated(_GetSliceMixin, ClassicSeq): pass
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
210 class NewSeqDeprecated(_GetSliceMixin, NewSeq): pass
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
211
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
212
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
213 class TupleTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
214 seq = (0,10,20,30,40,50)
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
215
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
216 class StringTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
217 seq = "this is a test"
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
218
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
219 class ByteArrayTestCase(SeqTestCase):
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
220 seq = bytearray("this is a test")
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
221
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
222 class UnicodeTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
223 seq = u"this is a test"
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
224
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
225 class ClassicSeqTestCase(SeqTestCase):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
226 seq = ClassicSeq((0,10,20,30,40,50))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
227
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
228 class NewSeqTestCase(SeqTestCase):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
229 seq = NewSeq((0,10,20,30,40,50))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
230
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
231 class ClassicSeqDeprecatedTestCase(SeqTestCase):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
232 seq = ClassicSeqDeprecated((0,10,20,30,40,50))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
233
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
234 class NewSeqDeprecatedTestCase(SeqTestCase):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
235 seq = NewSeqDeprecated((0,10,20,30,40,50))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
236
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
237
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
238 class XRangeTestCase(unittest.TestCase):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
239
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
240 def test_xrange(self):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
241 n = newstyle()
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
242 n.ind = 5
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
243 self.assertEqual(xrange(1, 20)[n], 6)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
244 self.assertEqual(xrange(1, 20).__getitem__(n), 6)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
245
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
246 class OverflowTestCase(unittest.TestCase):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
247
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
248 def setUp(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
249 self.pos = 2**100
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
250 self.neg = -self.pos
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
251
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
252 def test_large_longs(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
253 self.assertEqual(self.pos.__index__(), self.pos)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
254 self.assertEqual(self.neg.__index__(), self.neg)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
255
39464
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
256 def _getitem_helper(self, base):
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
257 class GetItem(base):
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
258 def __len__(self):
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
259 return maxint # cannot return long here
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
260 def __getitem__(self, key):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
261 return key
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
262 x = GetItem()
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
263 self.assertEqual(x[self.pos], self.pos)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
264 self.assertEqual(x[self.neg], self.neg)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
265 self.assertEqual(x[self.neg:self.pos].indices(maxsize),
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
266 (0, maxsize, 1))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
267 self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
268 (0, maxsize, 1))
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
269
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
270 def _getslice_helper_deprecated(self, base):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
271 class GetItem(base):
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
272 def __len__(self):
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
273 return maxint # cannot return long here
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
274 def __getitem__(self, key):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
275 return key
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
276 def __getslice__(self, i, j):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
277 return i, j
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
278 x = GetItem()
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
279 self.assertEqual(x[self.pos], self.pos)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
280 self.assertEqual(x[self.neg], self.neg)
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39507
diff changeset
281 self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
282 self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
283 (0, maxsize, 1))
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
284
39464
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
285 def test_getitem(self):
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
286 self._getitem_helper(object)
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58220
diff changeset
287 with test_support.check_py3k_warnings():
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
288 self._getslice_helper_deprecated(object)
39464
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
289
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
290 def test_getitem_classic(self):
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
291 class Empty: pass
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
292 # XXX This test fails (see bug #7532)
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
293 #self._getitem_helper(Empty)
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58220
diff changeset
294 with test_support.check_py3k_warnings():
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
295 self._getslice_helper_deprecated(Empty)
39464
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
296
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
297 def test_sequence_repeat(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
298 self.assertRaises(OverflowError, lambda: "a" * self.pos)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
299 self.assertRaises(OverflowError, lambda: "a" * self.neg)
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
300
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
301
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
302 def test_main():
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
303 test_support.run_unittest(
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
304 BaseTestCase,
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
305 ListTestCase,
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
306 TupleTestCase,
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59432
diff changeset
307 ByteArrayTestCase,
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
308 StringTestCase,
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
309 UnicodeTestCase,
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
310 ClassicSeqTestCase,
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
311 NewSeqTestCase,
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
312 XRangeTestCase,
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
313 OverflowTestCase,
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
314 )
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58220
diff changeset
315 with test_support.check_py3k_warnings():
58220
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
316 test_support.run_unittest(
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
317 ClassicSeqDeprecatedTestCase,
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
318 NewSeqDeprecatedTestCase,
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
319 )
4675f11fb125 Issue #7532: Add additional slicing test cases for new- and old-style
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
320
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
321
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
322 if __name__ == "__main__":
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
323 test_main()