PageRenderTime 243ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Tests/test_set.py

#
Python | 205 lines | 151 code | 25 blank | 29 comment | 9 complexity | d92198273a35f75a61a198f45b4372bd MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Microsoft Public License. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Microsoft Public License, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Microsoft Public License.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. ##
  16. ## Test built-in types: set/frozenset
  17. ##
  18. from iptest.assert_util import *
  19. from iptest.type_util import myset, myfrozenset
  20. # Some of these tests only apply to Python 2.5 compatibility
  21. import sys
  22. isPython25 = ((sys.version_info[0] == 2) and (sys.version_info[1] >= 5)) or (sys.version_info[0] > 2)
  23. Assert(isPython25 or not is_cli)
  24. s1 = [2, 4, 5]
  25. s2 = [4, 7, 9, 10]
  26. s3 = [2, 4, 5, 6]
  27. def test_equality():
  28. for x in (set, frozenset, myset, myfrozenset):
  29. for y in (set, frozenset, myset, myfrozenset):
  30. AreEqual(x(s1), y(s1))
  31. def test_sanity():
  32. for x in (set, frozenset, myset, myfrozenset):
  33. # creating as default
  34. y = x()
  35. AreEqual(len(y), 0)
  36. # creating with 2 args
  37. AssertError(TypeError, x, range(3), 3)
  38. #!!!AssertError(TypeError, x.__new__, str)
  39. #!!!AssertError(TypeError, x.__new__, str, 'abc')
  40. xs1, xs2, xs3 = x(s1), x(s2), x(s3)
  41. # membership
  42. AreEqual(4 in xs1, True)
  43. AreEqual(6 in xs1, False)
  44. # relation with another of the same type
  45. AreEqual(xs1.issubset(xs2), False)
  46. AreEqual(xs1.issubset(xs3), True)
  47. AreEqual(xs3.issuperset(xs1), True)
  48. AreEqual(xs3.issuperset(xs2), False)
  49. # equivalent op
  50. AreEqual(xs1 <= xs2, False)
  51. AreEqual(xs1 <= xs3, True)
  52. AreEqual(xs3 >= xs1, True)
  53. AreEqual(xs3 >= xs2, False)
  54. AreEqual(xs1.union(xs2), x([2, 4, 5, 7, 9, 10]))
  55. AreEqual(xs1.intersection(xs2), x([4]))
  56. AreEqual(xs1.difference(xs2), x([2, 5]))
  57. AreEqual(xs2.difference(xs1), x([7, 9, 10]))
  58. AreEqual(xs2.symmetric_difference(xs1), x([2, 5, 7, 9, 10]))
  59. AreEqual(xs3.symmetric_difference(xs1), x([6]))
  60. # equivalent op
  61. AreEqual(xs1 | xs2, x([2, 4, 5, 7, 9, 10]))
  62. AreEqual(xs1 & xs2, x([4]))
  63. AreEqual(xs1 - xs2, x([2, 5]))
  64. AreEqual(xs2 - xs1, x([7, 9, 10]))
  65. AreEqual(xs2 ^ xs1, x([2, 5, 7, 9, 10]))
  66. AreEqual(xs3 ^ xs1, x([6]))
  67. # repeat with list
  68. AreEqual(xs1.issubset(s2), False)
  69. AreEqual(xs1.issubset(s3), True)
  70. AreEqual(xs3.issuperset(s1), True)
  71. AreEqual(xs3.issuperset(s2), False)
  72. AreEqual(xs1.union(s2), x([2, 4, 5, 7, 9, 10]))
  73. AreEqual(xs1.intersection(s2), x([4]))
  74. AreEqual(xs1.difference(s2), x([2, 5]))
  75. AreEqual(xs2.difference(s1), x([7, 9, 10]))
  76. AreEqual(xs2.symmetric_difference(s1), x([2, 5, 7, 9, 10]))
  77. AreEqual(xs3.symmetric_difference(s1), x([6]))
  78. def test_ops():
  79. s1, s2, s3 = 'abcd', 'be', 'bdefgh'
  80. for t1 in (set, frozenset, myset, myfrozenset):
  81. for t2 in (set, frozenset, myset, myfrozenset):
  82. # set/frozenset creation
  83. AreEqual(t1(t2(s1)), t1(s1))
  84. # ops
  85. for (op, exp1, exp2) in [('&', 'b', 'bd'), ('|', 'abcde', 'abcdefgh'), ('-', 'acd', 'ac'), ('^', 'acde', 'acefgh')]:
  86. x1 = t1(s1)
  87. exec "x1 %s= t2(s2)" % op
  88. AreEqual(x1, t1(exp1))
  89. x1 = t1(s1)
  90. exec "x1 %s= t2(s3)" % op
  91. AreEqual(x1, t1(exp2))
  92. x1 = t1(s1)
  93. exec "y = x1 %s t2(s2)" % op
  94. AreEqual(y, t1(exp1))
  95. x1 = t1(s1)
  96. exec "y = x1 %s t2(s3)" % op
  97. AreEqual(y, t1(exp2))
  98. def test_none():
  99. x, y = set([None, 'd']), set(['a', 'b', 'c', None])
  100. AreEqual(x | y, set([None, 'a', 'c', 'b', 'd']))
  101. AreEqual(y | x, set([None, 'a', 'c', 'b', 'd']))
  102. AreEqual(x & y, set([None]))
  103. AreEqual(y & x, set([None]))
  104. AreEqual(x - y, set('d'))
  105. AreEqual(y - x, set('abc'))
  106. a = set()
  107. a.add(None)
  108. AreEqual(repr(a), 'set([None])')
  109. def test_cmp():
  110. """Verify we can compare sets that aren't the same type"""
  111. a = frozenset([1,2])
  112. b = set([1,2])
  113. abig = frozenset([1,2,3])
  114. bbig = set([1,2,3])
  115. AreEqual(cmp(a,b), 0)
  116. AreEqual(cmp(a,bbig), -1)
  117. AreEqual(cmp(abig,b), 1)
  118. class sset(set): pass
  119. class fset(frozenset): pass
  120. a = fset([1,2])
  121. b = sset([1,2])
  122. abig = fset([1,2,3])
  123. bbig = sset([1,2,3])
  124. AreEqual(cmp(a,b), 0)
  125. AreEqual(cmp(a,bbig), -1)
  126. AreEqual(cmp(abig,b), 1)
  127. def test_deque():
  128. import collections
  129. x = collections.deque([2,3,4,5,6])
  130. x.remove(2)
  131. AreEqual(x, collections.deque([3,4,5,6]))
  132. x.remove(6)
  133. AreEqual(x, collections.deque([3,4,5]))
  134. x.remove(4)
  135. AreEqual(x, collections.deque([3,5]))
  136. # get a deque w/ head/tail backwards...
  137. x = collections.deque([1,2,3,4,5,6,7,8])
  138. x.popleft()
  139. x.popleft()
  140. x.popleft()
  141. x.popleft()
  142. x.append(1)
  143. x.append(2)
  144. x.append(3)
  145. x.append(4)
  146. AreEqual(x, collections.deque([5,6,7,8, 1, 2, 3, 4]))
  147. x.remove(5)
  148. AreEqual(x, collections.deque([6,7,8, 1, 2, 3, 4]))
  149. x.remove(4)
  150. AreEqual(x, collections.deque([6,7,8, 1, 2, 3]))
  151. x.remove(8)
  152. AreEqual(x, collections.deque([6,7,1, 2, 3]))
  153. x.remove(2)
  154. AreEqual(x, collections.deque([6,7,1, 3]))
  155. class BadCmp:
  156. def __eq__(self, other):
  157. raise RuntimeError
  158. d = collections.deque([1,2, BadCmp()])
  159. AssertError(RuntimeError, d.remove, 3)
  160. if isPython25:
  161. def test_singleton():
  162. """Verify that an empty frozenset is a singleton"""
  163. AreEqual(frozenset([]) is frozenset([]), True)
  164. x = frozenset([1, 2, 3])
  165. AreEqual(x is frozenset(x), True)
  166. run_test(__name__)