Skip to content

Commit

Permalink
更新README,
Browse files Browse the repository at this point in the history
添加文本比对函数,
更新样例,
更新_test_file中的测试文件路径.

Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Jul 23, 2020
1 parent eee0802 commit e0950a9
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
},
"files.associations": {
"sshd_config": "bat",
"tuple": "cpp"
"tuple": "cpp",
"limits": "cpp"
}
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O0")


set(lab_number 00)
set(problem_order D)
set(problem_order C)

include_directories(./includes)
include_directories(./thrid_party)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @Author: nanoseeds
* @Date: 2020-07-15 23:52:04
* @LastEditors: nanoseeds
* @LastEditTime: 2020-07-22 22:20:53
* @LastEditTime: 2020-07-24 00:11:26
* @License: CC-BY-NC-SA_V4_0 or any later version
-->

Expand Down Expand Up @@ -109,13 +109,12 @@
+ PS: 此处注意,引用文件的相对路径,不是直接的`test/lab_00/lab_00_C_data/01.data.in`,
而是编译出的文件相对于测试数据的相对路径.
在样例中,编译出的文件在`CS203_DSAA_template/cmake-build-debug`下,所以需要加`./../`
+ PS2: 文件重定向输出不说了,因为输出就两个,保证数据正确和格式正确,数据正确完全可以通过读取*.data.out之后来判断,
格式正确主要是看output函数.
+ PS2: 文件重定向输出在下面.

