Пример #1
0
 def test_append(self):
     obj = ArrayOperations()
     obj.append(20)
     obj.append(40)
     self.assertEqual(20, obj[1])                                      # Case_1: Checks whether the item appended is at the index 1 instead of index 0,
     self.assertLess(0, len(obj))                                      # Case_2: Checks whether the initial count = 0 is less than the new count after appending items into the list
     self.assertEqual(2, len(obj))                                     # Case_3: Checks whether after appending 2 items, count = 2
Пример #2
0
 def test_insert(self):
     obj = ArrayOperations()
     obj.append(1)
     obj.append(2)
     obj.append(3)
     obj.insert(3,4)                                            # Case_1: Inserts item 4 at index 3 and shifts 3 to the right proving index starts from 1, checks whether this occurs, if so test passes
     self.assertEqual([1,2,4,3], obj)
     with self.assertRaises(IndexError):                        # Case_2: Checks whether it raises indexerror for an index > len(obj)
         obj.insert(6, 20)
Пример #3
0
 def test_getitem(self):
     obj = ArrayOperations()
     obj.append(20)
     obj.append(2)
     with self.assertRaises(IndexError):
         obj.__getitem__(3)                                           # Case_1: Index 3 is out of range since count is 2, only index 1 and 2 exist, hence indexerror will be raised
     self.assertEqual(20, obj[1])                                     # Case_2: Checking whether index 1 is 20 to check whether the index is starting from 1 instead of 0
     with self.assertRaises(IndexError):
         obj.__getitem__(-1)                                          # Case_3: Accounting for negative indexes which are also out of range
Пример #4
0
def file_list(filename):
    '''
    This function takes a filename as an input by the user and reads the contents of the file into a list, storing each line as a single item in the list
    :param: None
    :precondition: A file must exist from which the contents of the file can be read
    :postcondition: A list is created with each line in the file stored as a single item in the list
    :complexity: Best Case = Worst Case = O(n), where n is the length of the contents in the file, even if the contents are empty or filled the loop will run n times
    '''

    infile = open(filename, 'r')
    contents = infile.readlines()
    L = ArrayOperations()
    for i in range(len(contents)):
        L.append(contents[i].rstrip('\n'))
    infile.close()
    return L
Пример #5
0
 def test_setitem(self):
     obj = ArrayOperations()
     with self.assertRaises(IndexError):
         obj.__setitem__(5, 20)                                       # Case_1: Index 5 is out of range hence indexerror will be raised
     obj.append(1)
     obj.append(2)
     self.assertLess(1, len(obj))                                     # Case_2: Index 1 is in range, as two items apppended to give self.count = 2 (index < count i.e. index in range)
     with self.assertRaises(IndexError):
         obj.__setitem__(-4, 20)                                      # Case_3: Accounting for negative indexes which are also out of range
Пример #6
0
 def test_eq(self):
     obj = ArrayOperations()
     other = [1,2,3,4]
     obj.append(2)
     obj.append(3)
     self.assertFalse(obj.__eq__(other))                              # Case_1: If the length of the original list is not equal to the length of the other list, then assertFalse
     other1 = [2,3,5,6]
     obj.append(5)
     obj.append(6)
     self.assertTrue(obj.__eq__(other1))                              # Case_2: Checks whether every index of both the lists is the same, if so, assertTrue
Пример #7
0
 def test_resize_list(self):
     obj = ArrayOperations()
     obj.append(150)
     obj.append(175)
     self.assertEqual(100, len(obj.array))                           # Case_1: checks for the if condition failing, when the list is not full, the size remains the same i.e. 100
     for i in range(len(obj.array)):                                 # Case_2: appends items into the list equal to the len of the array, to check that when the array is full, if it resizes the array by 10 times to a len of 1000
         obj.append(i)
     self.assertEqual(1000, len(obj.array))
     self.assertIn(150, obj.array)                                   # Case_3: To check whether the original elements in the list are copied back into it after re-sizing
     self.assertIn(175, obj.array)
Пример #8
0
 def __init__(self):
     self.array = ArrayOperations()
