def test_pop_only_element():
    ll = SinglyLinkedList()
    for data in [0, "a", 1.5, [], {}, SinglyLinkedList()]:
        ll.insert(0, data)
        assert (not ll.is_empty())
        assert (id(ll.peek_head()) == id(ll.peek_tail()))
        actual = ll.pop(0)
        assert (ll.is_empty())
        assert (actual == data)
def test_repr():
    ll = SinglyLinkedList()
    assert (repr(ll) == "Empty")
    for i in range(5):
        ll.insert_tail(i)
    assert(repr(ll) == "0 {data: 0} -> 1 {data: 1} -> "\
        "2 {data: 2} -> 3 {data: 3} -> 4 {data: 4}")
def test_peek_invalid():
    ll = SinglyLinkedList()
    with pytest.raises(AssertionError):
        ll.peek(0)
    ll.insert(0, 0)
    for i in range(-ITERS // 2, ITERS // 2 + 1):
        if i == 0: continue
        with pytest.raises(AssertionError):
            ll.peek(i)
def test_insert_invalid():
    ll = SinglyLinkedList()
    for i in range(-ITERS // 2, 0):
        with pytest.raises(AssertionError):
            ll.insert(i, 0)
    for i in range(1, ITERS // 2 + 1):
        with pytest.raises(AssertionError):
            ll.insert(i, 0)
    assert (ll.is_empty())
def test_reverse():
    ll, arr = SinglyLinkedList(), []
    for i in range(ITERS):
        if i != 0:
            ll.insert(0, i)
            arr.insert(0, i)
        ll.reverse()
        arr = arr[::-1]
        assert (ll.to_array() == arr)
def test_random_ops():
    ll, arr = SinglyLinkedList(), []
    for i in range(ITERS):
        index = random.randrange(i + 1)
        data = random.randrange(MAX_VAL)
        ll.insert(index, data)
        arr.insert(index, data)
        assert (ll.to_array() == arr)
    for i in range(ITERS):
        index = random.randrange(ITERS)
        actual = ll.peek(index)
        expected = arr[index]
        assert (actual == expected)
    for i in range(ITERS, 0, -1):
        index = random.randrange(i)
        actual = arr.pop(index)
        expected = ll.pop(index)
        assert (actual == expected)
        assert (ll.to_array() == arr)
    assert (ll.is_empty() and len(arr) == 0)
 def setUp(self):
     self.sll = SinglyLinkedList()
     self.sll.insert(1)
     self.sll.insert(2)
     self.sll.insert(3)
def test_insert_tail():
    ll = SinglyLinkedList()
    for i in range(ITERS):
        ll.insert_tail(i)
        assert (len(ll) == i + 1)
        assert (ll.peek_tail() == i)
def base_ll():
    ll = SinglyLinkedList()
    for i in range(ITERS):
        ll.insert_tail(i)
    return ll
def test_ll_equality():
    ll1, ll2 = SinglyLinkedList(), SinglyLinkedList()
    for i in range(ITERS):
        ll1.insert_head(i)
        ll2.insert_head(i)
    assert (ll1 == ll2)