1
0

casewcmp.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. */
  10. #include "testlib.h"
  11. #include <iostream>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <iomanip>
  15. #include <string>
  16. #include <cstdlib>
  17. #include <cstdio>
  18. #include <cstring>
  19. #include <cmath>
  20. #include <ctime>
  21. #include <climits>
  22. #include <cassert>
  23. #include <vector>
  24. #include <queue>
  25. #include <stack>
  26. #include <deque>
  27. #include <set>
  28. #include <map>
  29. #include <bitset>
  30. #include <utility>
  31. #include <algorithm>
  32. using namespace std;
  33. #define forn(i, n) for (int i = 0; i < int(n); i++)
  34. string token;
  35. vector<string> readStreamCase(InStream& in, TResult pe, int testCase, bool& prereadCase)
  36. {
  37. if (!prereadCase)
  38. {
  39. string caseStr = in.readToken();
  40. if (caseStr != "Case")
  41. quitf(pe, "Expected 'Case' but found '%s' [test case %d]", compress(caseStr).c_str(), testCase);
  42. }
  43. string numExpStr;
  44. stringstream ss;
  45. ss << testCase;
  46. ss >> numExpStr;
  47. numExpStr += ":";
  48. string numStr = in.readToken();
  49. if (numExpStr != numStr)
  50. quitf(pe, "Expected '%s' but found '%s' [test case %d]", compress(numExpStr).c_str(), compress(numStr).c_str(), testCase);
  51. vector<string> result;
  52. while (!in.seekEof())
  53. {
  54. in.readTokenTo(token);
  55. if (token == "Case")
  56. {
  57. prereadCase = true;
  58. break;
  59. }
  60. result.push_back(token);
  61. }
  62. return result;
  63. }
  64. string stringsToString(const vector<string>& a)
  65. {
  66. if (a.empty())
  67. return "\"\" [size=0]";
  68. string elems;
  69. forn(i, a.size())
  70. elems += a[i] + " ";
  71. return format("\"%s\" [size=%u]", compress(trim(elems)).c_str(), (unsigned int)(a.size()));
  72. }
  73. int main(int argc, char* argv[])
  74. {
  75. setName("Tokens checker with testcase-support");
  76. registerTestlibCmd(argc, argv);
  77. int testCase = 0;
  78. bool ansPrereadCase = false;
  79. bool oufPrereadCase = false;
  80. while (!ans.seekEof())
  81. {
  82. testCase++;
  83. vector<string> ja = readStreamCase(ans, _fail, testCase, ansPrereadCase);
  84. vector<string> pa = readStreamCase(ouf, _pe, testCase, oufPrereadCase);
  85. if (ja != pa)
  86. {
  87. string js = stringsToString(ja);
  88. string ps = stringsToString(pa);
  89. quitf(_wa, "Sequences differ: jury has %s, but participant has %s [test case %d]", js.c_str(), ps.c_str(), testCase);
  90. }
  91. }
  92. quitf(_ok, "%d test cases(s)", testCase);
  93. }