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 test_linked_list_insert_before():
    ll = LinkedList()
    ll.head = Node(1)
    ll.head.next = Node(2)
    ll.insert_before(2, 3)
    assert ll.head.next.value == 3
    assert ll.head.next.next.value == 2
def test_linked_list_insert_after():
    ll = LinkedList()
    ll.head = Node(1)
    ll.head.next = Node(2)
    ll.insert_after(1, 4)
    assert ll.head.next.value == 4
    assert ll.head.next.next.value == 2
def test_linked_list_append():
    ll = LinkedList()
    ll.head = Node(1)
    ll.head.next = Node(2)
    ll.append(3)
    assert ll.head.next.next.value == 3
    assert ll.head.next.next.next == None
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_lists():
    """Two linked lists being merged with both empty."""

    ll_one = LinkedList()
    ll_two = LinkedList()

    merged_ll = merge_lists(ll_one, ll_two)
    assert merged_ll.head == None
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_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_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_create_linked_list():
    """Test a linked list can be made."""

    linked_list = LinkedList()
    # Note: I found out about the built-in `vars` function on StackOverflow
    # Source: https://stackoverflow.com/questions/109087/how-to-get-instance-variables-in-python
    keys = vars(linked_list).keys()
    assert len(keys) == 1 and "head" in keys
    assert linked_list.head == 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_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_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_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
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_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
Example #18
0
 def add(self, key, value):
     hashed_key_index = self._hash(key)
     if not self._bucket[hashed_key_index]:
         self._bucket[hashed_key_index] = LinkedList()
     self._bucket[hashed_key_index].insert((key, value))
Example #19
0
def test_ll_return_collection_of_values():
    test = LinkedList()
    test.insert('Apple')
    test.insert('Cherry')
    assert str(test) == "{'Cherry'}->{'Apple'}->None"
Example #20
0
def test_ll_includes_false():
    test = LinkedList()
    test.insert('Apple')
    expected = False
    actual = test.includes('Banana')
    assert actual == expected
Example #21
0
def test_ll_includes_true():
    test = LinkedList()
    test.insert('Apple')
    assert test.includes('Apple')
Example #22
0
def test_ll_insert_multiple():
    test = LinkedList()
    test.insert('Apple')
    test.insert('Cherry')
    assert test.includes('Apple')
    assert test.includes('Cherry')
Example #23
0
def test_ll_head():
    test = LinkedList()
    test.insert('Apple')
    assert test.head.value == 'Apple'
Example #24
0
def test_empty_linked_list():
    test = LinkedList()
    assert test
Example #25
0
def test_head_link():
    link_3 = LinkedList()
    link_3.insert(5)
    actual = str(link_3)
    expected = '{ 5 } -> None'
    assert actual == expected
Example #26
0
from challenges.linked_list.linked_list import LinkedList, Node

link = LinkedList()


def test_empty_list():
    actual = link.insert(Node(None))
    value = None
    assert actual == value


def test_insert_list_with_value_5():
    actual = link.insert(Node(5))
    value = None
    assert actual == value


def test_head_link():
    link_3 = LinkedList()
    link_3.insert(5)
    actual = str(link_3)
    expected = '{ 5 } -> None'
    assert actual == expected


new_link = LinkedList()


def test_multiple_insert():
    new_link.insert('c')
    new_link.insert('b')