Skip to content

Commit

Permalink
Only sanity-check if all validators accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
thorehusfeldt committed Feb 1, 2024
1 parent 9c1f7c3 commit 65c9143
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions bin/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class Testcase:
# tested, and `short_path` is the name of the testcase relative to
# `problem.path / 'data'`.
"""

def __init__(self, base_problem, path: Path, *, short_path=None):
assert path.suffix == '.in' or path.suffixes == [".in", ".statement"]

self.problem = base_problem

self.in_path = path
self.ans_path = (
self.in_path.with_suffix('.ans')
Expand All @@ -47,8 +48,13 @@ def __init__(self, base_problem, path: Path, *, short_path=None):
if self.root == 'bad':
warn('data/bad is deprecated. Use data/{invalid_inputs,invalid_answers} instead.')
self.root = 'invalid_inputs' if self.ans_path.is_file() else 'invalid_answers'
assert self.root in ['invalid_inputs', 'invalid_answers', 'secret', 'sample', 'test'], self.root # TODO add invalid_outputs

assert self.root in [
'invalid_inputs',
'invalid_answers',
'secret',
'sample',
'test',
], self.root # TODO add invalid_outputs

self.included = False
if path.is_symlink():
Expand All @@ -63,7 +69,9 @@ def __init__(self, base_problem, path: Path, *, short_path=None):
# Read using the short_path instead of the in_path, because during
# generate the testcase will live in a temporary directory, where
# testdata.yaml doesn't exist.
self.testdata_yaml = self.problem.get_testdata_yaml(self.problem.path / 'data' / self.short_path)
self.testdata_yaml = self.problem.get_testdata_yaml(
self.problem.path / 'data' / self.short_path
)

def with_suffix(self, ext):
return self.in_path.with_suffix(ext)
Expand Down Expand Up @@ -124,25 +132,27 @@ def validate_format(
bar,
constraints=None,
warn_instead_of_error=False,
args=None, # TODO never used?
args=None, # TODO never used?
):


check_constraints = constraints is not None
match mode:
case validate.Mode.INPUT:
validators = self.problem.validators(validate.InputValidator, check_constraints=check_constraints)
validators = self.problem.validators(
validate.InputValidator, check_constraints=check_constraints
)
expect_rejection = self.root == 'invalid_inputs'
case validate.Mode.ANSWER:
validators = (self.problem.validators(validate.AnswerValidator, check_constraints=check_constraints) +
self.problem.validators(validate.OutputValidator, check_constraints=check_constraints)
)
validators = self.problem.validators(
validate.AnswerValidator, check_constraints=check_constraints
) + self.problem.validators(
validate.OutputValidator, check_constraints=check_constraints
)
expect_rejection = self.root == 'invalid_answers'
case validate.Mode.OUTPUT:
raise NotImplementedError
case _:
raise ValueError


validator_accepted = []
for validator in validators:
Expand All @@ -155,7 +165,9 @@ def validate_format(

ret = validator.run(self, constraints=constraints, args=flags)
if ret.ok not in [True, config.RTV_WA]:
warn(f"Expected {validator} to exit with {config.RTV_AC} or {config.RTV_WA}, got {ret.ok}")
warn(
f"Expected {validator} to exit with {config.RTV_AC} or {config.RTV_WA}, got {ret.ok}"
)
ret.ok = config.RTV_WA
# TODO could also interpret 0 as RTV_AC
ok = ret.ok == True
Expand Down Expand Up @@ -186,7 +198,10 @@ def validate_format(
data = ret.err

bar.part_done(
ok or expect_rejection, message, data=data, warn_instead_of_error=warn_instead_of_error
ok or expect_rejection,
message,
data=data,
warn_instead_of_error=warn_instead_of_error,
)

if ok is True:
Expand Down Expand Up @@ -216,9 +231,16 @@ def validate_format(

break

validate.sanity_check(self.in_path if mode == validate.Mode.INPUT else self.ans_path, bar)

if expect_rejection and all(validator_accepted):
bar.error(f"{mode} validation (unexpectedly) succeeded")
if all(validator_accepted):
if expect_rejection:
success = False
bar.error(f"{mode} validation (unexpectedly) succeeded")
else:
success = True
validate.sanity_check(
self.in_path if mode == validate.Mode.INPUT else self.ans_path, bar
)
else:
success = not expect_rejection

return all(validator_accepted) != expect_rejection
return success

0 comments on commit 65c9143

Please sign in to comment.