linux done

This commit is contained in:
Zengtudor 2024-08-17 14:19:19 +08:00
parent 989c44891d
commit 8dfd101ae9
3 changed files with 116 additions and 49 deletions

View File

@ -5,13 +5,14 @@
#include <filesystem>
#include <fstream>
#include <functional>
#include <future>
#include <iostream>
#include<json/json.h>
#include <ostream>
#include <queue>
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#ifdef _WIN32
#include<windows.h>
@ -59,21 +60,30 @@ struct Test{
}
};
#ifdef _WIN32
struct ProgramRet{
time_point<steady_clock> start;
time_point<steady_clock> end;
};
void executeCommandWithRedirect(const std::string &command, const std::string &inputFile, const std::string &outputFile);
#else
struct ProgramRet{
time_point<system_clock> start;
time_point<system_clock> end;
};
#endif
template<typename T>
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 "<<s.id<<" TLE";
}else if(s.cmp==0){
os<<"tests "<<s.id<<" AC";
}else{
os<<"tests "<<s.id<<" WA line:"<<s.cmp;
}
return os;
}
};
int main(int argc, char* argv[]) {
LOG("Zengtudor OI test tools . loading files .. please add a json file in arg");
@ -119,6 +129,12 @@ int main(int argc, char* argv[]) {
LOG("creating building dir")
std::filesystem::create_directory(buildDir);
}
LOG("getting run time out")
int runtimeout = 0;
if(jsonValue["runtimeout"]!=Json::nullValue){
runtimeout = jsonValue["runtimeout"].asInt();
printValue(runtimeout)
}
LOG("compiling")
string compileCommands;
const path exePath = (buildDir/"main").string()+(isWindows==true?".exe":"");
@ -130,16 +146,19 @@ int main(int argc, char* argv[]) {
auto cpuNums = getCpuNums();
printValue(cpuNums)
if(jsonValue["tests"].size()==0)LOG("cannot find tests");
if(jsonValue["tests"].size()==0){
LOG("cannot find tests")
return 0;
}
LOG("parsing tests")
vector<Test> tests;
vector<Test> 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<std::function<ProgramRet()>> tasks;
const auto testsPath = projectPath/"tests";
@ -151,40 +170,90 @@ int main(int argc, char* argv[]) {
const string exePathStr = getProtectPath(exePath);
for(auto i:tests){
tasks.push([i,&exePathStr,&testsPath]()->ProgramRet{
string id_str = std::to_string(i.id);
string commands;
std::stringstream ss;
ss<<i.id;
string id_str;
ss>>id_str;
path outPath = testsPath/(id_str+".out") ;
// 构建命令
string commands = exePathStr + " < " + getProtectPath(i.in) + " > " + getProtectPath(outPath);
// 输出构建的命令进行调试
LOG(commands);
// 执行命令
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
system(commands.c_str());
auto end=high_resolution_clock::now();
// AS_EM(result, 0,"error in exec commands")
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();
futures.push_back(std::async(std::launch::async,i));
}
vector<ProgramRet> 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<Statu> 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<<status[i]<<endl;
}
exit(0);
}
int compareFiles(const std::string& path1, const std::string& path2) {
std::ifstream file1(path1);
std::ifstream file2(path2);
if (!file1.is_open()) {
std::cerr << "无法打开文件: " << path1 << std::endl;
exit(1);
}
if (!file2.is_open()) {
std::cerr << "无法打开文件: " << path2 << std::endl;
exit(1);
}
std::string line1, line2;
int lineNumber = 1;
while (std::getline(file1, line1) && std::getline(file2, line2)) {
if (line1 != line2) {
return lineNumber; // 返回第一个不同的行号
}
++lineNumber;
}
// 如果一个文件还没读完
if (std::getline(file1, line1) || std::getline(file2, line2)) {
return lineNumber;
}
return 0; // 文件内容完全一致
}
#ifdef _WIN32

View File

@ -1,8 +1,11 @@
#include<bits/stdc++.h>
#include <chrono>
#include <thread>
using namespace std;
int main(){
// this_thread::sleep_for(chrono::seconds(2));
string s;
cin>>s;
cout<<s<<endl;

View File

@ -5,10 +5,5 @@
"args":[
"-O3"
],
"tests":[
{
"in":"a.in",
"ans":"1.ans"
}
]
"runtimeout":100
}