Beispiel #1
0
    def test_setitem(self):
        l = UnrolledLinkedList(5)
        for i in range(11):
            l.append(i)

        self.assertRaises(TypeError, l.__setitem__, '0', 99)
        self.assertRaises(IndexError, l.__setitem__, 11, 99)
        self.assertRaises(IndexError, l.__setitem__, -12, 99)

        l[0] = 100
        self.assertEqual(100, l[0])
        l[1] = 101
        self.assertEqual(101, l[1])
        l[2] = 102
        self.assertEqual(102, l[2])
        l[4] = 104
        self.assertEqual(104, l[4])
        l[5] = 105
        self.assertEqual(105, l[5])
        self.assertEqual(105, l[-6])
        l[-5] = 106
        self.assertEqual(106, l[-5])
        l[-4] = 107
        self.assertEqual(107, l[-4])
        l[-2] = 109
        self.assertEqual(109, l[-2])
        l[-1] = 110
        self.assertEqual(110, l[-1])

        self.assertEqual('{[100, 101, 102], [3, 104, 105], [106, 107, 8, 109, 110]}', str(l))
Beispiel #2
0
    def test_iter(self):
        l = UnrolledLinkedList()

        i = 0
        for x in l:
            i += 1
        self.assertEqual(0, i)

        for i in range(50):
            l.append(i)

        for i, x in enumerate(l):
            self.assertEqual(i, x)
            self.assertEqual(l[i], x)

        del l[40]
        del l[30]
        del l[20]
        del l[10]
        del l[0]

        i = 0
        for x in l:
            if i % 10 == 0:
                self.assertNotEqual(i, x)
                i += 1
                self.assertEqual(i, x)
            else:
                self.assertEqual(i, x)
            i += 1
Beispiel #3
0
    def test_mul(self):
        l = UnrolledLinkedList(6)
        l.append(1)
        l.append(2)

        self.assertRaises(TypeError, l.__mul__, '4')

        l *= 1
        self.assertEqual(2, len(l))
        self.assertEqual(6, l.max_node_capacity)
        self.assertEqual('{[1, 2]}', str(l))

        list2 = l * 4
        self.assertEqual(8, len(list2))
        self.assertEqual(6, list2.max_node_capacity)
        self.assertEqual('{[1, 2, 1], [2, 1, 2, 1, 2]}', str(list2))

        l *= 0
        self.assertEqual(0, len(l))
        self.assertEqual(6, l.max_node_capacity)

        l = UnrolledLinkedList()

        l *= 10
        self.assertEqual(0, len(l))
    def test_del_item_with_max_six(self):
        '''
            RULES FOR DELETING
            When an element is deleted from the list it is simply removed
            from the array. If the number of elements in the array falls below
            N/2, we take the elements from a neighboring array to fill the
            array. If neighboring array also has N/2 elements then we merge
            both of the arrays.

            in Python integer division rounds down
            3 / 2 = 1
        '''
        ull = UnrolledLinkedList(max_node_capacity=6)
        for x in range(0, 20):
            ull.append(x)
        self.assertEqual(20, len(ull))
        self.assertEqual('{[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18, 19]}', str(ull))

        # delete middle of a node.
        # node size will be 2 which is < 3, so should readjust
        del ull[4]
        self.assertEqual(19, len(ull))
        self.assertEqual('{[0, 1, 2], [3, 5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18, 19]}', str(ull))

        # delete last element
        # size of node will be 4 which is > 3 so no adjust
        del ull[-1]
        self.assertEqual(18, len(ull))
        self.assertEqual('{[0, 1, 2], [3, 5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18]}', str(ull))

        # delete first element
        # node size will be 2 which is < 3, so should readjust
        del ull[0]
        self.assertEqual(17, len(ull))
        self.assertEqual('{[1, 2, 3], [5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18]}', str(ull))
Beispiel #5
0
    def test_str(self):
        l = UnrolledLinkedList(4)
        self.assertEqual('{}', str(l))

        for i in range(1, 8):
            l.append(i)

        self.assertEqual('{[1, 2], [3, 4], [5, 6, 7]}', str(l))
