Skip to content

Commit 32cd96c

Browse files
committed
feat: 开发学习总结程序
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent f351543 commit 32cd96c

File tree

10 files changed

+141
-60
lines changed

10 files changed

+141
-60
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
run: xmake
3737

3838
- name: xmake run
39-
run: xmake run learn
39+
run: xmake run summary --simple

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
/build
1+
/build/
2+
3+
/log/*
4+
!/log/placeholder
5+
26
.*/
37
!/.github/

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@
3838
```
3939

4040
运行 0 号练习。
41+
42+
5. 总结学习
43+
44+
使用
45+
46+
```shell
47+
xmake run summary
48+
```
49+
50+
总结所有练习通过情况。

learn/learn.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "test.h"
2+
#include <iostream>
3+
4+
int main(int argc, char **argv) {
5+
if (argc != 2) {
6+
std::cerr << "Usage: xmake run learn <exercice number>" << std::endl;
7+
return EXIT_FAILURE;
8+
}
9+
int num;
10+
if (1 != std::sscanf(argv[1], "%d", &num)) {
11+
std::cerr << "Invalid exercise number: " << argv[1] << std::endl;
12+
return EXIT_FAILURE;
13+
};
14+
if (!test_exercise(num, nullptr)) {
15+
return EXIT_FAILURE;
16+
}
17+
return EXIT_SUCCESS;
18+
}

learn/main.cpp

-55
This file was deleted.

learn/summary.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "test.h"
2+
#include <chrono>
3+
#include <iomanip>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <vector>
7+
8+
constexpr auto MAX_EXERCISE = 22;
9+
10+
int main(int argc, char **argv) {
11+
if (argc == 1) {
12+
std::vector<bool> result(MAX_EXERCISE + 1, false);
13+
auto success = 0;
14+
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
15+
if (test_exercise(i, nullptr)) {
16+
result[i] = true;
17+
++success;
18+
}
19+
}
20+
21+
std::cout << success << "/" << MAX_EXERCISE + 1 << " [";
22+
for (auto b : result) {
23+
std::cout << (b ? "\x1b[32m#\x1b[0m" : "\x1b[31mX\x1b[0m");
24+
}
25+
std::cout << ']' << std::endl;
26+
return EXIT_SUCCESS;
27+
}
28+
if (argc == 2 && std::strcmp(argv[1], "--simple") == 0) {
29+
auto time = std::chrono::system_clock::now();
30+
auto time_ = std::chrono::system_clock::to_time_t(time);
31+
std::stringstream ss;
32+
ss << std::put_time(std::localtime(&time_), "%Y-%m-%d-%H-%M-%S") << ".log";
33+
auto log_file = ss.str();
34+
35+
auto success = 0;
36+
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
37+
if (test_exercise(i, log_file.c_str())) {
38+
++success;
39+
}
40+
}
41+
42+
std::cout << success << "/" << MAX_EXERCISE + 1 << std::endl;
43+
return EXIT_SUCCESS;
44+
}
45+
std::cerr << "Usage: xmake run summary [--simple]" << std::endl;
46+
return EXIT_FAILURE;
47+
}

learn/test.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
6+
#ifndef __XMAKE__
7+
#define __XMAKE__ "XMAKE is not defined"
8+
#endif
9+
10+
namespace fs = std::filesystem;
11+
constexpr static auto XMAKE = __XMAKE__;
12+
13+
static int process_run(const char *cmd, const char *proj, const char *log) {
14+
static auto exercises = fs::absolute(fs::path(XMAKE) / "exercises");
15+
auto command = std::string("xmake ") + cmd + " -P " + exercises.string() + ' ' + proj;
16+
if (log) {
17+
command += " >> ";
18+
command += log;
19+
command += " 2>&1";
20+
}
21+
return std::system(command.c_str());
22+
}
23+
24+
bool test_exercise(int n, const char *log) {
25+
char str[] = "exerciseXX";
26+
std::sprintf(str, "exercise%02d", n);
27+
28+
if (log) {
29+
static auto log_ = fs::absolute(fs::path(XMAKE) / "log" / log);
30+
std::fstream(log_, std::ios::out | std::ios::app)
31+
<< "Testing " << str << std::endl
32+
<< std::endl;
33+
auto log__ = log_.string();
34+
auto log___ = log__.c_str();
35+
return process_run("", str, log___) == EXIT_SUCCESS && process_run("run", str, log___) == EXIT_SUCCESS;
36+
}
37+
38+
std::cout << "Testing " << str << std::endl
39+
<< std::endl;
40+
return process_run("", str, nullptr) == EXIT_SUCCESS && process_run("run", str, nullptr) == EXIT_SUCCESS;
41+
}

learn/test.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __TEST_H__
2+
#define __TEST_H__
3+
4+
bool test_exercise(int n, const char *log);
5+
6+
#endif// __TEST_H__

log/placeholder

Whitespace-only changes.

xmake.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
add_rules("mode.debug", "mode.release")
22
set_encodings("utf-8")
33

4+
target("test")
5+
set_kind("static")
6+
set_languages("cxx17")
7+
add_defines(string.format("__XMAKE__=\"%s\"", os.scriptdir():gsub("\\", "/")))
8+
add_files("learn/test.cpp")
9+
410
target("learn")
511
set_kind("binary")
12+
set_languages("cxx17")
13+
add_deps("test")
14+
add_files("learn/learn.cpp")
615

7-
add_defines(string.format("__XMAKE__=\"%s\"", os.scriptdir():gsub("\\", "/")))
8-
16+
target("summary")
17+
set_kind("binary")
918
set_languages("cxx17")
10-
add_files("learn/main.cpp")
19+
add_deps("test")
20+
add_files("learn/summary.cpp")

0 commit comments

Comments
 (0)