caseicmp.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Checker to compare output and answer in the form:
  3. *
  4. * Case 1: <number>
  5. * Case 2: <number>
  6. * ...
  7. * Case n: <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. vector<long long> readStream(InStream& in, TResult pe)
  35. {
  36. vector<long long> result;
  37. for (int testCase = 1; !in.seekEof(); testCase++)
  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. string numExpStr;
  43. stringstream ss;
  44. ss << testCase;
  45. ss >> numExpStr;
  46. numExpStr += ":";
  47. string numStr = in.readToken();
  48. if (numExpStr != numStr)
  49. quitf(pe, "Expected '%s' but found '%s' [test case %d]", compress(numExpStr).c_str(), compress(numStr).c_str(), testCase);
  50. result.push_back(in.readLong());
  51. }
  52. return result;
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. setName("Single int64 checker with testcase-support");
  57. registerTestlibCmd(argc, argv);
  58. vector<long long> ja = readStream(ans, _fail);
  59. vector<long long> pa = readStream(ouf, _pe);
  60. forn(i, min(ja.size(), pa.size()))
  61. if (ja[i] != pa[i])
  62. quitf(_wa, "Expected %s found %s [test case %d]", vtos(ja[i]).c_str(), vtos(pa[i]).c_str(), i + 1);
  63. if (ja.size() != pa.size())
  64. quitf(_pe, "Expected %d test case(s) but found %d", ja.size(), pa.size());
  65. string message = format("%d case(s):", ja.size());
  66. if (ja.size() <= 5)
  67. {
  68. forn(i, ja.size())
  69. message += " " + vtos(ja[i]);
  70. }
  71. else
  72. {
  73. forn(i, 3)
  74. message += " " + vtos(ja[i]);
  75. message += " ...";
  76. forn(i, 2)
  77. message += " " + vtos(ja[ja.size() - 2 + i]);
  78. }
  79. quitf(_ok, "%s", message.c_str());
  80. }