The algotester.h and algotester_generator.h libraries are created to facilitate writing the source codes for the problems. You should use them when writing all validators, checkers, scorers, and test generators.
The algotester.h library
While using the library, you will work with the
AlgotesterReader
class. This is a class for reading data.
While reading, it can also validate various properties. This reader can
work in two modes: strict for validators and non-strict for checkers and
scorers. In strict mode, you have to check the data format with all
spaces and end-of-line characters. In a non-strict mode it is not
necessary to read spaces or end-of-line characters.
List of AlgotesterReader
class methods and their brief
description:
readChar
– reads a single character. You can pass a string of possible values, whether to skip whitespaces (not possible in strict mode) and the name of the variable being read. It returns the character that was read, or prints an error message to stdout and stops working. If the symbol can take arbitrary values, then an empty string should be passed as a list of possible values.readSpace
– reads one space character. You should use this in strict mode.readEndl
– reads one end-of-line character. You should use this in strict mode.readEof
– reads end of file character. It should always be used in the validator and checker after reading the user stream to make sure there is no redundant output.isEof
– checks if it is end of file. It takes a parameter whether to skip whitespaces (not possible in strict mode). If whitespaces are skipped, then result of this function is true if only whitespaces are left.readInt
– returns a 64-bit integer. It can take such parameters as minimal and maximal values and the name of the variable being read. For exampleint n = reader.readInt(2, 100000, "n");
reads variable \(n\), and checks if it is in range from 2 to \(10^5\).readVectorInt
– returns vector of 64-bit integers. It takes size of vector as a parameter. You can pass minimal and maximal values for each element and name of the variable. For eachvector<int> a = reader.readVectorInt(5, 1, 100, "a");
reads a vector with 5 elements between 1 and 100. In strict mode, all values must be space-separated and end with the end-of-line character, with no extra spaces.readUInt
– the same asreadInt
, but works with unsigned integers.readToken
– reads a string. It can take a variable name that is being read and regular expression. You can also pass minimal and maximal string length. Here are some examples:string s = reader.readToken(1, 1e5, "s", "(0|1)*");
– reads binary string (each character is 0 or 1), and checks if its length is between 1 and \(10^5\).string s = reader.readToken("s", "[a-z]+");
– reads a string and checks if every character is latin lowercase letter.If there are length constraints you should pass them as additional parameters, and not as a part of regular expression.
readLine
– reads a line until end-of-line symbol.readDouble
– reads a floating point number. Possible parameters: minimal value, maximal value, minimal number of digits after the decimal point, maximal number of digits after the decimal point, whether it can be read in exponent format or not and the name of the variable. Boundary comparisons are performed explicitly, so consider real-number error when passing bounds.
You can find more information about this methods here.
Another important functions of the library:
initValidator
– returns reader in strict mode. You have to use it for validators. It has no parameters.initChecker
– returns an array of three readers in non-strict mode: input stream reader, user output reader and author output reader. You have to use it for checkers. It takes the programs input parameters (int argc, char* argv[]).initInteractiveChecker
– returns a tuple of such elements: input stream reader, user input stream (you have to write answers in this stream) and user output reader. You have to use it for interactive checkers. It takes the programs input parameters (int argc, char* argv[]).initScorer
– returns an array of three readers in non-strict mode: input stream reader, user output reader and author output reader. You have to use it for scorers. It takes the programs input parameters (int argc, char* argv[]).isClose
– checks if 2 double values are close with given absolute and relative tolerance. It takes two double values, absolute and relative tolerance. Default tolerances are \(10^{-7}\).equalsIgnoreCase
– checks if strings are equal ignoring case.check
– takes a condition and a string (error message). If condition is false prints error message.
You can find examples of validators, checkers and other source codes here.
The algotester_generator.h library
This library is designed to facilitate the writing of generators for
problems. It contains AlgotesterGenerator
class. To work
with it, you need to initialize the generator with the
initGenerator
function. It takes the input parameters of
your program. You can initialize it in this way:
auto gen = initGenerator(argc, argv)
. When creating tests
with the generator, we recommend you to select «Append hash to
arguments» checkbox. After that, you can use methods to generate
different values.
List of AlgotesterGenerator
class methods and their
brief description:
randInt
– generate uniform random unsigned 64-bit integer. You can pass bounds to this method. If you pass one integer \(n\), it will generate integer in range \([0, n)\). If you pass two integers \(l\) and \(r\), it will generate integer in range \([l, r]\).randUInt
– the same asrandInt
, but generate unsigned integers.randDouble
– generate uniform random real number in range \([0, 1]\). If you pass one integer \(n\), it will generate integer in range \([0, n]\). If you pass two integers \(l\) and \(r\), it will generate integer in range \([l, r]\).shuffle
– random shuffles elements. It can take vector, or iterators to the begin and end.randomElement
– returns random element of vector.randLogUniform
– takes integer \(n\), returns random integer in range \([1, n]\), such that natural logarithm of that number is uniformly distributed. Useful in cases when you want to generate both small and large values.randBiasedInt
– takes boundaries and \(t\) as parameters. If you pass one integer \(n\), it will generate integer in range \([0, n]\). If you pass two integers \(l\) and \(r\), it will generate integer in range \([l, r]\). Generates \(|t| + 1\) numbers. If \(t = 0\), returns random number, if \(t < 0\), returns minimum of \(t+1\) uniform values, if \(t > 0\), returns maximum of \(t+1\) uniform values. Works in O(1).getArg
– takes index \(i\), returns \(i\)-th argument of input parameters.
Full description of all methods you can find here.