def test_insert1(self): x = task2.ListADT(10) ## This should not fail. append(x, [1 for i in range(1000)]) ## If the ListADT has renamed the underlying array, or used some other ## representation, we can't realy test whether resizing is handled correctly. if not hasattr(x, "the_array"): raise AttributeError( "could not identify underlying array for the ListADT.") y = task2.ListADT(10) # If we gave a smaller constant, should have become 40. self.assertEqual(len(y.the_array), 40, "Allocated array below threshold.") # ... and with rounding y = task2.ListADT(44) for i in range(45): y.insert(i, i) self.assertEqual(len(y.the_array), 84, "Incorrect grow.") # math.ceil(44 * 1.9) # Check shrinking for i in range(25): y.delete(len(y) - 1) self.assertTrue(len(y.the_array) == 42, "Incorrect shrink.")
def undo(self): """ This function will undo the operation that we have done to the list """ # Set that we are in undo mode self.mode = 'undo' # This will pop up the last command that we have append or run (command, val) = self.command.pop() # It will check whether the command is insert or not if command == 'Insert': # if the command is insert then we will pop out the previous lines that # we have inserted and the with the index. (line_num, count) = self.previoustask.pop() for i in range(count): # we will delete all the lines that we have inserted to the list # using del command del self.text_lines.the_array[int(line_num)-1] # every time we will reduce the index by one value because we are deleting # one line at a time. self.text_lines.index -= 1 elif command == 'Delete': # if the command is delete then we will fetch the previous previosly deleted # lines with the line number (line, line_num) = self.previoustask.pop() # print(line_num, type(line_num)) templine = task2.ListADT(1) templine.insert(0,line) # print(templine) # then we will just insert those lines to the list with given line_num self.insert_num_strings(str(line_num+1), templine) elif command== 'DeleteAll': # print("Text index", self.text_lines.index) # print(self.text_lines.index) # if the command is to delete all the lines then we need to iterate over # each line that we have deleted. # val denote the number of deleted lines. # initialize the new list templine = task2.ListADT(1) for i in range(val): # print(i) # pop the previos line line = self.previoustask.pop() # print(line) # append that line # templine.the_array[0] = line templine.insert(0,line) # increment the index by one # templine.index += 1 # This will insert the line to our main list # self.insert_num_strings(str(val - i), templine) # print(templine) self.insert_num_strings('1', templine) # Now we are out of undo so we have set notundo mode here self.mode = 'notundo'
def test_shrink(self): shrinklist = task2.ListADT(51) shrinklist.insert(0, 0) # Should not shrink unless you take an element away from the list self.assertEqual(len(shrinklist.the_array), 51, "Incorrect shrink size") for i in range(1, 51): shrinklist.insert(i, i) # Should not shrink if length of list is not less than 1/4 of the size of its capacity shrinklist.delete(3) self.assertEqual(len(shrinklist.the_array), 97, "Incorrect shrink size") for i in range(49, 20, -1): shrinklist.delete(i) # Should shrink if the length of the list is less than 1/4 of its capacity (by half the size) self.assertEqual(len(shrinklist.the_array), 48, "Incorrect shrink size") for i in range(20, 10, -1): shrinklist.delete(i) # Should never shrink below a max size of 40 self.assertEqual(len(shrinklist.the_array), 40, "Incorrect shrink size")
def test_insert2(self): y = task2.ListADT(50) # If we gave a smaller constant, should have become 40. self.assertEqual(len(y.the_array), 50, "Allocated array below threshold.") # ... and with rounding y = task2.ListADT(80) for i in range(81): y.insert(i, i) self.assertEqual(len(y.the_array), 152, "Incorrect grow.") # math.ceil(44 * 1.9) # Check shrinking for i in range(45): y.delete(len(y) - 1) self.assertTrue(len(y.the_array) == 75, "Incorrect shrink.")
def __init__(self): """ This will initialize whe textlines with ListADT class (that we have implemented in the task2) """ self.text_lines = task2.ListADT(40) # this previous task is to monitor the previous line which is either deleted or # inserted self.previoustask = list() # This is to moniter which command is given insert or delete self.command = list() # This is to see whether we are in undo or out of undo function self.mode = 'notundo'
def read_filename(self, file_name): """ This function will open the file and then it will insert the data (lines from that file) into the list. """ self.text_lines = task2.ListADT(40) # This is to open the file f = open(file_name,"r") # This will read the files f1 = f.readlines() # This to close the files f.close() # iterate over each line of the file for i, lines in enumerate(f1): # Insert the line into the array self.text_lines.insert(i, lines)
def test_extend(self): extendlist = task2.ListADT(40) # To make sure it extends appropriately, make sure it starts at the correct size to extend from self.assertEqual(len(extendlist.the_array), 40, "Incorrect starting size") for i in range(40): extendlist.insert(i, i) # Once it reaches max capacity, should extend by 1.9* its original max size self.assertEqual(len(extendlist.the_array), 77, "Incorrect extending size") # Length of the list should be 40 once it extends (as this is its original max capacity) self.assertEqual(len(extendlist), 40, "Incorrect list size") extendlist.insert(3, 1) # Max size should remain the same even after an element is inserted, only extends when full self.assertEqual(len(extendlist.the_array), 77, "Incorrect extending size") # Making sure the list actually extended while the max size remained the same self.assertNotEqual(len(extendlist), 40, "Incorrect list size")
def search_string(self, query): """For future""" raise NotImplementedError def undo(self): """For future""" raise NotImplementedError if __name__ == "__main__": ed = Editor() while(1): text = input().split(' ') if text[0] == 'insert': # initialize the new list templine = task2.ListADT(1) count = 0 line_num = text[1] while(1): text2 = input().split('\n') if text2[0] == '.': break # append that line templine.the_array[count] = text2[0]+ '\n' # increment the index by one templine.index += 1 count += 1 ed.insert_num_strings(str(int(text[1])+1), templine) elif text[0] == 'delete': ed.delete_num(str(int(text[1])+1)) elif text[0] == 'print' :
def test_str1(self): # Check str for non-empty lists x = task2.ListADT(10) append(x, [1, 2, 3]) expected_output = '1\n2\n3' + '\n0' * (37) self.assertEqual(str(x).strip('\n'), expected_output)
def test_eq2(self): x = task2.ListADT(10) y = [2.5, 6, 7] self.assertEqual((x == y), False)
def test_eq1(self): x = task2.ListADT(10) y = task2.ListADT(10) self.assertEqual((x == y), True)
def test_setitem2(self): x = task2.ListADT(10) append(x, ['eq1', 'eq2', 'eq3']) x[2] = 'val' self.assertEqual(x[2], 'val')
def test_getitem2(self): x = task2.ListADT(10) append(x, ['eq1', 'eq2', 'eq3']) append(x, [1, 2, 3]) self.assertEqual(x[2], 'eq3')
def test_len2(self): x = task2.ListADT(10) append(x, ['eq1', 'eq2', 'eq3']) append(x, [1, 2, 3]) self.assertEqual(len(x), 6)
def test_len1(self): x = task2.ListADT(10) append(x, [1, 2, 3]) self.assertEqual(len(x), 3)
def test_str2(self): # Check str for non-empty lists x = task2.ListADT(10) append(x, ['eq1', 'eq2', 'eq3']) expected_output = 'eq1\neq2\neq3' + '\n0' * (37) self.assertEqual(str(x).strip('\n'), expected_output)
def __init__(self): """ This will initialize the textlines with ListADT class (that we have implemented in the task2) """ self.text_lines = task2.ListADT(40)
def test_setitem1(self): x = task2.ListADT(10) append(x, [1, 2, 3]) x[2] = 5 self.assertEqual(x[2], 5)