mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-11 21:38:05 +00:00
Fix posix-mock-test
This commit is contained in:
parent
1a62711d01
commit
e588b02b17
@ -15,11 +15,10 @@
|
|||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <clocale> // for locale_t
|
#include <clocale> // for locale_t
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib> // for strtod_l
|
#include <cstdlib> // for strtod_l
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
#if defined __APPLE__ || defined(__FreeBSD__)
|
#if defined __APPLE__ || defined(__FreeBSD__)
|
||||||
# include <xlocale.h> // for LC_NUMERIC_MASK on OS X
|
# include <xlocale.h> // for LC_NUMERIC_MASK on OS X
|
||||||
#endif
|
#endif
|
||||||
@ -351,12 +350,6 @@ class Locale {
|
|||||||
# ifdef _WIN32
|
# ifdef _WIN32
|
||||||
using locale_t = _locale_t;
|
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 void freelocale(locale_t locale) { _free_locale(locale); }
|
||||||
|
|
||||||
static double strtod_l(const char* nptr, char** endptr, _locale_t locale) {
|
static double strtod_l(const char* nptr, char** endptr, _locale_t locale) {
|
||||||
@ -371,7 +364,12 @@ class Locale {
|
|||||||
Locale(const Locale&) = delete;
|
Locale(const Locale&) = delete;
|
||||||
void operator=(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"));
|
if (!locale_) FMT_THROW(system_error(errno, "cannot create locale"));
|
||||||
}
|
}
|
||||||
~Locale() { freelocale(locale_); }
|
~Locale() { freelocale(locale_); }
|
||||||
|
@ -11,13 +11,15 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "posix-mock.h"
|
#include "posix-mock.h"
|
||||||
#include "../src/os.cc"
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "../src/os.cc"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
# undef max
|
# undef max
|
||||||
@ -215,10 +217,10 @@ TEST(UtilTest, GetPageSize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, OpenRetry) {
|
TEST(FileTest, OpenRetry) {
|
||||||
write_file("test", "there must be something here");
|
write_file("temp", "there must be something here");
|
||||||
std::unique_ptr<file> f{nullptr};
|
std::unique_ptr<file> f{nullptr};
|
||||||
EXPECT_RETRY(f.reset(new file("test", file::RDONLY)), open,
|
EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open,
|
||||||
"cannot open file test");
|
"cannot open file temp");
|
||||||
# ifndef _WIN32
|
# ifndef _WIN32
|
||||||
char c = 0;
|
char c = 0;
|
||||||
f->read(&c, 1);
|
f->read(&c, 1);
|
||||||
@ -230,14 +232,15 @@ TEST(FileTest, CloseNoRetryInDtor) {
|
|||||||
file::pipe(read_end, write_end);
|
file::pipe(read_end, write_end);
|
||||||
std::unique_ptr<file> f(new file(std::move(read_end)));
|
std::unique_ptr<file> f(new file(std::move(read_end)));
|
||||||
int saved_close_count = 0;
|
int saved_close_count = 0;
|
||||||
EXPECT_WRITE(stderr,
|
EXPECT_WRITE(
|
||||||
{
|
stderr,
|
||||||
close_count = 1;
|
{
|
||||||
f.reset(nullptr);
|
close_count = 1;
|
||||||
saved_close_count = close_count;
|
f.reset(nullptr);
|
||||||
close_count = 0;
|
saved_close_count = close_count;
|
||||||
},
|
close_count = 0;
|
||||||
format_system_error(EINTR, "cannot close file") + "\n");
|
},
|
||||||
|
format_system_error(EINTR, "cannot close file") + "\n");
|
||||||
EXPECT_EQ(2, saved_close_count);
|
EXPECT_EQ(2, saved_close_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,8 +255,8 @@ TEST(FileTest, CloseNoRetry) {
|
|||||||
|
|
||||||
TEST(FileTest, Size) {
|
TEST(FileTest, Size) {
|
||||||
std::string content = "top secret, destroy before reading";
|
std::string content = "top secret, destroy before reading";
|
||||||
write_file("test", content);
|
write_file("temp", content);
|
||||||
file f("test", file::RDONLY);
|
file f("temp", file::RDONLY);
|
||||||
EXPECT_GE(f.size(), 0);
|
EXPECT_GE(f.size(), 0);
|
||||||
EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
|
EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
|
||||||
# ifdef _WIN32
|
# ifdef _WIN32
|
||||||
@ -270,8 +273,8 @@ TEST(FileTest, Size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, MaxSize) {
|
TEST(FileTest, MaxSize) {
|
||||||
write_file("test", "");
|
write_file("temp", "");
|
||||||
file f("test", file::RDONLY);
|
file f("temp", file::RDONLY);
|
||||||
fstat_sim = MAX_SIZE;
|
fstat_sim = MAX_SIZE;
|
||||||
EXPECT_GE(f.size(), 0);
|
EXPECT_GE(f.size(), 0);
|
||||||
EXPECT_EQ(max_file_size(), f.size());
|
EXPECT_EQ(max_file_size(), f.size());
|
||||||
@ -385,10 +388,10 @@ TEST(FileTest, FdopenNoRetry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, OpenRetry) {
|
TEST(BufferedFileTest, OpenRetry) {
|
||||||
write_file("test", "there must be something here");
|
write_file("temp", "there must be something here");
|
||||||
std::unique_ptr<buffered_file> f{nullptr};
|
std::unique_ptr<buffered_file> f{nullptr};
|
||||||
EXPECT_RETRY(f.reset(new buffered_file("test", "r")), fopen,
|
EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen,
|
||||||
"cannot open file test");
|
"cannot open file temp");
|
||||||
# ifndef _WIN32
|
# ifndef _WIN32
|
||||||
char c = 0;
|
char c = 0;
|
||||||
if (fread(&c, 1, 1, f->get()) < 1)
|
if (fread(&c, 1, 1, f->get()) < 1)
|
||||||
@ -401,14 +404,15 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
|||||||
file::pipe(read_end, write_end);
|
file::pipe(read_end, write_end);
|
||||||
std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
|
std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
|
||||||
int saved_fclose_count = 0;
|
int saved_fclose_count = 0;
|
||||||
EXPECT_WRITE(stderr,
|
EXPECT_WRITE(
|
||||||
{
|
stderr,
|
||||||
fclose_count = 1;
|
{
|
||||||
f.reset(nullptr);
|
fclose_count = 1;
|
||||||
saved_fclose_count = fclose_count;
|
f.reset(nullptr);
|
||||||
fclose_count = 0;
|
saved_fclose_count = fclose_count;
|
||||||
},
|
fclose_count = 0;
|
||||||
format_system_error(EINTR, "cannot close file") + "\n");
|
},
|
||||||
|
format_system_error(EINTR, "cannot close file") + "\n");
|
||||||
EXPECT_EQ(2, saved_fclose_count);
|
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
|
# define FMT_LOCALE_THROW
|
||||||
# endif
|
# 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__) || \
|
# if defined(__APPLE__) || \
|
||||||
(defined(__FreeBSD__) && __FreeBSD_version < 1200002)
|
(defined(__FreeBSD__) && __FreeBSD_version < 1200002)
|
||||||
typedef int FreeLocaleResult;
|
typedef int FreeLocaleResult;
|
||||||
@ -516,12 +515,18 @@ double strtod_l(const char* nptr, char** endptr,
|
|||||||
|
|
||||||
# undef FMT_LOCALE_THROW
|
# 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) {
|
TEST(LocaleTest, LocaleMock) {
|
||||||
ScopedMock<LocaleMock> mock;
|
ScopedMock<LocaleMock> mock;
|
||||||
LocaleType locale = reinterpret_cast<LocaleType>(11);
|
LocaleType locale = reinterpret_cast<LocaleType>(11);
|
||||||
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
|
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
|
||||||
newlocale(222, "foo", locale);
|
FMT_SYSTEM(newlocale(222, "foo", locale));
|
||||||
}
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
TEST(LocaleTest, Locale) {
|
TEST(LocaleTest, Locale) {
|
||||||
# ifndef LC_NUMERIC_MASK
|
# ifndef LC_NUMERIC_MASK
|
||||||
|
@ -9,7 +9,11 @@
|
|||||||
#define FMT_POSIX_TEST_H
|
#define FMT_POSIX_TEST_H
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <locale.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#ifdef __APPLE__
|
||||||
|
# include <xlocale.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
@ -62,6 +66,10 @@ int pipe(int* pfds, unsigned psize, int textmode);
|
|||||||
FILE* fopen(const char* filename, const char* mode);
|
FILE* fopen(const char* filename, const char* mode);
|
||||||
int fclose(FILE* stream);
|
int fclose(FILE* stream);
|
||||||
int(fileno)(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
|
} // namespace test
|
||||||
|
|
||||||
#define FMT_SYSTEM(call) test::call
|
#define FMT_SYSTEM(call) test::call
|
||||||
|
Loading…
Reference in New Issue
Block a user