diff --git a/CMakeLists.txt b/CMakeLists.txt index 798e022f..2567ec26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,8 +82,8 @@ enable_testing() include_directories(.) -add_library(test-main - test/test-main.cc test/gtest-extra.cc test/gtest-extra.h) +set(TEST_MAIN_SRC test/test-main.cc test/gtest-extra.cc test/gtest-extra.h) +add_library(test-main ${TEST_MAIN_SRC}) target_link_libraries(test-main gtest format) cxx_test(gtest-extra-test test-main) @@ -96,6 +96,14 @@ if (HAVE_STD_CPP11_FLAG) set_target_properties(format-test PROPERTIES COMPILE_FLAGS "-std=c++11") endif () +if (HAVE_OPEN) + add_executable(posix-test test/posix-test.cc ${TEST_MAIN_SRC}) + set_target_properties(posix-test + PROPERTIES COMPILE_DEFINITIONS "FMT_INCLUDE_POSIX_TEST=1") + target_link_libraries(posix-test gtest format) + add_test(posix-test posix-test) +endif () + add_test(compile-test ${CMAKE_CTEST_COMMAND} --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test" diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 81af62bd..dfa13364 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -348,6 +348,12 @@ TEST(StreamingAssertionsTest, EXPECT_WRITE) { << "expected failure", "expected failure"); } +TEST(UtilTest, FormatSystemErrorMessage) { + fmt::Writer out; + fmt::internal::FormatSystemErrorMessage(out, EDOM, "test message"); + EXPECT_EQ(out.str(), FormatSystemErrorMessage(EDOM, "test message")); +} + #if FMT_USE_FILE_DESCRIPTORS // Checks if the file is open by reading one character from it. @@ -800,8 +806,6 @@ TEST(OutputRedirectTest, ErrorInDtor) { write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail } -// TODO: test retry on EINTR, test FormatSystemErrorMessage - #endif // FMT_USE_FILE_DESCRIPTORS } // namespace diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 5eec3607..5486aef7 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -46,6 +46,10 @@ #endif // _WIN32 +#ifdef FMT_INCLUDE_POSIX_TEST +# include "posix-test.h" +#endif + // Retries the expression while it evaluates to -1 and error equals to EINTR. #define FMT_RETRY(result, expression) \ do { \ @@ -53,7 +57,6 @@ } while (result == -1 && errno == EINTR) namespace { -// TODO: test #ifdef _WIN32 // On Windows the count argument to read and write is unsigned, so convert // it from size_t preventing integer overflow. diff --git a/test/posix-test.cc b/test/posix-test.cc new file mode 100644 index 00000000..e714d987 --- /dev/null +++ b/test/posix-test.cc @@ -0,0 +1,69 @@ +/* + Test wrappers around POSIX functions. + + Copyright (c) 2012-2014, Victor Zverovich + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix-test.h" + +#undef open + +#include +#include + +#include "gtest-extra.h" + +namespace { +int open_count; +} + +#ifndef _WIN32 +int test::open(const char *path, int oflag, int mode) { + if ((open_count++ % 3) == 2) + return ::open(path, oflag, mode); + errno = EINTR; + return -1; +} +#else +errno_t test::sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode) { + return _sopen_s(pfh, filename, oflag, shflag, pmode); +} +#endif + +#ifndef _WIN32 +# define EXPECT_RETRY(statement, func, message) \ + func##_count = 0; \ + statement; \ + EXPECT_EQ(3, func##_count); +#else +# define EXPECT_RETRY(statement, func, message) \ + EXPECT_SYSTEM_ERROR(statement, EINTR, message); +#endif + +TEST(FileTest, OpenRetry) { + EXPECT_RETRY(File file("CMakeLists.txt", File::RDONLY), + open, "cannot open file CMakeLists.txt"); +} + +// TODO: test retry on EINTR diff --git a/test/posix-test.h b/test/posix-test.h new file mode 100644 index 00000000..89924f95 --- /dev/null +++ b/test/posix-test.h @@ -0,0 +1,45 @@ +/* + Test wrappers around POSIX functions. + + Copyright (c) 2012-2014, Victor Zverovich + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FMT_POSIX_TEST_H +#define FMT_POSIX_TEST_H + +#include + +namespace test { + +#ifndef _WIN32 +int open(const char *path, int oflag, int mode); +#define open test::open +#else +errno_t sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode); +#define _sopen_s test::sopen_s +#endif + +} // namespace test + +#endif // FMT_POSIX_TEST_H