Пример #9
0
class TextEditor:
    def __init__(self):
        self.array = ArrayOperations()

    def insert_num(self, num, line):
        '''
        This function inserts a line of text in the list at the position inputted by the user, and raises an exception if no position is entered by the user
        :param: number position inputted by the user and the line of text inputted by the user
        :precondition: A list must already exist
        :postcondition: The list is updated as a line is inserted into it or an error is raised depending on the user's input
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        try:
            num = int(
                num
            )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
            self.array.insert(num, line)
        except ValueError:
            return False
        except IndexError:
            return False
        return True

    def read_filename(self, filename):
        '''
        This function takes a filename as an input by the user and reads the contents of the file into a list, storing each line as a single item in the list
        :param: filename of the file to be read
        :precondition: A file must exist from which the contents of the file can be read
        :postcondition: A list is created with each line in the file stored as a single item in the list
        :complexity: Best Case = Worst Case = O(n), where n is the length of the contents in the file, even if the contents are empty or filled the loop will run n times
        '''

        try:
            self.array = file_list(filename)
        except FileNotFoundError:
            return False
        return True

    def write_filename(self, filename):
        '''
        This function creates a new file or opens an existing file and writes every item in the list into the file and then closes the file.
        :param: filename of the file to be written to
        :precondition: There must be an existing list from which it copies the items into the file
        :postcondition: An existing file is overwritten with the contents of the list copied into it or a new file is created with the contents of the list copied into it
        :complexity: Best Case = Worst Case = O(n), where n is the length of the list, the loop will run n times
        '''

        infile = open(filename, 'w')
        for i in range(1, len(self.array) + 1):
            infile.writelines(self.array[i] + str('\n'))
        infile.close()

    def print_num(self, num):
        '''
        This function prints the line from the file at the position inputted by the user, however, if no position is inputted by the user it prints all the lines of the file
        :param: number position inputted by the user
        :precondition: The file must exist already to be accessed
        :postcondition: The particular line or all the lines of the file are printed for the user
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        if num == '':  # if no input for position is given, all the lines are printed
            print(self.array)
        else:
            try:
                num = int(
                    num
                )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
                print(self.array[num])
            except ValueError:
                return False
            except IndexError:
                return False
        return True

    def delete_num(self, num):
        '''
        This function deletes the line from the file at the position inputted by the user, however, if no position is inputted by the user it deletes all the lines of the file
        :param: number position inputted by the user
        :precondition: The file must have some existing lines to be deleted i.e. the file cannot be empty.
        :postcondition: The file is updated after the deletion has taken place depending on the user input
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        try:
            if num == '':
                for i in range(len(self.array), 0, -1):
                    self.array.delete(
                        i
                    )  # if no input for position is given, all the lines are deleted from the list
            else:
                num = int(
                    num
                )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
                self.array.delete(num)
        except IndexError:
            return False
        except ValueError:
            return False
        return True
Пример #10
0
 def test_remove(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)
     obj.remove(40)
     print(obj)
     self.assertEqual(2, len(obj))                                   # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(50, obj.__getitem__(2))                        # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 2 from index 3
     with self.assertRaises(ValueError):                             # Case_3: Since 6 does not exist as an item in the list, the ValueError is raised.
         obj.remove(6)
Пример #11
0
 def test_delete(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)                                                     # obj count initially 3
     obj.delete(3)                                                     # after delete obj count 2
     self.assertEqual(2, len(obj))                                     # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(30, obj.__getitem__(1))                           # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 2 from index 3
     with self.assertRaises(IndexError):
         obj.delete(4)                                                 # Case_3: Checks whether the index is less than the count to check if index is valid
Пример #12
0
 def test_str(self):
     obj = ArrayOperations()
     self.assertEqual(str(obj), "")                                   # Case_1: checks whether the empty list gives ""
     obj.append(1)
     obj.append(2)
     self.assertEqual(str(obj), "1\n2\n")                             # Case_2: For the list [1,2], we will get "1\n2\n"
Пример #13
0
 def test_contains(self):
     obj = ArrayOperations()
     obj.append(1)
     obj.append(2)
     self.assertTrue(obj.__contains__(1))                             # Case_1: list contains 1, hence will give us true
     self.assertFalse(obj.__contains__(4))                            # Case_2: list does not contain 4 hence will give false
Пример #14
0
 def test_len(self):
     obj = ArrayOperations()
     self.assertEqual(0, len(obj))                                    # Case_1: Checks whether the self.count = 0
     obj.append(1)
     obj.append(2)
     self.assertEqual(2, len(obj))                                    # Case_2: Appends two items into list to check whether self.count = 2