testlib

repository·master·Indexed 21 days ago

https://github.com/mikemirzayanov/testlib

A C++ library for competitive programming used to create checkers, interactors, validators, and test case generators.

Tokens
1.2K
Snippets
4
Records
4
Agent score
33%

What's inside testlib

  1. Create a Generator with testlib

    master

    A generator produces test cases using a random number generator. Use registerGen(argc, argv, seed_arg_index) to initialize, where seed_arg_index is the index of the command-line argument used to set the random seed.

    Common tasks include:

    • Generating random numbers: rnd.next(min, max).
    • Generating random strings/tokens: rnd.next("regex_pattern").
    • Generating random permutations: rnd.perm(n, seed).
    • Accessing command-line arguments: opt<T>(index).

    To ensure different test cases, call the generator with different command-line parameters (seeds).

    #include "testlib.h"
    
    int main(int argc, char* argv[]) {
        registerGen(argc, argv, 1);
        println(rnd.next(1, 10)); /* Random number in the range [1,10]. */
        println(rnd.next("[a-zA-Z0-9]{1,1000}")); /* Random word of length [1,1000]. */
    }
  2. Create a Validator with testlib

    master

    A validator ensures that the input file strictly adheres to the problem constraints. Use registerValidation(argc, argv) to initialize. It reads from the input file (inf). It is highly strict regarding whitespace and line endings: it expects readEoln() (End of Line) and readEof() (End of File) to be called explicitly. On Windows, readEoln() expects #13#10, while on other platforms it expects #10. If validation fails, it returns a non-zero exit code and writes a message to stdout.

    #include "testlib.h"
    
    int main(int argc, char* argv[]) {
        registerValidation(argc, argv);
        inf.readInt(1, 100, "n");
        inf.readEoln();
        inf.readEof();
    }
  3. Create an Interactor with testlib

    master

    An interactor manages the interaction between the test input (inf) and the participant's solution. Use registerInteraction(argc, argv) to initialize. The interactor reads from the input file (inf), writes queries to the solution (typically via std::cout), and reads the solution's response to write to the output file for later verification (tout). You can terminate the interaction using quitf(_wa, <comment>) if the participant violates the protocol.

    #include "testlib.h"
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
        setName("Interactor A+B");
        registerInteraction(argc, argv);
    
        // reads number of queries from test (input) file
        int n = inf.readInt();
        for (int i = 0; i < n; i++) {
            // reads query from test (input) file
            int a = inf.readInt();
            int b = inf.readInt();
    
            // writes query to the solution, endl makes flush
            cout << a << " " << b << endl;
    
            // writes output file to be verified by checker later
            tout << ouf.readInt() << endl;
        }
    
        // just message
        quitf(_ok, "%d queries processed", n);
    }
  4. Create a Checker with testlib

    master

    A checker is used to compare the output of a participant's program (ouf) against the expected answer (ans). Use registerTestlibCmd(argc, argv) to initialize the checker. You can use methods like ans.readInt() and ouf.readInt() to parse data. To signal the result, use quitf(_ok, ...) for a correct answer or quitf(_wa, ...) for a Wrong Answer, providing a descriptive message.

    #include "testlib.h"
    
    int main(int argc, char * argv[]) {
        setName("compares two signed integers");
        registerTestlibCmd(argc, argv);
        int ja = ans.readInt();
        int pa = ouf.readInt();
        if (ja != pa)
            quitf(_wa, "expected %d, found %d", ja, pa);
        quitf(_ok, "answer is %d", ja);
    }