casencmp.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Checker to compare output and answer in the form:
  3. *
  4. * Case 1: <number> <number> <number> ... <number>
  5. * Case 2: <number> <number> <number> ... <number>
  6. * ...
  7. * Case n: <number> <number> <number> ... <number>
  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<long long> 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<long long> 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(stringToLongLong(in, token.c_str()));
  61. }
  62. return result;
  63. }
  64. string longLongsToString(const vector<long long>& a)
  65. {
  66. if (a.empty())
  67. return "\"\" [size=0]";
  68. string elems;
  69. if (a.size() <= 5)
  70. {
  71. forn(i, a.size())
  72. elems += vtos(a[i]) + " ";
  73. }
  74. else
  75. {
  76. forn(i, 3)
  77. elems += vtos(a[i]) + " ";
  78. elems += "... ";
  79. forn(i, 2)
  80. elems += vtos(a[a.size() - 2 + i]) + " ";
  81. }
  82. return format("\"%s\" [size=%u]", trim(elems).c_str(), (unsigned int)(a.size()));
  83. }
  84. int main(int argc, char* argv[])
  85. {
  86. setName("Many int64s checker with testcase-support");
  87. registerTestlibCmd(argc, argv);
  88. int testCase = 0;
  89. bool ansPrereadCase = false;
  90. bool oufPrereadCase = false;
  91. while (!ans.seekEof())
  92. {
  93. testCase++;
  94. vector<long long> ja = readStreamCase(ans, _fail, testCase, ansPrereadCase);
  95. vector<long long> pa = readStreamCase(ouf, _pe, testCase, oufPrereadCase);
  96. if (ja != pa)
  97. {
  98. string js = longLongsToString(ja);
  99. string ps = longLongsToString(pa);
  100. quitf(_wa, "Sequences differ: jury has %s, but participant has %s [test case %d]", js.c_str(), ps.c_str(), testCase);
  101. }
  102. }
  103. quitf(_ok, "%d test cases(s)", testCase);
  104. }