Introduction to CPLib (1) — Modern C++ Library for Test Data in CP

Правка en5, от aberter0x3f, 2025-08-15 08:03:31

You can refer to the second introduction post here.

CPLib is a library written in C++ for processing test data of competitive programming problems. It helps you write clear and efficient checkers, interactors, validators, and generators. CPLib uses "variable input template" as its major feature, provides friendly feedback for humans and backends, and is working hard to reach better compatibility and efficiency.

You can get CPLib from Github Repository. The "single-header-snapshot" branch automatically updates single-header version of CPLib which is easy to use. Also, you can visit cplib.aberter0x3f.top for more information. For regex-related questions, please refer to the FAQ page.

Here is a basic example of a checker using CPLib. For more examples, visit the links above.

#include "cplib.hpp"
 
using namespace cplib;
 
// Define the Input struct to read problem-specific inputs (A and B for A+B problem)
struct Input {
  int a, b;
 
  static Input read(var::Reader& in) {
    // Read integer 'a' within range [-1000, 1000]
    auto a = in.read(var::i32("a", -1000, 1000));
    // Read integer 'b' within range [-1000, 1000]
    auto b = in.read(var::i32("b", -1000, 1000));
    return {a, b};
  }
};
 
// Define the Output struct to read the answer and evaluate it
struct Output {
  int ans;
 
  // Static read method to parse participant/jury output
  // It receives a var::Reader and the Input struct for context if needed.
  static Output read(var::Reader& in, const Input&) {
    // Read integer 'ans' within range [-2000, 2000]
    auto ans = in.read(var::i32("ans", -2000, 2000));
    return {ans};
  }
 
  // Static evaluate method to compare participant's output with jury's output
  // It receives an evaluate::Evaluator, participant's output (pans),
  // jury's output (jans), and the original Input.
  static evaluate::Result evaluate(evaluate::Evaluator& ev, const Output& pans, const Output& jans,
                                   const Input&) {
    // Use ev.eq to compare the 'ans' field.
    // If pans.ans == jans.ans, it contributes to AC. Otherwise, it marks WA.
    auto res = evaluate::Result::ac();
    res &= ev.eq("ans", pans.ans, jans.ans);
    return res;
  }
};
 
// Register the checker with CPLib, specifying the Input and Output structs.
// This macro sets up the main checker logic behind the scenes.
CPLIB_REGISTER_CHECKER(Input, Output);

The project is mainly written by aberter0x3f, and I, tiger2005, work on syntax designs and some tiny stuffs. The project is still under development, so feel free to make suggestions!

Теги cplib, test data, checker, generator, validator, interactor

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский aberter0x3f 2025-08-15 08:19:30 0 The original documentation domain (cplib.vercel.app) has expired. The API of CPLib has been significantly improved since this post was first written. (published)
en5 Английский aberter0x3f 2025-08-15 08:03:31 1514 (saved to drafts)
en4 Английский tiger2005 2024-08-06 08:08:52 103
en3 Английский tiger2005 2024-01-21 13:35:05 5 Tiny change: ' please relate to the [F' -> ' please refer to the [F'
en2 Английский tiger2005 2024-01-21 13:11:29 2
en1 Английский tiger2005 2024-01-21 13:09:02 1521 Initial revision (published)