Logo Wy Online Judge

WyOJ

Paste!

#include <cassert>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <string>

#ifdef _WIN32
#define COPY_COMMAND "copy"
#define REMOVE_COMMAND "del"
#define RUN_PREFIX ""
#else
#define COPY_COMMAND "cp"
#define REMOVE_COMMAND "rm"
#define RUN_PREFIX "./"
#endif

// 标准输入输出控制标志,文件输入输出请注释掉 
//#define STDIO

// 输入后缀 
#define INF_SUF "in"
// 输出后缀 
#define OUF_SUF "out"
// 答案后缀 
#define ANS_SUF "ans"
// 从第几个大样例开始测试 
#define TEST_START 1 
// 时间限制 
#define TLIMIT_IN_SECONDS 1.0

// 默认你可执行文件名等于题目名称 
// argv[1] 是可执行文件名,大样例没有前缀,argv[2]是大样例数目 
// argv[1] 是可执行文件名,大样例前缀是 argv[3],argv[2]是大样例数目 

// 用法两种
// 1. (./)本程序名 题目名称 要测试的大样例数量
// 2. (./)本程序名 题目名称 要测试的大样例数量 大样例前缀 

int builtin_checker(std::string ouf, std::string ans);

int main(int argc, char** argv) {
    using namespace std;
    string title = argv[1], pre = "";
    int cnt = stoi(argv[2]);
    if(argc == 4)
        pre = argv[3];
    for(int i = TEST_START; i < TEST_START + cnt; i++)
    {
        system((COPY_COMMAND " " + pre + to_string(i) + "." INF_SUF  " " + title + "." INF_SUF).c_str());
        auto start = clock();
        int ret;
#ifdef STDIO
        ret = system((RUN_PREFIX + title + " < " + title + "." INF_SUF " >" + title + "." OUF_SUF).c_str());
#else
        ret = system((RUN_PREFIX + title).c_str());
#endif
        double time = double(clock() - start) / CLOCKS_PER_SEC;
        printf("测试用例%d:用时为 %f 秒,", i, time);
        if(ret)
        	printf("运行时段错误");
		else if(time > TLIMIT_IN_SECONDS)
			printf("超出时间限制");
        else if(builtin_checker(title + "." OUF_SUF, pre + to_string(i) + "." ANS_SUF))
        	printf("结果与答案不符");
		else
			printf("结果与答案一致");
		printf(";\n");
        system((REMOVE_COMMAND " " + title + "." INF_SUF).c_str()); 
    }
    return 0;
}

int builtin_checker(std::string ouf, std::string ans) {
    using namespace std;
    ifstream in1(ouf);
    ifstream in2(ans);
    string token1, token2;
    int cnt1 = 0, cnt2 = 0;
    while(true)
    {
        int tmp = cnt1; 
        cnt1 += bool(getline(in1, token1));
        cnt2 += bool(getline(in2, token2));
        if(cnt1 != cnt2 || tmp == cnt1)
            break;
        while(!token1.empty() && token1.back() == ' ')
            token1.pop_back();
        while(!token2.empty() && token2.back() == ' ')
            token2.pop_back();
        if(token1 != token2)
        {
//            printf("wrong answer\nWrong answer on token %d, %s read %s, %s expected %s.\n",
//                cnt1, ouf.c_str(), token1.c_str(), ans.c_str(), token2.c_str());
            return 17;
        }
    }
    if(cnt1 < cnt2)
    {
//        printf("wrong answer\nFile %s too short.\n", ouf.c_str());
        return 18; 
    }
    if(cnt2 < cnt1)
    {
//        printf("wrong answer\nFile %s too long.\n", ouf.c_str());
        return 19; 
    }
//    printf("ok %d tokens.\n", cnt1); 
    return 0;
}