Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Sep 22, 2023
1 parent 78c459d commit 344a937
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 9 deletions.
71 changes: 71 additions & 0 deletions tests/commands/run/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pytest
import copy

from sinol_make.helpers import cache
from sinol_make.structs.cache_structs import CacheFile
from ...fixtures import *
from .util import *
from sinol_make import configure_parsers, util, oiejq
Expand Down Expand Up @@ -553,3 +555,72 @@ def test_flag_tests_not_existing_tests(create_package, time_tool, capsys):
assert e.value.code == 1
out = capsys.readouterr().out
assert "There are no tests to run." in out


@pytest.mark.parametrize("create_package", [get_simple_package_path(), get_verify_status_package_path(),
get_checker_package_path(), get_library_package_path(),
get_library_string_args_package_path(), get_limits_package_path(),
get_override_limits_package_path()], indirect=True)
def test_results_caching(create_package, time_tool):
"""
Test if test results are cached.
"""
package_path = create_package
create_ins_outs(package_path)
parser = configure_parsers()
args = parser.parse_args(["run", "--time-tool", time_tool])

def run():
command = Command()
command.run(args)
return command

start_time = time.time()
run()
length = time.time() - start_time

start_time = time.time()
command = run()
end_time = time.time() - start_time
assert end_time - start_time < length / 2

solutions = command.get_solutions(None)
for solution in solutions:
cache_file: CacheFile = cache.get_cache_file(solution)
for test in command.tests:
assert util.get_file_md5(test) in cache_file.tests
test_cache = cache_file.tests[util.get_file_md5(test)]
lang = package_util.get_file_lang(solution)
assert test_cache.time_limit == package_util.get_time_limit(test, command.config, lang, command.ID)
assert test_cache.memory_limit == package_util.get_memory_limit(test, command.config, lang, command.ID)
assert cache_file is not None
assert cache_file.tests != {}


@pytest.mark.parametrize("create_package", [get_checker_package_path()], indirect=True)
def test_results_caching_checker_changed(create_package, time_tool):
"""
Test if after changing checker source code, all cached test results are removed.
"""
package_path = create_package
create_ins_outs(package_path)
parser = configure_parsers()
args = parser.parse_args(["run", "--time-tool", time_tool])

# First run to cache test results.
command = Command()
command.run(args)

# Change checker source code.
checker_source = ""
with open(os.path.join(os.getcwd(), "prog", "chkchk.cpp"), "r") as f:
checker_source = f.read()
with open(os.path.join(os.getcwd(), "prog", "chkchk.cpp"), "w") as f:
f.write("// Changed checker source code.\n" + checker_source)

# Compile checker check if test results are removed.
command.compile_checker()
solutions = command.get_solutions(None)
for solution in solutions:
cache_file: CacheFile = cache.get_cache_file(solution)
assert cache_file.tests == {}
60 changes: 52 additions & 8 deletions tests/helpers/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import tempfile

from sinol_make.helpers import compile
from sinol_make.helpers.cache import check_compiled, save_compiled
from sinol_make.helpers import cache
from sinol_make.structs.cache_structs import CacheFile, CacheTest
from sinol_make.structs.status_structs import ExecutionResult, Status


def test_compilation_caching():
Expand All @@ -11,22 +13,64 @@ def test_compilation_caching():
program = os.path.join(tmpdir, 'program.cpp')
open(program, 'w').write('int main() { return 0; }')

assert check_compiled(program) is None
assert cache.check_compiled(program) is None

assert compile.compile(program, os.path.join(tmpdir, 'program'), compile_log=None)
exe_path = check_compiled(program)
exe_path = cache.check_compiled(program)
assert exe_path is not None

assert compile.compile(program, os.path.join(tmpdir, 'program'), compile_log=None)
exe_path2 = check_compiled(program)
exe_path2 = cache.check_compiled(program)
assert exe_path2 == exe_path

open(program, 'w').write('int main() { return 1; }')
assert check_compiled(program) is None
assert cache.check_compiled(program) is None
assert compile.compile(program, os.path.join(tmpdir, 'program'), compile_log=None)
assert check_compiled(program) is not None
assert cache.check_compiled(program) is not None

open(program, 'w').write('int main() { return 0; }')
assert check_compiled(program) is None
assert cache.check_compiled(program) is None
assert compile.compile(program, os.path.join(tmpdir, 'program'), compile_log=None)
assert check_compiled(program) is not None
assert cache.check_compiled(program) is not None


def test_cache():
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
assert cache.get_cache_file("program.cpp") == CacheFile()

cache_file = CacheFile(
md5sum="md5sum",
executable_path="program.e",
tests={
"md5sum1": CacheTest(
time_limit=1000,
memory_limit=1024,
time_tool="time",
result=ExecutionResult(
status=Status.OK,
Time=0.5,
Memory=512,
Points=10,
)
),
"md5sum2": CacheTest(
time_limit=2000,
memory_limit=2048,
time_tool="time",
result=ExecutionResult(
status=Status.OK,
Time=1,
Memory=1024,
Points=20,
)
),
}
)

with open("program.cpp", "w") as f:
f.write("int main() { return 0; }")
cache_file.save("program.cpp")
assert cache.get_cache_file("program.cpp") == cache_file
cache.save_compiled("program.cpp", "program.e", is_checker=True)
assert cache.get_cache_file("program.cpp").tests == {}
1 change: 0 additions & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def get_stack_size_package_path():
return os.path.join(os.path.dirname(__file__), "packages", "stc")



def get_override_limits_package_path():
"""
Get path to package with `override_limits` present in config (/test/packages/ovl)
Expand Down

0 comments on commit 344a937

Please sign in to comment.