ยปCore Development>Code coverage>Lib/test/test_index.py

Python code coverage for Lib/test/test_index.py

#countcontent
1n/aimport unittest
2n/afrom test import support
3n/aimport operator
4n/amaxsize = support.MAX_Py_ssize_t
5n/a
6n/aclass newstyle:
7n/a def __index__(self):
8n/a return self.ind
9n/a
10n/aclass TrapInt(int):
11n/a def __index__(self):
12n/a return int(self)
13n/a
14n/aclass BaseTestCase(unittest.TestCase):
15n/a def setUp(self):
16n/a self.o = newstyle()
17n/a self.n = newstyle()
18n/a
19n/a def test_basic(self):
20n/a self.o.ind = -2
21n/a self.n.ind = 2
22n/a self.assertEqual(operator.index(self.o), -2)
23n/a self.assertEqual(operator.index(self.n), 2)
24n/a
25n/a def test_slice(self):
26n/a self.o.ind = 1
27n/a self.n.ind = 2
28n/a slc = slice(self.o, self.o, self.o)
29n/a check_slc = slice(1, 1, 1)
30n/a self.assertEqual(slc.indices(self.o), check_slc.indices(1))
31n/a slc = slice(self.n, self.n, self.n)
32n/a check_slc = slice(2, 2, 2)
33n/a self.assertEqual(slc.indices(self.n), check_slc.indices(2))
34n/a
35n/a def test_wrappers(self):
36n/a self.o.ind = 4
37n/a self.n.ind = 5
38n/a self.assertEqual(6 .__index__(), 6)
39n/a self.assertEqual(-7 .__index__(), -7)
40n/a self.assertEqual(self.o.__index__(), 4)
41n/a self.assertEqual(self.n.__index__(), 5)
42n/a self.assertEqual(True.__index__(), 1)
43n/a self.assertEqual(False.__index__(), 0)
44n/a
45n/a def test_subclasses(self):
46n/a r = list(range(10))
47n/a self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
48n/a self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
49n/a
50n/a def test_error(self):
51n/a self.o.ind = 'dumb'
52n/a self.n.ind = 'bad'
53n/a self.assertRaises(TypeError, operator.index, self.o)
54n/a self.assertRaises(TypeError, operator.index, self.n)
55n/a self.assertRaises(TypeError, slice(self.o).indices, 0)
56n/a self.assertRaises(TypeError, slice(self.n).indices, 0)
57n/a
58n/a def test_int_subclass_with_index(self):
59n/a # __index__ should be used when computing indices, even for int
60n/a # subclasses. See issue #17576.
61n/a class MyInt(int):
62n/a def __index__(self):
63n/a return int(self) + 1
64n/a
65n/a my_int = MyInt(7)
66n/a direct_index = my_int.__index__()
67n/a operator_index = operator.index(my_int)
68n/a self.assertEqual(direct_index, 8)
69n/a self.assertEqual(operator_index, 7)
70n/a # Both results should be of exact type int.
71n/a self.assertIs(type(direct_index), int)
72n/a #self.assertIs(type(operator_index), int)
73n/a
74n/a def test_index_returns_int_subclass(self):
75n/a class BadInt:
76n/a def __index__(self):
77n/a return True
78n/a
79n/a class BadInt2(int):
80n/a def __index__(self):
81n/a return True
82n/a
83n/a bad_int = BadInt()
84n/a with self.assertWarns(DeprecationWarning):
85n/a n = operator.index(bad_int)
86n/a self.assertEqual(n, 1)
87n/a
88n/a bad_int = BadInt2()
89n/a n = operator.index(bad_int)
90n/a self.assertEqual(n, 0)
91n/a
92n/a
93n/aclass SeqTestCase:
94n/a # This test case isn't run directly. It just defines common tests
95n/a # to the different sequence types below
96n/a def setUp(self):
97n/a self.o = newstyle()
98n/a self.n = newstyle()
99n/a self.o2 = newstyle()
100n/a self.n2 = newstyle()
101n/a
102n/a def test_index(self):
103n/a self.o.ind = -2
104n/a self.n.ind = 2
105n/a self.assertEqual(self.seq[self.n], self.seq[2])
106n/a self.assertEqual(self.seq[self.o], self.seq[-2])
107n/a
108n/a def test_slice(self):
109n/a self.o.ind = 1
110n/a self.o2.ind = 3
111n/a self.n.ind = 2
112n/a self.n2.ind = 4
113n/a self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
114n/a self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
115n/a
116n/a def test_slice_bug7532(self):
117n/a seqlen = len(self.seq)
118n/a self.o.ind = int(seqlen * 1.5)
119n/a self.n.ind = seqlen + 2
120n/a self.assertEqual(self.seq[self.o:], self.seq[0:0])
121n/a self.assertEqual(self.seq[:self.o], self.seq)
122n/a self.assertEqual(self.seq[self.n:], self.seq[0:0])
123n/a self.assertEqual(self.seq[:self.n], self.seq)
124n/a self.o2.ind = -seqlen - 2
125n/a self.n2.ind = -int(seqlen * 1.5)
126n/a self.assertEqual(self.seq[self.o2:], self.seq)
127n/a self.assertEqual(self.seq[:self.o2], self.seq[0:0])
128n/a self.assertEqual(self.seq[self.n2:], self.seq)
129n/a self.assertEqual(self.seq[:self.n2], self.seq[0:0])
130n/a
131n/a def test_repeat(self):
132n/a self.o.ind = 3
133n/a self.n.ind = 2
134n/a self.assertEqual(self.seq * self.o, self.seq * 3)
135n/a self.assertEqual(self.seq * self.n, self.seq * 2)
136n/a self.assertEqual(self.o * self.seq, self.seq * 3)
137n/a self.assertEqual(self.n * self.seq, self.seq * 2)
138n/a
139n/a def test_wrappers(self):
140n/a self.o.ind = 4
141n/a self.n.ind = 5
142n/a self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
143n/a self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
144n/a self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
145n/a self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
146n/a self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
147n/a self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
148n/a
149n/a def test_subclasses(self):
150n/a self.assertEqual(self.seq[TrapInt()], self.seq[0])
151n/a
152n/a def test_error(self):
153n/a self.o.ind = 'dumb'
154n/a self.n.ind = 'bad'
155n/a indexobj = lambda x, obj: obj.seq[x]
156n/a self.assertRaises(TypeError, indexobj, self.o, self)
157n/a self.assertRaises(TypeError, indexobj, self.n, self)
158n/a sliceobj = lambda x, obj: obj.seq[x:]
159n/a self.assertRaises(TypeError, sliceobj, self.o, self)
160n/a self.assertRaises(TypeError, sliceobj, self.n, self)
161n/a
162n/a
163n/aclass ListTestCase(SeqTestCase, unittest.TestCase):
164n/a seq = [0,10,20,30,40,50]
165n/a
166n/a def test_setdelitem(self):
167n/a self.o.ind = -2
168n/a self.n.ind = 2
169n/a lst = list('ab!cdefghi!j')
170n/a del lst[self.o]
171n/a del lst[self.n]
172n/a lst[self.o] = 'X'
173n/a lst[self.n] = 'Y'
174n/a self.assertEqual(lst, list('abYdefghXj'))
175n/a
176n/a lst = [5, 6, 7, 8, 9, 10, 11]
177n/a lst.__setitem__(self.n, "here")
178n/a self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
179n/a lst.__delitem__(self.n)
180n/a self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
181n/a
182n/a def test_inplace_repeat(self):
183n/a self.o.ind = 2
184n/a self.n.ind = 3
185n/a lst = [6, 4]
186n/a lst *= self.o
187n/a self.assertEqual(lst, [6, 4, 6, 4])
188n/a lst *= self.n
189n/a self.assertEqual(lst, [6, 4, 6, 4] * 3)
190n/a
191n/a lst = [5, 6, 7, 8, 9, 11]
192n/a l2 = lst.__imul__(self.n)
193n/a self.assertIs(l2, lst)
194n/a self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
195n/a
196n/a
197n/aclass NewSeq:
198n/a
199n/a def __init__(self, iterable):
200n/a self._list = list(iterable)
201n/a
202n/a def __repr__(self):
203n/a return repr(self._list)
204n/a
205n/a def __eq__(self, other):
206n/a return self._list == other
207n/a
208n/a def __len__(self):
209n/a return len(self._list)
210n/a
211n/a def __mul__(self, n):
212n/a return self.__class__(self._list*n)
213n/a __rmul__ = __mul__
214n/a
215n/a def __getitem__(self, index):
216n/a return self._list[index]
217n/a
218n/a
219n/aclass TupleTestCase(SeqTestCase, unittest.TestCase):
220n/a seq = (0,10,20,30,40,50)
221n/a
222n/aclass ByteArrayTestCase(SeqTestCase, unittest.TestCase):
223n/a seq = bytearray(b"this is a test")
224n/a
225n/aclass BytesTestCase(SeqTestCase, unittest.TestCase):
226n/a seq = b"this is a test"
227n/a
228n/aclass StringTestCase(SeqTestCase, unittest.TestCase):
229n/a seq = "this is a test"
230n/a
231n/aclass NewSeqTestCase(SeqTestCase, unittest.TestCase):
232n/a seq = NewSeq((0,10,20,30,40,50))
233n/a
234n/a
235n/a
236n/aclass RangeTestCase(unittest.TestCase):
237n/a
238n/a def test_range(self):
239n/a n = newstyle()
240n/a n.ind = 5
241n/a self.assertEqual(range(1, 20)[n], 6)
242n/a self.assertEqual(range(1, 20).__getitem__(n), 6)
243n/a
244n/a
245n/aclass OverflowTestCase(unittest.TestCase):
246n/a
247n/a def setUp(self):
248n/a self.pos = 2**100
249n/a self.neg = -self.pos
250n/a
251n/a def test_large_longs(self):
252n/a self.assertEqual(self.pos.__index__(), self.pos)
253n/a self.assertEqual(self.neg.__index__(), self.neg)
254n/a
255n/a def test_getitem(self):
256n/a class GetItem:
257n/a def __len__(self):
258n/a assert False, "__len__ should not be invoked"
259n/a def __getitem__(self, key):
260n/a return key
261n/a x = GetItem()
262n/a self.assertEqual(x[self.pos], self.pos)
263n/a self.assertEqual(x[self.neg], self.neg)
264n/a self.assertEqual(x[self.neg:self.pos].indices(maxsize),
265n/a (0, maxsize, 1))
266n/a self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
267n/a (0, maxsize, 1))
268n/a
269n/a def test_sequence_repeat(self):
270n/a self.assertRaises(OverflowError, lambda: "a" * self.pos)
271n/a self.assertRaises(OverflowError, lambda: "a" * self.neg)
272n/a
273n/a
274n/aif __name__ == "__main__":
275n/a unittest.main()