def test_iterable(): """ PVectors can be created from iterables even though they can't be len() hinted. """ assert pvector(iter("a")) == pvector(iter("a"))
def test_delete_slice(): seq = pvector(range(5)) assert seq.delete(1, 4) == pvector([0, 4]) assert seq.delete(4, 1) == seq assert seq.delete(0, 1) == pvector([1, 2, 3, 4]) assert seq.delete(6, 8) == seq assert seq.delete(-1, 1) == seq assert seq.delete(1, -1) == pvector([0, 4])
def test_evolver_set_followed_by_delete(): evolver = pvector([1, 2]).evolver() evolver[1] = 3 assert [evolver[i] for i in range(len(evolver))] == [1, 3] del evolver[0] assert evolver.persistent() == pvector([3])
def test_delete_of_non_existing_element(): e = pvector([1, 2]).evolver() with pytest.raises(IndexError): del e[2] del e[0] del e[0] with pytest.raises(IndexError): del e[0] assert e.persistent() == pvector()
def test_repeat(): v = pvector([1, 2]) assert 5 * pvector() is pvector() assert v is 1 * v assert 0 * v is pvector() assert 2 * pvector([1, 2]) == pvector([1, 2, 1, 2]) assert -3 * pvector([1, 2]) is pvector()
def test_pvector_constructor(): container = pvector() assert len(container) == 0 container = pvector((0, "1", 2)) assert len(container) == 3 container = pvector([0, "1", 2]) assert len(container) == 3 container = pvector(range(5)) assert len(container) == 5 container = pvector(map(lambda x: x, range(10))) assert len(container) == 10
def test_random_insert_within_tail(): seq = pvector([1, 2, 3]) seq2 = seq.set(1, 4) assert seq2[1] == 4 assert seq[1] == 2
def test_multi_level_sequence_from_iterator(): seq = pvector(iter(range(8000))) seq2 = seq.append(11) assert seq[5] == 5 assert seq2[7373] == 7373 assert seq2[8000] == 11
def test_random_insert_outside_tail(): seq = pvector(range(20000)) seq2 = seq.set(19000, 4) assert seq2[19000] == 4 assert seq[19000] == 19000
def test_non_empty_repr(): v = pvector([1, 2, 3]) assert str(v) == "pvector([1, 2, 3])" # There's some state that needs to be reset between calls in the native version, # test that multiple invocations work. assert str(v) == "pvector([1, 2, 3])"
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_empty_initialization(): seq = pvector() assert len(seq) == 0 with pytest.raises(IndexError) as error: x = seq[0] assert str(error.value) == "Index out of range: 0"
def test_evolver_delete_by_index(): e = pvector([1, 2, 3]).evolver() del e[0] assert e.persistent() == python_pvector([2, 3]) assert e.append(4).persistent() == python_pvector([2, 3, 4])
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_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_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_iteration(): y = 0 seq = pvector(range(2000)) for x in seq: assert x == y y += 1 assert y == 2000
def test_failing_repr(): # See https://github.com/tobgu/pyrsistent/issues/84 class A(object): def __repr__(self): raise ValueError("oh no!") with pytest.raises(ValueError): repr(pvector([A()]))
def test_short_extend(): # Extend within tail length the_list = [1, 2] seq = pvector() seq2 = seq.extend(the_list) assert len(seq2) == len(the_list) assert seq2[0] == the_list[0] assert seq2[1] == the_list[1]
def test_insert_beyond_end(): seq = pvector(range(2)) seq2 = seq.set(2, 50) assert seq2[2] == 50 with pytest.raises(IndexError) as error: seq2.set(19, 4) assert str(error.value) == "Index out of range: 19"
def test_evolver_delete_function_by_index_multiple_times(): SIZE = 40 e = pvector(range(SIZE)).evolver() for i in range(SIZE): assert e[0] == i assert list(e.persistent()) == list(range(i, SIZE)) del e[0] assert e.persistent() == list()
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_delete_index(): seq = pvector([1, 2, 3]) assert seq.delete(0) == pvector([2, 3]) assert seq.delete(1) == pvector([1, 3]) assert seq.delete(2) == pvector([1, 2]) assert seq.delete(-1) == pvector([1, 2]) assert seq.delete(-2) == pvector([1, 3]) assert seq.delete(-3) == pvector([2, 3])
def test_append_works_and_does_not_affect_original_within_tail(): seq1 = pvector([3]) seq2 = seq1.append(2) assert len(seq1) == 1 assert seq1[0] == 3 assert len(seq2) == 2 assert seq2[0] == 3 assert seq2[1] == 2
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_repr_when_contained_object_contains_reference_to_self(): x = [1, 2, 3] v = pvector([1, 2, x]) x.append(v) assert str(v) == "pvector([1, 2, [1, 2, 3, pvector([1, 2, [...]])]])" # Run a GC to provoke any potential misbehavior import gc gc.collect()
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_slicing_reverse(): seq = pvector(range(10)) seq2 = seq[::-1] assert seq2[0] == 9 assert seq2[1] == 8 assert len(seq2) == 10 seq3 = seq[-3:-7:-1] assert seq3[0] == 7 assert seq3[3] == 4 assert len(seq3) == 4
def test_append_when_root_overflows(): seq = pvector([]) for x in range(32 * 33): seq = seq.append(x) seq = seq.append(10001) for i in range(32 * 33): assert seq[i] == i assert seq[32 * 33] == 10001