def do_test(self, rectangles_filename, encoded_filename, expected_png, expected): '''Test implementation: - rectangles_filename: name of the PNG file containing the rectangles - encoded_filename: name of the PNG file to create - N: number of rectangles to look for - expected_png: name of the PNG file containing the reference result - expected: the expected bounding-box TIMEOUT: 2 seconds for each test ''' if DEBUG: import program01 as program result = program.ex1(rectangles_filename, encoded_filename) else: with self.ignored_function('builtins.print'), \ self.ignored_function('pprint.pprint'), \ self.forbidden_function('builtins.input'), \ self.forbidden_function('builtins.eval'), \ self.check_open(allowed_filenames_modes={rectangles_filename: ['rb'], encoded_filename: ['wb']}), \ self.check_imports(allowed=['program01', '_io', 'images']), \ self.timeout(TIMEOUT), \ self.timer(TIMEOUT): import program01 as program result = program.ex1(rectangles_filename, encoded_filename) self.assertEqual(type(result), tuple, f"il risultato prodotto deve essere una tupla / the expected result should be a tuple ({result})") self.assertEqual(type(result[0]), int, f"il risultato prodotto deve essere una tupla di interi / the expected result should be a tuple of int ({result})") self.assertEqual(result, expected, "il valore restituito non e' corretto / the expected result is incorrect") self.check_img_file(encoded_filename, expected_png) return 1
def do_test(self, filename, start_city, clues, expected, doRecursionTest=True): """Test implementation - filename : the file containing the instructions for the spy - start_city : the city the spy starts from (an uppercase string) - clues : the sequence of clues (as a space-separated string) - expected : the expected result (a set of pairs (secret,final city)) - doRecursionTest: if True the recursion test is applied TIMEOUT: 1 seconds for each test """ expected = set([tuple(e) for e in expected]) N = len(expected) if DEBUG: import program01 as program result = program.ex1(filename, start_city, clues) else: # first check for recursion import sys if doRecursionTest: with self.assertIsRecursive('program01') as program: program.ex1(filename, start_city, clues) del program # then test the code with self.ignored_function('builtins.print'), \ self.ignored_function('pprint.pprint'), \ self.forbidden_function('builtins.input'), \ self.forbidden_function('builtins.eval'), \ self.check_open(allowed_filenames_modes={filename: ['r']}), \ self.check_imports(allowed=['program01', '_io', 'encodings.utf_8']), \ self.imported('program01') as program, \ self.timeout(TIMEOUT), \ self.timer(TIMEOUT): result = program.ex1(filename, start_city, clues) self.assertEqual( type(result), set, "The result should be a set / Il risultato prodotto deve essere un insieme" ) for e in result: self.assertEqual( type(e), tuple, "All the set elements should be tuples / Gli elementi dell'insieme devono essere tuple" ) self.assertEqual( len(e), 2, "All the set elements should be tuples of 2 elements / Gli elementi dell'insieme devono essere tuple di 2 elementi" ) self.assertEqual( result, expected, "The returned result is incorrect / Il risultato non è corretto") return 1
def do_test(self, poetry_file, e_prosody, e_module, e_lengths, e_finals): """Test implementation - poetry_file: the file containing the poem - e_prosody : the expected prosody - e_module : the expected module - e_lengths : the expected lengths - e_finals : the expected finals TIMEOUT: 0.1 seconds for each test """ if DEBUG: import program01 as program result = program.ex1(poetry_file) else: with self.ignored_function('builtins.print'), \ self.ignored_function('pprint.pprint'), \ self.forbidden_function('builtins.input'), \ self.forbidden_function('builtins.eval'), \ self.check_open(allowed_filenames_modes={poetry_file: ['r']}), \ self.check_imports(allowed=['program01', '_io', 'encodings.utf_8']), \ self.timeout(0.1), \ self.timer(0.1): import program01 as program result = program.ex1(poetry_file) self.assertEqual(type(result), tuple, "The result should be a tuple / Il risultato prodotto deve essere una tupla") self.assertEqual(len(result), 4, "The result should be a tuple of 4 elements / Il risultato deve essere una tupla di 4 elementi") prosody, module, lengths, finals = result self.assertEqual(type(prosody), list, "The first element should be a list of integers / Il primo elemento deve essere una lista di interi") self.assertEqual(type(prosody[0]), int, "The firts element should be a list of integers / Il terzo elemento deve essere una lista di interi") self.assertEqual(type(module), int, "The second element should be an integer / Il secondo elemento deve essere un intero") self.assertEqual(type(lengths), list, "The third element should be a list of integers / Il terzo elemento deve essere una lista di interi") self.assertEqual(type(lengths[0]), int, "The third element should be a list of integers / Il terzo elemento deve essere una lista di interi") self.assertEqual(type(finals), list, "The last element should be a list of strings / L'ultimo elemento deve essere una lista di stringhe") self.assertEqual(type(finals[0]), str, "The last element should be a list of strings / L'ultimo elemento deve essere una lista di stringhe") self.assertEqual(prosody, e_prosody, "The returned prosody is incorrect / La prosodia restituita non e' corretta") self.assertEqual(module, e_module, "The returned module is incorrect / Il modulo restituito non e' corretto") self.assertEqual(lengths, e_lengths, "The returned verse lengths are incorrect / Le lunghezze dei versi restituite non sono corrette") self.assertEqual(finals, e_finals, "The returned verse finals are incorrect / Le finali dei versi restituite non sono corrette") return 1
def do_test(self, sequence_file, a, b, n, expected): """Test implementation - sequence_file: the file containing the sequence - a, b: the interval extremes - n: the maximum number of sequences - expected: expected result TIMEOUT: 1 second for each test """ if DEBUG: import program01 as program result = program.ex1(sequence_file, a, b, n) else: with self.ignored_function('builtins.print'), \ self.ignored_function('pprint.pprint'), \ self.forbidden_function('builtins.input'), \ self.forbidden_function('builtins.eval'), \ self.check_open(allowed_filenames_modes={sequence_file: ['r']}), \ self.check_imports(allowed=['program01', '_io', 'encodings.utf_8']), \ self.timeout(1), \ self.timer(1): import program01 as program result = program.ex1(sequence_file, a, b, n) self.assertEqual( type(result), list, "The result should be a list / Il risultato prodotto deve essere una lista" ) self.assertEqual( type(result[0]), tuple, "The returned list should contain tuples / La lista restituita deve contenere tuple" ) self.assertEqual( type(result[0][0]), int, "The first element in the list’s tuples should be an integer / " + "La prima coordinata delle tuple restituite deve essere un intero") self.assertEqual( type(result[0][1]), list, "The second element in the list’s tuples should be a list / " + "La seconda coordinata delle tuple restituite deve essere una lista" ) self.assertEqual( type(result[0][1][0]), str, "The second element in the list’s tuples should be a list of strings / " + "la seconda coordinata delle tuple deve contenere una lista di stringhe" ) self.assertEqual( result, expected, "The returned list is incorrect / " + "La lista restituita non e' corretta") return 1
def do_test(self, g1, g2, g3, g4, hand_size, num_letters, expected): """Test implementation - g1, g2, g3, g4: words played by each of the 4 players - hand_size: number of tiles in a player’s hand - num_letters: number of initial letters - expected: expected result TIMEOUT: 0.5 seconds for each test """ g1 = g1[:] g2 = g2[:] g3 = g3[:] g4 = g4[:] if DEBUG: import program01 as program result = program.ex1(g1, g2, g3, g4, hand_size, num_letters) else: with self.ignored_function('builtins.print'), \ self.ignored_function('pprint.pprint'), \ self.forbidden_function('builtins.input'), \ self.forbidden_function('builtins.eval'), \ self.forbidden_function('builtins.open'), \ self.check_imports(allowed=['program01','_io']), \ self.timeout(0.5), \ self.timer(0.5): import program01 as program result = program.ex1(g1, g2, g3, g4, hand_size, num_letters) self.assertEqual(type(result), list, ('The output type should be: list\n' '[Il tipo di dato in output deve essere: list]')) self.assertEqual(type(result[0]), int, ('The output type should be: a list of int\n' '[Il tipo di dato in output deve essere: una lista di int]')) self.assertEqual(result, expected, ('The return value is incorrect\n' '[Il valore di ritorno è errato]')) return 1