|
1 | | -TESTLIB |
2 | | - |
3 | | -== What is it? == |
4 | | -Testlib is simple library which helps you to write |
5 | | - * checkers |
6 | | - * validators |
7 | | - * generators |
8 | | - * interactors |
9 | | -for programming competitions problems. |
10 | | -You can find latest release of the library on https://github.com/MikeMirzayanov/testlib/ |
11 | | -Problem development management system Polygon completely supports testlib. |
12 | | - |
13 | | -== How to use? == |
14 | | -Easest way is to read c++ sources in the checkers/, validators/, generators/ and interactors/ folders. |
15 | | -Also some classes and methods in testlib have documentation. |
16 | | - |
17 | | -Thanks for using testlib, |
18 | | -Mike Mirzayanov |
19 | | - |
| 1 | +# Testlib |
| 2 | + |
| 3 | +## Intro |
| 4 | + |
| 5 | +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! |
| 6 | + |
| 7 | +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++. |
| 8 | + |
| 9 | +This code has been used many times in Codeforces contests. |
| 10 | + |
| 11 | +## Samples |
| 12 | + |
| 13 | +### Checker |
| 14 | + |
| 15 | +This sample checker expects the same integer in the output and the answer. It ignores all white-spaces. See more examples in the package. |
| 16 | + |
| 17 | +```c++ |
| 18 | +#include "testlib.h" |
| 19 | + |
| 20 | +int main(int argc, char * argv[]) { |
| 21 | + setName("compares two signed integers"); |
| 22 | + registerTestlibCmd(argc, argv); |
| 23 | + int ja = ans.readInt(); |
| 24 | + int pa = ouf.readInt(); |
| 25 | + if (ja != pa) |
| 26 | + quitf(_wa, "expected %d, found %d", ja, pa); |
| 27 | + quitf(_ok, "answer is %d", ja); |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Interactor |
| 32 | + |
| 33 | +This sample interactor reads pairs of numbers from the input file, sends them to another program, reads |
| 34 | +the result, and writes it to an output file (to be verified later). Another option could be to terminate |
| 35 | +the interactor with `quitf(_wa, <comment>)`. |
| 36 | + |
| 37 | +```c++ |
| 38 | +#include "testlib.h" |
| 39 | +#include <iostream> |
| 40 | + |
| 41 | +using namespace std; |
| 42 | + |
| 43 | +int main(int argc, char* argv[]) { |
| 44 | + setName("Interactor A+B"); |
| 45 | + registerInteraction(argc, argv); |
| 46 | + |
| 47 | + // reads number of queries from test (input) file |
| 48 | + int n = inf.readInt(); |
| 49 | + for (int i = 0; i < n; i++) { |
| 50 | + // reads query from test (input) file |
| 51 | + int a = inf.readInt(); |
| 52 | + int b = inf.readInt(); |
| 53 | + |
| 54 | + // writes query to the solution, endl makes flush |
| 55 | + cout << a << " " << b << endl; |
| 56 | + |
| 57 | + // writes output file to be verified by checker later |
| 58 | + tout << ouf.readInt() << endl; |
| 59 | + } |
| 60 | + |
| 61 | + // just message |
| 62 | + quitf(_ok, "%d queries processed", n); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Validator |
| 67 | + |
| 68 | +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. |
| 69 | + |
| 70 | +```c++ |
| 71 | +#include "testlib.h" |
| 72 | + |
| 73 | +int main(int argc, char* argv[]) { |
| 74 | + registerValidation(argc, argv); |
| 75 | + inf.readInt(1, 100, "n"); |
| 76 | + inf.readEoln(); |
| 77 | + inf.readEof(); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Generator |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +```c++ |
| 86 | +#include "testlib.h" |
| 87 | + |
| 88 | +int main(int argc, char* argv[]) { |
| 89 | + registerGen(argc, argv, 1); |
| 90 | + println(rnd.next(1, 10)); /* Random number in the range [1,10]. */ |
| 91 | + println(rnd.next("[a-zA-Z0-9]{1,1000}")); /* Random word of length [1,1000]. */ |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +This generator outputs a random permutation; the size is equal to the first command-line argument. |
| 96 | + |
| 97 | +```c++ |
| 98 | +#include "testlib.h" |
| 99 | + |
| 100 | +int main(int argc, char* argv[]) { |
| 101 | + registerGen(argc, argv, 1); |
| 102 | + |
| 103 | + int n = opt<int>(1); |
| 104 | + println(n); |
| 105 | + println(rnd.perm(n, 1)); |
| 106 | +} |
| 107 | +``` |
0 commit comments