devilutionX/test/file_util_test.cpp
Gleb Mazovetskiy 076b0c0c05 Overhaul tests
1. Adds a `libdevilution_so` target when tests are enabled.
2. Each test file is now a separate binary target linked against `libdevilutionx_so` (can now run tests in parallel).
3. Tests are now defined in a separate `test/CMakeLists.txt` file.
4. Building the tests is now controlled by the standard `BUILD_TESTING` option (defined by CTest).
5. Tests are now built by default.
6. On CI, test errors are now reported.

Also:

* `.clang-format`: Enable SortIncludes in tests
* `path_test.cpp`: Fix -Wsign-compare
2021-12-16 20:26:51 +00:00

66 lines
1.6 KiB
C++

#include <gtest/gtest.h>
#include <fstream>
#include <iostream>
#include "utils/file_util.h"
using namespace devilution;
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