def test_linked_list_includes_value():
    """Test a linked list can be checked for the inclusion of a value."""

    value = 1
    linked_list = LinkedList()
    linked_list.insert(value)
    assert linked_list.includes(value)
def simple_ll():
    """Helper function to return a simple linked list with nodes."""

    linked_list = LinkedList()
    values = [5, 3, 2]
    for value in values:
        linked_list.insert(value)
    return linked_list
def test_merge_lists_empty_second_list():
    """Two linked lists being merged with empty second linked list."""

    ll_one = LinkedList()
    ll_two = LinkedList()
    value = 1
    ll_one.insert(value)

    merged_ll = merge_lists(ll_one, ll_two)
    assert merged_ll.head.value == 1
    assert merged_ll.head.next == None
def test_kth_from_end_ll_size_1():
    """Test a linked list to return the kth from the end value when the linked list has only one node."""

    k = 0
    linked_list = LinkedList()
    value = 1
    linked_list.insert(value)

    expected = value
    actual = linked_list.kth_from_end(k)
    assert actual == expected
def test_get_linked_list_values():
    """Test a linked list can be represented as a string."""

    linked_list = LinkedList()
    for i in range(3):
        value = i
        linked_list.insert(value)

    expected = "2, 1, 0"
    actual = str(linked_list)
    assert actual == expected
def test_insert_linked_list_node():
    """Test a node can be inserted at the head of the linked list."""

    value = 1
    linked_list = LinkedList()
    linked_list.insert(value)
    assert linked_list.head.value == value
    assert linked_list.head.next == None

    value = 2
    linked_list.insert(value)
    assert linked_list.head.value == 2
    assert isinstance(linked_list.head.next, Node)
    assert linked_list.head.next.value == 1
    assert linked_list.head.next.next == None
def test_linked_list_append():
    """Test a value can be appended to a linked list."""

    linked_list = LinkedList()
    value = 1
    linked_list.insert(value)

    value = 2
    linked_list.append(value)
    assert linked_list.head.next.value == value
    assert linked_list.head.next.next == None

    value = 3
    linked_list.append(value)
    assert linked_list.head.next.next.value == value
    assert linked_list.head.next.next.next == None
def test_linked_list_insert_before():
    """Test a new value can be inserted before a value already in the linked list."""

    linked_list = LinkedList()
    value = 1
    linked_list.insert(value)

    new_value = 2
    linked_list.insert_before(value, new_value)
    assert linked_list.head.value == new_value
    assert linked_list.head.next.value == value

    newer_value = 3
    linked_list.insert_before(value, newer_value)
    assert linked_list.head.next.value == newer_value
    assert linked_list.head.next.next.value == value
def test_linked_list_insert_after():
    """Test a value can be inserted after a value already in the linked list."""

    linked_list = LinkedList()
    value = 1
    linked_list.insert(value)

    new_value = 2
    linked_list.insert_after(value, new_value)
    assert linked_list.head.next.value == new_value
    assert linked_list.head.next.next == None

    newer_value = 3
    linked_list.insert_after(value, newer_value)
    assert linked_list.head.next.value == newer_value
    assert linked_list.head.next.next.value == new_value
    assert linked_list.head.next.next.next == None
def test_merge_lists_first_list_smaller_size():
    """Merge in zipper two linked lists with first list smaller."""

    ll_one = LinkedList()
    values = [2, 3]
    for value in values:
        # 3, 2
        ll_one.insert(value)

    ll_two = LinkedList()
    values = [9, 6, 4]
    for value in values:
        # 4, 6, 9
        ll_two.insert(value)

    expected = '3, 4, 2, 6, 9'
    actual = str(merge_lists(ll_one, ll_two))
    assert actual == expected
def test_merge_lists_second_list_smaller_size():
    """Merge in zipper two linked lists with second list smaller."""

    ll_one = LinkedList()
    values = [2, 3, 5]
    for value in values:
        # 5, 3, 2
        ll_one.insert(value)

    ll_two = LinkedList()
    values = [6, 4]
    for value in values:
        # 4, 6
        ll_two.insert(value)

    expected = '5, 4, 3, 6, 2'
    actual = str(merge_lists(ll_one, ll_two))
    assert actual == expected
def test_merge_lists_same_size():
    """Merge in zipper two linked linked of same size."""

    ll_one = LinkedList()
    values = [2, 3, 5]
    for value in values:
        # 5, 3, 2
        ll_one.insert(value)

    ll_two = LinkedList()
    values = [9, 6, 4]
    for value in values:
        # 4, 6, 9
        ll_two.insert(value)

    expected = '5, 4, 3, 6, 2, 9'
    actual = str(merge_lists(ll_one, ll_two))
    assert actual == expected
Пример #13
0
def test_ll_return_collection_of_values():
    test = LinkedList()
    test.insert('Apple')
    test.insert('Cherry')
    assert str(test) == "{'Cherry'}->{'Apple'}->None"
Пример #14
0
def test_ll_includes_false():
    test = LinkedList()
    test.insert('Apple')
    expected = False
    actual = test.includes('Banana')
    assert actual == expected
Пример #15
0
def test_ll_includes_true():
    test = LinkedList()
    test.insert('Apple')
    assert test.includes('Apple')
Пример #16
0
def test_ll_insert_multiple():
    test = LinkedList()
    test.insert('Apple')
    test.insert('Cherry')
    assert test.includes('Apple')
    assert test.includes('Cherry')
Пример #17
0
def test_ll_head():
    test = LinkedList()
    test.insert('Apple')
    assert test.head.value == 'Apple'
Пример #18
0
def test_head_link():
    link_3 = LinkedList()
    link_3.insert(5)
    actual = str(link_3)
    expected = '{ 5 } -> None'
    assert actual == expected