def test_cat(self): self.file_writing1.write("Irina Ivanova\n") self.file_writing1.close() self.file_writing2.write("[email protected]\n") self.file_writing2.close() file_reading1 = open(self.file_name1, 'r') content1 = file_reading1.read() file_reading1.close() file_reading2 = open(self.file_name2, 'r') content2 = file_reading2.read() file_reading2.close() solution.main() new_file_reading = open(self.new_file_name, 'r') content_new_file = new_file_reading.read() new_file_reading.close() concat_content = content1 + content2 self.assertEqual(concat_content, content_new_file) solution.main() new_file_reading = open(self.new_file_name, 'r') content_new_file = new_file_reading.read() new_file_reading.close() self.assertEqual(concat_content + concat_content, content_new_file)
def test_case_1(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main(9, 2) solution.main(8, 3) self.assertEqual(text_trap.getvalue(), '1\n' + '2\n')
def test_invalid_path(self): import sys sys_argv = ['solution.py', 'foo.csv'] with patch.object(sys, 'argv', sys_argv): with pytest.raises(FileNotFoundError): main()
def test_case_0(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main(2) self.assertEqual( text_trap.getvalue(), '2 x 1 = 2' + '\n' + '2 x 2 = 4' + '\n' + '2 x 3 = 6' + '\n' + '2 x 4 = 8' + '\n' + '2 x 5 = 10' + '\n' + '2 x 6 = 12' + '\n' + '2 x 7 = 14' + '\n' + '2 x 8 = 16' + '\n' + '2 x 9 = 18' + '\n' + '2 x 10 = 20' + '\n')
def test_wc(self): self.file_handle.write("Irina Ivanova\[email protected]") self.file_handle.close() self.assertEqual(2, solution.main()) sys.argv[1] = "words" self.assertEqual(3, solution.main()) sys.argv[1] = "chars" self.assertEqual(29, solution.main())
def test_input_run(self): fp = open('./testcase.txt', 'w') import random a = [ random.randint(0, 100) for x in xrange(0, 10) ] b = [ random.randint(0, 100) for x in xrange(0, 10) ] fp.write(' '.join(map(lambda x: str(x), a))) fp.write('\n') fp.write(' '.join(map(lambda x: str(x), b))) fp.close() fp = open('./testcase.txt', 'r') solution.main(fp)
def test_case_0(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main([ [1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0], [0, 0, 2, 4, 4, 0], [0, 0, 0, 2, 0, 0], [0, 0, 1, 2, 4, 0], ]) self.assertEqual(text_trap.getvalue(), '19\n')
def test_zipped(input_filename, output_filename): """Test computing average grades by student. I've used side_effect instead of return_value on stdin because it accepts an iterable. """ mock_in = _load_input_file(input_filename) expected = _load_output_file(output_filename) with patch('builtins.input', side_effect=mock_in), \ patch('sys.stdout', new=StringIO()) as mock_out: main() actual = mock_out.getvalue().strip() assert actual == expected
def test_case_0(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main([ ('riya', '*****@*****.**'), ('julia', '*****@*****.**'), ('julia', '*****@*****.**'), ('julia', '*****@*****.**'), ('samantha', '*****@*****.**'), ('tanya', '*****@*****.**'), ]) self.assertEqual( text_trap.getvalue(), 'julia\n' + 'julia\n' + 'riya\n' + 'samantha\n' + 'tanya\n')
def test_importing_random_int(self): numbers = [] for i in range(1, 101): numbers.append(i) solution.main(5, 'my_numbers.txt') file = open('my_numbers.txt', 'r') content = file.read() content = content.split('\n') numbers_from_file = list(content) int_numbers = [] for element in content: if element != '': int_numbers.append(int(element)) for element in int_numbers: self.assertTrue(element in numbers) file.close()
def test_cat(self): self.file_writing.write("Irina Ivanova") self.file_writing.close() file_reading = open(self.file_name, "r") content = file_reading.read() file_reading.close() self.assertEqual(content, solution.main())
def test_concat_files(self): sys.argv.append('solution.py') sys.argv.append('chisla.txt') self.file = open('solution.py', 'r') self.content = self.file.read() self.file.close() self.file = open('chisla.txt', 'r') self.content = self.file.read() self.file.close() self.assertEqual(self.content, solution.main())
def test_main(self): self.assertEqual(get_seat_data('BFFFBBFRRR'), (70, 7)) self.assertEqual(get_seat_data('FFFBBBFRRR'), (14, 7)) self.assertEqual(get_seat_data('BBFFBBFRLL'), (102, 4)) self.assertEqual(get_seat_id(70, 7), 567) self.assertEqual(get_seat_id(14, 7), 119) self.assertEqual(get_seat_id(102, 4), 820) self.assertEqual(main('test_input.txt'), 820)
def test_sum_integers(self): sum_integers = 0 filename = "some_integers.txt" file = open(filename, 'r') content = file.read() file.close() for element in content: if element != ' ': sum_integers = sum_integers + int(element) self.assertEqual(sum_integers, solution.main(filename))
class TestIDValidationMethods(unittest.TestCase): valid_id_string = "63-688580-R63" valid_long_id_string = "63-9347183-P26" invalid_id_string = "63-688580-F63" valid_id_char = "R" invalid_id_char = "F" valid_id_int = 63688580 # Execute the main function and retrieve stdout. We are going to run tests against this later to validate solution output. with redirect_stdout(StringIO()) as captured_stdout: solution.main() # Cast the string IO object to a string - stripping symbols. captured_text = captured_stdout.getvalue().strip() def test_task_1(self): self.assertNotRegex(self.captured_text,"Hello, world.", "\n\nDon't forget to delete the line: \nprint(\"Hello, world\")") self.assertNotEqual(self.captured_text,"", "\n\nOh no! nothing was printed. Check your syntax and indentations carefully!") self.assertRegex(self.captured_text,"59-772671-B59","\n\nBe sure that you're reading in the correct csv file: \ntruncated_mangled_voters_roll_hre.csv") def test_task_2(self): self.assertNotRegex(self.captured_text,"MUCHENGETE","\n\nRemember that you can select the n'th element of an array with: \narray[n]") def test_task_3(self): solution_result = solution.get_id_checkdigit(self.valid_id_string) self.assertEqual(len(solution_result),1,"\n\nEach ID has only one check digit. Be sure that you're only returning the third character from the end of the id_string") self.assertNotEqual(solution_result,"-","\n\nYou're off by one, be sure that you're selecting the third character from the end of the id_string") self.assertNotEqual(solution_result,"6","\n\nYou're off by one, be sure that you're selecting the third character from the end of the id_string") self.assertEqual(solution_result, self.valid_id_char, "\n\nBe sure that you're selecting the third character from the end of the ID: \nid_string[-3]") def test_task_4(self): solution_result = solution.get_id_digits(self.valid_id_string) self.assertIsInstance(solution_result, int, "\n\nRemember to cast your returned result into an integer") self.assertNotEqual(solution_result, 688643, "\n\nConcatenate your substrings before you cast to int") self.assertEqual(solution.get_id_digits(self.valid_long_id_string), 639347183, "\n\nAlthough it may be tempting to use string slicing here, some id_strings are 9 digits instead of 8. A better approach would be to split the string around the dashes with: \nid_string.split(\"-\") \nand then concatenate the first 2 items of the resulting array.") self.assertEqual(solution_result, self.valid_id_int, "") def test_task_5(self): solution_result = solution.calculate_id_checkdigit(self.valid_id_string) self.assertIsInstance(solution_result,str, "\n\nRemember the checkdigit is a character, not a number.") self.assertNotEqual(solution_result,"S","\n\nIt looks like you're off by 1, remember that python strings are indexed from 0 so subtract 1 from the result of your modulo 23 operation") self.assertEqual(solution_result, self.valid_id_char,"\n\nReread the instructions carefully.") def test_task_6(self): self.assertIsInstance(solution.check_id_valid(self.valid_id_string),bool,"\n\nThis function must return a boolean (True of False)") self.assertTrue(solution.check_id_valid(self.valid_id_string), "\n\nBe sure that your function returns True for valid id_string ") self.assertFalse(solution.check_id_valid(self.invalid_id_string), "\n\nBe sure that your function returns True for valid id_string ") def test_task_7(self): self.assertNotRegex(self.captured_text, "58-394313-D70", "\n\nBe sure that you are only printing invalid ID numbers.") def test_task_8(self): self.assertRegex(self.captured_text, "25-104900-T48", "\n\nRemember to change the input file from truncated_mangled_voters_roll_hre.csv to mangled_voters_roll_hre.csv")
def test_simple_cases(self): simple_cases = [[ '''3 4 - - - - - * - - - * - - 0 1''', '0 1 No\n' ], ['''3 4 - - - - - * - * - * - - 1 0''', '1 0 Yes\n'], ['''3 4 - - - - - * - * - * - - 1 2''', '1 2 No\n'], ['''1 4 - - - - 0 1''', '0 1 No\n'], [ '''5 4 - - - - * * * - - - - - - - * * - - - - 3 1''', '3 1 Yes\n' ], [ '''5 4 - - - - * * * - - - - - - * * * - - - - 3 0''', '3 0 No\n' ]] from StringIO import StringIO for input, output in simple_cases: outfile = StringIO() solution.main(StringIO(input), outfile) self.assertEqual(outfile.getvalue(), output)
def test_simple_cases(self): simple_cases = [ ['''3 4 - - - - - * - - - * - - 0 1''', '0 1 No\n'], ['''3 4 - - - - - * - * - * - - 1 0''', '1 0 Yes\n'], ['''3 4 - - - - - * - * - * - - 1 2''', '1 2 No\n'], ['''1 4 - - - - 0 1''', '0 1 No\n'], ['''5 4 - - - - * * * - - - - - - - * * - - - - 3 1''', '3 1 Yes\n'], ['''5 4 - - - - * * * - - - - - - * * * - - - - 3 0''', '3 0 No\n'] ] from StringIO import StringIO for input, output in simple_cases: outfile = StringIO() solution.main(StringIO(input), outfile) self.assertEqual(outfile.getvalue(), output)
def test_case_0(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main(5, 2) solution.main(8, 5) solution.main(2, 2) self.assertEqual(text_trap.getvalue(), '1\n' + '4\n' + '0\n')
def test_valid_input(self): import sys import csv sys_argv = ['solution.py', 'foo.csv'] csv_reader = [ ['15:20:04', '15:23:49', '+351217538222', '+351214434422'], ['16:43:02', '16:50:20', '+351217235554', '+351329932233'], ['17:44:04', '17:49:30', '+351914374373', '+351963433432'], ['09:11:30', '09:15:22', '+351914374373', '+351215355312'], ] csv.reader = MagicMock(return_value=csv_reader) with patch.object(sys, 'argv', sys_argv): with patch("builtins.open", mock_open(read_data="data")): assert main() == 0.67
def test_cat(self): self.file_writing1.write("Irina Ivanova") self.file_writing1.close() self.file_writing2.write("*****@*****.**") self.file_writing2.close() file_reading1 = open(self.file_name1, "r") content1 = file_reading1.read() file_reading1.close() file_reading2 = open(self.file_name2, "r") content2 = file_reading2.read() file_reading2.close() result = content1 + '\n' + content2 + '\n' self.assertEqual(result, solution.main())
def test_count_lines(self): sys.argv[1] = 'lines' sys.argv.append('story.txt') self.assertEqual(5, solution.main())
def test_main(self): self.assertEqual(main('test_input.txt'), 4)
def test_count_chars(self): sys.argv.append('chars') sys.argv.append('story.txt') self.assertEqual(1031, solution.main())
if __name__ == "__main__": import fileinput import sys from pathlib import Path try: try: from .solution import main except ImportError: from solution import main # type: ignore except ModuleNotFoundError: day = Path(__file__).parts[-2] print("Not implemented:", day, file=sys.stderr) exit(1) main(fileinput.input())
def _test_testcase(self, testf): with captured_output() as (out, err): fp = open(testf, 'r') solution.main(fp) exp = StringIO(fp.read()) self.assertEqual(out.getvalue(), exp.getvalue())
def test_cat_command(self): name1 = "some_text.txt" name2 = 'and_some_more_text.txt' self.assertEqual("This is just a little bit of text.\n\nAre you ready to rock?", solution.main(name1, name2))
def test_cat(self): self.assertEqual(self.content, solution.main())
def test_no_system_arg(self): with pytest.raises(IndexError): main()
def test_equal_55(self): sys.argv.append('chisla.txt') self.assertEqual(55, solution.main())
def test_input_run(self): fp = open('./input03.txt', 'r') solution.main(fp)
import sys sys.path[0:0] = ['..'] import solution class Africa1B(solution.Case): def read_case(self): return self.fin.readline().strip(), def solve_case(self, line): return ' '.join(reversed(line.split(' '))) def write_case(self, result): print >> self.fout, result if __name__ == '__main__': solution.main(Africa1B)
def _test_printing_result_on_stdout(self): fp = open('./testcase1.txt', 'r') solution.main(fp)
def test_cat2(self): self.assertEqual(self.content1 + self.content2, solution.main())
def read_case(self): c = self.read_int() i = self.read_int() p_list = self.read_int_list() return c, i, p_list def solve_case(self, c, i, p_list): p_len = len(p_list) indexes, p_list = zip( *sorted( zip(range(1, p_len+1), p_list), key=lambda x: x[1])) for i in range(p_len): val1 = p_list[i] for j in range(i+1, p_len): res = val1 + p_list[j] if res > c: break elif res == c: return sorted([indexes[i], indexes[j]]) raise Exception() def write_case(self, result): self.write_int_list(result) if __name__ == '__main__': solution.main(Africa1A)
def test_too_many_args(self): sys.argv.append('chisla.txt') sys.argv.append('chisla.txt') self.assertEqual("Too many arguments", solution.main())
from typing import List from solution import main def debug_hook( index: int, lhs: int, rhs: int, dst: int, op: int, code: List[int] ) -> None: print(f"{op=:02} @ ip={index:02x} :: {dst=:02x} <- ({lhs=:02x}, {rhs=:02x}) ({code[lhs]}), ({code[rhs]})") main(debug_hook=debug_hook)
test_case = input('How many input files? ') save_input = sys.stdin save_output = sys.stdout for i in range(test_case): INPUT = open(input_file_name + str(i) + '.txt', 'r') OUTPUT = open(output_file_name + str(i) + '.txt', 'w') #sys.stdin = Reader( sys.stdin, INPUT ) #sys.stdout = Writer( sys.stdout, OUTPUT ) sys.stdin = Reader(INPUT) sys.stdout = Writer(OUTPUT) t1 = time.time() try: solution.main() # from solution.py except: pass t2 = time.time() - t1 print 'EXCUTION TIME: ' + str(t2) INPUT.close() OUTPUT.close() sys.stdin = save_input sys.stdout = save_output for i in range(test_case): candidate = open(output_file_name + str(i) + '.txt', 'r') solution = open(solution_file_name + str(i) + '.txt', 'r') flag = is_matched(candidate, solution)
def test_sum(self): self.assertEqual(47372, solution.main())
def test_cat2(self): self.assertEqual('\n'.join(self.content), solution.main())
def test_cat_command(self): name = "some_text.txt" self.assertEqual("This is just a little bit of text.", solution.main(name))
def test_input(self): num_safe, allergens, elapsed_time = main(infile="input") assert num_safe == 2428 assert allergens == "bjq,jznhvh,klplr,dtvhzt,sbzd,tlgjzx,ctmbr,kqms"
def test_case_0(self): text_trap = io.StringIO() with redirect_stdout(text_trap): solution.main([1, 4, 3, 2]) self.assertEqual(text_trap.getvalue(), '2 3 4 1\n')
def test_example_input(self): num_safe, allergens, elapsed_time = main(infile="test_input") assert num_safe == 5 assert allergens == "mxmxvkd,sqjhc,fvjkl"
def test_special_swap(self): with captured_output() as (out, err): fp = open('./input4.txt', 'r') solution.main(fp) exp = StringIO(fp.read()) self.assertEqual(out.getvalue(), exp.getvalue())
def test_main(self): self.assertEqual(main('test_input_1.txt'), 35) self.assertEqual(main('test_input_2.txt'), 220)
def test_main(self): self.assertEqual(main('test_input.txt', number=127), 62)
import sys sys.path[0:0] = ['..'] import solution class Africa1B(solution.Case): def read_case(self): return self.fin.readline().strip(), def solve_case(self, line): return ' '.join(reversed(line.split(' '))) def write_case(self, result): print >>self.fout, result if __name__ == '__main__': solution.main(Africa1B)
def test_count_words(self): sys.argv[1] = 'words' sys.argv.append('story.txt') self.assertEqual(164, solution.main())
# coding=utf-8 import sys from solution import main from vehicle import cities if __name__ == "__main__": if len(sys.argv) != 2: print "Invalid Input" sys.exit() if sys.argv[1] in cities: main(sys.argv[1])
def test_input_sorted(self): with captured_output() as (out, err): fp = open('./testcase1.txt', 'r') solution.main(fp) exp = StringIO(fp.read()) self.assertEqual(out.getvalue(), exp.getvalue())
def test_no_files(self): sys.argv = [sys.argv[0]] self.assertEqual("No given arguments!", solution.main())
def test_not_equal_0(self): self.assertNotEqual(0, solution.main())