Skip to content

Commit

Permalink
Verify order of tests in inwer (#161)
Browse files Browse the repository at this point in the history
* Verify test order

* Add tests
  • Loading branch information
MasloMaslane authored Nov 29, 2023
1 parent 20a3ea5 commit 27ceb26
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/sinol_make/commands/inwer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import os
import multiprocessing as mp
from functools import cmp_to_key
from typing import Dict, List

from sinol_make import util
Expand Down Expand Up @@ -119,6 +120,69 @@ def verify_and_print_table(self) -> Dict[str, TestResult]:

return results

def verify_tests_order(self):
"""
Verifies if tests are in correct order.
"""
def get_id(test, func=str.isalpha):
basename = os.path.basename(os.path.splitext(test)[0])
return "".join(filter(func, basename[len(self.task_id):]))

ocen = sorted([test for test in self.tests if test.endswith('ocen.in')],
key=lambda test: int("".join(filter(str.isdigit, get_id(test, str.isdigit)))))
tests = list(set(self.tests) - set(ocen))
last_id = None
last_test = None
for test in ocen:
basename = os.path.basename(os.path.splitext(test)[0])
test_id = int("".join(filter(str.isdigit, basename)))
if last_id is not None and test_id != last_id + 1:
util.exit_with_error(f'Test {os.path.basename(test)} is in wrong order. '
f'Last test was {os.path.basename(last_test)}.')
last_id = test_id
last_test = test

def is_next(last, curr):
i = len(last) - 1
while i >= 0:
if last[i] != 'z':
last = last[:i] + chr(ord(last[i]) + 1) + last[i + 1:]
break
else:
last = last[:i] + 'a' + last[i + 1:]
i -= 1
if i < 0:
last = 'a' + last
return last == curr

def compare_id(test1, test2):
id1 = get_id(test1)
id2 = get_id(test2)
if id1 == id2:
return 0
if len(id1) == len(id2):
if id1 < id2:
return -1
return 1
elif len(id1) < len(id2):
return -1
return 1

groups = {}
for group in package_util.get_groups(self.tests, self.task_id):
groups[group] = sorted([test for test in tests if package_util.get_group(test, self.task_id) == group],
key=cmp_to_key(compare_id))
for group, group_tests in groups.items():
last_id = None
last_test = None
for test in group_tests:
test_id = get_id(test)
if last_id is not None and not is_next(last_id, test_id):
util.exit_with_error(f'Test {os.path.basename(test)} is in wrong order. '
f'Last test was {os.path.basename(last_test)}.')
last_id = test_id
last_test = test

def run(self, args: argparse.Namespace):
util.exit_if_not_package()

Expand Down Expand Up @@ -154,5 +218,7 @@ def run(self, args: argparse.Namespace):
if len(failed_tests) > 0:
util.exit_with_error(f'Verification failed for tests: {", ".join(failed_tests)}')
else:
print("Verifying tests order...")
self.verify_tests_order()
print(util.info('Verification successful.'))
exit(0)
33 changes: 33 additions & 0 deletions tests/commands/inwer/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,36 @@ def test_tests_comparator():
[f"{ti}1a.in", f"{ti}1b.in", f"{ti}2a.in", f"{ti}10a.in"]
assert inwer_util.sort_tests([f"{ti}2a.in", f"{ti}1a.in", f"{ti}1b.in", f"{ti}10a.in", f"{ti}10b.in"], ti) == \
[f"{ti}1a.in", f"{ti}1b.in", f"{ti}2a.in", f"{ti}10a.in", f"{ti}10b.in"]


def test_verify_tests_order():
command = Command()
command.task_id = "abc"
command.tests = ["abc1ocen.in", "abc2ocen.in", "abc3ocen.in",
"abc1a.in", "abc1b.in", "abc1c.in", "abc1d.in",
"abc2z.in", "abc2aa.in", "abc2ab.in", "abc2ac.in"]
command.verify_tests_order()

command.tests.remove("abc2ocen.in")
with pytest.raises(SystemExit):
command.verify_tests_order()

command.tests.append("abc2ocen.in")
command.tests.remove("abc1c.in")
with pytest.raises(SystemExit):
command.verify_tests_order()

command.tests.append("abc1c.in")
command.tests.remove("abc2aa.in")
with pytest.raises(SystemExit):
command.verify_tests_order()

command.tests.append("abc2aa.in")
command.tests.remove("abc1ocen.in")
command.tests.remove("abc2ocen.in")
command.tests.remove("abc3ocen.in")
command.tests.append("abc9ocen.in")
command.tests.append("abc10ocen.in")
command.tests.append("abc11ocen.in")

command.verify_tests_order()

0 comments on commit 27ceb26

Please sign in to comment.