test_ordereddict.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import unittest
  2. from collections import OrderedDict
  3. from random import shuffle
  4. class TestOrderedDict(unittest.TestCase):
  5. def test_init(self):
  6. #with self.assertRaises(TypeError):
  7. # OrderedDict([('a', 1), ('b', 2)], None) # too many args
  8. pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
  9. self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input
  10. #self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input
  11. self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input
  12. #self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
  13. # c=3, e=5).items()), pairs) # mixed input
  14. # make sure no positional args conflict with possible kwdargs
  15. #self.assertEqual(list(OrderedDict(self=42).items()), [('self', 42)])
  16. #self.assertEqual(list(OrderedDict(other=42).items()), [('other', 42)])
  17. #self.assertRaises(TypeError, OrderedDict, 42)
  18. #self.assertRaises(TypeError, OrderedDict, (), ())
  19. #self.assertRaises(TypeError, OrderedDict.__init__)
  20. # Make sure that direct calls to __init__ do not clear previous contents
  21. #d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
  22. #d.__init__([('e', 5), ('f', 6)], g=7, d=4)
  23. #self.assertEqual(list(d.items()),
  24. # [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
  25. # def test_update(self):
  26. # #with self.assertRaises(TypeError):
  27. # # OrderedDict().update([('a', 1), ('b', 2)], None) # too many args
  28. # pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
  29. # od = OrderedDict()
  30. # od.update(dict(pairs))
  31. # self.assertEqual(sorted(od.items()), pairs) # dict input
  32. # od = OrderedDict()
  33. # od.update(**dict(pairs))
  34. # self.assertEqual(sorted(od.items()), pairs) # kwds input
  35. # od = OrderedDict()
  36. # od.update(pairs)
  37. # self.assertEqual(list(od.items()), pairs) # pairs input
  38. # od = OrderedDict()
  39. # od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
  40. # self.assertEqual(list(od.items()), pairs) # mixed input
  41. # # Issue 9137: Named argument called 'other' or 'self'
  42. # # shouldn't be treated specially.
  43. # od = OrderedDict()
  44. # od.update(self=23)
  45. # self.assertEqual(list(od.items()), [('self', 23)])
  46. # od = OrderedDict()
  47. # od.update(other={})
  48. # self.assertEqual(list(od.items()), [('other', {})])
  49. # od = OrderedDict()
  50. # od.update(red=5, blue=6, other=7, self=8)
  51. # self.assertEqual(sorted(list(od.items())),
  52. # [('blue', 6), ('other', 7), ('red', 5), ('self', 8)])
  53. # # Make sure that direct calls to update do not clear previous contents
  54. # # add that updates items are not moved to the end
  55. # d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
  56. # d.update([('e', 5), ('f', 6)], g=7, d=4)
  57. # self.assertEqual(list(d.items()),
  58. # [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
  59. # self.assertRaises(TypeError, OrderedDict().update, 42)
  60. # self.assertRaises(TypeError, OrderedDict().update, (), ())
  61. # self.assertRaises(TypeError, OrderedDict.update)
  62. # def test_abc(self):
  63. # self.assertIsInstance(OrderedDict(), MutableMapping)
  64. # self.assertTrue(issubclass(OrderedDict, MutableMapping))
  65. def test_clear(self):
  66. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  67. shuffle(pairs)
  68. od = OrderedDict(pairs)
  69. self.assertEqual(len(od), len(pairs))
  70. od.clear()
  71. self.assertEqual(len(od), 0)
  72. def test_delitem(self):
  73. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  74. od = OrderedDict(pairs)
  75. del od['a']
  76. self.assertNotIn('a', od)
  77. #with self.assertRaises(KeyError):
  78. # del od['a']
  79. self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])
  80. def test_setitem(self):
  81. od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
  82. od['c'] = 10 # existing element
  83. od['f'] = 20 # new element
  84. self.assertEqual(list(od.items()),
  85. [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])
  86. def test_iterators(self):
  87. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  88. shuffle(pairs)
  89. od = OrderedDict(pairs)
  90. self.assertEqual(list(od), [t[0] for t in pairs])
  91. self.assertEqual(list(od.keys()), [t[0] for t in pairs])
  92. self.assertEqual(list(od.values()), [t[1] for t in pairs])
  93. self.assertEqual(list(od.items()), pairs)
  94. # self.assertEqual(list(reversed(od)),
  95. # [t[0] for t in reversed(pairs)])
  96. # self.assertEqual(list(reversed(od.keys())),
  97. # [t[0] for t in reversed(pairs)])
  98. # self.assertEqual(list(reversed(od.values())),
  99. # [t[1] for t in reversed(pairs)])
  100. # self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
  101. # def test_detect_deletion_during_iteration(self):
  102. # od = OrderedDict.fromkeys('abc')
  103. # it = iter(od)
  104. # key = next(it)
  105. # del od[key]
  106. # with self.assertRaises(Exception):
  107. # # Note, the exact exception raised is not guaranteed
  108. # # The only guarantee that the next() will not succeed
  109. # next(it)
  110. def test_popitem(self):
  111. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  112. shuffle(pairs)
  113. od = OrderedDict(pairs)
  114. while pairs:
  115. self.assertEqual(od.popitem(), pairs.pop())
  116. #with self.assertRaises(KeyError):
  117. # od.popitem()
  118. self.assertEqual(len(od), 0)
  119. def test_pop(self):
  120. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  121. shuffle(pairs)
  122. od = OrderedDict(pairs)
  123. shuffle(pairs)
  124. while pairs:
  125. k, v = pairs.pop()
  126. self.assertEqual(od.pop(k), v)
  127. #with self.assertRaises(KeyError):
  128. # od.pop('xyz')
  129. self.assertEqual(len(od), 0)
  130. self.assertEqual(od.pop(k, 12345), 12345)
  131. # make sure pop still works when __missing__ is defined
  132. # class Missing(OrderedDict):
  133. # def __missing__(self, key):
  134. # return 0
  135. # m = Missing(a=1)
  136. # self.assertEqual(m.pop('b', 5), 5)
  137. # self.assertEqual(m.pop('a', 6), 1)
  138. # self.assertEqual(m.pop('a', 6), 6)
  139. #with self.assertRaises(KeyError):
  140. # m.pop('a')
  141. def test_equality(self):
  142. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  143. shuffle(pairs)
  144. od1 = OrderedDict(pairs)
  145. od2 = OrderedDict(pairs)
  146. self.assertEqual(od1, od2) # same order implies equality
  147. pairs = pairs[2:] + pairs[:2]
  148. od2 = OrderedDict(pairs)
  149. self.assertNotEqual(od1, od2) # different order implies inequality
  150. # comparison to regular dict is not order sensitive
  151. self.assertEqual(od1, dict(od2))
  152. self.assertEqual(dict(od2), od1)
  153. # different length implied inequality
  154. self.assertNotEqual(od1, OrderedDict(pairs[:-1]))
  155. # def test_copying(self):
  156. # # Check that ordered dicts are copyable, deepcopyable, picklable,
  157. # # and have a repr/eval round-trip
  158. # pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  159. # od = OrderedDict(pairs)
  160. # def check(dup):
  161. # msg = "\ncopy: %s\nod: %s" % (dup, od)
  162. # self.assertIsNot(dup, od, msg)
  163. # self.assertEqual(dup, od)
  164. # check(od.copy())
  165. # check(copy.copy(od))
  166. # check(copy.deepcopy(od))
  167. # for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  168. # with self.subTest(proto=proto):
  169. # check(pickle.loads(pickle.dumps(od, proto)))
  170. # check(eval(repr(od)))
  171. # update_test = OrderedDict()
  172. # update_test.update(od)
  173. # check(update_test)
  174. # check(OrderedDict(od))
  175. # def test_yaml_linkage(self):
  176. # # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
  177. # # In yaml, lists are native but tuples are not.
  178. # pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  179. # od = OrderedDict(pairs)
  180. # # yaml.dump(od) -->
  181. # # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n'
  182. # self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1]))
  183. # def test_reduce_not_too_fat(self):
  184. # # do not save instance dictionary if not needed
  185. # pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  186. # od = OrderedDict(pairs)
  187. # self.assertIsNone(od.__reduce__()[2])
  188. # od.x = 10
  189. # self.assertIsNotNone(od.__reduce__()[2])
  190. # def test_pickle_recursive(self):
  191. # od = OrderedDict()
  192. # od[1] = od
  193. # for proto in range(-1, pickle.HIGHEST_PROTOCOL + 1):
  194. # dup = pickle.loads(pickle.dumps(od, proto))
  195. # self.assertIsNot(dup, od)
  196. # self.assertEqual(list(dup.keys()), [1])
  197. # self.assertIs(dup[1], dup)
  198. def test_repr(self):
  199. od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
  200. self.assertEqual(repr(od),
  201. "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])")
  202. #self.assertEqual(eval(repr(od)), od)
  203. self.assertEqual(repr(OrderedDict()), "OrderedDict()")
  204. # def test_repr_recursive(self):
  205. # # See issue #9826
  206. # od = OrderedDict.fromkeys('abc')
  207. # od['x'] = od
  208. # self.assertEqual(repr(od),
  209. # "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])")
  210. def test_setdefault(self):
  211. pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
  212. shuffle(pairs)
  213. od = OrderedDict(pairs)
  214. pair_order = list(od.items())
  215. self.assertEqual(od.setdefault('a', 10), 3)
  216. # make sure order didn't change
  217. self.assertEqual(list(od.items()), pair_order)
  218. self.assertEqual(od.setdefault('x', 10), 10)
  219. # make sure 'x' is added to the end
  220. self.assertEqual(list(od.items())[-1], ('x', 10))
  221. # make sure setdefault still works when __missing__ is defined
  222. # class Missing(OrderedDict):
  223. # def __missing__(self, key):
  224. # return 0
  225. # self.assertEqual(Missing().setdefault(5, 9), 9)
  226. def test_reinsert(self):
  227. # Given insert a, insert b, delete a, re-insert a,
  228. # verify that a is now later than b.
  229. od = OrderedDict()
  230. od['a'] = 1
  231. od['b'] = 2
  232. del od['a']
  233. od['a'] = 1
  234. self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
  235. # def test_move_to_end(self):
  236. # od = OrderedDict.fromkeys('abcde')
  237. # self.assertEqual(list(od), list('abcde'))
  238. # od.move_to_end('c')
  239. # self.assertEqual(list(od), list('abdec'))
  240. # od.move_to_end('c', 0)
  241. # self.assertEqual(list(od), list('cabde'))
  242. # od.move_to_end('c', 0)
  243. # self.assertEqual(list(od), list('cabde'))
  244. # od.move_to_end('e')
  245. # self.assertEqual(list(od), list('cabde'))
  246. # with self.assertRaises(KeyError):
  247. # od.move_to_end('x')
  248. # def test_sizeof(self):
  249. # # Wimpy test: Just verify the reported size is larger than a regular dict
  250. # d = dict(a=1)
  251. # od = OrderedDict(**d)
  252. # self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))
  253. # def test_override_update(self):
  254. # # Verify that subclasses can override update() without breaking __init__()
  255. # class MyOD(OrderedDict):
  256. # def update(self, *args, **kwds):
  257. # raise Exception()
  258. # items = [('a', 1), ('c', 3), ('b', 2)]
  259. # self.assertEqual(list(MyOD(items).items()), items)
  260. if __name__ == '__main__':
  261. unittest.main()