Beispiel #6
0
 def test_ull_two(self):
     test_list = UnrolledLinkedList(2);
     test_list.append(0)
     test_list.append(1)
     test_list.append(2)
     test_list.append(3)
     assert test_list[0] == 0
     assert test_list[1] == 1
     assert test_list[2] == 2
     assert test_list[3] == 3
     assert str(test_list) == "{[0], [1], [2, 3]}"
Beispiel #7
0
    def test_get_item_with_negative_index(self):
        ull = UnrolledLinkedList()
        ull.append(5)
        ull.append(10)
        self.assertEqual(2, len(ull))
        self.assertEqual('{[5, 10]}', str(ull))

        self.assertEqual(5, ull[-2])
        self.assertEqual(10, ull[-1])

        # test out of bounds
        with self.assertRaises(IndexError):
            ull[-1000000]
Beispiel #8
0
    def test_getitem(self):
        l = UnrolledLinkedList(3)
        for i in range(9):
            l.append(i)

        self.assertRaises(TypeError, l.__getitem__, '0')
        self.assertRaises(IndexError, l.__getitem__, 9)
        self.assertRaises(IndexError, l.__getitem__, -10)
        
        for i in range(9):
            self.assertEqual(i, l[i])
        self.assertEqual(8, l[-1])
        self.assertEqual(5, l[-4])
    def test_get_item_with_negative_index(self):
        ull = UnrolledLinkedList()
        ull.append(5)
        ull.append(10)
        self.assertEqual(2, len(ull))
        self.assertEqual('{[5, 10]}', str(ull))

        self.assertEqual(5, ull[-2])
        self.assertEqual(10, ull[-1])

        # test out of bounds
        with self.assertRaises(IndexError):
            ull[-1000000]
Beispiel #10
0
    def test_add_two_lists(self):

        list1 = self.getDefaultList()

        #print "TESTING ADDED 2 LISTS TOGETHER"
        list2 = UnrolledLinkedList(5)
        list2.append(5)
        list2.append(6)
        list2.append(7)
        list2.append(8)
        list2.append(9)

        #print str(list1)
        #print str(list2)

        combinedList = list1 + list2
        #print str(combinedList)
        self.assertEquals(str(combinedList), "{[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]}")
        assert 4 in combinedList
        assert 8 in combinedList
        self.assertEquals(combinedList[-6], 4)
        self.assertEquals(combinedList[5], 5)
        self.assertEquals(len(combinedList), 10)
        list4 = UnrolledLinkedList(2)
        list4.append(10)
        list4.append(11)
        combinedList += list4
Beispiel #11
0
    def test_add_two_lists(self):

        list1 = self.getDefaultList()

        #print "TESTING ADDED 2 LISTS TOGETHER"
        list2 = UnrolledLinkedList(5)
        list2.append(5)
        list2.append(6)
        list2.append(7)
        list2.append(8)
        list2.append(9)

        #print str(list1)
        #print str(list2)

        combinedList = list1 + list2
        #print str(combinedList)
        self.assertEquals(str(combinedList),
                          "{[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]}")
        assert 4 in combinedList
        assert 8 in combinedList
        self.assertEquals(combinedList[-6], 4)
        self.assertEquals(combinedList[5], 5)
        self.assertEquals(len(combinedList), 10)
        list4 = UnrolledLinkedList(2)
        list4.append(10)
        list4.append(11)
        combinedList += list4
Beispiel #12
0
    def test_split(self):
        l = UnrolledLinkedList(4)
        for i in range(1, 6):
            l.append(i)
        self.assertEqual(5, len(l))
        self.assertNotEqual(l.head, l.tail)
        self.assertEqual([1, 2], l.head.data_list)
        self.assertEqual([3, 4, 5], l.tail.data_list)

        l.append(6)
        l.append(7)
        self.assertEqual(7, len(l))
        self.assertNotEqual(l.head.next_node, l.tail)
        self.assertEqual([3, 4], l.head.next_node.data_list)
        self.assertEqual([5, 6, 7], l.tail.data_list)
Beispiel #13
0
    def test_reversed(self):
        l = UnrolledLinkedList()

        i = 0
        for x in reversed(l):
            i += 1
        self.assertEqual(0, i)

        for i in range(50):
            l.append(i)

        i = 1
        for x in reversed(l):
            self.assertEqual(l[-i], x)
            i += 1
