If you have written some programming problems, and have prepared test cases, you will probably experience the terrible feeling that some test cases may be invalid (meaning it does not agree with the constraints in problem statement): upper bound can be violated, your graph not satisfied connectivity requirements or is not at tree... It is reasonable to feel that way. Even experienced problem setters make mistakes sometimes (for example, in the prestigious ACM ICPC World final 2007).
It is strictly recommended to write a special program (called validator) to formally check each test to satisfy all requirements from problem statements. Validators are strictly required for problems on Codeforces. Polygon has built-in support of validators.
It is really easy to write a validator using testlib.h.
Example
Following is a validator that could be used for problem 100541A - Stock Market:
#include "testlib.h"
int main(int argc, char* argv[]) {
registerValidation(argc, argv);
int testCount = inf.readInt(1, 10, "testCount");
inf.readEoln();
for (int i = 0; i < testCount; i++) {
setTestCase(i + 1);
int n = inf.readInt(1, 100, "n");
inf.readSpace();
inf.readInt(1, 1'000'000, "w");
inf.readEoln();
inf.readInts(n, 1, 1000, "p");
inf.readEoln();
}
inf.readEof();
}
Original validator using an older version of testlib.h#include "testlib.h"
int main(int argc, char* argv[]) {
registerValidation(argc, argv);
int testCount = inf.readInt(1, 10, "testCount");
inf.readEoln();
for (int i = 0; i < testCount; i++) {
int n = inf.readInt(1, 100, "n");
inf.readSpace();
inf.readInt(1, 1000000, "w");
inf.readEoln();
for(int i = 0; i < n; ++i) {
inf.readInt(1, 1000, "p_i");
if (i < n-1)
inf.readSpace();
}
inf.readEoln();
}
inf.readEof();
}
The wonderful thing about this validator is that it is very simple and it is very difficult to write something incorrect.
More examples can be found at the Github repo
Available methods
The first line of your code should be registerValidation(argc, argv)
which does some magic in the background, so that you can use the necessary methods.
Full text and comments »