casencmp.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <vector>
  12. #include <string>
  13. using namespace std;
  14. string token;
  15. vector<long long> 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<long long> result;
  26. while (!in.seekEof()) {
  27. in.readTokenTo(token);
  28. string token = in.readToken();
  29. if (token == "Case") {
  30. prereadCase = true;
  31. break;
  32. }
  33. result.push_back(stringToLongLong(in, token.c_str()));
  34. }
  35. return result;
  36. }
  37. string longLongsToString(const vector<long long>& a) {
  38. if (a.empty())
  39. return "\"\" [size=0]";
  40. string elems;
  41. if (a.size() <= 5) {
  42. for (auto elem : a)
  43. elems += vtos(elem) + " ";
  44. }
  45. else {
  46. for (int i = 0; i < 3; i++)
  47. elems += vtos(a[i]) + " ";
  48. elems += "... ";
  49. for (int i = 0; i < 2; i++)
  50. elems += vtos(a[a.size() - 2 + i]) + " ";
  51. }
  52. return format("\"%s\" [size=%u]", trim(elems).c_str(), (unsigned int)(a.size()));
  53. }
  54. int main(int argc, char* argv[]) {
  55. setName("Many int64s checker with testcase-support");
  56. registerTestlibCmd(argc, argv);
  57. int testCase = 0;
  58. bool ansPrereadCase = false;
  59. bool oufPrereadCase = false;
  60. while (!ans.seekEof()) {
  61. testCase++;
  62. vector<long long> ja = readStreamCase(ans, testCase, ansPrereadCase);
  63. vector<long long> pa = readStreamCase(ouf, testCase, oufPrereadCase);
  64. if (ja != pa) {
  65. string js = longLongsToString(ja);
  66. string ps = longLongsToString(pa);
  67. quitf(_wa, "Sequences differ: jury has %s, but participant has %s [test case %d]", js.c_str(), ps.c_str(), testCase);
  68. }
  69. }
  70. quitf(_ok, "%d test cases(s)", testCase);
  71. }