dynarmic/test/posix-mock-test.cc
Lioncash d4c6fa3122 Squashed 'externals/fmt/' changes from 9bdd1596c..cd4af11ef
cd4af11ef Update version
1ebc2f7cc Bump version
f4c997062 Fix changelog
72920ba30 Update changelog
0907c08ae Fix handling of default alignmment with locale (#1801)
37c8f4eaf Don't use 128 bit integers with clang-cl (#1800)
eaaaec999 Workaround a bug in msvc
ccf8561cb Workaround broken numeric_limites, part 2 (#1787)
0cc73ebf7 Report error on missing named argument (#1796)
33efc3c94 Fix handling of iterators in locale-specific formatting (#1782)
b9d749095 Update version
86b63bb71 Bump version
cbf6be960 Update changelog
229ee9b46 Workaround broken numeric_limits (#1725)
2b7a146fa Fix a regression in handling digit separators (#1782)
89d0c7124 Fix compatibility with CMake 3.4 (#1779)
f19b1a521 Update version
5c67fefb2 Fix a changelog entry
1d2a556e1 Fix undefined reference error
04c9b62fb Merge release branch
6be6762e5 Fix date
f1dd2eb3c Bump version
fbf3b943c Workaround a bug in gcc
a29a01d30 Fix docs
9f0b3afb7 Bump version in namespace
86b2f99f8 Fix the docs
c472ff12d Update version
5173a76ba Update version
1614af352 Minor corrections in the changelog
569a9b3a7 Bump version
4e7e3c65a Update docs
0f7a6bfa1 Add a section on std::format compatibility
4faec5a5e Update README.rst
7dbc8ac71 Update changelog
c87dd746f Update changelog
372175caf Revert changelog changes
904754876 Add ClickHouse to the list of projects (#1751)
d30bca64e Revert changelog conversion since GFM is not supported there
d6047cdc4 Update changelog
810241b36 Convert changlog to markdown
661c47473 Rename changelog
7c33059fa Update ChangeLog.rst
9e20883ab Update README.rst
41899d522 Update changelog
f42f45908 Update changelog
2381df654 Update readme
7ae816563 Update README.rst
c56cf3d07 Update changelog and readme
01309a34a Deprecate arg_formatter
a62d06055 Update changelog
23e3a2eee Update changelog
d8e0554b9 Disable numeric formatting by default
1e8eea4f4 Update changelog
44bd5384a Fix formatting
20e19387a Update changelog
56fed7814 FMT_NUMERIC_ALIGN -> FMT_DEPRECATED_NUMERIC_ALIGN
56e63078f Make the n specifier an opt-in
31ce6bc70 Fix a conversion warning with Clang10 on Windows (#1750)
c9c5b90da Fix a typo. Thanks Tracy Chapman from TripleChecker
1f3f84631 Fix a typo
5de62af60 Fix possible infinite recursion in FMT_ASSERT (#1744)
cbddab2fe Use consistent include style
f69b6eaab Add a simple buffered stream with no sync
ba363b3a2 Use digit pairs as in unrolledlut
a6f8e7d86 Update changelog
e753244ab Update changelog
98a7a8b40 Update changelog and disable internal
3135d95fd Don't use non-portable attribute
8630a8f5f Tweak the docs
cc3a88e6b Extract docs from compile.h
79c4b6bd7 Apply clang-format
d130ee070 Document format string compilation
d0f90b5be Spelling fixes
6e080660d Update README.rst
31c3a2426 Spelling fixes
613b3b459 Spelling fixes
978521bb8 Fix a compile error introduced in #1738
4e94c649f Deprecate compile
1a83443e6 Add user-defined type support to compilation
8bef1c3b3 Tweaks for EDG based compilers (Intel, nVidia, MCST/Elbrus, etc).
b287c37c6 Do not use -Wl,--as-needed with emscripten.
2cac8a9d2 Reintroduce UDT support to fmt::to_string and test ADL
9a4cc8842 Add FMT_COMPILE support to format_to
5ddf9ee1b Streamline default FP formatting
0b3a83f7f Update README.rst
5aa5c9873 Added  #define WIN32_LEAN_AND_MEAN before including windows.h (#1729)
397ad1bec Optimize common case
7431165f3 Make to_string bypass format
ee4d4c7fd Inline compiled format
ab2f8484e Finish text::format
e900d735b Re-enable assert in format_decimal
f4de7b684 Fix ambiguity
1f8f5450b Reuse format_decimal
d702a68df Fix formatting of bool with FMT_COMPILE and add more tests
e956a14e9 Use write instead of format_int in to_string
98dcc251e Undo branching reduction
5b8641ddd Undo branching reduction
8c88abde6 Fix sign handling in 'L'
23b976a61 Reduce branching
9edee0e72 Optimize small string parsing
a909d42b7 Fix a warning
16637341b Enable compilation for all types
2d71d7e03 Add a simple format string compilation API
d259fcfb0 Tweak comments
704ed557a Move project in order to solve a CMake warning
8603bd20d Update README.rst
547f12ae6 Fix a warning (#1722)
f904e8a1b c++11 use formatting user-defined types (#1721)
100e8af08 Update README.rst
c11d0f056 Update README.rst
2453ee576 Improve default formatting
47ae52155 MINGW cross compiler fixes
936a1833c Add default_arg_formatter
f2c9cb624 Fix a UB
d3107f855 Cleanup arg_formatter_base
5e7c70e20 Simplify arg_formatter_base
38cc68b3e Inline visitor
6732ea500 Make symbols readable
57ddc77ce Make advance_to a noop for back_insert_iterator
50bad7d62 Optimize format string parsing
8f7a824e4 Inline visit
f11e96870 Optimize format string parsing
09737dd83 Optimize format handler
d9e3d6e6e Move format_handler to detail
795b47a7b Fix a warning (#1712)
95c6ac0cc fix typo which caused the loss of the counting information when using a printf context with a truncating_iterator
21409cfdd Fix warnings
88c8d534e Move digits10 to where they belong and add comments
0f3eaeac0 Fix a warning
344218510 Ignore /doc/node_modules directory
16aec0617 Cleanup arg_formatter_base
1e1193590 Fix format_decimal overloads
0893c9c2e Inline parse_format_string
3245145a4 Remove undocumented buffer_range and output_range
57fc44907 Increase VM disk size
7d22bebb6 Remove uses of buffer_range
8f2b5fe74 Don't install sphinx cache files
f095c67b6 Remove uses of buffer_range
5aabf1f71 Simplify copy_str
19c5b5d15 Simplify arg_formatter
519571ede Simplify arg_formatter_base
ac8dfd841 Improve handling of separators
2c6165a22 Reduce the number of comparisons
28639969e Use memcpy for copying digits
f5fa1dee5 Support custom FMT_INC_DIR in pkgconfig and cmake configs (#1702)
51bf9cfac Fix Mingw support
1a716caf5 Optimize common case
98d4bbf81 Update README.rst
8c8f74a87 fix zero flag for char types and make zero flag ignored if a precision is specified
bc1b89da2 Temporarily revert parsing changes
a7fb321ac Remove a redundant branch
8cadb9650 fix max/min macro (#1697)
297c3b2ed Fix an example (thanks Alexey Kuzmenko)
943532fec Make ostream formatter work with compile-time format strings (#1692)
bd8804019 Update README.rst
f230300ac Knuth is using fmt library (#1691)
a265e25b7 Optimize small string parsing
2aa2526f6 Optimize small string concatenation
8d78045e7 Move void_t to where it's used
7aafa6bc6 Update analytics
c66aae165 Adding sentinel support to fmt::join(). (#1689)
6d66de380 Add c specifier support to integral types (#1652)
6b219a58d fix interaction of space flag and '+' flag, as well as '-' flag and '0' flag (#1687)
eee2023c2 Update signatures
c5ed73aab Add fmt::detail::buffer to the docs (#704)
ea1cd9638 Fix apidoc
d3964d7b1 Merge branch 'master' of github.com:fmtlib/fmt
d18c6723a Update docs
96c18b26c make plus flag for printf not be ignored for char argument (#1683)
ba25baeb9 Apply doc patch to 6.2.1
981b517cc nested replacement fields may omit arg_id (#1681)
922ea924b Make dynamic_format_arg_store reusable and add reserve() (#1677)
e0d98923c Update version
806926537 internal -> detail (#1538)
963ee0831 Simplify named arguments
02a6fe59f Named arguments go brrr
de290f5c4 Ditch internal::arg_map
d0623de51 Bump version
73e335ed3 Make implicit capture explicit for C++20 (#1669)
b4d46e398 Update changelog
a182f7341 Update changelog
68201831a Support named args in dynamic_format_arg_store (#1655). (#1663)
7f723fbcb Consistently namespace qualify size_t
c06851456 Purge basic_writer
2f05054dd Purge basic_writer
f0ce21164 Revert enum change
44639b11f Fix some warnings (#1667)
1c86a99e8 Purge basic_writer
8f511fc12 Make copyfmt not throw (#1666)
59fe455f3 Remove compatibility stubs
b0f47a13e Separate nonfinite formatting
d6cea50d0 Remove deprecated APIs
40bc7163f Move FMT_MAYBE_UNUSED to where it's actually used
080e44d0b Fix inconsistent type detection (#1662)
7e57cace5 Exclude std::abort from compilation when compiling CUDA with Clang (#1661)
7b66e2f21 Inherit arg_formatter_base from basic_writer
bab3f5800 Refactor pointer formatting
9cc7edfdd Move int_writer to the namespace scope
8d9d528bf Improve handling of alignment
8efd1a8ef Improve handling of alignment
a71bc9c82 Use '0' fill with numeric align for consistency with std::format
60d85d598 Suppress ubsan warning
c3099beb6 Cleanup
cbb4cb899 Remove undocumented deprecated APIs
b85e9ac38 Simplify vformat_to
e3710ab97 FMT_CONSTEXPR -> constexpr
d59751f0f Update date formatting example to use threadsafe localtime
d6abb2fa0 Reduce library size
e9fdea90b Update README.rst
44b6584f2 Update README.rst
78f041ab5 build: Fix installation paths
7ca89bf87 Reduce template bloat in write_int
3c114d091 Fix a shadowing warning (#1658)
e2ef12a8c Allow to avoid inclusion of os.cc in fmt target
bca82719a Pass iterator by value
99da38962 Make write_padded non-members
f19d66794 Bump fuzzer allocation limit
3e6984761 Reduce branching in write_padded
9ac1eebd4 Reduce library size
e2ff91067 Replace FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  with fmt-specific macro (#1650)
f2ed03b91 Fix a warning (#1649)
9dde9f013 Reduce library size
b1af642d1 Reduce library size
4a617f25c Clarify encoding conversion in chrono
6f435f55c Improve compile time by using extern template (#1452)
cb475cb88 Clarify why we don't check argument id
1e1ac6e96 Check dynamic width/precision id at compile time (#1614)
e51c449fe Revert "Check dynamic widht/precision id at compile time (#1614)"
0463665ef Don't access a C string past precision in printf (#1595)
7d748a6f8 Check dynamic widht/precision id at compile time (#1614)
2b75bd7ce Get rid of do_check_format_string
4a1d5931c Simplify udl_formatter with FMT_STRING
811b0f905 Enable compile-time error tests
450e8eed9 Fix markup
b8fbcec1b Clarify formatter reuse
56bc86ffa Suppress bogus MSVC analysis warnings
3f79357ef Fix a recent regression in handling max packed arguments
8a11148f9 Add Facebook Folly to the list of projects
e371e8b68 Tweak readme
813732fed Improve readme formatting
3670d5b3f README: add vectorized.io/redpanda in the list of users
9e2ad7cf6 Add windows terminal to the projects using {fmt}
63479c851 Use a delegating ctor and add inlines
5944fcad3 Remove remaining wchar_t instantiation
e253b371b Don't generate RTTI for allocator
0c86f467b Fix build on ancient gcc
1929df4bc Simplify format_args
a13822181 Always inline arg_data functions
04e0dfd4b Always inline value ctors
04cde756b Simplify checks
c9a57b9a8 Fix incorrect assumptions about nul termination
f46f5ecaf Reenable constexpr _compile on GCC 9
6e8d7e277 Don't use constexpr on Intel compiler (#1628)
567ed03f8 Merge arg overloads and cleanup
c3fa33314 Remove warning in core.h with when compiling with gcc and -Wshadow
84898b462 Remove warning in format.h when compiling with gcc and -Wshadow
538d83fd0 Cleanup named arguments
8a4630686 Improve handling of named arguments
a9d62d3f3 Add check for CompiledFormat to avoid ambiguous call
fdcf7870a Add stack-based named argument storage
5899267c4 Fix a clang-tidy warning
07b4c246e Fix a typo
e99809f29 Fix ostream support in sprintf (#1631)
3cd5179f3 Fixed clang tidy warning -multiple declarations in a single statement reduces readability
7404e33a7 Fix clang warning about explicit ctor
3aab2171e Clean up basic_format_args
7645ca072 Clean up printf
e30d8391e Suppress an MSVC warning (#1622)
8cd8ef03e Simplify warning suppression
bbb6b357c Add floating-point L specifier (#1624)
36ea32640 Suppress a bogus MSVC warning
141a00d64 Define FMT_EXTERN_TEMPLATE_API on export
3860edc5d Bump version
7d01859ef Fix handling of unsigned char strings in printf
63b23e786 Merge branch 'master' of github.com:fmtlib/fmt
4999796c1 Fix the docs
34b3f7b7a Avoid windows issue with min() max() macros
27e3c0fe9 Update signature in the docs

git-subtree-dir: externals/fmt
git-subtree-split: cd4af11efc9c622896a3e4cb599fa28668ca3d05
2020-09-19 14:25:26 -04:00

558 lines
14 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.
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#include "posix-mock.h"
#include <errno.h>
#include <fcntl.h>
#include <climits>
#include <memory>
#include "../src/os.cc"
#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 testing::_;
using testing::Return;
using testing::StrEq;
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;
size_t read_nbyte;
size_t write_nbyte;
bool sysconf_error;
enum { NONE, MAX_SIZE, ERROR } fstat_sim;
} // namespace
#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, nullptr);
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, nullptr);
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);
}
#if FMT_USE_FCNTL
using fmt::file;
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("temp", "there must be something here");
std::unique_ptr<file> f{nullptr};
EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open,
"cannot open file temp");
# ifndef _WIN32
char c = 0;
f->read(&c, 1);
# endif
}
TEST(FileTest, CloseNoRetryInDtor) {
file read_end, write_end;
file::pipe(read_end, write_end);
std::unique_ptr<file> 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_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("temp", content);
file f("temp", 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::detail::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("temp", "");
file f("temp", 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];
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 };
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;
size_t size = UINT_MAX;
if (sizeof(unsigned) != sizeof(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;
size_t size = UINT_MAX;
if (sizeof(unsigned) != sizeof(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("temp", "there must be something here");
std::unique_ptr<buffered_file> f{nullptr};
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)
throw fmt::system_error(errno, "fread failed");
# endif
}
TEST(BufferedFileTest, CloseNoRetryInDtor) {
file read_end, write_end;
file::pipe(read_end, write_end);
std::unique_ptr<buffered_file> 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_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;
}
#endif // FMT_USE_FCNTL
struct test_mock {
static test_mock* instance;
} * test_mock::instance;
TEST(ScopedMock, Scope) {
{
ScopedMock<test_mock> mock;
EXPECT_EQ(&mock, test_mock::instance);
test_mock& copy = mock;
static_cast<void>(copy);
}
EXPECT_EQ(nullptr, test_mock::instance);
}
#ifdef FMT_LOCALE
typedef fmt::locale::type locale_type;
struct locale_mock {
static locale_mock* instance;
MOCK_METHOD3(newlocale, locale_type(int category_mask, const char* locale,
locale_type base));
MOCK_METHOD1(freelocale, void(locale_type locale));
MOCK_METHOD3(strtod_l,
double(const char* nptr, char** endptr, locale_type locale));
} * locale_mock::instance;
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4273)
# ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Winconsistent-dllimport"
# endif
_locale_t _create_locale(int category, const char* locale) {
return locale_mock::instance->newlocale(category, locale, 0);
}
void _free_locale(_locale_t locale) {
locale_mock::instance->freelocale(locale);
}
double _strtod_l(const char* nptr, char** endptr, _locale_t locale) {
return locale_mock::instance->strtod_l(nptr, endptr, locale);
}
# ifdef __clang__
# pragma clang diagnostic pop
# endif
# 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
# if defined(__APPLE__) || \
(defined(__FreeBSD__) && __FreeBSD_version < 1200002)
typedef int FreeLocaleResult;
# else
typedef void FreeLocaleResult;
# endif
FreeLocaleResult freelocale(locale_type locale) FMT_LOCALE_THROW {
locale_mock::instance->freelocale(locale);
return FreeLocaleResult();
}
double strtod_l(const char* nptr, char** endptr,
locale_type locale) FMT_LOCALE_THROW {
return locale_mock::instance->strtod_l(nptr, endptr, locale);
}
# undef FMT_LOCALE_THROW
# ifndef _WIN32
locale_t test::newlocale(int category_mask, const char* locale, locale_t base) {
return locale_mock::instance->newlocale(category_mask, locale, base);
}
TEST(LocaleTest, LocaleMock) {
ScopedMock<locale_mock> mock;
locale_type locale = reinterpret_cast<locale_type>(11);
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
FMT_SYSTEM(newlocale(222, "foo", locale));
}
# endif
TEST(LocaleTest, Locale) {
# ifndef LC_NUMERIC_MASK
enum { LC_NUMERIC_MASK = LC_NUMERIC };
# endif
ScopedMock<locale_mock> mock;
locale_type impl = reinterpret_cast<locale_type>(42);
EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), nullptr))
.WillOnce(Return(impl));
EXPECT_CALL(mock, freelocale(impl));
fmt::locale loc;
EXPECT_EQ(impl, loc.get());
}
TEST(LocaleTest, Strtod) {
ScopedMock<locale_mock> mock;
EXPECT_CALL(mock, newlocale(_, _, _))
.WillOnce(Return(reinterpret_cast<locale_type>(42)));
EXPECT_CALL(mock, freelocale(_));
fmt::locale loc;
const char* str = "4.2";
char end = 'x';
EXPECT_CALL(mock, strtod_l(str, _, loc.get()))
.WillOnce(testing::DoAll(testing::SetArgPointee<1>(&end), Return(777)));
EXPECT_EQ(777, loc.strtod(str));
EXPECT_EQ(&end, str);
}
#endif // FMT_LOCALE