Skip to content

Commit

Permalink
feat: 开发学习总结程序
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 10, 2024
1 parent f351543 commit 32cd96c
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
run: xmake

- name: xmake run
run: xmake run learn
run: xmake run summary --simple
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/build
/build/

/log/*
!/log/placeholder

.*/
!/.github/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@
```

运行 0 号练习。

5. 总结学习

使用

```shell
xmake run summary
```

总结所有练习通过情况。
18 changes: 18 additions & 0 deletions learn/learn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "test.h"
#include <iostream>

int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: xmake run learn <exercice number>" << std::endl;
return EXIT_FAILURE;
}
int num;
if (1 != std::sscanf(argv[1], "%d", &num)) {
std::cerr << "Invalid exercise number: " << argv[1] << std::endl;
return EXIT_FAILURE;
};
if (!test_exercise(num, nullptr)) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
55 changes: 0 additions & 55 deletions learn/main.cpp

This file was deleted.

47 changes: 47 additions & 0 deletions learn/summary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "test.h"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

constexpr auto MAX_EXERCISE = 22;

int main(int argc, char **argv) {
if (argc == 1) {
std::vector<bool> result(MAX_EXERCISE + 1, false);
auto success = 0;
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
if (test_exercise(i, nullptr)) {
result[i] = true;
++success;
}
}

std::cout << success << "/" << MAX_EXERCISE + 1 << " [";
for (auto b : result) {
std::cout << (b ? "\x1b[32m#\x1b[0m" : "\x1b[31mX\x1b[0m");
}
std::cout << ']' << std::endl;
return EXIT_SUCCESS;
}
if (argc == 2 && std::strcmp(argv[1], "--simple") == 0) {
auto time = std::chrono::system_clock::now();
auto time_ = std::chrono::system_clock::to_time_t(time);
std::stringstream ss;
ss << std::put_time(std::localtime(&time_), "%Y-%m-%d-%H-%M-%S") << ".log";
auto log_file = ss.str();

auto success = 0;
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
if (test_exercise(i, log_file.c_str())) {
++success;
}
}

std::cout << success << "/" << MAX_EXERCISE + 1 << std::endl;
return EXIT_SUCCESS;
}
std::cerr << "Usage: xmake run summary [--simple]" << std::endl;
return EXIT_FAILURE;
}
41 changes: 41 additions & 0 deletions learn/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#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 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());
}

bool test_exercise(int n, const char *log) {
char str[] = "exerciseXX";
std::sprintf(str, "exercise%02d", n);

if (log) {
static auto log_ = fs::absolute(fs::path(XMAKE) / "log" / log);
std::fstream(log_, std::ios::out | std::ios::app)
<< "Testing " << str << std::endl
<< std::endl;
auto log__ = log_.string();
auto log___ = log__.c_str();
return process_run("", str, log___) == EXIT_SUCCESS && process_run("run", str, log___) == EXIT_SUCCESS;
}

std::cout << "Testing " << str << std::endl
<< std::endl;
return process_run("", str, nullptr) == EXIT_SUCCESS && process_run("run", str, nullptr) == EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions learn/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __TEST_H__
#define __TEST_H__

bool test_exercise(int n, const char *log);

#endif// __TEST_H__
Empty file added log/placeholder
Empty file.
16 changes: 13 additions & 3 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
add_rules("mode.debug", "mode.release")
set_encodings("utf-8")

target("test")
set_kind("static")
set_languages("cxx17")
add_defines(string.format("__XMAKE__=\"%s\"", os.scriptdir():gsub("\\", "/")))
add_files("learn/test.cpp")

target("learn")
set_kind("binary")
set_languages("cxx17")
add_deps("test")
add_files("learn/learn.cpp")

add_defines(string.format("__XMAKE__=\"%s\"", os.scriptdir():gsub("\\", "/")))

target("summary")
set_kind("binary")
set_languages("cxx17")
add_files("learn/main.cpp")
add_deps("test")
add_files("learn/summary.cpp")

0 comments on commit 32cd96c

Please sign in to comment.