* mpqapi FStreamWrapper: Fix silly bugs
* mpqapi cleanup
1. Do not rely on stream positions for getting the initial file size.
2. Remove most `seek` calls that were unnecessary.
3. Replace magic numbers with constants.
4. A class to manage archive lifetime and all associated data.
* Revert "Revert "mpqapi: Log all fstream calls""
This reverts commit e08007670f.
* mpqapi: Fix saves on Amiga
Works around https://github.com/bebbo/libnix/issues/30
* mpqapi: Fix logging format specifiers
This fixes fstream logging on Amiga
* mpqapi: Do not ResizeFile unless it was modified
* Add tests for file_util
* mpqapi: Replace malloc/free with new[]/delete[]
* mpqapi: Keep track of size instead of using tellp
This reduces log noise and reliance on `tellp`.
* mpqapi: Refactor mpqapi_write_file_contents
* mpqapi: Ensure we don't seekp beyond EOF on Amiga
* mpqapi: Minor fixes
* mpqapi: Fix FSTREAM_CHECK on Windows
* mpqapi: Remove undefined methods from header
66 lines
1.6 KiB
C++
66 lines
1.6 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#include "file_util.h"
|
|
|
|
namespace dvl {
|
|
namespace {
|
|
|
|
void WriteDummyFile(const char *name, std::uintmax_t size)
|
|
{
|
|
std::ofstream test_file(name, std::ios::out | std::ios::trunc | std::ios::binary);
|
|
ASSERT_FALSE(test_file.fail());
|
|
const char c = '\0';
|
|
for (std::uintmax_t i = 0; i < size; ++i) {
|
|
test_file.write(&c, 1);
|
|
ASSERT_FALSE(test_file.fail());
|
|
}
|
|
}
|
|
|
|
std::string GetTmpPathName(const char *suffix = ".tmp")
|
|
{
|
|
const auto *current_test = ::testing::UnitTest::GetInstance()->current_test_info();
|
|
std::string result = "Test_";
|
|
result.append(current_test->test_case_name());
|
|
result += '_';
|
|
result.append(current_test->name());
|
|
result.append(suffix);
|
|
return result;
|
|
}
|
|
|
|
TEST(FileUtil, GetFileSize)
|
|
{
|
|
const std::string path = GetTmpPathName();
|
|
std::cout << path << std::endl;
|
|
WriteDummyFile(path.c_str(), 42);
|
|
std::uintmax_t result;
|
|
ASSERT_TRUE(GetFileSize(path.c_str(), &result));
|
|
EXPECT_EQ(result, 42);
|
|
}
|
|
|
|
TEST(FileUtil, FileExists)
|
|
{
|
|
EXPECT_FALSE(FileExists("this-file-should-not-exist"));
|
|
const std::string path = GetTmpPathName();
|
|
std::cout << path << std::endl;
|
|
WriteDummyFile(path.c_str(), 42);
|
|
EXPECT_TRUE(FileExists(path.c_str()));
|
|
}
|
|
|
|
TEST(FileUtil, ResizeFile)
|
|
{
|
|
const std::string path = GetTmpPathName();
|
|
std::cout << path << std::endl;
|
|
WriteDummyFile(path.c_str(), 42);
|
|
std::uintmax_t size;
|
|
ASSERT_TRUE(GetFileSize(path.c_str(), &size));
|
|
EXPECT_EQ(size, 42);
|
|
ASSERT_TRUE(ResizeFile(path.c_str(), 30));
|
|
ASSERT_TRUE(GetFileSize(path.c_str(), &size));
|
|
EXPECT_EQ(size, 30);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace dvl
|