forked from feiying123/training-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
68 lines (61 loc) · 2.13 KB
/
Copy pathtest.cpp
File metadata and controls
68 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "test.h"
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#ifndef __XMAKE__
#define __XMAKE__ "XMAKE is not defined"
#endif
namespace fs = std::filesystem;
constexpr static auto XMAKE = __XMAKE__;
static int process_run(const char *cmd, const char *proj, const char *log) {
static const auto exercises = fs::absolute(fs::path(XMAKE) / "exercises");
auto command = std::string("xmake ") + cmd + " -P \"" + exercises.string() + "\" " + proj;
if (log) {
command += " >> ";
command += log;
command += " 2>&1";
}
return std::system(command.c_str());
}
static bool test_exercise(int n, const char *log) {
std::ofstream file;
if (log) {
file.open(log, std::ios::out | std::ios::app);
}
std::ostream &os = log ? file : std::cout;
char str[] = "exerciseXX";
std::sprintf(str, "exercise%02d", n);
os << "\x1b[34m" << str << " testing" << "\x1b[0m" << std::endl
<< "==================" << std::endl;
auto pass = process_run("", str, log) == EXIT_SUCCESS && process_run("run", str, log) == EXIT_SUCCESS;
os << "=================" << std::endl
<< "\x1b[" << (pass ? 32 : 31) << 'm' << str << (pass ? " passed" : " failed") << "\x1b[0m" << std::endl
<< std::endl;
return pass;
}
Log &Log::operator<<(unsigned int n) {
namespace fs = std::filesystem;
bool pass;
if (std::holds_alternative<Console>(this->dst)) {
pass = test_exercise(n, nullptr);
} else if (std::holds_alternative<Null>(this->dst)) {
#if defined(_WIN32)
constexpr auto null = "nul";
#elif defined(__linux__) || defined(__unix__) || defined(__MACOSX__) || defined(__APPLE__)
constexpr auto null = "/dev/null";
#else
#error "Unsupported platform"
#endif
pass = test_exercise(n, null);
} else {
const auto path = fs::absolute(fs::path(XMAKE) / "log" / std::get<fs::path>(this->dst));
const auto path_string = path.string();
pass = test_exercise(n, path_string.c_str());
}
{
std::lock_guard lock(this->mutex);
this->result.push_back(pass);
}
return *this;
}