Skip to content

Commit

Permalink
Escape multiple test cases output prefix for bash
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Nov 13, 2016
1 parent 2f4f978 commit c61fe22
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ete-test/resources/multi-prefix/multi-prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ProblemSpec : public BaseProblemSpec {

void MultipleTestCasesConfig() {
Counter(T);
OutputPrefix("Case #%d: ");
OutputPrefix("Case \"$%d\\\": ");
}

void MultipleTestCasesConstraints() {
Expand Down
2 changes: 1 addition & 1 deletion ete-test/resources/multi-prefix/multi-prefix_solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ int main() {

for (int tc = 0; tc < T; tc++) {
scanf("%d %d", &A, &B);
printf("Case #%d: %d\n", tc+1, A + B);
printf("Case \"$%d\\\": %d\n", tc+1, A + B);
}
}
8 changes: 4 additions & 4 deletions ete-test/tcframe/GenerationEteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ TEST_F(GenerationEteTests, Multi_WithOutputPrefix) {
));

EXPECT_THAT(readFile("ete/multi-prefix/tc/multi-prefix_sample.out"), Eq(
"Case #1: 6\n"
"Case #2: 11\n"));
"Case \"$1\\\": 6\n"
"Case \"$2\\\": 11\n"));

EXPECT_THAT(readFile("ete/multi-prefix/tc/multi-prefix_1.out"), Eq(
"Case #1: 4\n"
"Case #2: 6\n"));
"Case \"$1\\\": 4\n"
"Case \"$2\\\": 6\n"));
}

}
3 changes: 2 additions & 1 deletion include/tcframe/os/UnixOperatingSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class UnixOperatingSystem : public OperatingSystem {
// Replace the prefix for the first tc, with the correct prefix for this tc
string firstPrefix = StringUtils::interpolate(outputPrefix.value(), 1);
string correctPrefix = StringUtils::interpolate(outputPrefix.value(), i);
sout2 << "printf \"" << correctPrefix << "\" >> " << baseOut << " && ";
string escapedCorrectPrefix = StringUtils::escape(correctPrefix, "\\$\"");
sout2 << "printf \"%s\" \"" << escapedCorrectPrefix << "\" >> " << baseOut << " && ";
sout2 << "cut -c " << (firstPrefix.size() + 1) << "- " << out << " >> " << baseOut;
} else {
sout2 << "cat " << out << " >> " << baseOut;
Expand Down
11 changes: 11 additions & 0 deletions include/tcframe/util/StringUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ class StringUtils {
return string(res);
}

static string escape(const string& s, const string& badChars) {
string res;
for (char c : s) {
if (badChars.find_first_of(c) != string::npos) {
res += "\\";
}
res += c;
}
return res;
}

static vector<string> split(const string& s, char delimiter) {
vector<string> result;

Expand Down
4 changes: 4 additions & 0 deletions test/tcframe/util/StringUtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ TEST_F(StringUtilsTests, Interpolate) {
EXPECT_THAT(StringUtils::interpolate("abc %d def", 42), Eq("abc 42 def"));
}

TEST_F(StringUtilsTests, Escape) {
EXPECT_THAT(StringUtils::escape("\"hello$world\"", "\"$"), Eq("\\\"hello\\$world\\\""));
}

TEST_F(StringUtilsTests, SplitAndTrimBySpace) {
EXPECT_THAT(StringUtils::splitAndTrimBySpace(" A B C D "), ElementsAre(
"A", "B", "C", "D"));
Expand Down

0 comments on commit c61fe22

Please sign in to comment.