From e588b02b172f677cdedda602901055ef243d0935 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 1 Apr 2020 08:42:14 -0700 Subject: [PATCH] Fix posix-mock-test --- include/fmt/os.h | 16 ++++------ test/posix-mock-test.cc | 71 ++++++++++++++++++++++------------------- test/posix-mock.h | 8 +++++ 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/include/fmt/os.h b/include/fmt/os.h index 95fafcf1..f43bb089 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -15,11 +15,10 @@ #include #include // for locale_t +#include #include #include // for strtod_l -#include - #if defined __APPLE__ || defined(__FreeBSD__) # include // for LC_NUMERIC_MASK on OS X #endif @@ -351,12 +350,6 @@ class Locale { # ifdef _WIN32 using locale_t = _locale_t; - enum { LC_NUMERIC_MASK = LC_NUMERIC }; - - static locale_t newlocale(int category_mask, const char* locale, locale_t) { - return _create_locale(category_mask, locale); - } - static void freelocale(locale_t locale) { _free_locale(locale); } static double strtod_l(const char* nptr, char** endptr, _locale_t locale) { @@ -371,7 +364,12 @@ class Locale { Locale(const Locale&) = delete; void operator=(const Locale&) = delete; - Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", nullptr)) { + Locale() { +# ifndef _WIN32 + locale_ = FMT_SYSTEM(newlocale(LC_NUMERIC_MASK, "C", nullptr)); +# else + locale_ = _create_locale(LC_NUMERIC, "C"); +# endif if (!locale_) FMT_THROW(system_error(errno, "cannot create locale")); } ~Locale() { freelocale(locale_); } diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index 6019caa9..acc0e2f5 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -11,13 +11,15 @@ #endif #include "posix-mock.h" -#include "../src/os.cc" #include #include + #include #include +#include "../src/os.cc" + #ifdef _WIN32 # include # undef max @@ -215,10 +217,10 @@ TEST(UtilTest, GetPageSize) { } TEST(FileTest, OpenRetry) { - write_file("test", "there must be something here"); + write_file("temp", "there must be something here"); std::unique_ptr f{nullptr}; - EXPECT_RETRY(f.reset(new file("test", file::RDONLY)), open, - "cannot open file test"); + EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open, + "cannot open file temp"); # ifndef _WIN32 char c = 0; f->read(&c, 1); @@ -230,14 +232,15 @@ TEST(FileTest, CloseNoRetryInDtor) { file::pipe(read_end, write_end); std::unique_ptr f(new file(std::move(read_end))); int saved_close_count = 0; - EXPECT_WRITE(stderr, - { - close_count = 1; - f.reset(nullptr); - saved_close_count = close_count; - close_count = 0; - }, - format_system_error(EINTR, "cannot close file") + "\n"); + EXPECT_WRITE( + stderr, + { + close_count = 1; + f.reset(nullptr); + saved_close_count = close_count; + close_count = 0; + }, + format_system_error(EINTR, "cannot close file") + "\n"); EXPECT_EQ(2, saved_close_count); } @@ -252,8 +255,8 @@ TEST(FileTest, CloseNoRetry) { TEST(FileTest, Size) { std::string content = "top secret, destroy before reading"; - write_file("test", content); - file f("test", file::RDONLY); + write_file("temp", content); + file f("temp", file::RDONLY); EXPECT_GE(f.size(), 0); EXPECT_EQ(content.size(), static_cast(f.size())); # ifdef _WIN32 @@ -270,8 +273,8 @@ TEST(FileTest, Size) { } TEST(FileTest, MaxSize) { - write_file("test", ""); - file f("test", file::RDONLY); + write_file("temp", ""); + file f("temp", file::RDONLY); fstat_sim = MAX_SIZE; EXPECT_GE(f.size(), 0); EXPECT_EQ(max_file_size(), f.size()); @@ -385,10 +388,10 @@ TEST(FileTest, FdopenNoRetry) { } TEST(BufferedFileTest, OpenRetry) { - write_file("test", "there must be something here"); + write_file("temp", "there must be something here"); std::unique_ptr f{nullptr}; - EXPECT_RETRY(f.reset(new buffered_file("test", "r")), fopen, - "cannot open file test"); + EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen, + "cannot open file temp"); # ifndef _WIN32 char c = 0; if (fread(&c, 1, 1, f->get()) < 1) @@ -401,14 +404,15 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) { file::pipe(read_end, write_end); std::unique_ptr f(new buffered_file(read_end.fdopen("r"))); int saved_fclose_count = 0; - EXPECT_WRITE(stderr, - { - fclose_count = 1; - f.reset(nullptr); - saved_fclose_count = fclose_count; - fclose_count = 0; - }, - format_system_error(EINTR, "cannot close file") + "\n"); + EXPECT_WRITE( + stderr, + { + fclose_count = 1; + f.reset(nullptr); + saved_fclose_count = fclose_count; + fclose_count = 0; + }, + format_system_error(EINTR, "cannot close file") + "\n"); EXPECT_EQ(2, saved_fclose_count); } @@ -492,11 +496,6 @@ double _strtod_l(const char* nptr, char** endptr, _locale_t locale) { # define FMT_LOCALE_THROW # endif -LocaleType newlocale(int category_mask, const char* locale, - LocaleType base) FMT_LOCALE_THROW { - return LocaleMock::instance->newlocale(category_mask, locale, base); -} - # if defined(__APPLE__) || \ (defined(__FreeBSD__) && __FreeBSD_version < 1200002) typedef int FreeLocaleResult; @@ -516,12 +515,18 @@ double strtod_l(const char* nptr, char** endptr, # undef FMT_LOCALE_THROW +# ifndef _WIN32 +locale_t test::newlocale(int category_mask, const char* locale, locale_t base) { + return LocaleMock::instance->newlocale(category_mask, locale, base); +} + TEST(LocaleTest, LocaleMock) { ScopedMock mock; LocaleType locale = reinterpret_cast(11); EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale)); - newlocale(222, "foo", locale); + FMT_SYSTEM(newlocale(222, "foo", locale)); } +# endif TEST(LocaleTest, Locale) { # ifndef LC_NUMERIC_MASK diff --git a/test/posix-mock.h b/test/posix-mock.h index 028aeee6..e76af700 100644 --- a/test/posix-mock.h +++ b/test/posix-mock.h @@ -9,7 +9,11 @@ #define FMT_POSIX_TEST_H #include +#include #include +#ifdef __APPLE__ +# include +#endif #ifdef _WIN32 # include @@ -62,6 +66,10 @@ int pipe(int* pfds, unsigned psize, int textmode); FILE* fopen(const char* filename, const char* mode); int fclose(FILE* stream); int(fileno)(FILE* stream); + +#if defined(FMT_LOCALE) && !defined(_WIN32) +locale_t newlocale(int category_mask, const char* locale, locale_t base); +#endif } // namespace test #define FMT_SYSTEM(call) test::call