test_index.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import unittest
  2. from test import test_support
  3. import operator
  4. #from sys import maxint
  5. #maxsize = test_support.MAX_Py_ssize_t
  6. #minsize = -maxsize-1
  7. class oldstyle:
  8. def __index__(self):
  9. return self.ind
  10. # class newstyle(object):
  11. # def __index__(self):
  12. # return self.ind
  13. class TrapInt(int):
  14. def __index__(self):
  15. return self
  16. class TrapLong(long):
  17. def __index__(self):
  18. return self
  19. class BaseTestCase(unittest.TestCase):
  20. def setUp(self):
  21. self.o = oldstyle()
  22. #self.n = newstyle()
  23. def test_basic(self):
  24. self.o.ind = -2
  25. #self.n.ind = 2
  26. self.assertEqual(operator.index(self.o), -2)
  27. #self.assertEqual(operator.index(self.n), 2)
  28. def test_slice(self):
  29. self.o.ind = 1
  30. #self.n.ind = 2
  31. slc = slice(self.o, self.o, self.o)
  32. check_slc = slice(1, 1, 1)
  33. self.assertEqual(slc.indices(self.o), check_slc.indices(1))
  34. #slc = slice(self.n, self.n, self.n)
  35. #check_slc = slice(2, 2, 2)
  36. #self.assertEqual(slc.indices(self.n), check_slc.indices(2))
  37. def test_wrappers(self):
  38. self.o.ind = 4
  39. #self.n.ind = 5
  40. #self.assertEqual(6.__index__(), 6)
  41. #self.assertEqual(-7L.__index__(), -7)
  42. self.assertEqual(self.o.__index__(), 4)
  43. #self.assertEqual(self.n.__index__(), 5)
  44. #self.assertEqual(True.__index__(), 1)
  45. #self.assertEqual(False.__index__(), 0)
  46. def test_subclasses(self):
  47. r = range(10)
  48. self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
  49. self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
  50. self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
  51. self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
  52. def test_error(self):
  53. self.o.ind = 'dumb'
  54. #self.n.ind = 'bad'
  55. self.assertRaises(TypeError, operator.index, self.o)
  56. #self.assertRaises(TypeError, operator.index, self.n)
  57. self.assertRaises(TypeError, slice(self.o).indices, 0)
  58. #self.assertRaises(TypeError, slice(self.n).indices, 0)
  59. class SeqTestCase(unittest.TestCase):
  60. # This test case isn't run directly. It just defines common tests
  61. # to the different sequence types below
  62. def setUp(self):
  63. self.o = oldstyle()
  64. #self.n = newstyle()
  65. self.o2 = oldstyle()
  66. #self.n2 = newstyle()
  67. def test_index(self):
  68. self.o.ind = -2
  69. #self.n.ind = 2
  70. #self.assertEqual(self.seq[self.n], self.seq[2])
  71. self.assertEqual(self.seq[self.o], self.seq[-2])
  72. def test_slice(self):
  73. self.o.ind = 1
  74. self.o2.ind = 3
  75. #self.n.ind = 2
  76. #self.n2.ind = 4
  77. self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
  78. #self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
  79. def test_slice_bug7532a(self):
  80. seqlen = len(self.seq)
  81. self.o.ind = int(seqlen * 1.5)
  82. #self.n.ind = seqlen + 2
  83. self.assertEqual(self.seq[self.o:], self.seq[0:0])
  84. self.assertEqual(self.seq[:self.o], self.seq)
  85. #self.assertEqual(self.seq[self.n:], self.seq[0:0])
  86. #self.assertEqual(self.seq[:self.n], self.seq)
  87. def test_slice_bug7532b(self):
  88. if isinstance(self.seq, ClassicSeq):
  89. self.skipTest('test fails for ClassicSeq')
  90. # These tests fail for ClassicSeq (see bug #7532)
  91. seqlen = len(self.seq)
  92. self.o2.ind = -seqlen - 2
  93. #self.n2.ind = -int(seqlen * 1.5)
  94. self.assertEqual(self.seq[self.o2:], self.seq)
  95. self.assertEqual(self.seq[:self.o2], self.seq[0:0])
  96. #self.assertEqual(self.seq[self.n2:], self.seq)
  97. #self.assertEqual(self.seq[:self.n2], self.seq[0:0])
  98. def test_repeat(self):
  99. self.o.ind = 3
  100. #self.n.ind = 2
  101. self.assertEqual(self.seq * self.o, self.seq * 3)
  102. #self.assertEqual(self.seq * self.n, self.seq * 2)
  103. self.assertEqual(self.o * self.seq, self.seq * 3)
  104. #self.assertEqual(self.n * self.seq, self.seq * 2)
  105. def test_wrappers(self):
  106. self.o.ind = 4
  107. #self.n.ind = 5
  108. self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
  109. self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
  110. self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
  111. #self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
  112. #self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
  113. #self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
  114. def test_subclasses(self):
  115. self.assertEqual(self.seq[TrapInt()], self.seq[0])
  116. self.assertEqual(self.seq[TrapLong()], self.seq[0])
  117. def test_error(self):
  118. self.o.ind = 'dumb'
  119. #self.n.ind = 'bad'
  120. indexobj = lambda x, obj: obj.seq[x]
  121. self.assertRaises(TypeError, indexobj, self.o, self)
  122. #self.assertRaises(TypeError, indexobj, self.n, self)
  123. sliceobj = lambda x, obj: obj.seq[x:]
  124. self.assertRaises(TypeError, sliceobj, self.o, self)
  125. #self.assertRaises(TypeError, sliceobj, self.n, self)
  126. class ListTestCase(SeqTestCase):
  127. seq = [0,10,20,30,40,50]
  128. def test_setdelitem(self):
  129. self.o.ind = -2
  130. #self.n.ind = 2
  131. lst = list('ab!cdefghi!j')
  132. del lst[self.o]
  133. #del lst[self.n]
  134. lst[self.o] = 'X'
  135. #lst[self.n] = 'Y'
  136. #self.assertEqual(lst, list('abYdefghXj'))
  137. self.assertEqual(lst, list('ab!cdefghXj'))
  138. # lst = [5, 6, 7, 8, 9, 10, 11]
  139. # lst.__setitem__(self.n, "here")
  140. # self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
  141. # lst.__delitem__(self.n)
  142. # self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
  143. def test_inplace_repeat(self):
  144. self.o.ind = 2
  145. #self.n.ind = 3
  146. lst = [6, 4]
  147. lst *= self.o
  148. self.assertEqual(lst, [6, 4, 6, 4])
  149. #lst *= self.n
  150. #self.assertEqual(lst, [6, 4, 6, 4] * 3)
  151. #lst = [5, 6, 7, 8, 9, 11]
  152. #l2 = lst.__imul__(self.n)
  153. #self.assertIs(l2, lst)
  154. #self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
  155. class _BaseSeq:
  156. def __init__(self, iterable):
  157. self._list = list(iterable)
  158. def __repr__(self):
  159. return repr(self._list)
  160. def __eq__(self, other):
  161. return self._list == other
  162. def __len__(self):
  163. return len(self._list)
  164. def __mul__(self, n):
  165. return self.__class__(self._list*n)
  166. __rmul__ = __mul__
  167. def __getitem__(self, index):
  168. return self._list[index]
  169. class _GetSliceMixin:
  170. def __getslice__(self, i, j):
  171. return self._list.__getslice__(i, j)
  172. class ClassicSeq(_BaseSeq): pass
  173. #class NewSeq(_BaseSeq, object): pass
  174. #class ClassicSeqDeprecated(_GetSliceMixin, ClassicSeq): pass
  175. #class NewSeqDeprecated(_GetSliceMixin, NewSeq): pass
  176. class TupleTestCase(SeqTestCase):
  177. seq = (0,10,20,30,40,50)
  178. class StringTestCase(SeqTestCase):
  179. seq = "this is a test"
  180. # class ByteArrayTestCase(SeqTestCase):
  181. # seq = bytearray("this is a test")
  182. # class UnicodeTestCase(SeqTestCase):
  183. # seq = u"this is a test"
  184. # class ClassicSeqTestCase(SeqTestCase):
  185. # seq = ClassicSeq((0,10,20,30,40,50))
  186. # class NewSeqTestCase(SeqTestCase):
  187. # seq = NewSeq((0,10,20,30,40,50))
  188. # class ClassicSeqDeprecatedTestCase(SeqTestCase):
  189. # seq = ClassicSeqDeprecated((0,10,20,30,40,50))
  190. # class NewSeqDeprecatedTestCase(SeqTestCase):
  191. # seq = NewSeqDeprecated((0,10,20,30,40,50))
  192. # class XRangeTestCase(unittest.TestCase):
  193. # def test_xrange(self):
  194. # n = newstyle()
  195. # n.ind = 5
  196. # self.assertEqual(xrange(1, 20)[n], 6)
  197. # self.assertEqual(xrange(1, 20).__getitem__(n), 6)
  198. # class OverflowTestCase(unittest.TestCase):
  199. # def setUp(self):
  200. # self.pos = 2**100
  201. # self.neg = -self.pos
  202. # def test_large_longs(self):
  203. # self.assertEqual(self.pos.__index__(), self.pos)
  204. # self.assertEqual(self.neg.__index__(), self.neg)
  205. # def _getitem_helper(self, base):
  206. # class GetItem(base):
  207. # def __len__(self):
  208. # return maxint # cannot return long here
  209. # def __getitem__(self, key):
  210. # return key
  211. # x = GetItem()
  212. # self.assertEqual(x[self.pos], self.pos)
  213. # self.assertEqual(x[self.neg], self.neg)
  214. # self.assertEqual(x[self.neg:self.pos].indices(maxsize),
  215. # (0, maxsize, 1))
  216. # self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
  217. # (0, maxsize, 1))
  218. # def _getslice_helper_deprecated(self, base):
  219. # class GetItem(base):
  220. # def __len__(self):
  221. # return maxint # cannot return long here
  222. # def __getitem__(self, key):
  223. # return key
  224. # def __getslice__(self, i, j):
  225. # return i, j
  226. # x = GetItem()
  227. # self.assertEqual(x[self.pos], self.pos)
  228. # self.assertEqual(x[self.neg], self.neg)
  229. # self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
  230. # self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
  231. # (0, maxsize, 1))
  232. # def test_getitem(self):
  233. # self._getitem_helper(object)
  234. # with test_support.check_py3k_warnings():
  235. # self._getslice_helper_deprecated(object)
  236. # def test_getitem_classic(self):
  237. # class Empty: pass
  238. # # XXX This test fails (see bug #7532)
  239. # #self._getitem_helper(Empty)
  240. # with test_support.check_py3k_warnings():
  241. # self._getslice_helper_deprecated(Empty)
  242. # def test_sequence_repeat(self):
  243. # self.assertRaises(OverflowError, lambda: "a" * self.pos)
  244. # self.assertRaises(OverflowError, lambda: "a" * self.neg)
  245. def test_main():
  246. test_support.run_unittest(
  247. BaseTestCase,
  248. ListTestCase,
  249. TupleTestCase,
  250. # ByteArrayTestCase,
  251. StringTestCase,
  252. # UnicodeTestCase,
  253. # ClassicSeqTestCase,
  254. # NewSeqTestCase,
  255. # XRangeTestCase,
  256. # OverflowTestCase,
  257. )
  258. # with test_support.check_py3k_warnings():
  259. # test_support.run_unittest(
  260. # ClassicSeqDeprecatedTestCase,
  261. # NewSeqDeprecatedTestCase,
  262. # )
  263. if __name__ == "__main__":
  264. test_main()