From f816975086e443297b2f7f74b15dca61bfc0327c Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Wed, 14 Dec 2016 11:58:47 -0800 Subject: [PATCH 01/11] updated linked list and test to allow empty --- src/linked_list.py | 15 +++++-- src/test_linked_list.py | 91 +++++++++++++++-------------------------- 2 files changed, 45 insertions(+), 61 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index f0b66db..f21d208 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -4,10 +4,16 @@ class LinkedList(object): """Class representation of linked list.""" - def __init__(self, head_node): + def __init__(self, iterable=None): """Instantiate linked list.""" - self.head_node = head_node - self.length = 1 + self.head_node = None + self.length = 0 + try: + for item in iterable: + self.push(item) + except TypeError: + if iterable: + print("Please only enter iterable values") def push(self, contents): """Add node to this linked list.""" @@ -16,6 +22,9 @@ def push(self, contents): def pop(self): """Remove and return the current head node.""" + if not self.head_node: + print("Linked list is already empty") + return old_head_node = self.head_node self.head_node = self.head_node.next_node self.length -= 1 diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 73089c0..11d1164 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -4,61 +4,51 @@ def test_node_init(): """Test node class init.""" from linked_list import Node - new_node = Node(1, None) - assert new_node.contents == 1 and new_node.next_node is None + new_node = Node(0, None) + assert new_node.contents == 0 and new_node.next_node is None def test_linkedlist_init(): """Test for LinkedList init.""" - from linked_list import Node, LinkedList - new_node = Node(34, None) - new_llist = LinkedList(new_node) - assert new_llist.length == 1 - assert new_llist.head_node == new_node + from linked_list import LinkedList + new_llist = LinkedList() + assert new_llist.length == 0 + assert new_llist.head_node is None def test_linkedlist_push(): """Test for LinkedList push.""" - from linked_list import Node, LinkedList - new_node = Node(34, None) - new_llist = LinkedList(new_node) - new_llist.push(1) - assert new_llist.length == 2 - assert new_llist.head_node.contents == 1 + from linked_list import LinkedList + new_llist = LinkedList() + new_llist.push("new") + assert new_llist.length == 1 + assert new_llist.head_node.contents == "new" def test_linkedlist_pop(): """Test for LinkedList pop.""" - from linked_list import Node, LinkedList - new_node = Node(34, None) - new_llist = LinkedList(new_node) + from linked_list import LinkedList + new_llist = LinkedList() + new_llist.push(34) new_llist.push(1) - new_llist.pop() + assert new_llist.pop().contents == 1 assert new_llist.head_node.contents == 34 assert new_llist.length == 1 + assert new_llist.pop().contents == 34 + assert new_llist.pop() is None def test_linkedlist_size(): """Test for LinkedList size.""" - from linked_list import Node, LinkedList - new_node = Node(34, None) - new_llist = LinkedList(new_node) - new_llist.push(1) - new_llist.pop() - new_llist.push('test1') - new_llist.push('test2') - new_llist.push('test3') - new_llist.push('test4') - assert new_llist.size() == 5 + from linked_list import LinkedList + new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) + assert new_llist.size() == 4 def test_linkedlist_search(): """Test for LinkedList search.""" - from linked_list import Node, LinkedList - new_node = Node(34, None) - new_llist = LinkedList(new_node) - new_llist.push(1) - new_llist.pop() + from linked_list import LinkedList + new_llist = LinkedList() new_llist.push('test1') new_llist.push('test2') new_llist.push('test3') @@ -70,24 +60,14 @@ def test_linkedlist_search(): def test_linkedlist_remove(): """Test for LinkedList remove.""" - from linked_list import Node, LinkedList - new_node = Node('Test5', None) - new_llist = LinkedList(new_node) - new_llist.push('test4') - new_llist.push('test3') - new_llist.push('test2') - new_llist.push('test1') + from linked_list import LinkedList + new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) new_llist.remove(new_llist.search('test2')) - assert new_llist.size() == 4 - assert new_llist.search('test1').next_node.contents == 'test3' - new_node2 = Node('Test5', None) - new_llist2 = LinkedList(new_node2) - new_llist2.push('test4') - new_llist2.push('test3') - new_llist2.push('test2') - new_llist2.push('test1') - new_llist2.remove(new_llist2.search('test1')) - assert new_llist2.head_node.contents == 'test2' + assert new_llist.size() == 3 + assert new_llist.search('test3').next_node.contents == 'test1' + new_llist2 = LinkedList(('test1', 'test2', 'test3', 'test4')) + new_llist2.remove(new_llist2.search('test4')) + assert new_llist2.head_node.contents == 'test3' try: new_llist.remove(new_llist.search('blah')) except ValueError: @@ -96,13 +76,8 @@ def test_linkedlist_remove(): def test_linkedlist_display(): """Test for LinkedList remove.""" - from linked_list import Node, LinkedList - new_node = Node('test5', None) - new_llist = LinkedList(new_node) - new_llist.push('test4') - new_llist.push('test3') - new_llist.push('test2') - new_llist.push('test1') - new_llist2 = LinkedList(new_node) - assert new_llist.display() == ('test1', 'test2', 'test3', 'test4', 'test5') + from linked_list import LinkedList + new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) + new_llist2 = LinkedList(('test5',)) + assert new_llist.display() == ('test4', 'test3', 'test2', 'test1') assert new_llist2.display() == ('test5',) From 127fd29cc9d67ad7ece0c978a84221af30d514d4 Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Wed, 14 Dec 2016 12:06:04 -0800 Subject: [PATCH 02/11] test removing tail node --- src/test_linked_list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 11d1164..9c57c05 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -68,6 +68,8 @@ def test_linkedlist_remove(): new_llist2 = LinkedList(('test1', 'test2', 'test3', 'test4')) new_llist2.remove(new_llist2.search('test4')) assert new_llist2.head_node.contents == 'test3' + new_llist2.remove(new_llist2.search('test1')) + assert new_llist2.search('test2').next_node is None try: new_llist.remove(new_llist.search('blah')) except ValueError: From 5e5f97bcef4f7ea9ab672e7bc080134b746ce8ec Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Thu, 15 Dec 2016 17:44:59 -0800 Subject: [PATCH 03/11] used fixtures in testing --- src/linked_list.py | 36 +++++---- src/test_linked_list.py | 170 ++++++++++++++++++++++++++++------------ 2 files changed, 143 insertions(+), 63 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index f21d208..5e7e1c8 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -28,7 +28,7 @@ def pop(self): old_head_node = self.head_node self.head_node = self.head_node.next_node self.length -= 1 - return old_head_node + return old_head_node.contents def size(self): """Return the current size of this linked list.""" @@ -36,14 +36,17 @@ def size(self): def search(self, search_value): """Return the node with the searched contents if found.""" - if search_value == self.head_node.contents: - return self.head_node - current_node = self.head_node - while current_node.contents != search_value: - if current_node.next_node is None: - return None - current_node = current_node.next_node - return current_node + if self.length: + if search_value == self.head_node.contents: + return self.head_node + current_node = self.head_node + while current_node.contents != search_value: + if current_node.next_node is None: + return None + current_node = current_node.next_node + return current_node + else: + return None def remove(self, remove_node): """Remove a node from linked list.""" @@ -61,12 +64,15 @@ def remove(self, remove_node): def display(self): """Return the tuple of all values in linked list.""" - new_list = [self.head_node.contents] - current_node = self.head_node - while current_node.next_node is not None: - current_node = current_node.next_node - new_list.append(current_node.contents) - return tuple(new_list) + if self.length == 0: + return None + else: + new_list = [self.head_node.contents] + current_node = self.head_node + while current_node.next_node is not None: + current_node = current_node.next_node + new_list.append(current_node.contents) + return tuple(new_list) class Node(object): diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 9c57c05..7e57030 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -1,5 +1,17 @@ """Tests for linked_list.py.""" +import pytest + + +@pytest.fixture +def sample_linked_list(): + """Create testing linked list.""" + from linked_list import LinkedList + one_llist = LinkedList([1]) + empty_llist = LinkedList() + new_llist = LinkedList([1, 2, 3, 4, 5]) + return (one_llist, empty_llist, new_llist) + def test_node_init(): """Test node class init.""" @@ -8,41 +20,72 @@ def test_node_init(): assert new_node.contents == 0 and new_node.next_node is None -def test_linkedlist_init(): - """Test for LinkedList init.""" - from linked_list import LinkedList - new_llist = LinkedList() - assert new_llist.length == 0 - assert new_llist.head_node is None +def test_linkedlist_init_empty_size(): + """Test for empty LinkedList init.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert empty_llist.length == 0 -def test_linkedlist_push(): - """Test for LinkedList push.""" - from linked_list import LinkedList - new_llist = LinkedList() +def test_linkedlist_init_empty_head(): + """Test head in empty LinkedList init.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert empty_llist.head_node is None + + +def test_linkedlist_init_one_size(): + """Test for LinkedList init single item.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert one_llist.length == 1 + + +def test_linkedlist_init_one_head(): + """Test head in LinkedList init single item.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert one_llist.head_node.contents == 1 + + +def test_linkedlist_init_list_size(): + """Test for LinkedList init with list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.length == 5 + + +def test_linkedlist_init_list_head(): + """Test head in LinkedList init with list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.head_node.contents == 5 + + +def test_linkedlist_push_size(): + """Test for LinkedList size after push.""" + one_llist, empty_llist, new_llist = sample_linked_list() + new_llist.push("new") + assert new_llist.length == 6 + + +def test_linkedlist_push_val(): + """Test for LinkedList head value after push.""" + one_llist, empty_llist, new_llist = sample_linked_list() new_llist.push("new") - assert new_llist.length == 1 assert new_llist.head_node.contents == "new" -def test_linkedlist_pop(): - """Test for LinkedList pop.""" - from linked_list import LinkedList - new_llist = LinkedList() - new_llist.push(34) - new_llist.push(1) - assert new_llist.pop().contents == 1 - assert new_llist.head_node.contents == 34 - assert new_llist.length == 1 - assert new_llist.pop().contents == 34 - assert new_llist.pop() is None +def test_linkedlist_pop_empty(): + """Test for Linked List pop on empty.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert empty_llist.pop() is None -def test_linkedlist_size(): - """Test for LinkedList size.""" - from linked_list import LinkedList - new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) - assert new_llist.size() == 4 +def test_linkedlist_pop_one(): + """Test for Linked List pop on list with one item.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert one_llist.pop() == 1 + + +def test_linkedlist_pop_list(): + """Test for Linked List pop on multi-item list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.pop() == 5 def test_linkedlist_search(): @@ -58,28 +101,59 @@ def test_linkedlist_search(): assert new_llist.search('test100') is None +def test_linkedlist_search_list(): + """Test for LinkedList search list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.search(2).contents == 2 + + +def test_linkedlist_search_empty(): + """Test for LinkedList search empty list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert empty_llist.search(2) is None + + +def test_linkedlist_search_list_false(): + """Test for LinkedList search list when search value is not in list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.search(100) is None + + def test_linkedlist_remove(): - """Test for LinkedList remove.""" - from linked_list import LinkedList - new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) - new_llist.remove(new_llist.search('test2')) - assert new_llist.size() == 3 - assert new_llist.search('test3').next_node.contents == 'test1' - new_llist2 = LinkedList(('test1', 'test2', 'test3', 'test4')) - new_llist2.remove(new_llist2.search('test4')) - assert new_llist2.head_node.contents == 'test3' - new_llist2.remove(new_llist2.search('test1')) - assert new_llist2.search('test2').next_node is None - try: - new_llist.remove(new_llist.search('blah')) - except ValueError: - assert True + """Test LinkedList remove() on a list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + new_llist.remove(new_llist.search(3)) + assert new_llist.search(3) is None + assert new_llist.search(4).next_node.contents == 2 + + +def test_linkedlist_remove_head(): + """Test LinkedList remove() the head on a list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + new_llist.remove(new_llist.search(5)) + assert new_llist.head_node.contents == 4 + + +def test_linkedlist_remove_empty(): + """Test LinkedList remove() on a list list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + with pytest.raises(ValueError): + new_llist.remove(new_llist.search(100)) def test_linkedlist_display(): - """Test for LinkedList remove.""" - from linked_list import LinkedList - new_llist = LinkedList(('test1', 'test2', 'test3', 'test4')) - new_llist2 = LinkedList(('test5',)) - assert new_llist.display() == ('test4', 'test3', 'test2', 'test1') - assert new_llist2.display() == ('test5',) + """Test for LinkedList display.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert new_llist.display() == (5, 4, 3, 2, 1) + + +def test_linkedlist_display_one(): + """Test for LinkedList display on single item list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert one_llist.display() == (1,) + + +def test_linkedlist_display_empty(): + """Test for LinkedList display on empty list.""" + one_llist, empty_llist, new_llist = sample_linked_list() + assert empty_llist.display() is None From 35ca79d2a8a064fa5008cab215fc2e5e499e5c0e Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Thu, 15 Dec 2016 17:57:34 -0800 Subject: [PATCH 04/11] changed error messages to actual error raising --- src/linked_list.py | 5 ++--- src/test_linked_list.py | 16 ++-------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index 5e7e1c8..d83b18b 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -13,7 +13,7 @@ def __init__(self, iterable=None): self.push(item) except TypeError: if iterable: - print("Please only enter iterable values") + raise TypeError("Please only enter iterable values") def push(self, contents): """Add node to this linked list.""" @@ -23,8 +23,7 @@ def push(self, contents): def pop(self): """Remove and return the current head node.""" if not self.head_node: - print("Linked list is already empty") - return + raise AttributeError("List is empty") old_head_node = self.head_node self.head_node = self.head_node.next_node self.length -= 1 diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 7e57030..9ca019d 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -73,7 +73,8 @@ def test_linkedlist_push_val(): def test_linkedlist_pop_empty(): """Test for Linked List pop on empty.""" one_llist, empty_llist, new_llist = sample_linked_list() - assert empty_llist.pop() is None + with pytest.raises(AttributeError): + empty_llist.pop() def test_linkedlist_pop_one(): @@ -88,19 +89,6 @@ def test_linkedlist_pop_list(): assert new_llist.pop() == 5 -def test_linkedlist_search(): - """Test for LinkedList search.""" - from linked_list import LinkedList - new_llist = LinkedList() - new_llist.push('test1') - new_llist.push('test2') - new_llist.push('test3') - new_llist.push('test4') - assert new_llist.search('test4').contents == 'test4' - assert new_llist.search('test2').contents == 'test2' - assert new_llist.search('test100') is None - - def test_linkedlist_search_list(): """Test for LinkedList search list.""" one_llist, empty_llist, new_llist = sample_linked_list() From 6ed3652b1aa279cc6eed979e3d82c52801010bd9 Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Tue, 27 Dec 2016 13:41:14 -0800 Subject: [PATCH 05/11] llist updates --- src/linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index d83b18b..230c1bc 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -23,7 +23,7 @@ def push(self, contents): def pop(self): """Remove and return the current head node.""" if not self.head_node: - raise AttributeError("List is empty") + raise IndexError("List is empty") old_head_node = self.head_node self.head_node = self.head_node.next_node self.length -= 1 @@ -71,7 +71,7 @@ def display(self): while current_node.next_node is not None: current_node = current_node.next_node new_list.append(current_node.contents) - return tuple(new_list) + return str(tuple(new_list)) class Node(object): From e085be0e43dd48997c2304c7d2fc9a6cd132bfd6 Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Tue, 27 Dec 2016 13:46:57 -0800 Subject: [PATCH 06/11] remove() edits --- src/linked_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index 230c1bc..1b2bb8d 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -23,7 +23,7 @@ def push(self, contents): def pop(self): """Remove and return the current head node.""" if not self.head_node: - raise IndexError("List is empty") + raise IndexError("List is already empty") old_head_node = self.head_node self.head_node = self.head_node.next_node self.length -= 1 @@ -53,8 +53,8 @@ def remove(self, remove_node): self.head_node = self.head_node.next_node self.length -= 1 return None - elif remove_node is None: - raise ValueError("Provided value not in list.") + elif self.lenth == 0: + raise IndexError("The list is already empty.") current_node = self.head_node while current_node.next_node != remove_node: current_node = current_node.next_node From 68e2840b4f526e3625bae8e710766eca6da7c51d Mon Sep 17 00:00:00 2001 From: jordan Date: Tue, 27 Dec 2016 13:49:43 -0800 Subject: [PATCH 07/11] fix typo --- src/linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linked_list.py b/src/linked_list.py index 1b2bb8d..9dc68ca 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -53,7 +53,7 @@ def remove(self, remove_node): self.head_node = self.head_node.next_node self.length -= 1 return None - elif self.lenth == 0: + elif self.length == 0: raise IndexError("The list is already empty.") current_node = self.head_node while current_node.next_node != remove_node: From fdf4a2c055e1697a1abccf382709c54aeef6ae6d Mon Sep 17 00:00:00 2001 From: jordan Date: Tue, 27 Dec 2016 15:31:41 -0800 Subject: [PATCH 08/11] fix remove --- src/linked_list.py | 26 ++++++++----- src/test_linked_list.py | 86 ++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index 9dc68ca..566d970 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -47,19 +47,20 @@ def search(self, search_value): else: return None - def remove(self, remove_node): + def remove(self, remove_value): """Remove a node from linked list.""" - if remove_node == self.head_node: - self.head_node = self.head_node.next_node - self.length -= 1 - return None - elif self.length == 0: - raise IndexError("The list is already empty.") + last_node = None current_node = self.head_node - while current_node.next_node != remove_node: + while current_node: + if current_node.contents == remove_value: + if last_node: + last_node.next_node = current_node.next_node + else: + self.head_node = current_node.next_node + self.length -= 1 + return + last_node = current_node current_node = current_node.next_node - current_node.next_node = current_node.next_node.next_node - self.length -= 1 def display(self): """Return the tuple of all values in linked list.""" @@ -81,3 +82,8 @@ def __init__(self, contents, next_node): """Instantiate linked list node.""" self.contents = contents self.next_node = next_node + + +test = LinkedList([4,3,'blah',1]) +test.remove(1) +print (test.display()) \ No newline at end of file diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 9ca019d..27804f1 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -10,7 +10,7 @@ def sample_linked_list(): one_llist = LinkedList([1]) empty_llist = LinkedList() new_llist = LinkedList([1, 2, 3, 4, 5]) - return (one_llist, empty_llist, new_llist) + return (empty_llist, one_llist, new_llist) def test_node_init(): @@ -20,94 +20,84 @@ def test_node_init(): assert new_node.contents == 0 and new_node.next_node is None -def test_linkedlist_init_empty_size(): +def test_linkedlist_init_empty_size(sample_linked_list): """Test for empty LinkedList init.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert empty_llist.length == 0 + assert sample_linked_list[0].length == 0 -def test_linkedlist_init_empty_head(): +def test_linkedlist_init_empty_head(sample_linked_list): """Test head in empty LinkedList init.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert empty_llist.head_node is None + assert sample_linked_list[0].head_node is None -def test_linkedlist_init_one_size(): +def test_linkedlist_init_one_size(sample_linked_list): """Test for LinkedList init single item.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert one_llist.length == 1 + assert sample_linked_list[1].length == 1 -def test_linkedlist_init_one_head(): +def test_linkedlist_init_one_head(sample_linked_list): """Test head in LinkedList init single item.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert one_llist.head_node.contents == 1 + assert sample_linked_list[1].head_node.contents == 1 -def test_linkedlist_init_list_size(): +def test_linkedlist_init_list_size(sample_linked_list): """Test for LinkedList init with list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.length == 5 + assert sample_linked_list[2].length == 5 -def test_linkedlist_init_list_head(): +def test_linkedlist_init_list_head(sample_linked_list): """Test head in LinkedList init with list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.head_node.contents == 5 + assert sample_linked_list[2].head_node.contents == 5 -def test_linkedlist_push_size(): +def test_linkedlist_push_size(sample_linked_list): """Test for LinkedList size after push.""" - one_llist, empty_llist, new_llist = sample_linked_list() - new_llist.push("new") - assert new_llist.length == 6 + test_linkedlist = sample_linked_list[2] + test_linkedlist.push("new") + assert test_linkedlist.length == 6 -def test_linkedlist_push_val(): +def test_linkedlist_push_val(sample_linked_list): """Test for LinkedList head value after push.""" - one_llist, empty_llist, new_llist = sample_linked_list() - new_llist.push("new") - assert new_llist.head_node.contents == "new" + test_linkedlist = sample_linked_list[2] + test_linkedlist.push("new") + assert test_linkedlist.head_node.contents == 'new' -def test_linkedlist_pop_empty(): +def test_linkedlist_pop_empty(sample_linked_list): """Test for Linked List pop on empty.""" - one_llist, empty_llist, new_llist = sample_linked_list() - with pytest.raises(AttributeError): - empty_llist.pop() + with pytest.raises(IndexError): + sample_linked_list[0].pop() -def test_linkedlist_pop_one(): +def tesst_linkedlist_pop_one(sample_linked_list): """Test for Linked List pop on list with one item.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert one_llist.pop() == 1 + assert sample_linked_list[1].pop() == 1 -def test_linkedlist_pop_list(): +def test_linkedlist_pop_list(sample_linked_list): """Test for Linked List pop on multi-item list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.pop() == 5 + assert sample_linked_list[2].pop() == 5 -def test_linkedlist_search_list(): +def test_linkedlist_search_list(sample_linked_list): """Test for LinkedList search list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.search(2).contents == 2 + assert sample_linked_list[2].search(2).contents == 2 -def test_linkedlist_search_empty(): +def tst_linkedlist_search_empty(): """Test for LinkedList search empty list.""" one_llist, empty_llist, new_llist = sample_linked_list() assert empty_llist.search(2) is None -def test_linkedlist_search_list_false(): +def tst_linkedlist_search_list_false(): """Test for LinkedList search list when search value is not in list.""" one_llist, empty_llist, new_llist = sample_linked_list() assert new_llist.search(100) is None -def test_linkedlist_remove(): +def tst_linkedlist_remove(): """Test LinkedList remove() on a list.""" one_llist, empty_llist, new_llist = sample_linked_list() new_llist.remove(new_llist.search(3)) @@ -115,33 +105,33 @@ def test_linkedlist_remove(): assert new_llist.search(4).next_node.contents == 2 -def test_linkedlist_remove_head(): +def tst_linkedlist_remove_head(): """Test LinkedList remove() the head on a list.""" one_llist, empty_llist, new_llist = sample_linked_list() new_llist.remove(new_llist.search(5)) assert new_llist.head_node.contents == 4 -def test_linkedlist_remove_empty(): +def tst_linkedlist_remove_empty(): """Test LinkedList remove() on a list list.""" one_llist, empty_llist, new_llist = sample_linked_list() with pytest.raises(ValueError): new_llist.remove(new_llist.search(100)) -def test_linkedlist_display(): +def tst_linkedlist_display(): """Test for LinkedList display.""" one_llist, empty_llist, new_llist = sample_linked_list() assert new_llist.display() == (5, 4, 3, 2, 1) -def test_linkedlist_display_one(): +def tst_linkedlist_display_one(): """Test for LinkedList display on single item list.""" one_llist, empty_llist, new_llist = sample_linked_list() assert one_llist.display() == (1,) -def test_linkedlist_display_empty(): +def est_linkedlist_display_empty(): """Test for LinkedList display on empty list.""" one_llist, empty_llist, new_llist = sample_linked_list() assert empty_llist.display() is None From 54961fb7ae33f69fb63cd0c14807002e8ab67b90 Mon Sep 17 00:00:00 2001 From: jordan Date: Tue, 27 Dec 2016 15:34:35 -0800 Subject: [PATCH 09/11] adding more tests --- src/test_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 27804f1..994884b 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -85,7 +85,7 @@ def test_linkedlist_search_list(sample_linked_list): assert sample_linked_list[2].search(2).contents == 2 -def tst_linkedlist_search_empty(): +def test_linkedlist_search_empty(): """Test for LinkedList search empty list.""" one_llist, empty_llist, new_llist = sample_linked_list() assert empty_llist.search(2) is None From 7f6b8169c7be781ae6e455cb24d78f52b4ace8ad Mon Sep 17 00:00:00 2001 From: Julien Wilson Date: Tue, 27 Dec 2016 16:04:20 -0800 Subject: [PATCH 10/11] changed tests to use fixtures --- src/test_linked_list.py | 58 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/test_linked_list.py b/src/test_linked_list.py index 994884b..085a8b8 100644 --- a/src/test_linked_list.py +++ b/src/test_linked_list.py @@ -70,7 +70,7 @@ def test_linkedlist_pop_empty(sample_linked_list): sample_linked_list[0].pop() -def tesst_linkedlist_pop_one(sample_linked_list): +def test_linkedlist_pop_one(sample_linked_list): """Test for Linked List pop on list with one item.""" assert sample_linked_list[1].pop() == 1 @@ -85,53 +85,49 @@ def test_linkedlist_search_list(sample_linked_list): assert sample_linked_list[2].search(2).contents == 2 -def test_linkedlist_search_empty(): +def test_linkedlist_search_empty(sample_linked_list): """Test for LinkedList search empty list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert empty_llist.search(2) is None + assert sample_linked_list[1].search(2) is None -def tst_linkedlist_search_list_false(): +def test_linkedlist_search_list_false(sample_linked_list): """Test for LinkedList search list when search value is not in list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.search(100) is None + assert sample_linked_list[2].search(100) is None -def tst_linkedlist_remove(): - """Test LinkedList remove() on a list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - new_llist.remove(new_llist.search(3)) - assert new_llist.search(3) is None - assert new_llist.search(4).next_node.contents == 2 +def test_linkedlist_remove(sample_linked_list): + """Test that removed value is gone after remove().""" + sample_linked_list[2].remove(3) + assert sample_linked_list[2].search(3) is None -def tst_linkedlist_remove_head(): +def test_linkedlist_remove_pointers(sample_linked_list): + """Test that previous node points to correct node after remove().""" + sample_linked_list[2].remove(3) + assert sample_linked_list[2].search(4).next_node.contents == 2 + + +def test_linkedlist_remove_head(sample_linked_list): """Test LinkedList remove() the head on a list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - new_llist.remove(new_llist.search(5)) - assert new_llist.head_node.contents == 4 + sample_linked_list[2].remove(5) + assert sample_linked_list[2].head_node.contents == 4 -def tst_linkedlist_remove_empty(): - """Test LinkedList remove() on a list list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - with pytest.raises(ValueError): - new_llist.remove(new_llist.search(100)) +def test_linkedlist_remove_empty(sample_linked_list): + """Test LinkedList remove() on an empty linked list.""" + sample_linked_list[1].remove(5) is None -def tst_linkedlist_display(): +def test_linkedlist_display(sample_linked_list): """Test for LinkedList display.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert new_llist.display() == (5, 4, 3, 2, 1) + assert sample_linked_list[2].display() == '(5, 4, 3, 2, 1)' -def tst_linkedlist_display_one(): +def test_linkedlist_display_one(sample_linked_list): """Test for LinkedList display on single item list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert one_llist.display() == (1,) + assert sample_linked_list[1].display() == '(1,)' -def est_linkedlist_display_empty(): +def test_linkedlist_display_empty(sample_linked_list): """Test for LinkedList display on empty list.""" - one_llist, empty_llist, new_llist = sample_linked_list() - assert empty_llist.display() is None + assert sample_linked_list[0].display() is None From dc108d412a8573f34ccce20cb471d8f2ed2f1fcd Mon Sep 17 00:00:00 2001 From: jordan Date: Tue, 27 Dec 2016 16:07:50 -0800 Subject: [PATCH 11/11] clean up ll.py --- src/linked_list.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/linked_list.py b/src/linked_list.py index 566d970..f1b91db 100644 --- a/src/linked_list.py +++ b/src/linked_list.py @@ -82,8 +82,3 @@ def __init__(self, contents, next_node): """Instantiate linked list node.""" self.contents = contents self.next_node = next_node - - -test = LinkedList([4,3,'blah',1]) -test.remove(1) -print (test.display()) \ No newline at end of file