diff --git a/src/main.cpp b/src/main.cpp index b90852a..8065f8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,13 +5,14 @@ #include #include #include +#include #include #include #include #include -#include #include #include +#include #ifdef _WIN32 #include @@ -59,21 +60,30 @@ struct Test{ } }; -#ifdef _WIN32 - struct ProgramRet{ - time_point start; - time_point end; - }; - void executeCommandWithRedirect(const std::string &command, const std::string &inputFile, const std::string &outputFile); -#else - struct ProgramRet{ - time_point start; - time_point end; - }; -#endif +struct ProgramRet{ + time_point start; + time_point end; +}; template T getValueOrPanic(T value); +int compareFiles(const std::string& path1, const std::string& path2); + +struct Statu{ + bool isTLE = false; + int cmp; + int id; + friend std::ostream& operator<<(std::ostream &os,Statu &s){ + if(s.isTLE){ + os<<"tests "< tests; + vector tests(jsonValue["tests"].size()+1); auto &jsonTests = jsonValue["tests"]; for(unsigned int i=1;i<=jsonTests.size();i++){ - tests.push_back({ + tests[i]={ .id = i, .in=getAbsolutePath(projectPath, getValueOrPanic(jsonTests[i-1]["in"]).asString()), .ans=getAbsolutePath(projectPath,getValueOrPanic(jsonTests[i-1]["ans"]).asString()), - }); + }; } queue> tasks; const auto testsPath = projectPath/"tests"; @@ -149,42 +168,92 @@ int main(int argc, char* argv[]) { } printValue(testsPath) const string exePathStr = getProtectPath(exePath); - for (auto i : tests) { - tasks.push([i, &exePathStr, &testsPath]() -> ProgramRet { - string id_str = std::to_string(i.id); - path outPath = testsPath / (id_str + ".out"); - - // 构建命令 - string commands = exePathStr + " < " + getProtectPath(i.in) + " > " + getProtectPath(outPath); - - // 输出构建的命令进行调试 - LOG(commands); - - // 执行命令 + for(auto i:tests){ + tasks.push([i,&exePathStr,&testsPath]()->ProgramRet{ + string commands; + std::stringstream ss; + ss<>id_str; + path outPath = testsPath/(id_str+".out") ; + commands = commands+exePathStr+" < "+getProtectPath(i.in)+" > "+getProtectPath(outPath); + LOG(commands) auto start = high_resolution_clock::now(); - #ifdef _WIN32 - executeCommandWithRedirect(commands, getProtectPath(i.in), getProtectPath(outPath)); - #else - system(commands.c_str()) - #endif - auto end = high_resolution_clock::now(); - - // AS_EM(result, 0,"error in exec commands") - + system(commands.c_str()); + auto end=high_resolution_clock::now(); return ProgramRet{ - .start = start, + .start=start, .end = end }; }); } - while(tasks.size()>0){ auto i = tasks.front(); tasks.pop(); - thread newThread(i); - AS_EQ(newThread.joinable(),true); - newThread.join(); + futures.push_back(std::async(std::launch::async,i)); } + vector results(jsonTests.size()+1); + for(auto &i:futures){ + if(runtimeout>0){ + if(i.wait_for(std::chrono::milliseconds(runtimeout))==std::future_status::ready){ + auto ret = i.get(); + results[ret.id]=ret; + } + }else{ + i.wait(); + auto ret = i.get(); + results[ret.id]=ret; + } + } + LOG("get results done") + for(int i=1;i<=jsonTests.size();i++){ + LOG(results[i]) + } + + vector status(jsonTests.size()+1); + for(int i=1;i<=jsonTests.size();i++){ + printValue(tests[i].ans) + status[i]={ + .isTLE=(results[i].id==0?true:false), + .cmp=compareFiles(results[i].outPath,tests[i].ans), + .id=i + }; + } + for(int i=1;i<=jsonTests.size();i++){ + cout< +#include +#include using namespace std; int main(){ + // this_thread::sleep_for(chrono::seconds(2)); string s; cin>>s; cout<