示例#1
0
    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)
示例#2
0
    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.")
示例#3
0
    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")
示例#4
0
    def test_output_gen(self):
        with captured_output() as (out, err):
            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")
示例#5
0
 def test_create_files_prefix_id(self):
     with captured_output() as (out, err):
         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"))
示例#6
0
    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."
        )
示例#7
0
    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)
示例#8
0
    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."
        )
示例#9
0
    def test_write_stuff(self):
        with captured_output() as (out, err):
            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)
示例#10
0
 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"))
示例#11
0
 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"))