Exemplo n.º 1
0
	def test_remove_dups(self):
		ae = self.assertEqual
		ll = linked_list.LinkedList()
		ll.insert("A")
		ll.insert("B")
		ll.insert("A")
		ll.insert("B")
		ll.insert("C")
		ll.remove_dups()
		ae(["C", "B", "A"], ll.plist())

		ll = linked_list.LinkedList()
		ll.insert("A")
		ll.insert("A")
		ll.insert("A")
		ll.remove_dups()
		ae(["A"], ll.plist())

		ll = linked_list.LinkedList()
		ll.insert("A")
		ll.insert("C")
		ll.insert("A")
		ll.remove_dups()
		ae(["A", "C"], ll.plist())

		ll = linked_list.LinkedList()
		ll.insert(1)
		ll.insert(0)
		ll.insert(2)
		ll.insert(2)
		ll.insert(2)
		ll.insert(1)
		ll.remove_dups()
		ae([1, 2, 0], ll.plist())
    def setUp(self):
        self.example1 = l.LinkedList()

        self.example2 = l.LinkedList()
        self.example2.insert_first(1)
        self.example2.insert_first(2)
        self.example2.insert_first(3)
        self.example2.insert_first(4)
Exemplo n.º 3
0
 def execute_test(self, name, impl):
     for x, y in self.testcases:
         merged = sorted(x + y)
         self.assertEqual(
             impl(linked_list.LinkedList(x),
                  linked_list.LinkedList(y)).as_list(),
             merged
         )
Exemplo n.º 4
0
    def setUp(self):
        A = linked_list.LinkedList([1, 2, 3])
        B = linked_list.LinkedList([4, 5, 6, 7, 8, 9, 10])
        B.next.next.next.next.next.next.next = B.next.next.next

        self.testcases = [
            (A, None),
            (B, B.next.next.next),
        ]
Exemplo n.º 5
0
 def testAddLinkedListInts(self):
     l1, l2 = llist.LinkedList(), llist.LinkedList()
     l1.add_from_list([3, 4, 5])
     l2.add_from_list([8, 4, 7, 9])
     self.assertEqual(
         llist.addLinkedListInts(l1, l2).to_list(), [1, 9, 2, 0, 1])
     l1, l2 = llist.LinkedList(), llist.LinkedList()
     l1.add_from_list([3])
     l2.add_from_list([8, 9, 9, 9])
     self.assertEqual(
         llist.addLinkedListInts(l1, l2).to_list(), [1, 0, 0, 0, 1])
Exemplo n.º 6
0
    def setUp(self):
        l1 = linked_list.LinkedList()
        l1.add_node(7)
        l1.add_node(1)
        l1.add_node(6)

        l2 = linked_list.LinkedList()
        l2.add_node(2)
        l2.add_node(1)
        l2.add_node(9)
        self.l1 = l1
        self.l2 = l2
Exemplo n.º 7
0
 def testPartition(self):
     x = llist.LinkedList()
     x.add_from_list([10, 3, 1, 4, 23, 42, 9])
     x.partition(5)
     self.assertEqual(x.to_list(), [3, 1, 4, 10, 23, 42, 9])
     # All values greater than partition
     x = llist.LinkedList()
     x.add_from_list([10, 3, 1, 4, 23, 42, 9])
     x.partition(0)
     self.assertEqual(x.to_list(), [10, 3, 1, 4, 23, 42, 9])
     # All values less than partition
     x = llist.LinkedList()
     x.add_from_list([10, 3, 1, 4, 23, 42, 9])
     x.partition(50)
     self.assertEqual(x.to_list(), [10, 3, 1, 4, 23, 42, 9])
Exemplo n.º 8
0
	def test_n_to_last(self):
		ae = self.assertEqual
		ll = linked_list.LinkedList()
		l = [ "A", "B", "C", "D" ]
		for e in l:
			ll.insert(e)
		ae("A", ll.n_to_last(0).data)
		ae("B", ll.n_to_last(1).data)
		ae("C", ll.n_to_last(2).data)
		ae("D", ll.n_to_last(3).data)
		ae(None, ll.n_to_last(4))

		ll = linked_list.LinkedList()
		ae(None, ll.n_to_last(4))
		ae(None, ll.n_to_last(0))