Beispiel #14
0
    def test_smoke_iters(self):
        test_list = UnrolledLinkedList(4)
        for i in xrange(0, 16):
            test_list.append(i)

        # Forwards
        counter = 0
        for item in test_list:
            assert item == counter
            counter += 1

        # Reverse
        counter = 15
        for item in reversed(test_list):
            assert item == counter
            counter -= 1
Beispiel #15
0
 def test_insert_with_max_four(self):
     ull = UnrolledLinkedList(max_node_capacity=4)
     self.assertEqual(0, len(ull))
     ull.append(5)
     self.assertEqual(1, len(ull))
     self.assertEqual('{[5]}', str(ull))
     ull.append(10)
     self.assertEqual(2, len(ull))
     self.assertEqual('{[5, 10]}', str(ull))
     ull.append(3)
     self.assertEqual(3, len(ull))
     self.assertEqual('{[5, 10, 3]}', str(ull))
     ull.append(8)
     self.assertEqual(4, len(ull))
     self.assertEqual('{[5, 10, 3, 8]}', str(ull))
     ull.append(1)
     self.assertEqual(5, len(ull))
     self.assertEqual('{[5, 10], [3, 8, 1]}', str(ull))
Beispiel #16
0
    def test_contains(self):
        l = UnrolledLinkedList(5)

        self.assertFalse(0 in l)

        l.append(0)
        self.assertTrue(0 in l)

        for i in range(5):
            l.append(i)

        self.assertTrue(4 in l)
        self.assertTrue(3 in l)
        self.assertTrue(2 in l)
        self.assertTrue(1 in l)

        self.assertFalse(5 in l)
        self.assertFalse(-1 in l)
 def test_insert_with_max_four(self):
     ull = UnrolledLinkedList(max_node_capacity=4)
     self.assertEqual(0, len(ull))
     ull.append(5)
     self.assertEqual(1, len(ull))
     self.assertEqual('{[5]}', str(ull))
     ull.append(10)
     self.assertEqual(2, len(ull))
     self.assertEqual('{[5, 10]}', str(ull))
     ull.append(3)
     self.assertEqual(3, len(ull))
     self.assertEqual('{[5, 10, 3]}', str(ull))
     ull.append(8)
     self.assertEqual(4, len(ull))
     self.assertEqual('{[5, 10, 3, 8]}', str(ull))
     ull.append(1)
     self.assertEqual(5, len(ull))
     self.assertEqual('{[5, 10], [3, 8, 1]}', str(ull))
Beispiel #18
0
    def test_del_item_with_max_six(self):
        '''
            RULES FOR DELETING
            When an element is deleted from the list it is simply removed
            from the array. If the number of elements in the array falls below
            N/2, we take the elements from a neighboring array to fill the
            array. If neighboring array also has N/2 elements then we merge
            both of the arrays.

            in Python integer division rounds down
            3 / 2 = 1
        '''
        ull = UnrolledLinkedList(max_node_capacity=6)
        for x in range(0, 20):
            ull.append(x)
        self.assertEqual(20, len(ull))
        self.assertEqual(
            '{[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18, 19]}',
            str(ull))

        # delete middle of a node.
        # node size will be 2 which is < 3, so should readjust
        del ull[4]
        self.assertEqual(19, len(ull))
        self.assertEqual(
            '{[0, 1, 2], [3, 5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18, 19]}',
            str(ull))

        # delete last element
        # size of node will be 4 which is > 3 so no adjust
        del ull[-1]
        self.assertEqual(18, len(ull))
        self.assertEqual(
            '{[0, 1, 2], [3, 5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18]}',
            str(ull))

        # delete first element
        # node size will be 2 which is < 3, so should readjust
        del ull[0]
        self.assertEqual(17, len(ull))
        self.assertEqual(
            '{[1, 2, 3], [5, 6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17, 18]}',
            str(ull))
Beispiel #19
0
 def test_slicing(self):
     #print "TESTING SLICING"
     sliceList = UnrolledLinkedList(4)
     sliceList.append(0)
     sliceList.append(1)
     sliceList.append(2)
     sliceList.append(3)
     sliceList.append(4)
     sliceList.append(5)
     sliceList.append(6)
     #print str(sliceList)
     sliceList = sliceList[-1:]
