test_list.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
  2. import unittest
  3. class IterInheritsTestCase(unittest.TestCase):
  4. def setUp(self):
  5. self.type2test = list
  6. def test_generator(self):
  7. def counter(low, high):
  8. current = low
  9. while current <= high:
  10. yield current
  11. current += 1
  12. l = list(counter(1,12))
  13. t = 4 in l
  14. self.assertTrue(t)
  15. def test_getitem(self):
  16. class Counter:
  17. def __getitem__(self,idx):
  18. if idx < 13:
  19. return idx
  20. else:
  21. raise StopIteration
  22. l = list(Counter())
  23. self.assertTrue(5 in l)
  24. def test_dunderiter(self):
  25. class Counter:
  26. def __init__(self, low, high):
  27. self.current = low
  28. self.high = high
  29. def __iter__(self):
  30. return self
  31. def next(self): # Python 3: def __next__(self)
  32. if self.current > self.high:
  33. raise StopIteration
  34. else:
  35. self.current += 1
  36. return self.current - 1
  37. l = list(Counter(1,12))
  38. self.assertTrue(5 in l)
  39. class Foo(Counter):
  40. pass
  41. l = list(Foo(100,120))
  42. self.assertTrue(105 in l)
  43. def test_str(self):
  44. l = list("this is a sequence")
  45. self.assertTrue("q" in l)
  46. def test_reversed(self):
  47. a = list(range(20))
  48. r = sorted(a,reverse=True)
  49. self.assertEqual(list(r), list(range(19, -1, -1)))
  50. def test_explicit_not_reversed(self):
  51. a = list(range(20))
  52. r = sorted(a,reverse=False)
  53. self.assertEqual(r, a)
  54. def test_delitem(self):
  55. self.type2test = list
  56. a = self.type2test([0, 1])
  57. del a[1]
  58. self.assertEqual(a, [0])
  59. del a[0]
  60. self.assertEqual(a, [])
  61. a = self.type2test([0, 1])
  62. del a[-2]
  63. self.assertEqual(a, [1])
  64. del a[-1]
  65. self.assertEqual(a, [])
  66. # todo: why __delitem__ not found?
  67. # a = self.type2test([0, 1])
  68. # self.assertRaises(IndexError, a.__delitem__, -3)
  69. # self.assertRaises(IndexError, a.__delitem__, 2)
  70. #
  71. # a = self.type2test([])
  72. # self.assertRaises(IndexError, a.__delitem__, 0)
  73. #
  74. # self.assertRaises(TypeError, a.__delitem__)
  75. def test_set_subscript(self):
  76. self.type2test = list
  77. a = self.type2test(range(20))
  78. # todo: again __setitem__ not found
  79. # self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3])
  80. # self.assertRaises(TypeError, a.__setitem__, slice(0, 10), 1)
  81. # self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2])
  82. # self.assertRaises(TypeError, a.__getitem__, 'x', 1)
  83. a[slice(2,10,3)] = [1,2,3]
  84. self.assertEqual(a, self.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3,
  85. 9, 10, 11, 12, 13, 14, 15,
  86. 16, 17, 18, 19]))
  87. def test_append(self):
  88. self.type2test = list
  89. a = self.type2test([])
  90. a.append(0)
  91. a.append(1)
  92. a.append(2)
  93. self.assertEqual(a, self.type2test([0, 1, 2]))
  94. self.assertRaises(TypeError, a.append)
  95. def test_extend(self):
  96. self.type2test = list
  97. a1 = self.type2test([0])
  98. a2 = self.type2test((0, 1))
  99. a = a1[:]
  100. a.extend(a2)
  101. self.assertEqual(a, a1 + a2)
  102. a.extend(self.type2test([]))
  103. self.assertEqual(a, a1 + a2)
  104. a.extend(a)
  105. self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1]))
  106. a = self.type2test("spam")
  107. a.extend("eggs")
  108. self.assertEqual(a, list("spameggs"))
  109. self.assertRaises(TypeError, a.extend, None)
  110. def test_insert(self):
  111. a = self.type2test([0, 1, 2])
  112. a.insert(0, -2)
  113. a.insert(1, -1)
  114. a.insert(2, 0)
  115. self.assertEqual(a, [-2, -1, 0, 0, 1, 2])
  116. b = a[:]
  117. b.insert(-2, "foo")
  118. b.insert(-200, "left")
  119. b.insert(200, "right")
  120. self.assertEqual(b, self.type2test(["left",-2,-1,0,0,"foo",1,2,"right"]))
  121. self.assertRaises(TypeError, a.insert)
  122. def test_pop(self):
  123. a = self.type2test([-1, 0, 1])
  124. a.pop()
  125. self.assertEqual(a, [-1, 0])
  126. a.pop(0)
  127. self.assertEqual(a, [0])
  128. self.assertRaises(IndexError, a.pop, 5)
  129. a.pop(0)
  130. self.assertEqual(a, [])
  131. self.assertRaises(IndexError, a.pop)
  132. self.assertRaises(TypeError, a.pop, 42, 42)
  133. a = self.type2test([0, 10, 20, 30, 40])
  134. def test_remove(self):
  135. a = self.type2test([0, 0, 1])
  136. a.remove(1)
  137. self.assertEqual(a, [0, 0])
  138. a.remove(0)
  139. self.assertEqual(a, [0])
  140. a.remove(0)
  141. self.assertEqual(a, [])
  142. self.assertRaises(ValueError, a.remove, 0)
  143. self.assertRaises(TypeError, a.remove)
  144. def test_count(self):
  145. a = self.type2test([0, 1, 2])*3
  146. self.assertEqual(a.count(0), 3)
  147. self.assertEqual(a.count(1), 3)
  148. self.assertEqual(a.count(3), 0)
  149. self.assertRaises(TypeError, a.count)
  150. # class BadExc(Exception):
  151. # pass
  152. #
  153. # class BadCmp:
  154. # def __eq__(self, other):
  155. # if other == 2:
  156. # raise BadExc()
  157. # return False
  158. #
  159. # self.assertRaises(BadExc, a.count, BadCmp())
  160. def test_index(self):
  161. u = self.type2test([0, 1])
  162. self.assertEqual(u.index(0), 0)
  163. self.assertEqual(u.index(1), 1)
  164. self.assertRaises(ValueError, u.index, 2)
  165. u = self.type2test([-2, -1, 0, 0, 1, 2])
  166. self.assertEqual(u.count(0), 2)
  167. self.assertEqual(u.index(0), 2)
  168. self.assertEqual(u.index(0, 2), 2)
  169. self.assertEqual(u.index(-2, -10), 0)
  170. self.assertEqual(u.index(0, 3), 3)
  171. self.assertEqual(u.index(0, 3, 4), 3)
  172. self.assertRaises(ValueError, u.index, 2, 0, -10)
  173. self.assertRaises(TypeError, u.index)
  174. # class BadExc(Exception):
  175. # pass
  176. #
  177. # class BadCmp:
  178. # def __eq__(self, other):
  179. # if other == 2:
  180. # raise BadExc()
  181. # return False
  182. #
  183. # a = self.type2test([0, 1, 2, 3])
  184. # self.assertRaises(BadExc, a.index, BadCmp())
  185. def test_slice(self):
  186. u = self.type2test("spam")
  187. u[:2] = "h"
  188. self.assertEqual(u, list("ham"))
  189. def test_extendedslicing(self):
  190. # subscript
  191. a = self.type2test([0,1,2,3,4])
  192. # deletion
  193. del a[::2]
  194. self.assertEqual(a, self.type2test([1,3]))
  195. a = self.type2test(range(5))
  196. del a[1::2]
  197. self.assertEqual(a, self.type2test([0,2,4]))
  198. a = self.type2test(range(5))
  199. del a[1::-2]
  200. self.assertEqual(a, self.type2test([0,2,3,4]))
  201. a = self.type2test(range(10))
  202. del a[::1000]
  203. self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9]))
  204. # assignment
  205. a = self.type2test(range(10))
  206. a[::2] = [-1]*5
  207. self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
  208. a = self.type2test(range(10))
  209. a[::-4] = [10]*3
  210. self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
  211. # todo: this odd test fails
  212. # a = self.type2test(range(4))
  213. # a[::-1] = a
  214. # self.assertEqual(a, self.type2test([3, 2, 1, 0]))
  215. a = self.type2test(range(10))
  216. b = a[:]
  217. c = a[:]
  218. a[2:3] = self.type2test(["two", "elements"])
  219. b[slice(2,3)] = self.type2test(["two", "elements"])
  220. c[2:3:] = self.type2test(["two", "elements"])
  221. self.assertEqual(a, b)
  222. self.assertEqual(a, c)
  223. a = self.type2test(range(10))
  224. a[::2] = tuple(range(5))
  225. self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9]))
  226. # test issue7788
  227. a = self.type2test(range(10))
  228. del a[9::1<<333]
  229. if __name__ == "__main__":
  230. unittest.main(verbosity=2)