casewcmp.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Checker to compare output and answer in the form:
  3. *
  4. * Case 1: <token> <token> ... <token>
  5. * Case 2: <token> <token> ... <token>
  6. * ...
  7. * Case n: <token> <token> ... <token>
  8. *
  9. * Tokens in each case should not contain "Case"
  10. */
  11. #include "testlib.h"
  12. #include <vector>
  13. #include <string>
  14. using namespace std;
  15. vector<string> readStreamCase(InStream &in, int testCase, bool &prereadCase) {
  16. if (!prereadCase) {
  17. string caseStr = in.readToken();
  18. if (caseStr != "Case")
  19. in.quitf(_pe, "Expected 'Case' but found '%s' [test case %d]", compress(caseStr).c_str(), testCase);
  20. }
  21. string numExpStr = to_string(testCase) + ":";
  22. string numStr = in.readToken();
  23. if (numExpStr != numStr)
  24. in.quitf(_pe, "Expected '%s' but found '%s' [test case %d]", compress(numExpStr).c_str(),
  25. compress(numStr).c_str(), testCase);
  26. vector<string> result;
  27. while (!in.seekEof()) {
  28. string token = in.readToken();
  29. if (token == "Case") {
  30. prereadCase = true;
  31. break;
  32. }
  33. result.push_back(token);
  34. }
  35. return result;
  36. }
  37. string stringsToString(const vector<string> &a) {
  38. if (a.empty())
  39. return "\"\" [size=0]";
  40. string elems;
  41. for (const auto& token: a)
  42. elems += token + " ";
  43. return format("\"%s\" [size=%u]", compress(trim(elems)).c_str(), (unsigned int) (a.size()));
  44. }
  45. int main(int argc, char *argv[]) {
  46. setName("Tokens checker with testcase-support");
  47. registerTestlibCmd(argc, argv);
  48. int testCase = 0;
  49. bool ansPrereadCase = false;
  50. bool oufPrereadCase = false;
  51. while (!ans.seekEof()) {
  52. testCase++;
  53. vector<string> ja = readStreamCase(ans, testCase, ansPrereadCase);
  54. vector<string> pa = readStreamCase(ouf, testCase, oufPrereadCase);
  55. if (ja != pa) {
  56. string js = stringsToString(ja);
  57. string ps = stringsToString(pa);
  58. quitf(_wa, "Sequences differ: jury has %s, but participant has %s [test case %d]", js.c_str(), ps.c_str(),
  59. testCase);
  60. }
  61. }
  62. quitf(_ok, "%d test cases(s)", testCase);
  63. }