Beispiel #20
0
 def test_slicing(self):
     #print "TESTING SLICING"
     sliceList = UnrolledLinkedList(4)
     sliceList.append(0)
     sliceList.append(1)
     sliceList.append(2)
     sliceList.append(3)
     sliceList.append(4)
     sliceList.append(5)
     sliceList.append(6)
     #print str(sliceList)
     sliceList = sliceList[-1:]
Beispiel #21
0
    def test_general(self):
        l = UnrolledLinkedList(16)
        for i in range(1000):
            l.append(i)

        self.assertEqual(1000, len(l))
        self.assertTrue(999 in l)
        self.assertTrue(50 in l)

        for i in range(250, 750):
            l[i] = 'delete me'

        self.assertEqual(1000, len(l))
        self.assertTrue('delete me' in l)
        self.assertEqual(249, l[249])
        self.assertEqual('delete me', l[250])
        self.assertEqual('delete me', l[251])
        self.assertEqual('delete me', l[748])
        self.assertEqual('delete me', l[749])
        self.assertEqual(750, l[750])

        for i in range(500):
            del l[250]

        self.assertEqual(500, len(l))
        self.assertFalse('delete me' in l)
        self.assertTrue(750, l[250])
        self.assertTrue(499, 999)

        for i in range(10):
            del l[499 - (i * 40)]

        self.assertEqual(490, len(l))
        self.assertFalse(999 in l)

        for i in range(489):
            del l[0]

        self.assertEqual(1, len(l))
        self.assertTrue(998 in l)
        self.assertTrue(998, l[0])
Beispiel #22
0
    def test_setitem_slice(self):
        l = UnrolledLinkedList(10)
        for i in range(5):
            l.append(i)

        t = UnrolledLinkedList(2)
        for i in range(10):
            t.append(9)

        l[2:3] = t

        self.assertEqual(14, len(l))
        self.assertEqual(1, l[1])
        for i in range(2, 12):
            self.assertEqual(9, l[i])
        self.assertEqual(3, l[12])

        t = UnrolledLinkedList(2)
        t.append(2)

        l[2:12] = t
        self.assertEqual(5, len(l))
        self.assertEqual('{[0, 1, 2, 3, 4]}', str(l))

        l[:] = t
        self.assertEqual(1, len(l))
        self.assertEqual('{[2]}', str(l))

        l = UnrolledLinkedList(5)
        test_list = [1, 2, 6, 9, 6, 1, 0, 8, 1, 8, 2, 6]
        for x in test_list:
            l.append(x)

        t = UnrolledLinkedList(3)
        test_list2 = [3, 6, 8, 4]
        for x in test_list2:
            t.append(x)

        l[0:2] = t
        self.assertEqual(14, len(l))
Beispiel #23
0
    def test_add(self):
        list1 = UnrolledLinkedList(4)
        list2 = ['not', 'an', 'UnrolledLinkedList']
        self.assertRaises(TypeError, list1.__add__, list2)

        list2 = UnrolledLinkedList(4)

        test_list = list1 + list2
        self.assertEqual(0, len(test_list))

        st = 'abcde'
        for c in st:
            list2.append(c)

        list1 += list2
        self.assertEqual(5, len(list1))

        list2 = UnrolledLinkedList()
        list2 = list1 + list2
        self.assertEqual(5, len(list2))

        list1 = UnrolledLinkedList(4)
        for i in range(5):
            list1.append(i)
        list2 = UnrolledLinkedList(10)
        for i in range(5, 21):
            list2.append(i)

        test_list = list1 + list2

        self.assertEqual(len(list1) + len(list2), len(test_list))
        self.assertEqual([18, 19, 20], test_list.tail.data_list)
Beispiel #24
0
    def test_getitem_slice(self):
        l = UnrolledLinkedList(5)
        for x in range(20):
            l.append(x)

        new_list = l[5:10]
        self.assertEqual(5, len(new_list))
        self.assertEqual(20, len(l))
        self.assertEqual('{[5, 6, 7, 8, 9]}', str(new_list))

        new_list = l[:]
        self.assertEqual(len(l), len(new_list))
        self.assertEqual(str(l), str(new_list))

        new_list = l[::2]
        self.assertEqual(10, len(new_list))
        for i in range(10):
            self.assertEqual(i*2, new_list[i])

        new_list = l[1::2]
        self.assertEqual(10, len(new_list))
        for i in range(0, 10):
            self.assertEqual((i*2)+1, new_list[i])
