set_test.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from pyrsistent import pset, s
  2. import pytest
  3. import pickle
  4. def test_literalish_works():
  5. assert s() is pset()
  6. assert s(1, 2) == pset([1, 2])
  7. def test_supports_hash():
  8. assert hash(s(1, 2)) == hash(s(1, 2))
  9. def test_empty_truthiness():
  10. assert s(1)
  11. assert not s()
  12. def test_contains_elements_that_it_was_initialized_with():
  13. initial = [1, 2, 3]
  14. s = pset(initial)
  15. assert set(s) == set(initial)
  16. assert len(s) == len(set(initial))
  17. def test_is_immutable():
  18. s1 = pset([1])
  19. s2 = s1.add(2)
  20. assert s1 == pset([1])
  21. assert s2 == pset([1, 2])
  22. s3 = s2.remove(1)
  23. assert s2 == pset([1, 2])
  24. assert s3 == pset([2])
  25. def test_remove_when_not_present():
  26. s1 = s(1, 2, 3)
  27. with pytest.raises(KeyError):
  28. s1.remove(4)
  29. def test_discard():
  30. s1 = s(1, 2, 3)
  31. assert s1.discard(3) == s(1, 2)
  32. assert s1.discard(4) is s1
  33. def test_is_iterable():
  34. assert sum(pset([1, 2, 3])) == 6
  35. def test_contains():
  36. s = pset([1, 2, 3])
  37. assert 2 in s
  38. assert 4 not in s
  39. def test_supports_set_operations():
  40. s1 = pset([1, 2, 3])
  41. s2 = pset([3, 4, 5])
  42. assert s1 | s2 == s(1, 2, 3, 4, 5)
  43. assert s1.union(s2) == s1 | s2
  44. assert s1 & s2 == s(3)
  45. assert s1.intersection(s2) == s1 & s2
  46. assert s1 - s2 == s(1, 2)
  47. assert s1.difference(s2) == s1 - s2
  48. assert s1 ^ s2 == s(1, 2, 4, 5)
  49. assert s1.symmetric_difference(s2) == s1 ^ s2
  50. def test_supports_set_comparisons():
  51. s1 = s(1, 2, 3)
  52. s3 = s(1, 2)
  53. s4 = s(1, 2, 3)
  54. assert s(1, 2, 3, 3, 5) == s(1, 2, 3, 5)
  55. assert s1 != s3
  56. assert s3 < s1
  57. assert s3 <= s1
  58. assert s3 <= s4
  59. assert s1 > s3
  60. assert s1 >= s3
  61. assert s4 >= s3
  62. def test_str():
  63. rep = str(pset([1, 2, 3]))
  64. assert rep == "pset([1, 2, 3])"
  65. def test_is_disjoint():
  66. s1 = pset([1, 2, 3])
  67. s2 = pset([3, 4, 5])
  68. s3 = pset([4, 5])
  69. assert not s1.isdisjoint(s2)
  70. assert s1.isdisjoint(s3)
  71. def test_evolver_simple_add():
  72. x = s(1, 2, 3)
  73. e = x.evolver()
  74. assert not e.is_dirty()
  75. e.add(4)
  76. assert e.is_dirty()
  77. x2 = e.persistent()
  78. assert not e.is_dirty()
  79. assert x2 == s(1, 2, 3, 4)
  80. assert x == s(1, 2, 3)
  81. def test_evolver_simple_remove():
  82. x = s(1, 2, 3)
  83. e = x.evolver()
  84. e.remove(2)
  85. x2 = e.persistent()
  86. assert x2 == s(1, 3)
  87. assert x == s(1, 2, 3)
  88. def test_evolver_no_update_produces_same_pset():
  89. x = s(1, 2, 3)
  90. e = x.evolver()
  91. assert e.persistent() is x
  92. def test_evolver_len():
  93. x = s(1, 2, 3)
  94. e = x.evolver()
  95. assert len(e) == 3
  96. def test_copy_returns_reference_to_self():
  97. s1 = s(10)
  98. assert s1.copy() is s1
  99. def test_pickling_empty_set():
  100. assert pickle.loads(pickle.dumps(s(), -1)) == s()
  101. def test_pickling_non_empty_map():
  102. assert pickle.loads(pickle.dumps(s(1, 2), -1)) == s(1, 2)
  103. def test_supports_weakref():
  104. import weakref
  105. weakref.ref(s(1))
  106. def test_update():
  107. assert s(1, 2, 3).update([3, 4, 4, 5]) == s(1, 2, 3, 4, 5)
  108. def test_update_no_elements():
  109. s1 = s(1, 2)
  110. assert s1.update([]) is s1
  111. def test_iterable():
  112. """
  113. PSets can be created from iterables even though they can't be len() hinted.
  114. """
  115. assert pset(iter("a")) == pset(iter("a"))