import pickle
import pytest
from pyrsistent import pdeque, dq


def test_basic_right_and_left():
    x = pdeque([1, 2])

    assert x.right == 2
    assert x.left == 1
    assert len(x) == 2


def test_construction_with_maxlen():
    assert pdeque([1, 2, 3, 4], maxlen=2) == pdeque([3, 4])
    assert pdeque([1, 2, 3, 4], maxlen=4) == pdeque([1, 2, 3, 4])
    assert pdeque([], maxlen=2) == pdeque()


def test_construction_with_invalid_maxlen():
    with pytest.raises(TypeError):
        pdeque([], maxlen='foo')

    with pytest.raises(ValueError):
        pdeque([], maxlen=-3)


def test_pop():
    x = pdeque([1, 2, 3, 4]).pop()
    assert x.right == 3
    assert x.left == 1

    x = x.pop()
    assert x.right == 2
    assert x.left == 1

    x = x.pop()
    assert x.right == 1
    assert x.left == 1

    x = x.pop()
    assert x == pdeque()

    x = pdeque([1, 2]).pop()
    assert x == pdeque([1])

    x = x.pop()
    assert x == pdeque()

    assert pdeque().append(1).pop() == pdeque()
    assert pdeque().appendleft(1).pop() == pdeque()


def test_pop_multiple():
    assert pdeque([1, 2, 3, 4]).pop(3) == pdeque([1])
    assert pdeque([1, 2]).pop(3) == pdeque()


def test_pop_with_negative_index():
    assert pdeque([1, 2, 3]).pop(-1) == pdeque([1, 2, 3]).popleft(1)
    assert pdeque([1, 2, 3]).popleft(-1) == pdeque([1, 2, 3]).pop(1)


def test_popleft():
    x = pdeque([1, 2, 3, 4]).popleft()
    assert x.left == 2
    assert x.right == 4

    x = x.popleft()
    assert x.left == 3
    assert x.right == 4

    x = x.popleft()
    assert x.right == 4
    assert x.left == 4

    x = x.popleft()
    assert x == pdeque()

    x = pdeque([1, 2]).popleft()
    assert x == pdeque([2])

    x = x.popleft()
    assert x == pdeque()

    assert pdeque().append(1).popleft() == pdeque()