Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Common Toolkit &&
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
103 Star 575 Fork 242

cpp-master/cpp-tbox

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
develop
Branches (18)
Tags (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
cpp-tbox
/
modules
/
util
/
fs_test.cpp
cpp-tbox
/
modules
/
util
/
fs_test.cpp
fs_test.cpp 11.61 KB
Copy Edit Raw Blame History
海卫哥 authored 2025年05月26日 07:28 +08:00 . tidy(util.fs): 更改名称
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include <gtest/gtest.h>
#include "fs.h"
const char *test_filename = "./fs_test.txt";
const char *test_filename_cannot_access = "./xsdf/fs_test.txt";
using namespace tbox;
using namespace tbox::util::fs;
//! 测试文件的读、写、删除、是否存在操作,成功的用例
TEST(fs, TextFileReadWrite_Ok) {
std::string text_tobe_read;
const std::string text_tobe_write1 = "hello, this is a test.";
const std::string text_tobe_write2 = "another text, yes, for test.\n ";
::unlink(test_filename); //! 先删一次
EXPECT_FALSE(IsFileExist(test_filename)); //! 文件不应该存在
EXPECT_TRUE(WriteStringToTextFile(test_filename, text_tobe_write1)); //! 写入数据
EXPECT_TRUE(IsFileExist(test_filename)); //! 文件应该存在
EXPECT_TRUE(ReadStringFromTextFile(test_filename, text_tobe_read)); //! 将文件读取出来
EXPECT_EQ(text_tobe_write1, text_tobe_read); //! 对比写入与读出的数据是否一致
//! 覆盖写入
EXPECT_TRUE(WriteStringToTextFile(test_filename, text_tobe_write2)); //! 再次写入数据
EXPECT_TRUE(ReadStringFromTextFile(test_filename, text_tobe_read)); //! 将文件读取出来
EXPECT_EQ(text_tobe_write2, text_tobe_read); //! 对比写入与读出的数据是否一致
EXPECT_TRUE(RemoveFile(test_filename));
EXPECT_FALSE(IsFileExist(test_filename));
}
TEST(fs, TextFileReadAppend_Ok) {
std::string text_tobe_read;
const std::string text_tobe_write1 = "hello, this is a test.";
const std::string text_tobe_write2 = "another text, yes, for test.\n ";
::unlink(test_filename); //! 先删一次
EXPECT_FALSE(IsFileExist(test_filename)); //! 文件不应该存在
EXPECT_TRUE(WriteStringToTextFile(test_filename, text_tobe_write1)); //! 写入数据
EXPECT_TRUE(AppendStringToTextFile(test_filename, text_tobe_write2)); //! 写入数据
EXPECT_TRUE(IsFileExist(test_filename)); //! 文件应该存在
EXPECT_TRUE(ReadStringFromTextFile(test_filename, text_tobe_read)); //! 将文件读取出来
EXPECT_EQ(text_tobe_write1 + text_tobe_write2, text_tobe_read); //! 对比写入与读出的数据是否一致
RemoveFile(test_filename);
}
TEST(fs, TextFileReadEachLine) {
::unlink(test_filename); //! 先删一次
const std::string text_tobe_write = "first line\r\nsecond line\nthird line";
EXPECT_TRUE(WriteStringToTextFile(test_filename, text_tobe_write)); //! 写入数据
std::vector<std::string> str_vec;
bool ret = ReadEachLineFromTextFile(test_filename,
[&](const std::string &line) { str_vec.push_back(line); }
);
EXPECT_TRUE(ret);
ASSERT_EQ(str_vec.size(), 3u);
EXPECT_EQ(str_vec[0], "first line");
EXPECT_EQ(str_vec[1], "second line");
EXPECT_EQ(str_vec[2], "third line");
}
TEST(fs, TextFileReadFirstLine) {
::unlink(test_filename); //! 先删一次
EXPECT_TRUE(WriteStringToTextFile(test_filename, "first line\r\n")); //! 写入数据
std::string text;
EXPECT_TRUE(ReadFirstLineFromTextFile(test_filename, text));
ASSERT_EQ(text, "first line");
::unlink(test_filename); //! 先删一次
EXPECT_TRUE(WriteStringToTextFile(test_filename, "one line")); //! 写入数据
EXPECT_TRUE(ReadFirstLineFromTextFile(test_filename, text));
ASSERT_EQ(text, "one line");
}
TEST(fs, FileFail) {
EXPECT_FALSE(WriteStringToTextFile(test_filename_cannot_access, "Hello"));
std::string text_tobe_read;
EXPECT_FALSE(ReadStringFromTextFile(test_filename_cannot_access, text_tobe_read));
EXPECT_FALSE(RemoveFile(test_filename_cannot_access));
}
TEST(fs, BinaryFileReadWrite_Ok) {
std::string binary_tobe_read;
std::string binary_tobe_write1 = "hello, this is a test.";
std::string binary_tobe_write2 = "another binary, yes, for test.\n ";
//! 插入0,円模拟二进制数据中存在0数据的情况
binary_tobe_write1[1] = '0円';
binary_tobe_write2[1] = '0円';
::unlink(test_filename); //! 先删一次
EXPECT_FALSE(IsFileExist(test_filename)); //! 文件不应该存在
EXPECT_TRUE(WriteBinaryToFile(test_filename, binary_tobe_write1)); //! 写入数据
EXPECT_TRUE(IsFileExist(test_filename)); //! 文件应该存在
EXPECT_TRUE(ReadBinaryFromFile(test_filename, binary_tobe_read)); //! 将文件读取出来
EXPECT_EQ(binary_tobe_write1, binary_tobe_read); //! 对比写入与读出的数据是否一致
//! 覆盖写入
EXPECT_TRUE(WriteBinaryToFile(test_filename, binary_tobe_write2)); //! 再次写入数据
EXPECT_TRUE(ReadBinaryFromFile(test_filename, binary_tobe_read)); //! 将文件读取出来
EXPECT_EQ(binary_tobe_write2, binary_tobe_read); //! 对比写入与读出的数据是否一致
EXPECT_TRUE(RemoveFile(test_filename));
EXPECT_FALSE(IsFileExist(test_filename));
}
TEST(fs, RenameFile) {
auto old_name = "old_file";
auto new_name = "new_file";
EXPECT_TRUE(WriteStringToTextFile(old_name, ""));
EXPECT_TRUE(Rename(old_name, new_name));
EXPECT_TRUE(IsFileExist(new_name));
EXPECT_FALSE(IsFileExist(old_name));
RemoveFile(new_name);
}
TEST(fs, RenameDirectory) {
auto old_name = "old_dir";
auto new_name = "new_dir";
EXPECT_TRUE(MakeDirectory(old_name));
EXPECT_TRUE(Rename(old_name, new_name));
EXPECT_TRUE(IsDirectoryExist(new_name));
EXPECT_FALSE(IsFileExist(old_name));
RemoveDirectory(old_name);
}
TEST(fs, IsDirectoryExist) {
EXPECT_FALSE(IsDirectoryExist("should/not/exist/directory"));
EXPECT_TRUE(IsDirectoryExist("/etc/"));
}
TEST(fs, MakeDirectory) {
int ret = 0;
//! 绝对路径
EXPECT_TRUE(MakeDirectory("/tmp/fs_test_dir"));
EXPECT_TRUE(IsDirectoryExist("/tmp/fs_test_dir"));
ret = system("rm -rf /tmp/fs_test_dir");
//! 相对路径
EXPECT_TRUE(MakeDirectory("1"));
EXPECT_TRUE(IsDirectoryExist("1"));
ret = system("rm -rf 1");
//! 相对路径
EXPECT_TRUE(MakeDirectory("./a/b/c"));
EXPECT_TRUE(IsDirectoryExist("./a/b/c"));
//! 重复的'/'
EXPECT_TRUE(MakeDirectory("./a///d/"));
EXPECT_TRUE(IsDirectoryExist("a/d"));
//! 权限不够
ret = system("chmod 440 ./a/d"); //! 将 ./a/d 设置为 440
EXPECT_FALSE(MakeDirectory("./a/d/e")); //! 创建失败,因为权限不够
ret = system("rm -rf ./a");
//! 空路径不能创建
EXPECT_FALSE(MakeDirectory(""));
//! 已存在对应的其它文件
ret = system("mkdir 1; touch 1/2");
EXPECT_FALSE(MakeDirectory("1/2/3")); //! 不成功,因为1/2是文件,不是目录
ret = system("rm -rf ./1");
//! 重复创建
EXPECT_TRUE(MakeDirectory("a/b/c"));
EXPECT_TRUE(MakeDirectory("a/b/c"));
ret = system("rm -rf ./a");
ret = system("mkdir a b; cd a; ln -s ../b .;");
EXPECT_TRUE(MakeDirectory("a/b/c"));
EXPECT_TRUE(IsFileExist("a/b/c"));
ret = system("rm -rf a b");
(void)ret;
}
TEST(fs, Dirname) {
EXPECT_EQ(Dirname("a"), ".");
EXPECT_EQ(Dirname("a/"), "a");
EXPECT_EQ(Dirname("a/b"), "a");
EXPECT_EQ(Dirname("a/b/"), "a/b");
EXPECT_EQ(Dirname(" a/b "), "a");
EXPECT_EQ(Dirname(""), ".");
EXPECT_EQ(Dirname(" a "), ".");
EXPECT_EQ(Dirname("/"), "/");
EXPECT_EQ(Dirname("/a"), "/");
EXPECT_EQ(Dirname("/a/"), "/a");
}
TEST(fs, Basename) {
EXPECT_STREQ(Basename("./a/b/c"), "c");
EXPECT_STREQ(Basename("abcdef"), "abcdef");
EXPECT_STREQ(Basename(""), "");
EXPECT_EQ(Basename(std::string("./a/b/c")), "c");
EXPECT_EQ(Basename(std::string("abcdef")), "abcdef");
EXPECT_EQ(Basename(std::string()), "");
}
TEST(fs, FilenameStem) {
EXPECT_EQ(FilenameStem("a.b"), "a");
EXPECT_EQ(FilenameStem("a"), "a");
EXPECT_EQ(FilenameStem("a."), "a");
EXPECT_EQ(FilenameStem("/w/a.b"), "a");
EXPECT_EQ(FilenameStem("/w.x/a"), "a");
EXPECT_EQ(FilenameStem(""), "");
}
TEST(fs, FilenameSuffix) {
EXPECT_EQ(FilenameSuffix("a.b"), "b");
EXPECT_EQ(FilenameSuffix("a"), "");
EXPECT_EQ(FilenameSuffix("a."), "");
EXPECT_EQ(FilenameSuffix("/w/a.b"), "b");
EXPECT_EQ(FilenameSuffix("/w.x/a"), "");
EXPECT_EQ(FilenameSuffix(""), "");
}
TEST(fs, RemoveDirectory) {
//! 绝对路径测试
int ret = 0;
ret = ::system("rm -rf /tmp/fs_test_dir");
ret = ::system("mkdir -p /tmp/fs_test_dir/first_dir");
(void)ret;
WriteStringToTextFile("/tmp/fs_test_dir/fs_test_0_1.txt", "hello, this is a test file");
WriteStringToTextFile("/tmp/fs_test_dir/fs_test_0_2.txt", "hello, this is a test file");
WriteStringToTextFile("/tmp/fs_test_dir/first_dir/fs_test_1_1.txt", "hello, this is a test file");
WriteStringToTextFile("/tmp/fs_test_dir/first_dir/fs_test_1_2.txt", "hello, this is a test file");
EXPECT_TRUE(RemoveDirectory("/tmp/fs_test_dir", true)); //! 仅删文件,不删目录
EXPECT_TRUE(IsDirectoryExist("/tmp/fs_test_dir/first_dir/"));
EXPECT_TRUE(RemoveDirectory("/tmp/fs_test_dir", false)); //! 全部删除
EXPECT_FALSE(IsDirectoryExist("/tmp/fs_test_dir"));
//! 相对路径测试,一层目录
MakeDirectory("./a");
WriteStringToTextFile("./a/fs_test_a_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/fs_test_a_2.txt", "hello, this is a test file");
EXPECT_TRUE(RemoveDirectory("./a"));
EXPECT_FALSE(IsDirectoryExist("./a"));
//! 相对路径测试,多层目录
MakeDirectory("./a/b/c");
WriteStringToTextFile("./a/fs_test_a_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/fs_test_a_2.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_2.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/c/fs_test_c_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/c/fs_test_c_2.txt", "hello, this is a test file");
EXPECT_TRUE(RemoveDirectory("./a"));
EXPECT_FALSE(IsDirectoryExist("./a"));
//! 重复的'/' 测试
MakeDirectory("./a/b");
WriteStringToTextFile("./a/fs_test_a_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/fs_test_a_2.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_2.txt", "hello, this is a test file");
EXPECT_TRUE(RemoveDirectory("./a//b"));
EXPECT_FALSE(IsDirectoryExist("./a/b"));
EXPECT_TRUE(RemoveDirectory("./a/"));
EXPECT_FALSE(IsDirectoryExist("./a"));
//! 目录尾部多加一个 '/' 测试
MakeDirectory("./a/b");
WriteStringToTextFile("./a/fs_test_a_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/fs_test_a_2.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_1.txt", "hello, this is a test file");
WriteStringToTextFile("./a/b/fs_test_b_2.txt", "hello, this is a test file");
EXPECT_TRUE(RemoveDirectory("./a/"));
EXPECT_FALSE(IsDirectoryExist("./a"));
// 只有目录,没有文件的删除测试
MakeDirectory("./a/b1/c");
MakeDirectory("./a/b2/c");
MakeDirectory("./a/b3/c");
EXPECT_TRUE(RemoveDirectory("./a"));
EXPECT_FALSE(IsDirectoryExist("./a"));
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

cpp-tbox是一个Linux C++,MIT开源许可的,基于 Reactor 模式的开发框架与组件库。它主要针对智能硬件、机器人、网络服务等开发领域。 它提供了一套易于开发、稳定可靠的框架,有:通信库(TCP/UDP/串口)、HTTP、线程池、定时器池、协程、日志、命令终端、状态机、行为树等非常实用的开发组件,以及完备实用的主框架,能为广大开发者节省大量开发与调试时间。
Cancel

Releases

No release

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cpp-master/cpp-tbox.git
git@gitee.com:cpp-master/cpp-tbox.git
cpp-master
cpp-tbox
cpp-tbox
develop
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /