Skip to content

Commit

Permalink
Refactor: include GenerationException and execution errors as Formatt…
Browse files Browse the repository at this point in the history
…edError
  • Loading branch information
fushar committed Sep 17, 2017
1 parent 77825cb commit 6531ba7
Show file tree
Hide file tree
Showing 33 changed files with 222 additions and 206 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ set(INCLUDE
include/tcframe/driver/SlugParser.hpp
include/tcframe/driver/SpecDriver.hpp
include/tcframe/driver/TestCaseDriver.hpp
include/tcframe/exception.hpp
include/tcframe/exception/FormattedError.hpp
include/tcframe/exception/NotImplementedException.hpp
include/tcframe/runner.hpp
include/tcframe/runner/aggregator.hpp
include/tcframe/runner/aggregator/Aggregator.hpp
Expand Down Expand Up @@ -88,7 +91,6 @@ set(INCLUDE
include/tcframe/runner/grader/TestCaseGrader.hpp
include/tcframe/runner/generator.hpp
include/tcframe/runner/generator/DefaultGeneratorLogger.hpp
include/tcframe/runner/generator/GenerationException.hpp
include/tcframe/runner/generator/GenerationOptions.hpp
include/tcframe/runner/generator/Generator.hpp
include/tcframe/runner/generator/GeneratorLogger.hpp
Expand Down Expand Up @@ -123,8 +125,6 @@ set(INCLUDE
include/tcframe/spec/core/Magic.hpp
include/tcframe/spec/core/SeedSetter.hpp
include/tcframe/spec/core/SpecYaml.hpp
include/tcframe/spec/exception.hpp
include/tcframe/spec/exception/FormattedError.hpp
include/tcframe/spec/io.hpp
include/tcframe/spec/io/GridIOSegment.hpp
include/tcframe/spec/io/GridIOSegmentManipulator.hpp
Expand Down Expand Up @@ -160,7 +160,6 @@ set(INCLUDE
include/tcframe/spec/verifier/MultipleTestCasesConstraintsVerificationResult.hpp
include/tcframe/spec/verifier/Verifier.hpp
include/tcframe/util.hpp
include/tcframe/util/NotImplementedException.hpp
include/tcframe/util/StringUtils.hpp
include/tcframe/util/optional.hpp
)
Expand All @@ -173,6 +172,7 @@ set(TEST_UNIT
test/unit/tcframe/driver/SpecDriverTests.cpp
test/unit/tcframe/driver/SlugParserTests.cpp
test/unit/tcframe/driver/TestCaseDriverTests.cpp
test/unit/tcframe/exception/FormattedErrorTests.cpp
test/unit/tcframe/helper.hpp
test/unit/tcframe/mock.hpp
test/unit/tcframe/runner/aggregator/MinAggregatorTests.cpp
Expand Down Expand Up @@ -213,6 +213,7 @@ set(TEST_UNIT
test/unit/tcframe/runner/logger/MockLoggerEngine.hpp
test/unit/tcframe/runner/logger/RunnerLoggerTests.cpp
test/unit/tcframe/runner/logger/SimpleLoggerEngineTests.cpp
test/unit/tcframe/runner/os/ExecutionResultTests.cpp
test/unit/tcframe/runner/os/MockOperatingSystem.hpp
test/unit/tcframe/runner/os/TestCasePathCreatorTests.cpp
test/unit/tcframe/runner/verdict/MockVerdictCreator.hpp
Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/driver/TestCaseDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <utility>

#include "RawIOManipulator.hpp"
#include "tcframe/exception.hpp"
#include "tcframe/spec/config.hpp"
#include "tcframe/spec/exception.hpp"
#include "tcframe/spec/io.hpp"
#include "tcframe/spec/testcase.hpp"
#include "tcframe/spec/verifier.hpp"
Expand Down
4 changes: 4 additions & 0 deletions include/tcframe/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include "tcframe/exception/FormattedError.hpp"
#include "tcframe/exception/NotImplementedException.hpp"
44 changes: 44 additions & 0 deletions include/tcframe/exception/FormattedError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

using std::move;
using std::pair;
using std::runtime_error;
using std::string;
using std::vector;

namespace tcframe {

struct FormattedError : public runtime_error {
private:
vector<pair<int, string>> messages_;

public:
~FormattedError() noexcept = default;

explicit FormattedError(vector<pair<int, string>> messages)
: runtime_error("")
, messages_(move(messages)) {}

const vector<pair<int, string>>& messages() const {
return messages_;
}

bool operator==(const FormattedError& o) const {
return messages_ == o.messages_;
}

static FormattedError combine(const vector<FormattedError>& errors) {
vector<pair<int, string>> combinedMessages;
for (const FormattedError& error : errors) {
combinedMessages.insert(combinedMessages.end(), error.messages().begin(), error.messages().end());
}
return FormattedError(combinedMessages);
}
};

}
1 change: 0 additions & 1 deletion include/tcframe/runner/generator.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "tcframe/runner/generator/DefaultGeneratorLogger.hpp"
#include "tcframe/runner/generator/GenerationException.hpp"
#include "tcframe/runner/generator/GenerationOptions.hpp"
#include "tcframe/runner/generator/Generator.hpp"
#include "tcframe/runner/generator/GeneratorLogger.hpp"
Expand Down
15 changes: 0 additions & 15 deletions include/tcframe/runner/generator/DefaultGeneratorLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "GeneratorLogger.hpp"
#include "tcframe/runner/logger.hpp"
#include "tcframe/spec/constraint.hpp"
#include "tcframe/spec/exception.hpp"
#include "tcframe/spec/verifier.hpp"
#include "tcframe/util.hpp"

Expand Down Expand Up @@ -63,20 +62,6 @@ class DefaultGeneratorLogger : public GeneratorLogger, public DefaultBaseLogger
engine_->logParagraph(0, "FAILED");
engine_->logParagraph(2, "Reasons:");
}

void logSimpleError(const runtime_error& e) {
engine_->logListItem1(2, e.what());
}

void logFormattedError(const FormattedError& e) {
for (auto p : e.messages()) {
if (p.first == 0) {
engine_->logListItem1(2, p.second);
} else {
engine_->logListItem2(3, p.second);
}
}
}
};

}
28 changes: 0 additions & 28 deletions include/tcframe/runner/generator/GenerationException.hpp

This file was deleted.

14 changes: 3 additions & 11 deletions include/tcframe/runner/generator/Generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include "GenerationOptions.hpp"
#include "GeneratorLogger.hpp"
#include "TestCaseGenerator.hpp"
#include "tcframe/runner/os.hpp"
#include "tcframe/exception.hpp"
#include "tcframe/runner/client.hpp"
#include "tcframe/runner/os.hpp"
#include "tcframe/spec/core.hpp"
#include "tcframe/spec/exception.hpp"
#include "tcframe/util.hpp"

using std::ostringstream;
Expand Down Expand Up @@ -90,17 +90,9 @@ class Generator {
try {
verify(testGroup);
combine(testGroup, options, multipleTestCasesOutputPrefix);
} catch (GenerationException& e) {
logger_->logMultipleTestCasesCombinationFailedResult();
e.callback()();
return false;
} catch (FormattedError& e) {
logger_->logMultipleTestCasesCombinationFailedResult();
logger_->logFormattedError(e);
return false;
} catch (runtime_error& e) {
logger_->logMultipleTestCasesCombinationFailedResult();
logger_->logSimpleError(e);
logger_->logError(&e);
return false;
}

Expand Down
3 changes: 0 additions & 3 deletions include/tcframe/runner/generator/GeneratorLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "tcframe/runner/logger.hpp"
#include "tcframe/spec/constraint.hpp"
#include "tcframe/spec/exception.hpp"
#include "tcframe/spec/verifier.hpp"
#include "tcframe/util.hpp"

Expand All @@ -29,8 +28,6 @@ class GeneratorLogger : public virtual BaseLogger {
virtual void logMultipleTestCasesCombinationIntroduction(const string& testGroupName) = 0;
virtual void logMultipleTestCasesCombinationSuccessfulResult() = 0;
virtual void logMultipleTestCasesCombinationFailedResult() = 0;
virtual void logSimpleError(const runtime_error& e) = 0;
virtual void logFormattedError(const FormattedError& e) = 0;
};

}
24 changes: 7 additions & 17 deletions include/tcframe/runner/generator/TestCaseGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <stdexcept>
#include <string>

#include "GenerationException.hpp"
#include "GenerationOptions.hpp"
#include "GeneratorLogger.hpp"
#include "tcframe/exception.hpp"
#include "tcframe/runner/client.hpp"
#include "tcframe/runner/evaluator.hpp"
#include "tcframe/runner/verdict.hpp"
Expand Down Expand Up @@ -43,17 +43,9 @@ class TestCaseGenerator {
generateInput(testCase, inputFilename);
generateOutput(testCase, inputFilename, outputFilename, options);
validateOutput(testCase, inputFilename, outputFilename, options);
} catch (GenerationException& e) {
logger_->logTestCaseFailedResult(testCase.description());
e.callback()();
return false;
} catch (FormattedError& e) {
logger_->logTestCaseFailedResult(testCase.description());
logger_->logFormattedError(e);
return false;
} catch (runtime_error& e) {
logger_->logTestCaseFailedResult(testCase.description());
logger_->logSimpleError(e);
logger_->logError(&e);
return false;
}

Expand Down Expand Up @@ -82,9 +74,7 @@ class TestCaseGenerator {

GenerationResult generationResult = evaluator_->generate(inputFilename, outputFilename, evaluationOptions);
if (!generationResult.executionResult().isSuccessful()) {
throw GenerationException([=] {
logger_->logExecutionResults({{"solution", generationResult.executionResult()}});
});
throw ExecutionResults::asFormattedError({{"solution", generationResult.executionResult()}});
}
}

Expand Down Expand Up @@ -123,10 +113,10 @@ class TestCaseGenerator {

ScoringResult scoringResult = evaluator_->score(inputFilename, outputFilename);
if (!(scoringResult.verdict().status() == VerdictStatus::ac())) {
throw GenerationException([=] {
logger_->logSimpleError(runtime_error(
"Sample test case output does not match with actual output produced by the solution"));
logger_->logExecutionResults({{"scorer", scoringResult.executionResult()}});
throw FormattedError::combine({
FormattedError(
{{0, "Sample test case output does not match with actual output produced by the solution"}}),
ExecutionResults::asFormattedError({{"scorer", scoringResult.executionResult()}})
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/runner/grader/BriefGraderLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BriefGraderLogger : public GraderLogger {

void logTestGroupIntroduction(int) {}
void logTestCaseIntroduction(const string&) {}
void logExecutionResults(const map<string, ExecutionResult>&) {}
void logError(runtime_error*) {}
void logIntroduction(const string&) {}
void logTestCaseVerdict(const Verdict&) {}

Expand Down
5 changes: 3 additions & 2 deletions include/tcframe/runner/grader/TestCaseGrader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include "GradingOptions.hpp"
#include "GraderLogger.hpp"
#include "tcframe/exception.hpp"
#include "tcframe/runner/evaluator.hpp"
#include "tcframe/runner/os.hpp"
#include "tcframe/runner/verdict.hpp"
#include "tcframe/util.hpp"

using std::string;

Expand All @@ -33,7 +33,8 @@ class TestCaseGrader {

logger_->logTestCaseVerdict(verdict);
if (!(verdict.status() == VerdictStatus::tle())) {
logger_->logExecutionResults(evaluationResult.executionResults());
FormattedError error = ExecutionResults::asFormattedError(evaluationResult.executionResults());
logger_->logError(&error);
}

return verdict;
Expand Down
4 changes: 3 additions & 1 deletion include/tcframe/runner/logger/BaseLogger.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include <map>
#include <stdexcept>
#include <string>

#include "tcframe/runner/os.hpp"

using std::map;
using std::runtime_error;
using std::string;

namespace tcframe {
Expand All @@ -16,7 +18,7 @@ class BaseLogger {

virtual void logTestGroupIntroduction(int testGroupId) = 0;
virtual void logTestCaseIntroduction(const string& testCaseName) = 0;
virtual void logExecutionResults(const map<string, ExecutionResult>& executionResults) = 0;
virtual void logError(runtime_error* e) = 0;
};

}
37 changes: 21 additions & 16 deletions include/tcframe/runner/logger/DefaultBaseLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include <map>
#include <string>
#include <sys/signal.h>

#include "BaseLogger.hpp"
#include "LoggerEngine.hpp"
#include "tcframe/exception.hpp"
#include "tcframe/runner/os.hpp"
#include "tcframe/spec/testcase.hpp"
#include "tcframe/util.hpp"
Expand Down Expand Up @@ -39,21 +39,26 @@ class DefaultBaseLogger : public virtual BaseLogger {
engine_->logHangingParagraph(1, testCaseName + ": ");
}

virtual void logExecutionResults(const map<string, ExecutionResult>& executionResults) {
for (const auto& entry : executionResults) {
const string& key = entry.first;
const ExecutionResult& executionResult = entry.second;

if (!executionResult.isSuccessful()) {
engine_->logListItem1(2, "Execution of " + key + " failed:");
if (executionResult.exitCode()) {
engine_->logListItem2(3, "Exit code: " + StringUtils::toString(executionResult.exitCode().value()));
engine_->logListItem2(3, "Standard error: " + executionResult.standardError());
} else {
engine_->logListItem2(3, "Exit signal: " + string(strsignal(executionResult.exitSignal().value())));
}
} else if (!executionResult.standardError().empty()) {
engine_->logListItem1(2, key + ": " + executionResult.standardError());
virtual void logError(runtime_error* e) {
auto formattedE = dynamic_cast<FormattedError*>(e);
if (formattedE != nullptr) {
logFormattedError(*formattedE);
} else {
logSimpleError(*e);
}
}

private:
void logSimpleError(const runtime_error& e) {
engine_->logListItem1(2, e.what());
}

void logFormattedError(const FormattedError& e) {
for (const auto& entry : e.messages()) {
if (entry.first == 0) {
engine_->logListItem1(2, entry.second);
} else {
engine_->logListItem2(3, entry.second);
}
}
}
Expand Down
Loading

0 comments on commit 6531ba7

Please sign in to comment.