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. vector<long long> readStreamCase(InStream &in, int testCase, bool &prereadCase) {
  15. if (!prereadCase) {
  16. string caseStr = in.readToken();
  17. if (caseStr != "Case")
  18. in.quitf(_pe, "Expected 'Case' but found '%s' [test case %d]", compress(caseStr).c_str(), testCase);
  19. }
  20. string numExpStr = to_string(testCase) + ":";
  21. string numStr = in.readToken();
  22. if (numExpStr != numStr)
  23. in.quitf(_pe, "Expected '%s' but found '%s' [test case %d]", compress(numExpStr).c_str(),
  24. compress(numStr).c_str(), testCase);
  25. vector<long long> result;
  26. while (!in.seekEof()) {
  27. string dummy;
  28. in.readTokenTo(dummy);
  29. string token = in.readToken();
  30. if (token == "Case") {
  31. prereadCase = true;
  32. break;
  33. }
  34. result.push_back(stringToLongLong(in, token.c_str()));
  35. }
  36. return result;
  37. }
  38. string longLongsToString(const vector<long long> &a) {
  39. if (a.empty())
  40. return "\"\" [size=0]";
  41. string elems;
  42. if (a.size() <= 5) {
  43. for (auto elem: a)
  44. elems += vtos(elem) + " ";
  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(),
  68. testCase);
  69. }
  70. }
  71. quitf(_ok, "%d test cases(s)", testCase);
  72. }