def test_evolver_simple_update_in_tree(): v = pvector(range(35)) e = v.evolver() e[10] = -10 assert e[10] == -10 assert e.persistent()[10] == -10
def test_pvectors_produced_from_the_same_evolver_do_not_interfere(): original_list = list(range(40)) v = pvector(original_list) e = v.evolver() e.extend([1, 2, 3]) e[2] = 20 e[35] = 350 v1 = e.persistent() v1_expected = original_list + [1, 2, 3] v1_expected[2] = 20 v1_expected[35] = 350 e.extend([-1, -2, -3]) e[3] = -30 e[36] = -360 v2 = e.persistent() v2_expected = v1_expected + [-1, -2, -3] v2_expected[3] = -30 v2_expected[36] = -360 assert list(v1) == v1_expected assert list(v2) == v2_expected
def test_evolver_extend(): v = pvector([1000]) e = v.evolver() e.extend([2000, 3000]) e[2] = 20000 assert list(e.persistent()) == [1000, 2000, 20000] assert list(v) == [1000]
def test_evolver_simple_update_just_outside_vector(): v = pvector() e = v.evolver() e[0] = 1 assert e[0] == 1 assert e.persistent()[0] == 1 assert len(v) == 0
def test_evolver_simple_update_in_tail(): v = pvector(range(35)) e = v.evolver() e[33] = -33 assert e[33] == -33 assert e.persistent()[33] == -33 assert v[33] == 33
def test_evolver_assign_and_read_with_negative_indices(): v = pvector([1, 2, 3]) e = v.evolver() e[-1] = 4 e.extend([11, 12, 13]) e[-1] = 33 assert e[-1] == 33 assert list(e.persistent()) == [1, 2, 4, 11, 12, 33]
def test_evolver_append(): v = pvector() e = v.evolver() e.append(1000) assert e[0] == 1000 e[0] = 2000 assert e[0] == 2000 assert list(e.persistent()) == [2000] assert list(v) == []
def test_get_evolver_referents(): """The C implementation of the evolver should expose the original PVector to the gc only once. """ if pvector.__module__ == "pyrsistent._pvector": pytest.skip("This test only applies to pvectorc") import gc v = pvector([1, 2, 3]) e = v.evolver() assert len([x for x in gc.get_referents(e) if x is v]) == 1
def test_no_dependencies_between_evolvers_from_the_same_pvector(): original_list = list(range(40)) v = pvector(original_list) e1 = v.evolver() e2 = v.evolver() e1.extend([1, 2, 3]) e1[2] = 20 e1[35] = 350 e2.extend([-1, -2, -3]) e2[2] = -20 e2[35] = -350 e1_expected = original_list + [1, 2, 3] e1_expected[2] = 20 e1_expected[35] = 350 assert list(e1.persistent()) == e1_expected e2_expected = original_list + [-1, -2, -3] e2_expected[2] = -20 e2_expected[35] = -350 assert list(e2.persistent()) == e2_expected
def test_evolver_multi_level_multi_update_in_tree(): # This test is mostly to detect memory/ref count issues in the native implementation v = pvector(range(3500)) e = v.evolver() # Update differs between first and second time since the # corresponding node will be marked as dirty the first time only. e[10] = -10 e[11] = -11 e[10] = -1000 # Update in neighbour node e[50] = -50 e[50] = -5000 # Update in node in other half of vector e[3000] = -3000 e[3000] = -30000 # Before freezing assert e[10] == -1000 assert e[11] == -11 assert e[50] == -5000 assert e[3000] == -30000 # Run a GC to provoke any potential misbehavior import gc gc.collect() v2 = e.persistent() assert v2[10] == -1000 assert v2[50] == -5000 assert v2[3000] == -30000 # Run a GC to provoke any potential misbehavior gc.collect() # After freezing assert e[10] == -1000 assert e[11] == -11 assert e[50] == -5000 assert e[3000] == -30000 # Original stays the same assert v[10] == 10 assert v[50] == 50 assert v[3000] == 3000
def test_evolver_returns_itself_on_evolving_operations(): # Does this to be able to chain operations v = pvector([1, 2]) assert v.evolver().append(3).extend([4, 5]).set( 1, 6).persistent() == pvector([1, 6, 3, 4, 5])
def test_evolver_with_no_updates_returns_same_pvector(): v = pvector([1, 2]) assert v.evolver().persistent() is v
def test_evolver_set_out_of_range(): v = pvector([0]) e = v.evolver() with pytest.raises(IndexError) as error: e[10] = 1 assert str(error.value) == "Index out of range: 10"
def test_evolver_deallocate_dirty_evolver(): # Ref count handling in native implementation v = pvector(range(3220)) e = v.evolver() e[10] = -10 e[3220] = -3220
def test_evolver_no_update(): # This is mostly a test against memory leaks in the C implementation v = pvector(range(40)) assert v.evolver().persistent() == v