跳到正文

个人记录

Testlib 的使用方法 翻译

Imakf约 6 分钟
原文

这是 2025年11月12日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2018年11月25日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

下载地址 https://github.com/MikeMirzayanov/testlib/archive/0.9.12.zip

Testlib 库

Intro 简介

This project contains C++ implementation of the testlib. It already used on many programming contests in Russia (Russian National Olympiad in Informatics, different stages of ACM-ICPC). Join!

本项目包含了C++实现的testlib。testlib已经用于许多俄罗斯的程序竞赛中(比如Russian National Olympiad in Informatics,ACM的不同阶段),快来使用testlib吧!

The library C++ code is tested for compatibility with Borland C++ (5.6), Microsoft Visual C++ (2003+), g++ (MinGW 4.0.2). Also we don’t hear any issues about incorrect work with other compilers.

testlib库已经过与 Borland C++ (5.6), Microsoft Visual C++ (2003+), g++ (MinGW 4.0.2). 的兼容性测试。而且我们没有听闻过testlib库任何与编译器的冲突问题。

This code used many times on Open Team Programming Collegiate Cup contests (http://opencup.ru/) running on Linux platform and compiled by g++ compiler.

Samples 使用范例

Checker SPJ判断

This is sample checker which expects in the output and in the answer the same integer. It ignores all white-spaces. See more examples in the package.

下面这个栗子需要输出一个和和答案文件中一样的intint。checker会忽略所有的不可见字符(\n 空格等)(忽略行末空格和\n)。在testlib的文件夹下可以看到更多的实例。

C++
#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);
}

Validator 判断HACK数据合法与否

This code reads input from the standard input and checks that it contains the only integer between 1 and 100, inclusive. Also validates that file ends with EOLN and EOF. On Windows it expects #13#10 as EOLN and it expects #10 as EOLN on other platforms. It doesn’t ignore white-spaces, so it works very strict. It will return non-zero code in case of illegal input and writes message into the standard output. See more examples in the package.

这份代码从标准输入(stdin)中读入,检测输入的整数是否在11~100100之间。您还需要读入\n 和 EOF。在Windows系统下EOLN是\r\n ,其他系统是\n。Valitador不会忽略不可见字符,所以说它的编写非常严格(写炸了你就肯定会被喷)。如果输入的HACK数据不合法,就会告诉您HACK格式错在哪里。同样在testlib的文件夹下可以看到更多的实例。

C++
#include "testlib.h"

int main(int argc, char* argv[])
{
    registerValidation(argc, argv);
    inf.readInt(1, 100);
    inf.readEoln();
    inf.readEof();
}

Generator 发生器

This generator outputs into the standard output random token, containing Latin letters or digits. The length of the token will be between 1 and 1000, inclusive. It will use uniformly distributed random. To generate different values, call it with different command line parameters. It is typical behaviour of testlib generator to setup randseed by command line. See more examples in the package.

Generator通过标准输出(stdout)输出随机字符串或数字,字符串长度要小于10001000。它使用的是均匀分布随机数。

C++
#include "testlib.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    registerGen(argc, argv, 1);
    cout << rnd.next(1, 10) << endl; /* Random number in the range [1,10]. */
    cout << rnd.next("[a-zA-Z0-9]{1,1000}") << endl;  /* Random word of length [1,1000]. */
}