1
0

casewcmp.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(), compress(numStr).c_str(), testCase);
  25. vector<string> result;
  26. while (!in.seekEof()) {
  27. string token = in.readToken();
  28. if (token == "Case") {
  29. prereadCase = true;
  30. break;
  31. }
  32. result.push_back(token);
  33. }
  34. return result;
  35. }
  36. string stringsToString(const vector<string>& a) {
  37. if (a.empty())
  38. return "\"\" [size=0]";
  39. string elems;
  40. for (auto token : a)
  41. elems += token + " ";
  42. return format("\"%s\" [size=%u]", compress(trim(elems)).c_str(), (unsigned int)(a.size()));
  43. }
  44. int main(int argc, char* argv[]) {
  45. setName("Tokens checker with testcase-support");
  46. registerTestlibCmd(argc, argv);
  47. int testCase = 0;
  48. bool ansPrereadCase = false;
  49. bool oufPrereadCase = false;
  50. while (!ans.seekEof()) {
  51. testCase++;
  52. vector<string> ja = readStreamCase(ans, testCase, ansPrereadCase);
  53. vector<string> pa = readStreamCase(ouf, testCase, oufPrereadCase);
  54. if (ja != pa) {
  55. string js = stringsToString(ja);
  56. string ps = stringsToString(pa);
  57. quitf(_wa, "Sequences differ: jury has %s, but participant has %s [test case %d]", js.c_str(), ps.c_str(), testCase);
  58. }
  59. }
  60. quitf(_ok, "%d test cases(s)", testCase);
  61. }