def test_str(): linked_list = LinkedList() assert linked_list.__str__() == 'LinkedList []' linked_list.append('Alice') linked_list.append('Bob') assert linked_list.__str__() == """LinkedList [
def test_insert_after_one(): my_list = LinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) my_list.insert_after(2, 4) assert my_list.__str__() == "3 2 4 1 "
def test_insert_after_two(): my_list = LinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) my_list.insert_after(1, 4) assert my_list.__str__() == "3 2 1 4 "
def test_linked_list_string(): """Test that the __str__ returns exactly what the display method does.""" from linked_list import LinkedList l = LinkedList() l.push(0) l.push(1) assert l.__str__() == l.display()
def test_insert_before_one(): my_list = LinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) my_list.insert_before(2, 7) assert my_list.__str__() == "3 7 2 1 "
def test_str(): """test_display.""" test_list = LinkedList() test_list.push(1) test_list.push(2) test_list.push(3) assert test_list.__str__() == '(3, 2, 1)'
def test_insert_before_two(): my_list = LinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) my_list.insert_before(3, 7) assert my_list.__str__() == "7 3 2 1 "
def test_print(): """Test the print method prints properly.""" l = LinkedList() l.insert(8) l.insert(9) l.insert(10) l.remove(9) assert l.__str__() == '(10, 8)'
def test_print(): """.""" from linked_list import LinkedList l = LinkedList() l.push(8) l.push(9) l.push(10) l.remove(9) assert l.__str__() == '(10, 8)'
def test__str__one(): expected = "3 2 1 " my_list = LinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) actual = my_list.__str__() assert actual == expected assert my_list.includes(2)
def test_to_string_method(): lst_three = LinkedList() lst_three.insert(1) lst_three.append(2) lst_three.append(3) lst_three.append(4) lst_three.append(5) actual = lst_three.__str__() expected = 'Node: 1 Node: 2 Node: 3 Node: 4 Node: 5 ' assert actual == expected
class Queue: """ this is a queue class operations: enqueue, dequeue, peek """ def __init__(self, *args): self.queue = LinkedList(*args) def enqueue(self, value): self.queue.append(value) def dequeue(self): self.queue.delete(0) def peek(self): return self.queue.search(0)[1].value def __str__(self): return self.queue.__str__()
class Stack: """ this class is use to create a stack data structure operations: push, pop, peek """ def __init__(self): self.stack = LinkedList() def push(self, value): self.stack.prepend(value) def pop(self): item = self.stack.delete(0) return item def peek(self): item = self.stack.head.value return item def __str__(self): return self.stack.__str__()
class Queue: def __init__(self): self._linked_list = LinkedList() def enqueue(self, data): self._linked_list.append(data=data) def dequeue(self): return self._linked_list.erase(index=0) def front(self): return self._linked_list.get(index=0) def back(self): return self._linked_list.get_last_node() def is_empty(self): return self._linked_list.length() == 0 def __str__(self): return self._linked_list.__str__()
def test__str__(): ll = LinkedList() ll.insert('violet') ll.insert('indigo') ll.insert('blue') assert ll.__str__() == 'blue indigo violet '
class TestLinkedList(unittest.TestCase): def setUp(self): self.ll = LinkedList() def test_isEmpty(self): self.assertTrue(self.ll.is_empty(), 'The list is not empty') self.ll.add(1) self.assertFalse(self.ll.is_empty(), 'The list is empty') def test_print(self): self.assertEqual(self.ll.__str__(), '[]') self.ll.add(1) self.assertEqual(self.ll.__str__(), '[1]') self.ll.add(2) self.assertEqual(self.ll.__str__(), '[2 -> 1]') def test_add(self): self.assertIsNone(self.ll.add(1)) self.assertEqual(self.ll.__str__(), '[1]') self.assertIsNone(self.ll.add(2)) self.assertEqual(self.ll.__str__(), '[2 -> 1]') def test_insert(self): self.assertIsNone(self.ll.insert(0, 1)) self.assertEqual(self.ll.__str__(), '[1]') self.assertIsNone(self.ll.insert(1, 2)) self.assertEqual(self.ll.__str__(), '[1 -> 2]') self.assertIsNone(self.ll.insert(0, 0)) self.assertEqual(self.ll.__str__(), '[0 -> 1 -> 2]') def test_index(self): self.ll.insert(0, 2) self.assertEqual(self.ll.index(2), 0) self.ll.add(0) self.assertEqual(self.ll.index(0), 0) def test_searh(self): self.assertFalse(self.ll.search(2)) self.ll.add(2) self.assertTrue(self.ll.search(2)) def test_pop(self): self.ll.add(1) self.ll.add(2) self.ll.add(3) self.assertEqual(self.ll.pop(), 1) self.assertEqual(self.ll.pop(), 2) self.assertEqual(self.ll.pop(), 3) self.ll.add(1) self.ll.add(2) self.ll.add(3) self.assertEqual(self.ll.pop(0), 3) self.assertEqual(self.ll.pop(1), 1) self.assertEqual(self.ll.pop(0), 2) def test_size(self): self.assertEqual(self.ll.size(), 0) self.ll.add(1) self.ll.add(2) self.ll.add(3) self.assertEqual(self.ll.size(), 3) def test_delete(self): self.ll.add(1) self.ll.add(2) self.ll.add(3) self.assertIsNone(self.ll.delete(2)) self.assertEqual(self.ll.__str__(), '[3 -> 1]') self.assertIsNone(self.ll.delete(1)) self.assertEqual(self.ll.__str__(), '[3]') self.assertIsNone(self.ll.delete(3)) self.assertEqual(self.ll.__str__(), '[]') def test_append(self): self.assertIsNone(self.ll.append(1)) self.assertEqual(self.ll.__str__(), '[1]') self.assertIsNone(self.ll.append(2)) self.assertEqual(self.ll.__str__(), '[1 -> 2]')
class LinkedListTest(unittest.TestCase): def setUp(self) -> None: self.ll = LinkedList() # Creates a new linked list with the letters A through G inclusive. 7 elements total. Referred to as 'standard # fill' in comments. def fill_linked_list_with_data(self): self.ll = LinkedList(['A', 'B', 'C', 'D', 'E', 'F', 'G']) # Inserts the letter 'A' at index 0 in an empty list, checks to see if it is at the front of the list. def test_add_link_before_empty_list(self): self.ll.add_link_before("A", 0) self.assertEqual(self.ll.get_front(), "A") # Inserts A at index 0, B at index 1 and C at index 2. Asserts A is at the front and C is at the back. # test_add_link_before: Changed indexes to match assignment 3 guidelines example, does not allow for creating a # new node past the list's length def test_add_link_before(self): self.ll.add_link_before("A", 0) self.ll.add_link_before("B", 0) self.ll.add_link_before("C", 1) self.assertEqual(self.ll.get_front(), "B") self.assertEqual(self.ll.get_back(), "A") # Tries to insert Z at index 26 in an empty list, verifies an IndexError is raised. def test_add_link_before_out_of_bounds(self): with self.assertRaises(IndexError): self.ll.add_link_before('Z', 26) # Tries to insert the '@' symbol at index -2, verifies an IndexError is raised. def test_add_link_before_negative_index(self): with self.assertRaises(IndexError): self.ll.add_link_before("@", -2) # Inserts H, G and F at index 0. Verifies F is before G and G is before H. def test_add_link_before_multiple_times_same_index_empty_list(self): self.ll.add_link_before("H", 0) self.ll.add_link_before("G", 0) self.ll.add_link_before("F", 0) self.assertEqual(self.ll.__str__(), '[F -> G -> H]') # Tries to remove a link in an empty list, verifies an IndexError is raised. def test_remove_link_empty_list(self): with self.assertRaises(IndexError): self.ll.remove_link(1) # Tries to remove a link at a negative index, verifies an IndexError is raised. def test_remove_link_negative_index(self): with self.assertRaises(IndexError): self.ll.remove_link(-93) # Adds A, B and C to list respectively. Removes B. Verifies A is first, C is last and B is gone. def test_remove_link(self): self.ll.add_back("A") self.ll.add_back("B") self.ll.add_back("C") self.ll.remove_link(1) self.assertEqual("A", self.ll.get_front()) self.assertEqual("C", self.ll.get_back()) self.assertFalse(self.ll.contains("B")) # Adds A then B and C to front, respectively, verifies that C is at the front. def test_add_front(self): self.ll.add_front("A") self.ll.add_front("B") self.ll.add_front("C") self.assertEqual(self.ll.get_front(), "C") # Adds X, Y then Z to the back. Verifies that Z is at the back. def test_add_back(self): self.ll.add_back("X") self.ll.add_back("Y") self.ll.add_back("Z") self.assertEqual("Z", self.ll.get_back()) # Using the standard fill (A through G), verifies that get_front() returns A and doesn't remove it. def test_get_front(self): self.fill_linked_list_with_data() self.assertEqual(self.ll.get_front(), "A") self.assertEqual(self.ll.get_front(), "A") # Using the standard fill, verifies that get_back() returns G and doesn't remove it. def test_get_back(self): self.fill_linked_list_with_data() self.assertEqual(self.ll.get_back(), "G") self.assertEqual(self.ll.get_back(), "G") # Using standard fill, removes the front (A) and verifies that B is now the front. def test_remove_front(self): self.fill_linked_list_with_data() self.ll.remove_front() self.assertEqual(self.ll.get_front(), "B") # Using standard fill, removes back (G) and verifies that F is now the back. def test_remove_back(self): self.fill_linked_list_with_data() self.ll.remove_back() self.assertEqual(self.ll.get_back(), "F") # Verifies a list with data is not empty. def test_is_empty_on_non_empty_list(self): self.fill_linked_list_with_data() self.assertFalse(self.ll.is_empty()) # Verifies a list without data is empty. def test_is_empty_on_empty_list(self): self.assertTrue(self.ll.is_empty()) # Verifies a list that had data, and is now empty, is empty. def test_is_empty_on_empty_list_that_was_previously_populated(self): self.fill_linked_list_with_data() for i in range(7): self.ll.remove_front() self.assertTrue(self.ll.is_empty()) # Verifies a value not in list causes contains() to return False. def test_contains_value_not_in_list(self): self.fill_linked_list_with_data() self.assertFalse(self.ll.contains("X")) # Verifies a value in the list causes contains() to return True. def test_contains_value_in_list(self): self.fill_linked_list_with_data() self.assertTrue(self.ll.contains("C")) # Verifies that when a value is in the list more than once, contains(value) will still return True. def test_contains_duplicate_values_in_list(self): self.fill_linked_list_with_data() self.ll.add_back("A") self.assertTrue(self.ll.contains("A")) # Verifies that contains(value) reports False, when the value removed is the first value in the list. def test_contains_after_remove_first_item(self): self.fill_linked_list_with_data() self.ll.remove('A') self.assertFalse(self.ll.contains('A')) # Verifies that contains(value) reports False, when the value removed is the last value in the list. def test_contains_after_remove_last_item(self): self.fill_linked_list_with_data() self.ll.remove('G') self.assertFalse(self.ll.contains('G')) # Verifies that contains(value) reports False, when the value removed (by index) is the first value in the list. def test_contains_after_remove_link(self): self.fill_linked_list_with_data() self.ll.remove_link(0) self.assertFalse(self.ll.contains('A')) # Verifies that contains(value) reports False, when the value removed (by index) is the last value in the list. def test_contains_after_remove_link_end_of_list(self): self.fill_linked_list_with_data() self.ll.remove_link(6) self.assertFalse(self.ll.contains('G')) # Verify None is returned by get_front() when a value is removed from an empty list. def test_remove_empty_list(self): self.ll.remove("Z") self.assertEqual(self.ll.get_front(), None) # Verify when remove(value) works as expected. def test_remove_value_in_list(self): self.fill_linked_list_with_data() self.ll.remove("D") for i in range(3): self.ll.remove_front() self.assertEqual(self.ll.get_front(), "E") # Verify a populated list doesn't change when a value is removed which does not exist in the list. def test_remove_value_not_in_list(self): self.fill_linked_list_with_data() self.ll.remove('X') self.assertEqual(self.ll.__str__(), '[A -> B -> C -> D -> E -> F -> G]')
def test_str(): list = LinkedList() list.insert(1) list.insert(2) assert list.__str__() == "{ 2 } -> { 1 } -> NULL"
def test_print_diplays_linked_list_like_tuple(itr): """Test that print for LinkedList matches the __str__ for a tuple.""" from linked_list import LinkedList l = LinkedList(itr) assert l.__str__() == tuple(itr[::-1]).__str__()