Beispiel #25
0
 def getDefaultList(self):
     newList = UnrolledLinkedList(3)
     newList.append(0)
     newList.append(1)
     newList.append(2)
     newList.append(3)
     newList.append(4)
     #{[0, 1], [2, 3, 4]}
     return newList
Beispiel #26
0
 def getDefaultList(self):
     newList = UnrolledLinkedList(3)
     newList.append(0)
     newList.append(1)
     newList.append(2)
     newList.append(3)
     newList.append(4)
     #{[0, 1], [2, 3, 4]}
     return newList
Beispiel #27
0
 def test_append(self):
     list1 = UnrolledLinkedList(6)
     #print "TESTING APPEND"
     list1.append(1)
     list1.append(2)
     list1.append(3)
     list1.append(4)
     list1.append(5)
     list1.append(6)
     #print str(list1)
     self.assertEquals(len(list1), 6)
Beispiel #28
0
 def test_append(self):
     list1 = UnrolledLinkedList(6)
     #print "TESTING APPEND"
     list1.append(1)
     list1.append(2)
     list1.append(3)
     list1.append(4)
     list1.append(5)
     list1.append(6)
     #print str(list1)
     self.assertEquals(len(list1), 6)
Beispiel #29
0
    def test_append(self):
        l = UnrolledLinkedList(4)
        l.append(1)
        self.assertEqual(1, len(l))
        self.assertEqual([1], l.head.data_list)

        l.append(2)
        self.assertEqual(2, len(l), )
        self.assertEqual([1, 2], l.head.data_list)

        l.append(3)
        l.append(4)
        self.assertEqual(4, len(l))
        self.assertEqual([1, 2, 3, 4], l.head.data_list)

        l = UnrolledLinkedList(5)
        for i in range(11):
            l.append(i)
        self.assertEqual('{[0, 1, 2], [3, 4, 5], [6, 7, 8, 9, 10]}', str(l))
Beispiel #30
0
    def contains_test(self):
        list_one = UnrolledLinkedList(max_node_capacity=8)

        list_one.append(5)
        list_one.append(6)
        list_one.append(7)
        list_one.append(8)
        list_one.append(9)

        self.assertTrue(5 in list_one)
        self.asserTrue(8 in list_one)
        self.assertFalse(100 in list_one)
    def contains_test(self):
        list_one = UnrolledLinkedList(max_node_capacity=8)

        list_one.append(5)
        list_one.append(6)
        list_one.append(7)
        list_one.append(8)
        list_one.append(9)

        self.assertTrue(5 in list_one)
        self.asserTrue(8 in list_one)
        self.assertFalse(100 in list_one)
    def test_set_item_positive_index(self):  # failed
        ull = UnrolledLinkedList(max_node_capacity=3)
        self.assertEqual(0, len(ull))
        ull.append(5)
        ull.append(10)
        ull.append(3)
        ull.append(8)
        ull.append(1)
        self.assertEqual(5, len(ull))
        self.assertEqual('{[5, 10], [3, 8, 1]}', str(ull))

        ull[0] = 0
        self.assertEqual(5, len(ull))
        self.assertEqual(0, ull[0])
        self.assertEqual('{[0, 10], [3, 8, 1]}', str(ull))

        ull[1] = 1
        self.assertEqual(5, len(ull))
        self.assertEqual(1, ull[1])
        self.assertEqual('{[0, 1], [3, 8, 1]}', str(ull))
Beispiel #33
0
    def test_complex_add(self):
        ull1 = UnrolledLinkedList()
        ull2 = UnrolledLinkedList()

        ull1.append(1)
        ull1.append(2)
        ull1.append(3)
        ull1.append(4)
        self.assertEqual(4, len(ull1))

        ull2.append(10)
        ull2.append(20)
        ull2.append(30)
        ull2.append(40)
        self.assertEqual(4, len(ull2))

        # must rebalance
        ull3 = ull1 + ull2
        self.assertEqual(8, len(ull3))
        self.assertEqual('{[1, 2, 3, 4, 10, 20, 30, 40]}', str(ull3))
    def test_complex_add(self):
        ull1 = UnrolledLinkedList()
        ull2 = UnrolledLinkedList()

        ull1.append(1)
        ull1.append(2)
        ull1.append(3)
        ull1.append(4)
        self.assertEqual(4, len(ull1))

        ull2.append(10)
        ull2.append(20)
        ull2.append(30)
        ull2.append(40)
        self.assertEqual(4, len(ull2))

        # must rebalance
        ull3 = ull1 + ull2
        self.assertEqual(8, len(ull3))
        self.assertEqual('{[1, 2, 3, 4, 10, 20, 30, 40]}', str(ull3))
