tests: Added UTF-8 file operation tests
This commit is contained in:
parent
ab1f4df9d9
commit
f9fc7051fc
2
lib/external/pattern_language
vendored
2
lib/external/pattern_language
vendored
@ -1 +1 @@
|
||||
Subproject commit 0ab398d950b5e46a8b3810a6a483ace393be8b50
|
||||
Subproject commit 5ab770490e69e2873fd18eb3d0e40735610dc034
|
@ -47,10 +47,12 @@ namespace hex::fs {
|
||||
size_t readBuffer(u8 *buffer, size_t size);
|
||||
std::vector<u8> readBytes(size_t numBytes = 0);
|
||||
std::string readString(size_t numBytes = 0);
|
||||
std::u8string readU8String(size_t numBytes = 0);
|
||||
|
||||
void write(const u8 *buffer, size_t size);
|
||||
void write(const std::vector<u8> &bytes);
|
||||
void write(const std::string &string);
|
||||
void write(const std::u8string &string);
|
||||
|
||||
[[nodiscard]] size_t getSize() const;
|
||||
void setSize(u64 size);
|
||||
|
@ -89,10 +89,24 @@ namespace hex::fs {
|
||||
if (bytes.empty())
|
||||
return "";
|
||||
|
||||
const char *cString = reinterpret_cast<const char *>(bytes.data());
|
||||
auto cString = reinterpret_cast<const char *>(bytes.data());
|
||||
return { cString, std::min(bytes.size(), std::strlen(cString)) };
|
||||
}
|
||||
|
||||
std::u8string File::readU8String(size_t numBytes) {
|
||||
if (!isValid()) return {};
|
||||
|
||||
if (getSize() == 0) return {};
|
||||
|
||||
auto bytes = readBytes(numBytes);
|
||||
|
||||
if (bytes.empty())
|
||||
return u8"";
|
||||
|
||||
auto cString = reinterpret_cast<const char8_t *>(bytes.data());
|
||||
return { cString, std::min(bytes.size(), std::strlen(reinterpret_cast<const char*>(bytes.data()))) };
|
||||
}
|
||||
|
||||
void File::write(const u8 *buffer, size_t size) {
|
||||
if (!isValid()) return;
|
||||
|
||||
@ -111,6 +125,12 @@ namespace hex::fs {
|
||||
std::fwrite(string.data(), string.size(), 1, this->m_file);
|
||||
}
|
||||
|
||||
void File::write(const std::u8string &string) {
|
||||
if (!isValid()) return;
|
||||
|
||||
std::fwrite(string.data(), string.size(), 1, this->m_file);
|
||||
}
|
||||
|
||||
size_t File::getSize() const {
|
||||
if (!isValid()) return 0;
|
||||
|
||||
|
@ -9,6 +9,8 @@ TEST_SEQUENCE("FileAccess") {
|
||||
const auto FilePath = std::fs::current_path() / "file.txt";
|
||||
const auto FileContent = "Hello World";
|
||||
|
||||
std::fs::create_directories(FilePath.parent_path());
|
||||
|
||||
{
|
||||
hex::fs::File file(FilePath, hex::fs::File::Mode::Create);
|
||||
TEST_ASSERT(file.isValid());
|
||||
@ -38,5 +40,43 @@ TEST_SEQUENCE("FileAccess") {
|
||||
TEST_FAIL();
|
||||
}
|
||||
|
||||
TEST_SUCCESS();
|
||||
};
|
||||
|
||||
TEST_SEQUENCE("UTF-8 Path") {
|
||||
const auto FilePath = std::fs::current_path() / u8"读写汉字" / u8"привет.txt";
|
||||
const auto FileContent = u8"שלום עולם";
|
||||
|
||||
std::fs::create_directories(FilePath.parent_path());
|
||||
|
||||
{
|
||||
hex::fs::File file(FilePath, hex::fs::File::Mode::Create);
|
||||
TEST_ASSERT(file.isValid());
|
||||
|
||||
file.write(FileContent);
|
||||
}
|
||||
|
||||
{
|
||||
hex::fs::File file(FilePath, hex::fs::File::Mode::Read);
|
||||
TEST_ASSERT(file.isValid());
|
||||
|
||||
TEST_ASSERT(file.readU8String() == FileContent);
|
||||
}
|
||||
|
||||
{
|
||||
hex::fs::File file(FilePath, hex::fs::File::Mode::Write);
|
||||
TEST_ASSERT(file.isValid());
|
||||
|
||||
|
||||
file.remove();
|
||||
TEST_ASSERT(!file.isValid());
|
||||
}
|
||||
|
||||
{
|
||||
hex::fs::File file(FilePath, hex::fs::File::Mode::Read);
|
||||
if (file.isValid())
|
||||
TEST_FAIL();
|
||||
}
|
||||
|
||||
TEST_SUCCESS();
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user