casencmp.cpp 2.5 KB

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