Beispiel #35
0
    def test_set_item_positive_index(self):  # failed
        ull = UnrolledLinkedList(max_node_capacity=3)
        self.assertEqual(0, len(ull))
        ull.append(5)
        ull.append(10)
        ull.append(3)
        ull.append(8)
        ull.append(1)
        self.assertEqual(5, len(ull))
        self.assertEqual('{[5, 10], [3, 8, 1]}', str(ull))

        ull[0] = 0
        self.assertEqual(5, len(ull))
        self.assertEqual(0, ull[0])
        self.assertEqual('{[0, 10], [3, 8, 1]}', str(ull))

        ull[1] = 1
        self.assertEqual(5, len(ull))
        self.assertEqual(1, ull[1])
        self.assertEqual('{[0, 1], [3, 8, 1]}', str(ull))
Beispiel #36
0
    def test_mult(self):
        combinedList = self.getDefaultList()

        length = len(combinedList)
        #print "TESTING *="
        combinedList *= 2
        #print str(combinedList)
        self.assertEquals(length*2, len(combinedList))

        #print "TESTING STRING LISTS"
        list3 = UnrolledLinkedList(4)
        list3.append('x')
        list3.append('x')
        list3.append('x')
        list3.append('x')
        list3.append('x')
        #print str(list3)
        del list3[2]
        #print str(list3)
        del list3[1]
Beispiel #37
0
    def test_mult(self):
        combinedList = self.getDefaultList()

        length = len(combinedList)
        #print "TESTING *="
        combinedList *= 2
        #print str(combinedList)
        self.assertEquals(length * 2, len(combinedList))

        #print "TESTING STRING LISTS"
        list3 = UnrolledLinkedList(4)
        list3.append('x')
        list3.append('x')
        list3.append('x')
        list3.append('x')
        list3.append('x')
        #print str(list3)
        del list3[2]
        #print str(list3)
        del list3[1]
Beispiel #38
0
    def test_smoke_mult(self):
        one = UnrolledLinkedList(4)
        one.append(0)
        one.append(1)
        one.append(2)
        one.append(3)
        one.append(4)
        one.append(5)

        one *= 1
        assert str(one) == "{[0, 1], [2, 3, 4, 5]}"

        one *= 4
        assert str(one) == "{[0, 1], [2, 3], [4, 5], [0, 1], [2, 3], [4, 5], [0, 1], [2, 3], [4, 5], [0, 1], [2, 3, 4, 5]}"

        # Test error raising for __mult__
        error_thrown = False
        try:
            two = UnrolledLinkedList
            one *= two
        except TypeError as te:
            assert str(te) == "Count parameter must be an int"
            error_thrown = True
        assert error_thrown is True
 def test_empty(self):
     list_five = UnrolledLinkedList(max_node_capacity=3)
     self.assertEqual(str(list_five), "{}")
     list_five.append(1)
     del list_five[0]
     self.assertEqual(str(list_five), "{}")
Beispiel #40
0
 def test_empty(self):
     list_five = UnrolledLinkedList(max_node_capacity=3)
     self.assertEqual(str(list_five), "{}")
     list_five.append(1)
     del list_five[0]
     self.assertEqual(str(list_five), "{}")
Beispiel #41
0
    def test_smoke_add_lists(self):
        one = UnrolledLinkedList(5)
        two = UnrolledLinkedList(5)

        one.append(0)
        one.append(1)
        one.append(2)
        one.append(3)
        one.append(4)
        one.append(5)

        two.append(10)
        two.append(11)
        two.append(12)
        two.append(13)
        two.append(14)
        two.append(15)

        one += two
        assert str(one) == "{[0, 1, 2], [3, 4, 5], [10, 11, 12], [13, 14, 15]}"

        # Test error raising for __add__
        error_thrown = False
        try:
            one += 'hello'
        except TypeError as te:
            assert str(te) == "Other parameter is not of type UnrolledLinkedList"
            error_thrown = True
        assert error_thrown is True
