Prerequisite knowledge: General
This page briefly introduces the concept and usage of validators.
Overview
A validator is used to verify the validity of generated test data. When the data for a problem has been generated but the problem setter is concerned that it may be invalid (that it does not satisfy the problem's constraints: overflow, a disconnected graph, not being a tree, ...), the setter usually uses a validator to check it.1
Because Codeforces supports hacking, every problem on Codeforces must have a validator. UOJ is the same. Polygon has built-in support for validators.
Usage
Simply enter ./val on the command line. The data is supplied through stdin. If you want to read from a file, use ./val < a.in.
If there is no problem with the data, nothing will be output and 0 will be returned; otherwise, an error message will be output and a non-zero value will be returned.
Notes
- When writing a validator, do not make any assumptions about the data being validated, because it may contain anything. Therefore, the problem setter must check all kinds of invalid cases (Testlib greatly simplifies this process).
- For example, given a tree with vertices as input, the main tasks are to determine whether is within the range and whether the input is a tree. However, after checking the range of , do not fail to check that the endpoints of each subsequently input edge are within the valid range, otherwise the validator may RE.
- Even if it will not RE, it should still be checked; otherwise, your error report will be incorrect. In the example above, if this is not checked, the error might be "not a tree", but the correct error should be "the edge's start/endpoints are not in ".
- Do not make any assumptions about how contestants read the input. Therefore, you must ensure that the data passed by the validator completely conforms to the input format.
- For example, a contestant may read digits character by character, reading only one space between numbers. Therefore, when writing a validator, every whitespace character in the data must be explicitly read by the validator (such as spaces and newlines).
- Do not forget
inf.readEof()at the end. - If the problem is open to hacks (or if the validator's error messages will be visible to others), make the error messages as user-friendly as possible.
- Use "item aliases" when reading variables.
- When the expression used in a check is not very easy to understand, use ensuref rather than ensure.
Example
The following is the validator for CF Gym 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++) {
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();
}