def test_concurrent(self): programs = ['test{}.py'.format(i) for i in range(16)] for fn in programs: with open(fn, 'w') as f: f.write('print({})'.format(16)) with open('std.py', 'w') as f: f.write('print({})'.format(16)) with IO() as test: Compare.program(*[(sys.executable, prog) for prog in programs], std_program=(sys.executable, 'std.py'), max_workers=None, input=test) ios = [IO() for i in range(16)] try: for f in ios: f.output_write('16') with IO() as std: std.output_write('16') Compare.output(*ios, std=std, max_workers=None) finally: for io in ios: io.close()
def test_fulltext_program(self): with open("correct.py", "w") as f: f.write("print(1)") with open("incorrect.py", "w") as f: f.write("print(2)") io = None with captured_output() as (out, err): io = IO("test_fulltext.in", "test_fulltext.out") io.output_writeln("1") try: with captured_output() as (out, err): Compare.program("python correct.py", "python incorrect.py", std=io, input=io, grader="FullText") except CompareMismatch as e: self.assertEqual(e.name, 'python incorrect.py') e = e.mismatch self.assertEqual(e.content, '2\n') self.assertEqual(e.std, '1\n') self.assertEqual(e.content_hash, '53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3') self.assertEqual(e.std_hash, '4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865') else: self.assertTrue(False) result = out.getvalue().strip() correct_out = 'python correct.py: Correct \npython incorrect.py: !!!INCORRECT!!! Hash mismatch: read 53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3, expected 4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865' self.assertEqual(result, correct_out)
def test_noipstyle_incorrect(self): io = None with captured_output() as (out, err): io = IO("test_compare_incorrect.in", "test_compare_incorrect.out") io.output_writeln("test123 \ntest123\n") with open("test_another_incorrect.out", "w") as f: f.write("test123\r\ntest124 ") try: with captured_output() as (out, err): Compare.output("test_another_incorrect.out", std=io) except CompareMismatch as e: self.assertEqual(e.name, 'test_another_incorrect.out') e = e.mismatch self.assertEqual(e.content, 'test123\r\ntest124 ') self.assertEqual(e.std, 'test123 \ntest123\n\n') self.assertEqual(str(e), 'On line 2 column 7, read 4, expected 3.') else: self.assertTrue(False) result = out.getvalue().strip() self.assertEqual( result, "test_another_incorrect.out: !!!INCORRECT!!! " "On line 2 column 7, read 4, expected 3.")
def test_init_overload(self): with IO(file_prefix='data{', data_id=5) as test: self.assertEqual(test.input_filename, 'data{5.in') self.assertEqual(test.output_filename, 'data{5.out') with IO('data{}.in', 'data{}.out', 5) as test: self.assertEqual(test.input_filename, 'data5.in') self.assertEqual(test.output_filename, 'data5.out') with open('data5.in', 'w+') as fin: with open('data5.out', 'w+') as fout: with IO(fin, fout) as test: self.assertEqual(test.input_file, fin) self.assertEqual(test.output_file, fout)
def test_noipstyle_correct(self): io = None with captured_output() as (out, err): io = IO("test_compare.in", "test_compare.out") io.output_writeln("test123 \ntest123\n") with open("test_another.out", "w") as f: f.write("test123\r\ntest123 ") with captured_output() as (out, err): Compare.output("test_another.out", std=io) result = out.getvalue().strip() self.assertEqual(result, "test_another.out: Correct")
def test_create_files_prefix_id(self): IO(file_prefix="test_prefix", data_id=233, input_suffix=".inp", output_suffix=".ans") self.assertTrue(os.path.exists("test_prefix233.inp")) self.assertTrue(os.path.exists("test_prefix233.ans"))
def test_output_gen(self): with IO("test_gen.in", "test_gen.out") as test: test.output_gen("echo 233") with open("test_gen.out") as f: output = f.read() self.assertEqual(output.strip("\n"), "233")
def test_noipstyle_incorrect(self): io = None with captured_output() as (out, err): io = IO("test_compare_incorrect.in", "test_compare_incorrect.out") io.output_writeln("test123 \ntest123\n") with open("test_another_incorrect.out", "w") as f: f.write("test123\r\ntest124 ") with captured_output() as (out, err): Compare.output("test_another_incorrect.out", std=io) result = out.getvalue().strip() self.assertEqual( result, "test_another_incorrect.out: !!!INCORRECT!!! On line 2 column 7, read 4, expected 3." )
def test_timeout(self): if sys.version_info >= (3, 3): with IO() as test: try: Compare.program(((sys.executable, '-c', '__import__(\'time\').sleep(10)'), 1), std=test, input=test) except subprocess.TimeoutExpired: pass else: self.assertTrue(False)
def test_file_input(self): with open("correct.py", "w") as f: f.write("print(input())") with open("std.py", "w") as f: f.write("print(input())") io = None with captured_output() as (out, err): io = IO() io.input_writeln("233") with captured_output() as (out, err): Compare.program("python correct.py", std_program="python std.py", input=io, grader="NOIPStyle") result = out.getvalue().strip() correct_out = 'python correct.py: Correct' self.assertEqual(result, correct_out)
def test_noipstyle_incorrect(self): io = None with captured_output() as (out, err): io = IO("test_compare_incorrect.in", "test_compare_incorrect.out") io.output_writeln("test123 \ntest123\n") with open("test_another_incorrect.out", "wb") as f: f.write(b"test123\r\ntest124 ") try: with captured_output() as (out, err): Compare.output("test_another_incorrect.out", std=io) except CompareMismatch as e: self.assertEqual(e.name, 'test_another_incorrect.out') e = e.mismatch self.assertEqual(e.content, 'test123\r\ntest124 ') # AssertionError self.assertEqual(e.std, 'test123 \ntest123\n\n') # AssertionError self.assertEqual( str(e), 'On line 2 column 7, read 4, expected 3.') # AssertionError # try: # self.assertEqual(e.content, 'test123\r\ntest124 ') # self.assertEqual(e.std, 'test123 \ntest123\n\n') # self.assertEqual(str(e), 'On line 2 column 7, read 4, expected 3.') # except AssertionError: # pass # # TODO... # # When this file run in python3.7, the function will throw AssertionError else: self.assertTrue(False) result = out.getvalue().strip() self.assertEqual( result, "test_another_incorrect.out: !!!INCORRECT!!! On line 2 column 7, read 4, expected 3." )
def p1269(): if not os.path.exists('1269'): os.mkdir('1269') os.system('g++ code/1269.cpp') # case 1~10 #边界输入 for case_id in range(1, 11): io = IO(file_prefix="1269/", data_id=case_id) io.input_write(open('raw_data/1269/data{}.in'.format(case_id)).read()) io.output_gen('./a.out') open('1269/scores', 'w').write(open('raw_data/scores/1269').read())
def test_write_stuff(self): with IO("test_write.in", "test_write.out") as test: test.input_write(1, 2, 3) test.input_writeln([4, 5, 6]) test.input_writeln(7, [8, 9]) test.output_write([9, 8], 7) test.output_writeln(6, 5, 4) test.output_writeln([3], 2, [1]) with open("test_write.in") as f: input = f.read() with open("test_write.out") as f: output = f.read() self.assertEqual(input.split(), ['1', '2', '3', '4', '5', '6', '7', '8', '9']) self.assertEqual(output.split(), ['9', '8', '7', '6', '5', '4', '3', '2', '1']) self.assertEqual(input.count("\n"), 2) self.assertEqual(output.count("\n"), 2)
import cyaron from cyaron import IO if __name__ == '__main__': io = IO(file_prefix="", data_id=1) io.input_writeln(1, 2, 3)
from cyaron import String from cyaron import IO import random cstr=['A','B'] for cases in range(4,7): io=IO('omega'+str(cases)+'.in','omega'+str(cases)+'.out') n=random.randint(10000,500000) k=random.randint(1,10) io.input_writeln(str(n)+' '+str(k)) for i in range(0,n): io.input_writeln(cstr[random.randint(0,1)]) pass pass
from cyaron import IO from cyaron import Compare from cyaron import Sequence from random import randint import random random.seed() io = IO(file_prefix="test") n = randint(1,10000000) seq = Sequence(lambda i,f:randint(0,10000000)).get(0,n-1) seq.sort() io.input_writeln(n) io.input_writeln(seq) io.output_write(seq)
def test_create_files_simple(self): with captured_output() as (out, err): IO("test_simple.in", "test_simple.out") self.assertTrue(os.path.exists("test_simple.in")) self.assertTrue(os.path.exists("test_simple.out"))
from cyaron import IO import random for cases in range(1, 7): io = IO('beta' + str(cases) + '.in', 'beta' + str(cases) + '.out') io.input_writeln( str(random.randint(1, pow(10, 10))) + ' ' + str(random.randint(1, 4))) pass
def test_create_files_simple(self): IO("test_simple.in", "test_simple.out") self.assertTrue(os.path.exists("test_simple.in")) self.assertTrue(os.path.exists("test_simple.out"))
from cyaron import Graph from cyaron import IO import random for cases in range(14, 21): io = IO('sigma' + str(cases) + '.in', 'sigma' + str(cases) + '.out') n = random.randint(500, 1000) m = random.randint(n - 1, 10000) io.input_writeln(str(n) + ' ' + str(m) + ' ' + str(random.randint(0, 5))) io.input_writeln('1 ' + str(random.randint(2, n - 1)) + ' ' + str(random.randint(1, 1000000))) for t in range(0, m - 2): io.input_writeln( str(random.randint(2, n - 1)) + ' ' + str(random.randint(2, n - 1)) + ' ' + str(random.randint(1, 1000000))) pass io.input_writeln( str(n) + ' ' + str(random.randint(2, n - 1)) + ' ' + str(random.randint(1, 1000000))) #tu=Graph.tree(n,0.4,0.25,weight_limit=(1,1000000)) #io.input_writeln(tu.to_str(shuffle=True)) pass
def p2176(): if not os.path.exists('2176'): os.mkdir('2176') os.system('g++ code/2176.cpp') # case 1 1 #简单输入 io = IO(file_prefix="2176/", data_id=1) io.input_writeln('3 3 3') io.output_gen('./a.out') # case 2 5 #不考虑输入大于 20 io = IO(file_prefix="2176/", data_id=2) for x in range(50): a = randrange(-1, 21) b = randrange(-1, 21) c = randrange(-1, 21) io.input_writeln(a, b, c) io.output_gen('./a.out') # case 3 4 #小数据范围,暴力可过 io = IO(file_prefix="2176/", data_id=3) for x in range(50): a = randrange(-1, 10) b = randrange(-1, 10) c = randrange(-1, 10) io.input_writeln(a, b, c) io.output_gen('./a.out') # case 3 4 #大数据范围 io = IO(file_prefix="2176/", data_id=4) for x in range(10000): a = randrange(-1, 22) b = randrange(-1, 22) c = randrange(-1, 22) io.input_writeln(a, b, c) io.output_gen('./a.out') os.remove('./a.out') open('2176/scores', 'w').write(open('raw_data/scores/2176').read())
from cyaron import String from cyaron import IO import random cstr = [] for cases in range(13, 21): io = IO('delta' + str(cases) + '.in', 'delta' + str(cases) + '.out') n = random.randint(301, 1000) #print(n) io.input_writeln(n) for i in range(0, n): cstr.append(String.random(random.randint(3, 10))) #print(cstr[i]) io.input_writeln(cstr[i]) pass for i in range(0, n): cstr.append(String.random(random.randint(3, 10))) pass an = random.randint(5001, 100000) #print(an) io.input_writeln(an) for i in range(0, an): #print(String.random(None,charset=cstr)) io.input_writeln(String.random(None, charset=cstr)) pass pass
def p1008(): if not os.path.exists('1008'): os.mkdir('1008') os.system('g++ code/1008.cpp') # case 1 1 #简单输入 io = IO(file_prefix="1008/", data_id=1) io.input_writeln('1') io.input_writeln('3') io.input_writeln('ab') io.input_writeln('ab') io.input_writeln('bc') io.output_gen('./a.out') # case 2~11 # n = 2 for case_id in range(2, 11): io = IO(file_prefix="1008/", data_id=case_id) io.input_writeln(20) for x in range(20): io.input_writeln(2) b = String.random(randrange(90, 100), charset=cyaron.ALPHABET_CAPITAL) c = String.random(randrange(90, 100), charset=cyaron.ALPHABET_CAPITAL) io.input_writeln(b) io.input_writeln(c) io.output_gen('./a.out') # case 11~20 #边界输入 for case_id in range(11, 21): io = IO(file_prefix="1008/", data_id=case_id) io.input_write( open('raw_data/1008/data{}.in'.format(case_id - 10)).read()) io.output_gen('./a.out') open('1008/scores', 'w').write(open('raw_data/scores/1008').read())
def p1366(): if not os.path.exists('1366'): os.mkdir('1366') os.system('g++ code/1366.cpp') # case 1 1 #简单输入 io = IO(file_prefix="1366/", data_id=1) io.input_writeln('5') io.input_writeln('5 1') io.input_writeln('4 1') io.input_writeln('6 1') io.input_writeln('7 2') io.input_writeln('8 3') io.input_writeln('0') io.output_gen('./a.out') # case 2 19 #大数据输入 io = IO(file_prefix="1366/", data_id=2) io.input_write(open('raw_data/1366.in').read()) io.output_gen('./a.out') open('1366/scores', 'w').write(open('raw_data/scores/1366').read())
def test_create_files_without_prefix_id(self): with captured_output() as (out, err): IO(file_prefix="test_prefix") self.assertTrue(os.path.exists("test_prefix.in")) self.assertTrue(os.path.exists("test_prefix.out"))
def p2171(): if not os.path.exists('2171'): os.mkdir('2171') os.system('g++ code/2171.cpp') # case 1 1 #简单输入 io = IO(file_prefix="2171/", data_id=1) io.input_writeln('7') io.input_writeln('1 7 3 5 9 4 10') io.output_gen('./a.out') # case 2~10 #小数据输入 for x in range(2, 11): io = IO(file_prefix="2171/", data_id=x) n = randrange(1, 10) io.input_writeln(n) data_list = [] for y in range(n): data_list.append(randrange(0, 1000 + 1)) io.input_writeln(data_list) io.output_gen('./a.out') # case 11~20 #大数据输入 for x in range(11, 21): io = IO(file_prefix="2171/", data_id=x) n = randrange(900, 1000 + 1) io.input_writeln(n) data_list = [] for y in range(n): data_list.append(randrange(0, 1000 + 1)) io.input_writeln(data_list) io.output_gen('./a.out') open('2171/scores', 'w').write(open('raw_data/scores/2171').read())
def p2080(): if not os.path.exists('2080'): os.mkdir('2080') os.system('g++ code/2080.cpp') # case 1 1 #简单输入 io = IO(file_prefix="2080/", data_id=1) io.input_writeln('ABCBDABF') io.input_writeln('BDCABAF') io.output_gen('./a.out') # case 2~10 #小数据输入 for x in range(2, 11): io = IO(file_prefix="2080/", data_id=x) a = String.random(randrange(7, 11), charset=cyaron.ALPHABET_CAPITAL) b = String.random(randrange(7, 11), charset=cyaron.ALPHABET_CAPITAL) io.input_writeln(a) io.input_writeln(b) io.output_gen('./a.out') # case 11~20 #大数据输入 for x in range(11, 21): io = IO(file_prefix="2080/", data_id=x) a = String.random(randrange(490, 501), charset=cyaron.ALPHABET_CAPITAL) b = String.random(randrange(490, 501), charset=cyaron.ALPHABET_CAPITAL) io.input_writeln(a) io.input_writeln(b) io.output_gen('./a.out') open('2080/scores', 'w').write(open('raw_data/scores/2080').read())
def p1304(): if not os.path.exists('1304'): os.mkdir('1304') os.system('g++ code/1304.cpp') # case 1 1 #简单输入 io = IO(file_prefix="1304/", data_id=1) io.input_writeln('2 2') io.input_writeln('0 2') io.input_writeln('1 0') io.output_gen('./a.out') # case 2~5 只有自然数,小数据 for x in range(2, 6): io = IO(file_prefix="1304/", data_id=x) n = randrange(2, 6) m = randrange(2, 6) io.input_writeln(n, m) for _ in range(n): data_list = [] for __ in range(m): data_list.append(randrange(0, 11)) io.input_writeln(data_list) io.output_gen('./a.out') # case 6~10 只有自然数,小数据 for x in range(6, 11): io = IO(file_prefix="1304/", data_id=x) n = randrange(8, 11) m = randrange(8, 11) io.input_writeln(n, m) for _ in range(n): data_list = [] for __ in range(m): data_list.append(randrange(0, 11)) io.input_writeln(data_list) io.output_gen('./a.out') # case 11~20 完全数据 for x in range(11, 21): io = IO(file_prefix="1304/", data_id=x) n = randrange(8, 11) m = randrange(8, 11) io.input_writeln(n, m) for _ in range(n): data_list = [] for __ in range(m): data_list.append(randrange(-10, 11)) io.input_writeln(data_list) io.output_gen('./a.out') open('1304/scores', 'w').write(open('raw_data/scores/1304').read())
def p2852(): if not os.path.exists('2852'): os.mkdir('2852') os.system('g++ code/2852.cpp') # case 1 1 #简单输入 io = IO(file_prefix="2852/", data_id=1) io.input_writeln('5') io.input_writeln('10') io.input_writeln('3 8') io.input_writeln('8 1 0') io.input_writeln('2 7 4 4') io.input_writeln('4 5 2 6 5') io.output_gen('./a.out') # case 2~10 #小数据 for x in range(2, 11): io = IO(file_prefix="2852/", data_id=x) n = randrange(1, 11) io.input_writeln(n) for y in range(n): row = [] for z in range(y + 1): row.append(randrange(0, 100)) io.input_writeln(row) io.output_gen('./a.out') # case 11~20 #大数据 for x in range(11, 21): io = IO(file_prefix="2852/", data_id=x) n = randrange(90, 101) io.input_writeln(n) for y in range(n): row = [] for z in range(y + 1): row.append(randrange(0, 100)) io.input_writeln(row) io.output_gen('./a.out') open('2852/scores', 'w').write(open('raw_data/scores/2852').read())