0066ad2d38
9e554999c Update version b34d92b05 Bump version d39ece187 Make rst2md runnable and update changelog fe2d715ff Update changelog 27b306701 Update changelog 68837079a Update changelog c98b202eb Update changelog 587a7f663 Update changelog and docs 84e5170c9 Update changelog and deprecate visit 130e412b6 Update changelog and docs 0bbdca5b8 Fix conversion warnings (#989) 77a724480 Implement fill/align/width for strftime-like formatting 3e01376e0 Implement fill/align/width parsing in chrono formatter 1f92f8a9d Remove noexcept 8668639ae Get rid of null_terminating_iterator in format 93fd473b8 Add support for builtin terminal colors. (#974) 61ad543c3 Windows .sln filename changed from FORMAT to FMT 7f7504b3f Clean up docs 37f599b1a Fix docs 8c2e15aed Make printf work in search (#164) de71db6d4 Fix asan error (#977) b180b3915 Fix default formatting 24594c747 Disable printing the reset escape code when no style modifiers where applied. (#973) b0f222471 Implement default chrono formatting 749276072 Add file stream support for stylized text printing. (#967) f54f3d0fb Move chrono-specific code to a separate header bf1f1c73e Fix time test b6bc6ec24 Add default ctor and fix use of constexpr macros in text_style acfa95d4a Workaround a bug in MSVC's strftime (#965) 628f83058 More chrono formatting aa3b5aba4 Implement locale-specific minute formatting 639de2175 Workaround more MSVC bugs 3242ddf7b Fix warnings bd1104046 Workaround a bug in MSVC 81b5c4a5f Add experimental emphasis support (#961) 7c4eb0fbe Fix warnings in time.h 2d624218b Fix another warning b31680990 Fix a warning b10ccb83e Add rpclib to projects 0497875ff Stop the orgy of casts 37dc495b9 Simplify MSVC workaround 2ff4996d0 Fix ambiguous complier error C2666 in vs2017.The '+' opeator may cause ambiguity.Avoid implicit conversion. 77656c672 Fix sign-conversion warnings reported by Clang7 ea5e4790b Fix formatting 86681c4bb Update README.rst e867768ee Do not override user provided compile flag 0c7f5c3ca Update README.rst e7e2ab107 Make return type of basic_format_args::max_size() consistent. 29352af36 Update README.rst 68214bd90 More time formatting bcf3fcd67 Clean up bit fiddling for argument packing 9dcf127fa Workaround a bogus MSVC warning b8b06e3e1 Fix conversion warnings in Grisu 322b2594e Implement more time specifiers 0835f1ba3 Use full paths for fmt.pc.in a084495d7 Add Ceph to projects fa1d4dbcf Fix warnings 2b2cfdac1 Update docs 99744f8f8 Suppress unfixable warning f5fe84923 Specialize formatter for chrono durations a5a9805a9 First stub at the datetime format parser 645c76a9a Fix dummy warnings fecb2d6f0 Eliminate msvc compiler warnings (#931) 64690d3a9 Add context_base::arg() 01640f44c Fully qualify dummy_int (#941) e37d6a984 add make_printf_args and make_wprintf_args functions (#934) 982ee5c69 parse_context -> format_parse_context b7b854855 thousands_sep -> thousands_sep_impl (#939) 00a8cc832 Fix formatting 33fbb3a7e Fix remaining linker errors. bd6121596 Disable fmt-impl-test in windows + shared lib. 702b3d161 Fix link error in windows with shared library. 9d4ef9435 Install pdb files. 6c95fb356 Default Context to format_context 16b78ee62 fix incompatibilities with c++2a mode in clang 19e008876 More locale support f2ee98810 Improve locale support 1385050e2 More formatter tests 03c1b110a Fix gcc 4.4 build cc805c616 Test enabled formatters e01579231 Disallow leading zeros in arg-id 34030deca Cleanup warning flags 6b26e3f2d Manifest & Gradle comment d286c9775 Update for Gradle build d951f6dfe Get latest Gradle (ver. 4.10.2) a23d59247 Fix check_format_string (#925) 36161284e Update docs 38f355d87 Revert "find sphinx-build before calling build.py" 324eac1aa Make locales work with any character type bdda4d603 Simplify compile-time strings 5ee1a4bc8 check for property 'mutable iterator' and SFINAE on it 2dea780fb change type naming and fix sfinae bug b98e8301d add non-char support for compile-time format check ccd3e8bbf Make is_constructible public (#918) 437315380 Update usage.rst 73cfd8f32 Fix colored print ec384302d additional test for print with background color 0a96c032b Parameterize v*printf on string type (#920) 61e6d2e38 Fix core version of vformat_to ea4010d70 Merge has_to_string_view into is_string 486fff597 Add sprintf_format instantiations and remove syntactic noise 1e3dcbba8 fix: 'format_to_n' compiles 'std::back_inserter' arguments f0328f8e3 Use char_traits::length in string_view ctor (#914) 895fb9845 Disallow gcc 4.4 failures 20c708bf6 Fix build on gcc 4.4 9d0c9c4bb cmake: output share/fmt.pc 2d2326a76 Fix compilation with older gcc 1ec027230 Get rid of FMT_UNION 2c81c851b Adapt any string-like type to be used by {fmt} just like the standard string types already supported. The adaption is totally non-intrusive. 846c644e8 Workeround broken sprintf in MSVC 13d472bd8 Compute output size for grisu b71d3fe7a Remove use_grisu 847abb6f8 Fix test dda47c946 Merge min_digits and max_digits 292462215 Fix naming of basic_format_specs members bda5f9a55 Replace grisu2_specs with core_format_specs b1ca608ba Remove unused empty_spec e8efdef8d Avoid extra copy 98f1c1fe8 Remove unused code 50b18a3c1 Integrate Grisu 699297520 Implement Grisu rounding 4bb76ef0c Remove redundant definition of print ddd7caf38 Fix locale-dependent formatting (#905) 10e03e695 use found python executable for launching sphinx-build 07200f445 find sphinx-build before calling build.py 08a65c228 Workaround broken constexpr in MSVC2017 167f8fe32 Fix a typo in api.rst 57983423c Remove signbit workaround 7bebb3e12 Clarify overload resolution in docs 939fbe556 Remove basic_fixed_buffer. 61f81a071 minor documentation corrections f27defc63 Parameterize printf functions on the type of the format string. 6a685571d Make 'std::*::basic_string_view' a valid argument type for 'format_str' parameters. 87a0408c6 Fix ostream.h build 2b5acad4a Remove redundant size argument to write_padded 655ce5338 is_format_string -> is_string fea712abb Parameterize ostream functions on the type of the format string. f16a118e8 Fix non-matching char types. 041bf83d9 Improve fmt::format readability 229903239 Document how to write a formatter for a type hierarchy f5480635c visit -> visit_format_arg cdf3fa08d Put related code together in fmt/core.h 38325248e Count width in code points (#628) deb901b9e Parameterize core functions on the type of the format string. 0f98de301 Update docs c797708fc Workaround strlen being non-constexpr in ARM toolchain 49b4c1e9d Update docs 63a87beba Add to_string_view 4e0c31465 checked_format_args -> checked_args c3538a1ee Simplify variadic functions further 2d7d0835d Simplify variadic functions 3f4cfa6c6 Implement UTF-8 string support f8027414f Impelement char8_t support 76a47d41c Cleanup the use of FMT_CHAR 267fdc7a1 Parameterize core functions on the type of the format string. 5bced1242 Parameterize more functions on string type 674999c52 fix vs2017 warning fmt::v5::localtime 'not all control paths return a value'. e4fea22d1 Make char8_t a strongly-typed enum 66992e90d Clarify that writing to memory_buffer appends (#877) e864acfdb Fix compilation with intel compilers (ICC/ICPC) v14.0 4cf21f58b constrain templated format_to on proper format string type. d7f17613f Fix compilation on platforms with exotic double (#878) e4ca37ccf Parameterize format_to on string type (#880) d66fa2216 Reduce syntactic noise 48e6dcd0f Implement workarounds for gcc 4.4 0ea3221d3 Remove is_named_arg and add FMT_CHAR 73c53d783 Parameterize 'printf(rgb color, ...)' and 'vprint_rgb(rgb color, ...)' on the type of the format string. d41be23ac Simplify string_view detection 2def9e4c8 Remove FMT_DTOR_NOEXCEPT ff6e46ed9 More cleanup 715f2b4c0 Remove require_wchar and internalize no_formatter_error ec0cdc46f Workaround Windows slowness git-subtree-dir: externals/fmt git-subtree-split: 9e554999ce02cf86fcdfe74fe740c4fe3f5a56d5
539 lines
13 KiB
C++
539 lines
13 KiB
C++
// Tests of the C++ interface to POSIX functions that require mocks
|
|
//
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
// All rights reserved.
|
|
//
|
|
// For the license information refer to format.h.
|
|
|
|
// Disable bogus MSVC warnings.
|
|
#ifdef _MSC_VER
|
|
# define _CRT_SECURE_NO_WARNINGS
|
|
#endif
|
|
|
|
#include "posix-mock.h"
|
|
#include "../src/posix.cc"
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <climits>
|
|
|
|
#ifdef _WIN32
|
|
# include <io.h>
|
|
# undef max
|
|
# undef ERROR
|
|
#endif
|
|
|
|
#include "gmock.h"
|
|
#include "gtest-extra.h"
|
|
#include "util.h"
|
|
|
|
using fmt::buffered_file;
|
|
using fmt::error_code;
|
|
using fmt::file;
|
|
|
|
using testing::internal::scoped_ptr;
|
|
using testing::_;
|
|
using testing::StrEq;
|
|
using testing::Return;
|
|
|
|
namespace {
|
|
int open_count;
|
|
int close_count;
|
|
int dup_count;
|
|
int dup2_count;
|
|
int fdopen_count;
|
|
int read_count;
|
|
int write_count;
|
|
int pipe_count;
|
|
int fopen_count;
|
|
int fclose_count;
|
|
int fileno_count;
|
|
std::size_t read_nbyte;
|
|
std::size_t write_nbyte;
|
|
bool sysconf_error;
|
|
|
|
enum FStatSimulation { NONE, MAX_SIZE, ERROR } fstat_sim;
|
|
}
|
|
|
|
#define EMULATE_EINTR(func, error_result) \
|
|
if (func##_count != 0) { \
|
|
if (func##_count++ != 3) { \
|
|
errno = EINTR; \
|
|
return error_result; \
|
|
} \
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
int test::open(const char *path, int oflag, int mode) {
|
|
EMULATE_EINTR(open, -1);
|
|
return ::open(path, oflag, mode);
|
|
}
|
|
#else
|
|
errno_t test::sopen_s(
|
|
int* pfh, const char *filename, int oflag, int shflag, int pmode) {
|
|
EMULATE_EINTR(open, EINTR);
|
|
return _sopen_s(pfh, filename, oflag, shflag, pmode);
|
|
}
|
|
#endif
|
|
|
|
#ifndef _WIN32
|
|
|
|
long test::sysconf(int name) {
|
|
long result = ::sysconf(name);
|
|
if (!sysconf_error)
|
|
return result;
|
|
// Simulate an error.
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
static off_t max_file_size() { return std::numeric_limits<off_t>::max(); }
|
|
|
|
int test::fstat(int fd, struct stat *buf) {
|
|
int result = ::fstat(fd, buf);
|
|
if (fstat_sim == MAX_SIZE)
|
|
buf->st_size = max_file_size();
|
|
return result;
|
|
}
|
|
|
|
#else
|
|
|
|
static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); }
|
|
|
|
DWORD test::GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
|
|
if (fstat_sim == ERROR) {
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
return INVALID_FILE_SIZE;
|
|
}
|
|
if (fstat_sim == MAX_SIZE) {
|
|
DWORD max = std::numeric_limits<DWORD>::max();
|
|
*lpFileSizeHigh = max >> 1;
|
|
return max;
|
|
}
|
|
return ::GetFileSize(hFile, lpFileSizeHigh);
|
|
}
|
|
|
|
#endif
|
|
|
|
int test::close(int fildes) {
|
|
// Close the file first because close shouldn't be retried.
|
|
int result = ::FMT_POSIX(close(fildes));
|
|
EMULATE_EINTR(close, -1);
|
|
return result;
|
|
}
|
|
|
|
int test::dup(int fildes) {
|
|
EMULATE_EINTR(dup, -1);
|
|
return ::FMT_POSIX(dup(fildes));
|
|
}
|
|
|
|
int test::dup2(int fildes, int fildes2) {
|
|
EMULATE_EINTR(dup2, -1);
|
|
return ::FMT_POSIX(dup2(fildes, fildes2));
|
|
}
|
|
|
|
FILE *test::fdopen(int fildes, const char *mode) {
|
|
EMULATE_EINTR(fdopen, FMT_NULL);
|
|
return ::FMT_POSIX(fdopen(fildes, mode));
|
|
}
|
|
|
|
test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
|
|
read_nbyte = nbyte;
|
|
EMULATE_EINTR(read, -1);
|
|
return ::FMT_POSIX(read(fildes, buf, nbyte));
|
|
}
|
|
|
|
test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
|
|
write_nbyte = nbyte;
|
|
EMULATE_EINTR(write, -1);
|
|
return ::FMT_POSIX(write(fildes, buf, nbyte));
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
int test::pipe(int fildes[2]) {
|
|
EMULATE_EINTR(pipe, -1);
|
|
return ::pipe(fildes);
|
|
}
|
|
#else
|
|
int test::pipe(int *pfds, unsigned psize, int textmode) {
|
|
EMULATE_EINTR(pipe, -1);
|
|
return _pipe(pfds, psize, textmode);
|
|
}
|
|
#endif
|
|
|
|
FILE *test::fopen(const char *filename, const char *mode) {
|
|
EMULATE_EINTR(fopen, FMT_NULL);
|
|
return ::fopen(filename, mode);
|
|
}
|
|
|
|
int test::fclose(FILE *stream) {
|
|
EMULATE_EINTR(fclose, EOF);
|
|
return ::fclose(stream);
|
|
}
|
|
|
|
int (test::fileno)(FILE *stream) {
|
|
EMULATE_EINTR(fileno, -1);
|
|
#ifdef fileno
|
|
return FMT_POSIX(fileno(stream));
|
|
#else
|
|
return ::FMT_POSIX(fileno(stream));
|
|
#endif
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
# define EXPECT_RETRY(statement, func, message) \
|
|
func##_count = 1; \
|
|
statement; \
|
|
EXPECT_EQ(4, func##_count); \
|
|
func##_count = 0;
|
|
# define EXPECT_EQ_POSIX(expected, actual) EXPECT_EQ(expected, actual)
|
|
#else
|
|
# define EXPECT_RETRY(statement, func, message) \
|
|
func##_count = 1; \
|
|
EXPECT_SYSTEM_ERROR(statement, EINTR, message); \
|
|
func##_count = 0;
|
|
# define EXPECT_EQ_POSIX(expected, actual)
|
|
#endif
|
|
|
|
static void write_file(fmt::cstring_view filename, fmt::string_view content) {
|
|
fmt::buffered_file f(filename, "w");
|
|
f.print("{}", content);
|
|
}
|
|
|
|
TEST(UtilTest, GetPageSize) {
|
|
#ifdef _WIN32
|
|
SYSTEM_INFO si = {};
|
|
GetSystemInfo(&si);
|
|
EXPECT_EQ(si.dwPageSize, fmt::getpagesize());
|
|
#else
|
|
EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize());
|
|
sysconf_error = true;
|
|
EXPECT_SYSTEM_ERROR(
|
|
fmt::getpagesize(), EINVAL, "cannot get memory page size");
|
|
sysconf_error = false;
|
|
#endif
|
|
}
|
|
|
|
TEST(FileTest, OpenRetry) {
|
|
write_file("test", "there must be something here");
|
|
scoped_ptr<file> f{FMT_NULL};
|
|
EXPECT_RETRY(f.reset(new file("test", file::RDONLY)),
|
|
open, "cannot open file test");
|
|
#ifndef _WIN32
|
|
char c = 0;
|
|
f->read(&c, 1);
|
|
#endif
|
|
}
|
|
|
|
TEST(FileTest, CloseNoRetryInDtor) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
scoped_ptr<file> f(new file(std::move(read_end)));
|
|
int saved_close_count = 0;
|
|
EXPECT_WRITE(stderr, {
|
|
close_count = 1;
|
|
f.reset(FMT_NULL);
|
|
saved_close_count = close_count;
|
|
close_count = 0;
|
|
}, format_system_error(EINTR, "cannot close file") + "\n");
|
|
EXPECT_EQ(2, saved_close_count);
|
|
}
|
|
|
|
TEST(FileTest, CloseNoRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
close_count = 1;
|
|
EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file");
|
|
EXPECT_EQ(2, close_count);
|
|
close_count = 0;
|
|
}
|
|
|
|
TEST(FileTest, Size) {
|
|
std::string content = "top secret, destroy before reading";
|
|
write_file("test", content);
|
|
file f("test", file::RDONLY);
|
|
EXPECT_GE(f.size(), 0);
|
|
EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
|
|
#ifdef _WIN32
|
|
fmt::memory_buffer message;
|
|
fmt::internal::format_windows_error(
|
|
message, ERROR_ACCESS_DENIED, "cannot get file size");
|
|
fstat_sim = ERROR;
|
|
EXPECT_THROW_MSG(f.size(), fmt::windows_error, fmt::to_string(message));
|
|
fstat_sim = NONE;
|
|
#else
|
|
f.close();
|
|
EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
|
|
#endif
|
|
}
|
|
|
|
TEST(FileTest, MaxSize) {
|
|
write_file("test", "");
|
|
file f("test", file::RDONLY);
|
|
fstat_sim = MAX_SIZE;
|
|
EXPECT_GE(f.size(), 0);
|
|
EXPECT_EQ(max_file_size(), f.size());
|
|
fstat_sim = NONE;
|
|
}
|
|
|
|
TEST(FileTest, ReadRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
enum { SIZE = 4 };
|
|
write_end.write("test", SIZE);
|
|
write_end.close();
|
|
char buffer[SIZE];
|
|
std::size_t count = 0;
|
|
EXPECT_RETRY(count = read_end.read(buffer, SIZE),
|
|
read, "cannot read from file");
|
|
EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
|
|
}
|
|
|
|
TEST(FileTest, WriteRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
enum { SIZE = 4 };
|
|
std::size_t count = 0;
|
|
EXPECT_RETRY(count = write_end.write("test", SIZE),
|
|
write, "cannot write to file");
|
|
write_end.close();
|
|
#ifndef _WIN32
|
|
EXPECT_EQ(static_cast<std::streamsize>(SIZE), count);
|
|
char buffer[SIZE + 1];
|
|
read_end.read(buffer, SIZE);
|
|
buffer[SIZE] = '\0';
|
|
EXPECT_STREQ("test", buffer);
|
|
#endif
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
TEST(FileTest, ConvertReadCount) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
char c;
|
|
std::size_t size = UINT_MAX;
|
|
if (sizeof(unsigned) != sizeof(std::size_t))
|
|
++size;
|
|
read_count = 1;
|
|
read_nbyte = 0;
|
|
EXPECT_THROW(read_end.read(&c, size), fmt::system_error);
|
|
read_count = 0;
|
|
EXPECT_EQ(UINT_MAX, read_nbyte);
|
|
}
|
|
|
|
TEST(FileTest, ConvertWriteCount) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
char c;
|
|
std::size_t size = UINT_MAX;
|
|
if (sizeof(unsigned) != sizeof(std::size_t))
|
|
++size;
|
|
write_count = 1;
|
|
write_nbyte = 0;
|
|
EXPECT_THROW(write_end.write(&c, size), fmt::system_error);
|
|
write_count = 0;
|
|
EXPECT_EQ(UINT_MAX, write_nbyte);
|
|
}
|
|
#endif
|
|
|
|
TEST(FileTest, DupNoRetry) {
|
|
int stdout_fd = FMT_POSIX(fileno(stdout));
|
|
dup_count = 1;
|
|
EXPECT_SYSTEM_ERROR(file::dup(stdout_fd), EINTR,
|
|
fmt::format("cannot duplicate file descriptor {}", stdout_fd));
|
|
dup_count = 0;
|
|
}
|
|
|
|
TEST(FileTest, Dup2Retry) {
|
|
int stdout_fd = FMT_POSIX(fileno(stdout));
|
|
file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
|
|
EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
|
|
fmt::format("cannot duplicate file descriptor {} to {}",
|
|
f1.descriptor(), f2.descriptor()));
|
|
}
|
|
|
|
TEST(FileTest, Dup2NoExceptRetry) {
|
|
int stdout_fd = FMT_POSIX(fileno(stdout));
|
|
file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
|
|
error_code ec;
|
|
dup2_count = 1;
|
|
f1.dup2(f2.descriptor(), ec);
|
|
#ifndef _WIN32
|
|
EXPECT_EQ(4, dup2_count);
|
|
#else
|
|
EXPECT_EQ(EINTR, ec.get());
|
|
#endif
|
|
dup2_count = 0;
|
|
}
|
|
|
|
TEST(FileTest, PipeNoRetry) {
|
|
file read_end, write_end;
|
|
pipe_count = 1;
|
|
EXPECT_SYSTEM_ERROR(
|
|
file::pipe(read_end, write_end), EINTR, "cannot create pipe");
|
|
pipe_count = 0;
|
|
}
|
|
|
|
TEST(FileTest, FdopenNoRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
fdopen_count = 1;
|
|
EXPECT_SYSTEM_ERROR(read_end.fdopen("r"),
|
|
EINTR, "cannot associate stream with file descriptor");
|
|
fdopen_count = 0;
|
|
}
|
|
|
|
TEST(BufferedFileTest, OpenRetry) {
|
|
write_file("test", "there must be something here");
|
|
scoped_ptr<buffered_file> f{FMT_NULL};
|
|
EXPECT_RETRY(f.reset(new buffered_file("test", "r")),
|
|
fopen, "cannot open file test");
|
|
#ifndef _WIN32
|
|
char c = 0;
|
|
if (fread(&c, 1, 1, f->get()) < 1)
|
|
throw fmt::system_error(errno, "fread failed");
|
|
#endif
|
|
}
|
|
|
|
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
scoped_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
|
|
int saved_fclose_count = 0;
|
|
EXPECT_WRITE(stderr, {
|
|
fclose_count = 1;
|
|
f.reset(FMT_NULL);
|
|
saved_fclose_count = fclose_count;
|
|
fclose_count = 0;
|
|
}, format_system_error(EINTR, "cannot close file") + "\n");
|
|
EXPECT_EQ(2, saved_fclose_count);
|
|
}
|
|
|
|
TEST(BufferedFileTest, CloseNoRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
buffered_file f = read_end.fdopen("r");
|
|
fclose_count = 1;
|
|
EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
|
|
EXPECT_EQ(2, fclose_count);
|
|
fclose_count = 0;
|
|
}
|
|
|
|
TEST(BufferedFileTest, FilenoNoRetry) {
|
|
file read_end, write_end;
|
|
file::pipe(read_end, write_end);
|
|
buffered_file f = read_end.fdopen("r");
|
|
fileno_count = 1;
|
|
EXPECT_SYSTEM_ERROR((f.fileno)(), EINTR, "cannot get file descriptor");
|
|
EXPECT_EQ(2, fileno_count);
|
|
fileno_count = 0;
|
|
}
|
|
|
|
struct TestMock {
|
|
static TestMock *instance;
|
|
} *TestMock::instance;
|
|
|
|
TEST(ScopedMock, Scope) {
|
|
{
|
|
ScopedMock<TestMock> mock;
|
|
EXPECT_EQ(&mock, TestMock::instance);
|
|
TestMock © = mock;
|
|
static_cast<void>(copy);
|
|
}
|
|
EXPECT_EQ(FMT_NULL, TestMock::instance);
|
|
}
|
|
|
|
#ifdef FMT_LOCALE
|
|
|
|
typedef fmt::Locale::Type LocaleType;
|
|
|
|
struct LocaleMock {
|
|
static LocaleMock *instance;
|
|
MOCK_METHOD3(newlocale, LocaleType (int category_mask, const char *locale,
|
|
LocaleType base));
|
|
MOCK_METHOD1(freelocale, void (LocaleType locale));
|
|
|
|
MOCK_METHOD3(strtod_l, double (const char *nptr, char **endptr,
|
|
LocaleType locale));
|
|
} *LocaleMock::instance;
|
|
|
|
#ifdef _MSC_VER
|
|
# pragma warning(push)
|
|
# pragma warning(disable: 4273)
|
|
|
|
_locale_t _create_locale(int category, const char *locale) {
|
|
return LocaleMock::instance->newlocale(category, locale, 0);
|
|
}
|
|
|
|
void _free_locale(_locale_t locale) {
|
|
LocaleMock::instance->freelocale(locale);
|
|
}
|
|
|
|
double _strtod_l(const char *nptr, char **endptr, _locale_t locale) {
|
|
return LocaleMock::instance->strtod_l(nptr, endptr, locale);
|
|
}
|
|
# pragma warning(pop)
|
|
#endif
|
|
|
|
#if defined(__THROW) && FMT_GCC_VERSION > 0 && FMT_GCC_VERSION <= 408
|
|
#define FMT_LOCALE_THROW __THROW
|
|
#else
|
|
#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;
|
|
#else
|
|
typedef void FreeLocaleResult;
|
|
#endif
|
|
|
|
FreeLocaleResult freelocale(LocaleType locale) FMT_LOCALE_THROW {
|
|
LocaleMock::instance->freelocale(locale);
|
|
return FreeLocaleResult();
|
|
}
|
|
|
|
double strtod_l(const char *nptr, char **endptr, LocaleType locale) FMT_LOCALE_THROW {
|
|
return LocaleMock::instance->strtod_l(nptr, endptr, locale);
|
|
}
|
|
|
|
#undef FMT_LOCALE_THROW
|
|
|
|
TEST(LocaleTest, LocaleMock) {
|
|
ScopedMock<LocaleMock> mock;
|
|
LocaleType locale = reinterpret_cast<LocaleType>(11);
|
|
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
|
|
newlocale(222, "foo", locale);
|
|
}
|
|
|
|
TEST(LocaleTest, Locale) {
|
|
#ifndef LC_NUMERIC_MASK
|
|
enum { LC_NUMERIC_MASK = LC_NUMERIC };
|
|
#endif
|
|
ScopedMock<LocaleMock> mock;
|
|
LocaleType impl = reinterpret_cast<LocaleType>(42);
|
|
EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), FMT_NULL))
|
|
.WillOnce(Return(impl));
|
|
EXPECT_CALL(mock, freelocale(impl));
|
|
fmt::Locale locale;
|
|
EXPECT_EQ(impl, locale.get());
|
|
}
|
|
|
|
TEST(LocaleTest, Strtod) {
|
|
ScopedMock<LocaleMock> mock;
|
|
EXPECT_CALL(mock, newlocale(_, _, _))
|
|
.WillOnce(Return(reinterpret_cast<LocaleType>(42)));
|
|
EXPECT_CALL(mock, freelocale(_));
|
|
fmt::Locale locale;
|
|
const char *str = "4.2";
|
|
char end = 'x';
|
|
EXPECT_CALL(mock, strtod_l(str, _, locale.get()))
|
|
.WillOnce(testing::DoAll(testing::SetArgPointee<1>(&end), Return(777)));
|
|
EXPECT_EQ(777, locale.strtod(str));
|
|
EXPECT_EQ(&end, str);
|
|
}
|
|
|
|
#endif // FMT_LOCALE
|