old_contents: """
  Hypothesis-based tests for pvector.
  """
  
  import gc
  
  from pytest import fixture
  
  from pyrsistent import pvector
  
  from hypothesis import given, strategies as st
  
  
  class TestObject(object):
      """
      An object that might catch reference count errors sometimes.
      """
      def __init__(self):
          self.id = id(self)
  
      def __repr__(self):
          return "<%s>" % (self.id,)
  
      def __del__(self):
          # If self is a dangling memory reference this check might fail. Or
          # segfault :)
          if self.id != id(self):
              raise RuntimeError()
  
  
  @fixture(scope="module")
  def gc_when_done(request):
      request.addfinalizer(gc.collect)
  
  
  def test_setup(gc_when_done):
      """
      Ensure we GC when tests finish.
      """
  
  
  Lists = st.lists(st.builds(TestObject))
  PVectors = Lists.map(lambda l: pvector(l))
  
  
  @given(Lists)
  def test_pvector(l):
      """
      ``pvector`` can be roundtripped to a list.
      """
      p = pvector(l)
      assert list(p) == l
      for i in range(len(l)):
          assert p[i] is l[i]
  
  
  @given(PVectors)
  def test_append(p):
      """
      ``append()`` adds an item to the end of the pvector.
      """
      obj = TestObject()
      result = p.append(obj)
      assert result[:-1] == p
      assert result[-1] is obj
  
  
  @given(PVectors, PVectors)
  def test_extend_with_pvector(p, p2):
      """
      ``extend()`` adds all items in given pvector to the end.
      """
      result = p.extend(p2)
      assert result[:len(p)] == p
      assert result[len(p):] == p2
  
  
  @given(PVectors, st.lists(st.builds(TestObject)))
  def test_extend_with_list(p, l):
      """
      ``extend()`` adds all items in given list to the end.
      """
      result = p.extend(l)
      assert result[:len(p)] == p
      assert list(result[len(p):]) == l
  
new_contents: """
  Hypothesis-based tests for pvector.
  """
  
  import gc
  
  from pytest import fixture
  
  from pyrsistent import pvector
  
  from hypothesis import strategies as st
  from hypothesis.stateful import RuleBasedStateMachine, Bundle, rule
  
  
  class TestObject(object):
      """
      An object that might catch reference count errors sometimes.
      """
      def __init__(self):
          self.id = id(self)
  
      def __repr__(self):
          return "<%s>" % (self.id,)
  
      def __del__(self):
          # If self is a dangling memory reference this check might fail. Or
          # segfault :)
          if self.id != id(self):
              raise RuntimeError()
  
  
  @fixture(scope="module")
  def gc_when_done(request):
      request.addfinalizer(gc.collect)
  
  
  def test_setup(gc_when_done):
      """
      Ensure we GC when tests finish.
      """
  
  
  # Pairs of a list and corresponding pvector:
  PVectorAndLists = st.lists(st.builds(TestObject)).map(
      lambda l: (l, pvector(l)))
  
  
  class PVectorBuilder(RuleBasedStateMachine):
      """
      Build a list and matching pvector step-by-step.
  
      In each step in the state machine we do same operation on a list and
      on a pvector, and then when we're done we compare the two.
      """
      sequences = Bundle("sequences")
  
      @rule(target=sequences, start=PVectorAndLists)
      def initial_value(self, start):
          """
          Some initial values generated by a hypothesis strategy.
          """
          return start
  
      @rule(target=sequences, former=sequences)
      def append(self, former):
          """
          Append an item to the pair of sequences.
          """
          l, pv = former
          obj = TestObject()
          l2 = l[:]
          l2.append(obj)
          return l2, pv.append(obj)
  
      @rule(target=sequences, start=sequences, end=sequences)
      def extend(self, start, end):
          """
          Extend a pair of sequences with another pair of sequences.
          """
          l, pv = start
          l2, pv2 = end
          l3 = l[:]
          l3.extend(l2)
          return l3, pv.extend(pv2)
  
      @rule(pair=sequences)
      def compare(self, pair):
          """
          The list and pvector must match.
          """
          l, pv = pair
          assert l == pv
          assert len(l) == len(pv)
          length = len(l)
          for i in range(length):
              assert l[i] == pv[i]
          for i in range(length):
              for j in range(i, length):
                  assert l[i:j] == pv[i:j]
  
  
  PVectorBuilderTests = PVectorBuilder.TestCase
  # Reduce number of tested examples:
  PVectorBuilderTests.settings.max_examples = 30
  
subject: Switch to stateful testing.