From 7c0fd44171797ef763635ad93eab0ea5ccbc6085 Mon Sep 17 00:00:00 2001 From: Naoto Mizuno Date: Tue, 20 Oct 2020 09:07:14 +0900 Subject: [PATCH] Test combined examples --- tests/example_tests/__init__.py | 0 tests/example_tests/test_examples.py | 72 ++++++++++++++++++++-------- tests/example_tests/util.py | 8 ---- 3 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 tests/example_tests/__init__.py delete mode 100644 tests/example_tests/util.py diff --git a/tests/example_tests/__init__.py b/tests/example_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/example_tests/test_examples.py b/tests/example_tests/test_examples.py index 52198a7..a57733f 100644 --- a/tests/example_tests/test_examples.py +++ b/tests/example_tests/test_examples.py @@ -1,28 +1,44 @@ from pathlib import Path +from subprocess import check_output, run +import sys +from tempfile import NamedTemporaryFile +from typing import TextIO import pytest -import util + +sources = ( + ('A', 'dsu_practice.py'), + ('B', 'fenwick_practice.py'), + ('C', 'floor_sum_practice.py'), + ('D', 'maxflow_practice.py'), + ('E', 'mincostflow_practice.py'), + ('F', 'convolution_practice.py'), + ('F', 'convolution_practice_int.py'), + ('G', 'scc_practice.py'), + ('H', 'twosat_practice.py'), + ('I', 'sa_practice.py'), + ('J', 'segtree_practice.py'), + ('J', 'segtree_practice_reversed.py'), + ('K', 'lazysegtree_practice_k.py'), + ('K', 'lazysegtree_practice_k_wo_modint.py'), + ('L', 'lazysegtree_practice_l.py')) + + +def run_example(source: str, stdin: TextIO) -> str: + source_path = Path(__file__).parent.parent.parent / 'example' / source + output = check_output((sys.executable, source_path), stdin=stdin) + return output.decode() + + +def combine(source: str, output: str) -> None: + source_path = Path(__file__).parent.parent.parent / 'example' / source + run((sys.executable, '-m', 'atcoder', source_path, '-o', output)) class TestExamples: - @pytest.mark.parametrize('problem_id, source', ( - ('A', 'dsu_practice.py'), - ('B', 'fenwick_practice.py'), - ('C', 'floor_sum_practice.py'), - ('D', 'maxflow_practice.py'), - ('E', 'mincostflow_practice.py'), - ('F', 'convolution_practice.py'), - ('F', 'convolution_practice_int.py'), - ('G', 'scc_practice.py'), - ('H', 'twosat_practice.py'), - ('I', 'sa_practice.py'), - ('J', 'segtree_practice.py'), - ('J', 'segtree_practice_reversed.py'), - ('K', 'lazysegtree_practice_k.py'), - ('K', 'lazysegtree_practice_k_wo_modint.py'), - ('L', 'lazysegtree_practice_l.py'))) - def test_examples(self, problem_id, source) -> None: + @pytest.mark.parametrize('problem_id, source', sources) + def test_examples(self, problem_id: str, source: str) -> None: input_path = (Path(__file__).parent / 'input') stdin_paths = list(input_path.glob(f'{problem_id}*_in.txt')) stdout_paths = list(input_path.glob(f'{problem_id}*_out.txt')) @@ -31,7 +47,25 @@ def test_examples(self, problem_id, source) -> None: for stdin_path, stdout_path in zip(stdin_paths, stdout_paths): with open(stdin_path) as stdin: - output = util.run(source, stdin).decode() + output = run_example(source, stdin) with open(stdout_path) as stdout: assert output == stdout.read() + + @pytest.mark.parametrize('problem_id, source', sources) + def test_combined_examples(self, problem_id: str, source: str) -> None: + with NamedTemporaryFile() as f: + combine(source, f.name) + + input_path = (Path(__file__).parent / 'input') + stdin_paths = list(input_path.glob(f'{problem_id}*_in.txt')) + stdout_paths = list(input_path.glob(f'{problem_id}*_out.txt')) + stdin_paths.sort() + stdout_paths.sort() + + for stdin_path, stdout_path in zip(stdin_paths, stdout_paths): + with open(stdin_path) as stdin: + output = run_example(f.name, stdin) + + with open(stdout_path) as stdout: + assert output == stdout.read() diff --git a/tests/example_tests/util.py b/tests/example_tests/util.py deleted file mode 100644 index afbb67b..0000000 --- a/tests/example_tests/util.py +++ /dev/null @@ -1,8 +0,0 @@ -from pathlib import Path -from subprocess import check_output -import sys - - -def run(source, stdin): - source_path = Path(__file__).parent.parent.parent / 'example' / source - return check_output((sys.executable, source_path), stdin=stdin)