Exemplo n.º 9
0
def test_push(data, result_one, result_two):
    """Test to ensure push function works properly."""
    test_list = linked_list.LinkedList()
    for i in data:
        test_list.push(i)
    assert test_list.head.value == result_one
    assert test_list.head.next.next.value == result_two
Exemplo n.º 10
0
def test_prepend_and_append():
    ll = linked_list.LinkedList()
    ll.prepend(1)
    ll.append(2)
    while ll.current is not None:
        print(ll.current.value)
        ll.current = ll.current.next
Exemplo n.º 11
0
 def insert(self, person):
     asci_code = self.get_asci(person.get_name())
     index = asci_code % self.size
     if self.table[index] is None:
         self.table[index] = ll.LinkedList(person)
     else:
         self.table[index].append(person)
Exemplo n.º 12
0
def test_empty_list_insert():
    lst = linked_list.LinkedList()
    new_node = linked_list.LinkedNode("n")
    lst.insert(new_node)
    assert lst.root == new_node
    assert lst.root.prv == new_node
    assert lst.root.nxt == new_node
Exemplo n.º 13
0
def test_search():
    """Should return the node containing 'val' in the list, if present, else None"""
    llst = l.LinkedList()
    with pytest.raises(TypeError):
        llst.search()
        llst.search("adfe", 5)
    assert llst.search("AEREKLJRE") is None

    llst.insert(5)

    assert llst.search(5) is llst.head

    llst.insert(4)
    llst.insert(3)

    assert llst.search(5) is llst.head.next.next
    evalnode = llst.search(4)
    assert isinstance(evalnode, Node)
    assert evalnode.content == 4
    assert isinstance(evalnode.next, Node)
    evalnode = llst.search(5)
    assert evalnode.content == 5
    assert evalnode.next is None
    llst.pop()
    assert llst.search(3) is None
Exemplo n.º 14
0
def test_size():
    linked_list = LL.LinkedList()
    assert linked_list.size() == 0
    linked_list.insert(1)
    linked_list.insert(2)
    linked_list.insert(3)
    assert linked_list.size() == 3
Exemplo n.º 15
0
    def test_size(self):
        ll = linked_list.LinkedList()
        self.assertEqual(ll.size(), 0)
        ll.add('a')
        ll.add('b')

        self.assertEqual(ll.size(), 2)
Exemplo n.º 16
0
    def test_delete(self):
        """
        Should delete an element from the LinkedList, should print 2
        """

        # Set up some Elements
        e1 = linked_list.Element(1)
        e2 = linked_list.Element(2)
        e3 = linked_list.Element(3)
        e4 = linked_list.Element(4)

        # Start setting up a LinkedList
        ll = linked_list.LinkedList(e1)
        ll.append(e2)
        ll.append(e3)

        # Insert Element
        ll.insert(e4, 3)

        # Delete Element
        ll.delete(1)

        # Test
        result = ll.get_position(1).value
        self.assertEqual(result, 2)
        result = ll.get_position(2).value
        self.assertEqual(result, 4)
        result = ll.get_position(3).value
        self.assertEqual(result, 3)
Exemplo n.º 17
0
	def test_insert(self):
		ae = self.assertEqual
		ll = linked_list.LinkedList()
		ae(0, ll.size)
		ll.insert("A")
		ae(1, ll.size)
		ll.insert("B")
		ae(2, ll.size)
Exemplo n.º 18
0
def test_insert():
    """Should be able to insert something at the head of the list, fail if nothing"""
    llst = l.LinkedList()
    with pytest.raises(TypeError):
        llst.insert()

    #returns nothing
    assert llst.insert("blaguboo") is None
Exemplo n.º 19
0
def test_remove():
    linked_list = LL.LinkedList()
    linked_list.insert(1)
    linked_list.insert(2)
    linked_list.insert(3)
    node = linked_list.search(2)
    assert linked_list.remove(node) is not None
    assert linked_list.remove(node) == None
