Skip to content

Commit

Permalink
Merge pull request #16 from ReCodEx/judge-improvements
Browse files Browse the repository at this point in the history
Adding new judge feature -- remove trailing whitespace.
  • Loading branch information
Martin Kruliš committed Jun 14, 2018
2 parents 135991c + 3b9b0c2 commit 3569305
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
4 changes: 2 additions & 2 deletions judges/recodex_token_judge/judge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ template <class READER, class LINE_COMPARATOR> class Judge
*/
void logImpairedCorrectLine(const line_t &line) const
{
bpp::log().error() << "-" << line.lineNumber() << ": " << line.getRawLineAsString();
bpp::log().error() << "-" << line.lineNumber() << ": " << line.getRawLineAsString() << "\n";
}


Expand All @@ -283,7 +283,7 @@ template <class READER, class LINE_COMPARATOR> class Judge
*/
void logImpairedResultLine(const line_t &line) const
{
bpp::log().error() << "+" << line.lineNumber() << ": " << line.getRawLineAsString();
bpp::log().error() << "+" << line.lineNumber() << ": " << line.getRawLineAsString() << "\n";
}


Expand Down
19 changes: 15 additions & 4 deletions judges/recodex_token_judge/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ template <typename CHAR = char, typename OFFSET = std::uint32_t> class Reader
bool mIgnoreEmptyLines; ///< Empty lines are skipped completely.
bool mAllowComments; ///< Allow comments (lines starting with '#'), which are completely skipped.
bool mIgnoreLineEnds; ///< Treat end lines as regular whitespace.
bool mIgnoreTrailingWhitespace; ///< All whitespace (empty lines) at the end of the file is ignored

char_t *mData; ///< Mmaped data of the file.
offset_t mOffset; ///< Offset from the beginning of the file (currently processed).
Expand Down Expand Up @@ -248,9 +249,9 @@ template <typename CHAR = char, typename OFFSET = std::uint32_t> class Reader
}

public:
Reader(bool ignoreEmptyLines, bool allowComments, bool ignoreLineEnds)
Reader(bool ignoreEmptyLines, bool allowComments, bool ignoreLineEnds, bool ignoreTrailingWhitespace)
: mIgnoreEmptyLines(ignoreEmptyLines), mAllowComments(allowComments), mIgnoreLineEnds(ignoreLineEnds),
mData(nullptr), mOffset(0), mLength(0)
mIgnoreTrailingWhitespace(ignoreTrailingWhitespace), mData(nullptr), mOffset(0), mLength(0)
{
}

Expand All @@ -275,6 +276,13 @@ template <typename CHAR = char, typename OFFSET = std::uint32_t> class Reader
mLength = mFile.length() / sizeof(char_t);
mLineNumber = 1;
mLineOffset = 0;

if (mIgnoreTrailingWhitespace) {
// Reduce the file length to ignore all whitespace at the end ...
while (mLength > 0 && std::isspace(mData[mLength - 1])) {
--mLength;
}
}
}


Expand Down Expand Up @@ -313,7 +321,9 @@ template <typename CHAR = char, typename OFFSET = std::uint32_t> class Reader
*/
std::unique_ptr<Line> readLine()
{
if (eof()) { return std::unique_ptr<Line>(); }
if (eof()) {
return std::unique_ptr<Line>();
}

offset_t startOffset = mOffset;
auto line = bpp::make_unique<Line>(*this, mLineNumber, mData + mOffset);
Expand Down Expand Up @@ -348,7 +358,8 @@ template <typename CHAR = char, typename OFFSET = std::uint32_t> class Reader
return std::unique_ptr<Line>();
}

line->mRawLength = mOffset - startOffset;
line->mRawLength =
line->mTokens.size() > 0 ? line->mTokens.back().charNumber() + line->mTokens.back().length() : 0;
return line;
}
};
Expand Down
11 changes: 8 additions & 3 deletions judges/recodex_token_judge/recodex-token-judge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ int main(int argc, char *argv[])
"allow-comments", "Lines starting with '#' are ignored completely."));
args.registerArg(bpp::make_unique<bpp::ProgramArguments::ArgBool>(
"ignore-line-ends", "New lines characters are treated as regular whitespace."));
args.getArg("ignore-empty-lines").conflictsWith("ignore-line-ends");
args.registerArg(bpp::make_unique<bpp::ProgramArguments::ArgBool>("ignore-trailing-whitespace",
"Any whitespace (i.e., empty lines or comments if allowed) at the end of files is ignored."));
args.getArg("ignore-empty-lines").conflictsWith("ignore-line-ends").conflictsWith("ignore-trailing-whitespace");
args.getArg("ignore-line-ends").conflictsWith("ignore-trailing-whitespace");

// Token comparator args
args.registerArg(bpp::make_unique<bpp::ProgramArguments::ArgBool>(
Expand Down Expand Up @@ -73,10 +76,12 @@ int main(int argc, char *argv[])
// Open data readers ...
Reader<> correctReader(args.getArgBool("ignore-empty-lines").getValue(),
args.getArgBool("allow-comments").getValue(),
args.getArgBool("ignore-line-ends").getValue());
args.getArgBool("ignore-line-ends").getValue(),
args.getArgBool("ignore-trailing-whitespace").getValue());
Reader<> resultReader(args.getArgBool("ignore-empty-lines").getValue(),
args.getArgBool("allow-comments").getValue(),
args.getArgBool("ignore-line-ends").getValue());
args.getArgBool("ignore-line-ends").getValue(),
args.getArgBool("ignore-trailing-whitespace").getValue());

correctReader.open(args[0]);
resultReader.open(args[1]);
Expand Down
19 changes: 12 additions & 7 deletions judges/recodex_token_judge/tests/10.error.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
0
-1: first row 42
+2: first
3: unexpected 'row'
+4: 42 pzr
-4: kzr pzr # this is the end
+5: kzr
0
-1: first row 42

+2: first

3: unexpected 'row'
+4: 42 pzr

-4: kzr pzr

+5: kzr

15 changes: 15 additions & 0 deletions judges/recodex_token_judge/tests/12-trailing-whitespace.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bats

load bats-shared

@test "ignore trailing whitespace" {
run $EXE_FILE --ignore-trailing-whitespace $CORRECT_FILE $RESULT_FILE
[ "$status" -eq 0 ]
[ "${lines[0]}" -eq 1 ]
}

@test "ignore trailing whitespace (negative test)" {
run $EXE_FILE $CORRECT_FILE $RESULT_FILE
[ "$status" -eq 1 ]
echo "$output" | diff -abB - $ERROR_FILE
}
1 change: 1 addition & 0 deletions judges/recodex_token_judge/tests/12.correct.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9 5 4 1 2 3 6 7 8 10
4 changes: 4 additions & 0 deletions judges/recodex_token_judge/tests/12.error.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0
+2:
+3:
+4:
4 changes: 4 additions & 0 deletions judges/recodex_token_judge/tests/12.result.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
9 5 4 1 2 3 6 7 8 10



0 comments on commit 3569305

Please sign in to comment.