Skip to content

Commit

Permalink
Support global constraints for every subtask
Browse files Browse the repository at this point in the history
Resolve #56
  • Loading branch information
fushar committed Nov 13, 2016
1 parent c61fe22 commit e777fcb
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 28 deletions.
4 changes: 4 additions & 0 deletions ete-test/resources/subtasks/subtasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ProblemSpec : public BaseProblemSpec {
LINE(res);
}

void Constraints() {
CONS(A <= B);
}

void Subtask1() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
Expand Down
6 changes: 4 additions & 2 deletions include/tcframe/spec/constraint/ConstraintSuite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class ConstraintSuiteBuilder {
, isInMultipleTestCasesConstraints_(false) {}

ConstraintSuiteBuilder& newSubtask() {
if (hasCurrentSubtask_) {
if (hasCurrentSubtask_ || !curConstraints.empty()) {
addCurrentSubtask();
} else {
}

if (!hasCurrentSubtask_) {
curSubtaskId_ = 0;
}

Expand Down
19 changes: 8 additions & 11 deletions include/tcframe/spec/core/BaseProblemSpec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,13 @@ class BaseProblemSpec
}

ConstraintSuite buildConstraintSuite() {
try {
Constraints();
} catch (NotImplementedException&) {
for (auto subtask : subtasks_) {
try {
ConstraintSuiteBuilder::newSubtask();
(this->*subtask)();
} catch (NotImplementedException&) {
break;
}
Constraints();
for (auto subtask : subtasks_) {
try {
ConstraintSuiteBuilder::newSubtask();
(this->*subtask)();
} catch (NotImplementedException&) {
break;
}
}

Expand All @@ -90,7 +87,7 @@ class BaseProblemSpec
virtual void GradingConfig() {}
virtual void InputFormat() = 0;
virtual void OutputFormat() {}
virtual void Constraints() {throw NotImplementedException();}
virtual void Constraints() {}
virtual void MultipleTestCasesConstraints() {}
virtual void Subtask1() {throw NotImplementedException();}
virtual void Subtask2() {throw NotImplementedException();}
Expand Down
8 changes: 4 additions & 4 deletions include/tcframe/spec/testcase/TestSuite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class TestSuiteBuilder {
TestSuiteBuilder()
: beforeClosure_([]{})
, afterClosure_([]{})
, curSampleSubtaskIds_({-1})
, curOfficialSubtaskIds_({-1})
, curSampleSubtaskIds_({})
, curOfficialSubtaskIds_({})
, curOfficialTestGroupId_(-1)
, hasCurOfficialTestGroup_(false)
, hasCurSampleTestCase_(false) {
Expand All @@ -94,7 +94,7 @@ class TestSuiteBuilder {
}

hasCurSampleTestCase_ = true;
curSampleSubtaskIds_ = {-1};
curSampleSubtaskIds_ = {};
curSubtaskIds_ = &curSampleSubtaskIds_;
curSampleInputLines_ = optional<vector<string>>();
curSampleOutputLines_ = optional<vector<string>>();
Expand All @@ -111,7 +111,7 @@ class TestSuiteBuilder {

hasCurOfficialTestGroup_ = true;
curOfficialTestGroupId_++;
curOfficialSubtaskIds_ = {-1};
curOfficialSubtaskIds_ = {};
curSubtaskIds_ = &curOfficialSubtaskIds_;
curOfficialTestCases_.clear();

Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/verifier/Verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Verifier {
}
}

if (subtaskIds.count(subtask.id())) {
if (subtask.id() == -1 || subtaskIds.count(subtask.id())) {
if (!unsatisfiedConstraintDescriptions.empty()) {
unsatisfiedConstraintDescriptionsBySubtaskId[subtask.id()] = unsatisfiedConstraintDescriptions;
}
Expand Down
16 changes: 15 additions & 1 deletion test/tcframe/generator/GeneratorLoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ TEST_F(GeneratorLoggerTests, ConstraintsVerificationFailure) {
logger.logConstraintsVerificationFailure(result);
}

TEST_F(GeneratorLoggerTests, ConstraintsVerificationFailure_WithGroups) {
TEST_F(GeneratorLoggerTests, ConstraintsVerificationFailure_WithSubtasks) {
{
InSequence sequence;
EXPECT_CALL(engine, logListItem1(2, "Does not satisfy subtask 2, on constraints:"));
Expand All @@ -91,6 +91,20 @@ TEST_F(GeneratorLoggerTests, ConstraintsVerificationFailure_WithGroups) {
logger.logConstraintsVerificationFailure(result);
}

TEST_F(GeneratorLoggerTests, ConstraintsVerificationFailure_WithConstraintsAndSubtasks) {
{
InSequence sequence;
EXPECT_CALL(engine, logListItem1(2, "Does not satisfy constraints, on:"));
EXPECT_CALL(engine, logListItem2(3, "X <= 10"));
EXPECT_CALL(engine, logListItem1(2, "Does not satisfy subtask 2, on constraints:"));
EXPECT_CALL(engine, logListItem2(3, "A <= 10"));
EXPECT_CALL(engine, logListItem2(3, "B <= 10"));
EXPECT_CALL(engine, logListItem1(2, "Satisfies subtask 1 but is not assigned to it"));
}
ConstraintsVerificationResult result({{-1, {"X <= 10"}}, {2, {"A <= 10", "B <= 10"}}}, {1});
logger.logConstraintsVerificationFailure(result);
}

TEST_F(GeneratorLoggerTests, MultipleTestCasesCombinationIntroduction) {
EXPECT_CALL(engine, logHangingParagraph(1, "Combining test cases into a single file (foo_3): "));

Expand Down
25 changes: 25 additions & 0 deletions test/tcframe/spec/constraint/ConstraintSuiteBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,29 @@ TEST_F(ConstraintSuiteBuilderTests, Building_WithSubtasks) {
EXPECT_THAT(constraintSuite2, Eq(expected));
}

TEST_F(ConstraintSuiteBuilderTests, Building_GlobalConstraintsAndSubtasks) {
ConstraintSuite constraintSuite = builder
.addConstraint([]{return true;}, "1 <= X && X <= 100")
.addConstraint([]{return true;}, "1 <= Y && Y <= 100")
.newSubtask()
.addConstraint([]{return true;}, "1 <= A && A <= 10")
.addConstraint([]{return true;}, "1 <= B && B <= 10")
.newSubtask()
.addConstraint([]{return true;}, "1 <= C && C <= 10")
.addConstraint([]{return true;}, "1 <= D && D <= 10")
.build();
ConstraintSuite expected({
Subtask(-1, {
Constraint([]{return true;}, "1 <= X && X <= 100"),
Constraint([]{return true;}, "1 <= Y && Y <= 100")}),
Subtask(1, {
Constraint([]{return true;}, "1 <= A && A <= 10"),
Constraint([]{return true;}, "1 <= B && B <= 10")}),
Subtask(2, {
Constraint([]{return true;}, "1 <= C && C <= 10"),
Constraint([]{return true;}, "1 <= D && D <= 10")})}, {});

EXPECT_THAT(constraintSuite, Eq(expected));
}

}
3 changes: 2 additions & 1 deletion test/tcframe/spec/core/BaseProblemSpecTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BaseProblemSpecTests : public Test {
}
};

class ProblemSpecWithSubtasks : public ProblemSpec {
class ProblemSpecWithSubtasks : public ProblemSpecWithConstraints {
protected:
void Subtask1() {
addConstraint([=] {return 1 <= A && A <= 100;}, "1 <= A && A <= 100");
Expand Down Expand Up @@ -134,6 +134,7 @@ TEST_F(BaseProblemSpecTests, MultipleTestCasesConstraints) {
TEST_F(BaseProblemSpecTests, Subtasks) {
ConstraintSuite constraintSuite = ProblemSpecWithSubtasks().buildConstraintSuite();
EXPECT_THAT(constraintSuite.constraints(), ElementsAre(
AllOf(Property(&Subtask::id, -1), Property(&Subtask::constraints, SizeIs(2))),
AllOf(Property(&Subtask::id, 1), Property(&Subtask::constraints, SizeIs(3))),
AllOf(Property(&Subtask::id, 2), Property(&Subtask::constraints, SizeIs(2)))));
}
Expand Down
16 changes: 8 additions & 8 deletions test/tcframe/spec/testcase/TestSuiteBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ TEST_F(TestSuiteBuilderTests, Building_OnlySample) {
TestGroup(0, {
TestCaseBuilder()
.setId("foo_sample_1")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setData(new SampleTestCaseData("10\n20\n", "yes\n"))
.build(),
TestCaseBuilder()
.setId("foo_sample_2")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setData(new SampleTestCaseData("30\n"))
.build()})});

Expand All @@ -82,13 +82,13 @@ TEST_F(TestSuiteBuilderTests, Building_OnlyOfficial) {
TestGroup(-1, {
TestCaseBuilder()
.setId("foo_1")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setDescription("N = 1")
.setData(new OfficialTestCaseData([]{}))
.build(),
TestCaseBuilder()
.setId("foo_2")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setDescription("N = 2")
.setData(new OfficialTestCaseData([]{}))
.build()})});
Expand Down Expand Up @@ -120,24 +120,24 @@ TEST_F(TestSuiteBuilderTests, Building_Both) {
TestGroup(0, {
TestCaseBuilder()
.setId("foo_sample_1")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setData(new SampleTestCaseData("10\n20\n", "yes\n"))
.build(),
TestCaseBuilder()
.setId("foo_sample_2")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setData(new SampleTestCaseData("30\n"))
.build()}),
TestGroup(-1, {
TestCaseBuilder()
.setId("foo_1")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setDescription("N = 1")
.setData(new OfficialTestCaseData([]{}))
.build(),
TestCaseBuilder()
.setId("foo_2")
.setSubtaskIds({-1})
.setSubtaskIds({})
.setDescription("N = 2")
.setData(new OfficialTestCaseData([]{}))
.build()})});
Expand Down
47 changes: 47 additions & 0 deletions test/tcframe/verifier/VerifierTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class VerifierTests : public Test {
.newSubtask()
.addConstraint([=]{return b5;}, "1 <= E && E <= 10")
.build();
ConstraintSuite constraintSuiteWithConstraintsAndSubtasks = ConstraintSuiteBuilder()
.addConstraint([=]{return b0;}, "1 <= X && X <= 10")
.newSubtask()
.addConstraint([=]{return b1;}, "1 <= A && A <= 10")
.addConstraint([=]{return b2;}, "1 <= B && B <= 10")
.newSubtask()
.addConstraint([=]{return b3;}, "1 <= C && C <= 10")
.addConstraint([=]{return b4;}, "1 <= D && D <= 10")
.build();
ConstraintSuite constraintSuiteWithMultipleTestCasesConstraints = ConstraintSuiteBuilder()
.addConstraint([=]{return b1;}, "1 <= A && A <= 10")
.addConstraint([=]{return b2;}, "1 <= B && B <= 10")
Expand All @@ -37,6 +46,7 @@ class VerifierTests : public Test {

Verifier verifier = Verifier(constraintSuite);
Verifier verifierWithSubtasks = Verifier(constraintSuiteWithSubtasks);
Verifier verifierWithConstraintsAndSubtasks = Verifier(constraintSuiteWithConstraintsAndSubtasks);
Verifier verifierWithMultipleTestCasesConstraints = Verifier(constraintSuiteWithMultipleTestCasesConstraints);

void SetUp() {
Expand Down Expand Up @@ -96,6 +106,43 @@ TEST_F(VerifierTests, Verification_WithSubtasks_Invalid_SomeConstraintsInvalid)
Pair(2, ElementsAre("1 <= D && D <= 10"))));
}

TEST_F(VerifierTests, Verification_WithConstraintsAndSubtasks_Valid_AllConstraintsValid) {
ConstraintsVerificationResult result = verifierWithConstraintsAndSubtasks.verifyConstraints({1, 2});

EXPECT_TRUE(result.isValid());
EXPECT_THAT(result.satisfiedButNotAssignedSubtaskIds(), IsEmpty());
EXPECT_THAT(result.unsatisfiedConstraintDescriptionsBySubtaskId(), IsEmpty());
}

TEST_F(VerifierTests, Verification_WithConstraintsAndSubtasks_Valid_AllAssignedSubtasksValid) {
b1 = false;
ConstraintsVerificationResult result = verifierWithConstraintsAndSubtasks.verifyConstraints({2});

EXPECT_TRUE(result.isValid());
EXPECT_THAT(result.satisfiedButNotAssignedSubtaskIds(), IsEmpty());
EXPECT_THAT(result.unsatisfiedConstraintDescriptionsBySubtaskId(), IsEmpty());
}

TEST_F(VerifierTests, Verification_WithConstraintsAndSubtasks_Invalid_SomeConstraintsValid) {
b4 = false;
ConstraintsVerificationResult result = verifierWithConstraintsAndSubtasks.verifyConstraints({2});

EXPECT_FALSE(result.isValid());
EXPECT_THAT(result.satisfiedButNotAssignedSubtaskIds(), ElementsAre(1));
EXPECT_THAT(result.unsatisfiedConstraintDescriptionsBySubtaskId(), ElementsAre(
Pair(2, ElementsAre("1 <= D && D <= 10"))));
}

TEST_F(VerifierTests, Verification_WithConstraintsAndSubtasks_Invalid_GlobalConstraintsValid) {
b0 = false;
ConstraintsVerificationResult result = verifierWithConstraintsAndSubtasks.verifyConstraints({1, 2});

EXPECT_FALSE(result.isValid());
EXPECT_THAT(result.satisfiedButNotAssignedSubtaskIds(), IsEmpty());
EXPECT_THAT(result.unsatisfiedConstraintDescriptionsBySubtaskId(), ElementsAre(
Pair(-1, ElementsAre("1 <= X && X <= 10"))));
}

TEST_F(VerifierTests, Verification_MultipleTestCases_Valid_AllConstraintsValid) {
MultipleTestCasesConstraintsVerificationResult result =
verifierWithMultipleTestCasesConstraints.verifyMultipleTestCasesConstraints();
Expand Down

0 comments on commit e777fcb

Please sign in to comment.