Exemplo n.º 20
0
 def test_contains(self):
     ll = linked_list.LinkedList()
     self.assertEqual(ll.contains('a'), False)
     ll.add('b')
     ll.add('a')
     self.assertEqual(ll.contains('b'), True)
     self.assertEqual(ll.contains('b'), True)
     self.assertEqual(ll.contains('c'), False)
Exemplo n.º 21
0
def test_list_insert():
    lst = linked_list.LinkedList(linked_list.LinkedNode(1))
    other_node = linked_list.LinkedNode("other")
    lst.insert(other_node)
    assert lst.length() == 2
    assert lst.root.nxt.data == "other"
    assert lst.root.prv == other_node
    assert lst.root.nxt.nxt == lst.root
Exemplo n.º 22
0
def test_remove_not_there():
    """Test to ensure remove function works properly when requested node is not in list."""
    test_list = linked_list.LinkedList()
    test_list.push('removed_node')
    removed_node = test_list.head
    test_list.pop()
    with pytest.raises(IndexError):
        test_list.remove(removed_node)
Exemplo n.º 23
0
def test_size():
    """Should return the correct size"""
    llst = l.LinkedList()
    assert llst.size() == 0
    for i in xrange(5):
        llst.insert(i)
    assert llst.size() == 5
    llst.pop()
    assert llst.size() == 4
Exemplo n.º 24
0
 def test_removeFirst(self):
     ll = linked_list.LinkedList()
     # Test out of bound
     with self.assertRaises(Exception):
         ll.removeFirst()
     ll.add('a')
     ll.add('b')
     ll.add('c')
     self.assertEqual(ll.removeFirst(), 'a')
Exemplo n.º 25
0
    def test_peekLast(self):
        ll = linked_list.LinkedList()
        with self.assertRaises(Exception) as context:
            ll.peekLast()

        ll.add('a')
        self.assertEqual(ll.peekLast(), 'a')
        ll.add('b')
        self.assertEqual(ll.peekLast(), 'b')
Exemplo n.º 26
0
 def testPop(self):
     n1, n2, n3 = ll.Node(1), ll.Node(2), ll.Node(3)
     llist = ll.LinkedList(n1)
     llist.insert_at_tail(n2)
     llist.insert_at_tail(n3)
     res = llist.pop()
     self.assertEqual(res.value, n1.value)
     self.assertEqual(res.next, None)
     self.assertEqual(n2.next, n3)
Exemplo n.º 27
0
def test_search():
    linked_list = LL.LinkedList()
    linked_list.insert(1)
    linked_list.insert(2)
    linked_list.insert(3)
    find = linked_list.search(2)
    expected = (2)
    actual = (find)
    assert expected, actual
Exemplo n.º 28
0
def initialize_linked_list_by_array(elements):

    lList = ll.LinkedList()

    for element in elements:
        lList.insert(element)

    lList.print_list()

    return lList.head
Exemplo n.º 29
0
    def test_indexOf(self):
        ll = linked_list.LinkedList()
        self.assertEqual(ll.indexOf('a'), -1)
        ll.add('b')
        ll.add('a')
        ll.add('c')

        self.assertEqual(ll.indexOf('a'), 1)
        self.assertEqual(ll.indexOf('b'), 0)
        self.assertEqual(ll.indexOf('c'), 2)
Exemplo n.º 30
0
 def testIsPalindrome(self):
     # Odd length palindrome
     x = llist.LinkedList()
     pal1 = ['m', 'a', 'd', 'a', 'm', 'i', 'm', 'a', 'd', 'a', 'm']
     x.add_from_list(pal1)
     self.assertTrue(x.isPalindrome())
     self.assertEqual(x.to_list(), pal1)
     # Even length palindrome
     x = llist.LinkedList()
     pal2 = ['a', 'n', 'n', 'a']
     x.add_from_list(pal2)
     self.assertTrue(x.isPalindrome())
     self.assertEqual(x.to_list(), pal2)
     # Not a palindrome
     x = llist.LinkedList()
     pal3 = ['a', 'n', 'n', 'i', 'e']
     x.add_from_list(pal3)
     self.assertFalse(x.isPalindrome())
     self.assertEqual(x.to_list(), pal3)