annotate Lib/test/test_index.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 1d84bf5f9478
children 7c3b849d181a 6768b2350c57
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)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
52
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
53 def test_subclasses(self):
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
54 r = range(10)
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
55 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
56 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
57 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
58 self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
39503
149de7256864 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39464
diff changeset
59
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
60 def test_error(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
61 self.o.ind = 'dumb'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
62 self.n.ind = 'bad'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
63 self.failUnlessRaises(TypeError, operator.index, self.o)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
64 self.failUnlessRaises(TypeError, operator.index, self.n)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
65 self.failUnlessRaises(TypeError, slice(self.o).indices, 0)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
66 self.failUnlessRaises(TypeError, slice(self.n).indices, 0)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
67
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
68
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
69 class SeqTestCase(unittest.TestCase):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
70 # 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
71 # to the different sequence types below
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
72 def setUp(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
73 self.o = oldstyle()
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
74 self.n = newstyle()
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
75 self.o2 = oldstyle()
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
76 self.n2 = newstyle()
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
77
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
78 def test_index(self):
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
79 self.o.ind = -2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
80 self.n.ind = 2
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
81 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
82 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
83
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
84 def test_slice(self):
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
85 self.o.ind = 1
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
86 self.o2.ind = 3
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
87 self.n.ind = 2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
88 self.n2.ind = 4
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
89 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
90 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
91
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
92 def test_repeat(self):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
93 self.o.ind = 3
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
94 self.n.ind = 2
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
95 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
96 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
97 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
98 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
99
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
100 def test_wrappers(self):
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
101 self.o.ind = 4
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
102 self.n.ind = 5
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
103 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
104 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
105 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
106 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
107 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
108 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
109
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
110 def test_subclasses(self):
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39503
diff changeset
111 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
112 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
113
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
114 def test_error(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
115 self.o.ind = 'dumb'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
116 self.n.ind = 'bad'
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
117 indexobj = lambda x, obj: obj.seq[x]
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
118 self.failUnlessRaises(TypeError, indexobj, self.o, self)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
119 self.failUnlessRaises(TypeError, indexobj, self.n, self)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
120 sliceobj = lambda x, obj: obj.seq[x:]
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
121 self.failUnlessRaises(TypeError, sliceobj, self.o, self)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
122 self.failUnlessRaises(TypeError, sliceobj, self.n, self)
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
123
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
124
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
125 class ListTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
126 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
127
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
128 def test_setdelitem(self):
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
129 self.o.ind = -2
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
130 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
131 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
132 del lst[self.o]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
133 del lst[self.n]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
134 lst[self.o] = 'X'
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
135 lst[self.n] = 'Y'
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
136 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
137
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
138 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
139 lst.__setitem__(self.n, "here")
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
140 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
141 lst.__delitem__(self.n)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
142 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
143
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
144 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
145 self.o.ind = 2
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
146 self.n.ind = 3
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
147 lst = [6, 4]
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
148 lst *= self.o
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
149 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
150 lst *= self.n
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
151 self.assertEqual(lst, [6, 4, 6, 4] * 3)
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37302
diff changeset
152
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
153 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
154 l2 = lst.__imul__(self.n)
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
155 self.assert_(l2 is lst)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
156 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
157
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
158
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
159 class TupleTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
160 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
161
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
162 class StringTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
163 seq = "this is a test"
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
164
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
165 class UnicodeTestCase(SeqTestCase):
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
166 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
167
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
168
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
169 class XRangeTestCase(unittest.TestCase):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
170
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
171 def test_xrange(self):
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
172 n = newstyle()
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
173 n.ind = 5
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
174 self.assertEqual(xrange(1, 20)[n], 6)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
175 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
176
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
177 class OverflowTestCase(unittest.TestCase):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
178
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
179 def setUp(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
180 self.pos = 2**100
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
181 self.neg = -self.pos
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
182
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
183 def test_large_longs(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
184 self.assertEqual(self.pos.__index__(), self.pos)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
185 self.assertEqual(self.neg.__index__(), self.neg)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
186
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
187 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
188 class GetItem(base):
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
189 def __len__(self):
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
190 return maxint #cannot return long here
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
191 def __getitem__(self, key):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
192 return key
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
193 def __getslice__(self, i, j):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
194 return i, j
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
195 x = GetItem()
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
196 self.assertEqual(x[self.pos], self.pos)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
197 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
198 self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
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
199 self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1))
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
200
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
201 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
202 self._getitem_helper(object)
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
203
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
204 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
205 class Empty: pass
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
206 self._getitem_helper(Empty)
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
207
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
208 def test_sequence_repeat(self):
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
209 self.failUnlessRaises(OverflowError, lambda: "a" * self.pos)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
210 self.failUnlessRaises(OverflowError, lambda: "a" * self.neg)
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
211
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
212
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
213 def test_main():
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
214 test_support.run_unittest(
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
215 BaseTestCase,
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
216 ListTestCase,
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
217 TupleTestCase,
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
218 StringTestCase,
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
219 UnicodeTestCase,
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37307
diff changeset
220 XRangeTestCase,
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37308
diff changeset
221 OverflowTestCase,
37302
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
222 )
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
223
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
224 if __name__ == "__main__":
fb7b52c4116c Checking in the test for PEP 357.
Armin Rigo <arigo@tunes.org>
parents: 36882
diff changeset
225 test_main()