testlib.h從入門到入墳


學了這么久OI連個spj都不會寫真是慚愧啊。。。

趁着沒退役趕緊學一波吧

配置

github下載地址

我是直接暴力復制粘貼的。。

然后扔到MingW的目錄里

直接引用就好啦

 

基本語法

引用testlib.h后,我們可以從三個地方讀入數據

inf:輸入文件

ouf:選手輸出

ans:標准輸出

當然,肯定不能直接用scanf讀入,testlib里內置了很多讀入函數

基本上就夠用了。。。

一個簡單的例子

#include "testlib.h"

// main 需要接收命令行參數
int main(int argc, char *argv[])
{
    // 初始化 checker 環境 —— 解析命令行參數、打開文件……
    registerTestlibCmd(argc, argv);

    // 三個全局變量 inf, ouf, ans 依次為輸入、選手輸出和參考輸出
    int pans = ouf.readInt(-2000, 2000);
    int jans = ans.readInt();

    if (pans == jans)
        quitf(_ok, "The sum is correct.");
    else
        quitf(_wa, "The sum is wrong: expected = %d, found = %d", jans, pans);

    return 0;
}

 

運行

運行的時候需要切換到checker所在的目錄,輸入以下命令

windows

checker <input-file> <output-file> <answer-file>

Linux

./checker <input-file> <output-file> <answer-file>

根據輸出結果可以判斷程序的對錯

 

注意事項

寫這篇文章主要是為了記一下容易翻車的地方。。

  • 讀入的時候必須把三個文件里的內容都讀完,不然會出現

 

如果全都讀完后仍然顯示這個。。。。

zzq給了個解決方案Orz

 

 

  •  checker內盡量不要出現中文字符,不然上傳到某些OJ的時候可能會出BUG

 

 自己寫了個模板

#include "testlib.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1005 + 10; 
void YES() {
    quitf(_ok, "The ans is correct.");
}
void NO() {
    quitf(_wa, "The ans is not correct.");
    exit(0);
}
void readInf() {

}
void readOuf() {

}
void readAns() {

}
int main(int argc, char *argv[]) {
    registerTestlibCmd(argc, argv);
    
    readInf();
    readOuf();
    readAns();
    

    return 0;
}
板子


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM