From e13bcbb5f4cd412dd96ec243782b117b0dd24495 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Fri, 16 Aug 2024 13:54:46 +0800 Subject: [PATCH] update --- src/main.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------- tests/1.in | 1 + 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 tests/1.in diff --git a/src/main.cpp b/src/main.cpp index fadd8ca..463bee0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include "json/reader.h" #include "json/value.h" +#include #include #include #include @@ -8,7 +9,9 @@ #include #include #include +#include #include +#include #ifdef _WIN32 #include @@ -16,7 +19,7 @@ #include "unistd.h" #endif -using std::cerr,std::endl,std::cout,std::ifstream,std::string,std::vector,std::queue,std::filesystem::path; +using std::cerr,std::endl,std::cout,std::ifstream,std::string,std::vector,std::queue,std::filesystem::path,std::thread,std::chrono::time_point,std::chrono::steady_clock,std::chrono::high_resolution_clock; #define AS_EQ(a,b){if((a)!=(b)){cerr<<"assert eq failed :"< start; + time_point end; +}; + template T getValueOrPanic(T value); @@ -116,20 +125,44 @@ int main(int argc, char* argv[]) { if(jsonValue["tests"].size()==0)LOG("cannot find tests"); LOG("parsing tests") vector tests; - for(auto i:jsonValue["tests"]){ + auto &jsonTests = jsonValue["tests"]; + for(unsigned int i=1;i<=jsonTests.size();i++){ tests.push_back({ - .in=getAbsolutePath(projectPath, getValueOrPanic(i["in"]).asString()), - .ans=getAbsolutePath(projectPath,getValueOrPanic(i["ans"]).asString()) + .id = i, + .in=getAbsolutePath(projectPath, getValueOrPanic(jsonTests[i-1]["in"]).asString()), + .ans=getAbsolutePath(projectPath,getValueOrPanic(jsonTests[i-1]["ans"]).asString()), }); } - queue> tasks; + queue> tasks; + const auto testsPath = projectPath/"tests"; + printValue(testsPath) const string exePathStr = getProtectPath(exePath); for(auto i:tests){ - tasks.push([i,&exePathStr](){ + tasks.push([i,&exePathStr,&testsPath]()->ProgramRet{ string commands; - commands = commands+exePathStr+" < "+getProtectPath(i.in)+" > "; + 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(); + system(commands.c_str()); + auto end=high_resolution_clock::now(); + return ProgramRet{ + .start=start, + .end = end + }; }); } + while(tasks.size()>0){ + auto i = tasks.front(); + tasks.pop(); + thread newThread(i); + AS_EQ(newThread.joinable(),true); + newThread.join(); + } } string getProtectPath(const std::filesystem::path &p){ diff --git a/tests/1.in b/tests/1.in new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/tests/1.in @@ -0,0 +1 @@ +1 \ No newline at end of file