mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Move FMT_STATIC_ASSERT to header and test.
This commit is contained in:
14
posix.cc
14
posix.cc
@ -54,20 +54,6 @@
|
|||||||
|
|
||||||
#endif // _WIN32
|
#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 {
|
namespace {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Return type of read and write functions.
|
// Return type of read and write functions.
|
||||||
|
14
posix.h
14
posix.h
@ -62,6 +62,20 @@
|
|||||||
# endif
|
# endif
|
||||||
#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
|
// Retries the expression while it evaluates to error_result and errno
|
||||||
// equals to EINTR.
|
// equals to EINTR.
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
@ -8,6 +8,7 @@ set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../..)
|
|||||||
function (expect_compile_error code)
|
function (expect_compile_error code)
|
||||||
check_cxx_source_compiles("
|
check_cxx_source_compiles("
|
||||||
#include \"format.cc\"
|
#include \"format.cc\"
|
||||||
|
#include \"posix.h\"
|
||||||
int main() {
|
int main() {
|
||||||
${code}
|
${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.
|
# Formatting a wide character with a narrow format string is forbidden.
|
||||||
expect_compile_error("fmt::format(\"{}\", L'a';")
|
expect_compile_error("fmt::format(\"{}\", L'a';")
|
||||||
|
|
||||||
|
expect_compile_error("FMT_STATIC_ASSERT(0 > 1, \"oops\");")
|
||||||
|
@ -56,14 +56,6 @@ int fileno_count;
|
|||||||
std::size_t read_nbyte;
|
std::size_t read_nbyte;
|
||||||
std::size_t write_nbyte;
|
std::size_t write_nbyte;
|
||||||
bool increase_file_size;
|
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) \
|
#define EMULATE_EINTR(func, error_result) \
|
||||||
@ -183,7 +175,13 @@ int test::fileno(FILE *stream) {
|
|||||||
|
|
||||||
void write_file(fmt::StringRef filename, fmt::StringRef content) {
|
void write_file(fmt::StringRef filename, fmt::StringRef content) {
|
||||||
fmt::BufferedFile f(filename, "w");
|
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) {
|
TEST(FileTest, OpenRetry) {
|
||||||
@ -222,15 +220,16 @@ TEST(FileTest, CloseNoRetry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, Size) {
|
TEST(FileTest, Size) {
|
||||||
WriteTestFile();
|
std::string content = "top secret, destroy before reading";
|
||||||
|
write_file("test", content);
|
||||||
File f("test", File::RDONLY);
|
File f("test", File::RDONLY);
|
||||||
EXPECT_EQ(TEST_FILE_CONTENT.size(), f.size());
|
EXPECT_EQ(content.size(), f.size());
|
||||||
f.close();
|
f.close();
|
||||||
EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
|
EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, MaxSize) {
|
TEST(FileTest, MaxSize) {
|
||||||
WriteTestFile();
|
write_file("test", "");
|
||||||
File f("test", File::RDONLY);
|
File f("test", File::RDONLY);
|
||||||
increase_file_size = true;
|
increase_file_size = true;
|
||||||
EXPECT_GE(f.size(), 0);
|
EXPECT_GE(f.size(), 0);
|
||||||
@ -238,8 +237,6 @@ TEST(FileTest, MaxSize) {
|
|||||||
increase_file_size = false;
|
increase_file_size = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test FMT_STATIC_ASSERT
|
|
||||||
|
|
||||||
TEST(FileTest, ReadRetry) {
|
TEST(FileTest, ReadRetry) {
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
|
Reference in New Issue
Block a user