Beispiel #42
0
    def test_smoke_single_list(self):
        # Test modifying max_node_capacity parameter
        test_list = UnrolledLinkedList(3)

        # Test append
        test_list.append(0)
        test_list.append(1)
        test_list.append(2)
        test_list.append(3)
        test_list.append(4)
        test_list.append(5)

        # Test __contains__
        assert 0 in test_list
        assert 1 in test_list
        assert 2 in test_list
        assert 3 in test_list
        assert 4 in test_list
        assert 5 in test_list
        assert 6 not in test_list

        # Test __get__
        assert test_list[0] == 0
        assert test_list[1] == 1
        assert test_list[2] == 2
        assert test_list[3] == 3
        assert test_list[4] == 4
        assert test_list[5] == 5

        # Test __get__ negative index
        assert test_list[-1] == 5
        assert test_list[-2] == 4
        assert test_list[-3] == 3
        assert test_list[-4] == 2
        assert test_list[-5] == 1
        assert test_list[-6] == 0

        error_thrown1 = False
        try:
            assert test_list[-7]
        except IndexError as ie:
            assert str(ie) == "Negative index given is out of array bounds"
            error_thrown1 = True
        assert error_thrown1 is True

        # Test __str__
        assert str(test_list) == "{[0, 1], [2, 3], [4, 5]}"

        # Test __set__
        test_list[-6] = 10
        test_list[1] = 11
        test_list[2] = 12
        test_list[3] = 13
        test_list[4] = 14
        test_list[5] = 15
        assert test_list[0] == 10
        assert test_list[1] == 11
        assert test_list[2] == 12
        assert test_list[3] == 13
        assert test_list[4] == 14
        assert test_list[5] == 15

        # Test error raising for __get__ function
        error_thrown1 = False
        try:
            temp = test_list['a']
        except TypeError as te:
            assert str(te) == "Indices can only be of type int"
            error_thrown1 = True
        assert error_thrown1 is True

        error_thrown1 = False
        try:
            temp = test_list[5000]
        except IndexError as ie:
            assert str(ie) == "Given index is out of list bounds"
            error_thrown1 = True
        assert error_thrown1 is True

        # Test error raising for __set__ function
        error_thrown1 = False
        try:
            test_list['a'] == 350
        except TypeError as te:
            assert str(te) == "Indices can only be of type int"
            error_thrown1 = True
        assert error_thrown1 is True

        error_thrown1 = False
        try:
            test_list[5000] = -1
        except IndexError as ie:
            assert str(ie) == "Given index is out of list bounds"
            error_thrown1 = True
        assert error_thrown1 is True

        # Test __len__
        assert len(test_list) is 6

        # Test delete
        del test_list[0]
        assert str(test_list) == "{[11, 12, 13], [14, 15]}"
        test_list.append(16)
        del test_list[1]
        # Make sure a middle node gets removed correctly
        assert str(test_list) == "{[11, 13], [14, 15, 16]}"
        del test_list[2]
        assert str(test_list) == "{[11, 13], [15, 16]}"
        test_list.append(17)
        del test_list[4]
        assert str(test_list) == "{[11, 13], [15, 16]}"
        del test_list[3]
        # This is a funny case where the node being deleted from is the
        # last node. None of the resources that we have been given access
        # to describing the behavior of ULLs if the current
        # node is less than half full and there is no next node. All the directions
        # given are concerned with the case where the current node has a next node
        # that it can pull additional elements from. Since this case isn't described,
        # it is assumed that the end node is to be left alone.
        assert str(test_list) == "{[11, 13], [15]}"
        # Make sure the end node gets deleted
        del test_list[2]
        assert str(test_list) == "{[11, 13]}"

        # Test deleting from end using negative indices
        del test_list[-2]
        assert str(test_list) == "{[13]}"
        del test_list[-1]
        assert str(test_list) == "{}"

        # Test error raising for __del__
        error_thrown1 = False
        try:
            del test_list['a']
        except TypeError:
            assert str(te) == "Indices can only be of type int"
            error_thrown1 = True
        assert error_thrown1 is True

        error_thrown1 = False
        try:
            del test_list[5000]
        except IndexError as ie:
            assert str(ie) == "Given index is out of list bounds"
            error_thrown1 = True
        assert error_thrown1 is True

        # Test getting when the list is empty
        error_thrown1 = False
        try:
            temp = test_list[0]
        except IndexError:
            error_thrown1 = True
        assert error_thrown1 is True
