Skip to content

Instantly share code, notes, and snippets.

@EndlessCheng
Last active August 29, 2020 03:23
Show Gist options
  • Save EndlessCheng/e882fbc2569aed5ccfcfbde83c7cad65 to your computer and use it in GitHub Desktop.
Save EndlessCheng/e882fbc2569aed5ccfcfbde83c7cad65 to your computer and use it in GitHub Desktop.
C++ mock test for competitive programming
#include <bits/stdc++.h>
using namespace std;
void run(istream &in, ostream &out) {
// write your code here
int x;
while (in >> x) {
out << x * 2 << '\n';
}
}
void trimR(string &s) { s.erase(find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(), s.end()); }
int main() {
// for CLion, add one line `add_definitions(-DMOCK)` in CMakeLists.txt
#ifdef MOCK
vector<vector<string>> examples = {
// add example inputs and outputs, or your custom test data
{
R"(2 3)", // input
R"(4
6)", // output
},
{
R"(-5 -6)", // input
R"(-10
-12)", // output
},
};
bool passedAll = true;
for (int i = 0; i < examples.size(); i++) {
string rawIn = examples[i][0], expectedOut = examples[i][1];
trimR(expectedOut);
istringstream mockIn(rawIn);
ostringstream mockOut;
run(mockIn, mockOut);
string actualOut = mockOut.str();
trimR(actualOut);
if (expectedOut != actualOut) {
passedAll = false;
cout << "WA " << i + 1 << endl;
cout << "Input:" << endl << rawIn << endl;
cout << "Expected:" << endl << expectedOut << endl;
cout << "Actual:" << endl << actualOut << endl << endl;
}
}
if (passedAll) {
cout << "OK! SUBMIT" << endl;
}
#else
ios::sync_with_stdio(false), cin.tie(nullptr);
run(cin, cout);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment