Skip to content

Commit

Permalink
Refactor extract_test_id function
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Sep 13, 2023
1 parent 7780a7a commit 72833f2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 66 deletions.
28 changes: 15 additions & 13 deletions src/sinol_make/commands/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.' \
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -199,15 +199,15 @@ 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:
print(8*" ", end=" | ")
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()

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 8 additions & 15 deletions src/sinol_make/helpers/package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):]


Expand Down Expand Up @@ -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, {})
Expand All @@ -132,26 +125,26 @@ 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.
"""
if args is not None and hasattr(args, "tl") and args.tl is not 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.
"""
if args is not None and hasattr(args, "ml") and args.ml is not 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):
Expand Down
76 changes: 38 additions & 38 deletions tests/helpers/test_package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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 = {
Expand All @@ -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": {
Expand All @@ -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():
Expand All @@ -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 = {
Expand All @@ -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": {
Expand All @@ -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)
Expand Down

0 comments on commit 72833f2

Please sign in to comment.