Beispiel #43
0
    def test_example(self):
        #----------------------appending test------------------------------------
        list_one = UnrolledLinkedList(max_node_capacity=8)
        self.assertIsNotNone(list_one)
        list_one.append(0)
        list_one.append(1)
        list_one.append(2)
        list_one.append(3)
        list_one.append(4)
        list_one.append(5)
        list_one.append(6)
        list_one.append(7)

        self.assertTrue(str(list_one)=="{[0,1,2,3,4,5,6,7]}")

        list_one.append(8)
        self.assertTrue(str(list_one)=="{[0,1,2,3], [4,5,6,7,8]}")

        list_one.append(9)
        list_one.append(10)
        list_one.append(11)
        list_one.append(12)
        self.assertTrue(str(list_one)=="{[0,1,2,3], [4,5,6,7], [8,9,10,11,12]}")

        #----------------------deleting test------------------------------------
        del list_one[-2]
        self.assertTrue(str(list_one)=="{[0,1,2,3], [4,5,6,7], [8,9,10,12]}")

        del list_one[3]
        self.assertTrue(str(list_one)=="{[0,1,2,4,5,6,7], [8,9,10,12]}")

        list_one.append(13)
        list_one.append(14)

        del list_one[0]
        del list_one[0]
        del list_one[0]
        del list_one[0]
        self.assertTrue(str(list_one)=="{[5,6,7,8,9], [10,12,13,14]}")

        #----------------------adding test------------------------------------
        list_two = UnrolledLinkedList(max_node_capacity=8)
        list_two.append(15)
        list_two.append(16)
        list_two.append(17)
        list_two.append(18)

        list_three = list_one + list_two
        self.assertTrue(str(list_three)=="{[5,6,7,8], [9,10,12,13], [14,15,16,17,18]}")       

        #----------------------getitem test------------------------------------
        self.assertTrue(list_three[0]==5)
        self.assertTrue(list_three[4]==9)
        self.assertTrue(list_three[8]==14)
        self.assertTrue(list_three[-1]==18)
        self.assertTrue(list_three[-6]==13)

        #----------------------setitem test------------------------------------
        list_three[0]=100
        self.assertTrue(list_three[0]==100)
        list_three[4]=101
        self.assertTrue(list_three[4]==101)
        list_three[-5]=200
        self.assertTrue(list_three[-5]==200)

        #----------------------iter test------------------------------------
        test = ""
        for i in list_three:
            test+=str(i)+" "
        self.assertTrue(test=="100 6 7 8 101 10 12 13 200 15 16 17 18 ")

        #----------------------reversed iter test------------------------------------
        test = ""
        for i in reversed(list_three):
            test+=str(i)+" "
        self.assertTrue(test=="18 17 16 15 200 13 12 10 101 8 7 6 100 ")

        #----------------------length test------------------------------------
        self.assertTrue(len(list_one)==9)
        self.assertTrue(len(list_two)==4)
        self.assertTrue(len(list_three)==13)

        #----------------------contains test------------------------------------
        self.assertTrue(200 in list_three)
        self.assertTrue(100 in list_three)
        self.assertTrue(15 in list_three)
        self.assertTrue(15 in list_two)
        self.assertFalse(15 in list_one)

        #----------------------mulitply test------------------------------------
        list_four = UnrolledLinkedList(max_node_capacity=8)
        list_four.append(1)
        list_four.append(2)
        list_four.append(3)
        list_four.append(4)
        list_four.append(5)
        list_four*=2
        self.assertTrue(str(list_four)=="{[1,2,3,4], [5,1,2,3,4,5]}")
        list_four*=3
        self.assertTrue(str(list_four)=="{[1,2,3,4], [5,1,2,3], [4,5,1,2], [3,4,5,1], [2,3,4,5], [1,2,3,4], [5,1,2,3,4,5]}")