From 72833f2a91e0a42a3c5af868d66f3548e8c1c82b Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Wed, 13 Sep 2023 11:21:32 +0200 Subject: [PATCH] Refactor `extract_test_id` function --- src/sinol_make/commands/run/__init__.py | 28 ++++----- src/sinol_make/helpers/package_util.py | 23 +++----- tests/helpers/test_package_util.py | 76 ++++++++++++------------- 3 files changed, 61 insertions(+), 66 deletions(-) diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index cac69de5..af46abc4 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -77,7 +77,7 @@ def print_view(term_width, term_height, task_id, program_groups_scores, all_resu for solution in names: lang = package_util.get_file_lang(solution) for test in tests: - time_sum += package_util.get_time_limit(test, config, lang, args) + time_sum += package_util.get_time_limit(test, config, lang, task_id, args) time_remaining = (len(executions) - print_data.i - 1) * 2 * time_sum / cpus / 1000.0 title = 'Done %4d/%4d. Time remaining (in the worst case): %5d seconds.' \ @@ -120,17 +120,17 @@ def print_table_end(): if results[test].Time is not None: if program_times[program][0] < results[test].Time: program_times[program] = (results[test].Time, package_util.get_time_limit(test, config, - lang, args)) + lang, task_id, args)) elif status == Status.TL: - program_times[program] = (2 * package_util.get_time_limit(test, config, lang, args), - package_util.get_time_limit(test, config, lang, args)) + program_times[program] = (2 * package_util.get_time_limit(test, config, lang, task_id, args), + package_util.get_time_limit(test, config, lang, task_id, args)) if results[test].Memory is not None: if program_memory[program][0] < results[test].Memory: program_memory[program] = (results[test].Memory, package_util.get_memory_limit(test, config, - lang, args)) + lang, task_id, args)) elif status == Status.ML: - program_memory[program] = (2 * package_util.get_memory_limit(test, config, lang, args), - package_util.get_memory_limit(test, config, lang, args)) + program_memory[program] = (2 * package_util.get_memory_limit(test, config, lang, task_id, args), + package_util.get_memory_limit(test, config, lang, task_id, args)) if status == Status.PENDING: group_status = Status.PENDING else: @@ -199,7 +199,7 @@ def print_group_seperator(): if status == Status.PENDING: print(10 * ' ', end=" | ") else: print("%3s" % colorize_status(status), - ("%17s" % color_time(result.Time, package_util.get_time_limit(test, config, lang, args))) + ("%17s" % color_time(result.Time, package_util.get_time_limit(test, config, lang, task_id, args))) if result.Time is not None else 7*" ", end=" | ") print() if not hide_memory: @@ -207,7 +207,7 @@ def print_group_seperator(): for program in program_group: lang = package_util.get_file_lang(program) result = all_results[program][package_util.get_group(test, task_id)][test] - print(("%20s" % color_memory(result.Memory, package_util.get_memory_limit(test, config, lang, args))) + print(("%20s" % color_memory(result.Memory, package_util.get_memory_limit(test, config, lang, task_id, args))) if result.Memory is not None else 10*" ", end=" | ") print() @@ -616,8 +616,10 @@ def run_solutions(self, compiled_commands, names, solutions): lang = package_util.get_file_lang(name) if result: for test in self.tests: - executions.append((name, executable, test, package_util.get_time_limit(test, self.config, lang, self.args), - package_util.get_memory_limit(test, self.config, lang, self.args), self.timetool_path)) + executions.append((name, executable, test, + package_util.get_time_limit(test, self.config, lang, self.ID, self.args), + package_util.get_memory_limit(test, self.config, lang, self.ID, self.args), + self.timetool_path)) all_results[name][self.get_group(test)][test] = ExecutionResult(Status.PENDING) os.makedirs(paths.get_executions_path(name), exist_ok=True) else: @@ -1152,8 +1154,8 @@ def run(self, args): lang = package_util.get_file_lang(solution) for test in self.tests: # The functions will exit if the limits are not set - _ = package_util.get_time_limit(test, self.config, lang, self.args) - _ = package_util.get_memory_limit(test, self.config, lang, self.args) + _ = package_util.get_time_limit(test, self.config, lang, self.ID, self.args) + _ = package_util.get_memory_limit(test, self.config, lang, self.ID, self.args) results, all_results = self.compile_and_run(solutions) self.check_errors(all_results) diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 0ee3556c..d6a72289 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -23,21 +23,14 @@ def get_task_id() -> str: util.exit_with_error("Invalid task id. Task id should be 3 characters long.") -def extract_test_id(test_path, task_id=None): +def extract_test_id(test_path, task_id): """ Extracts test group and number from test path. For example for test abc1a.in it returns 1a. :param test_path: Path to test file. - :param task_id: Task id. If None, it is extracted from config.yml. - If not found, it's assumed that the length of task id is 3. + :param task_id: Task id. :return: Test group and number. """ - if task_id is None: - try: - task_id = get_task_id() - except FileNotFoundError: - task_id = "abc" - return os.path.split(os.path.splitext(test_path)[0])[1][len(task_id):] @@ -114,8 +107,8 @@ def _get_limit_from_dict(dict: Dict[str, Any], limit_type: LimitTypes, test_id: return None -def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], lang: str): - test_id = extract_test_id(test_path) +def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], lang: str, task_id: str): + test_id = extract_test_id(test_path, task_id) test_group = str(get_group(test_path)) global_limit = _get_limit_from_dict(config, limit_type, test_id, test_group, test_path) override_limits_dict = config.get("override_limits", {}).get(lang, {}) @@ -132,7 +125,7 @@ def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], l util.exit_with_error(f'Memory limit was not defined for test {os.path.basename(test_path)} in config.yml.') -def get_time_limit(test_path, config, lang, args=None): +def get_time_limit(test_path, config, lang, task_id, args=None): """ Returns time limit for given test. """ @@ -140,10 +133,10 @@ def get_time_limit(test_path, config, lang, args=None): return args.tl * 1000 str_config = util.stringify_keys(config) - return _get_limit(LimitTypes.TIME_LIMIT, test_path, str_config, lang) + return _get_limit(LimitTypes.TIME_LIMIT, test_path, str_config, lang, task_id) -def get_memory_limit(test_path, config, lang, args=None): +def get_memory_limit(test_path, config, lang, task_id, args=None): """ Returns memory limit for given test. """ @@ -151,7 +144,7 @@ def get_memory_limit(test_path, config, lang, args=None): return int(args.ml * 1024) str_config = util.stringify_keys(config) - return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang) + return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang, task_id) def validate_test_names(task_id): diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index 4e993442..d30eef8a 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -17,10 +17,10 @@ def test_get_task_id(create_package): def test_extract_test_id(): - assert package_util.extract_test_id("in/abc1a.in") == "1a" - assert package_util.extract_test_id("in/abc10a.in") == "10a" - assert package_util.extract_test_id("in/abc12ca.in") == "12ca" - assert package_util.extract_test_id("in/abc0ocen.in") == "0ocen" + assert package_util.extract_test_id("in/abc1a.in", "abc") == "1a" + assert package_util.extract_test_id("in/abc10a.in", "abc") == "10a" + assert package_util.extract_test_id("in/abc12ca.in", "abc") == "12ca" + assert package_util.extract_test_id("in/abc0ocen.in", "abc") == "0ocen" assert package_util.extract_test_id("in/long_task_id2bc.in", "long_task_id") == "2bc" @@ -61,17 +61,17 @@ def test_get_time_limit(): } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc2a.in", config, "cpp") == 2000 - assert package_util.get_time_limit("in/abc2b.in", config, "cpp") == 2000 - assert package_util.get_time_limit("in/abc3a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc3ocen.in", config, "cpp") == 5000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") == 2000 + assert package_util.get_time_limit("in/abc2b.in", config, "cpp", "abc") == 2000 + assert package_util.get_time_limit("in/abc3a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc3ocen.in", config, "cpp", "abc") == 5000 - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 2000 - assert package_util.get_time_limit("in/abc2a.in", config, "py") == 3000 - assert package_util.get_time_limit("in/abc2b.in", config, "py") == 3000 - assert package_util.get_time_limit("in/abc3a.in", config, "py") == 2000 - assert package_util.get_time_limit("in/abc3ocen.in", config, "py") == 6000 + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 2000 + assert package_util.get_time_limit("in/abc2a.in", config, "py", "abc") == 3000 + assert package_util.get_time_limit("in/abc2b.in", config, "py", "abc") == 3000 + assert package_util.get_time_limit("in/abc3a.in", config, "py", "abc") == 2000 + assert package_util.get_time_limit("in/abc3ocen.in", config, "py", "abc") == 6000 # Test getting default time limit. config = { @@ -86,12 +86,12 @@ def test_get_time_limit(): } } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 2000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 2000 with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "cpp") + package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "py") + package_util.get_time_limit("in/abc2a.in", config, "py", "abc") config = { "time_limits": { @@ -106,11 +106,11 @@ def test_get_time_limit(): } } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "cpp") - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 1000 - assert package_util.get_time_limit("in/abc2a.in", config, "py") == 500 + package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 1000 + assert package_util.get_time_limit("in/abc2a.in", config, "py", "abc") == 500 def test_get_memory_limit(): @@ -131,15 +131,15 @@ def test_get_memory_limit(): } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 256 - assert package_util.get_memory_limit("in/abc2a.in", config, "cpp") == 512 - assert package_util.get_memory_limit("in/abc2b.in", config, "cpp") == 512 - assert package_util.get_memory_limit("in/abc3ocen.in", config, "cpp") == 128 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 256 + assert package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") == 512 + assert package_util.get_memory_limit("in/abc2b.in", config, "cpp", "abc") == 512 + assert package_util.get_memory_limit("in/abc3ocen.in", config, "cpp", "abc") == 128 - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 512 - assert package_util.get_memory_limit("in/abc2a.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc2b.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc3ocen.in", config, "py") == 256 + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 512 + assert package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc2b.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc3ocen.in", config, "py", "abc") == 256 # Test getting default memory limit. config = { @@ -154,12 +154,12 @@ def test_get_memory_limit(): } } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 1024 - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 2048 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 1024 + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 2048 with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "cpp") + package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "py") + package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") config = { "memory_limits": { @@ -174,11 +174,11 @@ def test_get_memory_limit(): } } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 1024 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 1024 with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "cpp") - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc2a.in", config, "py") == 512 + package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") == 512 @pytest.mark.parametrize("create_package", [util.get_simple_package_path()], indirect=True)