bag_test.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import pytest
  2. from pyrsistent import b, pbag
  3. def test_literalish_works():
  4. assert b(1, 2) == pbag([1, 2])
  5. def test_empty_bag():
  6. """
  7. creating an empty pbag returns a singleton.
  8. Note that this should NOT be relied upon in application code.
  9. """
  10. assert b() is b()
  11. def test_supports_hash():
  12. assert hash(b(1, 2)) == hash(b(2, 1))
  13. def test_hash_in_dict():
  14. assert {b(1,2,3,3): "hello"}[b(3,3,2,1)] == "hello"
  15. def test_empty_truthiness():
  16. assert b(1)
  17. assert not b()
  18. def test_repr_empty():
  19. assert repr(b()) == 'pbag([])'
  20. def test_repr_elements():
  21. assert repr(b(1, 2)) in ('pbag([1, 2])', 'pbag([2, 1])')
  22. def test_add_empty():
  23. assert b().add(1) == b(1)
  24. def test_remove_final():
  25. assert b().add(1).remove(1) == b()
  26. def test_remove_nonfinal():
  27. assert b().add(1).add(1).remove(1) == b(1)
  28. def test_remove_nonexistent():
  29. with pytest.raises(KeyError) as excinfo:
  30. b().remove(1)
  31. assert str(excinfo.exconly()) == 'KeyError: 1'
  32. def test_eq_empty():
  33. assert b() == b()
  34. def test_neq():
  35. assert b(1) != b()
  36. def test_eq_same_order():
  37. assert b(1, 2, 1) == b(1, 2, 1)
  38. def test_eq_different_order():
  39. assert b(2, 1, 2) == b(1, 2, 2)
  40. def test_count_non_existent():
  41. assert b().count(1) == 0
  42. def test_count_unique():
  43. assert b(1).count(1) == 1
  44. def test_count_duplicate():
  45. assert b(1, 1).count(1) == 2
  46. def test_length_empty():
  47. assert len(b()) == 0
  48. def test_length_unique():
  49. assert len(b(1)) == 1
  50. def test_length_duplicates():
  51. assert len(b(1, 1)) == 2
  52. def test_length_multiple_elements():
  53. assert len(b(1, 1, 2, 3)) == 4
  54. def test_iter_duplicates():
  55. assert list(b(1, 1)) == [1, 1]
  56. def test_iter_multiple_elements():
  57. assert list(b(1, 2, 2)) in ([1, 2, 2], [2, 2, 1])
  58. def test_contains():
  59. assert 1 in b(1)
  60. def test_not_contains():
  61. assert 1 not in b(2)
  62. def test_add():
  63. assert b(3, 3, 3, 2, 2, 1) + b(4, 3, 2, 1) == b(4,
  64. 3, 3, 3, 3,
  65. 2, 2, 2,
  66. 1, 1)
  67. def test_sub():
  68. assert b(1, 2, 3, 3) - b(3, 4) == b(1, 2, 3)
  69. def test_or():
  70. assert b(1, 2, 2, 3, 3, 3) | b(1, 2, 3, 4, 4) == b(1,
  71. 2, 2,
  72. 3, 3, 3,
  73. 4, 4)
  74. def test_and():
  75. assert b(1, 2, 2, 3, 3, 3) & b(2, 3, 3, 4) == b(2, 3, 3)
  76. def test_pbag_is_unorderable():
  77. with pytest.raises(TypeError):
  78. _ = b(1) < b(2) # type: ignore
  79. with pytest.raises(TypeError):
  80. _ = b(1) <= b(2) # type: ignore
  81. with pytest.raises(TypeError):
  82. _ = b(1) > b(2) # type: ignore
  83. with pytest.raises(TypeError):
  84. _ = b(1) >= b(2) # type: ignore
  85. def test_supports_weakref():
  86. import weakref
  87. weakref.ref(b(1))
  88. def test_update():
  89. assert pbag([1, 2, 2]).update([3, 3, 4]) == pbag([1, 2, 2, 3, 3, 4])
  90. def test_update_no_elements():
  91. b = pbag([1, 2, 2])
  92. assert b.update([]) is b
  93. def test_iterable():
  94. """
  95. PBags can be created from iterables even though they can't be len() hinted.
  96. """
  97. assert pbag(iter("a")) == pbag(iter("a"))