Move FMT_STATIC_ASSERT to header and test.

This commit is contained in:
Victor Zverovich 2014-09-12 12:05:32 -07:00
parent ea9989b254
commit cb7caa540f
4 changed files with 28 additions and 28 deletions

View File

@ -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.

14
posix.h
View File

@ -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

View File

@ -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\");")

View File

@ -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);