def test_node_constructor_one_diection(): """Test that there is a node that points at another node one direction.""" from dll import Node n = Node(0, Node(1)) assert n.val == 0 assert isinstance(n.nxt, Node) assert n.prev is None
def test_4(self): """ test that tail.next returns none """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.append(n1) dl.append(n2) self.assertEqual(dl.tail._next, None)
def test_node_constructor_both_diections(): """Test that there is a node that points at two different nodes.""" from dll import Node n = Node(0, Node(1), Node(2)) assert n.val == 0 assert isinstance(n.nxt, Node) assert isinstance(n.prev, Node) assert n.nxt is not n.prev
def test_2(self): """ test append by head """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.append(n1) dl.append(n2) self.assertEqual(dl.head._next._value, 3)
def test_3(self): """ test append by tail """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.append(n1) dl.append(n2) self.assertEqual(dl.tail._value, 3)
def test_4(self): """check that head is still the first node added to list""" n1 = Node('A') n2 = Node('B') dl = DoubleLinkedList() dl.append(n1) dl.append(n2) self.assertEqual(dl.head._value, 'A')
def test_3(self): """add two nodes. test that 2nd append makes changes tail to new node""" n1 = Node('A') n2 = Node('B') dl = DoubleLinkedList() dl.append(n1) dl.append(n2) self.assertEqual(dl.tail._value, 'B')
def test_6(self): """ prev of prior head is now node 2 (n1 here) """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.push(n1) dl.push(n2) self.assertEqual(n1._prev._value, 3)
def test_5(self): """ added node is now head """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.push(n1) dl.push(n2) self.assertEqual(dl.head._value, 3)
def test_7(self): """ next of prior head is now head """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.push(n1) dl.push(n2) dl.pop() self.assertEqual(dl.head._value, 5)
def test_8(self): """ prev of prior tail is now tail """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) dl.push(n1) dl.push(n2) dl.shift() self.assertEqual(dl.tail._value, 3)
def test_6(self): """check that pop removes head""" n1 = Node('A') n2 = Node('B') dl = DoubleLinkedList() dl.append(n1) #head and tail at this point dl.append(n2) # A is head and B is now tail dl.pop() # removes A so head value should be B self.assertEqual(dl.head._value, 'B')
def test_5(self): """check that push adds to front""" n1 = Node('A') n2 = Node('B') dl = DoubleLinkedList() dl.append(n1) dl.append(n2) n3 = Node('C') #will push C to front(head) dl.push(n3) self.assertEqual(dl.head._value, 'C')
def test_9(self): """ remove by value, removed node's next now next of removed node's prev """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) n3 = Node(6) dl.append(n1) dl.append(n2) dl.append(n3) dl.remove(3) self.assertEqual(dl.head._next._value, 6)
def test_7(self): """check that shift removes last node""" n1 = Node('A') n2 = Node('B') n3 = Node('C') dl = DoubleLinkedList() dl.append(n1) dl.append(n2) dl.append(n3) dl.shift() self.assertEqual(dl.tail._value, 'B')
def test_10(self): """ remove by value, return an exception if doesn't exist """ dl = DoubleLinkedList() n1 = Node(5) n2 = Node(3) n3 = Node(6) dl.append(n1) dl.append(n2) dl.append(n3) dl.remove(7) self.assertEqual(dl.remove(7), "Node does not exist!")
def test_node_init(): """Test node class init.""" from dll import Node new_node = Node(0, None, None) assert new_node.contents == 0 assert new_node.next_node is None assert new_node.previous_node is None
def test_node_constructor_with_no_nxt(): """Test that there is a node that points at None.""" from dll import Node n = Node(0) assert n.val == 0 assert n.nxt is None assert n.prev is None
def test_8(self): """test to remove tail by using remove method""" n1 = Node('A') n2 = Node('B') n3 = Node('C') dl = DoubleLinkedList() dl.append(n1) dl.append(n2) dl.append(n3) dl.remove('C') #this removes C so tail should become BaseException self.assertEqual(dl.tail._value, 'B')
def test_delete(self): n = 5 for i in range(n): self.dll.append(i) # Delete existing value node = self.dll.find(2) prev = node.prev next = node.next val = self.dll.delete(node) self.assertEqual(2, val, "Returned value should be 2") self.assertEqual(n - 1, len(self.dll), "Length should be " + str(n - 1)) self.assertIsNone(self.dll.find(2), "There should be no value of 2 in the dll") self.assertEqual(next, prev.next, "Links previous to next node") self.assertEqual(prev, next.prev, "Links next node to previous") # Delete non existing value node = Node(2) val = self.dll.delete(node) self.assertIsNone(val, "Should return None") self.assertEqual(n - 1, len(self.dll), "Length should still be " + str(n - 1)) # print(self.dll) # Delete first node node = self.dll.find(0) next = node.next self.dll.delete(node) self.assertEqual(len(self.dll), n - 2, "Length should be " + str(n - 2)) self.assertIsNone(self.dll.find(0), "There should be no value of 0 in the dll") for elem in self.dll: self.assertEqual(next.value, elem, "Next should now be at the HEAD") break
def sortedInsert(dll, data): newNode = Node(data) if dll.head and newNode.data < dll.head.data: newNode.next = dll.head newNode.next.prev = newNode dll.head = newNode return current = dll.head while current and current.next: if current.next.data > newNode.data: break current = current.next if current is None: dll.head = newNode elif current.next is None: current.next = newNode newNode.prev = current dll.tail = newNode else: newNode.next = current.next newNode.prev = current newNode.next.prev = newNode current.next = newNode
def test_test_previous_node(): """Test whether creating a previous with another previous node.""" test_node2 = Node(10) test_node1 = Node(20, previous_node=test_node2) assert test_node1.previous_node.value == 10
def test_test_next_node(): """Test whether creating a next with another node.""" test_node1 = Node(15) test_node2 = Node(10, next_node=test_node1) assert test_node2.next_node.value == 15
def test_create_empty_node(): """Test whether we create a node with a value 'val'.""" test_node = Node() assert test_node.value is None
def test_create_node(): """Test whether we create a node with a value 'val'.""" test_node = Node(15) assert test_node.value == 15
def test_node(): """Assert node class is created with specified val.""" from dll import Node new_node = Node(4) assert new_node.val == 4
def test_1(self): """make a node""" n1 = Node('A') self.assertEqual(n1._value, 'A')
def test_init_empty_node(): """Test a new empty node is None.""" from dll import Node new_node = Node() assert new_node.value is None
def test_1(self): n1 = Node(5) self.assertEqual(n1.value(), 5)
def test_init_node(): """Test a new node with value has correct value.""" from dll import Node new_node = Node(5) assert new_node.value == 5