This commit is contained in:
Zengtudor 2024-08-16 13:54:46 +08:00
parent 26969b340e
commit e13bcbb5f4
2 changed files with 41 additions and 7 deletions

View File

@ -1,5 +1,6 @@
#include "json/reader.h" #include "json/reader.h"
#include "json/value.h" #include "json/value.h"
#include <chrono>
#include <cstdlib> #include <cstdlib>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
@ -8,7 +9,9 @@
#include<json/json.h> #include<json/json.h>
#include <ostream> #include <ostream>
#include <queue> #include <queue>
#include <sstream>
#include <string> #include <string>
#include <thread>
#ifdef _WIN32 #ifdef _WIN32
#include<windows.h> #include<windows.h>
@ -16,7 +19,7 @@
#include "unistd.h" #include "unistd.h"
#endif #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 :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}} #define AS_EQ(a,b){if((a)!=(b)){cerr<<"assert eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}}
#define AS_NE(a,b){if((a)==(b)){cerr<<"assert not eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}} #define AS_NE(a,b){if((a)==(b)){cerr<<"assert not eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}}
@ -47,6 +50,7 @@ std::filesystem::path getAbsolutePath(std::filesystem::path rootPath,std::filesy
string getProtectPath(const std::filesystem::path &p); string getProtectPath(const std::filesystem::path &p);
struct Test{ struct Test{
unsigned int id;
std::filesystem::path in; std::filesystem::path in;
std::filesystem::path ans; std::filesystem::path ans;
friend std::ostream& operator<<(std::ostream& os,Test &t){ friend std::ostream& operator<<(std::ostream& os,Test &t){
@ -55,6 +59,11 @@ struct Test{
} }
}; };
struct ProgramRet{
time_point<steady_clock> start;
time_point<steady_clock> end;
};
template<typename T> template<typename T>
T getValueOrPanic(T value); T getValueOrPanic(T value);
@ -116,20 +125,44 @@ int main(int argc, char* argv[]) {
if(jsonValue["tests"].size()==0)LOG("cannot find tests"); if(jsonValue["tests"].size()==0)LOG("cannot find tests");
LOG("parsing tests") LOG("parsing tests")
vector<Test> tests; vector<Test> tests;
for(auto i:jsonValue["tests"]){ auto &jsonTests = jsonValue["tests"];
for(unsigned int i=1;i<=jsonTests.size();i++){
tests.push_back({ tests.push_back({
.in=getAbsolutePath(projectPath, getValueOrPanic(i["in"]).asString()), .id = i,
.ans=getAbsolutePath(projectPath,getValueOrPanic(i["ans"]).asString()) .in=getAbsolutePath(projectPath, getValueOrPanic(jsonTests[i-1]["in"]).asString()),
.ans=getAbsolutePath(projectPath,getValueOrPanic(jsonTests[i-1]["ans"]).asString()),
}); });
} }
queue<std::function<void()>> tasks; queue<std::function<ProgramRet()>> tasks;
const auto testsPath = projectPath/"tests";
printValue(testsPath)
const string exePathStr = getProtectPath(exePath); const string exePathStr = getProtectPath(exePath);
for(auto i:tests){ for(auto i:tests){
tasks.push([i,&exePathStr](){ tasks.push([i,&exePathStr,&testsPath]()->ProgramRet{
string commands; string commands;
commands = commands+exePathStr+" < "+getProtectPath(i.in)+" > "; std::stringstream ss;
ss<<i.id;
string id_str;
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){ string getProtectPath(const std::filesystem::path &p){

1
tests/1.in Normal file
View File

@ -0,0 +1 @@
1