Beispiel #1
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 #2
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 #3
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 #4
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 #5
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))
Beispiel #6
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 #7
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)
Beispiel #8
0
 def test_dunder_functions_exist(self):
     ull = UnrolledLinkedList()
     self.assertTrue(hasattr(ull, '__delitem__'))
     self.assertTrue(hasattr(ull, '__getitem__'))
     self.assertTrue(hasattr(ull, '__setitem__'))
     self.assertTrue(hasattr(ull, '__iter__'))
     self.assertTrue(hasattr(ull, '__str__'))
     self.assertTrue(hasattr(ull, '__len__'))
     self.assertTrue(hasattr(ull, '__contains__'))
     self.assertTrue(hasattr(ull, '__add__'))
     self.assertTrue(hasattr(ull, '__mul__'))
     print "Test done"
Beispiel #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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
Beispiel #17
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]}")
Beispiel #18
0
 def test_other_functions_exist(self):
     ull = UnrolledLinkedList()
     self.assertTrue(hasattr(ull, 'append'))
Beispiel #19
0
 def test_properties_exist(self):
     ull = UnrolledLinkedList()
     self.assertTrue(hasattr(ull, 'max_node_capacity'))
Beispiel #20
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 #21
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