diff --git a/posix.cc b/posix.cc index 2511f24d..302c78ad 100644 --- a/posix.cc +++ b/posix.cc @@ -54,20 +54,6 @@ #endif // _WIN32 -#if FMT_GCC_VERSION >= 407 -# define FMT_UNUSED __attribute__((unused)) -#else -# define FMT_UNUSED -#endif - -#if FMT_USE_STATIC_ASSERT -# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message) -#else -# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b) -# define FMT_STATIC_ASSERT(cond, message) \ - typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED -#endif - namespace { #ifdef _WIN32 // Return type of read and write functions. diff --git a/posix.h b/posix.h index 96a4e973..6a48adb6 100644 --- a/posix.h +++ b/posix.h @@ -62,6 +62,20 @@ # endif #endif +#if FMT_GCC_VERSION >= 407 +# define FMT_UNUSED __attribute__((unused)) +#else +# define FMT_UNUSED +#endif + +#if FMT_USE_STATIC_ASSERT +# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message) +#else +# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b) +# define FMT_STATIC_ASSERT(cond, message) \ + typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED +#endif + // Retries the expression while it evaluates to error_result and errno // equals to EINTR. #ifndef _WIN32 diff --git a/test/compile-test/CMakeLists.txt b/test/compile-test/CMakeLists.txt index c67441b5..f8f9e04a 100644 --- a/test/compile-test/CMakeLists.txt +++ b/test/compile-test/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../..) function (expect_compile_error code) check_cxx_source_compiles(" #include \"format.cc\" + #include \"posix.h\" int main() { ${code} } @@ -44,3 +45,5 @@ expect_compile_error("fmt::Writer() << fmt::pad(42, 5, L' ');") # Formatting a wide character with a narrow format string is forbidden. expect_compile_error("fmt::format(\"{}\", L'a';") + +expect_compile_error("FMT_STATIC_ASSERT(0 > 1, \"oops\");") diff --git a/test/posix-test.cc b/test/posix-test.cc index c995de06..5d5bdeb0 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -56,14 +56,6 @@ int fileno_count; std::size_t read_nbyte; std::size_t write_nbyte; bool increase_file_size; - -std::string TEST_FILE_CONTENT = "top secret, destroy before reading"; - -void WriteTestFile() { - BufferedFile bf("test", "w"); - bf.print(TEST_FILE_CONTENT); - bf.close(); -} } #define EMULATE_EINTR(func, error_result) \ @@ -183,7 +175,13 @@ int test::fileno(FILE *stream) { void write_file(fmt::StringRef filename, fmt::StringRef content) { fmt::BufferedFile f(filename, "w"); - fmt::print(f.get(), "{}", content); + f.print("{}", content); +} + +TEST(UtilTest, StaticAssert) { + FMT_STATIC_ASSERT(true, "success"); + // Static assertion failure is tested in compile-test because it causes + // a compile-time error. } TEST(FileTest, OpenRetry) { @@ -222,15 +220,16 @@ TEST(FileTest, CloseNoRetry) { } TEST(FileTest, Size) { - WriteTestFile(); + std::string content = "top secret, destroy before reading"; + write_file("test", content); File f("test", File::RDONLY); - EXPECT_EQ(TEST_FILE_CONTENT.size(), f.size()); + EXPECT_EQ(content.size(), f.size()); f.close(); EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes"); } TEST(FileTest, MaxSize) { - WriteTestFile(); + write_file("test", ""); File f("test", File::RDONLY); increase_file_size = true; EXPECT_GE(f.size(), 0); @@ -238,8 +237,6 @@ TEST(FileTest, MaxSize) { increase_file_size = false; } -// TODO: test FMT_STATIC_ASSERT - TEST(FileTest, ReadRetry) { File read_end, write_end; File::pipe(read_end, write_end);