test_containers.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from collections import defaultdict
  2. from sympy.core.basic import Basic
  3. from sympy.core.containers import (Dict, Tuple)
  4. from sympy.core.numbers import Integer
  5. from sympy.core.kind import NumberKind
  6. from sympy.matrices.common import MatrixKind
  7. from sympy.core.singleton import S
  8. from sympy.core.symbol import symbols
  9. from sympy.core.sympify import sympify
  10. from sympy.matrices.dense import Matrix
  11. from sympy.sets.sets import FiniteSet
  12. from sympy.core.containers import tuple_wrapper, TupleKind
  13. from sympy.core.expr import unchanged
  14. from sympy.core.function import Function, Lambda
  15. from sympy.core.relational import Eq
  16. from sympy.testing.pytest import raises
  17. from sympy.utilities.iterables import is_sequence, iterable
  18. from sympy.abc import x, y
  19. def test_Tuple():
  20. t = (1, 2, 3, 4)
  21. st = Tuple(*t)
  22. assert set(sympify(t)) == set(st)
  23. assert len(t) == len(st)
  24. assert set(sympify(t[:2])) == set(st[:2])
  25. assert isinstance(st[:], Tuple)
  26. assert st == Tuple(1, 2, 3, 4)
  27. assert st.func(*st.args) == st
  28. p, q, r, s = symbols('p q r s')
  29. t2 = (p, q, r, s)
  30. st2 = Tuple(*t2)
  31. assert st2.atoms() == set(t2)
  32. assert st == st2.subs({p: 1, q: 2, r: 3, s: 4})
  33. # issue 5505
  34. assert all(isinstance(arg, Basic) for arg in st.args)
  35. assert Tuple(p, 1).subs(p, 0) == Tuple(0, 1)
  36. assert Tuple(p, Tuple(p, 1)).subs(p, 0) == Tuple(0, Tuple(0, 1))
  37. assert Tuple(t2) == Tuple(Tuple(*t2))
  38. assert Tuple.fromiter(t2) == Tuple(*t2)
  39. assert Tuple.fromiter(x for x in range(4)) == Tuple(0, 1, 2, 3)
  40. assert st2.fromiter(st2.args) == st2
  41. def test_Tuple_contains():
  42. t1, t2 = Tuple(1), Tuple(2)
  43. assert t1 in Tuple(1, 2, 3, t1, Tuple(t2))
  44. assert t2 not in Tuple(1, 2, 3, t1, Tuple(t2))
  45. def test_Tuple_concatenation():
  46. assert Tuple(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
  47. assert (1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
  48. assert Tuple(1, 2) + (3, 4) == Tuple(1, 2, 3, 4)
  49. raises(TypeError, lambda: Tuple(1, 2) + 3)
  50. raises(TypeError, lambda: 1 + Tuple(2, 3))
  51. #the Tuple case in __radd__ is only reached when a subclass is involved
  52. class Tuple2(Tuple):
  53. def __radd__(self, other):
  54. return Tuple.__radd__(self, other + other)
  55. assert Tuple(1, 2) + Tuple2(3, 4) == Tuple(1, 2, 1, 2, 3, 4)
  56. assert Tuple2(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
  57. def test_Tuple_equality():
  58. assert not isinstance(Tuple(1, 2), tuple)
  59. assert (Tuple(1, 2) == (1, 2)) is True
  60. assert (Tuple(1, 2) != (1, 2)) is False
  61. assert (Tuple(1, 2) == (1, 3)) is False
  62. assert (Tuple(1, 2) != (1, 3)) is True
  63. assert (Tuple(1, 2) == Tuple(1, 2)) is True
  64. assert (Tuple(1, 2) != Tuple(1, 2)) is False
  65. assert (Tuple(1, 2) == Tuple(1, 3)) is False
  66. assert (Tuple(1, 2) != Tuple(1, 3)) is True
  67. def test_Tuple_Eq():
  68. assert Eq(Tuple(), Tuple()) is S.true
  69. assert Eq(Tuple(1), 1) is S.false
  70. assert Eq(Tuple(1, 2), Tuple(1)) is S.false
  71. assert Eq(Tuple(1), Tuple(1)) is S.true
  72. assert Eq(Tuple(1, 2), Tuple(1, 3)) is S.false
  73. assert Eq(Tuple(1, 2), Tuple(1, 2)) is S.true
  74. assert unchanged(Eq, Tuple(1, x), Tuple(1, 2))
  75. assert Eq(Tuple(1, x), Tuple(1, 2)).subs(x, 2) is S.true
  76. assert unchanged(Eq, Tuple(1, 2), x)
  77. f = Function('f')
  78. assert unchanged(Eq, Tuple(1), f(x))
  79. assert Eq(Tuple(1), f(x)).subs(x, 1).subs(f, Lambda(y, (y,))) is S.true
  80. def test_Tuple_comparision():
  81. assert (Tuple(1, 3) >= Tuple(-10, 30)) is S.true
  82. assert (Tuple(1, 3) <= Tuple(-10, 30)) is S.false
  83. assert (Tuple(1, 3) >= Tuple(1, 3)) is S.true
  84. assert (Tuple(1, 3) <= Tuple(1, 3)) is S.true
  85. def test_Tuple_tuple_count():
  86. assert Tuple(0, 1, 2, 3).tuple_count(4) == 0
  87. assert Tuple(0, 4, 1, 2, 3).tuple_count(4) == 1
  88. assert Tuple(0, 4, 1, 4, 2, 3).tuple_count(4) == 2
  89. assert Tuple(0, 4, 1, 4, 2, 4, 3).tuple_count(4) == 3
  90. def test_Tuple_index():
  91. assert Tuple(4, 0, 1, 2, 3).index(4) == 0
  92. assert Tuple(0, 4, 1, 2, 3).index(4) == 1
  93. assert Tuple(0, 1, 4, 2, 3).index(4) == 2
  94. assert Tuple(0, 1, 2, 4, 3).index(4) == 3
  95. assert Tuple(0, 1, 2, 3, 4).index(4) == 4
  96. raises(ValueError, lambda: Tuple(0, 1, 2, 3).index(4))
  97. raises(ValueError, lambda: Tuple(4, 0, 1, 2, 3).index(4, 1))
  98. raises(ValueError, lambda: Tuple(0, 1, 2, 3, 4).index(4, 1, 4))
  99. def test_Tuple_mul():
  100. assert Tuple(1, 2, 3)*2 == Tuple(1, 2, 3, 1, 2, 3)
  101. assert 2*Tuple(1, 2, 3) == Tuple(1, 2, 3, 1, 2, 3)
  102. assert Tuple(1, 2, 3)*Integer(2) == Tuple(1, 2, 3, 1, 2, 3)
  103. assert Integer(2)*Tuple(1, 2, 3) == Tuple(1, 2, 3, 1, 2, 3)
  104. raises(TypeError, lambda: Tuple(1, 2, 3)*S.Half)
  105. raises(TypeError, lambda: S.Half*Tuple(1, 2, 3))
  106. def test_tuple_wrapper():
  107. @tuple_wrapper
  108. def wrap_tuples_and_return(*t):
  109. return t
  110. p = symbols('p')
  111. assert wrap_tuples_and_return(p, 1) == (p, 1)
  112. assert wrap_tuples_and_return((p, 1)) == (Tuple(p, 1),)
  113. assert wrap_tuples_and_return(1, (p, 2), 3) == (1, Tuple(p, 2), 3)
  114. def test_iterable_is_sequence():
  115. ordered = [[], (), Tuple(), Matrix([[]])]
  116. unordered = [set()]
  117. not_sympy_iterable = [{}, '', '']
  118. assert all(is_sequence(i) for i in ordered)
  119. assert all(not is_sequence(i) for i in unordered)
  120. assert all(iterable(i) for i in ordered + unordered)
  121. assert all(not iterable(i) for i in not_sympy_iterable)
  122. assert all(iterable(i, exclude=None) for i in not_sympy_iterable)
  123. def test_TupleKind():
  124. kind = TupleKind(NumberKind, MatrixKind(NumberKind))
  125. assert Tuple(1, Matrix([1, 2])).kind is kind
  126. assert Tuple(1, 2).kind is TupleKind(NumberKind, NumberKind)
  127. assert Tuple(1, 2).kind.element_kind == (NumberKind, NumberKind)
  128. def test_Dict():
  129. x, y, z = symbols('x y z')
  130. d = Dict({x: 1, y: 2, z: 3})
  131. assert d[x] == 1
  132. assert d[y] == 2
  133. raises(KeyError, lambda: d[2])
  134. raises(KeyError, lambda: d['2'])
  135. assert len(d) == 3
  136. assert set(d.keys()) == {x, y, z}
  137. assert set(d.values()) == {S.One, S(2), S(3)}
  138. assert d.get(5, 'default') == 'default'
  139. assert d.get('5', 'default') == 'default'
  140. assert x in d and z in d and 5 not in d and '5' not in d
  141. assert d.has(x) and d.has(1) # SymPy Basic .has method
  142. # Test input types
  143. # input - a Python dict
  144. # input - items as args - SymPy style
  145. assert (Dict({x: 1, y: 2, z: 3}) ==
  146. Dict((x, 1), (y, 2), (z, 3)))
  147. raises(TypeError, lambda: Dict(((x, 1), (y, 2), (z, 3))))
  148. with raises(NotImplementedError):
  149. d[5] = 6 # assert immutability
  150. assert set(
  151. d.items()) == {Tuple(x, S.One), Tuple(y, S(2)), Tuple(z, S(3))}
  152. assert set(d) == {x, y, z}
  153. assert str(d) == '{x: 1, y: 2, z: 3}'
  154. assert d.__repr__() == '{x: 1, y: 2, z: 3}'
  155. # Test creating a Dict from a Dict.
  156. d = Dict({x: 1, y: 2, z: 3})
  157. assert d == Dict(d)
  158. # Test for supporting defaultdict
  159. d = defaultdict(int)
  160. assert d[x] == 0
  161. assert d[y] == 0
  162. assert d[z] == 0
  163. assert Dict(d)
  164. d = Dict(d)
  165. assert len(d) == 3
  166. assert set(d.keys()) == {x, y, z}
  167. assert set(d.values()) == {S.Zero, S.Zero, S.Zero}
  168. def test_issue_5788():
  169. args = [(1, 2), (2, 1)]
  170. for o in [Dict, Tuple, FiniteSet]:
  171. # __eq__ and arg handling
  172. if o != Tuple:
  173. assert o(*args) == o(*reversed(args))
  174. pair = [o(*args), o(*reversed(args))]
  175. assert sorted(pair) == sorted(pair)
  176. assert set(o(*args)) # doesn't fail