read.me 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Testlib
  2. ## Intro
  3. This project contains a C++ implementation of testlib. It is already being used in many programming contests in Russia, such as the Russian National Olympiad in Informatics and different stages of ICPC. Join!
  4. The library's C++ code is tested for compatibility with standard C++11 and higher on different versions of `g++`, `clang++`, and Microsoft Visual C++.
  5. This code has been used many times in Codeforces contests.
  6. ## Samples
  7. ### Checker
  8. This sample checker expects the same integer in the output and the answer. It ignores all white-spaces. See more examples in the package.
  9. ```c++
  10. #include "testlib.h"
  11. int main(int argc, char * argv[]) {
  12. setName("compares two signed integers");
  13. registerTestlibCmd(argc, argv);
  14. int ja = ans.readInt();
  15. int pa = ouf.readInt();
  16. if (ja != pa)
  17. quitf(_wa, "expected %d, found %d", ja, pa);
  18. quitf(_ok, "answer is %d", ja);
  19. }
  20. ```
  21. ### Interactor
  22. This sample interactor reads pairs of numbers from the input file, sends them to another program, reads
  23. the result, and writes it to an output file (to be verified later). Another option could be to terminate
  24. the interactor with `quitf(_wa, <comment>)`.
  25. ```c++
  26. #include "testlib.h"
  27. #include <iostream>
  28. using namespace std;
  29. int main(int argc, char* argv[]) {
  30. setName("Interactor A+B");
  31. registerInteraction(argc, argv);
  32. // reads number of queries from test (input) file
  33. int n = inf.readInt();
  34. for (int i = 0; i < n; i++) {
  35. // reads query from test (input) file
  36. int a = inf.readInt();
  37. int b = inf.readInt();
  38. // writes query to the solution, endl makes flush
  39. cout << a << " " << b << endl;
  40. // writes output file to be verified by checker later
  41. tout << ouf.readInt() << endl;
  42. }
  43. // just message
  44. quitf(_ok, "%d queries processed", n);
  45. }
  46. ```
  47. ### Validator
  48. This code reads input from the standard input and checks that it contains only one integer between 1 and 100, inclusive. It also validates that the file ends with EOLN and EOF. On Windows, it expects #13#10 as EOLN, and it expects #10 as EOLN on other platforms. It does not ignore white-spaces, so it works very strictly. It will return a non-zero code in the case of illegal input and write a message to the standard output. See more examples in the package.
  49. ```c++
  50. #include "testlib.h"
  51. int main(int argc, char* argv[]) {
  52. registerValidation(argc, argv);
  53. inf.readInt(1, 100, "n");
  54. inf.readEoln();
  55. inf.readEof();
  56. }
  57. ```
  58. ### Generator
  59. This generator outputs a random token to the standard output, containing Latin letters or digits. The length of the token will be between 1 and 1000, inclusive. It will use a uniformly distributed random generator. To generate different values, call it with different command-line parameters. It is typical behavior for a testlib generator to set up randseed by command line. See more examples in the package.
  60. ```c++
  61. #include "testlib.h"
  62. int main(int argc, char* argv[]) {
  63. registerGen(argc, argv, 1);
  64. println(rnd.next(1, 10)); /* Random number in the range [1,10]. */
  65. println(rnd.next("[a-zA-Z0-9]{1,1000}")); /* Random word of length [1,1000]. */
  66. }
  67. ```
  68. This generator outputs a random permutation; the size is equal to the first command-line argument.
  69. ```c++
  70. #include "testlib.h"
  71. int main(int argc, char* argv[]) {
  72. registerGen(argc, argv, 1);
  73. int n = opt<int>(1);
  74. println(n);
  75. println(rnd.perm(n, 1));
  76. }
  77. ```