4. 文本的输出重定向:
+ 一般来说,题目的输出不会太复杂,但是反例也不是没有.:比如专门考输出的[立体图](./source/lab_00/lab_00_D.cpp)
+ 这种情况下,使用c++的重定向输出就可以较为方便的对输入进行处理,同时保存输出方便调试.
```c++
``` c++
std::streambuf *strmin_buf = cin.rdbuf();
std::streambuf *strmout_buf = cout.rdbuf();
std::ifstream file_in;
Expand All @@ -128,7 +127,8 @@
cin.rdbuf(strmin_buf);
```
这样就将标准输出重定向到了01.test.out中.
+ 至于比较文件之间的差异,就需要用bash脚本了.
+ 至于比较文件之间的差异,可以使用内置的`compareFiles(const string& path1,const string& path2)`函数进行比较.
参考[文本比对_test_case_2](./test/lab_00/lab_00_D_test.cpp)

5. 为什么要将 `读取` `数据处理` `输出` 分开?
+ 便于理清思路,读完题目之后,不管别的,先把数据读入,输出的函数写好,方便后续写作.
Expand Down
1 change: 0 additions & 1 deletion algorithm/sort/sort_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ TEST_CASE("random test", "[sort]") {
nums.push_back(rand() % 114514);
}
vector<int> nums_result(nums);

sort_warpper(nums);
std::sort(nums_result.begin(), nums_result.end());
CHECK_THAT(nums, Equals(nums_result));
Expand Down
29 changes: 26 additions & 3 deletions includes/public.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
/*
* @Github: https://github.com/Certseeds/CS203_DSAA_template
* @Organization: SUSTech
* @Author: nanoseeds
* @Date: 2020-07-15 21:44:06
* @LastEditors: nanoseeds
* @LastEditTime: 2020-07-23 23:40:10
* @LICENSE: MIT
*/
/*
Expand Down Expand Up @@ -35,9 +36,12 @@ SOFTWARE.
#define CS203_DSAA_TEMPLATE_INCLUDES_PUBLIC_H

#include <string>
#include <cassert>
#include <fstream>
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <algorithm>
#include "CS203_helper.hpp"
#include "CS203_timer.hpp"

Expand All @@ -47,7 +51,7 @@ using std::cout;
CS203_timer timer{};

static int faster_streams = []() {
srand(time(0));
srand(time(0));
// use time to init the random seed
std::ios::sync_with_stdio(false);
std::istream::sync_with_stdio(false);
Expand All @@ -57,4 +61,23 @@ static int faster_streams = []() {
// 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能.
return 0;
}();

bool compareFiles(const std::string &p1, const std::string &p2) {
// get from https://stackoverflow.com/questions/6163611/compare-two-files
std::ifstream f1(p1, std::ifstream::binary | std::ifstream::ate);
std::ifstream f2(p2, std::ifstream::binary | std::ifstream::ate);
if (f1.fail() || f2.fail()) {
return false; //file problem
}
if (f1.tellg() != f2.tellg()) {
return false; //size mismatch
}
//seek back to beginning and use std::equal to compare contents
f1.seekg(0, std::ifstream::beg);
f2.seekg(0, std::ifstream::beg);
return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
std::istreambuf_iterator<char>(),
std::istreambuf_iterator<char>(f2.rdbuf()));
}

#endif //CS203_DSAA_TEMPLATE_INCLUDES_PUBLIC_H
1 change: 1 addition & 0 deletions script/cpp_test_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ using std::vector;
using Catch::Matchers::Equals;
using Catch::Matchers::UnorderedEquals;
using Catch::Matchers::Contains;
const string test_file_path = "./../test/lab_{}/lab_{}_{}_data/";

TEST_CASE("test case 1", "[test {} {}]") {{
auto output = cal(std::make_tuple(114, 514));
Expand Down
8 changes: 4 additions & 4 deletions script/file_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

main_code_template: str
test_code_template: str
GITHUB_USER: str = "Certseeds"
USER: str = "nanoseeds"
GITHUB_USER: str = "YOUR_GITHUB_NAME" # example: Certseeds
USER: str = "YOUR_USER_NAME" # example: nanoseeds
year: str = time.strftime("%Y", time.localtime())
create_time: str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
source_path: str = "./../source/lab_{}/lab_{}_{}.cpp"
Expand All @@ -63,8 +63,8 @@ def fill_file(lab_number: str, problem_order: str) -> None:
GITHUB_USER, USER, create_time, USER, year, USER))
print("main finish\n")
with open(test_path.format(lab_number, lab_number, problem_order), mode='a+', encoding="UTF-8") as file:
file.write(test_code_template.format(GITHUB_USER, USER, create_time, USER, year, USER, lab_number,
problem_order, lab_number, problem_order))
file.write(test_code_template.format(GITHUB_USER, USER, create_time, USER, year, USER, lab_number, lab_number,
problem_order, lab_number, problem_order, lab_number, problem_order))
print('test finish')


Expand Down
4 changes: 1 addition & 3 deletions source/lab_00/lab_00_D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ SOFTWARE.
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::string;
Expand All @@ -52,8 +51,7 @@ const vector<std::pair<int32_t, string>> spis{
{0, "+---+ |"},
{0, "| | +"},
{0, "| |/"},
{0, "+---+"}
};
{0, "+---+"}};

using input_data = vector<vector<int32_t>>;
using result_data = vector<vector<char>>;
Expand Down
31 changes: 16 additions & 15 deletions test/lab_00/lab_00_C_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ using std::vector;
using Catch::Matchers::Equals;
using Catch::Matchers::UnorderedEquals;
using Catch::Matchers::Contains;
const string test_file_path = "./../test/lab_00/lab_00_C_data/";

TEST_CASE("test case 1", "[test 00 C]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_C_data/01.data.in");
fin.open(test_file_path + "01.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
// 重定向开始,开始run
Expand All @@ -63,7 +64,7 @@ TEST_CASE("test case 1", "[test 00 C]") {
TEST_CASE("test case 2", "[test 00 C]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_C_data/02.data.in");
fin.open(test_file_path + "02.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
auto result_data = isBipartite(read());
Expand All @@ -74,7 +75,7 @@ TEST_CASE("test case 2", "[test 00 C]") {
TEST_CASE("test case 3", "[test 00 C]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_C_data/03.data.in");
fin.open(test_file_path + "03.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
auto result_data = isBipartite(read());
Expand All @@ -85,7 +86,7 @@ TEST_CASE("test case 3", "[test 00 C]") {
TEST_CASE("test case 4", "[test 00 C]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_C_data/04.data.in");
fin.open(test_file_path + "04.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
auto result_data = isBipartite(read());
Expand All @@ -96,7 +97,7 @@ TEST_CASE("test case 4", "[test 00 C]") {
TEST_CASE("test case 5", "[test 00 C]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_C_data/05.data.in");
fin.open(test_file_path + "05.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
auto result_data = isBipartite(read());
Expand All @@ -106,11 +107,11 @@ TEST_CASE("test case 5", "[test 00 C]") {

TEST_CASE("test case in loop", "[test 00 C]") {
const vector<string> strs{
"./../test/lab_00/lab_00_C_data/01.data.in",
"./../test/lab_00/lab_00_C_data/02.data.in",
"./../test/lab_00/lab_00_C_data/03.data.in",
"./../test/lab_00/lab_00_C_data/04.data.in",
"./../test/lab_00/lab_00_C_data/05.data.in"
test_file_path + "01.data.in",
test_file_path + "02.data.in",
test_file_path + "03.data.in",
test_file_path + "04.data.in",
test_file_path + "05.data.in"
};
const vector<uint8_t> result{false, true, true, false, false};
for (int i = 0; i < 5; ++i) {
Expand All @@ -127,11 +128,11 @@ TEST_CASE("test case in loop", "[test 00 C]") {

TEST_CASE("test case with tuple", "[test 00 C]") {
const vector<std::tuple<string, result_data>> input_result{
{"./../test/lab_00/lab_00_C_data/01.data.in", false},
{"./../test/lab_00/lab_00_C_data/02.data.in", true},
{"./../test/lab_00/lab_00_C_data/03.data.in", true},
{"./../test/lab_00/lab_00_C_data/04.data.in", false},
{"./../test/lab_00/lab_00_C_data/05.data.in", false}
{test_file_path + "01.data.in", false},
{test_file_path + "02.data.in", true},
{test_file_path + "03.data.in", true},
{test_file_path + "04.data.in", false},
{test_file_path + "05.data.in", false}
};
for (const auto &tup : input_result) {
string path;
Expand Down
2 changes: 1 addition & 1 deletion test/lab_00/lab_00_D_data/01.data.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
+---+---+---+---+ |/...
| | | | | +....
| | | | |/.....
+---+---+---+---+......
+---+---+---+---+......
30 changes: 18 additions & 12 deletions test/lab_00/lab_00_D_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ using std::vector;
using Catch::Matchers::Equals;
using Catch::Matchers::UnorderedEquals;
using Catch::Matchers::Contains;
const string test_file_path = "./../test/lab_00/lab_00_D_data/";

TEST_CASE("test case 1", "[test 00 D]") {
std::streambuf *backup;
std::ifstream fin;
fin.open("./../test/lab_00/lab_00_D_data/01.data.in");
fin.open(test_file_path + "01.data.in");
backup = cin.rdbuf();
cin.rdbuf(fin.rdbuf());
// 重定向开始,开始run
Expand All @@ -62,17 +63,22 @@ TEST_CASE("test case 1", "[test 00 D]") {
}

TEST_CASE("test case 2", "[test 00 C]") {
std::streambuf *strmin_buf = cin.rdbuf();
std::streambuf *strmout_buf = cout.rdbuf();
std::ifstream file_in;
std::ofstream file_out;
file_in.open("./../test/lab_00/lab_00_D_data/01.data.in");
file_out.open("./../test/lab_00/lab_00_D_data/01.test.out");
cin.rdbuf(file_in.rdbuf());
cout.rdbuf(file_out.rdbuf());
cal(read());
cin.rdbuf(strmin_buf);
cout.rdbuf(strmout_buf);
SECTION("do") {
std::streambuf *strmin_buf = cin.rdbuf();
std::streambuf *strmout_buf = cout.rdbuf();
std::ifstream file_in;
std::ofstream file_out;
file_in.open(test_file_path + "01.data.in");
file_out.open(test_file_path + "01.test.out");
cin.rdbuf(file_in.rdbuf());
cout.rdbuf(file_out.rdbuf());
cal(read());
cin.rdbuf(strmin_buf);
cout.rdbuf(strmout_buf);
cout.flush();
}SECTION("compare files") {
CHECK(compareFiles(test_file_path + "01.test.out", test_file_path + "01.data.out"));
}
}

#endif //CS203_DSAA_TEST_MACRO

0 comments on commit e0950a9

Please sign in to comment.