From 11354a5a2f5fdb22462bcf949aa3de7f91e930f9 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 17 Aug 2024 14:21:13 +0800 Subject: [PATCH] update --- src/main.cpp | 103 +++++++++++++++++++------------------------------- tests/xj.json | 10 +++++ 2 files changed, 48 insertions(+), 65 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8065f8e..b585af5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #ifdef _WIN32 #include @@ -61,8 +60,20 @@ struct Test{ }; struct ProgramRet{ + #ifdef _WIN32 + time_point start; + time_point end; + #else time_point start; time_point end; + #endif + + path outPath; + unsigned int id; + friend std::ostream& operator<<(std::ostream&os,ProgramRet&ret){ + os<<"ProgramRet { start: "< @@ -168,25 +179,37 @@ int main(int argc, char* argv[]) { } printValue(testsPath) const string exePathStr = getProtectPath(exePath); - 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) + for (int j=1;j<=jsonTests.size();j++) { + auto &i = tests[j]; + 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); + + // 执行命令 auto start = high_resolution_clock::now(); - system(commands.c_str()); - auto end=high_resolution_clock::now(); + int result = system(commands.c_str()); + auto end = high_resolution_clock::now(); + + if (result != 0) { + cerr << "Error executing command: " << commands << endl; + exit(1); + } + return ProgramRet{ - .start=start, - .end = end + .start = start, + .end = end, + .outPath=outPath, + .id=i.id }; }); } + vector> futures; while(tasks.size()>0){ auto i = tasks.front(); tasks.pop(); @@ -256,60 +279,10 @@ int compareFiles(const std::string& path1, const std::string& path2) { return 0; // 文件内容完全一致 } -#ifdef _WIN32 - -void executeCommandWithRedirect(const std::string &command, const std::string &inputFile, const std::string &outputFile) { - // 创建输入输出文件的句柄 - HANDLE hInput = CreateFile(inputFile.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (hInput == INVALID_HANDLE_VALUE) { - throw std::runtime_error("Failed to open input file: " + inputFile); - } - - HANDLE hOutput = CreateFile(outputFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (hOutput == INVALID_HANDLE_VALUE) { - CloseHandle(hInput); - throw std::runtime_error("Failed to open output file: " + outputFile); - } - - STARTUPINFO si; - PROCESS_INFORMATION pi; - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - si.dwFlags |= STARTF_USESTDHANDLES; // 使用标准句柄 - si.hStdInput = hInput; - si.hStdOutput = hOutput; - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); // 重定向错误输出 - - ZeroMemory(&pi, sizeof(pi)); - - // 将命令转换为 LPSTR 类型 - LPSTR cmd = const_cast(command.c_str()); - - // 创建进程 - if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) { - CloseHandle(hInput); - CloseHandle(hOutput); - throw std::runtime_error("CreateProcess failed with error: " + std::to_string(GetLastError())); - } - - // 等待子进程结束 - WaitForSingleObject(pi.hProcess, INFINITE); - - // 关闭句柄 - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - CloseHandle(hInput); - CloseHandle(hOutput); -} - -#endif - - string getProtectPath(const std::filesystem::path &p){ return "\""+p.string()+"\""; } - template T getValueOrPanic(T value){ AS_NE(value, Json::nullValue) diff --git a/tests/xj.json b/tests/xj.json index eaf6470..bc74c42 100644 --- a/tests/xj.json +++ b/tests/xj.json @@ -5,5 +5,15 @@ "args":[ "-O3" ], + "tests":[ + { + "in":"a.in", + "ans":"1.ans" + }, + { + "in":"a.in", + "ans":"2.ans" + } + ], "runtimeout":100 } \ No newline at end of file